| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** @file | ||
| 2 | * @brief Path sink which writes an SVG-compatible command string | ||
| 3 | *//* | ||
| 4 | * Authors: | ||
| 5 | * Krzysztof KosiĆski <tweenk.pl@gmail.com> | ||
| 6 | * | ||
| 7 | * Copyright 2014 Authors | ||
| 8 | * | ||
| 9 | * This library is free software; you can redistribute it and/or | ||
| 10 | * modify it either under the terms of the GNU Lesser General Public | ||
| 11 | * License version 2.1 as published by the Free Software Foundation | ||
| 12 | * (the "LGPL") or, at your option, under the terms of the Mozilla | ||
| 13 | * Public License Version 1.1 (the "MPL"). If you do not alter this | ||
| 14 | * notice, a recipient may use your version of this file under either | ||
| 15 | * the MPL or the LGPL. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the LGPL along with this library | ||
| 18 | * in the file COPYING-LGPL-2.1; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 20 | * You should have received a copy of the MPL along with this library | ||
| 21 | * in the file COPYING-MPL-1.1 | ||
| 22 | * | ||
| 23 | * The contents of this file are subject to the Mozilla Public License | ||
| 24 | * Version 1.1 (the "License"); you may not use this file except in | ||
| 25 | * compliance with the License. You may obtain a copy of the License at | ||
| 26 | * http://www.mozilla.org/MPL/ | ||
| 27 | * | ||
| 28 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY | ||
| 29 | * OF ANY KIND, either express or implied. See the LGPL or the MPL for | ||
| 30 | * the specific language governing rights and limitations. | ||
| 31 | */ | ||
| 32 | |||
| 33 | #include <cmath> | ||
| 34 | #include <iomanip> | ||
| 35 | #include <2geom/coord.h> | ||
| 36 | #include <2geom/svg-path-writer.h> | ||
| 37 | #include <glib.h> | ||
| 38 | |||
| 39 | namespace Geom { | ||
| 40 | |||
| 41 | 529 | static inline bool is_digit(char c) { | |
| 42 |
4/4✓ Branch 0 taken 502 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 447 times.
✓ Branch 3 taken 55 times.
|
529 | return c >= '0' && c <= '9'; |
| 43 | } | ||
| 44 | |||
| 45 | 1 | SVGPathWriter::SVGPathWriter() | |
| 46 | 1 | : _epsilon(0) | |
| 47 | 1 | , _precision(-1) | |
| 48 | 1 | , _optimize(false) | |
| 49 | 1 | , _use_shorthands(true) | |
| 50 |
2/4✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
1 | , _command(0) |
| 51 | { | ||
| 52 | // always use C locale for number formatting | ||
| 53 |
2/4✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
1 | _ns.imbue(std::locale::classic()); |
| 54 | 1 | _ns.unsetf(std::ios::floatfield); | |
| 55 | 1 | } | |
| 56 | |||
| 57 | 28 | void SVGPathWriter::moveTo(Point const &p) | |
| 58 | { | ||
| 59 | 28 | _setCommand('M'); | |
| 60 |
1/2✓ Branch 3 taken 28 times.
✗ Branch 4 not taken.
|
28 | _current_pars.push_back(p[X]); |
| 61 |
1/2✓ Branch 3 taken 28 times.
✗ Branch 4 not taken.
|
28 | _current_pars.push_back(p[Y]); |
| 62 | |||
| 63 | 28 | _current = _subpath_start = _quad_tangent = _cubic_tangent = p; | |
| 64 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
|
28 | if (!_optimize) { |
| 65 | 14 | flush(); | |
| 66 | } | ||
| 67 | 28 | } | |
| 68 | |||
| 69 | 44 | void SVGPathWriter::lineTo(Point const &p) | |
| 70 | { | ||
| 71 | // The weird setting of _current is to avoid drift with many almost-aligned segments | ||
| 72 | // The additional conditions ensure that the smaller dimension is rounded to zero | ||
| 73 | 44 | bool written = false; | |
| 74 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 22 times.
|
44 | if (_use_shorthands) { |
| 75 | 22 | Point r = _current - p; | |
| 76 |
5/6✓ Branch 3 taken 4 times.
✓ Branch 4 taken 18 times.
✓ Branch 9 taken 4 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 4 times.
✓ Branch 12 taken 18 times.
|
22 | if (are_near(p[X], _current[X], _epsilon) && std::abs(r[X]) < std::abs(r[Y])) { |
| 77 | // emit vlineto | ||
| 78 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | _setCommand('V'); |
| 79 |
1/2✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
4 | _current_pars.push_back(p[Y]); |
| 80 | 4 | _current[Y] = p[Y]; | |
| 81 | 4 | written = true; | |
| 82 |
5/6✓ Branch 3 taken 12 times.
✓ Branch 4 taken 6 times.
✓ Branch 9 taken 12 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 12 times.
✓ Branch 12 taken 6 times.
|
18 | } else if (are_near(p[Y], _current[Y], _epsilon) && std::abs(r[Y]) < std::abs(r[X])) { |
| 83 | // emit hlineto | ||
| 84 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | _setCommand('H'); |
| 85 |
1/2✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
|
12 | _current_pars.push_back(p[X]); |
| 86 | 12 | _current[X] = p[X]; | |
| 87 | 12 | written = true; | |
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 16 times.
|
44 | if (!written) { |
| 92 | // emit normal lineto | ||
| 93 |
3/4✓ Branch 0 taken 22 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
|
28 | if (_command != 'M' && _command != 'L') { |
| 94 | 22 | _setCommand('L'); | |
| 95 | } | ||
| 96 |
1/2✓ Branch 3 taken 28 times.
✗ Branch 4 not taken.
|
28 | _current_pars.push_back(p[X]); |
| 97 |
1/2✓ Branch 3 taken 28 times.
✗ Branch 4 not taken.
|
28 | _current_pars.push_back(p[Y]); |
| 98 | 28 | _current = p; | |
| 99 | } | ||
| 100 | |||
| 101 | 44 | _cubic_tangent = _quad_tangent = _current; | |
| 102 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 22 times.
|
44 | if (!_optimize) { |
| 103 | 22 | flush(); | |
| 104 | } | ||
| 105 | 44 | } | |
| 106 | |||
| 107 | 4 | void SVGPathWriter::quadTo(Point const &c, Point const &p) | |
| 108 | { | ||
| 109 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
|
4 | bool shorthand = _use_shorthands && are_near(c, _quad_tangent, _epsilon); |
| 110 | |||
| 111 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | _setCommand(shorthand ? 'T' : 'Q'); |
| 112 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (!shorthand) { |
| 113 |
1/2✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
4 | _current_pars.push_back(c[X]); |
| 114 |
1/2✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
4 | _current_pars.push_back(c[Y]); |
| 115 | } | ||
| 116 |
1/2✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
4 | _current_pars.push_back(p[X]); |
| 117 |
1/2✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
4 | _current_pars.push_back(p[Y]); |
| 118 | |||
| 119 | 4 | _current = _cubic_tangent = p; | |
| 120 | 4 | _quad_tangent = p + (p - c); | |
| 121 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (!_optimize) { |
| 122 | 2 | flush(); | |
| 123 | } | ||
| 124 | 4 | } | |
| 125 | |||
| 126 | 64 | void SVGPathWriter::curveTo(Point const &p1, Point const &p2, Point const &p3) | |
| 127 | { | ||
| 128 |
4/4✓ Branch 0 taken 32 times.
✓ Branch 1 taken 32 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 18 times.
|
64 | bool shorthand = _use_shorthands && are_near(p1, _cubic_tangent, _epsilon); |
| 129 | |||
| 130 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 50 times.
|
64 | _setCommand(shorthand ? 'S' : 'C'); |
| 131 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 14 times.
|
64 | if (!shorthand) { |
| 132 |
1/2✓ Branch 3 taken 50 times.
✗ Branch 4 not taken.
|
50 | _current_pars.push_back(p1[X]); |
| 133 |
1/2✓ Branch 3 taken 50 times.
✗ Branch 4 not taken.
|
50 | _current_pars.push_back(p1[Y]); |
| 134 | } | ||
| 135 |
1/2✓ Branch 3 taken 64 times.
✗ Branch 4 not taken.
|
64 | _current_pars.push_back(p2[X]); |
| 136 |
1/2✓ Branch 3 taken 64 times.
✗ Branch 4 not taken.
|
64 | _current_pars.push_back(p2[Y]); |
| 137 |
1/2✓ Branch 3 taken 64 times.
✗ Branch 4 not taken.
|
64 | _current_pars.push_back(p3[X]); |
| 138 |
1/2✓ Branch 3 taken 64 times.
✗ Branch 4 not taken.
|
64 | _current_pars.push_back(p3[Y]); |
| 139 | |||
| 140 | 64 | _current = _quad_tangent = p3; | |
| 141 | 64 | _cubic_tangent = p3 + (p3 - p2); | |
| 142 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 32 times.
|
64 | if (!_optimize) { |
| 143 | 32 | flush(); | |
| 144 | } | ||
| 145 | 64 | } | |
| 146 | |||
| 147 | 12 | void SVGPathWriter::arcTo(double rx, double ry, double angle, | |
| 148 | bool large_arc, bool sweep, Point const &p) | ||
| 149 | { | ||
| 150 | 12 | _setCommand('A'); | |
| 151 | 12 | _current_pars.push_back(rx); | |
| 152 | 12 | _current_pars.push_back(ry); | |
| 153 |
1/2✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
|
12 | _current_pars.push_back(deg_from_rad(angle)); |
| 154 |
3/4✓ Branch 1 taken 8 times.
✓ Branch 2 taken 4 times.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
|
12 | _current_pars.push_back(large_arc ? 1. : 0.); |
| 155 |
2/4✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
|
12 | _current_pars.push_back(sweep ? 1. : 0.); |
| 156 |
1/2✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
|
12 | _current_pars.push_back(p[X]); |
| 157 |
1/2✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
|
12 | _current_pars.push_back(p[Y]); |
| 158 | |||
| 159 | 12 | _current = _quad_tangent = _cubic_tangent = p; | |
| 160 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (!_optimize) { |
| 161 | 6 | flush(); | |
| 162 | } | ||
| 163 | 12 | } | |
| 164 | |||
| 165 | 20 | void SVGPathWriter::closePath() | |
| 166 | { | ||
| 167 | 20 | flush(); | |
| 168 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if (_optimize) { |
| 169 | 10 | _s << "z"; | |
| 170 | } else { | ||
| 171 | 10 | _s << " z"; | |
| 172 | } | ||
| 173 | 20 | _current = _quad_tangent = _cubic_tangent = _subpath_start; | |
| 174 | 20 | } | |
| 175 | |||
| 176 | 193 | void SVGPathWriter::flush() | |
| 177 | { | ||
| 178 |
5/6✓ Branch 0 taken 131 times.
✓ Branch 1 taken 62 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 131 times.
✓ Branch 5 taken 62 times.
✓ Branch 6 taken 131 times.
|
193 | if (_command == 0 || _current_pars.empty()) return; |
| 179 | |||
| 180 |
2/2✓ Branch 0 taken 55 times.
✓ Branch 1 taken 76 times.
|
131 | if (_optimize) { |
| 181 | 55 | _s << _command; | |
| 182 | } else { | ||
| 183 |
3/4✓ Branch 2 taken 76 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 62 times.
✓ Branch 7 taken 14 times.
|
76 | if (_s.tellp() != 0) { |
| 184 | 62 | _s << ' '; | |
| 185 | } | ||
| 186 | 76 | _s << _command; | |
| 187 | } | ||
| 188 | |||
| 189 | 131 | char lastchar = _command; | |
| 190 | 131 | bool contained_dot = false; | |
| 191 | |||
| 192 |
2/2✓ Branch 7 taken 584 times.
✓ Branch 8 taken 131 times.
|
715 | for (double _current_par : _current_pars) { |
| 193 | // TODO: optimize the use of absolute / relative coords | ||
| 194 |
1/2✓ Branch 2 taken 584 times.
✗ Branch 3 not taken.
|
584 | std::string cs = _formatCoord(_current_par); |
| 195 | |||
| 196 | // Separator handling logic. | ||
| 197 | // Floating point values can end with a digit or dot | ||
| 198 | // and start with a digit, a plus or minus sign, or a dot. | ||
| 199 | // The following cases require a separator: | ||
| 200 | // * digit-digit | ||
| 201 | // * digit-dot (only if the previous number didn't contain a dot) | ||
| 202 | // * dot-digit | ||
| 203 |
2/2✓ Branch 0 taken 292 times.
✓ Branch 1 taken 292 times.
|
584 | if (_optimize) { |
| 204 | // C++11: change to front() | ||
| 205 |
1/2✓ Branch 1 taken 292 times.
✗ Branch 2 not taken.
|
292 | char firstchar = cs[0]; |
| 206 |
2/2✓ Branch 1 taken 237 times.
✓ Branch 2 taken 55 times.
|
292 | if (is_digit(lastchar)) { |
| 207 |
2/2✓ Branch 1 taken 210 times.
✓ Branch 2 taken 27 times.
|
237 | if (is_digit(firstchar)) { |
| 208 |
1/2✓ Branch 1 taken 210 times.
✗ Branch 2 not taken.
|
210 | _s << " "; |
| 209 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
27 | } else if (firstchar == '.' && !contained_dot) { |
| 210 | ✗ | _s << " "; | |
| 211 | } | ||
| 212 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 55 times.
|
55 | } else if (lastchar == '.' && is_digit(firstchar)) { |
| 213 | ✗ | _s << " "; | |
| 214 | } | ||
| 215 |
1/2✓ Branch 1 taken 292 times.
✗ Branch 2 not taken.
|
292 | _s << cs; |
| 216 | |||
| 217 | // C++11: change to back() | ||
| 218 |
1/2✓ Branch 2 taken 292 times.
✗ Branch 3 not taken.
|
292 | lastchar = cs[cs.length()-1]; |
| 219 | 292 | contained_dot = cs.find('.') != std::string::npos; | |
| 220 | } else { | ||
| 221 |
2/4✓ Branch 1 taken 292 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 292 times.
✗ Branch 5 not taken.
|
292 | _s << " " << cs; |
| 222 | } | ||
| 223 | 584 | } | |
| 224 | 131 | _current_pars.clear(); | |
| 225 | 131 | _command = 0; | |
| 226 | } | ||
| 227 | |||
| 228 | 28 | void SVGPathWriter::clear() | |
| 229 | { | ||
| 230 | 28 | _s.clear(); | |
| 231 |
2/4✓ Branch 3 taken 28 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 28 times.
✗ Branch 7 not taken.
|
84 | _s.str(""); |
| 232 | 28 | _ns.clear(); | |
| 233 |
2/4✓ Branch 3 taken 28 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 28 times.
✗ Branch 7 not taken.
|
84 | _ns.str(""); |
| 234 | 28 | _command = 0; | |
| 235 | 28 | _current_pars.clear(); | |
| 236 | 28 | _current = Point(0,0); | |
| 237 | 28 | _subpath_start = Point(0,0); | |
| 238 | 28 | } | |
| 239 | |||
| 240 | ✗ | void SVGPathWriter::setPrecision(int prec) | |
| 241 | { | ||
| 242 | ✗ | _precision = prec; | |
| 243 | ✗ | if (prec < 0) { | |
| 244 | ✗ | _epsilon = 0; | |
| 245 | } else { | ||
| 246 | ✗ | _epsilon = std::pow(10., -prec); | |
| 247 | ✗ | _ns << std::setprecision(_precision); | |
| 248 | } | ||
| 249 | ✗ | } | |
| 250 | |||
| 251 | 146 | void SVGPathWriter::_setCommand(char cmd) | |
| 252 | { | ||
| 253 |
4/4✓ Branch 0 taken 56 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 41 times.
✓ Branch 3 taken 15 times.
|
146 | if (_command != 0 && _command != cmd) { |
| 254 | 41 | flush(); | |
| 255 | } | ||
| 256 | 146 | _command = cmd; | |
| 257 | 146 | } | |
| 258 | |||
| 259 | 584 | std::string SVGPathWriter::_formatCoord(Coord par) | |
| 260 | { | ||
| 261 | 584 | std::string ret; | |
| 262 |
1/2✓ Branch 0 taken 584 times.
✗ Branch 1 not taken.
|
584 | if (_precision < 0) { |
| 263 |
1/2✓ Branch 2 taken 584 times.
✗ Branch 3 not taken.
|
584 | ret = format_coord_shortest(par); |
| 264 | } else { | ||
| 265 | ✗ | _ns << par; | |
| 266 | ✗ | ret = _ns.str(); | |
| 267 | ✗ | _ns.clear(); | |
| 268 | ✗ | _ns.str(""); | |
| 269 | } | ||
| 270 | 584 | return ret; | |
| 271 | ✗ | } | |
| 272 | |||
| 273 | |||
| 274 | ✗ | std::string write_svg_path(PathVector const &pv, int prec, bool optimize, bool shorthands) | |
| 275 | { | ||
| 276 | ✗ | SVGPathWriter writer; | |
| 277 | ✗ | writer.setPrecision(prec); | |
| 278 | ✗ | writer.setOptimize(optimize); | |
| 279 | ✗ | writer.setUseShorthands(shorthands); | |
| 280 | |||
| 281 | ✗ | writer.feed(pv); | |
| 282 | ✗ | return writer.str(); | |
| 283 | ✗ | } | |
| 284 | |||
| 285 | } // namespace Geom | ||
| 286 | |||
| 287 | /* | ||
| 288 | Local Variables: | ||
| 289 | mode:c++ | ||
| 290 | c-file-style:"stroustrup" | ||
| 291 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) | ||
| 292 | indent-tabs-mode:nil | ||
| 293 | fill-column:99 | ||
| 294 | End: | ||
| 295 | */ | ||
| 296 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : | ||
| 297 |