izzi
SVG SUBSET C++ API
Loading...
Searching...
No Matches
a60-svg-elements-components.h
Go to the documentation of this file.
1// svg element parts -*- mode: C++ -*-
2
3// Copyright (C) 2014-2025 Benjamin De Kosnik <b.dekosnik@gmail.com>
4
5// This file is part of the alpha60-MiL SVG library. This library is
6// free software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the Free
8// Software Foundation; either version 3, or (at your option) any
9// later version.
10
11// This library is distributed in the hope that it will be useful, but
12// WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15
16#ifndef MiL_SVG_ELEMENTS_COMPONENTS_H
17#define MiL_SVG_ELEMENTS_COMPONENTS_H 1
18
19
20namespace svg {
21
22void
24{
26 te.start_element(t);
27 te.finish_element();
28 add_element(te);
29}
30
31void
33{
34 try
35 {
36 string filename(_M_name + ".svg");
37 std::ofstream f(filename);
38 if (!f.is_open() || !f.good())
39 throw std::runtime_error("svg_element::write fail");
40 f << _M_sstream.str() << std::endl;
41 }
42 catch(std::exception& e)
43 {
44 throw e;
45 }
46}
47
48/// SVG element beginning boilerplate for outermost (containing) svg_element.
49/// Variable: unit, x=0, y=0, width, height
50/// @param autoszp if true, then width=100% and height=auto
51void
52svg_element::start_element(const bool autoszp)
53{
54 const string start = R"_delimiter_(<?xml version="1.0" encoding="utf-8"?>
55<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
56<svg version="1.1"
57 xml:space="preserve"
58 xmlns="http://www.w3.org/2000/svg"
59 xmlns:xlink="http://www.w3.org/1999/xlink"
60 xmlns:html="http://www.w3.org/1999/xhtml"
61)_delimiter_";
62
63 const string id("__id");
64 const string unit("__unit");
65 const string width("__width");
66 const string height("__height");
67
68 string strip = R"_delimiter_(id="__id" x="0__unit" y="0__unit"
69width="__width__unit" height="__height__unit"
70viewBox="0 0 __width __height" enable-background="new 0 0 __width __height" role="img">
71)_delimiter_";
72
73 string_replace(strip, id, _M_name);
74
75 if (autoszp)
76 {
77 const string wunit = width + unit;
78 const string hunit = height + unit;
79 string_replace(strip, wunit, "100%");
80 string_replace(strip, hunit, "auto");
81 }
83 string_replace(strip, width, std::to_string(_M_area._M_width));
84 string_replace(strip, height, std::to_string(_M_area._M_height));
85
87 _M_sstream << strip << std::endl;
88}
89
90
91/// Default
92void
94{ start_element(false); }
95
96
97/// SVG element for nested svg_element.
98/// Use name, area, unit, typo for viewport frame
99/// @param p origin x,y position
100/// @param desta height, width of destination, iff > _M_area then enlarge.
101void
102svg_element::start_element(const point_2t p, const area destarea,
103 const style& styl)
104{
105 const string id("__id");
106 string start = R"_delimiter_(<svg id="__id" )_delimiter_";
108
109 const string unit("__unit");
110 const string width("__width");
111 const string height("__height");
112 const string dwidth("__dwidth");
113 const string dheight("__dheight");
114 const string px("__x");
115 const string py("__y");
116 string strip = R"_delimiter_(viewBox="0 0 __width __height" x="__x__unit" y="__y__unit" width="__dwidth__unit" height="__dheight__unit" )_delimiter_";
117
119 string_replace(strip, width, std::to_string(_M_area._M_width));
120 string_replace(strip, height, std::to_string(_M_area._M_height));
121
122 const auto [ dw, dh ] = destarea;
123 const auto [ x, y ] = p;
124 string_replace(strip, dwidth, std::to_string(dw));
125 string_replace(strip, dheight, std::to_string(dh));
126 string_replace(strip, px, std::to_string(int(x)));
127 string_replace(strip, py, std::to_string(int(y)));
128
129 // Check to make sure stream starts empty.
130 if (!this->empty())
131 {
132 string m("svg_element::start_element error buffer is not empty: " );
133 m += k::newline;
134 m += this->str();
135 m += k::newline;
136 throw std::runtime_error(m);
137 }
138
139 _M_sstream << start;
140 _M_sstream << strip << std::endl;
141
142 // Only add style if it is not the default argument.
143 const string nostr = to_string(color::none);
144 const string fillstr = to_string(styl._M_fill_color);
145 const bool colornonep = nostr == fillstr;
146 if (!colornonep && (styl._M_fill_opacity != 0 || styl._M_stroke_opacity != 0))
147 {
148 add_style(styl);
149 _M_sstream << k::space;
150 }
151 _M_sstream << '>' << k::newline;
152}
153
154
155void
157{
158 const string f = R"_delimiter_(<defs>
159 <filter id="gblur10" x="0" y="0">
160 <feGaussianBlur in="SourceGraphic" stdDeviation="10" />
161 <feOffset dx="0" dy="0" />
162 </filter>
163 <filter id="gblur20" x="0" y="0">
164 <feGaussianBlur in="SourceGraphic" stdDeviation="20" />
165 <feOffset dx="0" dy="0" />
166 </filter>
167 <filter id="gblur10y" x="0" y="0">
168 <feGaussianBlur in="SourceGraphic" stdDeviation="0, 10" />
169 <feOffset dx="0" dy="0" />
170 </filter>
171 <filter id="gblur20y" x="0" y="0">
172 <feGaussianBlur in="SourceGraphic" stdDeviation="0, 20" />
173 <feOffset dx="0" dy="0" />
174 </filter>
175 </defs>
176)_delimiter_";
177
178 _M_sstream << f << std::endl;
179}
180
181
182/// SVG element end boilerplate.
183void
185{
186 _M_sstream << "</svg>" << std::endl;
187}
188
189} // namespace svg
190
191#endif
stream_type _M_sstream
Virtual, only one buffer.
void add_style(const style &sty)
void finish_element()
SVG element end boilerplate.
void start(const string &desc="", const bool autoszp=false)
void start_element(void)
Default.
void add_title(const string &t)
bool empty()
Empty when the output buffer is.
string str() const
void add_element(const element_base &e)
unit
Measurement abstraction for absolute (not relative) measurements.
@ px
Pixel where 1 pixel x 96 PPI = .26 mm.
const string to_string(const unit e)
void string_replace(std::string &target, const std::string &match, const std::string &replace)
Definition a60-svg.h:44
std::tuple< space_type, space_type > point_2t
Point (x,y) in 2D space, space_type defaults to double.
Definition izzi-points.h:22
Datum consolidating style preferences.
color_qi _M_fill_color