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 GfxColor;
37 class GfxColorSpace;
38 class GfxRGB;
39 class GfxPath;
40 class GfxPattern;
41 class GfxTilingPattern;
42 class GfxShading;
43 class GfxFont;
44 class GfxImageColorMap;
45 class Stream;
46 class XRef;
48 class SPCSSAttr;
50 #include <vector>
51 #include <glib/gtypes.h>
53 namespace Inkscape {
54 namespace Extension {
55 namespace Internal {
57 struct SvgTransparencyGroup;
59 /**
60 * \struct SvgGraphicsState
61 * Holds information about the current softmask and group depth.
62 * Could be later used to store other graphics state parameters so that we could
63 * emit only the differences in style settings from the parent state.
64 */
65 struct SvgGraphicsState {
66 Inkscape::XML::Node *softmask; // Points to current softmask node
67 int group_depth; // Depth of nesting groups at this level
68 };
70 /**
71 * \struct SvgGlyph
72 * Holds information about glyphs added by PdfParser which haven't been added
73 * to the document yet.
74 */
75 struct SvgGlyph {
76 NR::Point position; // Absolute glyph coords
77 NR::Point text_position; // Absolute glyph coords in text space
78 double dx, dy; // Advance values
79 double rise; // Text rise parameter
80 char code[8]; // UTF-8 coded character
81 int code_size;
82 bool is_space;
84 bool style_changed; // Set to true if style has to be reset
85 SPCSSAttr *style;
86 int render_mode; // Text render mode
87 char *font_specification; // Pointer to current font specification
88 };
90 /**
91 * \class SvgBuilder
92 *
93 * Builds the inner SVG representation from the calls of PdfParser
94 *
95 */
96 class SvgBuilder {
97 public:
98 SvgBuilder(SPDocument *document, gchar *docname, XRef *xref);
99 SvgBuilder(SvgBuilder *parent, Inkscape::XML::Node *root);
100 virtual ~SvgBuilder();
102 // Property setting
103 void setDocumentSize(double width, double height); // Document size in px
104 void setAsLayer(char *layer_name=NULL);
105 void setGroupOpacity(double opacity);
106 Inkscape::XML::Node *getPreferences() {
107 return _preferences;
108 }
110 // Handling the node stack
111 Inkscape::XML::Node *pushGroup();
112 Inkscape::XML::Node *popGroup();
113 Inkscape::XML::Node *getContainer(); // Returns current group node
115 // Path adding
116 void addPath(GfxState *state, bool fill, bool stroke, bool even_odd=false);
117 void addShadedFill(GfxShading *shading, double *matrix, GfxPath *path, bool even_odd=false);
119 // Image handling
120 void addImage(GfxState *state, Stream *str, int width, int height,
121 GfxImageColorMap *color_map, int *mask_colors);
122 void addImageMask(GfxState *state, Stream *str, int width, int height,
123 bool invert);
124 void addMaskedImage(GfxState *state, Stream *str, int width, int height,
125 GfxImageColorMap *color_map,
126 Stream *mask_str, int mask_width, int mask_height,
127 bool invert_mask);
128 void addSoftMaskedImage(GfxState *state, Stream *str, int width, int height,
129 GfxImageColorMap *color_map,
130 Stream *mask_str, int mask_width, int mask_height,
131 GfxImageColorMap *mask_color_map);
133 // Transparency group and soft mask handling
134 void pushTransparencyGroup(GfxState *state, double *bbox,
135 GfxColorSpace *blending_color_space,
136 bool isolated, bool knockout,
137 bool for_softmask);
138 void popTransparencyGroup(GfxState *state);
139 void paintTransparencyGroup(GfxState *state, double *bbox);
140 void setSoftMask(GfxState *state, double *bbox, bool alpha,
141 Function *transfer_func, GfxColor *backdrop_color);
142 void clearSoftMask(GfxState *state);
144 // Text handling
145 void beginString(GfxState *state, GooString *s);
146 void endString(GfxState *state);
147 void addChar(GfxState *state, double x, double y,
148 double dx, double dy,
149 double originX, double originY,
150 CharCode code, int nBytes, Unicode *u, int uLen);
151 void beginTextObject(GfxState *state);
152 void endTextObject(GfxState *state);
154 bool isPatternTypeSupported(GfxPattern *pattern);
156 // State manipulation
157 void saveState();
158 void restoreState();
159 void updateStyle(GfxState *state);
160 void updateFont(GfxState *state);
161 void updateTextPosition(double tx, double ty);
162 void updateTextShift(GfxState *state, double shift);
163 void updateTextMatrix(GfxState *state);
165 // Clipping
166 void clip(GfxState *state, bool even_odd=false);
167 void setClipPath(GfxState *state, bool even_odd=false);
169 // Transforming
170 void setTransform(double c0, double c1, double c2, double c3, double c4,
171 double c5);
172 void setTransform(double const *transform);
173 bool getTransform(double *transform);
175 private:
176 void _init();
178 // Pattern creation
179 gchar *_createPattern(GfxPattern *pattern, GfxState *state, bool is_stroke=false);
180 gchar *_createGradient(GfxShading *shading, double *matrix, bool for_shading=false);
181 void _addStopToGradient(Inkscape::XML::Node *gradient, double offset,
182 GfxRGB *color, double opacity);
183 bool _addGradientStops(Inkscape::XML::Node *gradient, GfxShading *shading,
184 Function *func);
185 gchar *_createTilingPattern(GfxTilingPattern *tiling_pattern, GfxState *state,
186 bool is_stroke=false);
187 // Image/mask creation
188 Inkscape::XML::Node *_createImage(Stream *str, int width, int height,
189 GfxImageColorMap *color_map, int *mask_colors,
190 bool alpha_only=false, bool invert_alpha=false);
191 Inkscape::XML::Node *_createMask(double width, double height);
192 // Style setting
193 SPCSSAttr *_setStyle(GfxState *state, bool fill, bool stroke, bool even_odd=false);
194 void _setStrokeStyle(SPCSSAttr *css, GfxState *state);
195 void _setFillStyle(SPCSSAttr *css, GfxState *state, bool even_odd);
197 void _flushText(); // Write buffered text into doc
199 // Handling of node stack
200 Inkscape::XML::Node *pushNode(const char* name);
201 Inkscape::XML::Node *popNode();
202 std::vector<Inkscape::XML::Node *> _node_stack;
203 std::vector<int> _group_depth; // Depth of nesting groups
204 SvgTransparencyGroup *_transp_group_stack; // Transparency group stack
205 std::vector<SvgGraphicsState> _state_stack;
207 SPCSSAttr *_font_style; // Current font style
208 GfxFont *_current_font;
209 char *_font_specification;
210 double _font_scaling;
211 bool _need_font_update;
212 NR::Matrix _text_matrix;
213 NR::Point _text_position;
214 std::vector<SvgGlyph> _glyphs; // Added characters
215 bool _in_text_object; // Whether we are inside a text object
216 bool _invalidated_style;
217 GfxState *_current_state;
219 bool _is_top_level; // Whether this SvgBuilder is the top-level one
220 SPDocument *_doc;
221 gchar *_docname; // Basename of the URI from which this document is created
222 XRef *_xref; // Cross-reference table from the PDF doc we're converting from
223 Inkscape::XML::Document *_xml_doc;
224 Inkscape::XML::Node *_root; // Root node from the point of view of this SvgBuilder
225 Inkscape::XML::Node *_container; // Current container (group/pattern/mask)
226 Inkscape::XML::Node *_preferences; // Preferences container node
227 double _width, _height; // Document size in px
228 };
231 } } } /* namespace Inkscape, Extension, Internal */
233 #endif /* HAVE_POPPLER */
235 #endif /* __EXTENSION_INTERNAL_PDFINPUT_SVGBUILDER_H__ */
237 /*
238 Local Variables:
239 mode:c++
240 c-file-style:"stroustrup"
241 c-file-offsets:((innamespace . 0)(inline-open . 0))
242 indent-tabs-mode:nil
243 fill-column:99
244 End:
245 */
246 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :