Pangolin
Header-only C++20 plane computational geometry library
Loading...
Searching...
No Matches
pgl::Canvas Class Reference

Stores drawable objects and exports them as an SVG image. More...

#include <canvas.hpp>

Public Member Functions

 Canvas ()=default
 Creates an empty canvas with default style and viewport settings.
Canvasscale (double factor)
 Sets the global zoom factor used during SVG export.
Canvaswidth (double widthPixels)
 Sets the exported SVG width in pixels.
Canvasheight (double heightPixels)
 Sets the exported SVG height in pixels.
Canvassize (double widthPixels, double heightPixels)
 Sets the exported SVG size in pixels.
Canvasborders (bool enabled=true)
 Enables or disables the optional border around the SVG.
template<class PointType>
Canvasview (const Rectangle< PointType > &window)
 Fits the export to an explicit window of the plane instead of to the inserted geometry.
Canvasmargin (double marginPixels)
 Sets the margin reserved around the fitted drawing.
void writeSVG (const std::string &path) const
 Serializes the canvas to an SVG file.
std::string toSVG () const
 Serializes the canvas contents to an SVG string.
void writePDF (const std::string &path) const
 Serializes the canvas to a PDF file.
std::string toPDF () const
 Serializes the canvas contents to a PDF byte string.
void writeIPE (const std::string &path) const
 Serializes the canvas to an Ipe XML file (.ipe).
std::string toIPE () const
 Serializes the canvas contents to an Ipe XML string.
Canvasoperator<< (const CanvasCommand &command)
 Applies a style command to the current drawing style.
template<class Number, class Label>
Canvasoperator<< (const Point< Number, Label > &point)
 Appends a point using the current captured style.
template<class PointType, class Label>
Canvasoperator<< (const Segment< PointType, Label > &segment)
 Appends a segment using the current captured style.
template<class PointType, class Label>
Canvasoperator<< (const OrientedSegment< PointType, Label > &segment)
 Appends an oriented segment using the current captured style.
template<class PointType, class Label>
Canvasoperator<< (const Line< PointType, Label > &line)
 Appends a line using the current captured style.
template<class PointType, class Label>
Canvasoperator<< (const OrientedLine< PointType, Label > &line)
 Appends an oriented line using the current captured style.
template<class PointType, class Label>
Canvasoperator<< (const Ray< PointType, Label > &ray)
 Appends a ray using the current captured style.
template<class PointType, class Label>
Canvasoperator<< (const Halfplane< PointType, Label > &halfplane)
 Appends a half-plane using the current captured style.
template<class PointType, class Label>
Canvasoperator<< (const Rectangle< PointType, Label > &rectangle)
 Appends a rectangle using the current captured style.
template<class PointType, class Label>
Canvasoperator<< (const Triangle< PointType, Label > &triangle)
 Appends a triangle using the current captured style.
template<class PointType, class Label>
Canvasoperator<< (const Convex< PointType, Label > &convex)
 Appends a convex polygon using the current captured style.
template<class PointType, class Label>
Canvasoperator<< (const Disk< PointType, Label > &disk)
 Appends a disk using the current captured style.
template<class PointType, class Label>
Canvasoperator<< (const Polygon< PointType, Label > &polygon)
 Appends a (possibly non-convex) polygon using the current captured style.
template<class PointType, class Label>
Canvasoperator<< (const PolygonWithHoles< PointType, Label > &region)
 Appends a polygon with holes (one subpath per ring) using the current captured style.
template<class PointType, class Label>
Canvasoperator<< (const PolygonSet< PointType, Label > &set)
 Appends a polygon set (one subpath per ring of every component) using the current captured style.
template<class PointType, class Label, class Storage>
Canvasoperator<< (const MonotoneChain< PointType, Label, Storage > &chain)
 Appends an x-monotone chain (an SVG polyline) using the current captured style.
template<class PointType, class Label>
Canvasoperator<< (const Polyline< PointType, Label > &polyline)
 Appends a polyline (an open, possibly self-intersecting chain) using the current captured style.
template<class PointType, class Label>
Canvasoperator<< (const HalfplaneIntersection< PointType, Label > &region)
 Appends a half-plane intersection (clipped to the viewport, like a half-plane) using the current captured style.
template<class PointType>
Canvasoperator<< (const EmptyShape< PointType > &)
 Appends nothing: the empty shape has no geometry to draw.
template<class PointType>
Canvasoperator<< (const Shape< PointType > &shape)
 Appends the currently stored alternative of a runtime shape.
template<class... Types>
requires (requires(Canvas& canvas, const Types& value) { canvas << value; } && ...)
Canvasoperator<< (const std::variant< Types... > &objects)
 Appends the currently stored alternative of a variant.
template<class Type>
requires requires(Canvas& canvas, const Type& value) { canvas << value; }
Canvasoperator<< (const std::optional< Type > &object)
 Appends an optional object when it contains one.
