17 constexpr uint32_t k_grow_height_multiplier = 8;
27 struct CompositeResult {
38 GlyphAtlas* resolve_atlas(GlyphAtlas* hint)
46 "InkPress: no atlas available -- call set_default_font first");
65 std::optional<CompositeResult> composite(
66 std::string_view text,
71 float pen_y_start = 0.F)
73 const LayoutResult layout =
lay_out(text, atlas, 0.F, pen_y_start, buf_w);
74 if (layout.quads.empty()) {
78 const auto content_h =
static_cast<uint32_t
>(std::ceil(layout.final_pen_y))
79 + atlas.line_height();
80 const uint32_t dst_h = std::min(content_h, buf_h);
82 if (buf_w == 0 || dst_h == 0) {
86 CompositeResult result;
88 result.cursor_x =
static_cast<uint32_t
>(std::ceil(layout.final_pen_x));
89 result.cursor_y =
static_cast<uint32_t
>(std::ceil(layout.final_pen_y));
90 result.pixels.resize(
static_cast<size_t>(buf_w) * dst_h * 4, 0);
92 rasterize_quads(layout.quads, atlas, color, result.pixels.data(), buf_w, dst_h);
109 std::shared_ptr<Buffers::TextBuffer> make_buffer(
110 const CompositeResult& result,
113 glm::uvec2 render_bounds,
114 std::string_view text)
116 budget_h = std::max(budget_h, result.h);
118 const size_t budget_bytes =
static_cast<size_t>(buf_w) * budget_h * 4;
119 std::vector<uint8_t>
pixels(budget_bytes, 0);
121 const uint32_t copy_h = std::min(result.h,
122 static_cast<uint32_t
>(result.pixels.size() / (
static_cast<size_t>(buf_w) * 4)));
123 for (uint32_t row = 0; row < copy_h; ++row) {
125 pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
126 result.pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
127 static_cast<size_t>(buf_w) * 4);
130 auto buf = std::make_shared<Buffers::TextBuffer>(
135 buf->set_budget(buf_w, budget_h);
136 buf->set_render_bounds(render_bounds.x, render_bounds.y);
137 buf->set_accumulated_text(text);
138 buf->get_cursor_x() = result.cursor_x;
139 buf->get_cursor_y() = result.cursor_y;
142 "InkPress: {}x{} content in {}x{} texture, render bounds {}x{}",
143 buf_w, result.h, buf_w, budget_h, render_bounds.x, render_bounds.y);
151 std::span<const GlyphQuad> quads,
158 const uint8_t cr =
static_cast<uint8_t
>(std::clamp(color.r, 0.F, 1.F) * 255.F);
159 const uint8_t cg =
static_cast<uint8_t
>(std::clamp(color.g, 0.F, 1.F) * 255.F);
160 const uint8_t cb =
static_cast<uint8_t
>(std::clamp(color.b, 0.F, 1.F) * 255.F);
161 const uint8_t ca =
static_cast<uint8_t
>(std::clamp(color.a, 0.F, 1.F) * 255.F);
164 const std::span<const uint8_t> atlas_pixels = atlas_tex.
pixel_bytes(0);
165 const uint32_t atlas_size = atlas.
atlas_size();
167 for (
const auto&
q : quads) {
168 const auto gx =
static_cast<int32_t
>(std::floor(
q.x0));
169 const auto gy =
static_cast<int32_t
>(std::floor(
q.y0));
170 const auto gw =
static_cast<uint32_t
>(std::ceil(
q.x1 -
q.x0));
171 const auto gh =
static_cast<uint32_t
>(std::ceil(
q.y1 -
q.y0));
173 const auto src_x =
static_cast<uint32_t
>(
q.uv_x0 *
static_cast<float>(atlas_size));
174 const auto src_y =
static_cast<uint32_t
>(
q.uv_y0 *
static_cast<float>(atlas_size));
176 for (uint32_t row = 0; row < gh; ++row) {
177 const int32_t dst_row = gy +
static_cast<int32_t
>(row);
178 if (dst_row < 0 ||
static_cast<uint32_t
>(dst_row) >= buf_h) {
182 const uint8_t* src_row = atlas_pixels.data()
183 +
static_cast<size_t>(src_y + row) * atlas_size + src_x;
184 uint8_t* dst_row_ptr = dst +
static_cast<size_t>(dst_row) * buf_w * 4;
186 for (uint32_t col = 0; col < gw; ++col) {
187 const int32_t dst_col = gx +
static_cast<int32_t
>(col);
188 if (dst_col < 0 ||
static_cast<uint32_t
>(dst_col) >= buf_w) {
192 const uint8_t coverage = src_row[col];
193 const auto alpha =
static_cast<uint8_t
>(
194 (
static_cast<uint32_t
>(coverage) *
static_cast<uint32_t
>(ca)) / 255U);
196 uint8_t* px = dst_row_ptr +
static_cast<size_t>(dst_col) * 4;
207 const std::shared_ptr<Buffers::TextBuffer>& target,
208 std::span<const GlyphQuad> quads,
213 "ink_quads: target buffer is null");
222 const uint32_t buf_w = target->get_budget_width();
223 const uint32_t buf_h = target->get_budget_height();
224 const size_t buf_bytes =
static_cast<size_t>(buf_w) * buf_h * 4;
226 thread_local std::vector<uint8_t>
pixels;
227 pixels.assign(buf_bytes, 0);
230 target->set_pixel_data(
pixels.data(), buf_bytes);
237std::shared_ptr<Buffers::TextBuffer>
press(
238 std::string_view text,
248 const auto result = composite(text, *atlas, params.
color, buf_w, params.
render_bounds.y,
252 "press: no glyphs produced for '{}'", std::string(text));
256 const uint32_t budget_h = params.
budget_h > 0
257 ? std::max(params.
budget_h, result->h)
258 : std::min(result->h * k_grow_height_multiplier, params.
render_bounds.y);
260 return make_buffer(*result, buf_w, budget_h, params.
render_bounds, text);
263std::shared_ptr<Core::VKImage>
press(
264 std::string_view text,
265 glm::uvec2 render_bounds,
272 const uint32_t buf_w = render_bounds.x;
274 const uint32_t composite_h = std::max(render_bounds.y,
277 const auto result = composite(text, *atlas, params.
color, buf_w, composite_h,
278 static_cast<float>(atlas->
ascender()));
282 "press(AsTexture): no glyphs produced for '{}'", std::string(text));
286 const uint32_t budget_h = composite_h;
288 const size_t budget_bytes =
static_cast<size_t>(buf_w) * budget_h * 4;
289 std::vector<uint8_t>
pixels(budget_bytes, 0);
291 const auto copy_h =
static_cast<uint32_t
>(
292 result->pixels.size() / (
static_cast<size_t>(buf_w) * 4));
294 for (uint32_t row = 0; row < copy_h; ++row) {
296 pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
297 result->pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
298 static_cast<size_t>(buf_w) * 4);
306 "press(AsTexture): {}x{} content in {}x{} texture",
307 buf_w, result->h, buf_w, budget_h);
317 const std::shared_ptr<Buffers::TextBuffer>& target,
318 std::string_view text,
324 "repress: target buffer is null");
333 target->clear_accumulated_text();
334 target->reset_cursor();
336 const uint32_t buf_w = target->get_budget_width();
337 const uint32_t buf_h = target->get_budget_height();
338 const uint32_t bound_h = target->get_render_bounds_h();
340 const auto result = composite(text, *atlas, color, buf_w, bound_h,
344 "repress: no glyphs produced for '{}'", std::string(text));
349 const uint32_t new_h = std::min(result->h, bound_h);
350 target->resize_texture(buf_w, new_h);
351 target->set_budget(buf_w, new_h);
352 target->set_pixel_data(result->pixels.data(), result->pixels.size());
353 target->get_cursor_x() = result->cursor_x;
354 target->get_cursor_y() = result->cursor_y;
355 target->set_accumulated_text(text);
358 "repress(Fit): resized to {}x{}", buf_w, new_h);
362 const size_t buf_bytes =
static_cast<size_t>(buf_w) * buf_h * 4;
363 std::vector<uint8_t> cleared(buf_bytes, 0);
365 const uint32_t copy_h = std::min(result->h, buf_h);
366 for (uint32_t row = 0; row < copy_h; ++row) {
368 cleared.data() +
static_cast<size_t>(row) * buf_w * 4,
369 result->pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
370 static_cast<size_t>(buf_w) * 4);
373 target->set_pixel_data(cleared.data(), buf_bytes);
374 target->get_cursor_x() = result->cursor_x;
375 target->get_cursor_y() = result->cursor_y;
377 target->set_accumulated_text(text);
380 "repress(Clip): '{}' -> {}x{} into {}x{} budget",
381 std::string(text), buf_w, result->h, buf_w, buf_h);
387 std::shared_ptr<Core::VKImage>& target,
388 std::string_view text,
390 const std::shared_ptr<Buffers::VKBuffer>& staging)
394 "repress(VKImage): target is null");
402 const uint32_t buf_w = target->get_width();
403 const uint32_t buf_h = target->get_height();
405 const uint32_t composite_h = std::max(buf_h,
408 const auto result = composite(text, *atlas, params.
color, buf_w, composite_h,
409 static_cast<float>(atlas->
ascender()));
412 "repress(VKImage): no glyphs produced for '{}'", std::string(text));
418 const uint32_t new_h = composite_h;
419 const bool needs_realloc = composite_h != buf_h || buf_w != target->get_width();
421 const size_t buf_bytes =
static_cast<size_t>(buf_w) * new_h * 4;
422 std::vector<uint8_t>
pixels(buf_bytes, 0);
424 const uint32_t copy_h = std::min(result->h, new_h);
425 for (uint32_t row = 0; row < copy_h; ++row) {
427 pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
428 result->pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
429 static_cast<size_t>(buf_w) * 4);
438 loom.upload_data(target,
pixels.data(), buf_bytes, staging,
true);
440 loom.upload_data(target,
pixels.data(), buf_bytes);
444 "repress(VKImage): reallocated {}x{}", buf_w, new_h);
449 loom.upload_data(target,
pixels.data(), buf_bytes, staging);
451 loom.upload_data(target,
pixels.data(), buf_bytes);
455 "repress(VKImage): updated {}x{} in-place", buf_w, buf_h);
465 const std::shared_ptr<Buffers::TextBuffer>& target,
466 std::string_view text,
471 "impress: target buffer is null");
480 const uint32_t buf_w = target->get_budget_width();
481 const uint32_t buf_h = target->get_budget_height();
482 const uint32_t bound_h = target->get_render_bounds_h();
483 const auto pen_x =
static_cast<float>(target->get_cursor_x());
484 const auto pen_y =
static_cast<float>(target->get_cursor_y());
486 target->append_accumulated_text(text);
490 if (layout.
quads.empty()) {
492 "impress: no glyphs produced for '{}'", std::string(text));
496 const auto pen_y_ceil =
static_cast<uint32_t
>(std::ceil(layout.
final_pen_y));
497 const uint32_t new_cursor_y = pen_y_ceil;
498 const uint32_t content_h = pen_y_ceil + atlas->
line_height();
500 if (content_h > bound_h) {
504 if (content_h > buf_h) {
505 const uint32_t new_h = std::min(content_h * k_grow_height_multiplier, bound_h);
506 const std::string accumulated = target->get_accumulated_text();
508 const auto full = composite(accumulated, *atlas, color, buf_w, new_h);
513 target->resize_texture(buf_w, new_h);
514 target->set_budget(buf_w, new_h);
516 const size_t buf_bytes =
static_cast<size_t>(buf_w) * new_h * 4;
517 std::vector<uint8_t>
pixels(buf_bytes, 0);
518 const uint32_t copy_h = std::min(full->h, new_h);
519 for (uint32_t row = 0; row < copy_h; ++row) {
521 pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
522 full->pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
523 static_cast<size_t>(buf_w) * 4);
526 target->set_pixel_data(
pixels.data(), buf_bytes);
527 target->get_cursor_x() = full->cursor_x;
528 target->get_cursor_y() = full->h;
529 target->set_accumulated_text(accumulated);
532 "impress: vertical grow -> {}x{}", buf_w, new_h);
537 auto& pixel_data = target->get_pixel_data_mutable();
539 target->mark_pixels_dirty();
540 target->get_cursor_x() =
static_cast<uint32_t
>(std::ceil(layout.
final_pen_x));
541 target->get_cursor_y() = new_cursor_y;
544 "impress: '{}' at ({},{}) -> cursor ({},{})",
546 static_cast<uint32_t
>(pen_x),
static_cast<uint32_t
>(pen_y),
547 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::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 ascender() const
Ascender 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.
MAYAFLUX_API TextureLoom & get_texture_manager()
Get the global texture manager instance.
@ 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().