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;
57 SPClipPath *clip_path;
58 SPMask* mask;
60 NRMatrix transform; // the CTM
61 };
63 class CairoRenderContext {
64 friend class CairoRenderer;
65 public:
66 CairoRenderContext *cloneMe(void) const;
67 CairoRenderContext *cloneMe(double width, double height) const;
68 bool finish(void);
70 CairoRenderer *getRenderer(void) const;
71 cairo_t *getCairoContext(void) const;
73 typedef enum CairoRenderMode {
74 RENDER_MODE_NORMAL,
75 RENDER_MODE_CLIP
76 };
78 typedef enum CairoClipMode {
79 CLIP_MODE_PATH,
80 CLIP_MODE_MASK
81 };
83 bool setImageTarget(cairo_format_t format);
84 bool setPdfTarget(gchar const *utf8_fn);
85 bool setPsTarget(gchar const *utf8_fn);
86 /** Set the cairo_surface_t from an external source */
87 bool setSurfaceTarget(cairo_surface_t *surface, bool is_vector);
89 /** Creates the cairo_surface_t for the context with the
90 given width, height and with the currently set target
91 surface type. */
92 bool setupSurface(double width, double height);
94 cairo_surface_t *getSurface(void);
96 /** Saves the contents of the context to a PNG file. */
97 bool saveAsPng(const char *file_name);
99 /* Render/clip mode setting/query */
100 void setRenderMode(CairoRenderMode mode);
101 CairoRenderMode getRenderMode(void) const;
102 void setClipMode(CairoClipMode mode);
103 CairoClipMode getClipMode(void) const;
105 void addBpath(NArtBpath const *bp);
106 void setBpath(NArtBpath const *bp);
108 void pushLayer(void);
109 void popLayer(void);
111 /* Graphics state manipulation */
112 void pushState(void);
113 void popState(void);
114 CairoRenderState *getCurrentState(void) const;
115 CairoRenderState *getParentState(void) const;
116 void setStateForStyle(SPStyle const *style);
118 void transform(NRMatrix const *transform);
119 void setTransform(NRMatrix const *transform);
120 void getTransform(NRMatrix *copy) const;
121 void getParentTransform(NRMatrix *copy) const;
123 /* Clipping methods */
124 void addClipPath(NArtBpath const *bp, SPIEnum const *fill_rule);
125 void addClippingRect(double x, double y, double width, double height);
127 /* Rendering methods */
128 bool renderPath(NRBPath const *bpath, SPStyle const *style, NRRect const *pbox);
129 bool renderImage(unsigned char *px, unsigned int w, unsigned int h, unsigned int rs,
130 NRMatrix const *image_transform, SPStyle const *style);
131 bool renderGlyphtext(PangoFont *font, NRMatrix const *font_matrix,
132 std::vector<CairoGlyphInfo> const &glyphtext, SPStyle const *style);
134 /* More general rendering methods will have to be added (like fill, stroke) */
136 protected:
137 CairoRenderContext(CairoRenderer *renderer);
138 virtual ~CairoRenderContext(void);
140 float _width;
141 float _height;
142 unsigned short _dpi;
144 FILE *_stream;
146 unsigned int _is_valid : 1;
147 unsigned int _vector_based_target : 1;
149 cairo_t *_cr;
150 cairo_surface_t *_surface;
151 cairo_surface_type_t _target;
152 cairo_format_t _target_format;
153 PangoLayout *_layout;
155 unsigned int _clip_rule : 8;
156 unsigned int _clip_winding_failed : 1;
158 GSList *_state_stack;
159 CairoRenderState *_state; // the current state
161 CairoRenderer *_renderer;
163 CairoRenderMode _render_mode;
164 CairoClipMode _clip_mode;
166 cairo_pattern_t *_createPatternForPaintServer(SPPaintServer const *const paintserver,
167 NRRect const *pbox, float alpha);
168 cairo_pattern_t *_createPatternPainter(SPPaintServer const *const paintserver, NRRect const *pbox);
170 unsigned int _showGlyphs(cairo_t *cr, PangoFont *font, std::vector<CairoGlyphInfo> const &glyphtext, bool is_stroke);
172 bool _finishSurfaceSetup(cairo_surface_t *surface);
173 void _setFillStyle(SPStyle const *style, NRRect const *pbox);
174 void _setStrokeStyle(SPStyle const *style, NRRect const *pbox);
176 void _initCairoMatrix(cairo_matrix_t *matrix, NRMatrix const *transform);
177 void _concatTransform(cairo_t *cr, double xx, double yx, double xy, double yy, double x0, double y0);
178 void _concatTransform(cairo_t *cr, NRMatrix const *transform);
180 CairoRenderState *_createState(void);
181 };
183 } /* namespace Internal */
184 } /* namespace Extension */
185 } /* namespace Inkscape */
187 #endif /* !EXTENSION_INTERNAL_CAIRO_RENDER_CONTEXT_H_SEEN */
189 /*
190 Local Variables:
191 mode:c++
192 c-file-style:"stroustrup"
193 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
194 indent-tabs-mode:nil
195 fill-column:99
196 End:
197 */
198 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :