Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
BitmapFont.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#include "BitmapFont.hpp"
4#include "Utf8.hpp"
5#include "../core/Log.hpp"
7
8#include <cstdlib>
9
10namespace esengine::text {
11
12static std::string extractValue(const std::string& line, const std::string& key) {
13 std::string search = key + "=";
14 auto pos = line.find(search);
15 if (pos == std::string::npos) {
16 return "";
17 }
18
19 pos += search.size();
20 if (pos < line.size() && line[pos] == '"') {
21 pos++;
22 auto end = line.find('"', pos);
23 if (end == std::string::npos) {
24 return "";
25 }
26 return line.substr(pos, end - pos);
27 }
28
29 auto end = line.find(' ', pos);
30 if (end == std::string::npos) {
31 end = line.size();
32 }
33 return line.substr(pos, end - pos);
34}
35
36static i32 extractInt(const std::string& line, const std::string& key) {
37 auto val = extractValue(line, key);
38 return val.empty() ? 0 : std::atoi(val.c_str());
39}
40
41bool BitmapFont::parseFntContent(const std::string& content) {
42 std::string line;
43
44 for (usize at = 0; at < content.size();) {
45 usize end = content.find('\n', at);
46 if (end == std::string::npos) end = content.size();
47 line.assign(content, at, end - at);
48 at = end + 1;
49 if (line.empty()) {
50 continue;
51 }
52
53 if (line.compare(0, 7, "common ") == 0) {
54 line_height_ = static_cast<f32>(extractInt(line, "lineHeight"));
55 base_ = static_cast<f32>(extractInt(line, "base"));
56 tex_width_ = static_cast<u32>(extractInt(line, "scaleW"));
57 tex_height_ = static_cast<u32>(extractInt(line, "scaleH"));
58 } else if (line.compare(0, 5, "page ") == 0) {
59 page_file_ = extractValue(line, "file");
60 } else if (line.compare(0, 5, "char ") == 0) {
61 Glyph glyph;
62 glyph.id = static_cast<u32>(extractInt(line, "id"));
63 glyph.x = static_cast<f32>(extractInt(line, "x"));
64 glyph.y = static_cast<f32>(extractInt(line, "y"));
65 glyph.width = static_cast<f32>(extractInt(line, "width"));
66 glyph.height = static_cast<f32>(extractInt(line, "height"));
67 glyph.xOffset = static_cast<f32>(extractInt(line, "xoffset"));
68 glyph.yOffset = static_cast<f32>(extractInt(line, "yoffset"));
69 glyph.xAdvance = static_cast<f32>(extractInt(line, "xadvance"));
70 glyph.page = static_cast<u32>(extractInt(line, "page"));
71 glyphs_[glyph.id] = glyph;
72 } else if (line.compare(0, 8, "kerning ") == 0) {
73 u32 first = static_cast<u32>(extractInt(line, "first"));
74 u32 second = static_cast<u32>(extractInt(line, "second"));
75 f32 amount = static_cast<f32>(extractInt(line, "amount"));
76 u64 key = (static_cast<u64>(first) << 32) | second;
77 kerning_[key] = amount;
78 }
79 }
80
81 return !glyphs_.empty();
82}
83
84bool BitmapFont::loadFromFntText(const std::string& content,
85 const std::string& basePath,
87 if (!parseFntContent(content)) {
88 ES_LOG_ERROR("Failed to parse BMFont text content");
89 return false;
90 }
91
92 if (page_file_.empty()) {
93 ES_LOG_ERROR("BMFont has no page file");
94 return false;
95 }
96
97 std::string texturePath = basePath.empty() ? page_file_ : basePath + "/" + page_file_;
98 texture_ = rm.loadTexture(texturePath);
99
100 if (!texture_.isValid()) {
101 ES_LOG_ERROR("Failed to load BMFont texture: {}", texturePath);
102 return false;
103 }
104
105 auto* tex = rm.getTexture(texture_);
106 if (tex) {
107 tex_width_ = tex->getWidth();
108 tex_height_ = tex->getHeight();
109 }
110
111 return true;
112}
113
114bool BitmapFont::loadFromFntText(const std::string& content,
116 u32 texWidth, u32 texHeight) {
117 if (!parseFntContent(content)) {
118 ES_LOG_ERROR("Failed to parse BMFont text content");
119 return false;
120 }
121
122 texture_ = texture;
123 tex_width_ = texWidth;
124 tex_height_ = texHeight;
125 return true;
126}
127
129 u32 texWidth, u32 texHeight,
130 const std::string& chars,
131 u32 charWidth, u32 charHeight) {
132 texture_ = texture;
133 tex_width_ = texWidth;
134 tex_height_ = texHeight;
135 line_height_ = static_cast<f32>(charHeight);
136 base_ = line_height_;
137
138 // Guard against divide-by-zero (cols below, and `% cols` in the loop).
139 // ES_ASSERT is stripped in release, so these must be runtime checks.
140 if (charWidth == 0 || charHeight == 0 || texWidth < charWidth) {
141 ES_LOG_ERROR("BitmapFont::createLabelAtlas: invalid dimensions (char {}x{}, tex {}x{}); aborting atlas build",
142 charWidth, charHeight, texWidth, texHeight);
143 return;
144 }
145
146 u32 cols = texWidth / charWidth;
147
148 u32 glyphIndex = 0;
149 for (usize i = 0; i < chars.size();) {
150 const u32 charCode = nextCodepoint(chars.data(), chars.size(), i);
151
152 u32 col = glyphIndex % cols;
153 u32 row = glyphIndex / cols;
154
155 Glyph glyph;
156 glyph.id = charCode;
157 glyph.x = static_cast<f32>(col * charWidth);
158 glyph.y = static_cast<f32>(row * charHeight);
159 glyph.width = static_cast<f32>(charWidth);
160 glyph.height = static_cast<f32>(charHeight);
161 glyph.xOffset = 0;
162 glyph.yOffset = 0;
163 glyph.xAdvance = static_cast<f32>(charWidth);
164 glyph.page = 0;
165 glyphs_[charCode] = glyph;
166 ++glyphIndex;
167 }
168}
169
170BitmapFont::TextMetrics BitmapFont::measureText(const std::string& text, f32 fontSize, f32 spacing) const {
171 f32 totalWidth = 0;
172 u32 prevChar = 0;
173
174 for (usize i = 0; i < text.size();) {
175 u32 charCode = nextCodepoint(text.data(), text.size(), i);
176 auto* glyph = getGlyph(charCode);
177 if (!glyph) {
178 continue;
179 }
180 if (prevChar) {
181 totalWidth += getKerning(prevChar, charCode);
182 }
183 totalWidth += glyph->xAdvance + spacing;
184 prevChar = charCode;
185 }
186
187 return { totalWidth * fontSize, line_height_ * fontSize };
188}
189
190const Glyph* BitmapFont::getGlyph(u32 charCode) const {
191 auto it = glyphs_.find(charCode);
192 return it != glyphs_.end() ? &it->second : nullptr;
193}
194
195f32 BitmapFont::getKerning(u32 first, u32 second) const {
196 u64 key = (static_cast<u64>(first) << 32) | second;
197 auto it = kerning_.find(key);
198 return it != kerning_.end() ? it->second : 0.0f;
199}
200
201} // namespace esengine::text
Logging utilities for ESEngine.
#define ES_LOG_ERROR(...)
Logs an error message.
Definition Log.hpp:296
Central resource management system.
Central manager for GPU resources.
Texture * getTexture(TextureHandle handle)
Gets a texture by handle.
TextureHandle loadTexture(const std::string &path)
Loads a texture from file (with caching).
void createLabelAtlas(resource::TextureHandle texture, u32 texWidth, u32 texHeight, const std::string &chars, u32 charWidth, u32 charHeight)
bool loadFromFntText(const std::string &content, const std::string &basePath, resource::ResourceManager &rm)
f32 getKerning(u32 first, u32 second) const
const Glyph * getGlyph(u32 charCode) const
TextMetrics measureText(const std::string &text, f32 fontSize, f32 spacing) const
Handle< esengine::Texture > TextureHandle
Handle to a texture resource.
Definition Handle.hpp:217
static std::string extractValue(const std::string &line, const std::string &key)
constexpr u32 nextCodepoint(const char *data, usize length, usize &pos)
Definition Utf8.hpp:11
static i32 extractInt(const std::string &line, const std::string &key)
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
float f32
32-bit floating point
Definition Types.hpp:59