Debian Codegraph @

libs/opengl/src/gltext.cpp

raw
1 /* +------------------------------------------------------------------------+
2 | Mobile Robot Programming Toolkit (MRPT) |
3 | https://www.mrpt.org/ |
4 | |
5 | Copyright (c) 2005-2026, Individual contributors, see AUTHORS file |
6 | See: https://www.mrpt.org/Authors - All rights reserved. |
7 | Released under BSD License. See: https://www.mrpt.org/License |
8 +------------------------------------------------------------------------+ */
9
10 #include "opengl-precomp.h" // Precompiled header
11 //
12
13 // Stripped out version of libCVD gltext.cpp file, ported to OpenGL>=3
14 // 2-BSD License.
15
16 #include <mrpt/math/TPoint2D.h>
17
18 #include <algorithm>
19 #include <cassert>
20 #include <cmath>
21 #include <map>
22
23 #include "gltext.h"
24
25 using namespace std;
26
27 namespace mrpt::opengl::internal
28 {
29 struct Point
30 {
31 float x, y;
32 };
33
34 struct Font
35 {
36 typedef unsigned short Index;
37
38 struct Char
39 {
40 Index vertexOffset;
41 Index triangleOffset;
42 Index outlineOffset;
43 int numTriangles; // was: GLsizei
44 int numOutlines;
45 float advance;
46 };
47
48 Point* vertices;
49 Index* triangles;
50 Index* outlines;
51 Char* characters;
52 string glyphs;
53
54 const Char* findChar(const char c) const
55 {
56 size_t ind = glyphs.find(c);
57 if (ind == string::npos) return nullptr;
58 return characters + ind;
59 }
60
61 float getAdvance(const char c) const
62 {
63 const Char* ch = findChar(c);
64 if (!ch) return 0;
65 return ch->advance;
66 }
67
68 void fill(
69 const char c,
70 std::vector<mrpt::opengl::TTriangle>& tris,
71 std::vector<mrpt::math::TPoint3Df>& lines,
72 const mrpt::math::TPoint2Df& cursor) const
73 {
74 const Char* ch = findChar(c);
75 if (!ch || !ch->numTriangles) return;
76 // triangles
77 const Point* vs = vertices + ch->vertexOffset;
78 for (int i = 0; i < ch->numTriangles / 3; i++)
79 {
80 const auto idx0 = triangles[ch->triangleOffset + i * 3 + 0];
81 const auto idx1 = triangles[ch->triangleOffset + i * 3 + 1];
82 const auto idx2 = triangles[ch->triangleOffset + i * 3 + 2];
83
84 using P3f = mrpt::math::TPoint3Df;
85
86 tris.emplace_back(
87 P3f(cursor.x + vs[idx0].x, cursor.y + vs[idx0].y, .0f),
88 P3f(cursor.x + vs[idx1].x, cursor.y + vs[idx1].y, .0f),
89 P3f(cursor.x + vs[idx2].x, cursor.y + vs[idx2].y, .0f));
90 }
91 }
92
93 void outline(
94 const char c,
95 std::vector<mrpt::opengl::TTriangle>& tris,
96 std::vector<mrpt::math::TPoint3Df>& lines,
97 const mrpt::math::TPoint2Df& cursor) const
98 {
99 const Char* ch = findChar(c);
100 if (!ch || !ch->numOutlines) return;
101 // lines
102 const Point* vs = vertices + ch->vertexOffset;
103 for (int i = 0; i < ch->numOutlines / 2; i++)
104 {
105 const auto idx0 = outlines[ch->outlineOffset + i * 2 + 0];
106 const auto idx1 = outlines[ch->outlineOffset + i * 2 + 1];
107
108 lines.emplace_back(cursor.x + vs[idx0].x, cursor.y + vs[idx0].y, .0f);
109 lines.emplace_back(cursor.x + vs[idx1].x, cursor.y + vs[idx1].y, .0f);
110 }
111 }
112
113 void draw(
114 const char c,
115 std::vector<mrpt::opengl::TTriangle>& tris,
116 std::vector<mrpt::math::TPoint3Df>& lines,
117 const mrpt::math::TPoint2Df& cursor) const
118 {
119 outline(c, tris, lines, cursor);
120 fill(c, tris, lines, cursor);
121 }
122 };
123
124 // the fonts defined in these headers are derived from Bitstream Vera fonts. See
125 // http://www.gnome.org/fonts/ for license and details
126 #include "glfont_mono.h"
127 #include "glfont_sans.h"
128 #include "glfont_serif.h"
129
130 struct FontData
131 {
132 typedef map<string, Font*> FontMap;
133
134 FontData()
135 {
136 fonts["sans"] = &sans_font;
137 fonts["mono"] = &mono_font;
138 fonts["serif"] = &serif_font;
139 glSetFont("sans");
140 }
141 inline Font* currentFont() { return fonts[currentFontName]; }
142
143 string currentFontName;
144 FontMap fonts;
145 };
146
147 static struct FontData data;
148
149 void glSetFont(const std::string& fontname)
150 {
151 if (data.fonts.count(fontname) > 0) data.currentFontName = fontname;
152 }
153
154 const std::string& glGetFont() { return data.currentFontName; }
155
156 std::pair<double, double> glDrawText(
157 const std::string& text,
158 std::vector<mrpt::opengl::TTriangle>& tris,
159 std::vector<mrpt::math::TPoint3Df>& render_lines,
160 TEXT_STYLE style,
161 double spacing,
162 double kerning)
163 {
164 // Was: glPushMatrix();
165 mrpt::math::TPoint2Df cursor = {0, 0};
166
167 // figure out which operation to do on the Char (yes, this is a pointer to
168 // member function :)
169 void (Font::*operation)(
170 const char c, std::vector<mrpt::opengl::TTriangle>& tris,
171 std::vector<mrpt::math::TPoint3Df>& lines, const mrpt::math::TPoint2Df& cursor) const;
172 switch (style)
173 {
174 case FILL:
175 operation = &Font::fill;
176 break;
177 case OUTLINE:
178 operation = &Font::outline;
179 break;
180 case NICE:
181 // operation = &Font::draw; (See comments in definition of "NICE")
182 operation = &Font::fill;
183 break;
184
185 default:
186 THROW_EXCEPTION("Invalid style value");
187 };
188
189 int lines = 0;
190 double max_total = 0;
191 double total = 0;
192 const Font* font = data.currentFont();
193 const Font::Char* space = font->findChar(' ');
194 const double tab_width = 8 * ((space) ? (space->advance) : 1);
195 for (size_t i = 0; i < text.length(); ++i)
196 {
197 char c = text[i];
198 if (c == '\n')
199 {
200 cursor.x -= total;
201 cursor.y -= spacing;
202
203 max_total = std::max(max_total, total);
204 total = 0;
205 ++lines;
206 continue;
207 }
208 if (c == '\t')
209 {
210 const float advance = tab_width - std::fmod(total, tab_width);
211 total += advance;
212 cursor.x += advance;
213 continue;
214 }
215 const Font::Char* ch = font->findChar(c);
216 if (!ch)
217 {
218 c = toupper(c);
219 ch = font->findChar(c);
220 if (!ch)
221 {
222 c = '?';
223 ch = font->findChar(c);
224 }
225 }
226 if (!ch) continue;
227 (font->*operation)(c, tris, render_lines, cursor);
228
229 double w = ch->advance + kerning;
230 cursor.x += w;
231 total += w;
232 }
233
234 max_total = std::max(total, max_total);
235 return std::make_pair(max_total, (lines + 1) * spacing);
236 }
237
238 std::pair<double, double> glGetExtends(const std::string& text, double spacing, double kerning)
239 {
240 int lines = 0;
241 double max_total = 0;
242 double total = 0;
243 const Font* font = data.currentFont();
244 for (size_t i = 0; i < text.length(); ++i)
245 {
246 char c = text[i];
247 if (c == '\n')
248 {
249 max_total = std::max(max_total, total);
250 total = 0;
251 ++lines;
252 continue;
253 }
254 const Font::Char* ch = font->findChar(c);
255 if (!ch)
256 {
257 c = toupper(c);
258 ch = font->findChar(c);
259 if (!ch)
260 {
261 c = '?';
262 ch = font->findChar(c);
263 }
264 }
265 if (!ch) continue;
266 total += ch->advance + kerning;
267 }
268 max_total = std::max(total, max_total);
269 return std::make_pair(max_total, (lines + 1) * spacing);
270 }
271
272 void glDrawTextTransformed(
273 const std::string& text,
274 std::vector<mrpt::opengl::TTriangle>& tris,
275 std::vector<mrpt::math::TPoint3Df>& lines,
276 std::vector<mrpt::img::TColor>& line_colors,
277 const mrpt::poses::CPose3D& text_pose,
278 float text_scale,
279 const mrpt::img::TColor& text_color,
280 TEXT_STYLE style,
281 double spacing,
282 double kerning)
283 {
284 // Raw text primitives:
285 std::vector<mrpt::opengl::TTriangle> new_tris;
286 std::vector<mrpt::math::TPoint3Df> new_lines;
287 glDrawText(text, new_tris, new_lines, style, spacing, kerning);
288
289 // Transform triangles:
290 for (auto& t : new_tris)
291 {
292 t.setColor(text_color);
293
294 for (int i = 0; i < 3; i++)
295 {
296 text_pose.rotateVector(t.vertices[i].normal);
297 t.vertices[i].xyzrgba.pt = text_pose.composePoint(t.vertices[i].xyzrgba.pt * text_scale);
298 }
299 t.computeNormals();
300 tris.emplace_back(t);
301 }
302
303 // Transform line vertices:
304 for (auto& v : new_lines)
305 {
306 lines.emplace_back(text_pose.composePoint(v * text_scale));
307 line_colors.emplace_back(text_color);
308 }
309 }
310
311 } // namespace mrpt::opengl::internal