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) {
20 if (pos < line.size() && line[pos] ==
'"') {
22 auto end = line.find(
'"', pos);
23 if (end == std::string::npos) {
26 return line.substr(pos, end - pos);
29 auto end = line.find(
' ', pos);
30 if (end == std::string::npos) {
33 return line.substr(pos, end - pos);
38 return val.empty() ? 0 : std::atoi(val.c_str());
41bool BitmapFont::parseFntContent(
const std::string& content) {
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);
53 if (line.compare(0, 7,
"common ") == 0) {
54 line_height_ =
static_cast<f32>(
extractInt(line,
"lineHeight"));
58 }
else if (line.compare(0, 5,
"page ") == 0) {
60 }
else if (line.compare(0, 5,
"char ") == 0) {
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"));
71 glyphs_[glyph.id] = glyph;
72 }
else if (line.compare(0, 8,
"kerning ") == 0) {
76 u64 key = (
static_cast<u64>(first) << 32) | second;
77 kerning_[key] = amount;
81 return !glyphs_.empty();
85 const std::string& basePath,
87 if (!parseFntContent(content)) {
92 if (page_file_.empty()) {
97 std::string texturePath = basePath.empty() ? page_file_ : basePath +
"/" + page_file_;
100 if (!texture_.isValid()) {
101 ES_LOG_ERROR(
"Failed to load BMFont texture: {}", texturePath);
107 tex_width_ = tex->getWidth();
108 tex_height_ = tex->getHeight();
116 u32 texWidth,
u32 texHeight) {
117 if (!parseFntContent(content)) {
123 tex_width_ = texWidth;
124 tex_height_ = texHeight;
129 u32 texWidth,
u32 texHeight,
130 const std::string& chars,
131 u32 charWidth,
u32 charHeight) {
133 tex_width_ = texWidth;
134 tex_height_ = texHeight;
135 line_height_ =
static_cast<f32>(charHeight);
136 base_ = line_height_;
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);
146 u32 cols = texWidth / charWidth;
149 for (
usize i = 0; i < chars.size();) {
152 u32 col = glyphIndex % cols;
153 u32 row = glyphIndex / cols;
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);
165 glyphs_[charCode] = glyph;
183 totalWidth += glyph->xAdvance + spacing;
187 return { totalWidth * fontSize, line_height_ * fontSize };
191 auto it = glyphs_.find(charCode);
192 return it != glyphs_.end() ? &it->second :
nullptr;
196 u64 key = (
static_cast<u64>(first) << 32) | second;
197 auto it = kerning_.find(key);
198 return it != kerning_.end() ? it->second : 0.0f;
Logging utilities for ESEngine.
#define ES_LOG_ERROR(...)
Logs an error message.
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.
static std::string extractValue(const std::string &line, const std::string &key)
constexpr u32 nextCodepoint(const char *data, usize length, usize &pos)
static i32 extractInt(const std::string &line, const std::string &key)
std::int32_t i32
Signed 32-bit integer.
std::uint64_t u64
Unsigned 64-bit integer.
std::size_t usize
Size type for indexing and sizes.
std::uint32_t u32
Unsigned 32-bit integer.
float f32
32-bit floating point