SVGTools.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \ / O peration |
5  \ / A nd | www.openfoam.com
6  \/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2024 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Namespace
27  Foam::SVG
28 
29 Description
30  Collection of tools to generate SVG strings
31 
32 SourceFiles
33  SVGTools.H
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #ifndef Foam_SVGTools_H
38 #define Foam_SVGTools_H
39 
40 #include "Ostream.H"
41 #include "OStringStream.H"
42 #include "List.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 /*---------------------------------------------------------------------------*\
50  Namespace SVG Declaration
51 \*---------------------------------------------------------------------------*/
52 
53 namespace SVG
54 {
55  typedef std::pair<const char*, string> entryType;
56 
57  struct element;
58  Ostream& operator<<(Ostream& os, const element& e);
59 
60  // Base SVG element
61  struct element
62  {
63  const word key_;
66 
67  element
68  (
69  const word& key,
70  const std::initializer_list<entryType>& styles = {},
71  const std::initializer_list<entryType>& elems = {}
72  )
73  :
74  key_(key),
75  styles_(styles),
76  elems_(elems)
77  {}
78 
79  template<class Type>
80  void addAttr(const char* key, const Type& value)
81  {
82  OStringStream oss;
83  oss << value;
84  elems_.push_back(entryType(key, oss.str().c_str()));
85  }
86 
87  void addAttrStr(const char* key, const string& str)
88  {
89  elems_.push_back(entryType(key, str.c_str()));
90  }
91 
92  friend Ostream& operator<<(Ostream& os, const element& ele)
93  {
94  os << "<" << ele.key_;
95 
96  for (const auto& e : ele.elems_)
97  {
98  os << " " << e.first << "=" << e.second;
99  }
100 
101  os << " style=\"";
102  for (const auto& s : ele.styles_)
103  {
104  os << s.first << ":" << s.second.c_str() << ";";
105  }
106 
107  os << "\">";
109  return os;
110  }
111 
112  const word end = "</" + key_ + ">";
113  };
114 
115 
116  struct text;
117  Ostream& operator<<(Ostream& os, const text& t);
118 
119  // Text
120  struct text
121  :
122  element
123  {
124  const string text_;
125 
126  text
127  (
128  const string text,
129  const label left,
130  const label top,
131  const std::initializer_list<entryType>& styles = {},
132  const word anchor = "middle",
133  const std::initializer_list<entryType>& elems = {}
134  )
135  :
136  element("text", styles, elems),
137  text_(text)
138  {
139  elems_.push_back(entryType("x", Foam::name(left)));
140  elems_.push_back(entryType("y", Foam::name(top)));
141  elems_.push_back(entryType("text-anchor", anchor));
143  (
144  entryType("font-family", "Arial, Helvetica, sans-serif")
145  );
146  }
147 
148 
149  friend Ostream& operator<<(Ostream& os, const text& t)
150  {
151  // element::operator<<(os, t);
152  os << static_cast<const element&>(t);
153 
154  os << t.text_.c_str();
155 
156  os << t.end;
157 
158  return os;
159  }
160  };
161 
162 
163  struct line;
164  Ostream& operator<<(Ostream& os, const line& l);
165 
166  // Line
167  struct line
168  :
169  element
170  {
171  line
172  (
173  const label x1,
174  const label y1,
175  const label x2,
176  const label y2,
177  const std::initializer_list<entryType>& styles = {},
178  const std::initializer_list<entryType>& elems = {}
179  )
180  :
181  element("line", styles, elems)
182  {
183  elems_.push_back(entryType("x1", Foam::name(x1)));
185  elems_.push_back(entryType("x2", Foam::name(x2)));
187  }
188 
189 
190  friend Ostream& operator<<(Ostream& os, const line& l)
191  {
192  // element::operator<<(os, l);
193  os << static_cast<const element&>(l);
194  os << l.end;
195 
196  return os;
197  }
198  };
199 
200  struct header;
201  Ostream& operator<<(Ostream& os, const header& h);
203  // Header
204  struct header
205  {
206  label width_;
207  label height_;
208 
209  header(const label width, const label height)
210  :
211  width_(width),
212  height_(height)
213  {}
214 
215  friend Ostream& operator<<(Ostream& os, const header& h)
216  {
217  os << "<svg viewBox=\"0 0 " << h.width_ << ' ' << h.height_ << "\""
218  << " xmlns=\"http://www.w3.org/2000/svg\""
219  << " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
220  << " xmlns:bx=\"https://www.boxy-svg.com/bx\">";
221 
222  return os;
223  }
224  };
225 
226  // Close SVG element
227  const char* end = "</svg>";
228 
229 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
230 
231 } // End namespace SVG
232 } // End namespace Foam
233 
234 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
235 
236 #endif
237 
238 // ************************************************************************* //
A line primitive.
Definition: line.H:52
const word end
Definition: SVGTools.H:108
friend Ostream & operator<<(Ostream &os, const text &t)
Definition: SVGTools.H:145
DynamicList< entryType > elems_
Definition: SVGTools.H:61
line(const label x1, const label y1, const label x2, const label y2, const std::initializer_list< entryType > &styles={}, const std::initializer_list< entryType > &elems={})
Definition: SVGTools.H:168
element(const word &key, const std::initializer_list< entryType > &styles={}, const std::initializer_list< entryType > &elems={})
Definition: SVGTools.H:64
PointRef end() const noexcept
The end (second) point.
Definition: line.H:252
friend Ostream & operator<<(Ostream &os, const header &h)
Definition: SVGTools.H:211
void addAttr(const char *key, const Type &value)
Definition: SVGTools.H:76
Foam::string str() const
Get the string. As Foam::string instead of std::string (may change in future)
Definition: StringStream.H:256
text(const string text, const label left, const label top, const std::initializer_list< entryType > &styles={}, const word anchor="middle", const std::initializer_list< entryType > &elems={})
Definition: SVGTools.H:123
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Ostream & operator<<(Ostream &os, const element &e)
Definition: SVGTools.H:88
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
auto key(const Type &t) -> std::enable_if_t< std::is_enum_v< Type >, std::underlying_type_t< Type > >
Definition: foamGltfBase.H:103
dimensionedScalar y1(const dimensionedScalar &ds)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
std::pair< const char *, string > entryType
Definition: SVGTools.H:51
const string text_
Definition: SVGTools.H:120
OBJstream os(runTime.globalPath()/outputName)
header(const label width, const label height)
Definition: SVGTools.H:205
friend Ostream & operator<<(Ostream &os, const element &ele)
Definition: SVGTools.H:88
void push_back(const T &val)
Copy append an element to the end of this list.
Definition: DynamicListI.H:558
void addAttrStr(const char *key, const string &str)
Definition: SVGTools.H:83
const dimensionedScalar h
Planck constant.
const char * end
Definition: SVGTools.H:223
const word key_
Definition: SVGTools.H:59
DynamicList< entryType > styles_
Definition: SVGTools.H:60
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:208
Namespace for OpenFOAM.
friend Ostream & operator<<(Ostream &os, const line &l)
Definition: SVGTools.H:186