/* * Copyright (C) 2022 Riyyi * * SPDX-License-Identifier: MIT */ #pragma once #include // std::array #include // int32_t, uint32_t #include // std::shared_ptr #include // std::string #include // std::unordered_map #include // std::vector #include "glm/ext/vector_int2.hpp" // glm::ivec2 #include "glm/ext/vector_uint2.hpp" // glm::uvec2 #include "ruc/format/format.h" #include "ruc/singleton.h" #define PADDING 3 namespace Inferno { class Texture; struct Character { glm::uvec2 position; // Position glm::uvec2 size; // Width/height glm::ivec2 offset; // Offset from baseline to left / top of glyph uint32_t advance; // Amount to advance to next glyph std::unordered_map kernings; // Kernings for characters that come before this one }; // ------------------------------------- class Font { public: Font(const std::string& name); virtual ~Font() {} enum Padding { Top = 0, Right, Bottom, Left, }; inline std::string name() const { return m_name; } inline uint32_t size() const { return m_size; } inline uint32_t lineSpacing() const { return m_lineSpacing; } inline std::shared_ptr texture() const { return m_texture; } inline std::shared_ptr get(unsigned char c) const { return m_characterList.at(c); } inline std::shared_ptr operator[](unsigned char c) const { return m_characterList.at(c); } private: void parseFont(const std::string& font); std::string findAction(const std::string& line) const; std::vector findColumns(const std::string& line) const; std::string findValue(const std::string& key, const std::vector& columns) const; std::string m_name; uint32_t m_size = { 0 }; uint32_t m_lineSpacing = { 0 }; std::array m_padding = { 0 }; std::shared_ptr m_texture; std::unordered_map> m_characterList; }; // ------------------------------------- class FontManager final : public ruc::Singleton { public: FontManager(s); virtual ~FontManager(); void add(const std::string& name, std::shared_ptr font); std::shared_ptr load(const std::string& name); std::shared_ptr get(const std::string& name); bool exists(const std::string& name); void remove(const std::string& name); void remove(std::shared_ptr font); private: std::unordered_map> m_fontList; }; } // namespace Inferno template<> struct ruc::format::Formatter : Formatter> { void format(Builder& builder, glm::ivec2 value) const; }; // FontManager fm; // Font f = fm.load("path/to/font"); // Font f2("path/to/font"); // Character c = f['a']; // Look into using signed distance fields for texture map generation ? anti-aliasing for text // https://youtu.be/d8cfgcJR9Tk