NovaSVG 0.11.5
An SVG library
Loading...
Searching...
No Matches
foreign_object.h
Go to the documentation of this file.
1#ifndef NOVASVG_FOREIGN_OBJECT_H
2#define NOVASVG_FOREIGN_OBJECT_H
3#pragma once
4
5// The extension point for <foreignObject> (ADR-0010).
6//
7// novasvg does not know what is inside a <foreignObject>: it has a box and a piece of markup, and a
8// foreign_object_renderer that draws it. The built-in renderer ("simple": HTML as styled text) is one such
9// renderer, written against exactly this header. Give a document your own with
10// document::set_foreign_object_renderer(), e.g. one that rasterizes the markup with a real HTML engine and
11// hands the pixels to the painter.
12
13#include <memory>
14#include <optional>
15#include <string>
16#include <string_view>
17#include <vector>
18
19#include <stb/color.hpp>
20#include <stb/image.hpp>
21
22#include "../config.h"
23#include "../vg/geometry.h"
24
25namespace nova::svg {
26
28 std::string family;
29 float size = 16.f;
30 int weight = 400;
31 bool bold = false;
32 bool italic = false;
33};
34
36 float ascent = 0.f;
37 float height = 0.f;
38};
39
41 float width = 0.f;
42 float height = 0.f;
43 int line_count = 0;
44 float line_height = 0.f;
45};
46
49struct html_tag {
50 std::string tag;
51 std::string id;
52 std::string class_names;
53 std::string style;
54 int parent = -1;
55};
56
58 std::string value;
59 int specificity = 0;
60};
61
65class css_query {
66public:
67 virtual ~css_query() = default;
71 virtual std::optional<css_declaration> declared(const std::vector<html_tag>& nodes, int index, std::string_view property) const = 0;
72};
73
76 float factor = 1.f;
77 float about_x = 0.f;
78};
79
83public:
84 virtual ~foreign_object_painter() = default;
85 virtual void fill_rect(const vg::box& box_, const stb::color& color_) = 0;
87 virtual void fill_text(std::u32string_view text, const foreign_object_font& font_, float x, float y,
88 const stb::color& color_, const text_squeeze& squeeze = {}) = 0;
90 virtual void draw_image(const stb::image& image, const vg::box& dest) = 0;
92 virtual float measure_text(std::u32string_view text, const foreign_object_font& font_) const = 0;
93};
94
96 std::string_view markup;
99 float device_scale = 1.f;
100 const void* prepared = nullptr;
101};
102
105public:
106 virtual ~foreign_object_renderer() = default;
107
111 virtual std::shared_ptr<void> prepare(std::string_view markup, const css_query& css) const
112 {
113 (void)markup;
114 (void)css;
115 return nullptr;
116 }
117
118 virtual void render(const foreign_object_context& context, foreign_object_painter& painter) const = 0;
119
122 virtual std::optional<foreign_object_size> measure(const foreign_object_context& context, const foreign_object_painter& painter,
123 float max_width) const
124 {
125 (void)context;
126 (void)painter;
127 (void)max_width;
128 return std::nullopt;
129 }
130};
131
134NOVA_API std::shared_ptr<foreign_object_renderer> default_foreign_object_renderer();
135
136} // namespace nova::svg
137
138#endif // NOVASVG_FOREIGN_OBJECT_H
virtual ~css_query()=default
virtual std::optional< css_declaration > declared(const std::vector< html_tag > &nodes, int index, std::string_view property) const =0
virtual void fill_text(std::u32string_view text, const foreign_object_font &font_, float x, float y, const stb::color &color_, const text_squeeze &squeeze={})=0
x, y: the left end of the baseline.
virtual ~foreign_object_painter()=default
virtual float measure_text(std::u32string_view text, const foreign_object_font &font_) const =0
virtual foreign_object_font_metrics metrics(const foreign_object_font &font_) const =0
virtual void draw_image(const stb::image &image, const vg::box &dest)=0
Straight (not premultiplied) RGBA, stretched to dest. For renderers that rasterize.
virtual void fill_rect(const vg::box &box_, const stb::color &color_)=0
Called concurrently from const methods on one document: keep no mutable state without a lock.
virtual std::optional< foreign_object_size > measure(const foreign_object_context &context, const foreign_object_painter &painter, float max_width) const
virtual ~foreign_object_renderer()=default
virtual std::shared_ptr< void > prepare(std::string_view markup, const css_query &css) const
virtual void render(const foreign_object_context &context, foreign_object_painter &painter) const =0
Represents a 2D axis-aligned bounding box.
Definition geometry.h:18
#define NOVA_API
Definition config.h:29
std::shared_ptr< foreign_object_renderer > default_foreign_object_renderer()
int specificity
the larger one wins; a later declaration wins a tie
std::string_view markup
untouched, as in the source
vg::box box
the foreignObject's x, y, width, height
foreign_object_font font
inherited through the svg cascade
const void * prepared
what prepare() returned for this element
float device_scale
device pixels per user unit (the larger axis), for renderers that rasterize
int weight
CSS font-weight, 1 to 1000 (400 normal, 700 bold).
bool bold
weight >= 600: for a painter that only knows regular and bold; novasvg sets it with weight
std::string style
the style="" attribute, verbatim
std::string class_names
std::string tag
lower case
Condense text horizontally about about_x (a line that is wider than its box).