vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
text_layout.h
1#pragma once
2
3#include <cstddef>
4#include <string_view>
5
6#include "core/utf8.h"
7#include "resource/asset/font_asset.h"
8
9namespace Vkm::Engine {
10
27template <typename Visit>
28float walkGlyphs(const FontAsset& font, std::string_view text, float scale, Visit&& visit) {
29 float pen = 0.0f;
30 int previous = -1;
31 std::size_t at = 0;
32 while (at < text.size()) {
33 const std::size_t start = at;
34 const int index = FontAsset::glyphIndex(Utf8::next(text, at));
35 if (index < 0) {
36 previous = -1;
37 continue;
38 }
39 if (previous >= 0) pen += font.kern(previous, index) * scale;
40
41 const FontGlyph& glyph = font.glyphs[static_cast<std::size_t>(index)];
42 if (!visit(start, glyph, pen)) return pen;
43 pen += glyph.advance * scale;
44 previous = index;
45 }
46 return pen;
47}
48
57inline float measureText(const FontAsset& font, std::string_view text, float pixelSize) {
58 if (font.pixelHeight <= 0.0f) return 0.0f;
59 return walkGlyphs(
60 font,
61 text,
62 pixelSize / font.pixelHeight,
63 [](std::size_t, const FontGlyph&, float) { return true; }
64 );
65}
66
67} // namespace Vkm::Engine
A TrueType font baked to a signed-distance-field atlas.
Definition font_asset.h:48
static constexpr int glyphIndex(char32_t codepoint)
Where codepoint sits in the glyph table.
Definition font_asset.h:106
One glyph's atlas placement and layout metrics, in baked pixels.
Definition font_asset.h:21
float advance
Pen advance in baked pixels.
Definition font_asset.h:27