A TrueType font, loaded at a fixed pixel size, with a glyph cache.
More...
#include <font.h>
|
| struct | Impl |
| | Shared font state: the font bytes, the parsed font and the glyph cache. More...
|
| |
A TrueType font, loaded at a fixed pixel size, with a glyph cache.
A Font is a cheap handle: copying it shares the underlying font data and the rasterized glyph cache (reference counting), so passing fonts around by value is fine. Glyphs are rasterized lazily on first use and then cached, which makes repeated renders of the same labels cheap.
Fonts are immutable once loaded; to render at a different size use with_pixel_size(), which re-uses the already loaded font bytes.
- Example
A TrueType font, loaded at a fixed pixel size, with a glyph cache.
Font cached_font(const std::string &path, float pixel_size)
Load a font, re-using an already loaded one when possible.
Vertical metrics of a font at a given pixel size.
int x
Left edge of the bitmap, in image pixels.
- See also
- cached_font(), draw_text(), TextLayer
- Since
- 0.4.0
Definition at line 143 of file font.h.
◆ Font()
Construct an empty (invalid) font. See valid().
◆ advance()
Pen advance of one code point in pixels.
Definition at line 207 of file font.cpp.
◆ decode_utf8()
| std::vector< uint32_t > scimesh::Font::decode_utf8 |
( |
const std::string & |
text | ) |
|
|
static |
Decode a UTF-8 string into Unicode code points.
Invalid byte sequences are decoded as U+FFFD (replacement character) instead of throwing, so arbitrary user input is safe to pass in.
- Parameters
-
| text | A UTF-8 encoded string. |
Definition at line 311 of file font.cpp.
◆ family_name()
| std::string scimesh::Font::family_name |
( |
| ) |
const |
Family name of the font, e.g. "Inter" (empty if unavailable).
Definition at line 283 of file font.cpp.
◆ from_file()
| Font scimesh::Font::from_file |
( |
const std::string & |
path, |
|
|
float |
pixel_size |
|
) |
| |
|
static |
Load a TrueType font from a file.
- Parameters
-
| path | Path to a .ttf (or .otf with TrueType outlines) file. |
| pixel_size | Em size in output pixels, must be > 0. |
- Returns
- The loaded font.
- Exceptions
-
| std::runtime_error | If the file cannot be read or is not a supported font. |
Definition at line 170 of file font.cpp.
◆ from_memory()
| Font scimesh::Font::from_memory |
( |
std::vector< uint8_t > |
data, |
|
|
float |
pixel_size |
|
) |
| |
|
static |
Create a font from TTF data already in memory.
- Parameters
-
| data | Raw contents of a .ttf file (moved into the font). |
| pixel_size | Em size in output pixels, must be > 0. |
- Returns
- The loaded font.
- Exceptions
-
| std::runtime_error | If the data is not a supported font. |
Definition at line 178 of file font.cpp.
◆ glyph()
The rasterized glyph for a code point, rasterizing it on first use.
The returned pointer stays valid as long as any copy of this font lives (the glyphs are cached in the shared font data). Returns an empty glyph with a space-like advance when the font has no glyph for the code point, and nullptr only for an invalid font.
- Parameters
-
| codepoint | Unicode code point (not a UTF-8 byte). |
Definition at line 246 of file font.cpp.
◆ kerning()
Kerning adjustment between two code points, in pixels.
Only the legacy TrueType kern table is consulted (that is all stb_truetype supports). Fonts that carry their kerning in the modern GPOS table — many recent fonts, including the bundled Inter — return 0.0 here, which simply means "no kerning information available".
Definition at line 217 of file font.cpp.
◆ line_count()
| size_t scimesh::Font::line_count |
( |
const std::string & |
text | ) |
|
|
static |
Number of lines in a string (newline-separated, at least 1).
Definition at line 357 of file font.cpp.
◆ measure()
| float scimesh::Font::measure |
( |
const std::string & |
text | ) |
const |
Width of a single line of text in pixels, including kerning.
Newline characters are not handled specially here: callers that support multi-line text split the string first (see TextLayer).
- Parameters
-
| text | A UTF-8 encoded string. |
Definition at line 227 of file font.cpp.
◆ metrics()
Vertical metrics (ascent, descent, line gap) in pixels.
Returns all zeros for an invalid font.
Definition at line 205 of file font.cpp.
◆ pixel_size()
| float scimesh::Font::pixel_size |
( |
| ) |
const |
Em size of this font in pixels (0 for an invalid font).
Definition at line 201 of file font.cpp.
◆ source_path()
| std::string scimesh::Font::source_path |
( |
| ) |
const |
◆ valid()
| bool scimesh::Font::valid |
( |
| ) |
const |
|
inline |
Whether this handle refers to a loaded font.
Definition at line 166 of file font.h.
◆ with_pixel_size()
| Font scimesh::Font::with_pixel_size |
( |
float |
new_pixel_size | ) |
const |
A font handle for the same typeface at a different pixel size.
The already loaded font bytes are shared, so this does not touch the file system again. Used by the renderer to rasterize at the supersampled resolution, see RenderOptions::aa_samples.
- Parameters
-
| new_pixel_size | New em size in pixels, must be > 0. |
Definition at line 186 of file font.cpp.
The documentation for this class was generated from the following files: