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
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 COLLECTIVE_CURRENT, // 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 inline
99 const std::string & str() const {
100 return *this;
101 }
102
103 inline
104 std::string & str() {
105 return *this;
106 }
107
108 void set(const std::string & s);
109
110 void set(const AnchorElem::Anchor & anchor);
111
113 inline
114 bool isSet() const {
115 return !empty();
116 }
117
118 // Use the bounding box of the compound, "accumulated" object as anchor.
119 inline
120 bool isCollective() const {
121 return (drain::Enum<AnchorElem::Anchor>::getValue(*this) == Anchor::COLLECTIVE_CURRENT);
122 }
123
124 // Use the bounding box of the latest object as anchor.
125 bool isPrevious() const {
126 return (drain::Enum<AnchorElem::Anchor>::getValue(*this) == Anchor::PREVIOUS);
127 }
128
129 // Explicitly states that an object uses no anchor, hence the position is absolute (not aligned)
130 bool isNone() const {
131 return (drain::Enum<AnchorElem::Anchor>::getValue(*this) == Anchor::NONE);
132 }
133
134 // Use the bounding box of a named object as anchor.
135 inline
136 bool isSpecific() const {
137 return isSet() && (!isCollective()) && (!isPrevious()) && (!isNone());
138 }
139
140
141};
142
143} // drain::
144
145// }
146//
147// namespace drain {
148
149DRAIN_ENUM_DICT(image::AnchorElem::Anchor);
150DRAIN_ENUM_OSTREAM(image::AnchorElem::Anchor);
151
152namespace image {
153
155struct AlignAnchorSVG { // : public AlignSVG {
156
157
158public:
159
160 typedef AnchorElem anchor_t; // needed?
161
162 template <class T>
163 inline
164 void setMyAlignAnchor(const T & value){
165 adjustAnchor(myAnchorHorz, value);
166 myAnchorVert = myAnchorHorz;
167 updateAlign();
168 }
169
170 template <AlignBase::Axis AX, class T>
171 inline
172 void setMyAlignAnchor(const T & value){
173 adjustAnchor(getMyAlignAnchor<AX>(), value);
174 updateAlign();
175 }
176
177 template <class T>
178 inline
179 void setDefaultAlignAnchor(const T & value){
180 adjustAnchor(defaultAnchorHorz, value);
181 defaultAnchorVert = defaultAnchorHorz;
182 updateAlign();
183 }
184
185 template <AlignBase::Axis AX, class T>
186 inline
187 void setDefaultAlignAnchor(const T & value){
188 AnchorElem & defaultAnchor = getDefaultAlignAnchor<AX>();
189 adjustAnchor(defaultAnchor, value);
190 if (defaultAnchor.isPrevious()){
191 Logger(__FILE__, __FUNCTION__, __LINE__).suspicious("Removing explicit ", AnchorElem::Anchor::PREVIOUS, " as non-informative for group default");
192 // getDefaultAlignAnchor<AX>() = AnchorElem::Anchor::DEFAULT;
193 adjustAnchor(defaultAnchor, AnchorElem::Anchor::DEFAULT);
194 }
195 updateAlign();
196 }
197
198
200 template <AlignBase::Axis AX>
201 const anchor_t & getMyAlignAnchor() const;
202
204 template <AlignBase::Axis AX>
205 const anchor_t & getDefaultAlignAnchor() const;
206
208 template <AlignBase::Axis AX>
210
212 template <AlignBase::Axis AX>
213 anchor_t & getDefaultAlignAnchor();
214
215
216
217 inline virtual
218 ~AlignAnchorSVG(){};
219
220 void swapAnchors(AlignAnchorSVG & anchors);
221
222protected:
223
224 // "string detectors"
225
226 static inline
227 void adjustAnchor(AnchorElem & anchor, const std::string & value){
228 anchor.assign(value);
229 }
230
231 static inline
232 void adjustAnchor(AnchorElem & anchor, const char * value){
233 anchor.assign(value);
234 }
235
236 static inline
237 void adjustAnchor(AnchorElem & anchor, const AnchorElem & elem){
238 anchor.assign(elem);
239 }
240
241 template <class T>
242 static inline
243 void adjustAnchor(AnchorElem & anchor, const T & value){
244 anchor.assign(Enum<T>::getKey(value));
245 }
246
248 virtual
249 void updateAlign() = 0;
250
251 // essentially std::string's
252 anchor_t myAnchorHorz;
253 anchor_t myAnchorVert;
254
255 anchor_t defaultAnchorHorz;
256 anchor_t defaultAnchorVert;
257
258
259};
260
261
262template <>
263inline
264const AlignAnchorSVG::anchor_t & AlignAnchorSVG::getMyAlignAnchor<AlignBase::Axis::HORZ>() const {
265 return myAnchorHorz;
266};
267
268template <>
269inline
270const AlignAnchorSVG::anchor_t & AlignAnchorSVG::getMyAlignAnchor<AlignBase::Axis::VERT>() const {
271 return myAnchorVert;
272};
273
275template <>
276inline
277const AlignAnchorSVG::anchor_t & AlignAnchorSVG::getDefaultAlignAnchor<AlignBase::Axis::HORZ>() const {
278 return defaultAnchorHorz;
279};
280
281template <>
282inline
283const AlignAnchorSVG::anchor_t & AlignAnchorSVG::getDefaultAlignAnchor<AlignBase::Axis::VERT>() const {
284 return defaultAnchorVert;
285};
286
287template <>
288inline
289AlignAnchorSVG::anchor_t & AlignAnchorSVG::getMyAlignAnchor<AlignBase::Axis::HORZ>() {
290 return myAnchorHorz;
291};
292
293template <>
294inline
295AlignAnchorSVG::anchor_t & AlignAnchorSVG::getMyAlignAnchor<AlignBase::Axis::VERT>() {
296 return myAnchorVert;
297};
298
300template <>
301inline
302AlignAnchorSVG::anchor_t & AlignAnchorSVG::getDefaultAlignAnchor<AlignBase::Axis::HORZ>() {
303 return defaultAnchorHorz;
304};
305
306template <>
307inline
308AlignAnchorSVG::anchor_t & AlignAnchorSVG::getDefaultAlignAnchor<AlignBase::Axis::VERT>() {
309 return defaultAnchorVert;
310};
311
312
313
314} // image::
315
316
317
318} // drain::
319
320//DRAIN_ENUM_DICT(drain::image::AnchorElem::Anchor);
321//DRAIN_ENUM_OSTREAM(drain::image::AnchorElem::Anchor);
322
323#endif // DRAIN_ALIGN_SVG_H_
324
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Logger & suspicious(const TT &... args)
A weak warning about something going possibly wrong.
Definition Log.h:501
Definition DataSelector.cpp:1277
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:155
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:114