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>
26 #include <2geom/forward.h>
28 #include "style.h"
30 #include <cairo.h>
32 class SPClipPath;
33 class SPMask;
35 namespace Inkscape {
36 namespace Extension {
37 namespace Internal {
39 class CairoRenderer;
40 class CairoRenderContext;
41 class CairoRenderState;
42 class CairoGlyphInfo;
44 // Holds info for rendering a glyph
45 struct CairoGlyphInfo {
46 unsigned long index;
47 double x;
48 double y;
49 };
51 struct CairoRenderState {
52 unsigned int merge_opacity : 1; // whether fill/stroke opacity can be mul'd with item opacity
53 unsigned int need_layer : 1;
54 unsigned int has_overflow : 1;
55 unsigned int parent_has_userspace : 1; // whether the parent's ctm should be applied
56 float opacity;
57 bool has_filtereffect;
59 SPClipPath *clip_path;
60 SPMask* mask;
62 NR::Matrix transform; // the CTM
63 };
65 class CairoRenderContext {
66 friend class CairoRenderer;
67 public:
68 CairoRenderContext *cloneMe(void) const;
69 CairoRenderContext *cloneMe(double width, double height) const;
70 bool finish(void);
72 CairoRenderer *getRenderer(void) const;
73 cairo_t *getCairoContext(void) const;
75 typedef enum CairoRenderMode {
76 RENDER_MODE_NORMAL,
77 RENDER_MODE_CLIP
78 };
80 typedef enum CairoClipMode {
81 CLIP_MODE_PATH,
82 CLIP_MODE_MASK
83 };
85 bool setImageTarget(cairo_format_t format);
86 bool setPdfTarget(gchar const *utf8_fn);
87 bool setPsTarget(gchar const *utf8_fn);
88 /** Set the cairo_surface_t from an external source */
89 bool setSurfaceTarget(cairo_surface_t *surface, bool is_vector);
91 void setPSLevel(unsigned int level);
92 unsigned int getPSLevel(void);
93 void setPDFLevel(unsigned int level);
94 void setTextToPath(bool texttopath);
95 bool getTextToPath(void);
96 void setFilterToBitmap(bool filtertobitmap);
97 bool getFilterToBitmap(void);
98 void setBitmapResolution(int resolution);
99 int getBitmapResolution(void);
101 /** Creates the cairo_surface_t for the context with the
102 given width, height and with the currently set target
103 surface type. */
104 bool setupSurface(double width, double height);
106 cairo_surface_t *getSurface(void);
108 /** Saves the contents of the context to a PNG file. */
109 bool saveAsPng(const char *file_name);
111 /* Render/clip mode setting/query */
112 void setRenderMode(CairoRenderMode mode);
113 CairoRenderMode getRenderMode(void) const;
114 void setClipMode(CairoClipMode mode);
115 CairoClipMode getClipMode(void) const;
117 void addBpath(NArtBpath const *bp);
118 void setBpath(NArtBpath const *bp);
120 void pushLayer(void);
121 void popLayer(void);
123 /* Graphics state manipulation */
124 void pushState(void);
125 void popState(void);
126 CairoRenderState *getCurrentState(void) const;
127 CairoRenderState *getParentState(void) const;
128 void setStateForStyle(SPStyle const *style);
130 void transform(NR::Matrix const *transform);
131 void setTransform(NR::Matrix const *transform);
132 void getTransform(NR::Matrix *copy) const;
133 void getParentTransform(NR::Matrix *copy) const;
135 /* Clipping methods */
136 void addClipPath(NArtBpath const *bp, SPIEnum const *fill_rule);
137 void addClippingRect(double x, double y, double width, double height);
139 /* Rendering methods */
140 bool renderPathVector(Geom::PathVector const & pathv, SPStyle const *style, NRRect const *pbox);
141 bool renderPath(const_NRBPath const *bpath, SPStyle const *style, NRRect const *pbox);
142 bool renderImage(unsigned char *px, unsigned int w, unsigned int h, unsigned int rs,
143 NR::Matrix const *image_transform, SPStyle const *style);
144 bool renderGlyphtext(PangoFont *font, NR::Matrix const *font_matrix,
145 std::vector<CairoGlyphInfo> const &glyphtext, SPStyle const *style);
147 /* More general rendering methods will have to be added (like fill, stroke) */
149 protected:
150 CairoRenderContext(CairoRenderer *renderer);
151 virtual ~CairoRenderContext(void);
153 float _width;
154 float _height;
155 unsigned short _dpi;
156 unsigned int _pdf_level;
157 unsigned int _ps_level;
158 bool _is_texttopath;
159 bool _is_filtertobitmap;
160 int _bitmapresolution;
162 FILE *_stream;
164 unsigned int _is_valid : 1;
165 unsigned int _vector_based_target : 1;
167 cairo_t *_cr;
168 cairo_surface_t *_surface;
169 cairo_surface_type_t _target;
170 cairo_format_t _target_format;
171 PangoLayout *_layout;
173 unsigned int _clip_rule : 8;
174 unsigned int _clip_winding_failed : 1;
176 GSList *_state_stack;
177 CairoRenderState *_state; // the current state
179 CairoRenderer *_renderer;
181 CairoRenderMode _render_mode;
182 CairoClipMode _clip_mode;
184 cairo_pattern_t *_createPatternForPaintServer(SPPaintServer const *const paintserver,
185 NRRect const *pbox, float alpha);
186 cairo_pattern_t *_createPatternPainter(SPPaintServer const *const paintserver, NRRect const *pbox);
188 unsigned int _showGlyphs(cairo_t *cr, PangoFont *font, std::vector<CairoGlyphInfo> const &glyphtext, bool is_stroke);
190 bool _finishSurfaceSetup(cairo_surface_t *surface);
191 void _setFillStyle(SPStyle const *style, NRRect const *pbox);
192 void _setStrokeStyle(SPStyle const *style, NRRect const *pbox);
194 void _initCairoMatrix(cairo_matrix_t *matrix, NR::Matrix const *transform);
195 void _concatTransform(cairo_t *cr, double xx, double yx, double xy, double yy, double x0, double y0);
196 void _concatTransform(cairo_t *cr, NR::Matrix const *transform);
198 CairoRenderState *_createState(void);
199 };
201 } /* namespace Internal */
202 } /* namespace Extension */
203 } /* namespace Inkscape */
205 #endif /* !EXTENSION_INTERNAL_CAIRO_RENDER_CONTEXT_H_SEEN */
207 /*
208 Local Variables:
209 mode:c++
210 c-file-style:"stroustrup"
211 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
212 indent-tabs-mode:nil
213 fill-column:99
214 End:
215 */
216 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :