fix text rendering

This commit is contained in:
2025-12-17 00:33:23 +01:00
parent 09457e56d4
commit a9d0d7ec42

View File

@@ -1372,7 +1372,7 @@ mod window {
draw_text(framebuffer, width, text_x, text_y, hover.name, font, 14.0, 0xFFFFFF); draw_text(framebuffer, width, text_x, text_y, hover.name, font, 14.0, 0xFFFFFF);
draw_text(framebuffer, width, text_x, text_y + 20, draw_text(framebuffer, width, text_x, text_y + 20,
&format!("Duration: {:.2} μs", hover.duration_us), font, 12.0, 0xCCCCCC); &format!("Duration: {} μs", format_number(hover.duration_us)), font, 12.0, 0xCCCCCC);
draw_text(framebuffer, width, text_x, text_y + 38, draw_text(framebuffer, width, text_x, text_y + 38,
&format!("Thread: {}", hover.thread_id), font, 12.0, 0xCCCCCC); &format!("Thread: {}", hover.thread_id), font, 12.0, 0xCCCCCC);
draw_text(framebuffer, width, text_x, text_y + 56, draw_text(framebuffer, width, text_x, text_y + 56,
@@ -1409,17 +1409,23 @@ mod window {
size: f32, size: f32,
color: u32, color: u32,
) { ) {
let mut cursor_x = x as f32; let mut cursor_x = x as i32;
for ch in text.chars() { for ch in text.chars() {
let (metrics, bitmap) = font.rasterize(ch, size); let (metrics, bitmap) = font.rasterize(ch, size);
// xmin is the offset from the cursor to the left edge of the glyph
// ymin is the offset from the baseline to the bottom edge of the glyph
// The glyph's position is: (cursor + xmin, baseline - height - ymin)
let glyph_x = (cursor_x + metrics.xmin).max(0) as usize;
let glyph_y = (size as i32 + y as i32 - metrics.height as i32 - metrics.ymin).max(0) as usize;
for (i, &alpha) in bitmap.iter().enumerate() { for (i, &alpha) in bitmap.iter().enumerate() {
if alpha > 0 { if alpha > 0 {
let px = cursor_x as usize + (i % metrics.width); let px = glyph_x + (i % metrics.width);
let py = y + (i / metrics.width) + (size as usize - metrics.height); let py = glyph_y + (i / metrics.width);
if px < width { if px < width && py < framebuffer.len() / width {
let idx = py * width + px; let idx = py * width + px;
if idx < framebuffer.len() { if idx < framebuffer.len() {
let alpha_f = alpha as f32 / 255.0; let alpha_f = alpha as f32 / 255.0;
@@ -1430,7 +1436,8 @@ mod window {
} }
} }
cursor_x += metrics.advance_width; // Advance cursor by the advance_width (includes spacing)
cursor_x += metrics.advance_width as i32;
} }
} }
@@ -1438,4 +1445,26 @@ mod window {
let hash = name.bytes().fold(0u32, |acc, b| acc.wrapping_mul(31).wrapping_add(b as u32)); let hash = name.bytes().fold(0u32, |acc, b| acc.wrapping_mul(31).wrapping_add(b as u32));
COLORS[(hash as usize) % COLORS.len()] COLORS[(hash as usize) % COLORS.len()]
} }
// Format a number with thousands separators
fn format_number(n: f64) -> String {
let s = format!("{:.2}", n);
let parts: Vec<&str> = s.split('.').collect();
let integer_part = parts[0];
let decimal_part = if parts.len() > 1 { parts[1] } else { "00" };
// Add commas to integer part
let mut result = String::new();
let chars: Vec<char> = integer_part.chars().collect();
for (i, ch) in chars.iter().enumerate() {
if i > 0 && (chars.len() - i) % 3 == 0 {
result.push(',');
}
result.push(*ch);
}
result.push('.');
result.push_str(decimal_part);
result
}
} }