From 9cb6e4c1053f75accb0e580ac38d5668253a5450 Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Fri, 10 Dec 2021 16:59:32 +0000 Subject: [PATCH] Cleanups Font and FontManager a little. This was SVN commit r26050. --- source/graphics/Font.h | 6 +- source/graphics/FontManager.cpp | 104 ++++++++++++++++++-------------- 2 files changed, 63 insertions(+), 47 deletions(-) diff --git a/source/graphics/Font.h b/source/graphics/Font.h index bfc3915d90..98baa6a81c 100644 --- a/source/graphics/Font.h +++ b/source/graphics/Font.h @@ -26,8 +26,6 @@ */ class CFont { - friend class CFontManager; - CFont() {} public: struct GlyphData { @@ -80,6 +78,10 @@ public: CTexturePtr GetTexture() const { return m_Texture; } private: + friend class CFontManager; + + CFont() = default; + CTexturePtr m_Texture; bool m_HasRGB; // true if RGBA, false if ALPHA diff --git a/source/graphics/FontManager.cpp b/source/graphics/FontManager.cpp index 0521084119..1933323961 100644 --- a/source/graphics/FontManager.cpp +++ b/source/graphics/FontManager.cpp @@ -27,7 +27,7 @@ #include "ps/Filesystem.h" #include "renderer/Renderer.h" -#include +#include std::shared_ptr CFontManager::LoadFont(CStrIntern fontName) { @@ -55,77 +55,91 @@ bool CFontManager::ReadFont(CFont* font, CStrIntern fontName) const VfsPath path(L"fonts/"); // Read font definition file into a stringstream - std::shared_ptr buf; + std::shared_ptr buffer; size_t size; const VfsPath fntName(fontName.string() + ".fnt"); - if (g_VFS->LoadFile(path / fntName, buf, size) < 0) + if (g_VFS->LoadFile(path / fntName, buffer, size) < 0) { LOGERROR("Failed to open font file %s", (path / fntName).string8()); return false; } - std::istringstream FNTStream(std::string((const char*)buf.get(), size)); + std::istringstream fontStream( + std::string(reinterpret_cast(buffer.get()), size)); - int Version; - FNTStream >> Version; - if (Version != 101) // Make sure this is from a recent version of the font builder + int version; + fontStream >> version; + // Make sure this is from a recent version of the font builder. + if (version != 101) { LOGERROR("Font %s has invalid version", fontName.c_str()); - return 0; + return false; } - int TextureWidth, TextureHeight; - FNTStream >> TextureWidth >> TextureHeight; + int textureWidth, textureHeight; + fontStream >> textureWidth >> textureHeight; - std::string Format; - FNTStream >> Format; - if (Format == "rgba") + std::string format; + fontStream >> format; + if (format == "rgba") font->m_HasRGB = true; - else if (Format == "a") + else if (format == "a") font->m_HasRGB = false; else - debug_warn(L"Invalid .fnt format string"); - - int NumGlyphs; - FNTStream >> NumGlyphs; - - FNTStream >> font->m_LineSpacing; - FNTStream >> font->m_Height; - - font->m_BoundsX0 = FLT_MAX; - font->m_BoundsY0 = FLT_MAX; - font->m_BoundsX1 = -FLT_MAX; - font->m_BoundsY1 = -FLT_MAX; - - for (int i = 0; i < NumGlyphs; ++i) { - int Codepoint, TextureX, TextureY, Width, Height, OffsetX, OffsetY, Advance; - FNTStream >> Codepoint>>TextureX>>TextureY>>Width>>Height>>OffsetX>>OffsetY>>Advance; + LOGWARNING("Invalid .fnt format string"); + return false; + } - if (Codepoint < 0 || Codepoint > 0xFFFF) + int mumberOfGlyphs; + fontStream >> mumberOfGlyphs; + + fontStream >> font->m_LineSpacing; + fontStream >> font->m_Height; + + font->m_BoundsX0 = std::numeric_limits::max(); + font->m_BoundsY0 = std::numeric_limits::max(); + font->m_BoundsX1 = -std::numeric_limits::max(); + font->m_BoundsY1 = -std::numeric_limits::max(); + + for (int i = 0; i < mumberOfGlyphs; ++i) + { + int codepoint, textureX, textureY, width, height, offsetX, offsetY, advance; + fontStream >> codepoint + >> textureX >> textureY >> width >> height + >> offsetX >> offsetY >> advance; + + if (codepoint < 0 || codepoint > 0xFFFF) { - LOGWARNING("Font %s has invalid codepoint 0x%x", fontName.c_str(), Codepoint); + LOGWARNING("Font %s has invalid codepoint 0x%x", fontName.c_str(), codepoint); continue; } - float u = (float)TextureX / (float)TextureWidth; - float v = (float)TextureY / (float)TextureHeight; - float w = (float)Width / (float)TextureWidth; - float h = (float)Height / (float)TextureHeight; + const float u = static_cast(textureX) / textureWidth; + const float v = static_cast(textureY) / textureHeight; + const float w = static_cast(width) / textureWidth; + const float h = static_cast(height) / textureHeight; - CFont::GlyphData g = { u, -v, u+w, -v+h, (i16)OffsetX, (i16)-OffsetY, (i16)(OffsetX+Width), (i16)(-OffsetY+Height), (i16)Advance }; - font->m_Glyphs.set((u16)Codepoint, g); + CFont::GlyphData g = + { + u, -v, u + w, -v + h, + static_cast(offsetX), static_cast(-offsetY), + static_cast(offsetX + width), static_cast(-offsetY + height), + static_cast(advance) + }; + font->m_Glyphs.set(static_cast(codepoint), g); - font->m_BoundsX0 = std::min(font->m_BoundsX0, (float)g.x0); - font->m_BoundsY0 = std::min(font->m_BoundsY0, (float)g.y0); - font->m_BoundsX1 = std::max(font->m_BoundsX1, (float)g.x1); - font->m_BoundsY1 = std::max(font->m_BoundsY1, (float)g.y1); + font->m_BoundsX0 = std::min(font->m_BoundsX0, static_cast(g.x0)); + font->m_BoundsY0 = std::min(font->m_BoundsY0, static_cast(g.y0)); + font->m_BoundsX1 = std::max(font->m_BoundsX1, static_cast(g.x1)); + font->m_BoundsY1 = std::max(font->m_BoundsY1, static_cast(g.y1)); } - ENSURE(font->m_Height); // Ensure the height has been found (which should always happen if the font includes an 'I') + // Ensure the height has been found (which should always happen if the font includes an 'I'). + ENSURE(font->m_Height); // Load glyph texture - const VfsPath imgName(fontName.string() + ".png"); - CTextureProperties textureProps(path / imgName); + const VfsPath imageName(fontName.string() + ".png"); + CTextureProperties textureProps(path / imageName); textureProps.SetFilter(GL_LINEAR); if (!font->m_HasRGB) textureProps.SetFormatOverride(GL_ALPHA);