summaryrefslogtreecommitdiff
path: root/src/graphics
diff options
context:
space:
mode:
authorTengu712 <genreihoutengu@gmail.com>2026-10-01 23:54:19 +0900
committerTengu712 <genreihoutengu@gmail.com>2026-10-01 23:54:19 +0900
commit503486e2ce889858aac619985516374bddeb7a8f (patch)
tree5b959114ae5d5e2f4a697e805c17f21eace05453 /src/graphics
parent2c104d7936193bb19eb52b3e4d47657ffb18a9d7 (diff)
align text
Diffstat (limited to 'src/graphics')
-rw-r--r--src/graphics/command.rs25
-rw-r--r--src/graphics/font.rs4
2 files changed, 27 insertions, 2 deletions
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;
}
}
diff --git a/src/graphics/font.rs b/src/graphics/font.rs
index 78a28a3..c20e691 100644
--- a/src/graphics/font.rs
+++ b/src/graphics/font.rs
@@ -58,6 +58,10 @@ pub fn ascent(scale: f32) -> f32 {
FONT.ascent_unscaled() * factor(scale)
}
+pub fn height(scale: f32) -> f32 {
+ FONT.height_unscaled() * factor(scale)
+}
+
pub fn line_height(scale: f32) -> f32 {
(FONT.height_unscaled() + FONT.line_gap_unscaled()) * factor(scale)
}