MSPUBCollector.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* libmspub
00003  * Version: MPL 1.1 / GPLv2+ / LGPLv2+
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License or as specified alternatively below. You may obtain a copy of
00008  * the License at http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * Major Contributor(s):
00016  * Copyright (C) 2012 Brennan Vincent <brennanv@email.arizona.edu>
00017  * Copyright (C) 2012 Fridrich Strba <fridrich.strba@bluewin.ch>
00018  *
00019  * All Rights Reserved.
00020  *
00021  * For minor contributions see the git repository.
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPLv2+"), or
00025  * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
00026  * in which case the provisions of the GPLv2+ or the LGPLv2+ are applicable
00027  * instead of those above.
00028  */
00029 
00030 #ifndef __MSPUBCOLLECTOR_H__
00031 #define __MSPUBCOLLECTOR_H__
00032 
00033 #include <list>
00034 #include <vector>
00035 #include <map>
00036 #include <set>
00037 #include <string>
00038 #include <algorithm>
00039 
00040 #include <boost/ptr_container/ptr_map.hpp>
00041 #include <boost/ptr_container/ptr_vector.hpp>
00042 #include <boost/bind.hpp>
00043 #include <boost/function.hpp>
00044 
00045 #include <libwpd/libwpd.h>
00046 #include <libwpg/libwpg.h>
00047 
00048 #include "MSPUBTypes.h"
00049 #include "libmspub_utils.h"
00050 #include "MSPUBContentChunkType.h"
00051 #include "ShapeType.h"
00052 #include "Coordinate.h"
00053 #include "ShapeGroupElement.h"
00054 #include "Fill.h"
00055 #include "ColorReference.h"
00056 #include "PolygonUtils.h"
00057 #include "ShapeInfo.h"
00058 
00059 namespace libmspub
00060 {
00061 class MSPUBCollector
00062 {
00063   friend class Fill;
00064   friend class ImgFill;
00065   friend class SolidFill;
00066   friend class GradientFill;
00067   friend class PatternFill;
00068 public:
00069   typedef std::list<ContentChunkReference>::const_iterator ccr_iterator_t;
00070 
00071   MSPUBCollector(::libwpg::WPGPaintInterface *painter);
00072   virtual ~MSPUBCollector();
00073 
00074   // collector functions
00075   bool addPage(unsigned seqNum);
00076   bool addTextString(const std::vector<TextParagraph> &str, unsigned id);
00077   void addTextShape(unsigned stringId, unsigned seqNum, unsigned pageSeqNum);
00078   bool addImage(unsigned index, ImgType type, WPXBinaryData img);
00079   bool addShape(unsigned seqNum);
00080   void setShapePage(unsigned seqNum, unsigned pageSeqNum);
00081 
00082   void setShapeType(unsigned seqNum, ShapeType type);
00083   void setShapeCoordinatesInEmu(unsigned seqNum, int xs, int ys, int xe, int ye);
00084   void setShapeImgIndex(unsigned seqNum, unsigned index);
00085   void setShapeFill(unsigned seqNum, boost::shared_ptr<Fill> fill, bool skipIfNotBg);
00086   void setAdjustValue(unsigned seqNum, unsigned index, int adjust);
00087   void setShapeRotation(unsigned seqNum, double rotation);
00088   void setShapeFlip(unsigned, bool, bool);
00089   void setShapeMargins(unsigned seqNum, unsigned left, unsigned top, unsigned right, unsigned bottom);
00090   void setShapeBorderPosition(unsigned seqNum, BorderPosition pos);
00091   void setShapeCoordinatesRotated90(unsigned seqNum);
00092 
00093   void beginGroup();
00094   bool endGroup();
00095 
00096   void addShapeLine(unsigned seqNum, Line line);
00097   void setShapeOrder(unsigned seqNum);
00098   void setPageBgShape(unsigned pageSeqNum, unsigned seqNum);
00099   void setWidthInEmu(unsigned long);
00100   void setHeightInEmu(unsigned long);
00101 
00102   void addTextColor(ColorReference c);
00103   void addFont(std::vector<unsigned char> name);
00104 
00105   void addDefaultCharacterStyle(const CharacterStyle &style);
00106   void addDefaultParagraphStyle(const ParagraphStyle &style);
00107   void addPaletteColor(Color);
00108   bool setCurrentGroupSeqNum(unsigned seqNum);
00109 
00110   bool go();
00111 
00112   bool hasPage(unsigned seqNum) const;
00113 private:
00114 
00115   struct PageInfo
00116   {
00117     std::vector<ShapeGroupElement *> m_shapeGroupsOrdered;
00118     PageInfo() : m_shapeGroupsOrdered() { }
00119   };
00120 
00121   MSPUBCollector(const MSPUBCollector &);
00122   MSPUBCollector &operator=(const MSPUBCollector &);
00123 
00124   libwpg::WPGPaintInterface *m_painter;
00125   std::list<ContentChunkReference> m_contentChunkReferences;
00126   double m_width, m_height;
00127   bool m_widthSet, m_heightSet;
00128   unsigned short m_numPages;
00129   std::map<unsigned, std::vector<TextParagraph> > m_textStringsById;
00130   std::map<unsigned, PageInfo> m_pagesBySeqNum;
00131   std::vector<std::pair<ImgType, WPXBinaryData> > m_images;
00132   std::vector<ColorReference> m_textColors;
00133   std::vector<std::vector<unsigned char> > m_fonts;
00134   std::vector<CharacterStyle> m_defaultCharStyles;
00135   std::vector<ParagraphStyle> m_defaultParaStyles;
00136   std::map<unsigned, ShapeType> m_shapeTypesBySeqNum;
00137   std::vector<unsigned> m_possibleImageShapeSeqNums;
00138   std::vector<Color> m_paletteColors;
00139   std::vector<unsigned> m_shapeSeqNumsOrdered;
00140   std::map<unsigned, unsigned> m_pageSeqNumsByShapeSeqNum;
00141   std::map<unsigned, unsigned> m_bgShapeSeqNumsByPageSeqNum;
00142   std::set<unsigned> m_skipIfNotBgSeqNums;
00143   ShapeGroupElement *m_currentShapeGroup;
00144   boost::ptr_vector<ShapeGroupElement> m_topLevelShapes;
00145   std::map<unsigned, ShapeGroupElement *> m_groupsBySeqNum;
00146   std::map<unsigned, ShapeInfo> m_shapeInfosBySeqNum;
00147   std::set<unsigned> m_shapesWithCoordinatesRotated90;
00148   mutable std::vector<bool> m_calculationValuesSeen;
00149   // helper functions
00150   std::vector<int> getShapeAdjustValues(const ShapeInfo &info) const;
00151   void setRectCoordProps(Coordinate, WPXPropertyList *) const;
00152   boost::optional<std::vector<libmspub::TextParagraph> > getShapeText(const ShapeInfo &info) const;
00153   void setupShapeStructures(ShapeGroupElement &elt);
00154   void addBlackToPaletteIfNecessary();
00155   void assignShapesToPages();
00156   void writePage(unsigned pageSeqNum, const PageInfo &pageInfo) const;
00157   boost::function<void(void)> paintShape(const ShapeInfo &info, const Coordinate &relativeTo, const VectorTransformation2D &foldedTransform, bool isGroup, const VectorTransformation2D &thisTransform) const;
00158   double getCalculationValue(const ShapeInfo &info, unsigned index, bool recursiveEntry, const std::vector<int> &adjustValues) const;
00159 
00160   WPXPropertyList getCharStyleProps(const CharacterStyle &, unsigned defaultCharStyleIndex) const;
00161   WPXPropertyList getParaStyleProps(const ParagraphStyle &, unsigned defaultParaStyleIndex) const;
00162   double getSpecialValue(const ShapeInfo &info, const CustomShape &shape, int arg, const std::vector<int> &adjustValues) const;
00163 public:
00164   static WPXString getColorString(const Color &);
00165 };
00166 } // namespace libmspub
00167 
00168 #endif /* __MSPUBCOLLECTOR_H__ */
00169 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */