Code

Super duper mega (fun!) commit: replaced encoding=utf-8 with fileencoding=utf-8 in...
[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
17 #include <libnr/nr-pixblock.h>
18 #include <libnr/nr-convert2geom.h>
19 #include "../style.h"
20 #include "nr-arena.h"
21 #include "sp-canvas.h"
22 #include <2geom/pathvector.h>
23 #include <2geom/bezier-curve.h>
24 #include <2geom/hvlinesegment.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>
30 #include "helper/geom-curves.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_ptr(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 /*
72  * Can be called recursively.
73  * If optimize_stroke == false, the view Rect is not used.
74  */
75 static void
76 feed_curve_to_cairo(cairo_t *cr, Geom::Curve const &c, Geom::Matrix const & trans, Geom::Rect view, bool optimize_stroke)
77 {
78     if( is_straight_curve(c) )
79     {
80         Geom::Point end_tr = c.finalPoint() * trans;
81         if (!optimize_stroke) {
82             cairo_line_to(cr, end_tr[0], end_tr[1]);
83         } else {
84             Geom::Rect swept(c.initialPoint()*trans, end_tr);
85             if (swept.intersects(view)) {
86                 cairo_line_to(cr, end_tr[0], end_tr[1]);
87             } else {
88                 cairo_move_to(cr, end_tr[0], end_tr[1]);
89             }
90         }
91     }
92     else if(Geom::QuadraticBezier const *quadratic_bezier = dynamic_cast<Geom::QuadraticBezier const*>(&c)) {
93         std::vector<Geom::Point> points = quadratic_bezier->points();
94         points[0] *= trans;
95         points[1] *= trans;
96         points[2] *= trans;
97         Geom::Point b1 = points[0] + (2./3) * (points[1] - points[0]);
98         Geom::Point b2 = b1 + (1./3) * (points[2] - points[0]);
99         if (!optimize_stroke) {
100             cairo_curve_to(cr, b1[0], b1[1], b2[0], b2[1], points[2][0], points[2][1]);
101         } else {
102             Geom::Rect swept(points[0], points[2]);
103             swept.expandTo(points[1]);
104             if (swept.intersects(view)) {
105                 cairo_curve_to(cr, b1[0], b1[1], b2[0], b2[1], points[2][0], points[2][1]);
106             } else {
107                 cairo_move_to(cr, points[2][0], points[2][1]);
108             }
109         }
110     }
111     else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const*>(&c)) {
112         std::vector<Geom::Point> points = cubic_bezier->points();
113         //points[0] *= trans; // don't do this one here for fun: it is only needed for optimized strokes
114         points[1] *= trans;
115         points[2] *= trans;
116         points[3] *= trans;
117         if (!optimize_stroke) {
118             cairo_curve_to(cr, points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
119         } else {
120             points[0] *= trans;  // didn't transform this point yet
121             Geom::Rect swept(points[0], points[3]);
122             swept.expandTo(points[1]);
123             swept.expandTo(points[2]);
124             if (swept.intersects(view)) {
125                 cairo_curve_to(cr, points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
126             } else {
127                 cairo_move_to(cr, points[3][0], points[3][1]);
128             }
129         }
130     }
131 //    else if(Geom::SVGEllipticalArc const *svg_elliptical_arc = dynamic_cast<Geom::SVGEllipticalArc *>(c)) {
132 //        //TODO: get at the innards and spit them out to cairo
133 //    }
134     else {
135         //this case handles sbasis as well as all other curve types
136         Geom::Path sbasis_path = Geom::cubicbezierpath_from_sbasis(c.toSBasis(), 0.1);
138         //recurse to convert the new path resulting from the sbasis to svgd
139         for(Geom::Path::iterator iter = sbasis_path.begin(); iter != sbasis_path.end(); ++iter) {
140             feed_curve_to_cairo(cr, *iter, trans, view, optimize_stroke);
141         }
142     }
146 /** Feeds path-creating calls to the cairo context translating them from the Path */
147 static void
148 feed_path_to_cairo (cairo_t *ct, Geom::Path const &path)
150     if (path.empty())
151         return;
153     cairo_move_to(ct, path.initialPoint()[0], path.initialPoint()[1] );
155     for(Geom::Path::const_iterator cit = path.begin(); cit != path.end_open(); ++cit) {
156         feed_curve_to_cairo(ct, *cit, Geom::identity(), Geom::Rect(), false); // optimize_stroke is false, so the view rect is not used
157     }
159     if (path.closed()) {
160         cairo_close_path(ct);
161     }
164 /** Feeds path-creating calls to the cairo context translating them from the Path, with the given transform and shift */
165 static void
166 feed_path_to_cairo (cairo_t *ct, Geom::Path const &path, Geom::Matrix trans, Geom::OptRect area, bool optimize_stroke, double stroke_width)
168     if (!area)
169         return;
170     if (path.empty())
171         return;
173     // Transform all coordinates to coords within "area"
174     Geom::Point shift = area->min();
175     Geom::Rect view = *area;
176     view.expandBy (stroke_width);
177     view = view * (Geom::Matrix)Geom::Translate(-shift);
178     //  Pass transformation to feed_curve, so that we don't need to create a whole new path.
179     Geom::Matrix transshift(trans * Geom::Translate(-shift));
181     Geom::Point initial = path.initialPoint() * transshift;
182     cairo_move_to(ct, initial[0], initial[1] );
184     for(Geom::Path::const_iterator cit = path.begin(); cit != path.end_open(); ++cit) {
185         feed_curve_to_cairo(ct, *cit, transshift, view, optimize_stroke);
186     }
188     if (path.closed()) {
189         if (!optimize_stroke) {
190             cairo_close_path(ct);
191         } else {
192             cairo_line_to(ct, initial[0], initial[1]);
193             /* We cannot use cairo_close_path(ct) here because some parts of the path may have been
194                clipped and not drawn (maybe the before last segment was outside view area), which 
195                would result in closing the "subpath" after the last interruption, not the entire path.
197                However, according to cairo documentation:
198                The behavior of cairo_close_path() is distinct from simply calling cairo_line_to() with the equivalent coordinate
199                in the case of stroking. When a closed sub-path is stroked, there are no caps on the ends of the sub-path. Instead,
200                there is a line join connecting the final and initial segments of the sub-path. 
202                The correct fix will be possible when cairo introduces methods for moving without
203                ending/starting subpaths, which we will use for skipping invisible segments; then we
204                will be able to use cairo_close_path here. This issue also affects ps/eps/pdf export,
205                see bug 168129
206             */
207         }
208     }
211 /** Feeds path-creating calls to the cairo context translating them from the PathVector, with the given transform and shift
212  *  One must have done cairo_new_path(ct); before calling this function. */
213 void
214 feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv, Geom::Matrix trans, Geom::OptRect area, bool optimize_stroke, double stroke_width)
216     if (!area)
217         return;
218     if (pathv.empty())
219         return;
221     for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
222         feed_path_to_cairo(ct, *it, trans, area, optimize_stroke, stroke_width);
223     }
226 /** Feeds path-creating calls to the cairo context translating them from the PathVector
227  *  One must have done cairo_new_path(ct); before calling this function. */
228 void
229 feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv)
231     if (pathv.empty())
232         return;
234     for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
235         feed_path_to_cairo(ct, *it);
236     }
239 /*
240   Local Variables:
241   mode:c++
242   c-file-style:"stroustrup"
243   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
244   indent-tabs-mode:nil
245   fill-column:99
246   End:
247 */
248 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :