NovaSVG 0.11.5
An SVG library
Loading...
Searching...
No Matches
nova::svg::document Class Reference

#include <svg.h>

Public Types

enum class  pdf_mode { vector , raster }
 How render_to_pdf() writes the page. More...
enum class  pdf_text { outlines , embed , embed_ignore_license }
 How render_to_pdf() writes text in the vector mode (the raster mode has no text). More...

Public Member Functions

void apply_style_sheet (const std::string &content)
 Applies a CSS stylesheet to the dom.
element_list query_selector_all (const std::string &content) const
 Selects all elements that match the given CSS selector(s).
const std::string & title () const
 Returns the text of the root <svg>'s first <title> child (entities decoded, white space trimmed), or an empty string when there is none. render_to_pdf() writes it as the PDF's /Title.
float width () const
 Returns the intrinsic width of the dom in pixels.
float height () const
 Returns the intrinsic height of the dom in pixels.
box bounding_box () const
 Returns the smallest rectangle that encloses the dom content.
void update_layout ()
 Updates the layout of the dom if needed.
void force_layout ()
 Forces an immediate layout update.
void render (stb::image &image, const stb::matrix &matrix=stb::matrix()) const
 Renders the dom onto an existing image, using a transformation matrix.
stb::image render_to_image (int width=-1, int height=-1, const stb::color &background_color=stb::color(0, 0, 0, 0)) const
 Renders the dom to a new straight-alpha RGBA image with specified dimensions.
