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
16 #include <libnr/n-art-bpath.h>
17 #include <libnr/nr-matrix-ops.h>
18 #include <libnr/nr-matrix-fns.h>
19 #include <libnr/nr-pixblock.h>
20 #include <libnr/nr-convert2geom.h>
21 #include "../style.h"
22 #include "nr-arena.h"
23 #include "sp-canvas.h"
24 #include <2geom/pathvector.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 (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 /** Feeds path-creating calls to the cairo context translating them from the SPCurve, with the given transform and shift */
71 void
72 feed_curve_to_cairo (cairo_t *ct, NArtBpath const *bpath, NR::Matrix trans, NR::Maybe<NR::Rect> area, bool optimize_stroke, double stroke_width)
73 {
74 NR::Point next(0,0), last(0,0);
75 if (!area || area->isEmpty())
76 return;
77 NR::Point shift = area->min();
78 NR::Rect view = *area;
79 view.growBy (stroke_width);
80 NR::Rect swept;
81 bool closed = false;
82 NR::Point startpath(0,0);
83 for (int i = 0; bpath[i].code != NR_END; i++) {
84 switch (bpath[i].code) {
85 case NR_MOVETO_OPEN:
86 case NR_MOVETO:
87 if (closed) {
88 // we cannot use close_path because some of the curves/lines may have been optimized out
89 cairo_line_to(ct, startpath[NR::X], startpath[NR::Y]);
90 }
91 next[NR::X] = bpath[i].x3;
92 next[NR::Y] = bpath[i].y3;
93 next *= trans;
94 last = next;
95 next -= shift;
96 if (bpath[i].code == NR_MOVETO) {
97 // remember the start point of the subpath, for closing it later
98 closed = true;
99 startpath = next;
100 } else {
101 closed = false;
102 }
103 cairo_move_to(ct, next[NR::X], next[NR::Y]);
104 break;
106 case NR_LINETO:
107 next[NR::X] = bpath[i].x3;
108 next[NR::Y] = bpath[i].y3;
109 next *= trans;
110 if (optimize_stroke) {
111 swept = NR::Rect(last, next);
112 //std::cout << "swept: " << swept;
113 //std::cout << "view: " << view;
114 //std::cout << "intersects? " << (swept.intersects(view)? "YES" : "NO") << "\n";
115 }
116 last = next;
117 next -= shift;
118 if (!optimize_stroke || swept.intersects(view))
119 cairo_line_to(ct, next[NR::X], next[NR::Y]);
120 else
121 cairo_move_to(ct, next[NR::X], next[NR::Y]);
122 break;
124 case NR_CURVETO: {
125 NR::Point tm1, tm2, tm3;
126 tm1[0]=bpath[i].x1;
127 tm1[1]=bpath[i].y1;
128 tm2[0]=bpath[i].x2;
129 tm2[1]=bpath[i].y2;
130 tm3[0]=bpath[i].x3;
131 tm3[1]=bpath[i].y3;
132 tm1 *= trans;
133 tm2 *= trans;
134 tm3 *= trans;
135 if (optimize_stroke) {
136 swept = NR::Rect(last, last);
137 swept.expandTo(tm1);
138 swept.expandTo(tm2);
139 swept.expandTo(tm3);
140 }
141 last = tm3;
142 tm1 -= shift;
143 tm2 -= shift;
144 tm3 -= shift;
145 if (!optimize_stroke || swept.intersects(view))
146 cairo_curve_to (ct, tm1[NR::X], tm1[NR::Y], tm2[NR::X], tm2[NR::Y], tm3[NR::X], tm3[NR::Y]);
147 else
148 cairo_move_to(ct, tm3[NR::X], tm3[NR::Y]);
149 break;
150 }
152 default:
153 break;
154 }
155 }
156 }
159 /*
160 * Can be called recursively.
161 * If optimize_stroke == false, the view Rect is not used.
162 */
163 static void
164 feed_curve_to_cairo(cairo_t *cr, Geom::Curve const &c, Geom::Matrix const & trans, Geom::Rect view, bool optimize_stroke)
165 {
166 if( dynamic_cast<Geom::LineSegment const*>(&c) ||
167 dynamic_cast<Geom::HLineSegment const*>(&c) ||
168 dynamic_cast<Geom::VLineSegment const*>(&c) )
169 {
170 Geom::Point end_tr = c.finalPoint() * trans;
171 if (!optimize_stroke) {
172 cairo_line_to(cr, end_tr[0], end_tr[1]);
173 } else {
174 Geom::Rect swept(c.initialPoint()*trans, end_tr);
175 if (swept.intersects(view)) {
176 cairo_line_to(cr, end_tr[0], end_tr[1]);
177 } else {
178 cairo_move_to(cr, end_tr[0], end_tr[1]);
179 }
180 }
181 }
182 else if(Geom::QuadraticBezier const *quadratic_bezier = dynamic_cast<Geom::QuadraticBezier const*>(&c)) {
183 std::vector<Geom::Point> points = quadratic_bezier->points();
184 points[0] *= trans;
185 points[1] *= trans;
186 points[2] *= trans;
187 Geom::Point b1 = points[0] + (2./3) * (points[1] - points[0]);
188 Geom::Point b2 = b1 + (1./3) * (points[2] - points[0]);
189 if (!optimize_stroke) {
190 cairo_curve_to(cr, b1[0], b1[1], b2[0], b2[1], points[2][0], points[2][1]);
191 } else {
192 Geom::Rect swept(points[0], points[2]);
193 swept.expandTo(points[1]);
194 if (swept.intersects(view)) {
195 cairo_curve_to(cr, b1[0], b1[1], b2[0], b2[1], points[2][0], points[2][1]);
196 } else {
197 cairo_move_to(cr, points[2][0], points[2][1]);
198 }
199 }
200 }
201 else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const*>(&c)) {
202 std::vector<Geom::Point> points = cubic_bezier->points();
203 //points[0] *= trans; // don't do this one here for fun: it is only needed for optimized strokes
204 points[1] *= trans;
205 points[2] *= trans;
206 points[3] *= trans;
207 if (!optimize_stroke) {
208 cairo_curve_to(cr, points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
209 } else {
210 points[0] *= trans; // didn't transform this point yet
211 Geom::Rect swept(points[0], points[3]);
212 swept.expandTo(points[1]);
213 swept.expandTo(points[2]);
214 if (swept.intersects(view)) {
215 cairo_curve_to(cr, points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
216 } else {
217 cairo_move_to(cr, points[3][0], points[3][1]);
218 }
219 }
220 }
221 // else if(Geom::EllipticalArc const *svg_elliptical_arc = dynamic_cast<Geom::EllipticalArc *>(c)) {
222 // //TODO: get at the innards and spit them out to cairo
223 // }
224 else {
225 //this case handles sbasis as well as all other curve types
226 Geom::Path sbasis_path = Geom::cubicbezierpath_from_sbasis(c.toSBasis(), 0.1);
228 //recurse to convert the new path resulting from the sbasis to svgd
229 for(Geom::Path::iterator iter = sbasis_path.begin(); iter != sbasis_path.end(); ++iter) {
230 feed_curve_to_cairo(cr, *iter, trans, view, optimize_stroke);
231 }
232 }
233 }
236 /** Feeds path-creating calls to the cairo context translating them from the Path, with the given transform and shift */
237 void
238 feed_path_to_cairo (cairo_t *ct, Geom::Path const &path)
239 {
240 if (path.empty())
241 return;
243 cairo_move_to(ct, path.initialPoint()[0], path.initialPoint()[1] );
245 for(Geom::Path::const_iterator cit = path.begin(); cit != path.end_open(); ++cit) {
246 feed_curve_to_cairo(ct, *cit, Geom::identity(), Geom::Rect(), false); // optimize_stroke is false, so the view rect is not used
247 }
249 if (path.closed()) {
250 cairo_line_to(ct, path.initialPoint()[0], path.initialPoint()[1]);
251 // cairo_close_path(ct);
252 /* I think we should use cairo_close_path(ct) here but it doesn't work. (the closing line is not rendered completely)
253 According to cairo documentation:
254 The behavior of cairo_close_path() is distinct from simply calling cairo_line_to() with the equivalent coordinate
255 in the case of stroking. When a closed sub-path is stroked, there are no caps on the ends of the sub-path. Instead,
256 there is a line join connecting the final and initial segments of the sub-path.
257 */
258 }
259 }
261 /** Feeds path-creating calls to the cairo context translating them from the Path, with the given transform and shift */
262 void
263 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)
264 {
265 if (!area || area->isEmpty())
266 return;
267 if (path.empty())
268 return;
270 // Transform all coordinates to coords within "area"
271 Geom::Point shift = to_2geom(area->min());
272 NR::Rect view = *area;
273 view.growBy (stroke_width);
274 view = view * from_2geom(Geom::Translate(-shift));
275 // Pass transformation to feed_curve, so that we don't need to create a whole new path.
276 Geom::Matrix transshift(trans * Geom::Translate(-shift));
278 Geom::Point initial = path.initialPoint() * transshift;
279 cairo_move_to(ct, initial[0], initial[1] );
281 for(Geom::Path::const_iterator cit = path.begin(); cit != path.end_open(); ++cit) {
282 feed_curve_to_cairo(ct, *cit, transshift, to_2geom(view), optimize_stroke);
283 }
285 if (path.closed()) {
286 cairo_line_to(ct, initial[0], initial[1]);
287 // cairo_close_path(ct);
288 /* I think we should use cairo_close_path(ct) here but it doesn't work. (the closing line is not rendered completely)
289 According to cairo documentation:
290 The behavior of cairo_close_path() is distinct from simply calling cairo_line_to() with the equivalent coordinate
291 in the case of stroking. When a closed sub-path is stroked, there are no caps on the ends of the sub-path. Instead,
292 there is a line join connecting the final and initial segments of the sub-path.
293 */
294 }
295 }
297 /** Feeds path-creating calls to the cairo context translating them from the PathVector, with the given transform and shift */
298 void
299 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)
300 {
301 if (!area || area->isEmpty())
302 return;
303 if (pathv.empty())
304 return;
306 for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
307 feed_path_to_cairo(ct, *it, trans, area, optimize_stroke, stroke_width);
308 }
309 }
311 /** Feeds path-creating calls to the cairo context translating them from the PathVector */
312 void
313 feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv)
314 {
315 if (pathv.empty())
316 return;
318 for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
319 feed_path_to_cairo(ct, *it);
320 }
321 }
323 /*
324 Local Variables:
325 mode:c++
326 c-file-style:"stroustrup"
327 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
328 indent-tabs-mode:nil
329 fill-column:99
330 End:
331 */
332 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :