From 503486e2ce889858aac619985516374bddeb7a8f Mon Sep 17 00:00:00 2001 From: Tengu712 Date: Thu, 1 Oct 2026 23:54:19 +0900 Subject: align text --- src/graphics/command.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'src/graphics/command.rs') diff --git a/src/graphics/command.rs b/src/graphics/command.rs index 54b6744..f0330cb 100644 --- a/src/graphics/command.rs +++ b/src/graphics/command.rs @@ -72,13 +72,21 @@ fn build_text( ) { let scale = text.scale as f32; let line_height = font::line_height(scale); - let mut baseline = text.origin.y + font::ascent(scale); + let line_count = text.text.matches('\n').count(); + let height = line_height * line_count as f32 + font::height(scale); + let top = match text.v_align { + VerticalAlign::Top => text.origin.y, + VerticalAlign::Center => text.origin.y - height / 2.0, + VerticalAlign::Bottom => text.origin.y - height, + }; + let mut baseline = top + font::ascent(scale); for line in text.text.split('\n') { // NOTE: ピクセル境界に合わせるため、整数にする。 // 最終的にRust側でNDCに変換するが、fragmentation時に整数に直る。 let y = (baseline * scale_factor).round() / scale_factor; - let mut ox = text.origin.x; + let start = buf.len(); + let mut ox = 0.0; let mut prev = None; for c in line.chars() { @@ -110,6 +118,19 @@ fn build_text( ox += font::h_advance(scale, ci.glyph_id); } + let left = match text.h_align { + HorizontalAlign::Left => text.origin.x, + HorizontalAlign::Center => text.origin.x - ox / 2.0, + HorizontalAlign::Right => text.origin.x - ox, + }; + // NOTE: ピクセル境界に合わせるため、整数にする。 + // 最終的にRust側でNDCに変換するが、fragmentation時に整数に直る。 + let left = (left * scale_factor).round() / scale_factor; + + for instance in &mut buf[start..] { + instance.position.x += left / screen_width * 2.0; + } + baseline += line_height; } } -- cgit v1.3.1