| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file | ||
| 3 | * @brief Path sink for Cairo contexts | ||
| 4 | *//* | ||
| 5 | * Copyright 2014 Krzysztof KosiĆski | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it either under the terms of the GNU Lesser General Public | ||
| 9 | * License version 2.1 as published by the Free Software Foundation | ||
| 10 | * (the "LGPL") or, at your option, under the terms of the Mozilla | ||
| 11 | * Public License Version 1.1 (the "MPL"). If you do not alter this | ||
| 12 | * notice, a recipient may use your version of this file under either | ||
| 13 | * the MPL or the LGPL. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the LGPL along with this library | ||
| 16 | * in the file COPYING-LGPL-2.1; if not, output to the Free Software | ||
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | * You should have received a copy of the MPL along with this library | ||
| 19 | * in the file COPYING-MPL-1.1 | ||
| 20 | * | ||
| 21 | * The contents of this file are subject to the Mozilla Public License | ||
| 22 | * Version 1.1 (the "License"); you may not use this file except in | ||
| 23 | * compliance with the License. You may obtain a copy of the License at | ||
| 24 | * http://www.mozilla.org/MPL/ | ||
| 25 | * | ||
| 26 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY | ||
| 27 | * OF ANY KIND, either express or implied. See the LGPL or the MPL for | ||
| 28 | * the specific language governing rights and limitations. | ||
| 29 | * | ||
| 30 | */ | ||
| 31 | |||
| 32 | #ifdef HAVE_CAIRO | ||
| 33 | |||
| 34 | #include <cairo.h> | ||
| 35 | #include <2geom/cairo-path-sink.h> | ||
| 36 | #include <2geom/elliptical-arc.h> | ||
| 37 | |||
| 38 | namespace Geom { | ||
| 39 | |||
| 40 | ✗ | CairoPathSink::CairoPathSink(cairo_t *cr) | |
| 41 | ✗ | : _cr(cr) | |
| 42 | ✗ | {} | |
| 43 | |||
| 44 | ✗ | void CairoPathSink::moveTo(Point const &p) | |
| 45 | { | ||
| 46 | ✗ | cairo_move_to(_cr, p[X], p[Y]); | |
| 47 | ✗ | _current_point = p; | |
| 48 | ✗ | } | |
| 49 | |||
| 50 | ✗ | void CairoPathSink::lineTo(Point const &p) | |
| 51 | { | ||
| 52 | ✗ | cairo_line_to(_cr, p[X], p[Y]); | |
| 53 | ✗ | _current_point = p; | |
| 54 | ✗ | } | |
| 55 | |||
| 56 | ✗ | void CairoPathSink::curveTo(Point const &p1, Point const &p2, Point const &p3) | |
| 57 | { | ||
| 58 | ✗ | cairo_curve_to(_cr, p1[X], p1[Y], p2[X], p2[Y], p3[X], p3[Y]); | |
| 59 | ✗ | _current_point = p3; | |
| 60 | ✗ | } | |
| 61 | |||
| 62 | ✗ | void CairoPathSink::quadTo(Point const &p1, Point const &p2) | |
| 63 | { | ||
| 64 | // degree-elevate to cubic Bezier, since Cairo doesn't do quad Beziers | ||
| 65 | // google "Bezier degree elevation" for more info | ||
| 66 | ✗ | Point q1 = (1./3.) * _current_point + (2./3.) * p1; | |
| 67 | ✗ | Point q2 = (2./3.) * p1 + (1./3.) * p2; | |
| 68 | // q3 = p2 | ||
| 69 | ✗ | cairo_curve_to(_cr, q1[X], q1[Y], q2[X], q2[Y], p2[X], p2[Y]); | |
| 70 | ✗ | _current_point = p2; | |
| 71 | ✗ | } | |
| 72 | |||
| 73 | ✗ | void CairoPathSink::arcTo(double rx, double ry, double angle, | |
| 74 | bool large_arc, bool sweep, Point const &p) | ||
| 75 | { | ||
| 76 | ✗ | EllipticalArc arc(_current_point, rx, ry, angle, large_arc, sweep, p); | |
| 77 | // Cairo only does circular arcs. | ||
| 78 | // To do elliptical arcs, we must use a temporary transform. | ||
| 79 | ✗ | Affine uct = arc.unitCircleTransform(); | |
| 80 | |||
| 81 | // TODO move Cairo-2Geom matrix conversion into a common location | ||
| 82 | ✗ | cairo_matrix_t cm; | |
| 83 | ✗ | cm.xx = uct[0]; | |
| 84 | ✗ | cm.xy = uct[2]; | |
| 85 | ✗ | cm.x0 = uct[4]; | |
| 86 | ✗ | cm.yx = uct[1]; | |
| 87 | ✗ | cm.yy = uct[3]; | |
| 88 | ✗ | cm.y0 = uct[5]; | |
| 89 | |||
| 90 | ✗ | cairo_save(_cr); | |
| 91 | ✗ | cairo_transform(_cr, &cm); | |
| 92 | ✗ | if (sweep) { | |
| 93 | ✗ | cairo_arc(_cr, 0, 0, 1, arc.initialAngle(), arc.finalAngle()); | |
| 94 | } else { | ||
| 95 | ✗ | cairo_arc_negative(_cr, 0, 0, 1, arc.initialAngle(), arc.finalAngle()); | |
| 96 | } | ||
| 97 | ✗ | _current_point = p; | |
| 98 | ✗ | cairo_restore(_cr); | |
| 99 | |||
| 100 | /* Note that an extra linear segment will be inserted before the arc | ||
| 101 | * if Cairo considers the current point distinct from the initial point | ||
| 102 | * of the arc; we could partially alleviate this by not emitting | ||
| 103 | * linear segments that are followed by arc segments, but this would require | ||
| 104 | * buffering the input curves. */ | ||
| 105 | ✗ | } | |
| 106 | |||
| 107 | ✗ | void CairoPathSink::closePath() | |
| 108 | { | ||
| 109 | ✗ | cairo_close_path(_cr); | |
| 110 | ✗ | } | |
| 111 | |||
| 112 | ✗ | void CairoPathSink::flush() {} | |
| 113 | |||
| 114 | } // namespace Geom | ||
| 115 | |||
| 116 | #endif | ||
| 117 | |||
| 118 | /* | ||
| 119 | Local Variables: | ||
| 120 | mode:c++ | ||
| 121 | c-file-style:"stroustrup" | ||
| 122 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) | ||
| 123 | indent-tabs-mode:nil | ||
| 124 | fill-column:99 | ||
| 125 | End: | ||
| 126 | */ | ||
| 127 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : | ||
| 128 |