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 GfxPattern;
37 class GfxShadingPattern;
38 class GfxTilingPattern;
39 class GfxFont;
40 class GfxImageColorMap;
41 class Stream;
42 class XRef;
44 class SPCSSAttr;
46 #include <vector>
47 #include <glib/gtypes.h>
49 namespace Inkscape {
50 namespace Extension {
51 namespace Internal {
53 /**
54 * \struct SvgGlyph
55 * Holds information about glyphs added by PdfParser which haven't been added
56 * to the document yet.
57 */
58 struct SvgGlyph {
59 NR::Point position; // Absolute glyph coords
60 NR::Point text_position; // Absolute glyph coords in text space
61 double dx, dy; // Advance values
62 double rise; // Text rise parameter
63 char code[8]; // UTF-8 coded character
64 int code_size;
65 bool is_space;
67 bool style_changed; // Set to true if style has to be reset
68 SPCSSAttr *style;
69 int render_mode; // Text render mode
70 char *font_specification; // Pointer to current font specification
71 };
73 /**
74 * \class SvgBuilder
75 *
76 * Builds the inner SVG representation from the calls of PdfParser
77 *
78 */
79 class SvgBuilder {
80 public:
81 SvgBuilder(SPDocument *document, gchar *docname, XRef *xref);
82 SvgBuilder(SvgBuilder *parent, Inkscape::XML::Node *root);
83 ~SvgBuilder();
85 // Property setting
86 void setDocumentSize(double width, double height); // Document size in px
87 void setAsLayer(char *layer_name=NULL);
89 // Handling the node stack
90 Inkscape::XML::Node *pushGroup();
91 Inkscape::XML::Node *popGroup();
92 Inkscape::XML::Node *getContainer(); // Returns current group node
94 // Path adding
95 void addPath(GfxState *state, bool fill, bool stroke, bool even_odd=false);
97 // Image handling
98 void addImage(GfxState *state, Stream *str, int width, int height,
99 GfxImageColorMap *color_map, int *mask_colors);
100 void addImageMask(GfxState *state, Stream *str, int width, int height,
101 bool invert);
102 void addMaskedImage(GfxState *state, Stream *str, int width, int height,
103 GfxImageColorMap *color_map,
104 Stream *mask_str, int mask_width, int mask_height,
105 bool invert_mask);
106 void addSoftMaskedImage(GfxState *state, Stream *str, int width, int height,
107 GfxImageColorMap *color_map,
108 Stream *mask_str, int mask_width, int mask_height,
109 GfxImageColorMap *mask_color_map);
111 // Text handling
112 void beginString(GfxState *state, GooString *s);
113 void endString(GfxState *state);
114 void addChar(GfxState *state, double x, double y,
115 double dx, double dy,
116 double originX, double originY,
117 CharCode code, int nBytes, Unicode *u, int uLen);
118 void beginTextObject(GfxState *state);
119 void endTextObject(GfxState *state);
121 bool isPatternTypeSupported(GfxPattern *pattern);
123 // State manipulation
124 void saveState();
125 void restoreState();
126 void updateStyle(GfxState *state);
127 void updateFont(GfxState *state);
128 void updateTextPosition(double tx, double ty);
129 void updateTextShift(GfxState *state, double shift);
130 void updateTextMatrix(GfxState *state);
132 // Clipping
133 void clip(GfxState *state, bool even_odd=false);
134 void setClipPath(GfxState *state, bool even_odd=false);
136 // Transforming
137 void setTransform(double c0, double c1, double c2, double c3, double c4,
138 double c5);
139 void setTransform(double *transform);
140 bool getTransform(double *transform);
142 private:
143 SvgBuilder();
145 // Pattern creation
146 gchar *_createPattern(GfxPattern *pattern, GfxState *state, bool is_stroke=false);
147 gchar *_createGradient(GfxShadingPattern *shading_pattern);
148 bool _addStopsToGradient(Inkscape::XML::Node *gradient, Function *func, double opacity);
149 bool _addSamplesToGradient(Inkscape::XML::Node *gradient, Function *func,
150 double offset0, double offset1, double opacity);
151 gchar *_createTilingPattern(GfxTilingPattern *tiling_pattern, GfxState *state,
152 bool is_stroke=false);
153 // Image/mask creation
154 Inkscape::XML::Node *_createImage(Stream *str, int width, int height,
155 GfxImageColorMap *color_map, int *mask_colors,
156 bool alpha_only=false, bool invert_alpha=false);
157 Inkscape::XML::Node *_createMask(double width, double height);
158 // Style setting
159 SPCSSAttr *_setStyle(GfxState *state, bool fill, bool stroke, bool even_odd=false);
160 void _setStrokeStyle(SPCSSAttr *css, GfxState *state);
161 void _setFillStyle(SPCSSAttr *css, GfxState *state, bool even_odd);
163 void _flushText(); // Write buffered text into doc
165 std::vector<int> _group_depth; // Depth of nesting groups
167 SPCSSAttr *_font_style; // Current font style
168 GfxFont *_current_font;
169 char *_font_specification;
170 double _font_scaling;
171 bool _need_font_update;
172 NR::Matrix _text_matrix;
173 NR::Point _text_position;
174 std::vector<SvgGlyph> _glyphs; // Added characters
175 bool _in_text_object; // Whether we are inside a text object
176 bool _invalidated_style;
177 GfxState *_current_state;
179 bool _is_top_level; // Whether this SvgBuilder is the top-level one
180 SPDocument *_doc;
181 gchar *_docname; // Basename of the URI from which this document is created
182 XRef *_xref; // Cross-reference table from the PDF doc we're converting from
183 Inkscape::XML::Document *_xml_doc;
184 Inkscape::XML::Node *_root; // Root node from the point of view of this SvgBuilder
185 Inkscape::XML::Node *_container; // Current container (group/pattern/mask)
186 double _width, _height; // Document size in px
187 };
190 } } } /* namespace Inkscape, Extension, Internal */
192 #endif /* HAVE_POPPLER */
194 #endif /* __EXTENSION_INTERNAL_PDFINPUT_SVGBUILDER_H__ */
196 /*
197 Local Variables:
198 mode:c++
199 c-file-style:"stroustrup"
200 c-file-offsets:((innamespace . 0)(inline-open . 0))
201 indent-tabs-mode:nil
202 fill-column:99
203 End:
204 */
205 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :