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 #ifdef HAVE_CAIRO_PDF
22 #include "extension/extension.h"
23 #include <set>
24 #include <string>
26 #include "libnr/nr-path.h"
27 #include <libnr/nr-matrix-ops.h>
29 #include "style.h"
31 #include <cairo.h>
33 class SPClipPath;
34 class SPMask;
36 namespace Inkscape {
37 namespace Extension {
38 namespace Internal {
40 class CairoRenderer;
41 class CairoRenderContext;
42 class CairoRenderState;
43 class CairoGlyphInfo;
45 // Holds info for rendering a glyph
46 struct CairoGlyphInfo {
47 unsigned long index;
48 double x;
49 double y;
50 };
52 struct CairoRenderState {
53 unsigned int merge_opacity : 1; // whether fill/stroke opacity can be mul'd with item opacity
54 unsigned int need_layer : 1;
55 unsigned int has_overflow : 1;
56 unsigned int parent_has_userspace : 1; // whether the parent's ctm should be applied
57 float opacity;
59 SPClipPath *clip_path;
60 SPMask* mask;
62 NRMatrix 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);
88 /** Creates the cairo_surface_t for the context with the
89 given width, height and with the currently set target
90 surface type. */
91 bool setupSurface(double width, double height);
93 cairo_surface_t *getSurface(void);
95 /** Saves the contents of the context to a PNG file. */
96 bool saveAsPng(const char *file_name);
98 /* Render/clip mode setting/query */
99 void setRenderMode(CairoRenderMode mode);
100 CairoRenderMode getRenderMode(void) const;
101 void setClipMode(CairoClipMode mode);
102 CairoClipMode getClipMode(void) const;
104 void addBpath(NArtBpath const *bp);
105 void setBpath(NArtBpath const *bp);
107 void pushLayer(void);
108 void popLayer(void);
110 /* Graphics state manipulation */
111 void pushState(void);
112 void popState(void);
113 CairoRenderState *getCurrentState(void) const;
114 CairoRenderState *getParentState(void) const;
115 void setStateForStyle(SPStyle const *style);
117 void transform(NRMatrix const *transform);
118 void setTransform(NRMatrix const *transform);
119 void getTransform(NRMatrix *copy) const;
120 void getParentTransform(NRMatrix *copy) const;
122 /* Clipping methods */
123 void addClipPath(NArtBpath const *bp, SPIEnum const *fill_rule);
124 void addClippingRect(double x, double y, double width, double height);
126 /* Rendering methods */
127 bool renderPath(NRBPath const *bpath, SPStyle const *style, NRRect const *pbox);
128 bool renderImage(unsigned char *px, unsigned int w, unsigned int h, unsigned int rs,
129 NRMatrix const *image_transform, SPStyle const *style);
130 bool renderGlyphtext(PangoFont *font, NRMatrix const *font_matrix,
131 std::vector<CairoGlyphInfo> const &glyphtext, SPStyle const *style);
133 /* More general rendering methods will have to be added (like fill, stroke) */
135 protected:
136 CairoRenderContext(CairoRenderer *renderer);
137 ~CairoRenderContext(void);
139 float _width;
140 float _height;
141 unsigned short _dpi;
143 FILE *_stream;
145 unsigned int _is_valid : 1;
146 unsigned int _vector_based_target : 1;
148 cairo_t *_cr;
149 cairo_surface_t *_surface;
150 cairo_surface_type_t _target;
151 cairo_format_t _target_format;
152 PangoLayout *_layout;
154 unsigned int _clip_rule : 8;
155 unsigned int _clip_winding_failed : 1;
157 GSList *_state_stack;
158 CairoRenderState *_state; // the current state
160 CairoRenderer *_renderer;
162 CairoRenderMode _render_mode;
163 CairoClipMode _clip_mode;
165 cairo_pattern_t *_createPatternForPaintServer(SPPaintServer const *const paintserver,
166 NRRect const *pbox, float alpha);
167 cairo_pattern_t *_createPatternPainter(SPPaintServer const *const paintserver, NRRect const *pbox);
169 unsigned int _showGlyphs(cairo_t *cr, PangoFont *font, std::vector<CairoGlyphInfo> const &glyphtext, bool is_stroke);
171 void _setFillStyle(SPStyle const *style, NRRect const *pbox);
172 void _setStrokeStyle(SPStyle const *style, NRRect const *pbox);
174 void _initCairoMatrix(cairo_matrix_t *matrix, NRMatrix const *transform);
175 void _concatTransform(cairo_t *cr, double xx, double yx, double xy, double yy, double x0, double y0);
176 void _concatTransform(cairo_t *cr, NRMatrix const *transform);
178 CairoRenderState *_createState(void);
179 };
181 } /* namespace Internal */
182 } /* namespace Extension */
183 } /* namespace Inkscape */
185 #endif /* HAVE_CAIRO_PDF */
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 :