49d47ec77bda5369b3c694749b93d74b954fa7dd
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 static void
160 feed_curve_to_cairo(cairo_t *cr, Geom::Curve const &c, Geom::Matrix & trans, Geom::Rect view, bool optimize_stroke)
161 {
162 if(Geom::LineSegment const* line_segment = dynamic_cast<Geom::LineSegment const*>(&c)) {
163 // same logic can be used for hlinesegment and vlinesegment: just use finalpoint
164 Geom::Point end_tr = (*line_segment)[1] * trans;
165 if (!optimize_stroke) {
166 cairo_line_to(cr, end_tr[0], end_tr[1]);
167 } else {
168 Geom::Rect swept((*line_segment)[0]*trans, end_tr);
169 if (swept.intersects(view)) {
170 cairo_line_to(cr, end_tr[0], end_tr[1]);
171 } else {
172 cairo_move_to(cr, end_tr[0], end_tr[1]);
173 }
174 }
175 }
176 else if(Geom::QuadraticBezier const *quadratic_bezier = dynamic_cast<Geom::QuadraticBezier const*>(&c)) {
177 std::vector<Geom::Point> points = quadratic_bezier->points();
178 points[0] *= trans;
179 points[1] *= trans;
180 points[2] *= trans;
181 Geom::Point b1 = points[0] + (2./3) * (points[1] - points[0]);
182 Geom::Point b2 = b1 + (1./3) * (points[2] - points[0]);
183 if (!optimize_stroke) {
184 cairo_curve_to(cr, b1[0], b1[1], b2[0], b2[1], points[2][0], points[2][1]);
185 } else {
186 Geom::Rect swept(points[0], points[2]);
187 swept.expandTo(points[1]);
188 if (swept.intersects(view)) {
189 cairo_curve_to(cr, b1[0], b1[1], b2[0], b2[1], points[2][0], points[2][1]);
190 } else {
191 cairo_move_to(cr, points[2][0], points[2][1]);
192 }
193 }
194 }
195 else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const*>(&c)) {
196 std::vector<Geom::Point> points = cubic_bezier->points();
197 //points[0] *= trans; // don't do this one here for fun: it is only needed for optimized strokes
198 points[1] *= trans;
199 points[2] *= trans;
200 points[3] *= trans;
201 if (!optimize_stroke) {
202 cairo_curve_to(cr, points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
203 } else {
204 points[0] *= trans; // didn't transform this point yet
205 Geom::Rect swept(points[0], points[3]);
206 swept.expandTo(points[1]);
207 swept.expandTo(points[2]);
208 if (swept.intersects(view)) {
209 cairo_curve_to(cr, points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
210 } else {
211 cairo_move_to(cr, points[3][0], points[3][1]);
212 }
213 }
214 }
215 // else if(Geom::EllipticalArc const *svg_elliptical_arc = dynamic_cast<Geom::EllipticalArc *>(c)) {
216 // //TODO: get at the innards and spit them out to cairo
217 // }
218 else {
219 //this case handles sbasis as well as all other curve types
220 Geom::Path sbasis_path = path_from_sbasis(c.toSBasis(), 0.1);
222 //recurse to convert the new path resulting from the sbasis to svgd
223 for(Geom::Path::iterator iter = sbasis_path.begin(); iter != sbasis_path.end(); ++iter) {
224 feed_curve_to_cairo(cr, *iter, trans, view, optimize_stroke);
225 }
226 }
227 }
230 /** Feeds path-creating calls to the cairo context translating them from the SPCurve, with the given transform and shift */
231 void
232 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)
233 {
234 if (!area || area->isEmpty())
235 return;
236 if (path.empty())
237 return;
239 // Transform all coordinates to coords within "area"
240 Geom::Point shift = to_2geom(area->min());
241 NR::Rect view = *area;
242 view.growBy (stroke_width);
243 view = view * from_2geom(Geom::Translate(-shift));
244 // Pass transformation to feed_curve, so that we don't need to create a whole new path.
245 Geom::Matrix transshift(trans * Geom::Translate(-shift));
247 Geom::Point initial = path.initialPoint() * transshift;
248 cairo_move_to(ct, initial[0], initial[1] );
250 for(Geom::Path::const_iterator cit = path.begin(); cit != path.end_open(); ++cit) {
251 feed_curve_to_cairo(ct, *cit, transshift, to_2geom(view), optimize_stroke);
252 }
254 if (path.closed()) {
255 cairo_line_to(ct, initial[0], initial[1]);
256 // I think we should use cairo_close_path(ct) here but it doesn't work. (the closing line is not rendered completely)
257 /* according to cairo documentation:
258 The behavior of cairo_close_path() is distinct from simply calling cairo_line_to() with the equivalent coordinate
259 in the case of stroking. When a closed sub-path is stroked, there are no caps on the ends of the sub-path. Instead,
260 there is a line join connecting the final and initial segments of the sub-path.
261 */
262 }
263 }
265 /** Feeds path-creating calls to the cairo context translating them from the SPCurve, with the given transform and shift */
266 void
267 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)
268 {
269 if (!area || area->isEmpty())
270 return;
271 if (pathv.empty())
272 return;
274 for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
275 feed_path_to_cairo(ct, *it, trans, area, optimize_stroke, stroke_width);
276 }
277 }
279 /*
280 Local Variables:
281 mode:c++
282 c-file-style:"stroustrup"
283 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
284 indent-tabs-mode:nil
285 fill-column:99
286 End:
287 */
288 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :