Code

Warning cleanup
[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 #include <typeinfo>
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
17 #include <libnr/n-art-bpath.h>
18 #include <libnr/nr-matrix-ops.h>
19 #include <libnr/nr-matrix-fns.h>
20 #include <libnr/nr-pixblock.h>
21 #include <libnr/nr-convert2geom.h>
22 #include "../style.h"
23 #include "nr-arena.h"
24 #include "sp-canvas.h"
25 #include <2geom/pathvector.h>
26 #include <2geom/matrix.h>
27 #include <2geom/point.h>
28 #include <2geom/path.h>
29 #include <2geom/transforms.h>
30 #include <2geom/sbasis-to-bezier.h>
32 /** Creates a cairo context to render to the given pixblock on the given area */
33 cairo_t *
34 nr_create_cairo_context_for_data (NRRectL *area, NRRectL *buf_area, unsigned char *px, unsigned int rowstride)
35 {
36     if (!nr_rect_l_test_intersect (buf_area, area))
37         return NULL;
39     NRRectL clip;
40     nr_rect_l_intersect (&clip, buf_area, area);
41     unsigned char *dpx = px + (clip.y0 - buf_area->y0) * rowstride + 4 * (clip.x0 - buf_area->x0);
42     int width = area->x1 - area->x0;
43     int height = area->y1 - area->y0;
44     // even though cairo cannot draw in nonpremul mode, select ARGB32 for R8G8B8A8N as the closest; later eliminate R8G8B8A8N everywhere
45     cairo_surface_t* cst = cairo_image_surface_create_for_data
46         (dpx,
47          CAIRO_FORMAT_ARGB32,
48          width,
49          height,
50          rowstride);
51     cairo_t *ct = cairo_create (cst);
53     return ct;
54 }
56 /** Creates a cairo context to render to the given SPCanvasBuf on the given area */
57 cairo_t *
58 nr_create_cairo_context_canvasbuf (NRRectL */*area*/, SPCanvasBuf *b)
59 {
60     return nr_create_cairo_context_for_data (&(b->rect), &(b->rect), b->buf, b->buf_rowstride);
61 }
64 /** Creates a cairo context to render to the given NRPixBlock on the given area */
65 cairo_t *
66 nr_create_cairo_context (NRRectL *area, NRPixBlock *pb)
67 {
68     return nr_create_cairo_context_for_data (area, &(pb->area), NR_PIXBLOCK_PX (pb), pb->rs);
69 }
71 /** Feeds path-creating calls to the cairo context translating them from the SPCurve, with the given transform and shift */
72 void
73 feed_curve_to_cairo (cairo_t *ct, NArtBpath const *bpath, NR::Matrix trans, NR::Maybe<NR::Rect> area, bool optimize_stroke, double stroke_width)
74 {
75     NR::Point next(0,0), last(0,0);
76     if (!area || area->isEmpty())
77         return;
78     NR::Point shift = area->min();
79     NR::Rect view = *area;
80     view.growBy (stroke_width);
81     NR::Rect swept;
82     bool  closed = false;
83     NR::Point startpath(0,0);
84     for (int i = 0; bpath[i].code != NR_END; i++) {
85         switch (bpath[i].code) {
86             case NR_MOVETO_OPEN:
87             case NR_MOVETO:
88                 if (closed) {
89                     // we cannot use close_path because some of the curves/lines may have been optimized out
90                     cairo_line_to(ct, startpath[NR::X], startpath[NR::Y]);
91                 }
92                 next[NR::X] = bpath[i].x3;
93                 next[NR::Y] = bpath[i].y3;
94                 next *= trans;
95                 last = next;
96                 next -= shift;
97                 if (bpath[i].code == NR_MOVETO) {
98                     // remember the start point of the subpath, for closing it later
99                     closed = true;
100                     startpath = next;
101                 } else {
102                     closed = false;
103                 }
104                 cairo_move_to(ct, next[NR::X], next[NR::Y]);
105                 break;
107             case NR_LINETO:
108                 next[NR::X] = bpath[i].x3;
109                 next[NR::Y] = bpath[i].y3;
110                 next *= trans;
111                 if (optimize_stroke) {
112                     swept = NR::Rect(last, next);
113                     //std::cout << "swept: " << swept;
114                     //std::cout << "view: " << view;
115                     //std::cout << "intersects? " << (swept.intersects(view)? "YES" : "NO") << "\n";
116                 }
117                 last = next;
118                 next -= shift;
119                 if (!optimize_stroke || swept.intersects(view))
120                     cairo_line_to(ct, next[NR::X], next[NR::Y]);
121                 else
122                     cairo_move_to(ct, next[NR::X], next[NR::Y]);
123                 break;
125             case NR_CURVETO: {
126                 NR::Point  tm1, tm2, tm3;
127                 tm1[0]=bpath[i].x1;
128                 tm1[1]=bpath[i].y1;
129                 tm2[0]=bpath[i].x2;
130                 tm2[1]=bpath[i].y2;
131                 tm3[0]=bpath[i].x3;
132                 tm3[1]=bpath[i].y3;
133                 tm1 *= trans;
134                 tm2 *= trans;
135                 tm3 *= trans;
136                 if (optimize_stroke) {
137                     swept = NR::Rect(last, last);
138                     swept.expandTo(tm1);
139                     swept.expandTo(tm2);
140                     swept.expandTo(tm3);
141                 }
142                 last = tm3;
143                 tm1 -= shift;
144                 tm2 -= shift;
145                 tm3 -= shift;
146                 if (!optimize_stroke || swept.intersects(view))
147                     cairo_curve_to (ct, tm1[NR::X], tm1[NR::Y], tm2[NR::X], tm2[NR::Y], tm3[NR::X], tm3[NR::Y]);
148                 else
149                     cairo_move_to(ct, tm3[NR::X], tm3[NR::Y]);
150                 break;
151             }
153             default:
154                 break;
155         }
156     }
160 static void
161 feed_curve_to_cairo(cairo_t *cr, Geom::Curve const &c, Geom::Matrix & trans, Geom::Rect view, bool optimize_stroke)
163     if( typeid(c) == typeid(Geom::LineSegment) ||
164         typeid(c) == typeid(Geom::HLineSegment) ||
165         typeid(c) == typeid(Geom::VLineSegment) )
166     {
167         Geom::Point end_tr = c.finalPoint() * trans;
168         if (!optimize_stroke) {
169             cairo_line_to(cr, end_tr[0], end_tr[1]);
170         } else {
171             Geom::Rect swept(c.initialPoint()*trans, end_tr);
172             if (swept.intersects(view)) {
173                 cairo_line_to(cr, end_tr[0], end_tr[1]);
174             } else {
175                 cairo_move_to(cr, end_tr[0], end_tr[1]);
176             }
177         }
178     }
179     else if(Geom::QuadraticBezier const *quadratic_bezier = dynamic_cast<Geom::QuadraticBezier const*>(&c)) {
180         std::vector<Geom::Point> points = quadratic_bezier->points();
181         points[0] *= trans;
182         points[1] *= trans;
183         points[2] *= trans;
184         Geom::Point b1 = points[0] + (2./3) * (points[1] - points[0]);
185         Geom::Point b2 = b1 + (1./3) * (points[2] - points[0]);
186         if (!optimize_stroke) {
187             cairo_curve_to(cr, b1[0], b1[1], b2[0], b2[1], points[2][0], points[2][1]);
188         } else {
189             Geom::Rect swept(points[0], points[2]);
190             swept.expandTo(points[1]);
191             if (swept.intersects(view)) {
192                 cairo_curve_to(cr, b1[0], b1[1], b2[0], b2[1], points[2][0], points[2][1]);
193             } else {
194                 cairo_move_to(cr, points[2][0], points[2][1]);
195             }
196         }
197     }
198     else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const*>(&c)) {
199         std::vector<Geom::Point> points = cubic_bezier->points();
200         //points[0] *= trans; // don't do this one here for fun: it is only needed for optimized strokes
201         points[1] *= trans;
202         points[2] *= trans;
203         points[3] *= trans;
204         if (!optimize_stroke) {
205             cairo_curve_to(cr, points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
206         } else {
207             points[0] *= trans;  // didn't transform this point yet
208             Geom::Rect swept(points[0], points[3]);
209             swept.expandTo(points[1]);
210             swept.expandTo(points[2]);
211             if (swept.intersects(view)) {
212                 cairo_curve_to(cr, points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
213             } else {
214                 cairo_move_to(cr, points[3][0], points[3][1]);
215             }
216         }
217     }
218 //    else if(Geom::EllipticalArc const *svg_elliptical_arc = dynamic_cast<Geom::EllipticalArc *>(c)) {
219 //        //TODO: get at the innards and spit them out to cairo
220 //    }
221     else {
222         //this case handles sbasis as well as all other curve types
223         Geom::Path sbasis_path = path_from_sbasis(c.toSBasis(), 0.1);
225         //recurse to convert the new path resulting from the sbasis to svgd
226         for(Geom::Path::iterator iter = sbasis_path.begin(); iter != sbasis_path.end(); ++iter) {
227             feed_curve_to_cairo(cr, *iter, trans, view, optimize_stroke);
228         }
229     }
233 /** Feeds path-creating calls to the cairo context translating them from the SPCurve, with the given transform and shift */
234 void
235 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)
237     if (!area || area->isEmpty())
238         return;
239     if (path.empty())
240         return;
242     // Transform all coordinates to coords within "area"
243     Geom::Point shift = to_2geom(area->min());
244     NR::Rect view = *area;
245     view.growBy (stroke_width);
246     view = view * from_2geom(Geom::Translate(-shift));
247     //  Pass transformation to feed_curve, so that we don't need to create a whole new path.
248     Geom::Matrix transshift(trans * Geom::Translate(-shift));
250     Geom::Point initial = path.initialPoint() * transshift;
251     cairo_move_to(ct, initial[0], initial[1] );
253     for(Geom::Path::const_iterator cit = path.begin(); cit != path.end_open(); ++cit) {
254         feed_curve_to_cairo(ct, *cit, transshift, to_2geom(view), optimize_stroke);
255     }
257     if (path.closed()) {
258         cairo_line_to(ct, initial[0], initial[1]);
259         // I think we should use cairo_close_path(ct) here but it doesn't work. (the closing line is not rendered completely)
260         /* according to cairo documentation:
261            The behavior of cairo_close_path() is distinct from simply calling cairo_line_to() with the equivalent coordinate
262            in the case of stroking. When a closed sub-path is stroked, there are no caps on the ends of the sub-path. Instead,
263            there is a line join connecting the final and initial segments of the sub-path. 
264         */
265     }
268 /** Feeds path-creating calls to the cairo context translating them from the SPCurve, with the given transform and shift */
269 void
270 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)
272     if (!area || area->isEmpty())
273         return;
274     if (pathv.empty())
275         return;
277     for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
278         feed_path_to_cairo(ct, *it, trans, area, optimize_stroke, stroke_width);
279     }
282 /*
283   Local Variables:
284   mode:c++
285   c-file-style:"stroustrup"
286   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
287   indent-tabs-mode:nil
288   fill-column:99
289   End:
290 */
291 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :