scimesh 0.3.4
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
font.h
Go to the documentation of this file.
1
38
39#pragma once
40
41#include <scimesh/types.h>
42
43#include <cstdint>
44#include <memory>
45#include <string>
46#include <vector>
47
48namespace scimesh {
49
61 float ascent = 0.0f;
62
66 float descent = 0.0f;
67
69 float line_gap = 0.0f;
70
72 float line_height() const { return ascent + descent + line_gap; }
73
75 float box_height() const { return ascent + descent; }
76};
77
93 int width = 0;
94
96 int height = 0;
97
99 int x_offset = 0;
100
105 int y_offset = 0;
106
108 float advance = 0.0f;
109
111 std::vector<uint8_t> coverage;
112
114 bool empty() const { return width <= 0 || height <= 0 || coverage.empty(); }
115
117 uint8_t at(int x, int y) const {
118 return coverage[static_cast<size_t>(y) * static_cast<size_t>(width) +
119 static_cast<size_t>(x)];
120 }
121};
122
143class Font {
144public:
146 Font() = default;
147
155 static Font from_file(const std::string &path, float pixel_size);
156
163 static Font from_memory(std::vector<uint8_t> data, float pixel_size);
164
166 bool valid() const { return static_cast<bool>(impl_); }
167
169 float pixel_size() const;
170
173 std::string source_path() const;
174
178 FontMetrics metrics() const;
179
186 float measure(const std::string &text) const;
187
189 float advance(uint32_t codepoint) const;
190
197 float kerning(uint32_t first, uint32_t second) const;
198
207 const GlyphBitmap *glyph(uint32_t codepoint) const;
208
210 std::string family_name() const;
211
220
227 static std::vector<uint32_t> decode_utf8(const std::string &text);
228
230 static size_t line_count(const std::string &text);
231
232private:
233 struct Impl;
234
235 explicit Font(std::shared_ptr<Impl> impl) : impl_(std::move(impl)) {}
236
237 std::shared_ptr<Impl> impl_;
238};
239
251std::string resolve_font_path(const std::string &path);
252
269std::string default_font_path();
270
280Font cached_font(const std::string &path, float pixel_size);
281
287void clear_font_cache();
288
289} // namespace scimesh
A TrueType font, loaded at a fixed pixel size, with a glyph cache.
Definition font.h:143
static size_t line_count(const std::string &text)
Number of lines in a string (newline-separated, at least 1).
Definition font.cpp:357
std::string family_name() const
Family name of the font, e.g. "Inter" (empty if unavailable).
Definition font.cpp:283
static std::vector< uint32_t > decode_utf8(const std::string &text)
Decode a UTF-8 string into Unicode code points.
Definition font.cpp:311
static Font from_file(const std::string &path, float pixel_size)
Load a TrueType font from a file.
Definition font.cpp:170
FontMetrics metrics() const
Vertical metrics (ascent, descent, line gap) in pixels.
Definition font.cpp:205
Font with_pixel_size(float new_pixel_size) const
A font handle for the same typeface at a different pixel size.
Definition font.cpp:186
float advance(uint32_t codepoint) const
Pen advance of one code point in pixels.
Definition font.cpp:207
std::string source_path() const
Path this font was loaded from, or an empty string for from_memory() fonts.
Definition font.cpp:203
float pixel_size() const
Em size of this font in pixels (0 for an invalid font).
Definition font.cpp:201
Font()=default
Construct an empty (invalid) font. See valid().
float measure(const std::string &text) const
Width of a single line of text in pixels, including kerning.
Definition font.cpp:227
const GlyphBitmap * glyph(uint32_t codepoint) const
The rasterized glyph for a code point, rasterizing it on first use.
Definition font.cpp:246
bool valid() const
Whether this handle refers to a loaded font.
Definition font.h:166
float kerning(uint32_t first, uint32_t second) const
Kerning adjustment between two code points, in pixels.
Definition font.cpp:217
static Font from_memory(std::vector< uint8_t > data, float pixel_size)
Create a font from TTF data already in memory.
Definition font.cpp:178
std::string resolve_font_path(const std::string &path)
Turn a possibly empty font path into a path that exists.
Definition font.cpp:398
void clear_font_cache()
Drop all fonts from the process-wide font cache.
Definition font.cpp:428
std::string default_font_path()
Path of the bundled default font, or as overridden by the SCIMESH_FONT environment variable.
Definition font.cpp:371
Font cached_font(const std::string &path, float pixel_size)
Load a font, re-using an already loaded one when possible.
Definition font.cpp:405
Vertical metrics of a font at a given pixel size.
Definition font.h:59
float line_height() const
Total height of one line, in pixels (ascent + descent + line_gap).
Definition font.h:72
float ascent
Distance from the baseline to the top of the line, in pixels.
Definition font.h:61
float line_gap
Recommended extra spacing between two lines, in pixels.
Definition font.h:69
float box_height() const
Height of the glyph box of one line, in pixels (ascent + descent).
Definition font.h:75
float descent
Distance from the baseline to the bottom of the line, in pixels.
Definition font.h:66
One rasterized glyph: an 8-bit coverage bitmap plus placement info.
Definition font.h:91
uint8_t at(int x, int y) const
Coverage value at pixel (x, y), 0-255. No bounds checking.
Definition font.h:117
float advance
Pen advance for this glyph, in pixels.
Definition font.h:108
int x_offset
Horizontal offset from the pen position to the bitmap's left edge.
Definition font.h:99
int y_offset
Vertical offset from the baseline to the bitmap's top edge.
Definition font.h:105
int height
Bitmap height in pixels.
Definition font.h:96
bool empty() const
Whether this glyph has no pixels to draw (e.g. a space).
Definition font.h:114
std::vector< uint8_t > coverage
Coverage values, row-major, width * height entries.
Definition font.h:111
int width
Bitmap width in pixels.
Definition font.h:93
int y
Top edge of the bitmap, in image pixels.
Definition text.cpp:224
int x
Left edge of the bitmap, in image pixels.
Definition text.cpp:223
Fundamental types used throughout the scimesh rendering engine.