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#include <drain/Log.h>
47#include <drain/util/ClassXML.h>
48#include "AlignSVG.h"
49
50namespace drain {
51
52namespace image {
53
54/* Semantics:
55 *
56 * if myAnchor is set, use it.
57 * if myAnchor is unset, use group anchor; it servers as a default anchor
58 * if groupAnchor is unset -> use previous object as anchor.
59 *
60 * GroupAnchor (default anchor):
61 DEFAULT(=UNSET): use previous object
62 NONE: use nothing, don't align
63 PREVIOUS: use previous (unneeded, this is the default for group)
64 COLLECTIVE: use compound bounding bbox
65
66 MyAnchor (specific anchor, always overrides)
67 DEFAULT(=UNSET) -> use GroupAnchor
68 NONE: don't align me! (raise error if Align set?)
69 PREVIOUS:
70 COLLECTIVE: use compound bounding box
71
72 SPECIAL: both DEFAULT: use PREVIOUS
73 *
74 */
75
76// Could be internal class in Adapter?
77struct AnchorElem : public std::string {
78
79 // More like a symbol.
80 enum Anchor {
81 DEFAULT = 0, // use group's default anchor, or if that is not set, use previous
82 NONE = 1, // don't align at alla
83 PREVIOUS, // use previous object
84 NEXT, // use next object - applied typically by background map adapting to actual overlay image (requires revisiting loop)
85 CURRENT_COMPOUND, // use dynamically growing bounding box or the compound
86 COLLECTIVE_FINAL, // use dynamically growing bounding box or the compound - in the end.
87 };
88
89
90 inline
91 AnchorElem(const std::string & s="") : std::string(s){
92 };
93
94 inline
95 AnchorElem(const AnchorElem & a) : std::string(a){
96 };
97
98 AnchorElem & operator=(const AnchorElem &) = default;
99
100 inline
101 const std::string & str() const {
102 return *this;
103 }
104
105 inline
106 std::string & str() {
107 return *this;
108 }
109
110 void set(const std::string & s);
111
112 void set(const AnchorElem::Anchor & anchor);
113
115 inline
116 bool isSet() const {
117 return !empty();
118 }
119
120 // Use the bounding box of the compound, "accumulated" object as anchor.
121 inline
122 bool isCollective() const {
123 return (drain::Enum<AnchorElem::Anchor>::getValue(*this) == Anchor::CURRENT_COMPOUND);
124 }
125
126 // Use the bounding box of the latest object as anchor.
127 bool isPrevious() const {
128 return (drain::Enum<AnchorElem::Anchor>::getValue(*this) == Anchor::PREVIOUS);
129 }
130
131 // Explicitly states that an object uses no anchor, hence the position is absolute (not aligned)
132 bool isNone() const {
133 return (drain::Enum<AnchorElem::Anchor>::getValue(*this) == Anchor::NONE);
134 }
135
136 // Use the bounding box of a named object as anchor.
137 inline
138 bool isSpecific() const {
139 return isSet() && (!isCollective()) && (!isPrevious()) && (!isNone());
140 }
141
142
143};
144
145} // drain::
146
147// }
148//
149// namespace drain {
150
151DRAIN_ENUM_DICT(image::AnchorElem::Anchor);
152//DRAIN_ENUM_OSTREAM(image::AnchorElem::Anchor);
153
154namespace image {
155
156DRAIN_ENUM_OSTREAM(AnchorElem::Anchor)
157
158
159struct AlignAnchorSVG { // : public AlignSVG {
160
161
162public:
163
164 typedef AnchorElem anchor_t; // needed?
165
166 template <class T>
167 inline
168 void setMyAlignAnchor(const T & value){
169 adjustAnchor(myAnchorHorz, value);
170 myAnchorVert = myAnchorHorz;
171 updateAlign();
172 }
173
174 template <AlignBase::Axis AX, class T>
175 inline
176 void setMyAlignAnchor(const T & value){
177 adjustAnchor(getMyAlignAnchor<AX>(), value);
178 updateAlign();
179 }
180
181 template <class T>
182 inline
183 void setDefaultAlignAnchor(const T & value){
184 adjustAnchor(defaultAnchorHorz, value);
185 defaultAnchorVert = defaultAnchorHorz;
186 updateAlign();
187 }
188
189 template <AlignBase::Axis AX, class T>
190 inline
191 void setDefaultAlignAnchor(const T & value){
192 AnchorElem & defaultAnchor = getDefaultAlignAnchor<AX>();
193 adjustAnchor(defaultAnchor, value);
194 if (defaultAnchor.isPrevious()){
195 Logger(__FILE__, __FUNCTION__, __LINE__).suspicious("Removing explicit ", AnchorElem::Anchor::PREVIOUS, " as non-informative for group default");
196 // getDefaultAlignAnchor<AX>() = AnchorElem::Anchor::DEFAULT;
197 adjustAnchor(defaultAnchor, AnchorElem::Anchor::DEFAULT);
198 }
199 updateAlign();
200 }
201
202
204 template <AlignBase::Axis AX>
205 const anchor_t & getMyAlignAnchor() const;
206
208 template <AlignBase::Axis AX>
209 const anchor_t & getDefaultAlignAnchor() const;
210
212 template <AlignBase::Axis AX>
213 anchor_t & getMyAlignAnchor();
214
216 template <AlignBase::Axis AX>
217 anchor_t & getDefaultAlignAnchor();
218
219
220
221 inline virtual
222 ~AlignAnchorSVG(){};
223
224 void swapAnchors(AlignAnchorSVG & anchors);
225
226protected:
227
228 // "string detectors"
229
230 static inline
231 void adjustAnchor(AnchorElem & anchor, const std::string & value){
232 anchor.assign(value);
233 }
234
235 static inline
236 void adjustAnchor(AnchorElem & anchor, const char * value){
237 anchor.assign(value);
238 }
239
240 static inline
241 void adjustAnchor(AnchorElem & anchor, const AnchorElem & elem){
242 anchor.assign(elem);
243 }
244
245 static inline
246 void adjustAnchor(AnchorElem & anchor, const ClassXML & cls){
247 anchor.assign(cls);
248 }
249
250 template <class T>
251 static inline
252 void adjustAnchor(AnchorElem & anchor, const T & value){
253 anchor.assign(Enum<T>::getKey(value));
254 }
255
257 virtual
258 void updateAlign() = 0;
259
260 // essentially std::string's
261 anchor_t myAnchorHorz;
262 anchor_t myAnchorVert;
263
264 anchor_t defaultAnchorHorz;
265 anchor_t defaultAnchorVert;
266
267
268};
269
270
271template <>
272inline
273const AlignAnchorSVG::anchor_t & AlignAnchorSVG::getMyAlignAnchor<AlignBase::Axis::HORZ>() const {
274 return myAnchorHorz;
275}
276
277template <>
278inline
279const AlignAnchorSVG::anchor_t & AlignAnchorSVG::getMyAlignAnchor<AlignBase::Axis::VERT>() const {
280 return myAnchorVert;
281}
282
284template <>
285inline
286const AlignAnchorSVG::anchor_t & AlignAnchorSVG::getDefaultAlignAnchor<AlignBase::Axis::HORZ>() const {
287 return defaultAnchorHorz;
288}
289
290template <>
291inline
292const AlignAnchorSVG::anchor_t & AlignAnchorSVG::getDefaultAlignAnchor<AlignBase::Axis::VERT>() const {
293 return defaultAnchorVert;
294}
295
296template <>
297inline
298AlignAnchorSVG::anchor_t & AlignAnchorSVG::getMyAlignAnchor<AlignBase::Axis::HORZ>() {
299 return myAnchorHorz;
300}
301
302template <>
303inline
304AlignAnchorSVG::anchor_t & AlignAnchorSVG::getMyAlignAnchor<AlignBase::Axis::VERT>() {
305 return myAnchorVert;
306}
307
309template <>
310inline
311AlignAnchorSVG::anchor_t & AlignAnchorSVG::getDefaultAlignAnchor<AlignBase::Axis::HORZ>() {
312 return defaultAnchorHorz;
313}
314
315template <>
316inline
317AlignAnchorSVG::anchor_t & AlignAnchorSVG::getDefaultAlignAnchor<AlignBase::Axis::VERT>() {
318 return defaultAnchorVert;
319}
320
321
322
323} // image::
324
325
326
327} // drain::
328
329//DRAIN_ENUM_DICT(drain::image::AnchorElem::Anchor);
330//DRAIN_ENUM_OSTREAM(drain::image::AnchorElem::Anchor);
331
332#endif // DRAIN_ALIGN_SVG_H_
333
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:326
Logger & suspicious(const TT &... args)
A weak warning about something going possibly wrong.
Definition Log.h:510
Definition Data.h:1473
A container for a static dictionary of enumeration values.
Definition Enum.h:51
static const std::string & getKey(const std::string &s, bool lenient=true)
Convenience for object.set(...) like commands.
Definition Enum.h:144
Adapter designed for NodeSVG.
Definition AlignAnchorSVG.h:159
const anchor_t & getMyAlignAnchor() const
Store anchor object/symbol for aligning this object.
virtual void updateAlign()=0
Redefined in NodeSVG.
Definition AlignAnchorSVG.h:77
bool isSet() const
If not set, use default.
Definition AlignAnchorSVG.h:116