template<std::ranges::input_range Range>
requires std::ranges::input_range<const Range> && requires( Canvas& canvas, std::ranges::range_reference_t<const Range> object
) { canvas << object; }
Canvasoperator<< (const Range &objects)
 Appends every object in an input range, in iteration order.

Detailed Description

Stores drawable objects and exports them as an SVG image.

The canvas keeps a current SVG style. Each inserted primitive captures the style that was active at insertion time.

Constructor & Destructor Documentation

◆ Canvas()

pgl::Canvas::Canvas ( )
default

Creates an empty canvas with default style and viewport settings.

Member Function Documentation

◆ borders()

Canvas & pgl::Canvas::borders ( bool enabled = true)
inline

Enables or disables the optional border around the SVG.

Parameters
enabledWhether to draw the border.
Returns
This canvas.

◆ height()

Canvas & pgl::Canvas::height ( double heightPixels)
inline

Sets the exported SVG height in pixels.

Parameters
heightPixelsPositive height in pixels.
Returns
This canvas.

◆ margin()

Canvas & pgl::Canvas::margin ( double marginPixels)
inline

Sets the margin reserved around the fitted drawing.

Parameters
marginPixelsNon-negative margin in pixels.
Returns
This canvas.

◆ operator<<() [1/23]

Canvas & pgl::Canvas::operator<< ( const CanvasCommand & command)
inline

Applies a style command to the current drawing style.

Existing elements keep their captured style.

Parameters
commandStyle command to apply.
Returns
This canvas.

◆ operator<<() [2/23]

template<class PointType, class Label>
Canvas & pgl::Canvas::operator<< ( const Convex< PointType, Label > & convex)
inline

Appends a convex polygon using the current captured style.

◆ operator<<() [3/23]

template<class PointType, class Label>
Canvas & pgl::Canvas::operator<< ( const Disk< PointType, Label > & disk)
inline

Appends a disk using the current captured style.

◆ operator<<() [4/23]

template<class PointType>
Canvas & pgl::Canvas::operator<< ( const EmptyShape< PointType > & )
inline

Appends nothing: the empty shape has no geometry to draw.

◆ operator<<() [5/23]

template<class PointType, class Label>
Canvas & pgl::Canvas::operator<< ( const Halfplane< PointType, Label > & halfplane)
inline

Appends a half-plane using the current captured style.

◆ operator<<() [6/23]

template<class PointType, class Label>
Canvas & pgl::Canvas::operator<< ( const HalfplaneIntersection< PointType, Label > & region)
inline

Appends a half-plane intersection (clipped to the viewport, like a half-plane) using the current captured style.

◆ operator<<() [7/23]

template<class PointType, class Label>
Canvas & pgl::Canvas::operator<< ( const Line< PointType, Label > & line)
inline

Appends a line using the current captured style.

◆ operator<<() [8/23]

template<class PointType, class Label, class Storage>
Canvas & pgl::Canvas::operator<< ( const MonotoneChain< PointType, Label, Storage > & chain)
inline

Appends an x-monotone chain (an SVG polyline) using the current captured style.

◆ operator<<() [9/23]

template<class PointType, class Label>
Canvas & pgl::Canvas::operator<< ( const OrientedLine< PointType, Label > & line)
inline

Appends an oriented line using the current captured style.

◆ operator<<() [10/23]

template<class PointType, class Label>
Canvas & pgl::Canvas::operator<< ( const OrientedSegment< PointType, Label > & segment)
inline

Appends an oriented segment using the current captured style.

◆ operator<<() [11/23]

template<class Number, class Label>
Canvas & pgl::Canvas::operator<< ( const Point< Number, Label > & point)
inline

Appends a point using the current captured style.

◆ operator<<() [12/23]

template<class PointType, class Label>
Canvas & pgl::Canvas::operator<< ( const Polygon< PointType, Label > & polygon)
inline

Appends a (possibly non-convex) polygon using the current captured style.

◆ operator<<() [13/23]

template<class PointType, class Label>
Canvas & pgl::Canvas::operator<< ( const PolygonSet< PointType, Label > & set)
inline

Appends a polygon set (one subpath per ring of every component) using the current captured style.

The whole set is a single element: it carries one title and one style, and a set that comes apart into several pieces stays one drawn object.

◆ operator<<() [14/23]

template<class PointType, class Label>
Canvas & pgl::Canvas::operator<< ( const PolygonWithHoles< PointType, Label > & region)
inline

Appends a polygon with holes (one subpath per ring) using the current captured style.

◆ operator<<() [15/23]

template<class PointType, class Label>
Canvas & pgl::Canvas::operator<< ( const Polyline< PointType, Label > & polyline)
inline

Appends a polyline (an open, possibly self-intersecting chain) using the current captured style.

