Code

Merging from trunk
[inkscape.git] / src / extension / internal / cairo-render-context.h
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 <2geom/forward.h>
26 #include "style.h"
28 #include <cairo.h>
30 class SPClipPath;
31 class SPMask;
33 namespace Inkscape {
34 namespace Extension {
35 namespace Internal {
37 class CairoRenderer;
38 class CairoRenderContext;
39 class CairoRenderState;
40 class CairoGlyphInfo;
42 // Holds info for rendering a glyph
43 struct CairoGlyphInfo {
44     unsigned long index;
45     double x;
46     double y;
47 };
49 struct CairoRenderState {
50     unsigned int merge_opacity : 1;     // whether fill/stroke opacity can be mul'd with item opacity
51     unsigned int need_layer : 1;        // whether object is masked, clipped, and/or has a non-zero opacity
52     unsigned int has_overflow : 1;
53     unsigned int parent_has_userspace : 1;  // whether the parent's ctm should be applied
54     float opacity;
55     bool has_filtereffect;
56     Geom::Matrix item_transform;     // this item's item->transform, for correct clipping
58     SPClipPath *clip_path;
59     SPMask* mask;
61     Geom::Matrix transform;     // the CTM
62 };
64 class CairoRenderContext {
65     friend class CairoRenderer;
66 public:
67     CairoRenderContext *cloneMe(void) const;
68     CairoRenderContext *cloneMe(double width, double height) const;
69     bool finish(void);
71     CairoRenderer *getRenderer(void) const;
72     cairo_t *getCairoContext(void) const;
74     typedef enum CairoRenderMode {
75         RENDER_MODE_NORMAL,
76         RENDER_MODE_CLIP
77     };
79     typedef enum CairoClipMode {
80         CLIP_MODE_PATH,
81         CLIP_MODE_MASK
82     };
84     bool setImageTarget(cairo_format_t format);
85     bool setPdfTarget(gchar const *utf8_fn);
86     bool setPsTarget(gchar const *utf8_fn);
87     /** Set the cairo_surface_t from an external source */
88     bool setSurfaceTarget(cairo_surface_t *surface, bool is_vector);
90     void setPSLevel(unsigned int level);
91     void setEPS(bool eps);
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 addPathVector(Geom::PathVector const &pv);
118     void setPathVector(Geom::PathVector const &pv);
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(Geom::Matrix const *transform);
131     void setTransform(Geom::Matrix const *transform);
132     void getTransform(Geom::Matrix *copy) const;
133     void getParentTransform(Geom::Matrix *copy) const;
135     /* Clipping methods */
136     void addClipPath(Geom::PathVector const &pv, 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 renderImage(unsigned char *px, unsigned int w, unsigned int h, unsigned int rs,
142                      Geom::Matrix const *image_transform, SPStyle const *style);
143     bool renderGlyphtext(PangoFont *font, Geom::Matrix const *font_matrix,
144                          std::vector<CairoGlyphInfo> const &glyphtext, SPStyle const *style);
146     /* More general rendering methods will have to be added (like fill, stroke) */
148 protected:
149     CairoRenderContext(CairoRenderer *renderer);
150     virtual ~CairoRenderContext(void);
152     float _width;
153     float _height;
154     unsigned short _dpi;
155     unsigned int _pdf_level;
156     unsigned int _ps_level;
157     bool _eps;
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; // Cairo context
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, Geom::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, Geom::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 :