1 #ifndef __EXTENSION_INTERNAL_PDFINPUT_SVGBUILDER_H__
2 #define __EXTENSION_INTERNAL_PDFINPUT_SVGBUILDER_H__
4 /** \file
5 * SVG representation creator using libpoppler.
6 *
7 * Authors:
8 * miklos erdelyi
9 *
10 * Copyright (C) 2007 Authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #ifdef HAVE_CONFIG_H
16 # include <config.h>
17 #endif
19 #ifdef HAVE_POPPLER
21 class SPDocument;
22 namespace Inkscape {
23 namespace XML {
24 class Document;
25 class Node;
26 }
27 }
29 #include "libnr/nr-point.h"
30 #include "libnr/nr-matrix.h"
32 #include "CharTypes.h"
33 class GooString;
34 class Function;
35 struct GfxState;
36 class GfxRGB;
37 class GfxPath;
38 class GfxPattern;
39 class GfxTilingPattern;
40 class GfxShading;
41 class GfxFont;
42 class GfxImageColorMap;
43 class Stream;
44 class XRef;
46 class SPCSSAttr;
48 #include <vector>
49 #include <glib/gtypes.h>
51 namespace Inkscape {
52 namespace Extension {
53 namespace Internal {
55 /**
56 * \struct SvgGlyph
57 * Holds information about glyphs added by PdfParser which haven't been added
58 * to the document yet.
59 */
60 struct SvgGlyph {
61 NR::Point position; // Absolute glyph coords
62 NR::Point text_position; // Absolute glyph coords in text space
63 double dx, dy; // Advance values
64 double rise; // Text rise parameter
65 char code[8]; // UTF-8 coded character
66 int code_size;
67 bool is_space;
69 bool style_changed; // Set to true if style has to be reset
70 SPCSSAttr *style;
71 int render_mode; // Text render mode
72 char *font_specification; // Pointer to current font specification
73 };
75 /**
76 * \class SvgBuilder
77 *
78 * Builds the inner SVG representation from the calls of PdfParser
79 *
80 */
81 class SvgBuilder {
82 public:
83 SvgBuilder(SPDocument *document, gchar *docname, XRef *xref);
84 SvgBuilder(SvgBuilder *parent, Inkscape::XML::Node *root);
85 ~SvgBuilder();
87 // Property setting
88 void setDocumentSize(double width, double height); // Document size in px
89 void setAsLayer(char *layer_name=NULL);
91 // Handling the node stack
92 Inkscape::XML::Node *pushGroup();
93 Inkscape::XML::Node *popGroup();
94 Inkscape::XML::Node *getContainer(); // Returns current group node
96 // Path adding
97 void addPath(GfxState *state, bool fill, bool stroke, bool even_odd=false);
98 void addShadedFill(GfxShading *shading, double *matrix, GfxPath *path, bool even_odd=false);
100 // Image handling
101 void addImage(GfxState *state, Stream *str, int width, int height,
102 GfxImageColorMap *color_map, int *mask_colors);
103 void addImageMask(GfxState *state, Stream *str, int width, int height,
104 bool invert);
105 void addMaskedImage(GfxState *state, Stream *str, int width, int height,
106 GfxImageColorMap *color_map,
107 Stream *mask_str, int mask_width, int mask_height,
108 bool invert_mask);
109 void addSoftMaskedImage(GfxState *state, Stream *str, int width, int height,
110 GfxImageColorMap *color_map,
111 Stream *mask_str, int mask_width, int mask_height,
112 GfxImageColorMap *mask_color_map);
114 // Text handling
115 void beginString(GfxState *state, GooString *s);
116 void endString(GfxState *state);
117 void addChar(GfxState *state, double x, double y,
118 double dx, double dy,
119 double originX, double originY,
120 CharCode code, int nBytes, Unicode *u, int uLen);
121 void beginTextObject(GfxState *state);
122 void endTextObject(GfxState *state);
124 bool isPatternTypeSupported(GfxPattern *pattern);
126 // State manipulation
127 void saveState();
128 void restoreState();
129 void updateStyle(GfxState *state);
130 void updateFont(GfxState *state);
131 void updateTextPosition(double tx, double ty);
132 void updateTextShift(GfxState *state, double shift);
133 void updateTextMatrix(GfxState *state);
135 // Clipping
136 void clip(GfxState *state, bool even_odd=false);
137 void setClipPath(GfxState *state, bool even_odd=false);
139 // Transforming
140 void setTransform(double c0, double c1, double c2, double c3, double c4,
141 double c5);
142 void setTransform(double *transform);
143 bool getTransform(double *transform);
145 private:
146 SvgBuilder();
148 // Pattern creation
149 gchar *_createPattern(GfxPattern *pattern, GfxState *state, bool is_stroke=false);
150 gchar *_createGradient(GfxShading *shading, double *matrix, bool for_shading=false);
151 void _addStopToGradient(Inkscape::XML::Node *gradient, double offset,
152 GfxRGB *color, double opacity);
153 bool _addGradientStops(Inkscape::XML::Node *gradient, GfxShading *shading,
154 Function *func);
155 gchar *_createTilingPattern(GfxTilingPattern *tiling_pattern, GfxState *state,
156 bool is_stroke=false);
157 // Image/mask creation
158 Inkscape::XML::Node *_createImage(Stream *str, int width, int height,
159 GfxImageColorMap *color_map, int *mask_colors,
160 bool alpha_only=false, bool invert_alpha=false);
161 Inkscape::XML::Node *_createMask(double width, double height);
162 // Style setting
163 SPCSSAttr *_setStyle(GfxState *state, bool fill, bool stroke, bool even_odd=false);
164 void _setStrokeStyle(SPCSSAttr *css, GfxState *state);
165 void _setFillStyle(SPCSSAttr *css, GfxState *state, bool even_odd);
167 void _flushText(); // Write buffered text into doc
169 std::vector<int> _group_depth; // Depth of nesting groups
171 SPCSSAttr *_font_style; // Current font style
172 GfxFont *_current_font;
173 char *_font_specification;
174 double _font_scaling;
175 bool _need_font_update;
176 NR::Matrix _text_matrix;
177 NR::Point _text_position;
178 std::vector<SvgGlyph> _glyphs; // Added characters
179 bool _in_text_object; // Whether we are inside a text object
180 bool _invalidated_style;
181 GfxState *_current_state;
183 bool _is_top_level; // Whether this SvgBuilder is the top-level one
184 SPDocument *_doc;
185 gchar *_docname; // Basename of the URI from which this document is created
186 XRef *_xref; // Cross-reference table from the PDF doc we're converting from
187 Inkscape::XML::Document *_xml_doc;
188 Inkscape::XML::Node *_root; // Root node from the point of view of this SvgBuilder
189 Inkscape::XML::Node *_container; // Current container (group/pattern/mask)
190 double _width, _height; // Document size in px
191 };
194 } } } /* namespace Inkscape, Extension, Internal */
196 #endif /* HAVE_POPPLER */
198 #endif /* __EXTENSION_INTERNAL_PDFINPUT_SVGBUILDER_H__ */
200 /*
201 Local Variables:
202 mode:c++
203 c-file-style:"stroustrup"
204 c-file-offsets:((innamespace . 0)(inline-open . 0))
205 indent-tabs-mode:nil
206 fill-column:99
207 End:
208 */
209 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :