Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
Geom::SVGPathWriter Class Reference

Serialize paths to SVG path data strings. More...

#include <svg-path-writer.h>

Inheritance diagram for Geom::SVGPathWriter:
Geom::PathSink

Public Member Functions

 SVGPathWriter ()
 
 ~SVGPathWriter () override
 
void moveTo (Point const &p) override
 Move to a different point without creating a segment.
 
void lineTo (Point const &p) override
 Output a line segment.
 
void quadTo (Point const &c, Point const &p) override
 Output a cubic Bezier segment.
 
void curveTo (Point const &c0, Point const &c1, Point const &p) override
 Output a quadratic Bezier segment.
 
void arcTo (double rx, double ry, double angle, bool large_arc, bool sweep, Point const &p) override
 Output an elliptical arc segment.
 
void closePath () override
 Close the current path with a line segment.
 
void flush () override
 Flush any internal state of the generator.
 
void clear ()
 Clear any path data written so far.
 
void setPrecision (int prec)
 Set output precision.
 
void setOptimize (bool opt)
 Enable or disable length optimization.
 
void setUseShorthands (bool use)
 Enable or disable the use of V, H, T and S commands where possible.
 
std::string str () const
 Retrieve the generated path data string.
 
- Public Member Functions inherited from Geom::PathSink
virtual bool backspace ()
 Undo the last segment.
 
virtual void feed (Curve const &c, bool moveto_initial=true)
 
virtual void feed (Path const &p)
 Output a subpath.
 
virtual void feed (PathVector const &v)
 Output a path.
 
virtual void feed (Rect const &)
 Output an axis-aligned rectangle, using moveTo, lineTo and closePath.
 
virtual void feed (Circle const &e)
 Output a circle as two elliptical arcs.
 
virtual void feed (Ellipse const &e)
 Output an ellipse as two elliptical arcs.
 
virtual ~PathSink ()
 

Private Member Functions

void _setCommand (char cmd)
 
std::string _formatCoord (Coord par)
 

Private Attributes

std::ostringstream _s
 
std::ostringstream _ns
 
std::vector< Coord_current_pars
 
Point _subpath_start
 
Point _current
 
Point _quad_tangent
 
Point _cubic_tangent
 
Coord _epsilon
 
int _precision
 
bool _optimize
 
bool _use_shorthands
 
char _command
 

Detailed Description

Serialize paths to SVG path data strings.

You can access the generated string by calling the str() method.

Definition at line 45 of file svg-path-writer.h.

Constructor & Destructor Documentation

◆ SVGPathWriter()

Geom::SVGPathWriter::SVGPathWriter ( )

Definition at line 45 of file svg-path-writer.cpp.

References _ns.

◆ ~SVGPathWriter()

Geom::SVGPathWriter::~SVGPathWriter ( )
inlineoverride

Definition at line 50 of file svg-path-writer.h.

Member Function Documentation

◆ _formatCoord()

std::string Geom::SVGPathWriter::_formatCoord ( Coord  par)
private

Definition at line 259 of file svg-path-writer.cpp.

References _ns, _precision, and Geom::format_coord_shortest().

Referenced by flush().

◆ _setCommand()

void Geom::SVGPathWriter::_setCommand ( char  cmd)
private

Definition at line 251 of file svg-path-writer.cpp.

References _command, and flush().

Referenced by arcTo(), curveTo(), lineTo(), moveTo(), and quadTo().

◆ arcTo()

void Geom::SVGPathWriter::arcTo ( double  rx,
double  ry,
double  angle,
bool  large_arc,
bool  sweep,
Point const &  p 
)
overridevirtual

Output an elliptical arc segment.

See the EllipticalArc class for the documentation of parameters.

Implements Geom::PathSink.

Definition at line 147 of file svg-path-writer.cpp.

References _cubic_tangent, _current, _current_pars, _optimize, _quad_tangent, _setCommand(), flush(), Geom::X, and Geom::Y.

◆ clear()

void Geom::SVGPathWriter::clear ( )

Clear any path data written so far.

Definition at line 228 of file svg-path-writer.cpp.

References _command, _current, _current_pars, _ns, _s, and _subpath_start.

Referenced by TEST_F().

◆ closePath()

void Geom::SVGPathWriter::closePath ( )
overridevirtual

Close the current path with a line segment.

Implements Geom::PathSink.

Definition at line 165 of file svg-path-writer.cpp.

References _cubic_tangent, _current, _optimize, _quad_tangent, _s, _subpath_start, and flush().

◆ curveTo()

void Geom::SVGPathWriter::curveTo ( Point const &  c0,
Point const &  c1,
Point const &  p 
)
overridevirtual

◆ flush()

void Geom::SVGPathWriter::flush ( )
overridevirtual

Flush any internal state of the generator.

This call should implicitly finish the current subpath. Calling this method should be idempotent, because the default implementations of path() and pathvector() will call it multiple times in a row.

Implements Geom::PathSink.

Definition at line 176 of file svg-path-writer.cpp.

References _command, _current_pars, _formatCoord(), _optimize, _s, and Geom::is_digit().

