Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
cairo-render-context.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2#ifndef EXTENSION_INTERNAL_CAIRO_RENDER_CONTEXT_H_SEEN
3#define EXTENSION_INTERNAL_CAIRO_RENDER_CONTEXT_H_SEEN
4
8/*
9 * Authors:
10 * Miklos Erdelyi <erdelyim@gmail.com>
11 *
12 * Copyright (C) 2006 Miklos Erdelyi
13 *
14 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
15 */
16
17#include "extension/extension.h"
18#include <set>
19#include <string>
20
21#include <2geom/forward.h>
22#include <2geom/affine.h>
23
24#include "style-internal.h" // SPIEnum
25
26#include <cairo.h>
27
28class SPClipPath;
29class SPMask;
30
31typedef struct _PangoFont PangoFont;
32typedef struct _PangoLayout PangoLayout;
33
34namespace Inkscape {
35class Pixbuf;
36
37namespace Extension::Internal {
38
39class CairoRenderer;
40class CairoRenderContext;
41struct CairoRenderState;
42
43// Holds info for rendering a glyph
45 unsigned long index;
46 double x;
47 double y;
48};
49
51 unsigned merge_opacity : 1 = true;
52 unsigned need_layer : 1 = false;
53 unsigned has_overflow : 1 = false;
54 unsigned parent_has_userspace : 1 = false;
55 unsigned has_filtereffect : 1 = false;
56
57 float opacity = 1.0;
59
61 SPMask* mask = nullptr;
62
64};
65
67 friend class CairoRenderer;
68public:
69 // Constructor is private: only a CairoRenderer can create a new context.
71
72 CairoRenderContext(CairoRenderContext const &other) = delete; // We hold a FILE handle
74
77
78 /* Rendering methods */
85
90
95
96 CairoRenderContext createSimilar(double width, double height) const;
97 bool finish(bool finish_surface = true);
98 bool finishPage();
99 bool nextPage(double width, double height, char const *label);
100
102
103 bool setImageTarget(cairo_format_t format);
104 bool setPdfTarget(gchar const *utf8_fn);
105 bool setPsTarget(gchar const *utf8_fn);
107 bool setSurfaceTarget(cairo_surface_t *surface, bool is_vector, cairo_matrix_t *ctm=nullptr);
108
110 void setMetadata(SPDocument const &document);
111
112 void setPSLevel(unsigned int level);
113 void setEPS(bool eps) { _eps = eps; }
114 void setPDFLevel(unsigned int level);
115 void setTextToPath(bool texttopath) { _is_texttopath = texttopath; }
116 void setOmitText(bool omittext) { _is_omittext = omittext; }
117 void setFilterToBitmap(bool filtertobitmap) { _is_filtertobitmap = filtertobitmap; }
119 void setBitmapResolution(unsigned resolution) { _bitmapresolution = resolution; }
121
125 bool setupSurface(double width, double height);
126
128
130 bool saveAsPng(const char *file_name);
131
133 void newPage();
134
135 /* Render/clip mode setting/query */
140
141 void addPathVector(Geom::PathVector const &pv);
142 void setPathVector(Geom::PathVector const &pv);
143
144 void pushLayer();
145 void popLayer(cairo_operator_t composite = CAIRO_OPERATOR_CLEAR);
146
147 void tagBegin(const char* link);
148 void tagEnd();
149 void destBegin(const char* link);
150 void destEnd();
151
152 /* Graphics state manipulation */
153 void pushState();
154 void popState();
155 const CairoRenderState *getCurrentState() const { return &_state_stack.back(); }
156 const CairoRenderState *getParentState() const;
157 void setStateForStyle(SPStyle const *style);
158 void setStateForItem(SPItem const *item);
159 void setStateNeedsLayer(bool state_needs_layer) { _state_stack.back().need_layer = state_needs_layer; }
160 void setStateMergeOpacity(bool state_merge_opacity) { _state_stack.back().merge_opacity = state_merge_opacity; }
161
162 void transform(Geom::Affine const &transform);
168
169 /* Clipping methods */
170 void addClipPath(Geom::PathVector const &pv, SPIEnum<SPWindRule> const *fill_rule);
171 void addClippingRect(double x, double y, double width, double height);
172
173 bool renderPathVector(Geom::PathVector const &pathv, SPStyle const *style, Geom::OptRect const &pbox, CairoPaintOrder order = STROKE_OVER_FILL);
174 bool renderImage(Inkscape::Pixbuf const *pb,
175 Geom::Affine const &image_transform, SPStyle const *style);
176 bool renderGlyphtext(PangoFont *font, Geom::Affine const &font_matrix,
177 std::vector<CairoGlyphInfo> const &glyphtext, SPStyle const *style,
178 bool second_pass = false);
179
180 /* More general rendering methods will have to be added (like fill, stroke) */
181
182private:
189
190 float _width = 0.0;
191 float _height = 0.0;
192 unsigned _dpi = 72;
193 unsigned int _pdf_level = 1;
194 unsigned int _ps_level = 1;
195 unsigned _bitmapresolution = 72;
196
197 bool _is_valid : 1 = false;
198 bool _eps : 1 = false;
199 bool _is_texttopath : 1 = false;
200 bool _is_omittext : 1 = false;
201 bool _is_show_page : 1 = false;
202 bool _is_filtertobitmap : 1 = false;
203 // If both ps and pdf are false, then we are printing.
204 bool _is_pdf : 1 = false;
205 bool _is_ps : 1 = false;
206
207 unsigned int _clip_rule : 8;
208 unsigned int _clip_winding_failed : 1;
209 unsigned int _vector_based_target : 1 = false;
211
212 FILE *_stream = nullptr;
213
214 cairo_t *_cr = nullptr; // Cairo context
216 cairo_surface_type_t _target = CAIRO_SURFACE_TYPE_IMAGE;
217 cairo_format_t _target_format = CAIRO_FORMAT_ARGB32;
218
220 std::vector<CairoRenderState> _state_stack;
222
225
226 // Metadata to set on the cairo surface (if the surface supports it)
228 Glib::ustring title;
229 Glib::ustring author;
230 Glib::ustring subject;
231 Glib::ustring keywords;
232 Glib::ustring copyright;
233 Glib::ustring creator;
234 Glib::ustring cdate; // currently unused
235 Glib::ustring mdate; // currently unused
237
239 Geom::OptRect const &pbox, float alpha);
240 cairo_pattern_t *_createPatternPainter(SPPaintServer const *const paintserver, Geom::OptRect const &pbox);
241 cairo_pattern_t *_createHatchPainter(SPPaintServer const *const paintserver, Geom::OptRect const &pbox);
242
243 unsigned int _showGlyphs(cairo_t *cr, PangoFont *font, std::vector<CairoGlyphInfo> const &glyphtext, bool is_stroke);
244
245 bool _finishSurfaceSetup(cairo_surface_t *surface, cairo_matrix_t *ctm = nullptr);
247
248 void _setFillStyle(SPStyle const *style, Geom::OptRect const &pbox);
249 void _setStrokeStyle(SPStyle const *style, Geom::OptRect const &pbox);
250 float _mergedOpacity(float source_opacity) const;
251
252 void _concatTransform(cairo_t *cr, double xx, double yx, double xy, double yy, double x0, double y0);
254
256 void _prepareRenderText();
257
258 void _freeResources();
259
260 template <cairo_surface_type_t type>
261 bool _setVectorTarget(gchar const *utf8_fn);
262
263 std::map<gpointer, cairo_font_face_t *> _font_table;
264 static void font_data_free(gpointer data);
265
266 CairoRenderState *_addState() { return &_state_stack.emplace_back(); }
267};
268
269} // namespace Extension::Internal
270} // namespace Inkscape
271
272#endif // !EXTENSION_INTERNAL_CAIRO_RENDER_CONTEXT_H_SEEN
273
274/*
275 Local Variables:
276 mode:c++
277 c-file-style:"stroustrup"
278 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
279 indent-tabs-mode:nil
280 fill-column:99
281 End:
282*/
283// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
3x3 affine transformation matrix.
struct _PangoFont PangoFont
struct _PangoLayout PangoLayout
Cairo::RefPtr< Cairo::ImageSurface > surface
Definition canvas.cpp:137
3x3 matrix representing an affine transformation.
Definition affine.h:70
Axis-aligned rectangle that can be empty.
Definition rect.h:203
Sequence of subpaths.
Definition pathvector.h:122
void newPage()
On targets supporting multiple pages, sends subsequent rendering to a new page.
bool _finishSurfaceSetup(cairo_surface_t *surface, cairo_matrix_t *ctm=nullptr)
CairoRenderContext(CairoRenderContext const &other)=delete
cairo_pattern_t * _createPatternPainter(SPPaintServer const *const paintserver, Geom::OptRect const &pbox)
unsigned int _showGlyphs(cairo_t *cr, PangoFont *font, std::vector< CairoGlyphInfo > const &glyphtext, bool is_stroke)
void addClipPath(Geom::PathVector const &pv, SPIEnum< SPWindRule > const *fill_rule)
bool saveAsPng(const char *file_name)
Saves the contents of the context to a PNG file.
struct Inkscape::Extension::Internal::CairoRenderContext::CairoRenderContextMetadata _metadata
void setMetadata(SPDocument const &document)
Extract metadata from the document and store it in the context.
std::map< gpointer, cairo_font_face_t * > _font_table
cairo_pattern_t * _createPatternForPaintServer(SPPaintServer const *const paintserver, Geom::OptRect const &pbox, float alpha)
bool renderPathVector(Geom::PathVector const &pathv, SPStyle const *style, Geom::OptRect const &pbox, CairoPaintOrder order=STROKE_OVER_FILL)
bool renderGlyphtext(PangoFont *font, Geom::Affine const &font_matrix, std::vector< CairoGlyphInfo > const &glyphtext, SPStyle const *style, bool second_pass=false)
Called by Layout-TNG-Output, this function decides how to apply styles and write out the final shapes...
bool renderImage(Inkscape::Pixbuf const *pb, Geom::Affine const &image_transform, SPStyle const *style)
CairoRenderContext createSimilar(double width, double height) const
Creates a new render context which will be compatible with the given context's Cairo surface.
CairoRenderContext & operator=(CairoRenderContext const &other)=delete
void _concatTransform(cairo_t *cr, Geom::Affine const &transform)
bool setSurfaceTarget(cairo_surface_t *surface, bool is_vector, cairo_matrix_t *ctm=nullptr)
Set the cairo_surface_t from an external source.
bool nextPage(double width, double height, char const *label)
When writing multiple pages, resize the next page.
void _setStrokeStyle(SPStyle const *style, Geom::OptRect const &pbox)
bool finishPage()
Each page that's made should call finishPage to complete it.
void _concatTransform(cairo_t *cr, double xx, double yx, double xy, double yy, double x0, double y0)
bool setupSurface(double width, double height)
Creates the cairo_surface_t for the context with the given width, height and with the currently set t...
void addClippingRect(double x, double y, double width, double height)
cairo_pattern_t * _createHatchPainter(SPPaintServer const *const paintserver, Geom::OptRect const &pbox)
void popLayer(cairo_operator_t composite=CAIRO_OPERATOR_CLEAR)
void _setFillStyle(SPStyle const *style, Geom::OptRect const &pbox)
Class to hold image data for raster images.
Definition cairo-utils.h:31
Typed SVG document implementation.
Definition document.h:101
Enum type internal to SPStyle.
Base class for visual SVG elements.
Definition sp-item.h:109
An SVG style object.
Definition style.h:45
const unsigned order
_cairo_pattern cairo_pattern_t
struct _cairo_surface cairo_surface_t
Inkscape::Extension::Extension: Frontend to certain, possibly pluggable, actions.
Contains forward declarations of 2geom types.
SPItem * item
Glib::ustring label
Helper class to stream background task notifications as a series of messages.
int mode
struct _cairo cairo_t
Definition path-cairo.h:16
static const Point data[]
unsigned parent_has_userspace
whether the parent's ctm should be applied
unsigned need_layer
whether object is masked, clipped, and/or has a non-zero opacity
Geom::Affine item_transform
this item's item->transform, for correct clipping
unsigned merge_opacity
whether fill/stroke opacity can be mul'd with item opacity
SPStyle internal: classes that are internal to SPStyle.
double height
double width