NovaSVG 0.11.5
An SVG library
Loading...
Searching...
No Matches
svg.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2023-present Mohammad Raziei
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23*/
24
25
26#ifndef NOVASVG_SVG_H
27#define NOVASVG_SVG_H
28#pragma once
29
30#include <cstdint>
31#include <memory>
32#include <string>
33#include <cctype>
34#include <vector>
35
36#include "../config.h"
37
38#define NOVA_VERSION_MAJOR 0
39#define NOVA_VERSION_MINOR 11
40#define NOVA_VERSION_PATCH 5
41
42#define NOVA_VERSION_ENCODE(major, minor, patch) (((major) * 10000) + ((minor) * 100) + ((patch) * 1))
43#define NOVA_VERSION NOVA_VERSION_ENCODE(NOVA_VERSION_MAJOR, NOVA_VERSION_MINOR, NOVA_VERSION_PATCH)
44
45#define NOVA_VERSION_XSTRINGIZE(major, minor, patch) #major"."#minor"."#patch
46#define NOVA_VERSION_STRINGIZE(major, minor, patch) NOVA_VERSION_XSTRINGIZE(major, minor, patch)
47#define NOVA_VERSION_STRING NOVA_VERSION_STRINGIZE(NOVA_VERSION_MAJOR, NOVA_VERSION_MINOR, NOVA_VERSION_PATCH)
48
49#include <stb/color.hpp>
50#include <stb/image.hpp>
51#include <stb/outline.hpp>
52
53#include "../vg/geometry.h"
54#include "foreign_object.h"
55
56namespace nova::vg {
57class pixmap; // @internal: the premultiplied-ARGB32 render target; the public pixel type is stb::image
58}
59
60namespace nova::svg {
61using namespace vg;
62
71int version();
72
81std::string version_string();
82
83} // namespace nova::svg
84
85namespace nova::svg {
86using namespace vg;
87
88class svg_node;
89class svg_text_node;
90class svg_element;
91
92class element;
93class text_node;
94
96public:
100 node() = default;
101
106 bool is_text_node() const;
107
112 bool is_element() const;
113
119
125
131
136 bool is_null() const { return m_node == nullptr; }
137
142 operator bool() const { return !is_null(); }
143
149 bool operator==(const node& nd) const { return m_node == nd.m_node; }
150
156 bool operator!=(const node& nd) const { return m_node != nd.m_node; }
157
158protected:
159 node(svg_node* nd);
160 svg_node* nd() const { return m_node; }
161 svg_node* m_node{nullptr};
162 friend class element;
163};
164
165using node_list = std::vector<node>;
166
167class NOVA_API text_node : public node {
168public:
172 text_node() = default;
173
178 const std::string& data() const;
179
184 void set_data(const std::string& data);
185
186private:
187 text_node(svg_text_node* text);
188 svg_text_node* text() const;
189 friend class node;
190};
191
192class NOVA_API element : public node {
193public:
197 element() = default;
198
204 bool has_attribute(const std::string& name) const;
205
211 const std::string& attribute(const std::string& name) const;
212
218 void set_attribute(const std::string& name, const std::string& value);
219
230 void render(stb::image& image, const stb::matrix& matrix = stb::matrix()) const;
231
239 stb::image render_to_image(int width = -1, int height = -1, const stb::color& background_color = stb::color(0, 0, 0, 0)) const;
240
245 void render(pixmap& bitmap, const stb::matrix& matrix = stb::matrix()) const;
246
250 pixmap render_to_pixmap(int width = -1, int height = -1, const stb::color& background_color = stb::color(0, 0, 0, 0)) const;
251
256 stb::matrix local_matrix() const;
257
262 stb::matrix global_matrix() const;
263
269
275
281
287
288private:
289 element(svg_element* el);
290 svg_element* el(bool layout_if_needed = false) const;
291 friend class node;
292 friend class document;
293};
294
295using element_list = std::vector<element>;
296
297class svg_root_element;
298
300public:
306 static std::unique_ptr<document> load_file(const std::string& filename);
307
313 static std::unique_ptr<document> load_data(const std::string& string);
314
320 static std::unique_ptr<document> load_data(const char* data);
321
328 static std::unique_ptr<document> load_data(const char* data, size_t length_);
329
334 void apply_style_sheet(const std::string& content);
335
341 element_list query_selector_all(const std::string& content) const;
342
347 const std::string& title() const;
348
353 float width() const;
354
359 float height() const;
360
366
371
376
388 void render(stb::image& image, const stb::matrix& matrix = stb::matrix()) const;
389
397 stb::image render_to_image(int width = -1, int height = -1, const stb::color& background_color = stb::color(0, 0, 0, 0)) const;
398
403 void render(pixmap& bitmap, const stb::matrix& matrix = stb::matrix()) const;
404
408 pixmap render_to_pixmap(int width = -1, int height = -1, const stb::color& background_color = stb::color(0, 0, 0, 0)) const;
409
418 enum class pdf_mode { vector, raster };
419
432 enum class pdf_text { outlines, embed, embed_ignore_license };
433
441 std::vector<uint8_t> render_to_pdf(pdf_mode mode = pdf_mode::vector, float dpi = 192.f, pdf_text text = pdf_text::outlines) const;
442
447 bool save_pdf(const std::string& filename, pdf_mode mode = pdf_mode::vector, float dpi = 192.f, pdf_text text = pdf_text::outlines) const;
448
455 element element_from_point(float x, float y) const;
456
462 element element_by_id(const std::string& id) const;
463
469
473
479 void set_foreign_object_renderer(std::shared_ptr<foreign_object_renderer> renderer);
480
482 std::shared_ptr<foreign_object_renderer> foreign_object_renderer_ptr() const { return m_foreign_object_renderer; }
483
484private:
485 document();
486 document(const document&) = delete;
487 document& operator=(const document&) = delete;
488 svg_root_element* root_element(bool layout_if_needed = false) const;
489 bool parse(const char* data, size_t length_);
490 std::unique_ptr<svg_root_element> m_root_element;
491 std::string m_title; // the root's first <title> child: the parser skips the element, this keeps its text
492 std::shared_ptr<foreign_object_renderer> m_foreign_object_renderer = default_foreign_object_renderer();
493 friend class svg_uri_reference;
494 friend class svg_node;
495};
496
503 float width = 0.f;
504 float ascent = 0.f;
505 float descent = 0.f;
506 float line_gap = 0.f;
507};
508
528NOVA_API bool is_valid_selector(const std::string& selector);
529
530NOVA_API text_metrics measure_text(const std::string& utf8_text, const std::string& font_family, float font_size,
531 bool bold = false, bool italic = false);
532
539NOVA_API text_metrics measure_text(const std::string& utf8_text, const std::string& font_family, float font_size,
540 int weight, bool italic = false);
541
542} // namespace nova::svg
543
544#endif // NOVASVG_SVG_H
void apply_style_sheet(const std::string &content)
Applies a CSS stylesheet to the dom.
box bounding_box() const
Returns the smallest rectangle that encloses the dom content.
void render(stb::image &image, const stb::matrix &matrix=stb::matrix()) const
Renders the dom onto an existing image, using a transformation matrix.
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.
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....
friend class svg_node
Definition svg.h:494
void render(pixmap &bitmap, const stb::matrix &matrix=stb::matrix()) const
document & operator=(document &&)
std::shared_ptr< foreign_object_renderer > foreign_object_renderer_ptr() const
The renderer in use (null: foreignObject elements are skipped).
Definition svg.h:482
document(document &&)
pixmap render_to_pixmap(int width=-1, int height=-1, const stb::color &background_color=stb::color(0, 0, 0, 0)) const
element element_by_id(const std::string &id) const
Retrieves an el by its ID.
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, size_t length_)
Load an svg dom from a string with a specified length.
static std::unique_ptr< document > load_data(const char *data)
Load an svg dom from a null-terminated string.
element element_from_point(float x, float y) const
Returns the topmost el under the specified point.
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....
float height() const
Returns the intrinsic height of the dom in pixels.
pdf_text
How render_to_pdf() writes text in the vector mode (the raster mode has no text).
Definition svg.h:432
const std::string & title() const
Returns the text of the root <svg>'s first <title> child (entities decoded, white space trimmed),...
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 update_layout()
Updates the layout of the dom if needed.
element_list query_selector_all(const std::string &content) const
Selects all elements that match the given CSS selector(s).
float width() const
Returns the intrinsic width of the dom in pixels.
friend class svg_uri_reference
Definition svg.h:493
pdf_mode
How render_to_pdf() writes the page.
Definition svg.h:418
static std::unique_ptr< document > load_file(const std::string &filename)
Load an svg dom from a file.
void force_layout()
Forces an immediate layout update.
element document_element() const
Retrieves the dom el.
pixmap render_to_pixmap(int width=-1, int height=-1, const stb::color &background_color=stb::color(0, 0, 0, 0)) const
void set_attribute(const std::string &name, const std::string &value)
Sets the value of an attribute.
stb::matrix global_matrix() const
Retrieves the global transformation matrix of the el.
friend class node
Definition svg.h:291
stb::matrix local_matrix() const
Retrieves the local transformation matrix of the el.
box bounding_box() const
Retrieves the bounding box of the el without any transformations.
const std::string & attribute(const std::string &name) const
Retrieves the value of an attribute.
element()=default
Constructs a null el.
friend class document
Definition svg.h:292
node_list children() const
Returns the child nodes of this nd.
void render(stb::image &image, const stb::matrix &matrix=stb::matrix()) const
Renders the el onto an existing image, using a transformation matrix.
void render(pixmap &bitmap, const stb::matrix &matrix=stb::matrix()) const
box local_bounding_box() const
Retrieves the local bounding box of the el.
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 el to a new straight-alpha RGBA image with specified dimensions.
bool has_attribute(const std::string &name) const
Checks if the el has a specific attribute.
box global_bounding_box() const
Retrieves the global bounding box of the el.
friend class element
Definition svg.h:162
text_node to_text_node() const
Converts the nd to a text_node.
node()=default
Constructs a null nd.
element parent_element() const
Returns the parent el.
bool is_null() const
Checks if the nd is null.
Definition svg.h:136
bool operator==(const node &nd) const
Checks if two nodes are equal.
Definition svg.h:149
svg_node * nd() const
Definition svg.h:160
bool is_text_node() const
Checks if the nd is a text nd.
svg_node * m_node
Definition svg.h:161
node(svg_node *nd)
element to_element() const
Converts the nd to an element.
bool operator!=(const node &nd) const
Checks if two nodes are not equal.
Definition svg.h:156
bool is_element() const
Checks if the nd is an el nd.
text_node()=default
Constructs a null text nd.
friend class node
Definition svg.h:189
const std::string & data() const
Returns the text content of the nd.
void set_data(const std::string &data)
Sets the text content of the nd.
Represents a 2D axis-aligned bounding box.
Definition geometry.h:18
#define NOVA_API
Definition config.h:29
bool is_valid_selector(const std::string &selector)
Measures utf8_text with the same font lookup, shaping and kerning that <text> uses.
std::shared_ptr< foreign_object_renderer > default_foreign_object_renderer()
int version()
Returns the version of the novasvg library encoded in a single integer.
std::vector< node > node_list
Definition svg.h:165
std::string version_string()
Returns the novasvg library version as a human-readable string in "X.Y.Z" format.
std::vector< element > element_list
Definition svg.h:295
text_metrics measure_text(const std::string &utf8_text, const std::string &font_family, float font_size, bool bold=false, bool italic=false)
Definition svg.h:56
How big a piece of text is, measured the way a <text> element is laid out.
Definition svg.h:502
float ascent
Distance from the baseline up to the top of the font's line box; never negative.
Definition svg.h:504
float descent
Distance from the baseline down to the bottom of the line box; never negative.
Definition svg.h:505
float line_gap
Extra leading the font asks for between two lines.
Definition svg.h:506
float width
Advance of the whole text (kerning and ligatures included).
Definition svg.h:503