WPG2Parser.h
Go to the documentation of this file.
00001 /* libwpg
00002  * Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
00003  * Copyright (C) 2005 Fridrich Strba (fridrich.strba@bluewin.ch)
00004  * Copyright (C) 2004 Marc Oude Kotte (marc@solcon.nl)
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; if not, write to the
00018  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA  02111-1301 USA
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 <libwpd/libwpd.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; element[2][2] = 1;
00049                 element[0][1] = element[0][2] = 0;
00050                 element[1][0] = element[1][2] = 0;
00051                 element[2][0] = element[2][1] = 0;
00052         }
00053 
00054         void transform(long& x, long& y) const
00055         {
00056                 long rx = (long)(element[0][0]*x + element[1][0]*y + element[2][0]);
00057                 long ry = (long)(element[0][1]*x + element[1][1]*y + element[2][1]);
00058                 x = rx;
00059                 y = ry;
00060         }
00061 
00062         ::WPXPropertyList transformPoint(const ::WPXPropertyList& p) const
00063         {
00064                 ::WPXPropertyList propList;
00065                 propList.insert("svg:x", (element[0][0]*p["svg:x"]->getDouble() + element[1][0]*p["svg:y"]->getDouble() + element[2][0]));
00066                 propList.insert("svg:y", (element[0][1]*p["svg:x"]->getDouble() + element[1][1]*p["svg:y"]->getDouble() + element[2][1]));
00067                 return propList;
00068         }
00069 
00070         ::WPXPropertyList transformRect(const ::WPXPropertyList& r) const
00071         {
00072                 ::WPXPropertyList propList;
00073                 double oldx1 = r["svg:x"]->getDouble();
00074                 double oldy1 = r["svg:y"]->getDouble();
00075                 double oldx2 = r["svg:x"]->getDouble() + r["svg:width"]->getDouble();
00076                 double oldy2 = r["svg:y"]->getDouble() + r["svg:height"]->getDouble();
00077                 
00078                 double newx1 = element[0][0]*oldx1 + element[1][0]*oldy1 + element[2][0];
00079                 double newy1 = element[0][1]*oldx1 + element[1][1]*oldy1 + element[2][1];
00080                 double newx2 = element[0][0]*oldx2 + element[1][0]*oldy2 + element[2][0];
00081                 double newy2 = element[0][1]*oldx2 + element[1][1]*oldy2 + element[2][1];
00082                 
00083                 propList.insert("svg:x", (double)newx1);
00084                 propList.insert("svg:y", (double)newy1);
00085                 propList.insert("svg:width", (newx2-newx1));
00086                 propList.insert("svg:height", (newy2-newy1));
00087                 return propList;
00088         }
00089 
00090         WPG2TransformMatrix& transformBy(const WPG2TransformMatrix& m)
00091         {
00092                 double result[3][3];
00093 
00094                 for(int i = 0; i < 3; i++)
00095                         for(int j = 0; j < 3; j++)
00096                         {
00097                                 result[i][j] = 0;
00098                                 for(int k = 0; k < 3; k++)
00099                                         result[i][j] += m.element[i][k]*element[k][j];
00100                         }
00101 
00102                 for(int x = 0; x < 3; x++)
00103                         for(int y = 0; y < 3; y++)
00104                                 element[x][y] = result[x][y];
00105 
00106                 return *this;
00107         }
00108 };
00109 
00110 class WPGCompoundPolygon
00111 {
00112 public:
00113         WPG2TransformMatrix matrix;
00114         bool isFilled;
00115         bool isFramed;
00116         bool isClosed;
00117 
00118         WPGCompoundPolygon(): matrix(), isFilled(true), isFramed(true), isClosed(true) {}
00119 };
00120 
00121 class WPGGroupContext
00122 {
00123 public:
00124         unsigned subIndex;
00125         int parentType;
00126         ::WPXPropertyListVector compoundPath;
00127         WPG2TransformMatrix compoundMatrix;
00128         bool compoundWindingRule;
00129         bool compoundFilled;
00130         bool compoundFramed;
00131         bool compoundClosed;
00132 
00133         WPGGroupContext(): subIndex(0), parentType(0),
00134         compoundPath(), compoundMatrix(), compoundWindingRule(false),
00135         compoundFilled(false), compoundFramed(true), compoundClosed(false)      {}
00136 
00137         bool isCompoundPolygon() const { return parentType == 0x1a; }
00138 };
00139 
00140 class WPGBitmapContext
00141 {
00142 public:
00143         double x1, y1, x2, y2;
00144         long hres, vres;
00145         WPGBitmapContext(): x1(0), y1(0), x2(0), y2(0), hres(100), vres(100) {}
00146 };
00147 
00148 class WPGBinaryDataContext
00149 {
00150 public:
00151         double x1, y1, x2, y2;
00152         int numObjects, objectIndex;
00153         std::vector<WPXString> mimeTypes;
00154         WPGBinaryDataContext(): x1(0), y1(0), x2(0), y2(0), numObjects(0), objectIndex(0), mimeTypes() {}
00155 };
00156 
00157 class WPGTextDataContext
00158 {
00159 public:
00160         double x1, y1, x2, y2;
00161         unsigned short flags;
00162         unsigned char vertAlign;
00163         unsigned char horAlign;
00164         double baseLineAngle;
00165         WPGTextDataContext(): x1(0), y1(0), x2(0), y2(0), flags(), vertAlign(), horAlign(), baseLineAngle(0.0) {}
00166 };
00167 
00168 class WPG2Parser : public WPGXParser
00169 {
00170 public:
00171         WPG2Parser(WPXInputStream *input, libwpg::WPGPaintInterface* painter, bool isEmbedded = false);
00172         bool parse();
00173 
00174 private:
00175         void handleStartWPG();
00176         void handleEndWPG();
00177         void handleFormSettings();
00178         void handleLayer();
00179         void handleCompoundPolygon();
00180 
00181         void handlePenStyleDefinition();
00182 //      void handlePatternDefinition();
00183         void handleColorPalette();
00184         void handleDPColorPalette();
00185         void handlePenForeColor();
00186         void handleDPPenForeColor();
00187         void handlePenBackColor();
00188         void handleDPPenBackColor();
00189         void handlePenStyle();
00190         void handlePenSize();
00191         void handleDPPenSize();
00192         void handleLineCap();
00193         void handleLineJoin();
00194         void handleBrushGradient();
00195         void handleDPBrushGradient();
00196         void handleBrushForeColor();
00197         void handleDPBrushForeColor();
00198         void handleBrushBackColor();
00199         void handleDPBrushBackColor();
00200         void handleBrushPattern();
00201 
00202         void handlePolyline();
00203         void handlePolyspline();
00204         void handlePolycurve();
00205         void handleRectangle();
00206         void handleArc();
00207 
00208         void handleBitmap();
00209         void handleBitmapData();
00210         
00211         void handleTextData();
00212         void handleTextLine();
00213         void handleTextBlock();
00214         void handleTextPath();
00215         
00216         void handleObjectCapsule();
00217         void handleObjectImage();
00218 
00219         void resetPalette();
00220         void flushCompoundPolygon();
00221         void setPenStyle();
00222 
00223         // parsing context
00224         int m_recordLength;
00225         long m_recordEnd;
00226         bool m_success;
00227         bool m_exit;
00228         bool m_graphicsStarted;
00229         unsigned int m_xres;
00230         unsigned int m_yres;
00231         long m_xofs;
00232         long m_yofs;
00233         long m_width;
00234         long m_height;
00235         bool m_doublePrecision;
00236         ::WPXPropertyList m_style;
00237         libwpg::WPGColor m_penForeColor;
00238         libwpg::WPGColor m_penBackColor;
00239         libwpg::WPGColor m_brushForeColor;
00240         libwpg::WPGColor m_brushBackColor;
00241         libwpg::WPGDashArray m_dashArray;
00242         ::WPXPropertyListVector m_gradient;
00243         std::map<unsigned int,libwpg::WPGDashArray> m_dashArrayStyles;
00244         bool m_layerOpened;
00245         unsigned int m_layerId;
00246         WPG2TransformMatrix m_matrix;
00247         double m_gradientAngle;
00248         ::WPXPropertyList m_gradientRef;
00249         std::stack<WPGGroupContext> m_groupStack;
00250         WPG2TransformMatrix m_compoundMatrix;
00251         bool m_compoundWindingRule;
00252         bool m_compoundFilled;
00253         bool m_compoundFramed;
00254         bool m_compoundClosed;
00255         WPGBitmapContext m_bitmap;
00256         WPGBinaryDataContext m_binaryData;
00257         bool m_hFlipped, m_vFlipped;
00258         WPGTextDataContext m_textData;
00259         bool m_drawTextData;
00260 
00261         class ObjectCharacterization;
00262         void parseCharacterization(ObjectCharacterization*);
00263         unsigned m_binaryId;
00264         
00265         bool m_embedded;
00266 };
00267 
00268 #endif // __WPG2PARSER_H__