1 #ifndef EXTENSION_INTERNAL_CAIRO_RENDER_CONTEXT_H_SEEN
2 #define EXTENSION_INTERNAL_CAIRO_RENDER_CONTEXT_H_SEEN
4 /** \file
5 * Declaration of CairoRenderContext, a class used for rendering with Cairo.
6 */
7 /*
8 * Authors:
9 * Miklos Erdelyi <erdelyim@gmail.com>
10 *
11 * Copyright (C) 2006 Miklos Erdelyi
12 *
13 * Licensed under GNU GPL
14 */
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
20 #include "extension/extension.h"
21 #include <set>
22 #include <string>
24 #include "libnr/nr-path.h"
25 #include <libnr/nr-matrix-ops.h>
27 #include "style.h"
29 #include <cairo.h>
31 class SPClipPath;
32 class SPMask;
34 namespace Inkscape {
35 namespace Extension {
36 namespace Internal {
38 class CairoRenderer;
39 class CairoRenderContext;
40 class CairoRenderState;
41 class CairoGlyphInfo;
43 // Holds info for rendering a glyph
44 struct CairoGlyphInfo {
45 unsigned long index;
46 double x;
47 double y;
48 };
50 struct CairoRenderState {
51 unsigned int merge_opacity : 1; // whether fill/stroke opacity can be mul'd with item opacity
52 unsigned int need_layer : 1;
53 unsigned int has_overflow : 1;
54 unsigned int parent_has_userspace : 1; // whether the parent's ctm should be applied
55 float opacity;
56 bool has_filtereffect;
58 SPClipPath *clip_path;
59 SPMask* mask;
61 NR::Matrix transform; // the CTM
62 };
64 class CairoRenderContext {
65 friend class CairoRenderer;
66 public:
67 CairoRenderContext *cloneMe(void) const;
68 CairoRenderContext *cloneMe(double width, double height) const;
69 bool finish(void);
71 CairoRenderer *getRenderer(void) const;
72 cairo_t *getCairoContext(void) const;
74 typedef enum CairoRenderMode {
75 RENDER_MODE_NORMAL,
76 RENDER_MODE_CLIP
77 };
79 typedef enum CairoClipMode {
80 CLIP_MODE_PATH,
81 CLIP_MODE_MASK
82 };
84 bool setImageTarget(cairo_format_t format);
85 bool setPdfTarget(gchar const *utf8_fn);
86 bool setPsTarget(gchar const *utf8_fn);
87 /** Set the cairo_surface_t from an external source */
88 bool setSurfaceTarget(cairo_surface_t *surface, bool is_vector);
90 void setPSLevel(unsigned int level);
91 unsigned int getPSLevel(void);
92 void setPDFLevel(unsigned int level);
93 void setTextToPath(bool texttopath);
94 bool getTextToPath(void);
95 void setFilterToBitmap(bool filtertobitmap);
96 bool getFilterToBitmap(void);
97 void setBitmapResolution(int resolution);
98 int getBitmapResolution(void);
100 /** Creates the cairo_surface_t for the context with the
101 given width, height and with the currently set target
102 surface type. */
103 bool setupSurface(double width, double height);
105 cairo_surface_t *getSurface(void);
107 /** Saves the contents of the context to a PNG file. */
108 bool saveAsPng(const char *file_name);
110 /* Render/clip mode setting/query */
111 void setRenderMode(CairoRenderMode mode);
112 CairoRenderMode getRenderMode(void) const;
113 void setClipMode(CairoClipMode mode);
114 CairoClipMode getClipMode(void) const;
116 void addBpath(NArtBpath const *bp);
117 void setBpath(NArtBpath const *bp);
119 void pushLayer(void);
120 void popLayer(void);
122 /* Graphics state manipulation */
123 void pushState(void);
124 void popState(void);
125 CairoRenderState *getCurrentState(void) const;
126 CairoRenderState *getParentState(void) const;
127 void setStateForStyle(SPStyle const *style);
129 void transform(NR::Matrix const *transform);
130 void setTransform(NR::Matrix const *transform);
131 void getTransform(NR::Matrix *copy) const;
132 void getParentTransform(NR::Matrix *copy) const;
134 /* Clipping methods */
135 void addClipPath(NArtBpath const *bp, SPIEnum const *fill_rule);
136 void addClippingRect(double x, double y, double width, double height);
138 /* Rendering methods */
139 bool renderPath(const_NRBPath const *bpath, SPStyle const *style, NRRect const *pbox);
140 bool renderImage(unsigned char *px, unsigned int w, unsigned int h, unsigned int rs,
141 NR::Matrix const *image_transform, SPStyle const *style);
142 bool renderGlyphtext(PangoFont *font, NR::Matrix const *font_matrix,
143 std::vector<CairoGlyphInfo> const &glyphtext, SPStyle const *style);
145 /* More general rendering methods will have to be added (like fill, stroke) */
147 protected:
148 CairoRenderContext(CairoRenderer *renderer);
149 virtual ~CairoRenderContext(void);
151 float _width;
152 float _height;
153 unsigned short _dpi;
154 unsigned int _pdf_level;
155 unsigned int _ps_level;
156 bool _is_texttopath;
157 bool _is_filtertobitmap;
158 int _bitmapresolution;
160 FILE *_stream;
162 unsigned int _is_valid : 1;
163 unsigned int _vector_based_target : 1;
165 cairo_t *_cr;
166 cairo_surface_t *_surface;
167 cairo_surface_type_t _target;
168 cairo_format_t _target_format;
169 PangoLayout *_layout;
171 unsigned int _clip_rule : 8;
172 unsigned int _clip_winding_failed : 1;
174 GSList *_state_stack;
175 CairoRenderState *_state; // the current state
177 CairoRenderer *_renderer;
179 CairoRenderMode _render_mode;
180 CairoClipMode _clip_mode;
182 cairo_pattern_t *_createPatternForPaintServer(SPPaintServer const *const paintserver,
183 NRRect const *pbox, float alpha);
184 cairo_pattern_t *_createPatternPainter(SPPaintServer const *const paintserver, NRRect const *pbox);
186 unsigned int _showGlyphs(cairo_t *cr, PangoFont *font, std::vector<CairoGlyphInfo> const &glyphtext, bool is_stroke);
188 bool _finishSurfaceSetup(cairo_surface_t *surface);
189 void _setFillStyle(SPStyle const *style, NRRect const *pbox);
190 void _setStrokeStyle(SPStyle const *style, NRRect const *pbox);
192 void _initCairoMatrix(cairo_matrix_t *matrix, NR::Matrix const *transform);
193 void _concatTransform(cairo_t *cr, double xx, double yx, double xy, double yy, double x0, double y0);
194 void _concatTransform(cairo_t *cr, NR::Matrix const *transform);
196 CairoRenderState *_createState(void);
197 };
199 } /* namespace Internal */
200 } /* namespace Extension */
201 } /* namespace Inkscape */
203 #endif /* !EXTENSION_INTERNAL_CAIRO_RENDER_CONTEXT_H_SEEN */
205 /*
206 Local Variables:
207 mode:c++
208 c-file-style:"stroustrup"
209 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
210 indent-tabs-mode:nil
211 fill-column:99
212 End:
213 */
214 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :