gdk-graphics 0a0aef92e917cfbac53dffbc32c295e4de914093
Loading...
Searching...
No Matches
font.h
1// © Joseph Cameron - MIT License
2
3#ifndef GDK_GRAPHICS_EXT_FONT_H
4#define GDK_GRAPHICS_EXT_FONT_H
5
7
8#include <cstddef>
9#include <memory>
10#include <optional>
11#include <span>
12
13namespace gdk::graphics::ext {
14 enum class font_sizing {
15 ascent_to_descent,
16 em
17 };
18
20 struct font_settings final {
22 std::size_t face = 0;
23
25 font_sizing sizing = font_sizing::ascent_to_descent;
26
28 std::size_t atlas_edge = 512;
29
31 std::size_t max_atlas_edge = 2048;
32 };
33
41 class font final {
42 public:
43 struct glyph {
44 float s0 = 0;
45 float t0 = 0;
46 float s1 = 0;
47 float t1 = 0;
48
49 float left = 0;
50 float top = 0;
51 float width = 0;
52 float height = 0;
53 float advance = 0;
54 };
55
59 [[nodiscard]] const glyph *try_get(char32_t aCodepoint);
60
66 [[nodiscard]] const glyph &fallback() const;
67
72 void clear();
73
75 [[nodiscard]] float kerning(char32_t aLeft, char32_t aRight) const;
76
78 [[nodiscard]] texture_data::view atlas() const;
79
81 struct atlas_region {
82 std::size_t x = 0, y = 0, width = 0, height = 0;
83 };
84
86 [[nodiscard]] std::optional<atlas_region> atlas_changed_since(std::size_t aGeneration) const;
87
89 [[nodiscard]] std::size_t atlas_generation() const;
90
92 [[nodiscard]] float line_height() const;
93
95 [[nodiscard]] float ascent() const;
96
98 [[nodiscard]] float descent() const;
99
108 std::span<const texture_data::encoded_byte> aTrueTypeData,
109 float aPixelSize,
110 font_settings aSettings = {}
111 );
112
114 [[nodiscard]] std::size_t atlas_edge() const;
115
116 ~font();
117
118 font(font &&) noexcept;
119 font &operator=(font &&) noexcept;
120
121 font(const font &) = delete;
122 font &operator=(const font &) = delete;
123
124 private:
125 struct impl;
126 std::unique_ptr<impl> m_pImpl;
127 };
128}
129
130#endif
float kerning(char32_t aLeft, char32_t aRight) const
additional spacing between two adjacent codepoints in pixels.
void clear()
empties the atlas back to the size it was constructed with.
std::size_t atlas_edge() const
the atlas's current edge length in texels. Changes when the atlas grows.
const glyph & fallback() const
the font's .notdef glyph, which most fonts draw as a visible box.
float line_height() const
baseline to baseline distance in pixels
font(std::span< const texture_data::encoded_byte > aTrueTypeData, float aPixelSize, font_settings aSettings={})
builds a font from the contents of a truetype or opentype file, or a collection
float ascent() const
baseline to the top of the tallest glyph, in pixels
texture_data::view atlas() const
view of the atlas texture
std::optional< atlas_region > atlas_changed_since(std::size_t aGeneration) const
which part of the atlas has changed since a given generation.
const glyph * try_get(char32_t aCodepoint)
the glyph for a codepoint or nullptr when the atlas has no room left for it.
float descent() const
baseline to the bottom of the lowest glyph, in pixels. Negative.
std::size_t atlas_generation() const
a number that changes whenever the atlas does.
a rectangle of the atlas, in texels
Definition font.h:81
float top
baseline to the quad's top edge, in pixels, up is positive
Definition font.h:50
float s0
atlas texture coordinates
Definition font.h:44
float width
quad width in pixels
Definition font.h:51
float t0
atlas texture coordinates
Definition font.h:45
float left
pen to the quad's left edge, in pixels
Definition font.h:49
float t1
atlas texture coordinates
Definition font.h:47
float s1
atlas texture coordinates
Definition font.h:46
float height
quad height in pixels
Definition font.h:52
float advance
how far the pen moves after this glyph, in pixels
Definition font.h:53
everything about a font that has a default
Definition font.h:20
font_sizing sizing
what the constructor's pixel size measures.
Definition font.h:25
std::size_t atlas_edge
the atlas starts square with this edge length
Definition font.h:28
std::size_t face
which font of a collection (.ttc, .otc) to use
Definition font.h:22
std::size_t max_atlas_edge
how far the atlas may grow
Definition font.h:31
provides a pointer a contiguous list of channel data representing 2D texture metadata that contains i...
Definition texture_data.h:27
what the core takes: decoded channel data and the metadata describing it