Loading...
Searching...
No Matches
AlignAnchorSVG.h
1/*
2
3MIT License
4
5Copyright (c) 2023 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31/*
32 * TreeSVG.h
33 *
34 * Created on: Nov, 2024
35 * Author: mpeura
36 */
37
38#ifndef DRAIN_ALIGN_ADAPTER_SVG
39#define DRAIN_ALIGN_ADAPTER_SVG
40
41//#include "drain/util/FileInfo.h"
42// #include "drain/util/Frame.h"
43//
44#include <string>
45//#include "drain/util/EnumFlags.h"
46
47#include "AlignSVG.h"
48
49namespace drain {
50
51namespace image {
52
53/* Semantics:
54 *
55 * if myAnchor is set, use it.
56 * if myAnchor is unset, use group anchor; it servers as a default anchor
57 * if groupAnchor is unset -> use previous object as anchor.
58 *
59 * GroupAnchor (default anchor):
60 DEFAULT(=UNSET): use previous object
61 NONE: use nothing, don't align
62 PREVIOUS: use previous (unneeded, this is the default for group)
63 COLLECTIVE: use compound bounding bbox
64
65 MyAnchor (specific anchor, always overrides)
66 DEFAULT(=UNSET) -> use GroupAnchor
67 NONE: don't align me! (raise error if Align set?)
68 PREVIOUS:
69 COLLECTIVE: use compound bounding box
70
71 SPECIAL: both DEFAULT: use PREVIOUS
72 *
73 */
74
75// Could be internal class in Adapter?
76struct AnchorElem : public std::string {
77
78 // More like a symbol.
79 enum Anchor {
80 DEFAULT = 0, // use group's default anchor, or if that is not set, use previous
81 NONE = 1, // don't align at alla
82 PREVIOUS, // use previous object
83 NEXT, // use next object - applied typically by background map adapting to actual overlay image (requires revisiting loop)
84 COLLECTIVE_CURRENT, // use dynamically growing bounding box or the compound
85 COLLECTIVE_FINAL, // use dynamically growing bounding box or the compound - in the end.
86 };
87
88
89 inline
90 AnchorElem(const std::string & s="") : std::string(s){
91 };
92
93 inline
94 AnchorElem(const AnchorElem & a) : std::string(a){
95 };
96
97 inline
98 const std::string & str() const {
99 return *this;
100 }
101
102 inline
103 std::string & str() {
104 return *this;
105 }
106
107 void set(const std::string & s);
108
109 void set(const AnchorElem::Anchor & anchor);
110
112 inline
113 bool isSet() const {
114 return !empty();
115 }
116
117 // Use the bounding box of the compound, "accumulated" object as anchor.
118 inline
119 bool isCollective() const {
120 return (drain::EnumDict<AnchorElem::Anchor>::getValue(*this) == Anchor::COLLECTIVE_CURRENT);
121 }
122
123 // Use the bounding box of the latest object as anchor.
124 bool isPrevious() const {
125 return (drain::EnumDict<AnchorElem::Anchor>::getValue(*this) == Anchor::PREVIOUS);
126 }
127
128 // Explicitly states that an object uses no anchor, hence the position is absolute (not aligned)
129 bool isNone() const {
130 return (drain::EnumDict<AnchorElem::Anchor>::getValue(*this) == Anchor::NONE);
131 }
132
133 // Use the bounding box of a named object as anchor.
134 inline
135 bool isSpecific() const {
136 return isSet() && (!isCollective()) && (!isPrevious()) && (!isNone());
137 }
138
139
140};
141
142}
143
144}
145
146/*
147DRAIN_ENUM_DICT(drain::image::AnchorElem::Anchor);
148
149DRAIN_ENUM_OSTREAM(drain::image::AnchorElem::Anchor);
150*/
151
152
153namespace drain {
154
155DRAIN_ENUM_DICT(image::AnchorElem::Anchor);
156DRAIN_ENUM_OSTREAM(image::AnchorElem::Anchor);
157
158namespace image {
159
161struct AlignAnchorSVG { // : public AlignSVG {
162
163
164public:
165
166 typedef AnchorElem anchor_t; // needed?
167
168 template <class T>
169 inline
170 void setMyAlignAnchor(const T & value){
171 adjustAnchor(myAnchorHorz, value);
172 myAnchorVert = myAnchorHorz;
173 updateAlign();
174 }
175
176 template <AlignBase::Axis AX, class T>
177 inline
178 void setMyAlignAnchor(const T & value){
179 adjustAnchor(getMyAlignAnchor<AX>(), value);
180 updateAlign();
181 }
182
183 template <class T>
184 inline
185 void setDefaultAlignAnchor(const T & value){
186 adjustAnchor(defaultAnchorHorz, value);
187 defaultAnchorVert = defaultAnchorHorz;
188 updateAlign();
189 }
190
191 template <AlignBase::Axis AX, class T>
192 inline
193 void setDefaultAlignAnchor(const T & value){
194 AnchorElem & defaultAnchor = getDefaultAlignAnchor<AX>();
195 adjustAnchor(defaultAnchor, value);
196 if (defaultAnchor.isPrevious()){
197 Logger(__FILE__, __FUNCTION__, __LINE__).suspicious("Removing explicit ", AnchorElem::Anchor::PREVIOUS, " as non-informative for group default");
198 // getDefaultAlignAnchor<AX>() = AnchorElem::Anchor::DEFAULT;
199 adjustAnchor(defaultAnchor, AnchorElem::Anchor::DEFAULT);
200 }
201 updateAlign();
202 }
203
204
206 template <AlignBase::Axis AX>
207 const anchor_t & getMyAlignAnchor() const;
208
210 template <AlignBase::Axis AX>
211 const anchor_t & getDefaultAlignAnchor() const;
212
214 template <AlignBase::Axis AX>
216
218 template <AlignBase::Axis AX>
219 anchor_t & getDefaultAlignAnchor();
220
221
222
223 inline virtual
224 ~AlignAnchorSVG(){};
225
226 void swapAnchors(AlignAnchorSVG & anchors);
227
228protected:
229
230 // "string detectors"
231
232 static inline
233 void adjustAnchor(AnchorElem & anchor, const std::string & value){
234 anchor.assign(value);
235 }
236
237 static inline
238 void adjustAnchor(AnchorElem & anchor, const char * value){
239 anchor.assign(value);
240 }
241
242 static inline
243 void adjustAnchor(AnchorElem & anchor, const AnchorElem & elem){
244 anchor.assign(elem);
245 }
246
247 template <class T>
248 static inline
249 void adjustAnchor(AnchorElem & anchor, const T & value){
250 anchor.assign(EnumDict<T>::getKey(value));
251 }
252
254 virtual
255 void updateAlign() = 0;
256
257 // essentially std::string's
258 anchor_t myAnchorHorz;
259 anchor_t myAnchorVert;
260
261 anchor_t defaultAnchorHorz;
262 anchor_t defaultAnchorVert;
263
264
265};
266
267
268template <>
269inline
270const AlignAnchorSVG::anchor_t & AlignAnchorSVG::getMyAlignAnchor<AlignBase::Axis::HORZ>() const {
271 return myAnchorHorz;
272};
273
274template <>
275inline
276const AlignAnchorSVG::anchor_t & AlignAnchorSVG::getMyAlignAnchor<AlignBase::Axis::VERT>() const {
277 return myAnchorVert;
278};
279
281template <>
282inline
283const AlignAnchorSVG::anchor_t & AlignAnchorSVG::getDefaultAlignAnchor<AlignBase::Axis::HORZ>() const {
284 return defaultAnchorHorz;
285};
286
287template <>
288inline
289const AlignAnchorSVG::anchor_t & AlignAnchorSVG::getDefaultAlignAnchor<AlignBase::Axis::VERT>() const {
290 return defaultAnchorVert;
291};
292
293template <>
294inline
295AlignAnchorSVG::anchor_t & AlignAnchorSVG::getMyAlignAnchor<AlignBase::Axis::HORZ>() {
296 return myAnchorHorz;
297};
298
299template <>
300inline
301AlignAnchorSVG::anchor_t & AlignAnchorSVG::getMyAlignAnchor<AlignBase::Axis::VERT>() {
302 return myAnchorVert;
303};
304
306template <>
307inline
308AlignAnchorSVG::anchor_t & AlignAnchorSVG::getDefaultAlignAnchor<AlignBase::Axis::HORZ>() {
309 return defaultAnchorHorz;
310};
311
312template <>
313inline
314AlignAnchorSVG::anchor_t & AlignAnchorSVG::getDefaultAlignAnchor<AlignBase::Axis::VERT>() {
315 return defaultAnchorVert;
316};
317
318
319
320} // image::
321
322
323
324} // drain::
325
326//DRAIN_ENUM_DICT(drain::image::AnchorElem::Anchor);
327//DRAIN_ENUM_OSTREAM(drain::image::AnchorElem::Anchor);
328
329#endif // DRAIN_ALIGN_SVG_H_
330
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:312
Logger & suspicious(const TT &... args)
A weak warning about something going possibly wrong.
Definition Log.h:500
Definition DataSelector.cpp:1277
A container for a static dictionary of enumeration values.
Definition EnumFlags.h:69
static const std::string & getKey(const std::string &s, bool lenient=true)
Convenience for object.set(...) like commands.
Definition EnumFlags.h:162
Adapter designed for NodeSVG.
Definition AlignAnchorSVG.h:161
const anchor_t & getMyAlignAnchor() const
Store anchor object/symbol for aligning this object.
virtual void updateAlign()=0
Redefined in NodeSVG.
Definition AlignAnchorSVG.h:76
bool isSet() const
If not set, use default.
Definition AlignAnchorSVG.h:113