izzi
SVG SUBSET C++ API
Loading...
Searching...
No Matches
a60-svg-render-state.h
Go to the documentation of this file.
1// svg render basics -*- mode: C++ -*-
2
3// Copyright (C) 2014-2024 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_RENDER_STATE_H
17#define MiL_SVG_RENDER_STATE_H 1
18
19
20namespace svg {
21
22 /**
23 Selected or Active in render area.
24 Make discrete element or layer (visible, outline, etc) if true.
25 Used as a (visibility, outline, etc.) bitmask
26 */
27 enum class select
28 {
29 none = 1u << 0, ///< nothing
30 cartography = 1u << 1, ///< cartographic elements
31 image = 1u << 2, ///< image
32 raster = 1u << 3, ///< raster (pixel) elements
33 vector = 1u << 4, ///< svg path, circle, rectangle, etc.
34 blur = 1u << 5, ///< blur or gradient version of vector
35 echo = 1u << 6, ///< b & w outline version of vector
36 text = 1u << 7, ///< metadata, header
37 title = 1u << 8, ///< title
38 legend = 1u << 9, ///< text and symbol legends
39 axis = 1u << 10, ///< axes
40 grid = 1u << 11, ///< grid
41 linex = 1u << 12, ///< horizontal lines
42 liney = 1u << 13, ///< vertical lines
43 ticks = 1u << 14, ///< ticks, markers
44 tooltip = 1u << 15, ///< tooltips meta layer
45 telecom = 1u << 16, ///< telecom infrastructure
46 tor = 1u << 17, ///< tor exit node infrastructure
47 satellite = 1u << 18, ///< megaconstellation satellite
48 cable = 1u << 19, ///< fiber optic
49 glyph = 1u << 20, ///< glyph
50 svg = 1u << 21, ///< svg element, perhaps nested
51 alt = 1u << 22, ///< alternate use specified in situ
52 background = 1u << 23, ///< background elements
53 top = 1u << 24, ///< top
54 bottom = 1u << 25, ///< bottom
55 odd = 1u << 26, ///< odd
56 even = 1u << 27, ///< even
57 all = 1u << 29, ///< all
58 _S_end = 1u << 30 ///< last
59 };
60
61 inline constexpr select
63 {
64 using __utype = typename std::underlying_type<select>::type;
65 __utype r = static_cast<__utype>(__a) & static_cast<__utype>(__b);
66 return static_cast<select>(r);
67 }
68
69 inline constexpr select
71 {
72 using __utype = typename std::underlying_type<select>::type;
73 return select(static_cast<__utype>(__a) | static_cast<__utype>(__b));
74 }
75
76 inline constexpr select
78 {
79 using __utype = typename std::underlying_type<select>::type;
80 return select(static_cast<__utype>(__a) ^ static_cast<__utype>(__b));
81 }
82
83 inline constexpr select
85 {
86 using __utype = typename std::underlying_type<select>::type;
87 return select(~static_cast<__utype>(__a));
88 }
89
90 inline const select&
92 { return __a = __a | __b; }
93
94 inline const select&
96 { return __a = __a & __b; }
97
98 inline const select&
100 { return __a = __a ^ __b; }
101
102 /// Forwarding functions.
103 inline void
105 { a = b; }
106
107 inline void
109 { a &= ~b; }
110
111 inline void
113 { a |= b; }
114
115
116 /**
117 Some high-level nobs for rendering: scale tuning.
118
119 small == defense distributed == 70k-300k
120 medium == westworld == 2-4M
121 large == stranger things == 10M+
122
123 [1-5]xsmall == reduction from least to greatest
124 1x == baseline
125 [1-5]xlarge == enlargement from least to greatest
126 */
137
138
139 // Convenience function for mapping values to strings.
140 string
142 {
143 using enum_map_type = std::map<scale, std::string>;
144
145 static enum_map_type enum_map;
146 if (enum_map.empty())
147 {
148 enum_map[scale::r5s] = "reduce-5s";
149 enum_map[scale::r4s] = "reduce-4s";
150 enum_map[scale::r3s] = "reduce-3s";
151 enum_map[scale::r2s] = "reduce-2s";
152 enum_map[scale::r1s] = "reduce-1s";
153 enum_map[scale::e5s] = "enlarge-5s";
154 enum_map[scale::e4s] = "enlarge-4s";
155 enum_map[scale::e3s] = "enlarge-3s";
156 enum_map[scale::e2s] = "enlarge-2s";
157 enum_map[scale::e1s] = "enlarge-1s";
158 enum_map[scale::medium] = "medium";
159 enum_map[scale::baseline] = "baseline";
160 enum_map[scale::small] = "small";
161 enum_map[scale::xsmall] = "xsmall";
162 enum_map[scale::xxsmall] = "xxsmall";
163 enum_map[scale::large] = "large";
164 enum_map[scale::xlarge] = "xlarge";
165 enum_map[scale::xxlarge] = "xxlarge";
166 }
167 return enum_map[e];
168 }
169
170
171/// Settings for glyph, graphic, chart, and collection object render.
173{
175
176 bool
177 is_visible(const select v) const
178 {
179 using __utype = typename std::underlying_type<select>::type;
180 __utype isv(static_cast<__utype>(visible_mode & v));
181 return isv > 0;
182 }
183
186};
187
188
189/// Collection derived states.
191{
194
195 double opacity;
197
198 // Values with similar geo coordinates counted in as one meta coordinate.
199 bool weigh;
200
201 // Genderate colors from color bands (true) or use finite colors (false).
203
204 // Alternate output naming mode via scheme (true = info, false = map)
205 bool alt;
206
207 collection_rstate(const double o = 0.10,
208 const scale rscale = scale::medium)
210 scale_mode(rscale), opacity(o), text_scale_max(0),
211 weigh(false), color_generated(true), alt(false)
212 { }
213
215
216 string
217 mangle() const
218 {
219 string mangled;
220
221 bool foundp = false;
222
223 const uint vi = static_cast<uint>(visible_mode);
224 if (vi > 0)
225 {
226 mangled += "visible-mode-";
227 mangled += std::to_string(vi);
228 foundp = true;
229 }
230
231 const uint oi = static_cast<uint>(outline_mode);
232 if (oi > 0)
233 {
234 if (foundp)
235 mangled += k::hyphen;
236 else
237 foundp = true;
238 mangled += "outline-mode-";
239 mangled += std::to_string(oi);
240 }
241
242 if (opacity > 0)
243 {
244 if (foundp)
245 mangled += k::hyphen;
246 else
247 foundp = true;
248 mangled += "opacity-";
249 const double o(opacity * 100);
250 const uint oi = static_cast<uint>(o);
251 mangled += std::to_string(oi);
252 }
253
254 return mangled;
255 }
256};
257
258
259/// Render settings for collections.
261{
262 using colormap = std::unordered_map<string, color_qi>;
263
265
267 get_color(const string& s) const
268 {
269 auto i = klrs.find(s);
270 if (i != klrs.end())
271 return i->second;
272 else
273 throw std::runtime_error("render_state::get_color " + s + " not found");
274 }
275
276 color_rstate() = default;
277};
278
279
280/// Global state.
283{
284 static color_rstate state;
285 return state;
286}
287
288
289/// Named render state.
290/// Datum to take id string and tie it to visual representation.
292{
293 style styl; // Overrides render_state_base.opacity
294 string name;
295 double rotate; // Degrees to rotate to origin (CW), if any.
296 double multiple; // Scale factor, if any.
297
298 static style dstyl;
299
300 explicit
301 id_rstate(const style s = dstyl, const string f = "",
302 const double angle = 0.0, const double scale = 1.0)
303 : styl(s), name(f), rotate(angle), multiple(scale) { }
304
305 id_rstate(const id_rstate&) = default;
306 id_rstate& operator=(const id_rstate&) = default;
307};
308
310
311using id_rstate_umap = std::unordered_map<string, id_rstate>;
312
313
316{
317 static id_rstate_umap cache;
318 return cache;
319}
320
321
322/// Add value to cache with base style of styl, colors klr, visibility vis.
323void
324add_to_id_rstate_cache(const string id, const style styl,
325 const select vis)
326{
328
329 id_rstate state(styl, id);
330 set_select(state.visible_mode, vis);
331 cache.insert(std::make_pair(id, state));
332}
333
334
335/// Given identifier/name/id, get corresponding id_rstate from cache.
336const id_rstate
337get_id_rstate(const string id)
338{
339 const id_rstate_umap& cache = get_id_rstate_cache();
340
341 id_rstate ret;
342 if (cache.count(id) == 1)
343 {
344 auto iter = cache.find(id);
345 ret = iter->second;
346 }
347 else
348 {
349 // If default string is set in the cache, use it.
350 if (cache.count("") == 1)
351 {
352 auto iter = cache.find("");
353 ret = iter->second;
354 }
355 }
356 return ret;
357}
358
359
360/// Roll through render states given in values squentially,
361/// index starts with zero.
362const id_rstate
364{
365 static uint indx(0);
366 string value;
367 if (indx < values.size())
368 {
369 value = values[indx];
370 ++indx;
371 }
372 else
373 {
374 // Start over.
375 indx = 0;
376 value = values[indx];
377 }
378 return get_id_rstate(value);
379}
380
381} // namespace svg
382
383#endif
const select & operator^=(select &__a, select __b)
std::unordered_map< string, id_rstate > id_rstate_umap
const string to_string(const unit e)
constexpr select operator|(select __a, select __b)
void add_to_id_rstate_cache(const string id, const style styl, const select vis)
Add value to cache with base style of styl, colors klr, visibility vis.
const select & operator&=(select &__a, select __b)
@ text
metadata, header
@ liney
vertical lines
@ telecom
telecom infrastructure
@ alt
alternate use specified in situ
@ legend
text and symbol legends
@ ticks
ticks, markers
@ cartography
cartographic elements
@ satellite
megaconstellation satellite
@ raster
raster (pixel) elements
@ vector
svg path, circle, rectangle, etc.
@ linex
horizontal lines
@ tor
tor exit node infrastructure
@ cable
fiber optic
@ blur
blur or gradient version of vector
@ echo
b & w outline version of vector
@ background
background elements
@ tooltip
tooltips meta layer
void clear_select(select &a, const select &b)
constexpr select operator^(select __a, select __b)
id_rstate_umap & get_id_rstate_cache()
color_rstate & get_render_state()
Global state.
constexpr select operator&(select __a, select __b)
void set_select(select &a, const select &b)
Forwarding functions.
const id_rstate traverse_states(const strings &values)
Roll through render states given in values squentially, index starts with zero.
void flip_select(select &a, const select &b)
const select & operator|=(select &__a, select __b)
unsigned int uint
Definition a60-svg.h:57
constexpr select operator~(select __a)
std::vector< string > strings
Definition a60-svg.h:37
const id_rstate get_id_rstate(const string id)
Given identifier/name/id, get corresponding id_rstate from cache.
collection_rstate(const collection_rstate &)=default
collection_rstate(const double o=0.10, const scale rscale=scale::medium)
Color quantified as integral RGB components in the range [0,255]. aka like Scalar in OpenCV.
Render settings for collections.
std::unordered_map< string, color_qi > colormap
color_qi get_color(const string &s) const
color_rstate()=default
Named render state. Datum to take id string and tie it to visual representation.
id_rstate(const id_rstate &)=default
id_rstate & operator=(const id_rstate &)=default
id_rstate(const style s=dstyl, const string f="", const double angle=0.0, const double scale=1.0)
render_state_base(const select m=select::none)
bool is_visible(const select v) const
Datum consolidating style preferences.