MSPUBCollector.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /*
00003  * This file is part of the libmspub project.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this
00007  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
00008  */
00009 
00010 #ifndef __MSPUBCOLLECTOR_H__
00011 #define __MSPUBCOLLECTOR_H__
00012 
00013 #include <list>
00014 #include <vector>
00015 #include <map>
00016 #include <set>
00017 #include <string>
00018 #include <algorithm>
00019 
00020 #include <boost/ptr_container/ptr_vector.hpp>
00021 #include <boost/bind.hpp>
00022 #include <boost/function.hpp>
00023 
00024 #include <librevenge/librevenge.h>
00025 #include <librevenge/librevenge.h>
00026 
00027 #include "MSPUBTypes.h"
00028 #include "libmspub_utils.h"
00029 #include "MSPUBContentChunkType.h"
00030 #include "ShapeType.h"
00031 #include "Coordinate.h"
00032 #include "ShapeGroupElement.h"
00033 #include "Fill.h"
00034 #include "ColorReference.h"
00035 #include "PolygonUtils.h"
00036 #include "ShapeInfo.h"
00037 #include "BorderArtInfo.h"
00038 #include "Dash.h"
00039 #include "Arrow.h"
00040 #include "VerticalAlign.h"
00041 #include "EmbeddedFontInfo.h"
00042 #include "Shadow.h"
00043 
00044 namespace libmspub
00045 {
00046 class MSPUBCollector
00047 {
00048   friend class Fill;
00049   friend class ImgFill;
00050   friend class SolidFill;
00051   friend class GradientFill;
00052   friend class PatternFill;
00053 public:
00054   typedef std::list<ContentChunkReference>::const_iterator ccr_iterator_t;
00055 
00056   MSPUBCollector(::librevenge::RVNGDrawingInterface *painter);
00057   virtual ~MSPUBCollector();
00058 
00059   // collector functions
00060   void collectMetaData(const librevenge::RVNGPropertyList &metaData);
00061 
00062   bool addPage(unsigned seqNum);
00063   bool addTextString(const std::vector<TextParagraph> &str, unsigned id);
00064   void addTextShape(unsigned stringId, unsigned seqNum);
00065   bool addImage(unsigned index, ImgType type, librevenge::RVNGBinaryData img);
00066   void setBorderImageOffset(unsigned index, unsigned offset);
00067   librevenge::RVNGBinaryData *addBorderImage(ImgType type, unsigned borderArtIndex);
00068   void setShapePage(unsigned seqNum, unsigned pageSeqNum);
00069 
00070   void setNextPage(unsigned seqNum);
00071 
00072   void setShapeType(unsigned seqNum, ShapeType type);
00073   void setShapeCropType(unsigned seqNum, ShapeType cropType);
00074   void setShapePictureRecolor(unsigned seqNum, const ColorReference &recolor);
00075   void setShapePictureBrightness(unsigned seqNum, int brightness);
00076   void setShapePictureContrast(unsigned seqNum, int contrast);
00077   void setShapeTableInfo(unsigned seqNum, const TableInfo &ti);
00078   void setShapeBorderImageId(unsigned seqNum, unsigned borderImageId);
00079   void setShapeCoordinatesInEmu(unsigned seqNum, int xs, int ys, int xe, int ye);
00080   void setShapeImgIndex(unsigned seqNum, unsigned index);
00081   void setShapeFill(unsigned seqNum, boost::shared_ptr<Fill> fill, bool skipIfNotBg);
00082   void setShapeDash(unsigned seqNum, const Dash &dash);
00083   void setAdjustValue(unsigned seqNum, unsigned index, int adjust);
00084   void setShapeRotation(unsigned seqNum, double rotation);
00085   void setShapeFlip(unsigned, bool, bool);
00086   void setShapeMargins(unsigned seqNum, unsigned left, unsigned top, unsigned right, unsigned bottom);
00087   void setShapeBorderPosition(unsigned seqNum, BorderPosition pos);
00088   void setShapeCoordinatesRotated90(unsigned seqNum);
00089   void setShapeCustomPath(unsigned seqNum,
00090                           const DynamicCustomShape &shape);
00091   void setShapeClipPath(unsigned seqNum, const std::vector<libmspub::Vertex> &clip);
00092   void setShapeVerticalTextAlign(unsigned seqNum, VerticalAlign va);
00093   void designateMasterPage(unsigned seqNum);
00094   void setMasterPage(unsigned pageSeqNum, unsigned masterSeqNum);
00095   void setShapeStretchBorderArt(unsigned seqNum);
00096   void setShapeShadow(unsigned seqNum, const Shadow &shadow);
00097 
00098   // Microsoft "Embedded OpenType" ... need to figure out how to convert
00099   // this to a sane format and how to get LibreOffice to understand embedded fonts.
00100   void addEOTFont(const librevenge::RVNGString &name, const librevenge::RVNGBinaryData &data);
00101 
00102   void beginGroup();
00103   bool endGroup();
00104 
00105   void setShapeLineBackColor(unsigned seqNum, ColorReference backColor);
00106   void addShapeLine(unsigned seqNum, Line line);
00107   void setShapeOrder(unsigned seqNum);
00108   void setPageBgShape(unsigned pageSeqNum, unsigned seqNum);
00109   void setWidthInEmu(unsigned long);
00110   void setHeightInEmu(unsigned long);
00111   void setShapeNumColumns(unsigned seqNum, unsigned numColumns);
00112   void setShapeColumnSpacing(unsigned seqNum, unsigned spacing);
00113   void setShapeBeginArrow(unsigned seqNum, const Arrow &arrow);
00114   void setShapeEndArrow(unsigned seqNum, const Arrow &arrow);
00115 
00116   void addTextColor(ColorReference c);
00117   void addFont(std::vector<unsigned char> name);
00118 
00119   void addDefaultCharacterStyle(const CharacterStyle &style);
00120   void addDefaultParagraphStyle(const ParagraphStyle &style);
00121   void addPaletteColor(Color);
00122   bool setCurrentGroupSeqNum(unsigned seqNum);
00123 
00124   void useEncodingHeuristic();
00125 
00126   void setTableCellTextEnds(unsigned textId, const std::vector<unsigned> &ends);
00127   void setTextStringOffset(unsigned textId, unsigned offset);
00128 
00129   bool go();
00130 
00131   bool hasPage(unsigned seqNum) const;
00132 private:
00133 
00134   struct PageInfo
00135   {
00136     std::vector<ShapeGroupElement *> m_shapeGroupsOrdered;
00137     PageInfo() : m_shapeGroupsOrdered() { }
00138   };
00139 
00140   MSPUBCollector(const MSPUBCollector &);
00141   MSPUBCollector &operator=(const MSPUBCollector &);
00142 
00143   librevenge::RVNGDrawingInterface *m_painter;
00144   std::list<ContentChunkReference> m_contentChunkReferences;
00145   double m_width, m_height;
00146   bool m_widthSet, m_heightSet;
00147   unsigned short m_numPages;
00148   std::map<unsigned, std::vector<TextParagraph> > m_textStringsById;
00149   std::map<unsigned, PageInfo> m_pagesBySeqNum;
00150   std::vector<std::pair<ImgType, librevenge::RVNGBinaryData> > m_images;
00151   std::vector<BorderArtInfo> m_borderImages;
00152   std::vector<ColorReference> m_textColors;
00153   std::vector<std::vector<unsigned char> > m_fonts;
00154   std::vector<CharacterStyle> m_defaultCharStyles;
00155   std::vector<ParagraphStyle> m_defaultParaStyles;
00156   std::map<unsigned, ShapeType> m_shapeTypesBySeqNum;
00157   std::vector<Color> m_paletteColors;
00158   std::vector<unsigned> m_shapeSeqNumsOrdered;
00159   std::map<unsigned, unsigned> m_pageSeqNumsByShapeSeqNum;
00160   std::map<unsigned, unsigned> m_bgShapeSeqNumsByPageSeqNum;
00161   std::set<unsigned> m_skipIfNotBgSeqNums;
00162   ShapeGroupElement *m_currentShapeGroup;
00163   boost::ptr_vector<ShapeGroupElement> m_topLevelShapes;
00164   std::map<unsigned, ShapeGroupElement *> m_groupsBySeqNum;
00165   std::list<EmbeddedFontInfo> m_embeddedFonts;
00166   std::map<unsigned, ShapeInfo> m_shapeInfosBySeqNum;
00167   std::set<unsigned> m_masterPages;
00168   std::set<unsigned> m_shapesWithCoordinatesRotated90;
00169   std::map<unsigned, unsigned> m_masterPagesByPageSeqNum;
00170   std::map<unsigned, std::vector<unsigned> > m_tableCellTextEndsByTextId;
00171   std::map<unsigned, unsigned> m_stringOffsetsByTextId;
00172   mutable std::vector<bool> m_calculationValuesSeen;
00173   std::vector<unsigned> m_pageSeqNumsOrdered;
00174   bool m_encodingHeuristic;
00175   std::vector<unsigned char> m_allText;
00176   mutable boost::optional<const char *> m_calculatedEncoding;
00177   librevenge::RVNGPropertyList m_metaData;
00178 
00179   // helper functions
00180   std::vector<int> getShapeAdjustValues(const ShapeInfo &info) const;
00181   boost::optional<unsigned> getMasterPageSeqNum(unsigned pageSeqNum) const;
00182   void setRectCoordProps(Coordinate, librevenge::RVNGPropertyList *) const;
00183   boost::optional<std::vector<libmspub::TextParagraph> > getShapeText(const ShapeInfo &info) const;
00184   void setupShapeStructures(ShapeGroupElement &elt);
00185   void addBlackToPaletteIfNecessary();
00186   void assignShapesToPages();
00187   void writePage(unsigned pageSeqNum) const;
00188   void writePageShapes(unsigned pageSeqNum) const;
00189   void writePageBackground(unsigned pageSeqNum) const;
00190   void writeImage(double x, double y, double height, double width,
00191                   ImgType type, const librevenge::RVNGBinaryData &blob,
00192                   boost::optional<Color> oneBitColor) const;
00193   bool pageIsMaster(unsigned pageSeqNum) const;
00194 
00195   boost::function<void(void)> paintShape(const ShapeInfo &info, const Coordinate &relativeTo, const VectorTransformation2D &foldedTransform, bool isGroup, const VectorTransformation2D &thisTransform) const;
00196   double getCalculationValue(const ShapeInfo &info, unsigned index, bool recursiveEntry, const std::vector<int> &adjustValues) const;
00197 
00198   librevenge::RVNGPropertyList getCharStyleProps(const CharacterStyle &, boost::optional<unsigned> defaultCharStyleIndex) const;
00199   librevenge::RVNGPropertyList getParaStyleProps(const ParagraphStyle &, boost::optional<unsigned> defaultParaStyleIndex) const;
00200   double getSpecialValue(const ShapeInfo &info, const CustomShape &shape, int arg, const std::vector<int> &adjustValues) const;
00201   void ponderStringEncoding(const std::vector<TextParagraph> &str);
00202   const char *getCalculatedEncoding() const;
00203 public:
00204   static librevenge::RVNGString getColorString(const Color &);
00205 };
00206 } // namespace libmspub
00207 
00208 #endif /* __MSPUBCOLLECTOR_H__ */
00209 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */