Code

063b56ae57829fe3d1bb9badcd4110a9d7a32873
[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::Rect view, bool optimize_stroke)
162     if(Geom::LineSegment const* line_segment = dynamic_cast<Geom::LineSegment const*>(&c)) {
163         if (!optimize_stroke) {
164             cairo_line_to(cr, (*line_segment)[1][0], (*line_segment)[1][1]);
165         } else {
166             Geom::Rect swept((*line_segment)[0], (*line_segment)[1]);
167             if (swept.intersects(view)) {
168                 cairo_line_to(cr, (*line_segment)[1][0], (*line_segment)[1][1]);
169             } else {
170                 cairo_move_to(cr, (*line_segment)[1][0], (*line_segment)[1][1]);
171             }
172         }
173     }
174     else if(Geom::QuadraticBezier const *quadratic_bezier = dynamic_cast<Geom::QuadraticBezier const*>(&c)) {
175         std::vector<Geom::Point> points = quadratic_bezier->points();
176         Geom::Point b1 = points[0] + (2./3) * (points[1] - points[0]);
177         Geom::Point b2 = b1 + (1./3) * (points[2] - points[0]);
178         if (!optimize_stroke) {
179             cairo_curve_to(cr, b1[0], b1[1], b2[0], b2[1], points[2][0], points[2][1]);
180         } else {
181             Geom::Rect swept(points[0], points[2]);
182             swept.expandTo(points[1]);
183             if (swept.intersects(view)) {
184                 cairo_curve_to(cr, b1[0], b1[1], b2[0], b2[1], points[2][0], points[2][1]);
185             } else {
186                 cairo_move_to(cr, points[2][0], points[2][1]);
187             }
188         }
189     }
190     else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const*>(&c)) {
191         std::vector<Geom::Point> points = cubic_bezier->points();
192         if (!optimize_stroke) {
193             cairo_curve_to(cr, points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
194         } else {
195             Geom::Rect swept(points[0], points[3]);
196             swept.expandTo(points[1]);
197             swept.expandTo(points[2]);
198             if (swept.intersects(view)) {
199                 cairo_curve_to(cr, points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
200             } else {
201                 cairo_move_to(cr, points[3][0], points[3][1]);
202             }
203         }
204     }
205 //    else if(Geom::EllipticalArc const *svg_elliptical_arc = dynamic_cast<Geom::EllipticalArc *>(c)) {
206 //        //TODO: get at the innards and spit them out to cairo
207 //    }
208     else {
209         //this case handles sbasis as well as all other curve types
210         Geom::Path sbasis_path = path_from_sbasis(c.toSBasis(), 0.1);
212         //recurse to convert the new path resulting from the sbasis to svgd
213         for(Geom::Path::iterator iter = sbasis_path.begin(); iter != sbasis_path.end(); ++iter) {
214             feed_curve_to_cairo(cr, *iter, view, optimize_stroke);
215         }
216     }
220 /** Feeds path-creating calls to the cairo context translating them from the SPCurve, with the given transform and shift */
221 void
222 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)
224     if (!area || area->isEmpty())
225         return;
226     if (path.empty())
227         return;
229     // Transform all coordinates to coords within "area"
230     Geom::Point shift = to_2geom(area->min());
231     NR::Rect view = *area;
232     view.growBy (stroke_width);
233     view = view * from_2geom(Geom::Translate(-shift));
234     Geom::Path const path_trans = path * (trans * Geom::Translate(-shift));
236     cairo_move_to(ct, path_trans.initialPoint()[0], path_trans.initialPoint()[1] );
238     for(Geom::Path::const_iterator cit = path_trans.begin(); cit != path_trans.end_open(); ++cit) {
239         feed_curve_to_cairo(ct, *cit, to_2geom(view), optimize_stroke);
240     }
242     if (path_trans.closed()) {
243         cairo_line_to(ct, path_trans.initialPoint()[0], path_trans.initialPoint()[1] );
244     }
247 /** Feeds path-creating calls to the cairo context translating them from the SPCurve, with the given transform and shift */
248 void
249 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)
251     if (!area || area->isEmpty())
252         return;
253     if (pathv.empty())
254         return;
256     for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
257         feed_path_to_cairo(ct, *it, trans, area, optimize_stroke, stroke_width);
258     }
261 /*
262   Local Variables:
263   mode:c++
264   c-file-style:"stroustrup"
265   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
266   indent-tabs-mode:nil
267   fill-column:99
268   End:
269 */
270 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :