| 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 | #ifndef LIB2GEOM_SEEN_SVG_PATH_WRITER_H | ||
| 34 | #define LIB2GEOM_SEEN_SVG_PATH_WRITER_H | ||
| 35 | |||
| 36 | #include <2geom/path-sink.h> | ||
| 37 | #include <sstream> | ||
| 38 | |||
| 39 | namespace Geom { | ||
| 40 | |||
| 41 | /** @brief Serialize paths to SVG path data strings. | ||
| 42 | * You can access the generated string by calling the str() method. | ||
| 43 | * @ingroup Paths | ||
| 44 | */ | ||
| 45 | class SVGPathWriter | ||
| 46 | : public PathSink | ||
| 47 | { | ||
| 48 | public: | ||
| 49 | SVGPathWriter(); | ||
| 50 | ✗ | ~SVGPathWriter() override {} | |
| 51 | |||
| 52 | void moveTo(Point const &p) override; | ||
| 53 | void lineTo(Point const &p) override; | ||
| 54 | void quadTo(Point const &c, Point const &p) override; | ||
| 55 | void curveTo(Point const &c0, Point const &c1, Point const &p) override; | ||
| 56 | void arcTo(double rx, double ry, double angle, | ||
| 57 | bool large_arc, bool sweep, Point const &p) override; | ||
| 58 | void closePath() override; | ||
| 59 | void flush() override; | ||
| 60 | |||
| 61 | /// Clear any path data written so far. | ||
| 62 | void clear(); | ||
| 63 | |||
| 64 | /** @brief Set output precision. | ||
| 65 | * When the parameter is negative, the path writer enters a verbatim mode | ||
| 66 | * which preserves all values exactly. */ | ||
| 67 | void setPrecision(int prec); | ||
| 68 | |||
| 69 | /** @brief Enable or disable length optimization. | ||
| 70 | * | ||
| 71 | * When set to true, the path writer will optimize the generated path data | ||
| 72 | * for minimum length. However, this will make the data less readable, | ||
| 73 | * because spaces between commands and coordinates will be omitted where | ||
| 74 | * unnecessary for correct parsing. | ||
| 75 | * | ||
| 76 | * When set to false, the string will be a straightforward, partially redundant | ||
| 77 | * representation of the passed commands, optimized for readability. | ||
| 78 | * Commands and coordinates will always be separated by spaces and the command | ||
| 79 | * symbol will not be omitted for multiple consecutive commands of the same type. | ||
| 80 | * | ||
| 81 | * Length optimization is turned off by default. */ | ||
| 82 | ✗ | void setOptimize(bool opt) { _optimize = opt; } | |
| 83 | |||
| 84 | /** @brief Enable or disable the use of V, H, T and S commands where possible. | ||
| 85 | * Shorthands are turned on by default. */ | ||
| 86 | ✗ | void setUseShorthands(bool use) { _use_shorthands = use; } | |
| 87 | |||
| 88 | /// Retrieve the generated path data string. | ||
| 89 | ✗ | std::string str() const { return _s.str(); } | |
| 90 | |||
| 91 | private: | ||
| 92 | void _setCommand(char cmd); | ||
| 93 | std::string _formatCoord(Coord par); | ||
| 94 | |||
| 95 | std::ostringstream _s, _ns; | ||
| 96 | std::vector<Coord> _current_pars; | ||
| 97 | Point _subpath_start; | ||
| 98 | Point _current; | ||
| 99 | Point _quad_tangent; | ||
| 100 | Point _cubic_tangent; | ||
| 101 | Coord _epsilon; | ||
| 102 | int _precision; | ||
| 103 | bool _optimize; | ||
| 104 | bool _use_shorthands; | ||
| 105 | char _command; | ||
| 106 | }; | ||
| 107 | |||
| 108 | std::string write_svg_path(PathVector const &pv, int prec = -1, bool optimize = false, bool shorthands = true); | ||
| 109 | |||
| 110 | } // namespace Geom | ||
| 111 | |||
| 112 | #endif // LIB2GEOM_SEEN_SVG_PATH_WRITER_H | ||
| 113 | /* | ||
| 114 | Local Variables: | ||
| 115 | mode:c++ | ||
| 116 | c-file-style:"stroustrup" | ||
| 117 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) | ||
| 118 | indent-tabs-mode:nil | ||
| 119 | fill-column:99 | ||
| 120 | End: | ||
| 121 | */ | ||
| 122 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : | ||
| 123 |