18 constexpr uint32_t k_grow_height_multiplier = 8;
28 struct CompositeResult {
46 void rasterize_styled(
47 std::span<const GlyphQuad> quads,
50 std::span<const StyleSpan> spans,
60 std::vector<bool> covered(quads.size(),
false);
61 std::vector<GlyphQuad> bucket;
63 for (
const auto& span : spans) {
64 const size_t lo = std::min(span.start, span.end);
65 const size_t hi = std::max(span.start, span.end);
68 for (
size_t i = 0; i < quads.size(); ++i) {
69 if (quads[i].byte_offset >= lo && quads[i].byte_offset < hi) {
70 bucket.push_back(quads[i]);
74 if (!bucket.empty()) {
80 for (
size_t i = 0; i < quads.size(); ++i) {
82 bucket.push_back(quads[i]);
85 if (!bucket.empty()) {
97 void fill_bytes(uint8_t*
dst,
size_t byte_count, glm::vec4
color)
99 const auto r =
static_cast<uint8_t
>(std::clamp(
color.r, 0.F, 1.F) * 255.F);
100 const auto g =
static_cast<uint8_t
>(std::clamp(
color.g, 0.F, 1.F) * 255.F);
101 const auto b =
static_cast<uint8_t
>(std::clamp(
color.b, 0.F, 1.F) * 255.F);
102 const auto a =
static_cast<uint8_t
>(std::clamp(
color.a, 0.F, 1.F) * 255.F);
104 for (
size_t i = 0; i + 3 < byte_count; i += 4) {
115 std::vector<uint8_t> make_filled(
size_t byte_count, glm::vec4
color)
117 std::vector<uint8_t> buf(byte_count);
118 fill_bytes(buf.data(), byte_count,
color);
126 GlyphAtlas* resolve_atlas(GlyphAtlas* hint)
134 "InkPress: no atlas available, call set_default_font first");
155 std::optional<CompositeResult> composite(
156 std::string_view text,
161 float pen_y_start = 0.F,
162 std::span<const StyleSpan> spans = {},
163 glm::vec4
background = { 0.F, 0.F, 0.F, 0.F })
165 const LayoutResult layout =
lay_out(text, atlas, 0.F, pen_y_start, buf_w);
167 const auto content_h =
static_cast<uint32_t
>(std::ceil(layout.final_pen_y))
168 + atlas.line_height();
169 const uint32_t dst_h = std::min(content_h, buf_h);
171 if (buf_w == 0 || dst_h == 0) {
175 CompositeResult result;
176 result.h = content_h;
177 result.cursor_x =
static_cast<uint32_t
>(std::ceil(layout.final_pen_x));
178 result.cursor_y =
static_cast<uint32_t
>(std::ceil(layout.final_pen_y));
179 result.pixels = make_filled(
static_cast<size_t>(buf_w) * dst_h * 4, background);
181 rasterize_styled(layout.quads, atlas,
color, spans, result.pixels.data(), buf_w, dst_h);
201 std::shared_ptr<Buffers::TextBuffer> make_buffer(
202 const CompositeResult& result,
205 glm::uvec2 render_bounds,
206 std::string_view text,
207 glm::vec4 background)
209 budget_h = std::max(budget_h, result.h);
211 const size_t budget_bytes =
static_cast<size_t>(buf_w) * budget_h * 4;
212 std::vector<uint8_t>
pixels = make_filled(budget_bytes, background);
214 const uint32_t copy_h = std::min(result.h,
215 static_cast<uint32_t
>(result.pixels.size() / (
static_cast<size_t>(buf_w) * 4)));
216 for (uint32_t row = 0; row < copy_h; ++row) {
218 pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
219 result.pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
220 static_cast<size_t>(buf_w) * 4);
223 auto buf = std::make_shared<Buffers::TextBuffer>(
225 Portal::Graphics::ImageFormat::RGBA8,
228 buf->set_budget(buf_w, budget_h);
229 buf->set_render_bounds(render_bounds.x, render_bounds.y);
230 buf->set_background(background);
231 buf->set_accumulated_text(text);
232 buf->get_cursor_x() = result.cursor_x;
233 buf->get_cursor_y() = result.cursor_y;
236 "InkPress: {}x{} content in {}x{} texture, render bounds {}x{}",
237 buf_w, result.h, buf_w, budget_h, render_bounds.x, render_bounds.y);
245 std::span<const GlyphQuad> quads,
252 const uint8_t cr =
static_cast<uint8_t
>(std::clamp(
color.r, 0.F, 1.F) * 255.F);
253 const uint8_t cg =
static_cast<uint8_t
>(std::clamp(
color.g, 0.F, 1.F) * 255.F);
254 const uint8_t cb =
static_cast<uint8_t
>(std::clamp(
color.b, 0.F, 1.F) * 255.F);
255 const uint8_t ca =
static_cast<uint8_t
>(std::clamp(
color.a, 0.F, 1.F) * 255.F);
258 const std::span<const uint8_t> atlas_pixels = atlas_tex.
pixel_bytes(0);
259 const uint32_t atlas_size = atlas.
atlas_size();
261 for (
const auto&
q : quads) {
262 const auto gx =
static_cast<int32_t
>(std::floor(
q.x0));
263 const auto gy =
static_cast<int32_t
>(std::floor(
q.y0));
264 const auto gw =
static_cast<uint32_t
>(std::ceil(
q.x1 -
q.x0));
265 const auto gh =
static_cast<uint32_t
>(std::ceil(
q.y1 -
q.y0));
267 const auto src_x =
static_cast<uint32_t
>(
q.uv_x0 *
static_cast<float>(atlas_size));
268 const auto src_y =
static_cast<uint32_t
>(
q.uv_y0 *
static_cast<float>(atlas_size));
270 for (uint32_t row = 0; row < gh; ++row) {
271 const int32_t dst_row = gy +
static_cast<int32_t
>(row);
272 if (dst_row < 0 ||
static_cast<uint32_t
>(dst_row) >= buf_h) {
276 const uint8_t* src_row = atlas_pixels.data()
277 +
static_cast<size_t>(
src_y + row) * atlas_size +
src_x;
278 uint8_t* dst_row_ptr =
dst +
static_cast<size_t>(dst_row) * buf_w * 4;
280 for (uint32_t col = 0; col < gw; ++col) {
281 const int32_t dst_col = gx +
static_cast<int32_t
>(col);
282 if (dst_col < 0 ||
static_cast<uint32_t
>(dst_col) >= buf_w) {
286 const uint8_t coverage = src_row[col];
287 const uint32_t src_a = (
static_cast<uint32_t
>(coverage) *
static_cast<uint32_t
>(ca)) / 255U;
289 uint8_t*
px = dst_row_ptr +
static_cast<size_t>(dst_col) * 4;
291 const uint32_t dst_a =
px[3];
292 const uint32_t blended_dst_a = (dst_a * (255U - src_a)) / 255U;
293 const uint32_t out_a = src_a + blended_dst_a;
301 px[0] =
static_cast<uint8_t
>((cr * src_a +
px[0] * blended_dst_a) / out_a);
302 px[1] =
static_cast<uint8_t
>((cg * src_a +
px[1] * blended_dst_a) / out_a);
303 px[2] =
static_cast<uint8_t
>((cb * src_a +
px[2] * blended_dst_a) / out_a);
304 px[3] =
static_cast<uint8_t
>(out_a);
312 const std::shared_ptr<Buffers::TextBuffer>& target,
313 std::span<const GlyphQuad> quads,
319 "ink_quads: target buffer is null");
323 GlyphAtlas* atlas = resolve_atlas(atlas_hint);
328 const uint32_t buf_w = target->get_budget_width();
329 const uint32_t buf_h = target->get_budget_height();
330 const size_t buf_bytes =
static_cast<size_t>(buf_w) * buf_h * 4;
332 thread_local std::vector<uint8_t>
pixels;
334 fill_bytes(
pixels.data(), buf_bytes, target->get_background());
337 target->set_pixel_data(
pixels.data(), buf_bytes);
344std::shared_ptr<Buffers::TextBuffer>
press(
345 std::string_view text,
355 const auto result = composite(text, *atlas, params.
color, buf_w, params.
render_bounds.y,
359 "press: no glyphs produced for '{}'", std::string(text));
363 const uint32_t budget_h = params.
budget_h > 0
364 ? std::max(params.
budget_h, result->h)
365 : std::min(result->h * k_grow_height_multiplier, params.
render_bounds.y);
370std::shared_ptr<Core::VKImage>
press(
371 std::string_view text,
372 glm::uvec2 render_bounds,
374 const std::shared_ptr<Buffers::VKBuffer>& staging)
380 const uint32_t buf_w = render_bounds.x;
382 const uint32_t composite_h = std::max(render_bounds.y,
385 const auto result = composite(text, *atlas, params.
color, buf_w, composite_h,
390 "press(AsTexture): no glyphs produced for '{}'", std::string(text));
394 const uint32_t budget_h = composite_h;
396 const size_t budget_bytes =
static_cast<size_t>(buf_w) * budget_h * 4;
399 const auto copy_h =
static_cast<uint32_t
>(
400 result->pixels.size() / (
static_cast<size_t>(buf_w) * 4));
402 for (uint32_t row = 0; row < copy_h; ++row) {
404 pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
405 result->pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
406 static_cast<size_t>(buf_w) * 4);
411 std::shared_ptr<Core::VKImage>
image;
413 image = loom.create_2d_uninitialized(buf_w, budget_h, Portal::Graphics::ImageFormat::RGBA8, 1);
416 loom.upload_data(
image,
pixels.data(), budget_bytes, staging,
true);
418 image = loom.create_2d(buf_w, budget_h, Portal::Graphics::ImageFormat::RGBA8,
423 "press(AsTexture): {}x{} content in {}x{} texture",
424 buf_w, result->h, buf_w, budget_h);
434 const std::shared_ptr<Buffers::TextBuffer>& target,
435 std::string_view text,
438 std::span<const StyleSpan> spans)
442 "repress: target buffer is null");
451 target->clear_accumulated_text();
452 target->reset_cursor();
454 const uint32_t buf_w = target->get_budget_width();
455 const uint32_t buf_h = target->get_budget_height();
456 const uint32_t bound_h = target->get_render_bounds_h();
457 const glm::vec4 background = target->get_background();
459 const auto result = composite(text, *atlas,
color, buf_w, bound_h,
460 static_cast<float>(atlas->
line_height()), spans, background);
463 "repress: no glyphs produced for '{}'", std::string(text));
468 const uint32_t new_h = std::min(result->h, bound_h);
469 target->resize_texture(buf_w, new_h);
470 target->set_budget(buf_w, new_h);
471 target->set_pixel_data(result->pixels.data(), result->pixels.size());
472 target->get_cursor_x() = result->cursor_x;
473 target->get_cursor_y() = result->cursor_y;
474 target->set_accumulated_text(text);
477 "repress(Fit): resized to {}x{}", buf_w, new_h);
481 const size_t buf_bytes =
static_cast<size_t>(buf_w) * buf_h * 4;
482 std::vector<uint8_t> cleared = make_filled(buf_bytes, background);
484 const uint32_t copy_h = std::min(result->h, buf_h);
485 for (uint32_t row = 0; row < copy_h; ++row) {
487 cleared.data() +
static_cast<size_t>(row) * buf_w * 4,
488 result->pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
489 static_cast<size_t>(buf_w) * 4);
492 target->set_pixel_data(cleared.data(), buf_bytes);
493 target->get_cursor_x() = result->cursor_x;
494 target->get_cursor_y() = result->cursor_y;
496 target->set_accumulated_text(text);
499 "repress(Clip): '{}' -> {}x{} into {}x{} budget",
500 std::string(text), buf_w, result->h, buf_w, buf_h);
506 std::shared_ptr<Core::VKImage>& target,
507 std::string_view text,
509 const std::shared_ptr<Buffers::VKBuffer>& staging)
513 "repress(VKImage): target is null");
521 const uint32_t buf_w = target->get_width();
522 const uint32_t buf_h = target->get_height();
524 const uint32_t composite_h = std::max(buf_h,
527 const auto result = composite(text, *atlas, params.
color, buf_w, composite_h,
531 "repress(VKImage): no glyphs produced for '{}'", std::string(text));
537 const uint32_t new_h = composite_h;
538 const bool needs_realloc = composite_h != buf_h || buf_w != target->get_width();
540 const size_t buf_bytes =
static_cast<size_t>(buf_w) * new_h * 4;
543 const uint32_t copy_h = std::min(result->h, new_h);
544 for (uint32_t row = 0; row < copy_h; ++row) {
546 pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
547 result->pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
548 static_cast<size_t>(buf_w) * 4);
552 target = loom.create_2d_uninitialized(buf_w, new_h, Portal::Graphics::ImageFormat::RGBA8, 1);
556 loom.upload_data(target,
pixels.data(), buf_bytes, staging,
true);
558 loom.upload_data(target,
pixels.data(), buf_bytes);
562 "repress(VKImage): reallocated {}x{}", buf_w, new_h);
567 loom.upload_data(target,
pixels.data(), buf_bytes, staging,
true);
569 loom.upload_data(target,
pixels.data(), buf_bytes);
573 "repress(VKImage): updated {}x{} in-place", buf_w, buf_h);
583 const std::shared_ptr<Buffers::TextBuffer>& target,
584 std::string_view text,
586 std::span<const StyleSpan> spans)
590 "impress: target buffer is null");
599 const uint32_t buf_w = target->get_budget_width();
600 const uint32_t buf_h = target->get_budget_height();
601 const uint32_t bound_h = target->get_render_bounds_h();
602 const auto pen_x =
static_cast<float>(target->get_cursor_x());
603 const auto pen_y =
static_cast<float>(target->get_cursor_y());
605 target->append_accumulated_text(text);
609 const auto pen_y_ceil =
static_cast<uint32_t
>(std::ceil(layout.
final_pen_y));
610 const uint32_t new_cursor_y = pen_y_ceil;
611 const uint32_t content_h = pen_y_ceil + atlas->
line_height();
613 if (content_h > bound_h) {
617 if (content_h > buf_h) {
618 const uint32_t new_h = std::min(content_h * k_grow_height_multiplier, bound_h);
619 const std::string accumulated = target->get_accumulated_text();
620 const glm::vec4 background = target->get_background();
622 const auto full = composite(accumulated, *atlas,
color, buf_w, new_h,
623 static_cast<float>(atlas->
line_height()), spans, background);
628 target->resize_texture(buf_w, new_h);
629 target->set_budget(buf_w, new_h);
631 const size_t buf_bytes =
static_cast<size_t>(buf_w) * new_h * 4;
632 std::vector<uint8_t>
pixels = make_filled(buf_bytes, background);
633 const uint32_t copy_h = std::min(full->h, new_h);
634 for (uint32_t row = 0; row < copy_h; ++row) {
636 pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
637 full->pixels.data() +
static_cast<size_t>(row) * buf_w * 4,
638 static_cast<size_t>(buf_w) * 4);
641 target->set_pixel_data(
pixels.data(), buf_bytes);
642 target->get_cursor_x() = full->cursor_x;
643 target->get_cursor_y() = full->cursor_y;
644 target->set_accumulated_text(accumulated);
647 "impress: vertical grow -> {}x{}", buf_w, new_h);
652 auto& pixel_data = target->get_pixel_data_mutable();
653 rasterize_styled(layout.
quads, *atlas,
color, spans, pixel_data.data(), buf_w, buf_h);
654 target->mark_pixels_dirty();
655 target->get_cursor_x() =
static_cast<uint32_t
>(std::ceil(layout.
final_pen_x));
656 target->get_cursor_y() = new_cursor_y;
659 "impress: '{}' at ({},{}) -> cursor ({},{})",
661 static_cast<uint32_t
>(pen_x),
static_cast<uint32_t
>(pen_y),
662 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.
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.
void ink_quads(const std::shared_ptr< Buffers::TextBuffer > &target, std::span< const GlyphQuad > quads, glm::vec4 color, GlyphAtlas *atlas_hint)
Rasterize a mutated quad span into an existing TextBuffer.
RedrawPolicy
Policy controlling TextBuffer reuse behaviour in repress().
@ Fit
Replace content. Reallocate GPU texture if text exceeds existing budget.
bool repress(const std::shared_ptr< Buffers::TextBuffer > &target, std::string_view text, glm::vec4 color, RedrawPolicy policy, std::span< const StyleSpan > spans)
Re-composite a UTF-8 string into an existing TextBuffer.
ImpressResult
Result of an impress() call.
@ Overflow
Vertical budget exceeded. Texture reallocated. Previous content cleared.
@ Ok
Run composited at cursor. No GPU state change.
ImpressResult impress(const std::shared_ptr< Buffers::TextBuffer > &target, std::string_view text, glm::vec4 color, std::span< const StyleSpan > spans)
Append a UTF-8 string into an existing TextBuffer at the current cursor.
std::shared_ptr< Buffers::TextBuffer > press(std::string_view text, const PressParams ¶ms)
Composite a UTF-8 string into a new TextBuffer.
LayoutResult lay_out(std::string_view text, GlyphAtlas &atlas, float pen_x, float pen_y, uint32_t wrap_w, uint32_t tab_width)
Lay out a UTF-8 string into a sequence of screen-space quads.
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 not covered by a span in spans.
uint32_t budget_h
Initial vertical budget in pixels. Zero applies the grow heuristic.
std::vector< StyleSpan > spans
Optional per-byte-range color overrides. Empty applies color uniformly.
glm::uvec2 render_bounds
Hard render bounds in pixels.
glm::vec4 background
RGBA fill composited beneath every glyph.
Construction parameters for press().