#include "ui/TextEngine.hpp" #include #include #include #include #include #include "core/Assets.hpp" #include "core/Log.hpp" namespace dp::ui { namespace { std::uint32_t decodeCp(const std::string& s, std::size_t& i) { const unsigned char c = static_cast(s[i]); std::uint32_t cp = c; std::size_t n = 1; if (c >= 0xF0) { cp = c & 0x07; n = 4; } else if (c >= 0xE0) { cp = c & 0x0F; n = 3; } else if (c >= 0xC0) { cp = c & 0x1F; n = 2; } for (std::size_t k = 1; k < n && i + k < s.size(); ++k) cp = (cp << 6) | (static_cast(s[i + k]) & 0x3F); i += std::min(n, s.size() - i); return cp; } bool emojiCp(std::uint32_t cp) { return (cp >= 0x1F000 && cp <= 0x1FAFF) || (cp >= 0x2600 && cp <= 0x27BF) || cp == 0x200D || cp == 0xFE0F || cp == 0x20E3 || (cp >= 0xE0020 && cp <= 0xE007F) || (cp >= 0x2B50 && cp <= 0x2B55) || cp == 0x2B1B || cp == 0x2B1C || cp == 0x2764 || cp == 0x203C || cp == 0x2049 || (cp >= 0x23E9 && cp <= 0x23FA) || cp == 0x231A || cp == 0x231B || cp == 0x2122 || cp == 0x2139 || (cp >= 0x2194 && cp <= 0x21AA); } struct Segment { std::string text; bool emoji; }; std::vector segments(const std::string& s) { std::vector out; std::size_t i = 0; while (i < s.size()) { const std::size_t start = i; const std::uint32_t cp = decodeCp(s, i); bool em = emojiCp(cp); if (em && cp == 0xFE0F && !out.empty() && !out.back().emoji) { // Variation selector after a text symbol: attach it to the symbol as emoji presentation. std::size_t last = out.back().text.size(); std::size_t p = last; while (p > 0 && (static_cast(out.back().text[p - 1]) & 0xC0) == 0x80) --p; if (p > 0) --p; const std::string sym = out.back().text.substr(p); out.back().text.resize(p); if (out.back().text.empty()) out.pop_back(); out.push_back({sym + s.substr(start, i - start), true}); continue; } if (!out.empty() && out.back().emoji == em) out.back().text += s.substr(start, i - start); else out.push_back({s.substr(start, i - start), em}); } return out; } } // namespace TextEngine& TextEngine::instance() { static TextEngine engine; return engine; } bool TextEngine::init() { if (ready_) return true; if (!TTF_WasInit() && !TTF_Init()) { Log::error(std::string("TTF_Init failed: ") + SDL_GetError()); return false; } ready_ = true; return true; } void TextEngine::setScale(float scale) { if (scale == scale_) return; scale_ = scale; clearRenderCache(); for (auto& kv : fonts_) TTF_CloseFont(kv.second); fonts_.clear(); } void TextEngine::clearRenderCache() { render_.clear(); renderBytes_ = 0; } TTF_Font* TextEngine::open(const FontSpec& spec, float pixelSize) { const std::string key = spec.key() + "@" + std::to_string(static_cast(pixelSize * 100)); auto it = fonts_.find(key); if (it != fonts_.end()) return it->second; const char* file = "fonts/Inter-Regular.ttf"; int style = TTF_STYLE_NORMAL; if (spec.mono) { file = "fonts/DejaVuSansMono.ttf"; if (static_cast(spec.weight) >= 600) style |= TTF_STYLE_BOLD; if (spec.italic) style |= TTF_STYLE_ITALIC; } else if (spec.italic && static_cast(spec.weight) < 600) { file = "fonts/Inter-Italic.ttf"; } else { switch (spec.weight) { case Weight::Regular: file = "fonts/Inter-Regular.ttf"; break; case Weight::Medium: file = "fonts/Inter-Medium.ttf"; break; case Weight::Semibold: file = "fonts/Inter-SemiBold.ttf"; break; case Weight::Bold: file = "fonts/Inter-Bold.ttf"; break; } if (spec.italic) style |= TTF_STYLE_ITALIC; } TTF_Font* font = TTF_OpenFont(Assets::path(file).c_str(), pixelSize); if (!font) { Log::error(std::string("font open failed: ") + file + ": " + SDL_GetError()); fonts_[key] = nullptr; return nullptr; } TTF_SetFontStyle(font, style); TTF_SetFontHinting(font, TTF_HINTING_LIGHT); fonts_[key] = font; return font; } float TextEngine::width(const FontSpec& spec, const std::string& utf8) { if (utf8.empty()) return 0; const std::string key = spec.key() + "|" + utf8; auto it = measure_.find(key); if (it != measure_.end()) return it->second; TTF_Font* font = open(spec, spec.size * kMeasureFactor); float result = 0; if (font) { for (const Segment& seg : segments(utf8)) { if (seg.emoji) { result += spec.size * kEmojiAdvance; continue; } int w = 0, h = 0; if (TTF_GetStringSize(font, seg.text.c_str(), seg.text.size(), &w, &h)) result += static_cast(w) / kMeasureFactor; } } if (measure_.size() >= kMaxMeasureEntries) measure_.clear(); measure_[key] = result; return result; } float TextEngine::ascent(const FontSpec& spec) { TTF_Font* font = open(spec, spec.size * kMeasureFactor); return font ? static_cast(TTF_GetFontAscent(font)) / kMeasureFactor : spec.size * 0.95f; } float TextEngine::descent(const FontSpec& spec) { TTF_Font* font = open(spec, spec.size * kMeasureFactor); return font ? -static_cast(TTF_GetFontDescent(font)) / kMeasureFactor : spec.size * 0.25f; } BitmapPtr TextEngine::render(const FontSpec& spec, Color color, const std::string& utf8) { if (utf8.empty()) return nullptr; char colorKey[16]; std::snprintf(colorKey, sizeof colorKey, "%02x%02x%02x", color.r, color.g, color.b); const std::string key = spec.key() + "|" + colorKey + "|" + std::to_string(static_cast(scale_ * 100)) + "|" + utf8; auto it = render_.find(key); if (it != render_.end()) return it->second; TTF_Font* font = open(spec, spec.size * scale_); if (!font) return nullptr; const int height = std::max(1, TTF_GetFontHeight(font)); struct Piece { BitmapPtr bmp; int x; }; std::vector pieces; int cursor = 0; for (const Segment& seg : segments(utf8)) { BitmapPtr piece = seg.emoji ? renderEmoji(seg.text, spec.size * scale_, height) : renderText(font, color, seg.text); if (!piece) continue; pieces.push_back({piece, cursor}); cursor += seg.emoji ? static_cast(std::lround(spec.size * kEmojiAdvance * scale_)) : piece->width(); } if (pieces.empty()) return nullptr; auto bmp = std::make_shared(std::max(1, cursor), height); for (const Piece& pc : pieces) { const int dy = (height - pc.bmp->height()) / 2; for (int y = 0; y < pc.bmp->height(); ++y) { const int ty = y + dy; if (ty < 0 || ty >= height) continue; for (int x = 0; x < pc.bmp->width(); ++x) { const int tx = x + pc.x; if (tx >= bmp->width()) break; bmp->data()[static_cast(ty) * static_cast(bmp->width()) + static_cast(tx)] = pc.bmp->at(x, y); } } } if (render_.size() >= kMaxRenderEntries || renderBytes_ + bmp->bytes() > kMaxRenderBytes) clearRenderCache(); renderBytes_ += bmp->bytes(); render_[key] = bmp; return bmp; } } // namespace dp::ui namespace dp::ui { BitmapPtr TextEngine::renderText(TTF_Font* font, Color color, const std::string& text) { SDL_Surface* raw = TTF_RenderText_Blended(font, text.c_str(), text.size(), SDL_Color{color.r, color.g, color.b, 255}); if (!raw) return nullptr; SDL_Surface* surf = raw->format == SDL_PIXELFORMAT_ARGB8888 ? raw : SDL_ConvertSurface(raw, SDL_PIXELFORMAT_ARGB8888); if (surf != raw) SDL_DestroySurface(raw); if (!surf) return nullptr; auto bmp = std::make_shared(surf->w, surf->h); for (int y = 0; y < surf->h; ++y) { const auto* row = reinterpret_cast(static_cast(surf->pixels) + static_cast(y) * static_cast(surf->pitch)); std::copy(row, row + surf->w, bmp->data() + static_cast(y) * static_cast(surf->w)); } SDL_DestroySurface(surf); return bmp; } BitmapPtr TextEngine::renderEmoji(const std::string& text, float pixelSize, int lineHeightPx) { if (!emojiFont_) { emojiFont_ = TTF_OpenFont(Assets::path("fonts/NotoColorEmoji.ttf").c_str(), 109); if (!emojiFont_) return nullptr; } SDL_Surface* raw = TTF_RenderText_Blended(emojiFont_, text.c_str(), text.size(), SDL_Color{255, 255, 255, 255}); if (!raw) return nullptr; SDL_Surface* surf = raw->format == SDL_PIXELFORMAT_ARGB8888 ? raw : SDL_ConvertSurface(raw, SDL_PIXELFORMAT_ARGB8888); if (surf != raw) SDL_DestroySurface(raw); if (!surf) return nullptr; // Area-average downscale to the target emoji height (about 1.17 em), alpha-weighted. const float targetH = std::min(static_cast(lineHeightPx) * 1.0f, pixelSize * 1.17f); const float s = targetH / static_cast(surf->h); const int tw = std::max(1, static_cast(std::lround(static_cast(surf->w) * s))); const int th = std::max(1, static_cast(std::lround(targetH))); auto bmp = std::make_shared(tw, th); const auto* px = static_cast(surf->pixels); const int pitch = surf->pitch / 4; for (int y = 0; y < th; ++y) { const int sy0 = static_cast(static_cast(y) / s), sy1 = std::min(surf->h, std::max(sy0 + 1, static_cast(static_cast(y + 1) / s))); for (int x = 0; x < tw; ++x) { const int sx0 = static_cast(static_cast(x) / s), sx1 = std::min(surf->w, std::max(sx0 + 1, static_cast(static_cast(x + 1) / s))); double a = 0, r = 0, g = 0, b = 0; int count = 0; for (int yy = sy0; yy < sy1; ++yy) { for (int xx = sx0; xx < sx1; ++xx) { const std::uint32_t p = px[static_cast(yy) * static_cast(pitch) + static_cast(xx)]; const double pa = static_cast(p >> 24); a += pa; r += static_cast((p >> 16) & 0xFF) * pa; g += static_cast((p >> 8) & 0xFF) * pa; b += static_cast(p & 0xFF) * pa; ++count; } } if (count == 0 || a <= 0) continue; const std::uint32_t oa = static_cast(a / count + 0.5); bmp->data()[static_cast(y) * static_cast(tw) + static_cast(x)] = (oa << 24) | (static_cast(r / a + 0.5) << 16) | (static_cast(g / a + 0.5) << 8) | static_cast(b / a + 0.5); } } SDL_DestroySurface(surf); return bmp; } } // namespace dp::ui