Referenced by _setCommand(), arcTo(), closePath(), curveTo(), lineTo(), moveTo(), and quadTo().

◆ lineTo()

void Geom::SVGPathWriter::lineTo ( Point const &  p)
overridevirtual

◆ moveTo()

void Geom::SVGPathWriter::moveTo ( Point const &  p)
overridevirtual

Move to a different point without creating a segment.

Usually starts a new subpath.

Implements Geom::PathSink.

Definition at line 57 of file svg-path-writer.cpp.

References _cubic_tangent, _current, _current_pars, _optimize, _quad_tangent, _setCommand(), _subpath_start, flush(), Geom::X, and Geom::Y.

◆ quadTo()

void Geom::SVGPathWriter::quadTo ( Point const &  c,
Point const &  p 
)
overridevirtual

◆ setOptimize()

void Geom::SVGPathWriter::setOptimize ( bool  opt)
inline

Enable or disable length optimization.

When set to true, the path writer will optimize the generated path data for minimum length. However, this will make the data less readable, because spaces between commands and coordinates will be omitted where unnecessary for correct parsing.

When set to false, the string will be a straightforward, partially redundant representation of the passed commands, optimized for readability. Commands and coordinates will always be separated by spaces and the command symbol will not be omitted for multiple consecutive commands of the same type.

Length optimization is turned off by default.

Definition at line 82 of file svg-path-writer.h.

References _optimize.

Referenced by TEST_F(), and Geom::write_svg_path().

◆ setPrecision()

void Geom::SVGPathWriter::setPrecision ( int  prec)

Set output precision.

When the parameter is negative, the path writer enters a verbatim mode which preserves all values exactly.

Definition at line 240 of file svg-path-writer.cpp.

References _epsilon, _ns, and _precision.

Referenced by Geom::write_svg_path().

◆ setUseShorthands()

void Geom::SVGPathWriter::setUseShorthands ( bool  use)
inline

Enable or disable the use of V, H, T and S commands where possible.

Shorthands are turned on by default.

Definition at line 86 of file svg-path-writer.h.

References _use_shorthands.

Referenced by TEST_F(), and Geom::write_svg_path().

◆ str()

std::string Geom::SVGPathWriter::str ( ) const
inline

Retrieve the generated path data string.

Definition at line 89 of file svg-path-writer.h.

References _s.

Referenced by Geom::operator<<(), Geom::operator<<(), TEST_F(), and Geom::write_svg_path().

Member Data Documentation

◆ _command

char Geom::SVGPathWriter::_command
private

Definition at line 105 of file svg-path-writer.h.

Referenced by _setCommand(), clear(), flush(), and lineTo().

◆ _cubic_tangent

Point Geom::SVGPathWriter::_cubic_tangent
private

Definition at line 100 of file svg-path-writer.h.

Referenced by arcTo(), closePath(), curveTo(), lineTo(), moveTo(), and quadTo().

◆ _current

Point Geom::SVGPathWriter::_current
private

Definition at line 98 of file svg-path-writer.h.

Referenced by arcTo(), clear(), closePath(), curveTo(), lineTo(), moveTo(), and quadTo().

◆ _current_pars

std::vector<Coord> Geom::SVGPathWriter::_current_pars
private

Definition at line 96 of file svg-path-writer.h.

Referenced by arcTo(), clear(), curveTo(), flush(), lineTo(), moveTo(), and quadTo().

◆ _epsilon

Coord Geom::SVGPathWriter::_epsilon
private

Definition at line 101 of file svg-path-writer.h.

Referenced by curveTo(), lineTo(), quadTo(), and setPrecision().

◆ _ns

std::ostringstream Geom::SVGPathWriter::_ns
private

Definition at line 95 of file svg-path-writer.h.

Referenced by _formatCoord(), clear(), setPrecision(), and SVGPathWriter().

◆ _optimize

bool Geom::SVGPathWriter::_optimize
private

Definition at line 103 of file svg-path-writer.h.

Referenced by arcTo(), closePath(), curveTo(), flush(), lineTo(), moveTo(), quadTo(), and setOptimize().

◆ _precision

int Geom::SVGPathWriter::_precision
private

Definition at line 102 of file svg-path-writer.h.

Referenced by _formatCoord(), and setPrecision().

◆ _quad_tangent

Point Geom::SVGPathWriter::_quad_tangent
private

Definition at line 99 of file svg-path-writer.h.

Referenced by arcTo(), closePath(), curveTo(), lineTo(), moveTo(), and quadTo().

◆ _s

std::ostringstream Geom::SVGPathWriter::_s
private

Definition at line 95 of file svg-path-writer.h.

Referenced by clear(), closePath(), flush(), and str().

◆ _subpath_start

Point Geom::SVGPathWriter::_subpath_start
private

Definition at line 97 of file svg-path-writer.h.

Referenced by clear(), closePath(), and moveTo().

◆ _use_shorthands

bool Geom::SVGPathWriter::_use_shorthands
private

Definition at line 104 of file svg-path-writer.h.

Referenced by curveTo(), lineTo(), quadTo(), and setUseShorthands().


The documentation for this class was generated from the following files: