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 : ulong
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 Some high-level nobs for rendering: scale tuning.
117
118 small == defense distributed == 70k-300k
119 medium == westworld == 2-4M
120 large == stranger things == 10M+
121
122 [1-5]xsmall == reduction from least to greatest
123 1x == baseline
124 [1-5]xlarge == enlargement from least to greatest
125 */
136
137
138 // Convenience function for mapping values to strings.
139 string
141 {
142 using enum_map_type = std::map<scale, std::string>;
143
144 static enum_map_type enum_map;
145 if (enum_map.empty())
146 {
147 enum_map[scale::r5s] = "reduce-5s";
148 enum_map[scale::r4s] = "reduce-4s";
149 enum_map[scale::r3s] = "reduce-3s";
150 enum_map[scale::r2s] = "reduce-2s";
151 enum_map[scale::r1s] = "reduce-1s";
152 enum_map[scale::e5s] = "enlarge-5s";
153 enum_map[scale::e4s] = "enlarge-4s";
154 enum_map[scale::e3s] = "enlarge-3s";
155 enum_map[scale::e2s] = "enlarge-2s";
156 enum_map[scale::e1s] = "enlarge-1s";
157 enum_map[scale::medium] = "medium";
158 enum_map[scale::baseline] = "baseline";
159 enum_map[scale::small] = "small";
160 enum_map[scale::xsmall] = "xsmall";
161 enum_map[scale::xxsmall] = "xxsmall";
162 enum_map[scale::large] = "large";
163 enum_map[scale::xlarge] = "xlarge";
164 enum_map[scale::xxlarge] = "xxlarge";
165 }
166 return enum_map[e];
167 }
168
169
170/// Settings for glyph, graphic, chart, and collection object render.
172{
174
175 bool
176 is_visible(const select v) const
177 {
178 using __utype = typename std::underlying_type<select>::type;
179 __utype isv(static_cast<__utype>(visible_mode & v));
180 return isv > 0;
181 }
182
185};
186
187
188/// Collection derived states.
190{
193
194 double opacity;
196
197 // Values with similar geo coordinates counted in as one meta coordinate.
198 bool weigh;
199
200 // Genderate colors from color bands (true) or use finite colors (false).
202
203 // Alternate output naming mode via scheme (true = info, false = map)
204 bool alt;
205
206 collection_rstate(const double o = 0.10,
207 const scale rscale = scale::medium)
209 scale_mode(rscale), opacity(o), text_scale_max(0),
210 weigh(false), color_generated(true), alt(false)
211 { }
212
214
215 string
216 mangle() const
217 {
218 string mangled;
219
220 bool foundp = false;
221
222 const uint vi = static_cast<uint>(visible_mode);
223 if (vi > 0)
224 {
225 mangled += "visible-mode-";
226 mangled += std::to_string(vi);
227 foundp = true;
228 }
229
230 const uint oi = static_cast<uint>(outline_mode);
231 if (oi > 0)
232 {
233 if (foundp)
234 mangled += k::hyphen;
235 else
236 foundp = true;
237 mangled += "outline-mode-";
238 mangled += std::to_string(oi);
239 }
240
241 if (opacity > 0)
242 {
243 if (foundp)
244 mangled += k::hyphen;
245 else
246 foundp = true;
247 mangled += "opacity-";
248 const double o(opacity * 100);
249 const uint oi = static_cast<uint>(o);
250 mangled += std::to_string(oi);
251 }
252
253 return mangled;
254 }
255};
256
257
258/// Render settings for collections.
260{
261 using colormap = std::unordered_map<string, color_qi>;
262
264
266 get_color(const string& s) const
267 {
268 auto i = klrs.find(s);
269 if (i != klrs.end())
270 return i->second;
271 else
272 throw std::runtime_error("render_state::get_color " + s + " not found");
273 }
274
275 color_rstate() = default;
276};
277
278
279/// Global state.
282{
283 static color_rstate state;
284 return state;
285}
286
287
288/// Named render state.
289/// Datum to take id string and tie it to visual representation.
291{
292 style styl; // Overrides render_state_base.opacity
293 string name;
294 double rotate; // Degrees to rotate to origin (CW), if any.
295 double multiple; // Scale factor, if any.
296
297 static style dstyl;
298
299 explicit
300 id_rstate(const style s = dstyl, const string f = "",
301 const double angle = 0.0, const double scale = 1.0)
302 : styl(s), name(f), rotate(angle), multiple(scale) { }
303
304 id_rstate(const id_rstate&) = default;
305 id_rstate& operator=(const id_rstate&) = default;
306};
307
309
310using id_rstate_umap = std::unordered_map<string, id_rstate>;
311
312
315{
316 static id_rstate_umap cache;
317 return cache;
318}
319
320
321/// Add value to cache with base style of styl, colors klr, visibility vis.
322void
323add_to_id_rstate_cache(const string id, const style styl,
324 const select vis)
325{
327
328 id_rstate state(styl, id);
329 set_select(state.visible_mode, vis);
330 cache.insert(std::make_pair(id, state));
331}
332
333
334/// Given identifier/name/id, get corresponding id_rstate from cache.
335const id_rstate
336get_id_rstate(const string id)
337{
338 const id_rstate_umap& cache = get_id_rstate_cache();
339
340 id_rstate ret;
341 if (cache.count(id) == 1)
342 {
343 auto iter = cache.find(id);
344 ret = iter->second;
345 }
346 else
347 {
348 // If default string is set in the cache, use it.
349 if (cache.count("") == 1)
350 {
351 auto iter = cache.find("");
352 ret = iter->second;
353 }
354 }
355 return ret;
356}
357
358
359/// Roll through render states given in values squentially,
360/// index starts with zero.
361const id_rstate
363{
364 static uint indx(0);
365 string value;
366 if (indx < values.size())
367 {
368 value = values[indx];
369 ++indx;
370 }
371 else
372 {
373 // Start over.
374 indx = 0;
375 value = values[indx];
376 }
377 return get_id_rstate(value);
378}
379
380} // namespace svg
381
382#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)
unsigned long ulong
Definition a60-svg.h:58
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)
@ 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
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.