d4a9f7062b71572ea11777b34d2f4a28c957c8dd
1 /*
2 * Helper functions to use cairo with inkscape
3 *
4 * Copyright (C) 2007 bulia byak
5 * Copyright (C) 2008 Johan Engelen
6 *
7 * Released under GNU GPL
8 *
9 */
11 #include <cairo.h>
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
17 #include <libnr/nr-pixblock.h>
18 #include <libnr/nr-convert2geom.h>
19 #include "../style.h"
20 #include "nr-arena.h"
21 #include "sp-canvas.h"
22 #include <2geom/pathvector.h>
23 #include <2geom/bezier-curve.h>
24 #include <2geom/hvlinesegment.h>
25 #include <2geom/matrix.h>
26 #include <2geom/point.h>
27 #include <2geom/path.h>
28 #include <2geom/transforms.h>
29 #include <2geom/sbasis-to-bezier.h>
31 /** Creates a cairo context to render to the given pixblock on the given area */
32 cairo_t *
33 nr_create_cairo_context_for_data (NRRectL *area, NRRectL *buf_area, unsigned char *px, unsigned int rowstride)
34 {
35 if (!nr_rect_l_test_intersect_ptr(buf_area, area))
36 return NULL;
38 NRRectL clip;
39 nr_rect_l_intersect (&clip, buf_area, area);
40 unsigned char *dpx = px + (clip.y0 - buf_area->y0) * rowstride + 4 * (clip.x0 - buf_area->x0);
41 int width = area->x1 - area->x0;
42 int height = area->y1 - area->y0;
43 // even though cairo cannot draw in nonpremul mode, select ARGB32 for R8G8B8A8N as the closest; later eliminate R8G8B8A8N everywhere
44 cairo_surface_t* cst = cairo_image_surface_create_for_data
45 (dpx,
46 CAIRO_FORMAT_ARGB32,
47 width,
48 height,
49 rowstride);
50 cairo_t *ct = cairo_create (cst);
52 return ct;
53 }
55 /** Creates a cairo context to render to the given SPCanvasBuf on the given area */
56 cairo_t *
57 nr_create_cairo_context_canvasbuf (NRRectL */*area*/, SPCanvasBuf *b)
58 {
59 return nr_create_cairo_context_for_data (&(b->rect), &(b->rect), b->buf, b->buf_rowstride);
60 }
63 /** Creates a cairo context to render to the given NRPixBlock on the given area */
64 cairo_t *
65 nr_create_cairo_context (NRRectL *area, NRPixBlock *pb)
66 {
67 return nr_create_cairo_context_for_data (area, &(pb->area), NR_PIXBLOCK_PX (pb), pb->rs);
68 }
70 /*
71 * Can be called recursively.
72 * If optimize_stroke == false, the view Rect is not used.
73 */
74 static void
75 feed_curve_to_cairo(cairo_t *cr, Geom::Curve const &c, Geom::Matrix const & trans, Geom::Rect view, bool optimize_stroke)
76 {
77 if( dynamic_cast<Geom::LineSegment const*>(&c) ||
78 dynamic_cast<Geom::HLineSegment const*>(&c) ||
79 dynamic_cast<Geom::VLineSegment const*>(&c) )
80 {
81 Geom::Point end_tr = c.finalPoint() * trans;
82 if (!optimize_stroke) {
83 cairo_line_to(cr, end_tr[0], end_tr[1]);
84 } else {
85 Geom::Rect swept(c.initialPoint()*trans, end_tr);
86 if (swept.intersects(view)) {
87 cairo_line_to(cr, end_tr[0], end_tr[1]);
88 } else {
89 cairo_move_to(cr, end_tr[0], end_tr[1]);
90 }
91 }
92 }
93 else if(Geom::QuadraticBezier const *quadratic_bezier = dynamic_cast<Geom::QuadraticBezier const*>(&c)) {
94 std::vector<Geom::Point> points = quadratic_bezier->points();
95 points[0] *= trans;
96 points[1] *= trans;
97 points[2] *= trans;
98 Geom::Point b1 = points[0] + (2./3) * (points[1] - points[0]);
99 Geom::Point b2 = b1 + (1./3) * (points[2] - points[0]);
100 if (!optimize_stroke) {
101 cairo_curve_to(cr, b1[0], b1[1], b2[0], b2[1], points[2][0], points[2][1]);
102 } else {
103 Geom::Rect swept(points[0], points[2]);
104 swept.expandTo(points[1]);
105 if (swept.intersects(view)) {
106 cairo_curve_to(cr, b1[0], b1[1], b2[0], b2[1], points[2][0], points[2][1]);
107 } else {
108 cairo_move_to(cr, points[2][0], points[2][1]);
109 }
110 }
111 }
112 else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const*>(&c)) {
113 std::vector<Geom::Point> points = cubic_bezier->points();
114 //points[0] *= trans; // don't do this one here for fun: it is only needed for optimized strokes
115 points[1] *= trans;
116 points[2] *= trans;
117 points[3] *= trans;
118 if (!optimize_stroke) {
119 cairo_curve_to(cr, points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
120 } else {
121 points[0] *= trans; // didn't transform this point yet
122 Geom::Rect swept(points[0], points[3]);
123 swept.expandTo(points[1]);
124 swept.expandTo(points[2]);
125 if (swept.intersects(view)) {
126 cairo_curve_to(cr, points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
127 } else {
128 cairo_move_to(cr, points[3][0], points[3][1]);
129 }
130 }
131 }
132 // else if(Geom::SVGEllipticalArc const *svg_elliptical_arc = dynamic_cast<Geom::SVGEllipticalArc *>(c)) {
133 // //TODO: get at the innards and spit them out to cairo
134 // }
135 else {
136 //this case handles sbasis as well as all other curve types
137 Geom::Path sbasis_path = Geom::cubicbezierpath_from_sbasis(c.toSBasis(), 0.1);
139 //recurse to convert the new path resulting from the sbasis to svgd
140 for(Geom::Path::iterator iter = sbasis_path.begin(); iter != sbasis_path.end(); ++iter) {
141 feed_curve_to_cairo(cr, *iter, trans, view, optimize_stroke);
142 }
143 }
144 }
147 /** Feeds path-creating calls to the cairo context translating them from the Path */
148 static void
149 feed_path_to_cairo (cairo_t *ct, Geom::Path const &path)
150 {
151 if (path.empty())
152 return;
154 cairo_move_to(ct, path.initialPoint()[0], path.initialPoint()[1] );
156 for(Geom::Path::const_iterator cit = path.begin(); cit != path.end_open(); ++cit) {
157 feed_curve_to_cairo(ct, *cit, Geom::identity(), Geom::Rect(), false); // optimize_stroke is false, so the view rect is not used
158 }
160 if (path.closed()) {
161 cairo_line_to(ct, path.initialPoint()[0], path.initialPoint()[1]);
162 // cairo_close_path(ct);
163 /* I think we should use cairo_close_path(ct) here but it doesn't work. (the closing line is not rendered completely)
164 According to cairo documentation:
165 The behavior of cairo_close_path() is distinct from simply calling cairo_line_to() with the equivalent coordinate
166 in the case of stroking. When a closed sub-path is stroked, there are no caps on the ends of the sub-path. Instead,
167 there is a line join connecting the final and initial segments of the sub-path.
168 */
169 }
170 }
172 /** Feeds path-creating calls to the cairo context translating them from the Path, with the given transform and shift */
173 static void
174 feed_path_to_cairo (cairo_t *ct, Geom::Path const &path, Geom::Matrix trans, NR::Maybe<NR::Rect> area, bool optimize_stroke, double stroke_width)
175 {
176 if (!area || area->isEmpty())
177 return;
178 if (path.empty())
179 return;
181 // Transform all coordinates to coords within "area"
182 Geom::Point shift = to_2geom(area->min());
183 NR::Rect view = *area;
184 view.growBy (stroke_width);
185 view = view * from_2geom(Geom::Translate(-shift));
186 // Pass transformation to feed_curve, so that we don't need to create a whole new path.
187 Geom::Matrix transshift(trans * Geom::Translate(-shift));
189 Geom::Point initial = path.initialPoint() * transshift;
190 cairo_move_to(ct, initial[0], initial[1] );
192 for(Geom::Path::const_iterator cit = path.begin(); cit != path.end_open(); ++cit) {
193 feed_curve_to_cairo(ct, *cit, transshift, to_2geom(view), optimize_stroke);
194 }
196 if (path.closed()) {
197 cairo_line_to(ct, initial[0], initial[1]);
198 /* It seems we cannot use cairo_close_path(ct) here; maybe because some parts of the path have been clipped and not drawn (maybe the before last segment was outside view area)
199 However, according to cairo documentation:
200 The behavior of cairo_close_path() is distinct from simply calling cairo_line_to() with the equivalent coordinate
201 in the case of stroking. When a closed sub-path is stroked, there are no caps on the ends of the sub-path. Instead,
202 there is a line join connecting the final and initial segments of the sub-path.
203 */
204 }
205 }
207 /** Feeds path-creating calls to the cairo context translating them from the PathVector, with the given transform and shift
208 * One must have done cairo_new_path(ct); before calling this function. */
209 void
210 feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv, Geom::Matrix trans, NR::Maybe<NR::Rect> area, bool optimize_stroke, double stroke_width)
211 {
212 if (!area || area->isEmpty())
213 return;
214 if (pathv.empty())
215 return;
217 for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
218 feed_path_to_cairo(ct, *it, trans, area, optimize_stroke, stroke_width);
219 }
220 }
222 /** Feeds path-creating calls to the cairo context translating them from the PathVector
223 * One must have done cairo_new_path(ct); before calling this function. */
224 void
225 feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv)
226 {
227 if (pathv.empty())
228 return;
230 for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
231 feed_path_to_cairo(ct, *it);
232 }
233 }
235 /*
236 Local Variables:
237 mode:c++
238 c-file-style:"stroustrup"
239 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
240 indent-tabs-mode:nil
241 fill-column:99
242 End:
243 */
244 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :