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-2020 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
41 f << _M_sstream.str() << std::endl;
42 }
43 catch(std::exception& e)
44 {
45 throw e;
46 }
47}
48
49/// SVG element beginning boilerplate for outermost (containing) svg_element.
50/// Variable: unit, x=0, y=0, width, height
51void
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 id="svg2" 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 unit("__unit");
64 const string width("__width");
65 const string height("__height");
66
67 string strip = R"_delimiter_(x="0__unit" y="0__unit"
68width="__width__unit" height="__height__unit"
69viewBox="0 0 __width __height" enable-background="new 0 0 __width __height">
70)_delimiter_";
71
73 string_replace(strip, width, std::to_string(_M_area._M_width));
74 string_replace(strip, height, std::to_string(_M_area._M_height));
75
77 _M_sstream << strip << std::endl;
78}
79
80
81/// SVG element for nested svg_element.
82/// Use name, area, unit, typo for viewport frame
83/// @param p origin x,y position
84/// @param desta height, width of destination, iff > _M_area then enlarge.
85void
86svg_element::start_element(const point_2t p, const area destarea,
87 const style& styl)
88{
89 const string id("__id");
90 string start = R"_delimiter_(<svg id="__id" )_delimiter_";
92
93 const string unit("__unit");
94 const string width("__width");
95 const string height("__height");
96 const string dwidth("__dwidth");
97 const string dheight("__dheight");
98 const string px("__x");
99 const string py("__y");
100 string strip = R"_delimiter_(viewBox="0 0 __width __height" x="__x__unit" y="__y__unit" width="__dwidth__unit" height="__dheight__unit" )_delimiter_";
101
103 string_replace(strip, width, std::to_string(_M_area._M_width));
104 string_replace(strip, height, std::to_string(_M_area._M_height));
105
106 const auto [ dw, dh ] = destarea;
107 const auto [ x, y ] = p;
108 string_replace(strip, dwidth, std::to_string(dw));
109 string_replace(strip, dheight, std::to_string(dh));
110 string_replace(strip, px, std::to_string(int(x)));
111 string_replace(strip, py, std::to_string(int(y)));
112
113 // Check to make sure stream starts empty.
114 if (!this->empty())
115 {
116 string m("svg_element::start_element error buffer is not empty: " );
117 m += k::newline;
118 m += this->str();
119 m += k::newline;
120 throw std::runtime_error(m);
121 }
122
123 _M_sstream << start;
124 _M_sstream << strip << std::endl;
125
126 // Only add style if it is not the default argument.
127 const string nostr = to_string(color::none);
128 const string fillstr = to_string(styl._M_fill_color);
129 const bool colornonep = nostr == fillstr;
130 if (!colornonep && (styl._M_fill_opacity != 0 || styl._M_stroke_opacity != 0))
131 {
132 add_style(styl);
133 _M_sstream << k::space;
134 }
135 _M_sstream << '>' << k::newline;
136}
137
138
139void
141{
142 const string f = R"_delimiter_(<defs>
143 <filter id="gblur10" x="0" y="0">
144 <feGaussianBlur in="SourceGraphic" stdDeviation="10" />
145 <feOffset dx="0" dy="0" />
146 </filter>
147 <filter id="gblur20" x="0" y="0">
148 <feGaussianBlur in="SourceGraphic" stdDeviation="20" />
149 <feOffset dx="0" dy="0" />
150 </filter>
151 <filter id="gblur10y" x="0" y="0">
152 <feGaussianBlur in="SourceGraphic" stdDeviation="0, 10" />
153 <feOffset dx="0" dy="0" />
154 </filter>
155 <filter id="gblur20y" x="0" y="0">
156 <feGaussianBlur in="SourceGraphic" stdDeviation="0, 20" />
157 <feOffset dx="0" dy="0" />
158 </filter>
159 </defs>
160)_delimiter_";
161
162 _M_sstream << f << std::endl;
163}
164
165
166/// SVG element end boilerplate.
167void
169{
170 _M_sstream << "</svg>" << std::endl;
171}
172
173} // namespace svg
174
175#endif
stream_type _M_sstream
Virtual, only one buffer.
void add_style(const style &sty)
void add_title(const string &t)
void start_element()
SVG element beginning boilerplate for outermost (containing) svg_element. Variable: unit,...
bool empty()
Empty when the output buffer is.
void start(const string &desc="")
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:43
std::tuple< space_type, space_type > point_2t
Point (x,y) in 2D space.
Definition a60-svg.h:65
Datum consolidating style preferences.
color_qi _M_fill_color