Code

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