Loading...
Searching...
No Matches
TextSVG.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_TEXT_SVG
39#define DRAIN_TEXT_SVG
40
41#include <string>
42
43// #include "drain/Enum.h"
44// #include "drain/util/XML.h"
45// #include "drain/util/Frame.h"
46// #include "Alignment.h"
47#include "TreeSVG.h"
48// #include "AlignSVG.h"
49
50
51namespace drain {
52
53namespace image {
54
55
56class TextBox {
57
58public:
59
60 static
61 const drain::ClassXML TEXTBOX;
62
63 //TextBox(ImagePanel &imagePanel, const drain::Frame2D<int> & geom = {0,0});
67 TextBox(TreeSVG & group); // , const drain::Frame2D<int> & geom = {0,0});
68
69 TextBox(TreeSVG & group, const TreeSVG::path_elem_t & identifier); // , const drain::Frame2D<int> & geom = {0,0});
70
74 TextBox(TextBox &textBox);
75
76 void setLineHeight(const svg::coord_t w);
77
78 void setFontSize(const svg::coord_t w);
79
80 TreeSVG & addLineAligned(const std::string & s, char edge='|') const ;
81
83 template <char SEP=0, class ...TT>
84 TreeSVG & addLine(const TT &... args) const {
85 return addLineAligned(drain::StringBuilder<SEP>(args...).str(), '|');
86 }
87
88 void addLines(const std::string & lines, char separator='\n', char edge='|') const;
89
90
91 // Unused
92 void setLocation(const drain::Point2D<int> & point);
93
94 TreeSVG & getBackground() const;
95
96 /*
97 // Unused
98 void setGeometry(const drain::Frame2D<int> & geom);
99
100 // Unused
101 void setWidth(const svg::coord_t w);
102
103 // Unused
104 void setHeight(const svg::coord_t h);
105
106
107
108 */
109
110 // Top-level element.
111 TreeSVG & topGroup;
112
113 // Main group, immediately under textGroup.
114 TreeSVG & adapterGroup;
115 // AnchorElem sharedHorzAnchor;
116
117 // ??
118 // CompleteAlignment<const AlignBase::Axis, AlignBase::Axis::HORZ> alignHorz; // (AlignSVG::CENTER, MutualAlign::OUTSIDE)
119 // CompleteAlignment<const AlignBase::Axis, AlignBase::Axis::VERT> alignVert;
120
121 // static
122 // const std::string FLIP_FUNCTION_JS;
123
124
125protected:
126
127 // svg::coord_t lineHeight = 0;
128
129 void init();
130
132 template <class ...TT>
133 TreeSVG & addLineOLD(const TT &... args);
134
135 static
136 const std::string LINE_HEIGHT;
137
138};
139
140
141
142/*
143template <char SEP,class ... TT>
144void TextBox::addLine(const TT &... args) const {
145
146
147 drain::Logger mout(__FILE__, __FUNCTION__);
148
149 //TreeSVG & textLine = textGroup.addChild()(svg::TEXT);
150 TreeSVG & group = adapterGroup;
151
152 float fontSize = group->getStyle("font-size", 0.0f);
153 if (fontSize == 0.0f){
154 fontSize = group->get(LINE_HEIGHT, 20);
155 group->setStyle("font-size", fontSize);
156 }
157
158 const int lineHeight = group->get(LINE_HEIGHT, (4*fontSize)/3);
159
160
161 const std::string line = drain::StringBuilder<SEP>(args...).str();
162
163 if (line.empty()){
164 // TODO: consider allowing empty line
165 return; // textLine;
166 }
167
168
169 CompleteHorzAlign alignHorz(AlignSVG::CENTER, MutualAlign::OUTSIDE); // , MutualAlign::INSIDE);
170 CompleteVertAlign alignVert(AlignSVG::BOTTOM, MutualAlign::OUTSIDE);
171 // CompleteAlignment<const AlignBase::Axis, AlignBase::Axis::HORZ> alignHorz(AlignSVG::CENTER, MutualAlign::OUTSIDE); // , MutualAlign::INSIDE);
172 // CompleteAlignment<const AlignBase::Axis, AlignBase::Axis::VERT> alignVert(AlignSVG::BOTTOM, MutualAlign::OUTSIDE);
173
174 std::list<std::string> parts;
175 drain::StringTools::split(line, parts, '|');
176 switch (parts.size()) {
177 case 1:
178 alignHorz.set(AlignSVG::CENTER);
179 break;
180 case 2:
181 alignHorz.set(AlignSVG::LEFT);
182 break;
183 default:
184 alignHorz.set(AlignSVG::LEFT);
185 mout.warn("Text contained several alignment markers '|'");
186 break;
187 }
188
189 for (const std::string & part: parts){ // Maximally two.
190
191 if (!part.empty()){
192
193 TreeSVG & text = group.addChild()(svg::TEXT);
194 // text->addClass(TEXTBOX);
195
196 //text->setFontSize(fontSize, (15*fontSize)/10);
197 text->setHeight(lineHeight);
198 text->setMargin(lineHeight/4);
199 text->setText(part);
200
201 //text->setMyAlignAnchor<AlignBase::Axis::HORZ>(RackSVG::ElemClass::BACKGROUND);
202
203 text->setAlign(alignHorz);
204
205 // text->setMyAlignAnchor<AlignBase::Axis::VERT>(AnchorElem::PREVIOUS); // For the first element, this is "labelAnchor"
206 text->setAlign(alignVert);
207
208 // text[svg::TITLE](svg::TITLE)->setText(part);
209 // The first VERT align (stored in alignVert) has now been used, so next element is on the same row.
210 alignVert.set(AlignSVG::TOP, MutualAlign::INSIDE);
211 }
212
213 // If there is another part, align it RIGHT
214 alignHorz.set(AlignSVG::RIGHT);
215 }
216
217}
218*/
219
220
221} // image::
222
223} // drain::
224
225/*
226template<class ... TT>
227TreeSVG & TextBox::addLineOLD(const TT &... args) {
228
229
230 const bool FIRST = textGroup.empty();
231 TreeSVG & textLine = textGroup.addChild()(svg::TEXT);
232 textLine->setTextSafe(args...);
233 textLine->setFontSize(13,17);
234
235 // HORZ alignment, always:
236 // textLine->setMyAlignAnchor<AlignBase::HORZ>(RackSVG::ElemClass::BACKGROUND);
237 textLine->setAlign(alignHorz, MutualAlign::Topol::INSIDE);
238
239 if (FIRST){
240 // textLine->setMyAlignAnchor<AlignBase::VERT>(RackSVG::ElemClass::BACKGROUND);
241 textLine->setAlign(AlignSVG::TOP, MutualAlign::Topol::INSIDE);
242 }
243 else {
244 // textLine->setMyAlignAnchor<AlignBase::VERT>(AnchorElem::PREVIOUS); // default, remove..
245 textLine->setAlign(AlignSVG::BOTTOM, MutualAlign::Topol::OUTSIDE);
246 }
247
248
249 return textLine;
250}
251*/
252
253/*
254 if (sharedHorzAnchor.isSet()) {
255 textLine->setMyAlignAnchor<AlignBase::Axis::HORZ>(sharedHorzAnchor);
256 } else if (!FIRST) {
257 textLine->setMyAlignAnchor<AlignBase::Axis::HORZ>(AnchorElem::PREVIOUS);
258 }
259 */
260
261 /* SAVE, for further flips...
262 *
263 textLine->setAlign(alignHorz);
264 // VERT alignment
265 if (FIRST) {
266 // textLine->setMyAlignAnchor<AlignBase::Axis::VERT>(sharedHorzAnchor);
267 textLine->setAlign(alignVert);
268 } else {
269 textLine->setMyAlignAnchor<AlignBase::Axis::VERT>(AnchorElem::PREVIOUS);
270 AlignBase::Pos pos = alignVert.pos;
271 if (alignVert.topol == MutualAlign::Topol::INSIDE) {
272 // In this case, invert the direction, because:
273 // - if first elem was at the top, continue down.
274 // - if first elem was at the bottom, continue up.
275 pos = AlignBase::flip(pos);
276 }
277 textLine->setAlign(AlignBase::Axis::VERT, pos,
278 MutualAlign::Topol::OUTSIDE);
279 }
280 */
281
282
283#endif // DRAIN_TEXT_SVG
284
A wrapper marking string an CSS effect.
Definition ClassXML.h:57
Definition StringBuilder.h:58
Definition TextSVG.h:56
TreeSVG & addLine(const TT &... args) const
Adds a TEXT elem, containing concatenated arguments.
Definition TextSVG.h:84
TreeSVG & addLineOLD(const TT &... args)
Adds a TEXT elem, containing concatenated arguments.
Definition Data.h:1473
Definition Point.h:48