Code

027a89e828e0c326eade6a4aa177a9690a09bd79
[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 <2geom/pathvector.h>
24 #include <2geom/matrix.h>
25 #include <2geom/point.h>
26 #include <2geom/path.h>
27 #include <2geom/transforms.h>
28 #include <2geom/sbasis-to-bezier.h>
30 /** Creates a cairo context to render to the given pixblock on the given area */
31 cairo_t *
32 nr_create_cairo_context (NRRectL *area, NRPixBlock *pb)
33 {
34     if (!nr_rect_l_test_intersect (&pb->area, area))
35         return NULL;
37     NRRectL clip;
38     nr_rect_l_intersect (&clip, &pb->area, area);
39     unsigned char *dpx = NR_PIXBLOCK_PX (pb) + (clip.y0 - pb->area.y0) * pb->rs + NR_PIXBLOCK_BPP (pb) * (clip.x0 - pb->area.x0);
40     int width = area->x1 - area->x0;
41     int height = area->y1 - area->y0;
42     // even though cairo cannot draw in nonpremul mode, select ARGB32 for R8G8B8A8N as the closest; later eliminate R8G8B8A8N everywhere
43     cairo_surface_t* cst = cairo_image_surface_create_for_data
44         (dpx,
45          ((pb->mode == NR_PIXBLOCK_MODE_R8G8B8A8P || pb->mode == NR_PIXBLOCK_MODE_R8G8B8A8N) ? CAIRO_FORMAT_ARGB32 : (pb->mode == NR_PIXBLOCK_MODE_R8G8B8? CAIRO_FORMAT_RGB24 : CAIRO_FORMAT_A8)),
46          width,
47          height,
48          pb->rs);
49     cairo_t *ct = cairo_create (cst);
51     return ct;
52 }
54 /** Feeds path-creating calls to the cairo context translating them from the SPCurve, with the given transform and shift */
55 void
56 feed_curve_to_cairo (cairo_t *ct, NArtBpath const *bpath, NR::Matrix trans, NR::Maybe<NR::Rect> area, bool optimize_stroke, double stroke_width)
57 {
58     NR::Point next(0,0), last(0,0);
59     if (!area || area->isEmpty())
60         return;
61     NR::Point shift = area->min();
62     NR::Rect view = *area;
63     view.growBy (stroke_width);
64     NR::Rect swept;
65     bool  closed = false;
66     NR::Point startpath(0,0);
67     for (int i = 0; bpath[i].code != NR_END; i++) {
68         switch (bpath[i].code) {
69             case NR_MOVETO_OPEN:
70             case NR_MOVETO:
71                 if (closed) {
72                     // we cannot use close_path because some of the curves/lines may have been optimized out
73                     cairo_line_to(ct, startpath[NR::X], startpath[NR::Y]);
74                 }
75                 next[NR::X] = bpath[i].x3;
76                 next[NR::Y] = bpath[i].y3;
77                 next *= trans;
78                 last = next;
79                 next -= shift;
80                 if (bpath[i].code == NR_MOVETO) {
81                     // remember the start point of the subpath, for closing it later
82                     closed = true;
83                     startpath = next;
84                 } else {
85                     closed = false;
86                 }
87                 cairo_move_to(ct, next[NR::X], next[NR::Y]);
88                 break;
90             case NR_LINETO:
91                 next[NR::X] = bpath[i].x3;
92                 next[NR::Y] = bpath[i].y3;
93                 next *= trans;
94                 if (optimize_stroke) {
95                     swept = NR::Rect(last, next);
96                     //std::cout << "swept: " << swept;
97                     //std::cout << "view: " << view;
98                     //std::cout << "intersects? " << (swept.intersects(view)? "YES" : "NO") << "\n";
99                 }
100                 last = next;
101                 next -= shift;
102                 if (!optimize_stroke || swept.intersects(view))
103                     cairo_line_to(ct, next[NR::X], next[NR::Y]);
104                 else
105                     cairo_move_to(ct, next[NR::X], next[NR::Y]);
106                 break;
108             case NR_CURVETO: {
109                 NR::Point  tm1, tm2, tm3;
110                 tm1[0]=bpath[i].x1;
111                 tm1[1]=bpath[i].y1;
112                 tm2[0]=bpath[i].x2;
113                 tm2[1]=bpath[i].y2;
114                 tm3[0]=bpath[i].x3;
115                 tm3[1]=bpath[i].y3;
116                 tm1 *= trans;
117                 tm2 *= trans;
118                 tm3 *= trans;
119                 if (optimize_stroke) {
120                     swept = NR::Rect(last, last);
121                     swept.expandTo(tm1);
122                     swept.expandTo(tm2);
123                     swept.expandTo(tm3);
124                 }
125                 last = tm3;
126                 tm1 -= shift;
127                 tm2 -= shift;
128                 tm3 -= shift;
129                 if (!optimize_stroke || swept.intersects(view))
130                     cairo_curve_to (ct, tm1[NR::X], tm1[NR::Y], tm2[NR::X], tm2[NR::Y], tm3[NR::X], tm3[NR::Y]);
131                 else
132                     cairo_move_to(ct, tm3[NR::X], tm3[NR::Y]);
133                 break;
134             }
136             default:
137                 break;
138         }
139     }
143 static void
144 feed_curve_to_cairo(cairo_t *cr, Geom::Curve const &c, Geom::Rect view, bool optimize_stroke)
146     if(Geom::LineSegment const* line_segment = dynamic_cast<Geom::LineSegment const*>(&c)) {
147         if (!optimize_stroke) {
148             cairo_line_to(cr, (*line_segment)[1][0], (*line_segment)[1][1]);
149         } else {
150             Geom::Rect swept((*line_segment)[0], (*line_segment)[1]);
151             if (swept.intersects(view)) {
152                 cairo_line_to(cr, (*line_segment)[1][0], (*line_segment)[1][1]);
153             } else {
154                 cairo_move_to(cr, (*line_segment)[1][0], (*line_segment)[1][1]);
155             }
156         }
157     }
158     else if(Geom::QuadraticBezier const *quadratic_bezier = dynamic_cast<Geom::QuadraticBezier const*>(&c)) {
159         std::vector<Geom::Point> points = quadratic_bezier->points();
160         Geom::Point b1 = points[0] + (2./3) * (points[1] - points[0]);
161         Geom::Point b2 = b1 + (1./3) * (points[2] - points[0]);
162         if (!optimize_stroke) {
163             cairo_curve_to(cr, b1[0], b1[1], b2[0], b2[1], points[2][0], points[2][1]);
164         } else {
165             Geom::Rect swept(points[0], points[2]);
166             swept.expandTo(points[1]);
167             if (swept.intersects(view)) {
168                 cairo_curve_to(cr, b1[0], b1[1], b2[0], b2[1], points[2][0], points[2][1]);
169             } else {
170                 cairo_move_to(cr, points[2][0], points[2][1]);
171             }
172         }
173     }
174     else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const*>(&c)) {
175         std::vector<Geom::Point> points = cubic_bezier->points();
176         if (!optimize_stroke) {
177             cairo_curve_to(cr, points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
178         } else {
179             Geom::Rect swept(points[0], points[3]);
180             swept.expandTo(points[1]);
181             swept.expandTo(points[2]);
182             if (swept.intersects(view)) {
183                 cairo_curve_to(cr, points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
184             } else {
185                 cairo_move_to(cr, points[3][0], points[3][1]);
186             }
187         }
188     }
189 //    else if(Geom::EllipticalArc const *svg_elliptical_arc = dynamic_cast<Geom::EllipticalArc *>(c)) {
190 //        //TODO: get at the innards and spit them out to cairo
191 //    }
192     else {
193         //this case handles sbasis as well as all other curve types
194         Geom::Path sbasis_path = path_from_sbasis(c.toSBasis(), 0.1);
196         //recurse to convert the new path resulting from the sbasis to svgd
197         for(Geom::Path::iterator iter = sbasis_path.begin(); iter != sbasis_path.end(); ++iter) {
198             feed_curve_to_cairo(cr, *iter, view, optimize_stroke);
199         }
200     }
204 /** Feeds path-creating calls to the cairo context translating them from the SPCurve, with the given transform and shift */
205 void
206 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)
208     if (!area || area->isEmpty())
209         return;
210     if (path.empty())
211         return;
213     // Transform all coordinates to coords within "area"
214     Geom::Point shift = to_2geom(area->min());
215     NR::Rect view = *area;
216     view.growBy (stroke_width);
217     view = view * from_2geom(Geom::Translate(-shift));
218     Geom::Path const path_trans = path * (trans * Geom::Translate(-shift));
220     cairo_move_to(ct, path_trans.initialPoint()[0], path_trans.initialPoint()[1] );
222     for(Geom::Path::const_iterator cit = path_trans.begin(); cit != path_trans.end_open(); ++cit) {
223         feed_curve_to_cairo(ct, *cit, to_2geom(view), optimize_stroke);
224     }
226     if (path.closed()) {
227         cairo_line_to(ct, path_trans.initialPoint()[0], path_trans.initialPoint()[1] );
228     }
231 /** Feeds path-creating calls to the cairo context translating them from the SPCurve, with the given transform and shift */
232 void
233 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)
235     if (!area || area->isEmpty())
236         return;
237     if (pathv.empty())
238         return;
240     for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
241         feed_path_to_cairo(ct, *it, trans, area, optimize_stroke, stroke_width);
242     }
245 /*
246   Local Variables:
247   mode:c++
248   c-file-style:"stroustrup"
249   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
250   indent-tabs-mode:nil
251   fill-column:99
252   End:
253 */
254 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :