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);
87 bool setPsTarget(gchar const *utf8_fn);
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 ~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 void _setFillStyle(SPStyle const *style, NRRect const *pbox);
173 void _setStrokeStyle(SPStyle const *style, NRRect const *pbox);
175 void _initCairoMatrix(cairo_matrix_t *matrix, NRMatrix const *transform);
176 void _concatTransform(cairo_t *cr, double xx, double yx, double xy, double yy, double x0, double y0);
177 void _concatTransform(cairo_t *cr, NRMatrix const *transform);
179 CairoRenderState *_createState(void);
180 };
182 } /* namespace Internal */
183 } /* namespace Extension */
184 } /* namespace Inkscape */
186 #endif /* HAVE_CAIRO_PDF */
188 #endif /* !EXTENSION_INTERNAL_CAIRO_RENDER_CONTEXT_H_SEEN */
190 /*
191 Local Variables:
192 mode:c++
193 c-file-style:"stroustrup"
194 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
195 indent-tabs-mode:nil
196 fill-column:99
197 End:
198 */
199 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :