Files
aee-arcade/source/core/rendering/text.hpp
T
JailDesignerandClaude Opus 4.7 a63deac6a3 refactor(core/rendering): deduplicar Text::write* i nomenar magic numbers (DUP-03 + HC-01)
Text tenia cinc mètodes (write, writeColored, writeMonoDigits, length,
lengthMonoDigits) i dos getters (glyphWidth, getGlyphClip) que repetien
el mateix patró de resolució de codepoint: find(cp), fallback a '?' si
no hi és. Extret a un helper privat resolveGlyph(cp) que centralitza
aquesta lògica; totes les funcions passen a usar-lo.

write() ara delega directament a writeColored(...) passant el color per
defecte, eliminant ~19 línies del bucle UTF-8 duplicades.

Els literals 1 (índex de "tinta" al PNG) i 15 (color per defecte a la
paleta) passen a ser Text::INK_COLOR_INDEX i Text::DEFAULT_TEXT_COLOR
(HC-01).

Build + smoke 5s xvfb OK (exit 124).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-20 09:32:37 +02:00

87 lines
3.7 KiB
C++

#pragma once
#include <SDL3/SDL.h>
#include <cstdint>
#include <memory>
#include <string>
#include <unordered_map>
#include "core/rendering/sprite/sprite.hpp"
class Surface;
// Bitmap font amb suport UTF-8. Els glifs es guarden en una única graella
// al png, amb ample variable per caràcter i una tinta monocroma que es
// recolora al vol via Sprite::render(src, tgt).
class Text {
public:
struct Offset {
int x{0};
int y{0};
int w{0};
};
struct File {
int box_width{0};
int box_height{0};
int columns{16};
int cell_spacing{0};
int row_spacing{0};
std::unordered_map<std::uint32_t, Offset> offset;
};
// Flags per a writeDX (composables amb |).
static constexpr Uint8 COLOR_FLAG = 1;
static constexpr Uint8 SHADOW_FLAG = 2;
static constexpr Uint8 CENTER_FLAG = 4;
static constexpr Uint8 STROKE_FLAG = 8;
// Índex del píxel "tinta" al PNG de la font. Els glifs són monocroms
// sobre transparent i tots els píxels opacs usen aquest índex; la Surface
// el recolora al vol per a emetre el color destí.
static constexpr Uint8 INK_COLOR_INDEX = 1;
// Color per defecte a la paleta quan s'usa write() sense especificar-lo.
static constexpr Uint8 DEFAULT_TEXT_COLOR = 15;
Text(const std::shared_ptr<Surface>& surface, const std::string& text_file);
Text(const std::shared_ptr<Surface>& surface, const std::shared_ptr<File>& text_file);
~Text() = default;
Text(const Text&) = delete;
Text(Text&&) = delete;
auto operator=(const Text&) -> Text& = delete;
auto operator=(Text&&) -> Text& = delete;
void write(int x, int y, const std::string& text, int kerning = 1, int length = -1);
void writeColored(int x, int y, const std::string& text, Uint8 color, int kerning = 1, int length = -1);
void writeShadowed(int x, int y, const std::string& text, Uint8 color, Uint8 shadow_distance = 1, int kerning = 1, int length = -1);
void writeCentered(int x, int y, const std::string& text, int kerning = 1, int length = -1);
void writeDX(Uint8 flags, int x, int y, const std::string& text, int kerning = 1, Uint8 text_color = 0, Uint8 shadow_distance = 1, Uint8 shadow_color = 0, int length = -1);
// Dibuixa amb amplada fixa només per als dígits '0'-'9' (cadascun centrat
// dins d'una cel·la de digit_cell_w). La resta de glifs mantenen amplada
// natural. Evita jitter en comptadors tipus "9:59.59".
void writeMonoDigits(int x, int y, const std::string& text, Uint8 color, int digit_cell_w, int kerning = 1);
[[nodiscard]] auto length(const std::string& text, int kerning = 1) const -> int;
[[nodiscard]] auto lengthMonoDigits(const std::string& text, int digit_cell_w, int kerning = 1) const -> int;
[[nodiscard]] auto getCharacterSize() const -> int { return box_width_; }
[[nodiscard]] auto glyphWidth(std::uint32_t codepoint, int kerning = 0) const -> int;
[[nodiscard]] auto getGlyphClip(std::uint32_t codepoint) const -> SDL_FRect;
[[nodiscard]] auto getSprite() const -> Sprite* { return sprite_.get(); }
static auto loadTextFile(const std::string& file_path) -> std::shared_ptr<File>;
static auto codepointToUtf8(std::uint32_t cp) -> std::string;
static auto nextCodepoint(const std::string& s, std::size_t& pos) -> std::uint32_t;
private:
// Busca l'Offset del codepoint; si no hi és, cau al glif '?' com a
// substitut. Retorna nullptr si tampoc hi ha '?'.
[[nodiscard]] auto resolveGlyph(std::uint32_t cp) const -> const Offset*;
std::unique_ptr<Sprite> sprite_;
int box_width_{0};
int box_height_{0};
std::unordered_map<std::uint32_t, Offset> offset_;
};