14 constexpr uint32_t k_grow_height_multiplier = 8;
24 struct CompositeResult {
35 GlyphAtlas* resolve_atlas(GlyphAtlas* hint)
43 "InkPress: no atlas available -- call set_default_font first");
62 std::optional<CompositeResult> composite(
63 std::string_view text,
68 float pen_y_start = 0.F)
70 const LayoutResult layout =
lay_out(text, atlas, 0.F, pen_y_start, buf_w);
71 if (layout.quads.empty()) {
75 const auto content_h =
static_cast<uint32_t
>(std::ceil(layout.final_pen_y))
76 + atlas.line_height();
77 const uint32_t dst_h = std::min(content_h, buf_h);
79 if (buf_w == 0 || dst_h == 0) {
83 CompositeResult result;
85 result.cursor_x =
static_cast<uint32_t
>(std::ceil(layout.final_pen_x));
86 result.cursor_y =
static_cast<uint32_t
>(std::ceil(layout.final_pen_y));
87 result.pixels.resize(
static_cast<size_t>(buf_w) * dst_h * 4, 0);
106 std::shared_ptr<Buffers::TextBuffer> make_buffer(
107 const CompositeResult& result,
110 glm::uvec2 render_bounds,
111 std::string_view text)
113 budget_h = std::max(budget_h, result.h);
115 const size_t budget_bytes =
static_cast<size_t>(buf_w) * budget_h * 4;
116 std::vector<uint8_t>
pixels(budget_bytes, 0);
118 const uint32_t copy_h = std::min(result.h,
119 static_cast<uint32_t
>(result.pixels.size() / (buf_w * 4)));
120 for (uint32_t row = 0; row < copy_h; ++row) {
122 pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
123 result.pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
124 static_cast<size_t>(buf_w) * 4);
127 auto buf = std::make_shared<Buffers::TextBuffer>(
132 buf->set_budget(buf_w, budget_h);
133 buf->set_render_bounds(render_bounds.x, render_bounds.y);
134 buf->set_accumulated_text(text);
135 buf->get_cursor_x() = result.cursor_x;
136 buf->get_cursor_y() = result.cursor_y;
139 "InkPress: {}x{} content in {}x{} texture, render bounds {}x{}",
140 buf_w, result.h, buf_w, budget_h, render_bounds.x, render_bounds.y);
148 std::span<const GlyphQuad> quads,
155 const uint8_t cr =
static_cast<uint8_t
>(std::clamp(
color.r, 0.F, 1.F) * 255.F);
156 const uint8_t cg =
static_cast<uint8_t
>(std::clamp(
color.g, 0.F, 1.F) * 255.F);
157 const uint8_t cb =
static_cast<uint8_t
>(std::clamp(
color.b, 0.F, 1.F) * 255.F);
158 const uint8_t ca =
static_cast<uint8_t
>(std::clamp(
color.a, 0.F, 1.F) * 255.F);
161 const std::span<const uint8_t> atlas_pixels = atlas_tex.
pixel_bytes(0);
162 const uint32_t atlas_size = atlas.
atlas_size();
164 for (
const auto&
q : quads) {
165 const auto gx =
static_cast<int32_t
>(std::floor(
q.x0));
166 const auto gy =
static_cast<int32_t
>(std::floor(
q.y0));
167 const auto gw =
static_cast<uint32_t
>(std::ceil(
q.x1 -
q.x0));
168 const auto gh =
static_cast<uint32_t
>(std::ceil(
q.y1 -
q.y0));
170 const auto src_x =
static_cast<uint32_t
>(
q.uv_x0 *
static_cast<float>(atlas_size));
171 const auto src_y =
static_cast<uint32_t
>(
q.uv_y0 *
static_cast<float>(atlas_size));
173 for (uint32_t row = 0; row < gh; ++row) {
174 const int32_t dst_row = gy +
static_cast<int32_t
>(row);
175 if (dst_row < 0 ||
static_cast<uint32_t
>(dst_row) >= buf_h) {
179 const uint8_t* src_row = atlas_pixels.data()
180 +
static_cast<size_t>(src_y + row) * atlas_size + src_x;
181 uint8_t* dst_row_ptr = dst +
static_cast<size_t>(dst_row) * buf_w * 4;
183 for (uint32_t col = 0; col < gw; ++col) {
184 const int32_t dst_col = gx +
static_cast<int32_t
>(col);
185 if (dst_col < 0 ||
static_cast<uint32_t
>(dst_col) >= buf_w) {
189 const uint8_t coverage = src_row[col];
190 const auto alpha =
static_cast<uint8_t
>(
191 (
static_cast<uint32_t
>(coverage) *
static_cast<uint32_t
>(ca)) / 255U);
193 uint8_t* px = dst_row_ptr +
static_cast<size_t>(dst_col) * 4;
204 const std::shared_ptr<Buffers::TextBuffer>& target,
205 std::span<const GlyphQuad> quads,
210 "ink_quads: target buffer is null");
219 const uint32_t buf_w = target->get_budget_width();
220 const uint32_t buf_h = target->get_budget_height();
221 const size_t buf_bytes =
static_cast<size_t>(buf_w) * buf_h * 4;
223 thread_local std::vector<uint8_t>
pixels;
224 pixels.assign(buf_bytes, 0);
227 target->set_pixel_data(
pixels.data(), buf_bytes);
234std::shared_ptr<Buffers::TextBuffer>
press(
235 std::string_view text,
245 const auto result = composite(text, *atlas, params.
color, buf_w, params.
render_bounds.y,
249 "press: no glyphs produced for '{}'", std::string(text));
253 const uint32_t budget_h = params.
budget_h > 0
254 ? std::max(params.
budget_h, result->h)
255 : std::min(result->h * k_grow_height_multiplier, params.
render_bounds.y);
257 return make_buffer(*result, buf_w, budget_h, params.
render_bounds, text);
265 const std::shared_ptr<Buffers::TextBuffer>& target,
266 std::string_view text,
272 "repress: target buffer is null");
281 target->clear_accumulated_text();
282 target->reset_cursor();
284 const uint32_t buf_w = target->get_budget_width();
285 const uint32_t buf_h = target->get_budget_height();
286 const uint32_t bound_h = target->get_render_bounds_h();
288 const auto result = composite(text, *atlas,
color, buf_w, bound_h,
292 "repress: no glyphs produced for '{}'", std::string(text));
297 const uint32_t new_h = std::min(result->h, bound_h);
298 target->resize_texture(buf_w, new_h);
299 target->set_budget(buf_w, new_h);
300 target->set_pixel_data(result->pixels.data(), result->pixels.size());
301 target->get_cursor_x() = result->cursor_x;
302 target->get_cursor_y() = result->cursor_y;
303 target->set_accumulated_text(text);
306 "repress(Fit): resized to {}x{}", buf_w, new_h);
310 const size_t buf_bytes =
static_cast<size_t>(buf_w) * buf_h * 4;
311 std::vector<uint8_t> cleared(buf_bytes, 0);
313 const uint32_t copy_h = std::min(result->h, buf_h);
314 for (uint32_t row = 0; row < copy_h; ++row) {
316 cleared.data() +
static_cast<size_t>(row) * buf_w * 4,
317 result->pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
318 static_cast<size_t>(buf_w) * 4);
321 target->set_pixel_data(cleared.data(), buf_bytes);
322 target->get_cursor_x() = result->cursor_x;
323 target->get_cursor_y() = result->cursor_y;
325 target->set_accumulated_text(text);
328 "repress(Clip): '{}' -> {}x{} into {}x{} budget",
329 std::string(text), buf_w, result->h, buf_w, buf_h);
339 const std::shared_ptr<Buffers::TextBuffer>& target,
340 std::string_view text,
345 "impress: target buffer is null");
354 const uint32_t buf_w = target->get_budget_width();
355 const uint32_t buf_h = target->get_budget_height();
356 const uint32_t bound_h = target->get_render_bounds_h();
357 const auto pen_x =
static_cast<float>(target->get_cursor_x());
358 const auto pen_y =
static_cast<float>(target->get_cursor_y());
360 target->append_accumulated_text(text);
364 if (layout.
quads.empty()) {
366 "impress: no glyphs produced for '{}'", std::string(text));
370 const auto pen_y_ceil =
static_cast<uint32_t
>(std::ceil(layout.
final_pen_y));
371 const uint32_t new_cursor_y = pen_y_ceil;
372 const uint32_t content_h = pen_y_ceil + atlas->
line_height();
374 if (content_h > bound_h) {
378 if (content_h > buf_h) {
379 const uint32_t new_h = std::min(content_h * k_grow_height_multiplier, bound_h);
380 const std::string accumulated = target->get_accumulated_text();
382 const auto full = composite(accumulated, *atlas,
color, buf_w, new_h);
387 target->resize_texture(buf_w, new_h);
388 target->set_budget(buf_w, new_h);
390 const size_t buf_bytes =
static_cast<size_t>(buf_w) * new_h * 4;
391 std::vector<uint8_t>
pixels(buf_bytes, 0);
392 const uint32_t copy_h = std::min(full->h, new_h);
393 for (uint32_t row = 0; row < copy_h; ++row) {
395 pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
396 full->pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
397 static_cast<size_t>(buf_w) * 4);
400 target->set_pixel_data(
pixels.data(), buf_bytes);
401 target->get_cursor_x() = full->cursor_x;
402 target->get_cursor_y() = full->h;
403 target->set_accumulated_text(accumulated);
406 "impress: vertical grow -> {}x{}", buf_w, new_h);
411 auto& pixel_data = target->get_pixel_data_mutable();
413 target->mark_pixels_dirty();
414 target->get_cursor_x() =
static_cast<uint32_t
>(std::ceil(layout.
final_pen_x));
415 target->get_cursor_y() = new_cursor_y;
418 "impress: '{}' at ({},{}) -> cursor ({},{})",
420 static_cast<uint32_t
>(pen_x),
static_cast<uint32_t
>(pen_y),
421 target->get_cursor_x(), target->get_cursor_y());
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
const std::vector< float > * pixels
std::optional< glm::vec3 > color
std::span< const uint8_t > pixel_bytes(uint32_t layer=0) const
Read-only byte-level view over the pixel buffer.
SignalSourceContainer wrapping GPU texture data as addressable pixel bytes.
uint32_t line_height() const
Line advance in pixels for this atlas's pixel_size.
uint32_t atlas_size() const
Atlas texture dimension (width == height == atlas_size).
const Kakshya::TextureContainer & texture() const
The atlas texture as a TextureContainer (R8, atlas_size x atlas_size).
Rasterizes and packs glyphs from a FontFace into a TextureContainer.
GlyphAtlas * get_default_glyph_atlas() const
Return the default GlyphAtlas, or nullptr if set_default_font() has not been called successfully.
static TypeFaceFoundry & instance()
@ API
API calls from external code.
@ Portal
High-level user-facing API layer.
@ RGBA8
Four channel 8-bit.
ImpressResult impress(const std::shared_ptr< Buffers::TextBuffer > &target, std::string_view text, glm::vec4 color)
Append a UTF-8 string into an existing TextBuffer at the current cursor.
void rasterize_quads(std::span< const GlyphQuad > quads, GlyphAtlas &atlas, glm::vec4 color, uint8_t *dst, uint32_t buf_w, uint32_t buf_h)
Write glyph quads into a caller-provided RGBA8 pixel buffer.
RedrawPolicy
Policy controlling TextBuffer reuse behaviour in repress().
@ Fit
Replace content. Reallocate GPU texture if text exceeds existing budget.
ImpressResult
Result of an impress() call.
@ Overflow
Vertical budget exceeded. Texture reallocated. Previous content cleared.
@ Ok
Run composited at cursor. No GPU state change.
void ink_quads(const std::shared_ptr< Buffers::TextBuffer > &target, std::span< const GlyphQuad > quads, glm::vec4 color)
Rasterize a mutated quad span into an existing TextBuffer.
LayoutResult lay_out(std::string_view text, GlyphAtlas &atlas, float pen_x, float pen_y, uint32_t wrap_w)
Lay out a UTF-8 string into a sequence of screen-space quads.
bool repress(const std::shared_ptr< Buffers::TextBuffer > &target, std::string_view text, glm::vec4 color, RedrawPolicy policy)
Re-composite a UTF-8 string into an existing TextBuffer.
std::shared_ptr< Buffers::TextBuffer > press(std::string_view text, const PressParams ¶ms)
Composite a UTF-8 string into a new TextBuffer.
std::vector< GlyphQuad > quads
Result of lay_out(), carrying the quads and the final pen position.
GlyphAtlas * atlas
Glyph atlas to use. Null selects the TypeFaceFoundry default at call time.
glm::vec4 color
RGBA color applied to all glyphs.
uint32_t budget_h
Initial vertical budget in pixels. Zero applies the grow heuristic.
glm::uvec2 render_bounds
Hard render bounds in pixels.
Construction parameters for press().