Code

remove many unnecessary to_2geom and from_2geom calls
[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_line_to(ct, path.initialPoint()[0], path.initialPoint()[1]);
161 //        cairo_close_path(ct);
162         /* I think we should use cairo_close_path(ct) here but it doesn't work. (the closing line is not rendered completely)
163            According to cairo documentation:
164            The behavior of cairo_close_path() is distinct from simply calling cairo_line_to() with the equivalent coordinate
165            in the case of stroking. When a closed sub-path is stroked, there are no caps on the ends of the sub-path. Instead,
166            there is a line join connecting the final and initial segments of the sub-path. 
167         */
168     }
171 /** Feeds path-creating calls to the cairo context translating them from the Path, with the given transform and shift */
172 static void
173 feed_path_to_cairo (cairo_t *ct, Geom::Path const &path, Geom::Matrix trans, boost::optional<NR::Rect> area, bool optimize_stroke, double stroke_width)
175     if (!area || area->isEmpty())
176         return;
177     if (path.empty())
178         return;
180     // Transform all coordinates to coords within "area"
181     Geom::Point shift = area->min();
182     NR::Rect view = *area;
183     view.growBy (stroke_width);
184     view = view * (NR::Matrix)Geom::Translate(-shift);
185     //  Pass transformation to feed_curve, so that we don't need to create a whole new path.
186     Geom::Matrix transshift(trans * Geom::Translate(-shift));
188     Geom::Point initial = path.initialPoint() * transshift;
189     cairo_move_to(ct, initial[0], initial[1] );
191     for(Geom::Path::const_iterator cit = path.begin(); cit != path.end_open(); ++cit) {
192         feed_curve_to_cairo(ct, *cit, transshift, to_2geom(view), optimize_stroke);
193     }
195     if (path.closed()) {
196         cairo_line_to(ct, initial[0], initial[1]);
197         /* It seems we cannot use cairo_close_path(ct) here; maybe because some parts of the path have been clipped and not drawn (maybe the before last segment was outside view area)
198            However, according to cairo documentation:
199            The behavior of cairo_close_path() is distinct from simply calling cairo_line_to() with the equivalent coordinate
200            in the case of stroking. When a closed sub-path is stroked, there are no caps on the ends of the sub-path. Instead,
201            there is a line join connecting the final and initial segments of the sub-path. 
202         */
203     }
206 /** Feeds path-creating calls to the cairo context translating them from the PathVector, with the given transform and shift
207  *  One must have done cairo_new_path(ct); before calling this function. */
208 void
209 feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv, Geom::Matrix trans, boost::optional<NR::Rect> area, bool optimize_stroke, double stroke_width)
211     if (!area || area->isEmpty())
212         return;
213     if (pathv.empty())
214         return;
216     for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
217         feed_path_to_cairo(ct, *it, trans, area, optimize_stroke, stroke_width);
218     }
221 /** Feeds path-creating calls to the cairo context translating them from the PathVector
222  *  One must have done cairo_new_path(ct); before calling this function. */
223 void
224 feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv)
226     if (pathv.empty())
227         return;
229     for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
230         feed_path_to_cairo(ct, *it);
231     }
234 /*
235   Local Variables:
236   mode:c++
237   c-file-style:"stroustrup"
238   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
239   indent-tabs-mode:nil
240   fill-column:99
241   End:
242 */
243 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :