vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
font_asset.h
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <cstdint>
6#include <vector>
7
8#include <glm/glm.hpp>
9
10#include "resource/resource.h"
11#include "resource/resource_handle.h"
12
13namespace Vkm::Engine {
14
21struct FontGlyph {
22 glm::vec2 uvMin = {0.0f, 0.0f};
23 glm::vec2 uvMax = {0.0f, 0.0f};
24 glm::vec2 size = {0.0f, 0.0f};
26 glm::vec2 offset = {0.0f, 0.0f};
27 float advance = 0.0f;
28};
29
37 uint32_t pair = 0;
38 float adjust = 0.0f;
39};
40
48struct FontAsset : public Resource {
49 static constexpr uint32_t ASCII_FIRST = 0x20;
50 static constexpr uint32_t ASCII_LAST = 0x7E;
51 static constexpr uint32_t LATIN1_FIRST = 0xA0;
52 static constexpr uint32_t LATIN1_LAST = 0xFF;
53
55 static constexpr uint32_t MARKS[] = {
56 0x2013,
57 0x2014,
58 0x2018,
59 0x2019,
60 0x201C,
61 0x201D,
62 0x2022,
63 0x2026,
64 0x20AC
65 };
66
67 static constexpr uint32_t ASCII_COUNT = ASCII_LAST - ASCII_FIRST + 1;
68 static constexpr uint32_t LATIN1_COUNT = LATIN1_LAST - LATIN1_FIRST + 1;
69 static constexpr uint32_t MARK_COUNT = sizeof(MARKS) / sizeof(MARKS[0]);
70 static constexpr uint32_t GLYPH_COUNT = ASCII_COUNT + LATIN1_COUNT + MARK_COUNT;
71
78 static constexpr uint32_t MIP_LEVELS = 4;
79
80 std::vector<uint8_t> atlasPixels;
81 uint32_t atlasSize = 0;
82
83 float pixelHeight = 0.0f;
84 float ascent = 0.0f;
86 float descent = 0.0f;
87
93 float lineHeight = 0.0f;
94
95 std::array<FontGlyph, GLYPH_COUNT> glyphs{};
96
98 std::vector<KerningPair> kerning;
99
106 static constexpr int glyphIndex(char32_t codepoint) {
107 if (codepoint >= ASCII_FIRST && codepoint <= ASCII_LAST) {
108 return static_cast<int>(codepoint - ASCII_FIRST);
109 }
110 if (codepoint >= LATIN1_FIRST && codepoint <= LATIN1_LAST) {
111 return static_cast<int>(ASCII_COUNT + codepoint - LATIN1_FIRST);
112 }
113 for (uint32_t i = 0; i < MARK_COUNT; ++i) {
114 if (MARKS[i] == codepoint) return static_cast<int>(ASCII_COUNT + LATIN1_COUNT + i);
115 }
116 return -1;
117 }
118
125 static constexpr char32_t codepointAt(uint32_t index) {
126 if (index < ASCII_COUNT) return ASCII_FIRST + index;
127 if (index < ASCII_COUNT + LATIN1_COUNT) return LATIN1_FIRST + index - ASCII_COUNT;
128 return MARKS[index - ASCII_COUNT - LATIN1_COUNT];
129 }
130
137 const FontGlyph* glyph(char32_t codepoint) const {
138 const int index = glyphIndex(codepoint);
139 return index < 0 ? nullptr : &glyphs[static_cast<size_t>(index)];
140 }
141
150 float kern(int left, int right) const {
151 const uint32_t pair = static_cast<uint32_t>(left) * GLYPH_COUNT + static_cast<uint32_t>(right);
152 const auto found = std::lower_bound(
153 kerning.begin(),
154 kerning.end(),
155 pair,
156 [](const KerningPair& entry, uint32_t key) { return entry.pair < key; }
157 );
158 return (found != kerning.end() && found->pair == pair) ? found->adjust : 0.0f;
159 }
160};
161
162using FontHandle = Handle<FontAsset>;
163
164} // namespace Vkm::Engine
A TrueType font baked to a signed-distance-field atlas.
Definition font_asset.h:48
static constexpr char32_t codepointAt(uint32_t index)
The codepoint glyph index holds; the inverse of glyphIndex.
Definition font_asset.h:125
std::vector< uint8_t > atlasPixels
Single-channel SDF atlas, atlasSize x atlasSize texels.
Definition font_asset.h:80
static constexpr uint32_t ASCII_FIRST
Space.
Definition font_asset.h:49
static constexpr uint32_t LATIN1_FIRST
No-break space.
Definition font_asset.h:51
uint32_t atlasSize
Atlas dimension in texels (square).
Definition font_asset.h:81
static constexpr uint32_t LATIN1_LAST
y with diaeresis.
Definition font_asset.h:52
float lineHeight
Baseline-to-baseline distance for stacked lines, in baked pixels.
Definition font_asset.h:93
float ascent
Definition font_asset.h:84
static constexpr uint32_t MARKS[]
Dashes, curly quotes, the bullet, the ellipsis and the euro sign, ascending.
Definition font_asset.h:55
float descent
Baseline-to-bottom, in baked pixels (negative below the baseline).
Definition font_asset.h:86
const FontGlyph * glyph(char32_t codepoint) const
The glyph for codepoint.
Definition font_asset.h:137
static constexpr uint32_t MIP_LEVELS
How many levels of the atlas a renderer may sample, the full size included.
Definition font_asset.h:78
float kern(int left, int right) const
The kerning between glyph left and glyph right, in baked pixels.
Definition font_asset.h:150
static constexpr uint32_t ASCII_LAST
'~'.
Definition font_asset.h:50
static constexpr int glyphIndex(char32_t codepoint)
Where codepoint sits in the glyph table.
Definition font_asset.h:106
std::vector< KerningPair > kerning
Sorted on KerningPair::pair; unkerned pairs are absent.
Definition font_asset.h:98
One glyph's atlas placement and layout metrics, in baked pixels.
Definition font_asset.h:21
glm::vec2 offset
Quad top-left relative to the pen on the baseline (negative y is above it).
Definition font_asset.h:26
glm::vec2 size
Definition font_asset.h:24
float advance
Pen advance in baked pixels.
Definition font_asset.h:27
How far the pen moves between one glyph and the next, past their advances.
Definition font_asset.h:36
float adjust
Added to the left glyph's advance, in baked pixels; negative pulls together.
Definition font_asset.h:38