58b0a10e2812b814c92ab75204f68686e1004291
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 NRMatrix 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 void setPDFLevel(unsigned int level);
92 void setTextToPath(bool texttopath);
93 void setFilterToBitmap(bool filtertobitmap);
95 /** Creates the cairo_surface_t for the context with the
96 given width, height and with the currently set target
97 surface type. */
98 bool setupSurface(double width, double height);
100 cairo_surface_t *getSurface(void);
102 /** Saves the contents of the context to a PNG file. */
103 bool saveAsPng(const char *file_name);
105 /* Render/clip mode setting/query */
106 void setRenderMode(CairoRenderMode mode);
107 CairoRenderMode getRenderMode(void) const;
108 void setClipMode(CairoClipMode mode);
109 CairoClipMode getClipMode(void) const;
111 void addBpath(NArtBpath const *bp);
112 void setBpath(NArtBpath const *bp);
114 void pushLayer(void);
115 void popLayer(void);
117 /* Graphics state manipulation */
118 void pushState(void);
119 void popState(void);
120 CairoRenderState *getCurrentState(void) const;
121 CairoRenderState *getParentState(void) const;
122 void setStateForStyle(SPStyle const *style);
124 void transform(NRMatrix const *transform);
125 void setTransform(NRMatrix const *transform);
126 void getTransform(NRMatrix *copy) const;
127 void getParentTransform(NRMatrix *copy) const;
129 /* Clipping methods */
130 void addClipPath(NArtBpath const *bp, SPIEnum const *fill_rule);
131 void addClippingRect(double x, double y, double width, double height);
133 /* Rendering methods */
134 bool renderPath(NRBPath const *bpath, SPStyle const *style, NRRect const *pbox);
135 bool renderImage(unsigned char *px, unsigned int w, unsigned int h, unsigned int rs,
136 NRMatrix const *image_transform, SPStyle const *style);
137 bool renderGlyphtext(PangoFont *font, NRMatrix const *font_matrix,
138 std::vector<CairoGlyphInfo> const &glyphtext, SPStyle const *style);
140 /* More general rendering methods will have to be added (like fill, stroke) */
142 protected:
143 CairoRenderContext(CairoRenderer *renderer);
144 virtual ~CairoRenderContext(void);
146 float _width;
147 float _height;
148 unsigned short _dpi;
149 unsigned int _pdf_level;
150 unsigned int _ps_level;
151 bool _is_texttopath;
152 bool _is_filtertobitmap;
154 FILE *_stream;
156 unsigned int _is_valid : 1;
157 unsigned int _vector_based_target : 1;
159 cairo_t *_cr;
160 cairo_surface_t *_surface;
161 cairo_surface_type_t _target;
162 cairo_format_t _target_format;
163 PangoLayout *_layout;
165 unsigned int _clip_rule : 8;
166 unsigned int _clip_winding_failed : 1;
168 GSList *_state_stack;
169 CairoRenderState *_state; // the current state
171 CairoRenderer *_renderer;
173 CairoRenderMode _render_mode;
174 CairoClipMode _clip_mode;
176 cairo_pattern_t *_createPatternForPaintServer(SPPaintServer const *const paintserver,
177 NRRect const *pbox, float alpha);
178 cairo_pattern_t *_createPatternPainter(SPPaintServer const *const paintserver, NRRect const *pbox);
180 unsigned int _showGlyphs(cairo_t *cr, PangoFont *font, std::vector<CairoGlyphInfo> const &glyphtext, bool is_stroke);
182 bool _finishSurfaceSetup(cairo_surface_t *surface);
183 void _setFillStyle(SPStyle const *style, NRRect const *pbox);
184 void _setStrokeStyle(SPStyle const *style, NRRect const *pbox);
186 void _initCairoMatrix(cairo_matrix_t *matrix, NRMatrix const *transform);
187 void _concatTransform(cairo_t *cr, double xx, double yx, double xy, double yy, double x0, double y0);
188 void _concatTransform(cairo_t *cr, NRMatrix const *transform);
190 CairoRenderState *_createState(void);
191 };
193 } /* namespace Internal */
194 } /* namespace Extension */
195 } /* namespace Inkscape */
197 #endif /* !EXTENSION_INTERNAL_CAIRO_RENDER_CONTEXT_H_SEEN */
199 /*
200 Local Variables:
201 mode:c++
202 c-file-style:"stroustrup"
203 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
204 indent-tabs-mode:nil
205 fill-column:99
206 End:
207 */
208 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :