refactor(render): TextBackend owns its atlas, and staged glyphs carry a rotation basis

This commit is contained in:
Martino Ferrari committed 2026-09-28 09:57:28 +02:00
1 parent 90e17a0a11
commit e074f57ddf
23 files changed
+1073 -227

No files matched your search

+45 -22
View File
@@ -1,7 +1,7 @@
#pragma once
#include <plot/core/types.hpp>
#include <plot/math/vec2.hpp>
#include <plot/render/bitmap_font.hpp>
#include <plot/render/text.hpp>
#include <vector>
@@ -55,41 +55,64 @@ class LineBatch {
/// CPU-side staging for one instanced text draw call.
///
/// Like `LineBatch` it keeps its capacity across frames. Each staged glyph is
/// one instance: the glyph quad's top-left corner in pixels, top-left origin
/// like `plot::Rect`, plus the glyph's atlas texel origin, interleaved as
/// `x,y,u,v` — the layout the text shader declares (see buffer.hpp).
/// one instance: the glyph cell's top-left corner in pixels, the glyph rect's
/// normalised top-left in the atlas and the cell's rotation basis, interleaved as
/// `x,y,u,v,ax,ay` — the layout the text shader declares (see `TextInstance` in
/// buffer.hpp).
///
/// Every glyph a batch stages is read through one atlas rect of one size — its
/// cell — so that size is recorded once (`glyph_cell_uv()`) and handed to the
/// draw, rather than riding along with every instance.
class TextBatch {
public:
/// Starts a frame: empties the batch, keeps its capacity.
/// Starts a frame: empties the batch and the staged cell, keeps the capacity.
void begin() noexcept;
/// Same as `begin()`.
void clear() noexcept;
/// Stages one glyph cell at `pos_px` (its top-left corner), reading the cell
/// from `slot` in the font atlas (see `BitmapFont::atlas_origin`). `slot`
/// indexes the atlas, not a codepoint.
void add_glyph(Vec2 pos_px, u32 slot);
/// Stages every codepoint of `utf8` and returns the pen past the last glyph,
/// so the run is exactly `font.measure(utf8) * px_per_cell /
/// BitmapFont::kGlyphPx` pixels wide — the width the draw covers when it is
/// told the same cell size as `u_px_range`.
/// Stages one glyph cell whose *pen* is `pen_px`: the cell's top-left corner is
/// the pen moved by the glyph's bearing (see `Glyph`), scaled and measured
/// along the run's own axes, so a glyph keeps its place inside a rotated run.
///
/// `px_per_cell` is the on-screen size of one glyph cell: pass
/// `BitmapFont::kGlyphPx` for 1:1 text, and anything else to scale the run.
/// The glyph quad itself is sized by the shader's `u_px_range`, which must be
/// the same value. A null or empty string stages nothing and returns `pen_px`.
Vec2 add_text(Vec2 pen_px, const char* utf8, const BitmapFont& font,
f32 px_per_cell = static_cast<f32>(BitmapFont::kGlyphPx));
/// `px` is the scale: on-screen pixels per font pixel, the factor `measure()`
/// is multiplied by — `1.0` draws the glyphs at the size the backend
/// rasterised them. `axis` is the direction the run advances in, in pixel
/// space; it rotates the cell (see `TextInstance`), and is normalised here, so
/// any non-zero direction works and a zero or non-finite one means "unrotated".
void add_glyph(Vec2 pen_px, const Glyph& glyph, f32 px = 1.0f,
Vec2 axis = {1.0f, 0.0f});
/// Stages every codepoint of `utf8` from `pen_px` and returns the pen past the
/// last glyph, so the run is exactly `font.measure(utf8) * px` long measured
/// along `axis` — the length the plot layer reserved for the label, at the size
/// the text pipeline then draws it.
///
/// The codepoints and the advances come from the backend itself, through
/// `next_codepoint()` and `glyph()`, so a run stages exactly what `measure()`
/// summed. A codepoint the backend has no glyph for stages nothing and moves
/// the pen nowhere — a run is never wider than the glyphs it can draw. A null
/// or empty string stages nothing and returns `pen_px`.
Vec2 add_text(const char* utf8, Vec2 pen_px, f32 px, const TextBackend& font,
Vec2 axis = {1.0f, 0.0f});
/// Glyphs staged so far, one instance each.
u32 glyph_count() const noexcept { return static_cast<u32>(v_.size() / 4u); }
u32 glyph_count() const noexcept { return static_cast<u32>(v_.size() / 6u); }
/// Interleaved `x,y,u,v`, four floats per staged glyph.
/// Interleaved `x,y,u,v,ax,ay`, six floats per staged glyph.
const std::vector<f32>& data() const noexcept { return v_; }
/// The staged glyphs' cell in the atlas, normalised: `(u1 - u0, v1 - v0)` of
/// the glyphs staged, which the draw passes as `u_glyph_uv` (see
/// `TextProgram::set_glyph_uv`). `(0, 0)` while nothing is staged.
Vec2 glyph_cell_uv() const noexcept { return cell_uv_; }
private:
/// Stages one cell whose basis the caller has already normalised, so a run and
/// the pen it advances share one rounding of the axis.
void add_cell(Vec2 pen_px, const Glyph& glyph, f32 px, Vec2 unit_ux);
std::vector<f32> v_;
Vec2 cell_uv_{};
};
} // namespace plot::render