GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Font2D.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_GRAPHICS_2D_FONT_2D_HPP
5#define GREM_GRAPHICS_2D_FONT_2D_HPP
6
8
15#include <GREM/core/math.hpp>
19
20namespace grem::graphics {
21
22class Device; // Forward declaration, to avoid including Device.hpp.
23
32 .initialResolution = 128,
33 .padding = 1,
34 .alignment = 1,
35 };
36
50 bool useLinearFiltering = false;
51
59 [[nodiscard]] constexpr bool operator==(const Font2DOptions& other) const = default;
60};
61
66class Font2D {
67public:
75
84
88 struct LineMetrics {
89 float ascender;
90 float descender;
91 float height;
92 };
93
115 GREM_API(graphics_2d) Font2D(const Filesystem& filesystem, CStringView filepath, const Font2DOptions& options = {});
116
118 ~Font2D() = default;
119
124 Font2D(const Font2D&) = delete;
125
127 Font2D(Font2D&& other) noexcept = default;
128
133 Font2D& operator=(const Font2D& other) = delete;
134
136 Font2D& operator=(Font2D&& other) noexcept = default;
137
141 void clearRenderedGlyphs() noexcept;
142
156 [[nodiscard]] GREM_API(graphics_2d) Optional<RenderedGlyphInfo> findRenderedGlyphInfo(uint32_t characterSize, char32_t codePoint) const noexcept;
157
186 GREM_API(graphics_2d) Pair<RenderedGlyphInfo, bool> renderGlyph(Device& device, uint32_t characterSize, char32_t codePoint);
187
202 GREM_API(graphics_2d) void setOptions(Device& device, const Font2DOptions& newOptions);
203
216 [[nodiscard]] GREM_API(graphics_2d) GlyphMetrics getGlyphMetrics(uint32_t characterSize, char32_t codePoint) const noexcept;
217
225 [[nodiscard]] GREM_API(graphics_2d) LineMetrics getLineMetrics(uint32_t characterSize) const noexcept;
226
240 [[nodiscard]] GREM_API(graphics_2d) vec2 getKerning(uint32_t characterSize, char32_t left, char32_t right) const noexcept;
241
251 [[nodiscard]] const Texture& getAtlasTexture() const noexcept {
252 return atlasTexture;
253 }
254
262 [[nodiscard]] Font2DOptions getOptions() const noexcept {
263 return options;
264 }
265
266private:
267 struct FontDeleter {
268 GREM_API(graphics_2d) void operator()(void* handle) const noexcept;
269 };
270
271 struct RenderedGlyphKey {
272 uint32_t characterSize;
273 char32_t codePoint;
274
275 [[nodiscard]] constexpr auto operator<=>(const RenderedGlyphKey&) const = default;
276 };
277
278 Allocation<byte> fontFileContents{};
279 UniqueHandle<void*, FontDeleter> font{};
280 resource::AtlasPacker atlasPacker;
281 Texture atlasTexture{};
282 OrderedMap<RenderedGlyphKey, RenderedGlyphInfo> renderedGlyphs{};
283 Font2DOptions options;
284};
285
286} // namespace grem::graphics
287
288#endif
Base interface for a filesystem.
Definition Filesystem.hpp:25
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
Persistent system managing the rendering context of a Window.
Definition Device.hpp:68
GlyphMetrics getGlyphMetrics(uint32_t characterSize, char32_t codePoint) const noexcept
Get the dimensions of a single glyph in this font, for shaping text.
Pair< RenderedGlyphInfo, bool > renderGlyph(Device &device, uint32_t characterSize, char32_t codePoint)
Render the glyph for a specific character and store it in the texture atlas, if it has not already be...
Font2D(const Filesystem &filesystem, CStringView filepath, const Font2DOptions &options={})
Load a font from a file.
void clearRenderedGlyphs() noexcept
Remove all rendered glyphs in the font and reset the texture atlas.
~Font2D()=default
Destructor.
Font2D(const Font2D &)=delete
Copying a font is not allowed, since the memory location of the file contents must remain stable.
Font2DOptions getOptions() const noexcept
Get the current configuration options of the font.
Definition Font2D.hpp:262
Optional< RenderedGlyphInfo > findRenderedGlyphInfo(uint32_t characterSize, char32_t codePoint) const noexcept
Look up the information about a glyph's entry in the texture atlas for a specific code point.
void setOptions(Device &device, const Font2DOptions &newOptions)
Update the configuration options of this font and recreate the texture atlas if necessary to apply th...
Font2D(Font2D &&other) noexcept=default
Move constructor.
const Texture & getAtlasTexture() const noexcept
Get the texture atlas to use when rendering glyphs from this font.
Definition Font2D.hpp:251
Font2D & operator=(Font2D &&other) noexcept=default
Move assignment.
LineMetrics getLineMetrics(uint32_t characterSize) const noexcept
Get the vertical dimensions for shaping lines of text with this font.
Font2D & operator=(const Font2D &other)=delete
Copying a font is not allowed, since the memory location of the file contents must remain stable.
vec2 getKerning(uint32_t characterSize, char32_t left, char32_t right) const noexcept
Get the kerning offset to use between a pair of adjacent character glyphs while shaping text.
Storage for multidimensional data, such as 2D images, on the GPU, combined with a sampler configurati...
Definition Texture.hpp:700
Definition buffer_layouts.hpp:20
vec< 2, float > vec2
Definition math.hpp:438
constexpr std::compare_three_way_result_t< A, B > operator<=>(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:273
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635
Definition Pair.hpp:53
Configuration options for a Font2D.
Definition Font2D.hpp:27
bool useLinearFiltering
Use bilinear filtering rather than nearest-neighbor interpolation when rendering text at a non-1:1 sc...
Definition Font2D.hpp:50
resource::AtlasPackerOptions atlasPackerOptions
Configuration options for the font's internal atlas packer.
Definition Font2D.hpp:31
constexpr bool operator==(const Font2DOptions &other) const =default
Compare these options to another set of options for equality.
Dimensions of a single glyph in this font, for shaping text.
Definition Font2D.hpp:79
float advance
Horizontal offset to apply in order to advance to the next glyph position, excluding any kerning.
Definition Font2D.hpp:82
vec2 bearing
Offset from the baseline to apply to the glyph's rectangle position when rendering this glyph.
Definition Font2D.hpp:81
vec2 size
Size of this glyph's rectangle when rendered, in pixels.
Definition Font2D.hpp:80
Vertical dimensions for shaping lines of text with this font.
Definition Font2D.hpp:88
float ascender
Vertical offset from the baseline to the visual top of the text.
Definition Font2D.hpp:89
float height
Vertical offset to apply in order to advance to the next line.
Definition Font2D.hpp:91
float descender
Vertical offset from the baseline to the visual bottom of the text.
Definition Font2D.hpp:90
Information about a single glyph's entry in the texture atlas.
Definition Font2D.hpp:71
vec2 sizeInAtlas
Size of this glyph's rectangle in the texture atlas, in texels.
Definition Font2D.hpp:73
vec2 positionInAtlas
Position of this glyph's rectangle in the texture atlas, in texels.
Definition Font2D.hpp:72
Configuration options for an AtlasPacker.
Definition AtlasPacker.hpp:20