[gpu] Right-align RTL lines to average width

Detect RTL direction from shaped buffers and right-align those
lines in the demo.  Uses the average width of RTL lines as the
alignment target for a balanced look.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.6 committed 2026-03-28 12:54:26 -06:00
1 parent a0ec979e39
commit a41f194bef
3 files changed
+70

No files matched your search

+35
View File
@@ -142,6 +142,8 @@ struct gpu_output_t
unsigned text_len HB_UNUSED,
hb_bool_t utf8_clusters HB_UNUSED)
{
unsigned vert_start = demo_buffer_get_vertex_count (buf);
unsigned count = hb_buffer_get_length (buffer);
hb_glyph_info_t *infos = hb_buffer_get_glyph_infos (buffer, nullptr);
hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, nullptr);
@@ -153,6 +155,11 @@ struct gpu_output_t
pos[i].x_offset,
pos[i].y_offset,
pos[i].x_advance);
unsigned vert_end = demo_buffer_get_vertex_count (buf);
if (hb_buffer_get_direction (buffer) == HB_DIRECTION_RTL && vert_end > vert_start)
rtl_lines.push_back ({vert_start, vert_end});
}
void error (const char *message)
@@ -163,6 +170,31 @@ struct gpu_output_t
template <typename app_t>
void finish (hb_buffer_t *buffer_ HB_UNUSED, const app_t *app HB_UNUSED)
{
/* Right-align RTL lines to the logical extents width. */
if (!rtl_lines.empty ())
{
/* Compute average line width across RTL lines. */
float sum_max_x = 0;
for (auto &lr : rtl_lines)
{
float line_max_x = 0;
for (unsigned i = lr.start; i < lr.end; i++)
{
float x = demo_buffer_get_vertex_x (buf, i);
if (x > line_max_x)
line_max_x = x;
}
lr.max_x = line_max_x;
sum_max_x += line_max_x;
}
float avg_width = sum_max_x / rtl_lines.size ();
/* Right-align each RTL line to the average width. */
for (auto &lr : rtl_lines)
demo_buffer_shift_vertices (buf, lr.start, lr.end,
avg_width - lr.max_x);
}
demo_font_print_stats (demo_font_);
demo_view_print_help (vu);
demo_view_setup (vu);
@@ -191,9 +223,12 @@ struct gpu_output_t
private:
struct line_range_t { unsigned start, end; float max_x; };
double font_size = 1;
hb_font_t *font = nullptr;
hb_face_t *face = nullptr;
std::vector<line_range_t> rtl_lines;
GLFWwindow *window = nullptr;
demo_glstate_t *st = nullptr;
+23
View File
@@ -173,6 +173,29 @@ demo_buffer_add_glyph (demo_buffer_t *buffer,
buffer->dirty = true;
}
unsigned
demo_buffer_get_vertex_count (demo_buffer_t *buffer)
{
return buffer->vertices->size ();
}
float
demo_buffer_get_vertex_x (demo_buffer_t *buffer, unsigned index)
{
return (*buffer->vertices)[index].x;
}
void
demo_buffer_shift_vertices (demo_buffer_t *buffer,
unsigned start,
unsigned end,
float dx)
{
for (unsigned i = start; i < end && i < buffer->vertices->size (); i++)
(*buffer->vertices)[i].x += dx;
buffer->dirty = true;
}
void
demo_buffer_draw (demo_buffer_t *buffer)
{
+12
View File
@@ -51,6 +51,18 @@ void
demo_buffer_current_line (demo_buffer_t *buffer,
double font_size);
unsigned
demo_buffer_get_vertex_count (demo_buffer_t *buffer);
float
demo_buffer_get_vertex_x (demo_buffer_t *buffer, unsigned index);
void
demo_buffer_shift_vertices (demo_buffer_t *buffer,
unsigned start,
unsigned end,
float dx);
void
demo_buffer_draw (demo_buffer_t *buffer);