void render (pixmap &bitmap, const stb::matrix &matrix=stb::matrix()) const
pixmap render_to_pixmap (int width=-1, int height=-1, const stb::color &background_color=stb::color(0, 0, 0, 0)) const
std::vector< uint8_t > render_to_pdf (pdf_mode mode=pdf_mode::vector, float dpi=192.f, pdf_text text=pdf_text::outlines) const
 Renders the document as a one-page PDF (page size = the document's size, 1 px = 0.75 pt).
bool save_pdf (const std::string &filename, pdf_mode mode=pdf_mode::vector, float dpi=192.f, pdf_text text=pdf_text::outlines) const
 render_to_pdf() written to a file.
element element_from_point (float x, float y) const
 Returns the topmost el under the specified point.
element element_by_id (const std::string &id) const
 Retrieves an el by its ID.
element document_element () const
 Retrieves the dom el.
 document (document &&)
document & operator= (document &&)
 ~document ()
void set_foreign_object_renderer (std::shared_ptr< foreign_object_renderer > renderer)
 Sets the renderer that draws this document's <foreignObject> elements (see foreign_object.h).
std::shared_ptr< foreign_object_renderer > foreign_object_renderer_ptr () const
 The renderer in use (null: foreignObject elements are skipped).

Static Public Member Functions

static std::unique_ptr< document > load_file (const std::string &filename)
 Load an svg dom from a file.
static std::unique_ptr< document > load_data (const std::string &string)
 Load an svg dom from a string.
static std::unique_ptr< document > load_data (const char *data)
 Load an svg dom from a null-terminated string.
static std::unique_ptr< document > load_data (const char *data, size_t length_)
 Load an svg dom from a string with a specified length.

Friends

class svg_uri_reference
class svg_node

Detailed Description

Definition at line 299 of file svg.h.

Member Enumeration Documentation

◆ pdf_mode

enum class nova::svg::document::pdf_mode
strong

How render_to_pdf() writes the page.

  • vector: shapes, text (as outlines), gradients, clips, opacity groups and blend modes stay vector; what PDF cannot express (masks, clip masks, filters) and pattern tiles are rendered to bitmaps at dpi and placed in the same page, so the page is never worse than the raster mode and usually much smaller and sharper.
  • raster: the whole page is one bitmap rendered at dpi. Every feature of the renderer works, nothing is vector.
Enumerator
vector 
raster 

Definition at line 418 of file svg.h.

◆ pdf_text

enum class nova::svg::document::pdf_text
strong

How render_to_pdf() writes text in the vector mode (the raster mode has no text).

  • outlines: the letters are shapes. The page looks the same everywhere and no font is needed to view it; an invisible text layer on top lets a viewer select, search and copy the text. The default.
  • embed: real text, shown with a subset of the font that is embedded in the file (only the glyphs the page uses). A font that may not be embedded (its OS/2 fsType says restricted, bitmaps only, or no subsetting) or cannot be subset (CFF outlines, a variable font at a chosen weight) keeps its text as outlines, and a warning is logged once per font.
  • embed_ignore_license: embed, but the license bits of the font are not read. Whoever asks for this answers for the license; a font that cannot be subset still stays outlines.
Enumerator
outlines 
embed 
embed_ignore_license 

Definition at line 432 of file svg.h.

Constructor & Destructor Documentation

◆ document()

nova::svg::document::document ( document && )

References document().

Referenced by document(), and operator=().

◆ ~document()

nova::svg::document::~document ( )

Member Function Documentation

◆ apply_style_sheet()

void nova::svg::document::apply_style_sheet ( const std::string & content)

Applies a CSS stylesheet to the dom.

Parameters
contentA string containing the CSS rules to apply, with comments removed.

◆ bounding_box()

box nova::svg::document::bounding_box ( ) const

Returns the smallest rectangle that encloses the dom content.

Returns
A Box representing the bounding box of the dom.

◆ document_element()

element nova::svg::document::document_element ( ) const

Retrieves the dom el.

Returns
The root element of the dom.

◆ element_by_id()

element nova::svg::document::element_by_id ( const std::string & id) const

Retrieves an el by its ID.

Parameters
idThe ID of the el to retrieve.
Returns
The element with the specified ID, or a null element if not found.

◆ element_from_point()

element nova::svg::document::element_from_point ( float x,
float y ) const

Returns the topmost el under the specified point.

Parameters
xThe x-coordinate in viewport space.
yThe y-coordinate in viewport space.
Returns
The topmost element at the given point, or a null element if no match is found.

◆ force_layout()

void nova::svg::document::force_layout ( )

Forces an immediate layout update.

◆ foreign_object_renderer_ptr()

std::shared_ptr< foreign_object_renderer > nova::svg::document::foreign_object_renderer_ptr ( ) const
inline

The renderer in use (null: foreignObject elements are skipped).

Definition at line 482 of file svg.h.

◆ height()

float nova::svg::document::height ( ) const

Returns the intrinsic height of the dom in pixels.

Returns
The height of the dom.

Referenced by render_to_image(), and render_to_pixmap().

◆ load_data() [1/3]

std::unique_ptr< document > nova::svg::document::load_data ( const char * data)
static

Load an svg dom from a null-terminated string.

Parameters
dataThe string containing the svg data.
Returns
A pointer to the loaded document, or nullptr on failure.

◆ load_data() [2/3]

std::unique_ptr< document > nova::svg::document::load_data ( const char * data,
size_t length_ )
static

Load an svg dom from a string with a specified length.

Parameters
dataThe string containing the svg data.
lengthThe length of the string in bytes.
Returns
A pointer to the loaded document, or nullptr on failure.

◆ load_data() [3/3]

std::unique_ptr< document > nova::svg::document::load_data ( const std::string & string)
static

Load an svg dom from a string.

Parameters
stringThe svg data as a string.
Returns
A pointer to the loaded document, or nullptr on failure.

◆ load_file()

std::unique_ptr< document > nova::svg::document::load_file ( const std::string & filename)
static

Load an svg dom from a file.

Parameters
filenameThe path to the svg file.
Returns
A pointer to the loaded document, or nullptr on failure.

◆ operator=()

document & nova::svg::document::operator= ( document && )

References document().

◆ query_selector_all()

element_list nova::svg::document::query_selector_all ( const std::string & content) const

Selects all elements that match the given CSS selector(s).

Parameters
contentA string containing the CSS selector(s) to match elements.
Returns
A list of elements matching the selector(s).

◆ render() [1/2]

void nova::svg::document::render ( pixmap & bitmap,
const stb::matrix & matrix = stb::matrix() ) const

◆ render() [2/2]

void nova::svg::document::render ( stb::image & image,
const stb::matrix & matrix = stb::matrix() ) const

Renders the dom onto an existing image, using a transformation matrix.

The dom is drawn on a transparent layer the size of image and the layer is composited over the image's pixels with stb::image::blend (normal mode, "over" with straight alpha). Because the layer starts transparent, mix-blend-mode inside the dom blends against the layer, not against what was already in image. A root background-color is composited over the image too, instead of replacing it. Any pixel format is accepted; images larger than 32767 pixels on a side are left untouched.

Parameters
imageThe image to render onto (changed in place).
matrixThe root transformation matrix.

◆ render_to_image()

stb::image nova::svg::document::render_to_image ( int width = -1,
int height = -1,
const stb::color & background_color = stb::color(0, 0, 0, 0) ) const

Renders the dom to a new straight-alpha RGBA image with specified dimensions.

Parameters
widthThe desired width in pixels, or -1 to auto-scale based on the intrinsic size.
heightThe desired height in pixels, or -1 to auto-scale based on the intrinsic size.
backgroundColorThe background color to render onto.
Returns
The rendered image, or an empty image if the dom has nothing to draw or memory ran out.

References height(), and width().

◆ render_to_pdf()

std::vector< uint8_t > nova::svg::document::render_to_pdf ( pdf_mode mode = pdf_mode::vector,
float dpi = 192.f,
pdf_text text = pdf_text::outlines ) const

Renders the document as a one-page PDF (page size = the document's size, 1 px = 0.75 pt).

Parameters
modeSee pdf_mode.
dpiResolution of every bitmap in the file (96 is the SVG's own pixel size).
textHow text is written, see pdf_text.
Returns
The PDF file's bytes, or empty when the document has no size.

References outlines, and vector.

◆ render_to_pixmap()

pixmap nova::svg::document::render_to_pixmap ( int width = -1,
int height = -1,
const stb::color & background_color = stb::color(0, 0, 0, 0) ) const

References height(), and width().

◆ save_pdf()

bool nova::svg::document::save_pdf ( const std::string & filename,
pdf_mode mode = pdf_mode::vector,
float dpi = 192.f,
pdf_text text = pdf_text::outlines ) const

render_to_pdf() written to a file.

Returns
false if nothing could be rendered or the file could not be written.

References outlines, and vector.

◆ set_foreign_object_renderer()

void nova::svg::document::set_foreign_object_renderer ( std::shared_ptr< foreign_object_renderer > renderer)

Sets the renderer that draws this document's <foreignObject> elements (see foreign_object.h).

The default is the built-in simple HTML renderer. A null renderer skips every foreignObject.

◆ title()

const std::string & nova::svg::document::title ( ) const

Returns the text of the root <svg>'s first <title> child (entities decoded, white space trimmed), or an empty string when there is none. render_to_pdf() writes it as the PDF's /Title.

◆ update_layout()

void nova::svg::document::update_layout ( )

Updates the layout of the dom if needed.

◆ width()

float nova::svg::document::width ( ) const

Returns the intrinsic width of the dom in pixels.

Returns
The width of the dom.

Referenced by render_to_image(), and render_to_pixmap().

◆ svg_node

friend class svg_node
friend

Definition at line 494 of file svg.h.

References svg_node.

Referenced by svg_node.

◆ svg_uri_reference

friend class svg_uri_reference
friend

Definition at line 493 of file svg.h.

References svg_uri_reference.

Referenced by svg_uri_reference.


The documentation for this class was generated from the following file:
  • /home/runner/work/novasvg/novasvg/include/nova/svg/svg.h