#pragma once // Spec: SPEC.md UX-FONT-01, NFR-MEM-01. SDL3_ttf wrapper: measurement at a fixed 8x virtual size (layout is // independent of the window scale), rendering at device scale, bounded caches. #include #include #include "ui/Bitmap.hpp" #include "ui/Font.hpp" #include "ui/Geometry.hpp" struct TTF_Font; namespace dp::ui { class TextEngine { public: static TextEngine& instance(); bool init(); // loads nothing eagerly; verifies the bundled fonts exist void setScale(float scale); float scale() const { return scale_; } // Points. float width(const FontSpec& font, const std::string& utf8); float ascent(const FontSpec& font); float descent(const FontSpec& font); // Device-pixel bitmap, straight alpha, cached. Never null for non-empty text. BitmapPtr render(const FontSpec& font, Color color, const std::string& utf8); static constexpr std::size_t kMaxRenderEntries = 3000; static constexpr std::size_t kMaxRenderBytes = 96u * 1024u * 1024u; static constexpr std::size_t kMaxMeasureEntries = 40000; static constexpr int kMeasureFactor = 8; static constexpr float kEmojiAdvance = 1.17f; private: TextEngine() = default; TTF_Font* open(const FontSpec& font, float pixelSize); BitmapPtr renderText(TTF_Font* font, Color color, const std::string& text); BitmapPtr renderEmoji(const std::string& text, float pixelSize, int lineHeightPx); TTF_Font* emojiFont_ = nullptr; void clearRenderCache(); float scale_ = 1; bool ready_ = false; std::unordered_map fonts_; std::unordered_map measure_; std::unordered_map render_; std::size_t renderBytes_ = 0; }; } // namespace dp::ui