◆ operator<<() [16/23]

template<std::ranges::input_range Range>
requires std::ranges::input_range<const Range> && requires( Canvas& canvas, std::ranges::range_reference_t<const Range> object
) { canvas << object; }
Canvas & pgl::Canvas::operator<< ( const Range & objects)
inline

Appends every object in an input range, in iteration order.

◆ operator<<() [17/23]

template<class PointType, class Label>
Canvas & pgl::Canvas::operator<< ( const Ray< PointType, Label > & ray)
inline

Appends a ray using the current captured style.

◆ operator<<() [18/23]

template<class PointType, class Label>
Canvas & pgl::Canvas::operator<< ( const Rectangle< PointType, Label > & rectangle)
inline

Appends a rectangle using the current captured style.

◆ operator<<() [19/23]

template<class PointType, class Label>
Canvas & pgl::Canvas::operator<< ( const Segment< PointType, Label > & segment)
inline

Appends a segment using the current captured style.

◆ operator<<() [20/23]

template<class PointType>
Canvas & pgl::Canvas::operator<< ( const Shape< PointType > & shape)
inline

Appends the currently stored alternative of a runtime shape.

◆ operator<<() [21/23]

template<class Type>
requires requires(Canvas& canvas, const Type& value) { canvas << value; }
Canvas & pgl::Canvas::operator<< ( const std::optional< Type > & object)
inline

Appends an optional object when it contains one.

◆ operator<<() [22/23]

template<class... Types>
requires (requires(Canvas& canvas, const Types& value) { canvas << value; } && ...)
Canvas & pgl::Canvas::operator<< ( const std::variant< Types... > & objects)
inline

Appends the currently stored alternative of a variant.

◆ operator<<() [23/23]

template<class PointType, class Label>
Canvas & pgl::Canvas::operator<< ( const Triangle< PointType, Label > & triangle)
inline

Appends a triangle using the current captured style.

◆ scale()

Canvas & pgl::Canvas::scale ( double factor)
inline

Sets the global zoom factor used during SVG export.

Parameters
factorPositive zoom multiplier.
Returns
This canvas.

◆ size()

Canvas & pgl::Canvas::size ( double widthPixels,
double heightPixels )
inline

Sets the exported SVG size in pixels.

Parameters
widthPixelsPositive width in pixels.
heightPixelsPositive height in pixels.
Returns
This canvas.

◆ toIPE()

std::string pgl::Canvas::toIPE ( ) const
inline

Serializes the canvas contents to an Ipe XML string.

Returns
Complete Ipe (.ipe) XML document.

◆ toPDF()

std::string pgl::Canvas::toPDF ( ) const
inline

Serializes the canvas contents to a PDF byte string.

Returns
Complete PDF document bytes.

◆ toSVG()

std::string pgl::Canvas::toSVG ( ) const
inline

Serializes the canvas contents to an SVG string.

Returns
Complete SVG document.

◆ view()

template<class PointType>
Canvas & pgl::Canvas::view ( const Rectangle< PointType > & window)
inline

Fits the export to an explicit window of the plane instead of to the inserted geometry.

The window is what the image shows: infinite primitives are clipped to it and whatever geometry falls outside it falls outside the image. It is the way to frame a drawing whose interesting part is not its bounding box — an arrangement whose far-away crossings would shrink everything else, or a drawing whose bounding box is stretched by the points defining a line. Calling it again replaces the window, and scale and margin still apply on top of it.

Template Parameters
PointTypePoint type of the window rectangle.
Parameters
windowRectangle of the plane to show.
Returns
This canvas.

◆ width()

Canvas & pgl::Canvas::width ( double widthPixels)
inline

Sets the exported SVG width in pixels.

Parameters
widthPixelsPositive width in pixels.
Returns
This canvas.

◆ writeIPE()

void pgl::Canvas::writeIPE ( const std::string & path) const
inline

Serializes the canvas to an Ipe XML file (.ipe).

The exported document defines a single page whose layout matches the canvas dimensions, reusing the same fitted viewport as SVG/PDF export. Colors and pen widths are stored as absolute (non-symbolic) Ipe attribute values. Opacities below 1 are declared as named <opacity> entries in the style sheet, since Ipe requires the opacity and stroke-opacity path attributes to reference a symbolic name.

Parameters
pathOutput file path.

◆ writePDF()

void pgl::Canvas::writePDF ( const std::string & path) const
inline

Serializes the canvas to a PDF file.

PDF export reuses the same fitted viewport as SVG export so the page matches the canvas dimensions and overall layout. Stroke and fill opacity are exported through standard PDF ExtGState resources (/ca and /CA).

Parameters
pathOutput file path.

◆ writeSVG()

void pgl::Canvas::writeSVG ( const std::string & path) const
inline

Serializes the canvas to an SVG file.

Parameters
pathOutput file path.