scimesh 0.3.4
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
text.h
Go to the documentation of this file.
1
34
35#pragma once
36
37#include <scimesh/font.h>
38#include <scimesh/image.h>
39#include <scimesh/types.h>
40
41#include <algorithm>
42#include <string>
43#include <vector>
44
45namespace scimesh {
46
50enum class TextSpace {
56 WORLD,
57
64 SCREEN
65};
66
72 Color color = Color(0.0f, 0.0f, 0.0f, 1.0f);
73
78 Color halo_color = Color(0.0f, 0.0f, 0.0f, 0.0f);
79
81 float halo_width = 1.0f;
82
85 float line_spacing = 1.2f;
86
94 float rotation = 0.0f;
95};
96
107 float width = 0.0f;
108
110 float height = 0.0f;
111
113 float ascent = 0.0f;
114
116 float descent = 0.0f;
117
119 size_t line_count = 0;
120};
121
145TextExtent measure_text(const std::string &text, float size,
146 const std::string &font_file = "",
147 float line_spacing = 1.2f);
148
180void draw_text(Image &image, const std::string &text, const Font &font, float x,
181 float baseline_y, const TextDrawStyle &style = TextDrawStyle());
182
208struct TextLayer {
213 std::vector<Vec3> positions;
214
216 std::vector<std::string> strings;
217
221 std::vector<Color> colors;
222
228 std::string font_file;
229
234 float size = 16.0f;
235
238
244 Vec2 adj = Vec2(0.5f, 0.5f);
245
248 Vec2 offset = Vec2(0.0f, 0.0f);
249
252 float line_spacing = 1.2f;
253
264 float rotation = 0.0f;
265
276 bool depth_test = true;
277
281 Color halo_color = Color(0.0f, 0.0f, 0.0f, 0.0f);
282
284 float halo_width = 1.0f;
285
290 size_t count() const { return std::min(positions.size(), strings.size()); }
291
293 bool empty() const { return count() == 0; }
294
297 Color color_or(size_t i, const Color &fallback) const {
298 if (colors.empty()) {
299 return fallback;
300 }
301 return (i < colors.size()) ? colors[i] : colors.back();
302 }
303
305 Vec3 position_or(size_t i) const {
306 return (i < positions.size()) ? positions[i] : Vec3(0.0f);
307 }
308
310 const std::string &string_or(size_t i) const {
311 static const std::string kEmpty;
312 return (i < strings.size()) ? strings[i] : kEmpty;
313 }
314};
315
324 const TextLayer *layer = nullptr;
326 std::string name;
327};
328
336namespace detail {
337
352void render_text_layers(const std::vector<TextNodeRef> &layers, Image &output,
353 const Mat4 &view_projection, float pixel_scale,
354 const std::vector<float> &z_buffer,
355 const Color &default_color);
356
357} // namespace detail
358
359} // namespace scimesh
A TrueType font, loaded at a fixed pixel size, with a glyph cache.
Definition font.h:143
TrueType font loading, glyph rasterization and text measurement.
The Image — an RGBA pixel buffer with compositing and I/O operations.
void render_text_layers(const std::vector< TextNodeRef > &layers, Image &output, const Mat4 &view_projection, float pixel_scale, const std::vector< float > &z_buffer, const Color &default_color)
Draw all text layers into an already rendered image.
Definition text.cpp:448
void draw_text(Image &image, const std::string &text, const Font &font, float x, float baseline_y, const TextDrawStyle &style)
Draw text into an image at a given position and baseline.
Definition text.cpp:406
TextExtent measure_text(const std::string &text, float size, const std::string &font_file, float line_spacing)
Measure a (possibly multi-line) string in a given font and size.
Definition text.cpp:422
glm::vec2 Vec2
2-component floating-point vector (xy).
Definition types.h:32
TextSpace
The coordinate space of a TextLayer's positions.
Definition text.h:50
@ WORLD
3D world coordinates, projected with the scene camera.
@ SCREEN
Output image pixels, origin at the top left, y growing down.
glm::mat4 Mat4
4×4 floating-point matrix.
Definition types.h:65
glm::vec3 Vec3
3-component floating-point vector (xyz).
Definition types.h:46
An RGBA color with floating-point components.
Definition types.h:88
A 2D RGBA image buffer.
Definition image.h:92
How to draw text onto an image with draw_text().
Definition text.h:70
Color color
Text color; alpha values below 1 blend with the background.
Definition text.h:72
float rotation
Rotation of the text in degrees, counter-clockwise, about the point (x, baseline_y) passed to draw_te...
Definition text.h:94
float line_spacing
Distance between two lines of a multi-line string, as a multiple of the height of the font's glyph bo...
Definition text.h:85
Color halo_color
Color of the halo (outline) drawn behind the glyphs.
Definition text.h:78
float halo_width
Halo thickness in pixels (default: 1.0).
Definition text.h:81
Size of a (possibly multi-line) piece of text.
Definition text.h:105
float height
Height of the whole text block, in pixels.
Definition text.h:110
float ascent
Distance from the first baseline to the top of the block, in pixels.
Definition text.h:113
float descent
Distance from the last baseline to the bottom of the block, in pixels.
Definition text.h:116
float width
Width of the widest line, in pixels.
Definition text.h:107
size_t line_count
Number of lines (a trailing newline does not add one).
Definition text.h:119
A batch of text labels drawn together, in one coordinate space.
Definition text.h:208
std::vector< Vec3 > positions
Anchor positions, in world space or in screen pixels.
Definition text.h:213
bool empty() const
Whether this layer holds no label with a position and a string.
Definition text.h:293
Vec3 position_or(size_t i) const
Position of label i (origin when out of range).
Definition text.h:305
size_t count() const
Number of labels in this layer.
Definition text.h:290
TextSpace space
Coordinate space of positions (default: TextSpace::WORLD).
Definition text.h:237
std::vector< Color > colors
One color per label; may be empty (then the renderer uses RenderOptions::default_color) or shorter th...
Definition text.h:221
float halo_width
Halo thickness in output pixels (default: 1).
Definition text.h:284
Color color_or(size_t i, const Color &fallback) const
Color of label i, falling back to fallback when the layer holds no colors at all.
Definition text.h:297
Vec2 offset
Extra offset applied after anchoring, in output pixels (positive x = right, positive y = down).
Definition text.h:248
bool depth_test
Whether the label is hidden by geometry in front of its anchor (default: true).
Definition text.h:276
Vec2 adj
Where the position sits relative to the text box, in [0, 1].
Definition text.h:244
const std::string & string_or(size_t i) const
String of label i (empty when out of range).
Definition text.h:310
float size
Text height ("em" size) in output pixels (default: 16).
Definition text.h:234
std::string font_file
Path to the .ttf file to use, or an empty string for the bundled default font (see default_font_path(...
Definition text.h:228
Color halo_color
Color of the halo (outline) drawn behind the glyphs.
Definition text.h:281
float rotation
Rotation of the label in degrees, counter-clockwise, about the anchor position (default: 0).
Definition text.h:264
std::vector< std::string > strings
One string per entry in positions (UTF-8; \n for line breaks).
Definition text.h:216
float line_spacing
Distance between two lines of a multi-line string, as a multiple of the height of the font's glyph bo...
Definition text.h:252
A non-owning reference to one text layer in a scene, together with its placement transform (and optio...
Definition text.h:323
const TextLayer * layer
Pointer to the owned layer (never null for a valid node).
Definition text.h:324
Mat4 transform
Model matrix applied to the anchor positions.
Definition text.h:325
std::string name
Optional node name (used by exporters / debugging).
Definition text.h:326
int x
Left edge of the bitmap, in image pixels.
Definition text.cpp:223
Fundamental types used throughout the scimesh rendering engine.