svg-path-writer.cpp revision 76addc201c409e81eaaa73fe27cc0f79c4db097c
/** @file
* @brief Path sink which writes an SVG-compatible command string
*//*
* Authors:
* Krzysztof KosiĆski <tweenk.pl@gmail.com>
*
* Copyright 2014 Authors
*
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*/
#include <cmath>
#include <iomanip>
#include <glib.h>
namespace Geom {
static inline bool is_digit(char c) {
return c >= '0' && c <= '9';
}
: _epsilon(0)
, _precision(-1)
, _optimize(false)
, _use_shorthands(true)
, _command(0)
{
// always use C locale for number formatting
}
{
_setCommand('M');
_current_pars.push_back(p[X]);
_current_pars.push_back(p[Y]);
if (!_optimize) {
flush();
}
}
{
// The weird setting of _current is to avoid drift with many almost-aligned segments
// The additional conditions ensure that the smaller dimension is rounded to zero
bool written = false;
if (_use_shorthands) {
// emit vlineto
_setCommand('V');
_current_pars.push_back(p[Y]);
_current[Y] = p[Y];
written = true;
// emit hlineto
_setCommand('H');
_current_pars.push_back(p[X]);
_current[X] = p[X];
written = true;
}
}
if (!written) {
// emit normal lineto
_setCommand('L');
}
_current_pars.push_back(p[X]);
_current_pars.push_back(p[Y]);
_current = p;
}
if (!_optimize) {
flush();
}
}
{
if (!shorthand) {
_current_pars.push_back(c[X]);
_current_pars.push_back(c[Y]);
}
_current_pars.push_back(p[X]);
_current_pars.push_back(p[Y]);
_current = _cubic_tangent = p;
_quad_tangent = p + (p - c);
if (!_optimize) {
flush();
}
}
{
if (!shorthand) {
}
if (!_optimize) {
flush();
}
}
{
_setCommand('A');
_current_pars.push_back(p[X]);
_current_pars.push_back(p[Y]);
if (!_optimize) {
flush();
}
}
void SVGPathWriter::closePath()
{
flush();
if (_optimize) {
_s << "z";
} else {
_s << " z";
}
}
void SVGPathWriter::flush()
{
if (_optimize) {
} else {
_s << ' ';
}
}
bool contained_dot = false;
for (unsigned i = 0; i < _current_pars.size(); ++i) {
// TODO: optimize the use of absolute / relative coords
// Separator handling logic.
// Floating point values can end with a digit or dot
// and start with a digit, a plus or minus sign, or a dot.
// The following cases require a separator:
// * digit-digit
// * digit-dot (only if the previous number didn't contain a dot)
// * dot-digit
if (_optimize) {
// C++11: change to front()
_s << " ";
_s << " ";
}
_s << " ";
}
// C++11: change to back()
} else {
}
}
_command = 0;
}
void SVGPathWriter::clear()
{
_command = 0;
_subpath_start = Point(0,0);
}
{
_precision = prec;
if (prec < 0) {
_epsilon = 0;
} else {
}
}
{
flush();
}
}
{
if (_precision < 0) {
} else {
}
return ret;
}
{
}
} // namespace Geom
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :