Code

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