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;
58 Geom::Matrix item_transform; // this item's item->transform, for correct clipping
60 SPClipPath *clip_path;
61 SPMask* mask;
63 Geom::Matrix transform; // the CTM
64 };
66 class CairoRenderContext {
67 friend class CairoRenderer;
68 public:
69 CairoRenderContext *cloneMe(void) const;
70 CairoRenderContext *cloneMe(double width, double height) const;
71 bool finish(void);
73 CairoRenderer *getRenderer(void) const;
74 cairo_t *getCairoContext(void) const;
76 typedef enum CairoRenderMode {
77 RENDER_MODE_NORMAL,
78 RENDER_MODE_CLIP
79 };
81 typedef enum CairoClipMode {
82 CLIP_MODE_PATH,
83 CLIP_MODE_MASK
84 };
86 bool setImageTarget(cairo_format_t format);
87 bool setPdfTarget(gchar const *utf8_fn);
88 bool setPsTarget(gchar const *utf8_fn);
89 /** Set the cairo_surface_t from an external source */
90 bool setSurfaceTarget(cairo_surface_t *surface, bool is_vector);
92 void setPSLevel(unsigned int level);
93 void setEPS(bool eps);
94 unsigned int getPSLevel(void);
95 void setPDFLevel(unsigned int level);
96 void setTextToPath(bool texttopath);
97 bool getTextToPath(void);
98 void setFilterToBitmap(bool filtertobitmap);
99 bool getFilterToBitmap(void);
100 void setBitmapResolution(int resolution);
101 int getBitmapResolution(void);
103 /** Creates the cairo_surface_t for the context with the
104 given width, height and with the currently set target
105 surface type. */
106 bool setupSurface(double width, double height);
108 cairo_surface_t *getSurface(void);
110 /** Saves the contents of the context to a PNG file. */
111 bool saveAsPng(const char *file_name);
113 /* Render/clip mode setting/query */
114 void setRenderMode(CairoRenderMode mode);
115 CairoRenderMode getRenderMode(void) const;
116 void setClipMode(CairoClipMode mode);
117 CairoClipMode getClipMode(void) const;
119 void addPathVector(Geom::PathVector const &pv);
120 void setPathVector(Geom::PathVector const &pv);
122 void pushLayer(void);
123 void popLayer(void);
125 /* Graphics state manipulation */
126 void pushState(void);
127 void popState(void);
128 CairoRenderState *getCurrentState(void) const;
129 CairoRenderState *getParentState(void) const;
130 void setStateForStyle(SPStyle const *style);
132 void transform(Geom::Matrix const *transform);
133 void setTransform(Geom::Matrix const *transform);
134 void getTransform(Geom::Matrix *copy) const;
135 void getParentTransform(Geom::Matrix *copy) const;
137 /* Clipping methods */
138 void addClipPath(Geom::PathVector const &pv, SPIEnum const *fill_rule);
139 void addClippingRect(double x, double y, double width, double height);
141 /* Rendering methods */
142 bool renderPathVector(Geom::PathVector const & pathv, SPStyle const *style, NRRect const *pbox);
143 bool renderPath(const_NRBPath const *bpath, SPStyle const *style, NRRect const *pbox);
144 bool renderImage(unsigned char *px, unsigned int w, unsigned int h, unsigned int rs,
145 Geom::Matrix const *image_transform, SPStyle const *style);
146 bool renderGlyphtext(PangoFont *font, Geom::Matrix const *font_matrix,
147 std::vector<CairoGlyphInfo> const &glyphtext, SPStyle const *style);
149 /* More general rendering methods will have to be added (like fill, stroke) */
151 protected:
152 CairoRenderContext(CairoRenderer *renderer);
153 virtual ~CairoRenderContext(void);
155 float _width;
156 float _height;
157 unsigned short _dpi;
158 unsigned int _pdf_level;
159 unsigned int _ps_level;
160 bool _eps;
161 bool _is_texttopath;
162 bool _is_filtertobitmap;
163 int _bitmapresolution;
165 FILE *_stream;
167 unsigned int _is_valid : 1;
168 unsigned int _vector_based_target : 1;
170 cairo_t *_cr;
171 cairo_surface_t *_surface;
172 cairo_surface_type_t _target;
173 cairo_format_t _target_format;
174 PangoLayout *_layout;
176 unsigned int _clip_rule : 8;
177 unsigned int _clip_winding_failed : 1;
179 GSList *_state_stack;
180 CairoRenderState *_state; // the current state
182 CairoRenderer *_renderer;
184 CairoRenderMode _render_mode;
185 CairoClipMode _clip_mode;
187 cairo_pattern_t *_createPatternForPaintServer(SPPaintServer const *const paintserver,
188 NRRect const *pbox, float alpha);
189 cairo_pattern_t *_createPatternPainter(SPPaintServer const *const paintserver, NRRect const *pbox);
191 unsigned int _showGlyphs(cairo_t *cr, PangoFont *font, std::vector<CairoGlyphInfo> const &glyphtext, bool is_stroke);
193 bool _finishSurfaceSetup(cairo_surface_t *surface);
194 void _setFillStyle(SPStyle const *style, NRRect const *pbox);
195 void _setStrokeStyle(SPStyle const *style, NRRect const *pbox);
197 void _initCairoMatrix(cairo_matrix_t *matrix, Geom::Matrix const *transform);
198 void _concatTransform(cairo_t *cr, double xx, double yx, double xy, double yy, double x0, double y0);
199 void _concatTransform(cairo_t *cr, Geom::Matrix const *transform);
201 CairoRenderState *_createState(void);
202 };
204 } /* namespace Internal */
205 } /* namespace Extension */
206 } /* namespace Inkscape */
208 #endif /* !EXTENSION_INTERNAL_CAIRO_RENDER_CONTEXT_H_SEEN */
210 /*
211 Local Variables:
212 mode:c++
213 c-file-style:"stroustrup"
214 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
215 indent-tabs-mode:nil
216 fill-column:99
217 End:
218 */
219 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :