TreeUtilsSVG.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2023 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 /*
32  * TreeSVG.h
33  *
34  * Created on: Jun 24, 2012
35  * Author: mpeura
36  */
37 
38 #ifndef DRAIN_TREE_UTILS_SVG
39 #define DRAIN_TREE_UTILS_SVG
40 
41 //
42 
43 #include <drain/util/Frame.h>
44 #include <drain/util/EnumFlags.h>
45 #include <drain/util/Rectangle.h>
46 #include "AlignSVG.h"
47 #include "TreeSVG.h"
48 
49 namespace drain {
50 
51 namespace image {
52 /*
53 struct PanelConfSVG {
54 
56  bool absolutePaths = true;
57 
58  // Currently, applications are recommended to handle "false" and "none". Or "auto"?
59  // std::string title;
60  // FontSizes fontSize;
61 
62  inline // maxPerGroup(10), layout(Alignment::HORZ, LayoutSVG::INCR), legend(LEFT, EMBED),
63  PanelConfSVG() : absolutePaths(true){
64  }
65 
66 };
67 */
68 
70 
75 //template <>
76 // const drain::EnumDict<AlignSVG_FOO>::dict_t drain::EnumDict<AlignSVG_FOO>::dict;
77 
78 struct TreeUtilsSVG {
79 
80 public:
81 
82  // static PanelConfSVG defaultConf; // Consider separating Rack-specific properties.
83 
84  /*
85  static
86  const std::set<XML::intval_t> abstractTags;
87 
88  static
89  bool isAbstract(XML::intval_t tag);
90  */
91 
93 
96  static
97  bool computeBoundingBox(const TreeSVG & group, drain::Box<svg::coord_t> & box);
98 
100 
103  static
104  void finalizeBoundingBox(TreeSVG & svg);
105 
106 
108 
113  // static
114  // void getBoundingFrame(const TreeSVG & group, drain::Frame2D<int> & frame, AlignBase::Axis orientation=AlignBase::Axis::HORZ);
115 
116  static
117  void setRelativePaths(drain::image::TreeSVG & object, const drain::FilePath & filepath);
118 
119 
120  // NEW ---------------------
121  static
122  void superAlign(TreeSVG & node, AlignBase::Axis orientation = AlignBase::Axis::HORZ, LayoutSVG::Direction direction = LayoutSVG::Direction::INCR);
123  //const Point2D<svg::coord_t> & offset = {0,0}); // replaces alignSequence
124 
125 
126 
127  // static
128  // void realignElem(TreeSVG & elem, const Box<svg::coord_t> & anchorBox);
129 
131  // static
132  // void realignObject(const Box<svg::coord_t> & anchorBox, TreeSVG & obj);
133 
134  static
135  void realignObject(const Box<svg::coord_t> & anchorBoxHorz, const Box<svg::coord_t> & anchorBoxVert, TreeSVG & obj);
136 
137  // static
138  // void realignObject(AlignBase::Axis axis, svg::coord_t pos, svg::coord_t width, TreeSVG & obj, svg::coord_t & newPos); // , Point2D<svg::coord_t> & newLocation);
139 
140  // static
141  // void realignObjectHorz(TreeSVG & obj, const Box<svg::coord_t> & anchorBoxHorz, svg::coord_t & coord);
142 
143  static
144  void realignObjectHorz(TreeSVG & obj, const Box<svg::coord_t> & anchorBoxHorz);
145 
146  // static
147  //void realignObjectVert(TreeSVG & obj, const Box<svg::coord_t> & anchorBoxVert, svg::coord_t & coord);
148 
149  static
150  void realignObjectVert(TreeSVG & obj, const Box<svg::coord_t> & anchorBoxVert);
151 
152 
154  static
155  void translateAll(TreeSVG & group, const Point2D<svg::coord_t> &offset);
156 
157 };
158 
159 
160 class TranslatorSVG : public drain::TreeVisitor<TreeSVG> {
161 
162 public:
163 
164  const Point2D<svg::coord_t> offset;
165 
166  template <class T>
167  inline
168  TranslatorSVG(const Point2D<T> & offset) : offset(offset){};
169 
170  template <typename T>
171  inline
172  TranslatorSVG(T dx, T dy) : offset(dx, dy){};
173 
174  int visitPrefix(TreeSVG & tree, const TreeSVG::path_t & path) override;
175 
176  // int visitPostfix(TreeSVG & tree, const TreeSVG::path_t & path) override;
177 };
178 
179 
180 class BBoxRetrieverSVG : public drain::TreeVisitor<TreeSVG> {
181 
182 public:
183 
184  BBoxSVG box;
185  // Box<svg::coord_t> box;
186 
187  int visitPrefix(TreeSVG & tree, const TreeSVG::path_t & path) override;
188 
189  int visitPostfix(TreeSVG & tree, const TreeSVG::path_t & path) override;
190 };
191 
192 
193 class RelativePathSetterSVG : public drain::TreeVisitor<TreeSVG> {
194 
195 public:
196 
197  const std::string dir;
198 
199  inline
200  RelativePathSetterSVG(const drain::FilePath & filepath) :
201  dir(filepath.dir.empty() ? "" : filepath.dir.str()+'/') {
202  }
203 
204  int visitPrefix(TreeSVG & tree, const TreeSVG::path_t & path) override;
205 
206 
207 };
208 
209 
210 
211 
230 } // image::
231 
232 /*
233 template <>
234 const drain::EnumDict<image::LayoutSVG::Axis>::dict_t drain::EnumDict<image::LayoutSVG::Axis>::dict;
235 
236 template <>
237 const drain::EnumDict<image::LayoutSVG::Direction>::dict_t drain::EnumDict<image::LayoutSVG::Direction>::dict;
238 */
239 
240 } // drain::
241 
242 
243 
244 
245 #endif // TREESVG_H_
246 
Extracts and stores directory path, base filename and extension.
Definition: FilePath.h:58
Default implementation of a tree visitor (concept) compatible TreeUtils::traverser()
Definition: TreeUtils.h:270
Definition: TreeUtilsSVG.h:180
Definition: TreeSVG.h:87
Definition: TreeUtilsSVG.h:193
Definition: TreeUtilsSVG.h:160
int visitPrefix(TreeSVG &tree, const TreeSVG::path_t &path) override
Definition: TreeUtilsSVG.cpp:498
Definition: DataSelector.cpp:1277
SVG classes marking requests of relative horizontal and vertical alignment.
Definition: TreeUtilsSVG.h:78
static void superAlign(TreeSVG &node, AlignBase::Axis orientation=AlignBase::Axis::HORZ, LayoutSVG::Direction direction=LayoutSVG::Direction::INCR)
Definition: TreeUtilsSVG.cpp:293
static void finalizeBoundingBox(TreeSVG &svg)
Compute bounding box and set the top-level SVG width, height and viewBox properties.
Definition: TreeUtilsSVG.cpp:58
static bool computeBoundingBox(const TreeSVG &group, drain::Box< svg::coord_t > &box)
Compute bounding box of the whole structure.
Definition: TreeUtilsSVG.cpp:514
static void realignObjectVert(TreeSVG &obj, const Box< svg::coord_t > &anchorBoxVert)
Definition: TreeUtilsSVG.cpp:176
static void translateAll(TreeSVG &group, const Point2D< svg::coord_t > &offset)
Recursively move elements with (x, y).
Definition: TreeUtilsSVG.cpp:463
static void setRelativePaths(drain::image::TreeSVG &object, const drain::FilePath &filepath)
Computes the width and height for a bounding box IMAGE and RECT elements.
static void realignObject(const Box< svg::coord_t > &anchorBoxHorz, const Box< svg::coord_t > &anchorBoxVert, TreeSVG &obj)
Align object respect to an anchor frame.
Definition: TreeSVG.h:55