WPG2Parser.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
00002 /* libwpg
00003  * Version: MPL 2.0 / LGPLv2.1+
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  * Major Contributor(s):
00010  * Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
00011  * Copyright (C) 2005 Fridrich Strba (fridrich.strba@bluewin.ch)
00012  * Copyright (C) 2004 Marc Oude Kotte (marc@solcon.nl)
00013  *
00014  * For minor contributions see the git repository.
00015  *
00016  * Alternatively, the contents of this file may be used under the terms
00017  * of the GNU Lesser General Public License Version 2.1 or later
00018  * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
00019  * applicable instead of those above.
00020  *
00021  * For further information visit http://libwpg.sourceforge.net
00022  */
00023 
00024 /* "This product is not manufactured, approved, or supported by
00025  * Corel Corporation or Corel Corporation Limited."
00026  */
00027 
00028 #ifndef __WPG2PARSER_H__
00029 #define __WPG2PARSER_H__
00030 
00031 #include "WPGXParser.h"
00032 #include "WPGDashArray.h"
00033 #include "WPGBitmap.h"
00034 #include <librevenge/librevenge.h>
00035 
00036 #include <map>
00037 #include <stack>
00038 #include <vector>
00039 
00040 class WPG2TransformMatrix
00041 {
00042 public:
00043         double element[3][3];
00044 
00045         WPG2TransformMatrix()
00046         {
00047                 // identity transformation
00048                 element[0][0] = element[1][1] = 1;
00049                 element[2][2] = 1;
00050                 element[0][1] = element[0][2] = 0;
00051                 element[1][0] = element[1][2] = 0;
00052                 element[2][0] = element[2][1] = 0;
00053         }
00054 
00055         void transform(long &x, long &y) const
00056         {
00057                 long rx = (long)(element[0][0]*x + element[1][0]*y + element[2][0]);
00058                 long ry = (long)(element[0][1]*x + element[1][1]*y + element[2][1]);
00059                 x = rx;
00060                 y = ry;
00061         }
00062 
00063         ::librevenge::RVNGPropertyList transformPoint(const ::librevenge::RVNGPropertyList &p) const
00064         {
00065                 ::librevenge::RVNGPropertyList propList;
00066                 propList.insert("svg:x", (element[0][0]*p["svg:x"]->getDouble() + element[1][0]*p["svg:y"]->getDouble() + element[2][0]));
00067                 propList.insert("svg:y", (element[0][1]*p["svg:x"]->getDouble() + element[1][1]*p["svg:y"]->getDouble() + element[2][1]));
00068                 return propList;
00069         }
00070 
00071         ::librevenge::RVNGPropertyList transformRect(const ::librevenge::RVNGPropertyList &r) const
00072         {
00073                 ::librevenge::RVNGPropertyList propList;
00074                 double oldx1 = r["svg:x"]->getDouble();
00075                 double oldy1 = r["svg:y"]->getDouble();
00076                 double oldx2 = r["svg:x"]->getDouble() + r["svg:width"]->getDouble();
00077                 double oldy2 = r["svg:y"]->getDouble() + r["svg:height"]->getDouble();
00078 
00079                 double newx1 = element[0][0]*oldx1 + element[1][0]*oldy1 + element[2][0];
00080                 double newy1 = element[0][1]*oldx1 + element[1][1]*oldy1 + element[2][1];
00081                 double newx2 = element[0][0]*oldx2 + element[1][0]*oldy2 + element[2][0];
00082                 double newy2 = element[0][1]*oldx2 + element[1][1]*oldy2 + element[2][1];
00083 
00084                 propList.insert("svg:x", (double)newx1);
00085                 propList.insert("svg:y", (double)newy1);
00086                 propList.insert("svg:width", (newx2-newx1));
00087                 propList.insert("svg:height", (newy2-newy1));
00088                 return propList;
00089         }
00090 
00091         WPG2TransformMatrix &transformBy(const WPG2TransformMatrix &m)
00092         {
00093                 double result[3][3];
00094 
00095                 for (int i = 0; i < 3; i++)
00096                         for (int j = 0; j < 3; j++)
00097                         {
00098                                 result[i][j] = 0;
00099                                 for (int k = 0; k < 3; k++)
00100                                         result[i][j] += m.element[i][k]*element[k][j];
00101                         }
00102 
00103                 for (int x = 0; x < 3; x++)
00104                         for (int y = 0; y < 3; y++)
00105                                 element[x][y] = result[x][y];
00106 
00107                 return *this;
00108         }
00109 };
00110 
00111 class WPGCompoundPolygon
00112 {
00113 public:
00114         WPG2TransformMatrix matrix;
00115         bool isFilled;
00116         bool isFramed;
00117         bool isClosed;
00118 
00119         WPGCompoundPolygon(): matrix(), isFilled(true), isFramed(true), isClosed(true) {}
00120 };
00121 
00122 class WPGGroupContext
00123 {
00124 public:
00125         unsigned subIndex;
00126         int parentType;
00127         ::librevenge::RVNGPropertyListVector compoundPath;
00128         WPG2TransformMatrix compoundMatrix;
00129         bool compoundWindingRule;
00130         bool compoundFilled;
00131         bool compoundFramed;
00132         bool compoundClosed;
00133 
00134         WPGGroupContext(): subIndex(0), parentType(0),
00135                 compoundPath(), compoundMatrix(), compoundWindingRule(false),
00136                 compoundFilled(false), compoundFramed(true), compoundClosed(false)      {}
00137 
00138         bool isCompoundPolygon() const
00139         {
00140                 return parentType == 0x1a;
00141         }
00142 };
00143 
00144 class WPGBitmapContext
00145 {
00146 public:
00147         double x1, y1, x2, y2;
00148         long hres, vres;
00149         WPGBitmapContext(): x1(0), y1(0), x2(0), y2(0), hres(100), vres(100) {}
00150 };
00151 
00152 class WPGBinaryDataContext
00153 {
00154 public:
00155         double x1, y1, x2, y2;
00156         int numObjects, objectIndex;
00157         std::vector<librevenge::RVNGString> mimeTypes;
00158         WPGBinaryDataContext(): x1(0), y1(0), x2(0), y2(0), numObjects(0), objectIndex(0), mimeTypes() {}
00159 };
00160 
00161 class WPGTextDataContext
00162 {
00163 public:
00164         double x1, y1, x2, y2;
00165         unsigned short flags;
00166         unsigned char vertAlign;
00167         unsigned char horAlign;
00168         double baseLineAngle;
00169         WPGTextDataContext(): x1(0), y1(0), x2(0), y2(0), flags(), vertAlign(), horAlign(), baseLineAngle(0.0) {}
00170 };
00171 
00172 class WPG2Parser : public WPGXParser
00173 {
00174 public:
00175         WPG2Parser(librevenge::RVNGInputStream *input, librevenge::RVNGDrawingInterface *painter, bool isEmbedded = false);
00176         bool parse();
00177 
00178 private:
00179         void handleStartWPG();
00180         void handleEndWPG();
00181         void handleFormSettings();
00182         void handleLayer();
00183         void handleCompoundPolygon();
00184 
00185         void handlePenStyleDefinition();
00186 //      void handlePatternDefinition();
00187         void handleColorPalette();
00188         void handleDPColorPalette();
00189         void handlePenForeColor();
00190         void handleDPPenForeColor();
00191         void handlePenBackColor();
00192         void handleDPPenBackColor();
00193         void handlePenStyle();
00194         void handlePenSize();
00195         void handleDPPenSize();
00196         void handleLineCap();
00197         void handleLineJoin();
00198         void handleBrushGradient();
00199         void handleDPBrushGradient();
00200         void handleBrushForeColor();
00201         void handleDPBrushForeColor();
00202         void handleBrushBackColor();
00203         void handleDPBrushBackColor();
00204         void handleBrushPattern();
00205 
00206         void handlePolyline();
00207         void handlePolyspline();
00208         void handlePolycurve();
00209         void handleRectangle();
00210         void handleArc();
00211 
00212         void handleBitmap();
00213         void handleBitmapData();
00214 
00215         void handleTextData();
00216         void handleTextLine();
00217         void handleTextBlock();
00218         void handleTextPath();
00219 
00220         void handleObjectCapsule();
00221         void handleObjectImage();
00222 
00223         void resetPalette();
00224         void flushCompoundPolygon();
00225         void setPenStyle();
00226 
00227         unsigned int getRemainingRecordLength() const;
00228         bool checkRLESize(unsigned bytes) const;
00229 
00230         // parsing context
00231         int m_recordLength;
00232         long m_recordEnd;
00233         bool m_success;
00234         bool m_exit;
00235         bool m_graphicsStarted;
00236         unsigned int m_xres;
00237         unsigned int m_yres;
00238         long m_xofs;
00239         long m_yofs;
00240         long m_width;
00241         long m_height;
00242         bool m_doublePrecision;
00243         ::librevenge::RVNGPropertyList m_style;
00244         libwpg::WPGColor m_penForeColor;
00245         libwpg::WPGColor m_penBackColor;
00246         libwpg::WPGColor m_brushForeColor;
00247         libwpg::WPGColor m_brushBackColor;
00248         libwpg::WPGDashArray m_dashArray;
00249         ::librevenge::RVNGPropertyListVector m_gradient;
00250         std::map<unsigned int,libwpg::WPGDashArray> m_dashArrayStyles;
00251         bool m_layerOpened;
00252 #ifdef DEBUG
00253         unsigned int m_layerId;
00254 #endif
00255         WPG2TransformMatrix m_matrix;
00256         double m_gradientAngle;
00257         ::librevenge::RVNGPropertyList m_gradientRef;
00258         std::stack<WPGGroupContext> m_groupStack;
00259         WPG2TransformMatrix m_compoundMatrix;
00260         bool m_compoundWindingRule;
00261         bool m_compoundFilled;
00262         bool m_compoundFramed;
00263         bool m_compoundClosed;
00264         WPGBitmapContext m_bitmap;
00265         WPGBinaryDataContext m_binaryData;
00266         bool m_hFlipped, m_vFlipped;
00267         WPGTextDataContext m_textData;
00268         bool m_drawTextData;
00269 
00270         class ObjectCharacterization;
00271         void parseCharacterization(ObjectCharacterization *);
00272 #if DUMP_BINARY_DATA
00273         unsigned m_binaryId;
00274 #endif
00275 };
00276 
00277 #endif // __WPG2PARSER_H__
00278 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */