Skip to content

Commit fb2984a

Browse files
committed
[TTF] Add a helper to estimate a string’s rendered size
1 parent 6f7d389 commit fb2984a

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

rust_programs/ttf_renderer/src/render.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ pub fn render_char_onto(
2929
render_glyph_onto(glyph, font, onto, draw_loc, draw_color, font_size)
3030
}
3131

32+
pub fn rendered_string_size(s: &str, font: &Font, font_size: Size) -> Size {
33+
let scale_factor = font_size.height as f64 / font.units_per_em as f64;
34+
let mut bounding_box = Size::new(
35+
0,
36+
(font.bounding_box.height() as f64 * scale_factor) as isize,
37+
);
38+
for ch in s.chars() {
39+
let codepoint = Codepoint::from(ch);
40+
if let Some(glyph) = font.glyph_for_codepoint(codepoint) {
41+
let scaled_metrics = glyph
42+
.render_metrics
43+
.metrics()
44+
.scale_to_font_size(font.units_per_em, &font_size);
45+
bounding_box.width += scaled_metrics.advance_width as isize;
46+
}
47+
}
48+
bounding_box
49+
}
50+
3251
pub fn render_antialiased_glyph_onto(
3352
glyph: &GlyphRenderDescription,
3453
font: &Font,
@@ -77,8 +96,8 @@ pub fn render_antialiased_glyph_onto(
7796
let scaled_polygon_stack = PolygonStack::new(&scaled_polygons);
7897
let scaled_edges = scaled_polygon_stack.lines();
7998
let scaled_polygon_bounding_box = bounding_box_from_edges(&scaled_edges);
80-
println!("Got superscaled edges bounding box {superscaled_polygon_bounding_box}");
81-
println!("Got scaled edges bounding box {scaled_polygon_bounding_box}");
99+
//println!("Got superscaled edges bounding box {superscaled_polygon_bounding_box}");
100+
//println!("Got scaled edges bounding box {scaled_polygon_bounding_box}");
82101
let upscaled_bounding_box_width =
83102
superscaled_polygon_bounding_box.size.width.ceil() as usize;
84103
let upscaled_bounding_box_height =
@@ -98,7 +117,7 @@ pub fn render_antialiased_glyph_onto(
98117
assert_eq!(line.p1.y, line.p2.y, "Expect horizontal scanlines");
99118
let line_y = line.p1.y - superscaled_polygon_bounding_box.origin.y;
100119
//let line_y = line.p1.y;
101-
println!("Line {line}, origin {line_y}");
120+
//println!("Line {line}, origin {line_y}");
102121
for line_x in line.min_x().round() as usize..line.max_x().round() as usize {
103122
upscaled_bounding_box[line_y as usize][line_x] = true;
104123
onto.putpixel(
@@ -112,14 +131,14 @@ pub fn render_antialiased_glyph_onto(
112131
let downscaled_height = scaled_polygon_bounding_box.size.height.ceil() as usize;
113132
//println!("downscaled width {downscaled_width} height {downscaled_height}");
114133
let mut downscaled_bounding_box = vec![vec![0.0; downscaled_width]; downscaled_height];
115-
println!("Font size {font_size}");
134+
//println!("Font size {font_size}");
116135

117136
// Compare every pixel with its neighbors
118137
for downscaled_y in 0..downscaled_height as usize {
119138
let upscaled_y: usize = downscaled_y * ssaa_factor as usize;
120139
for downscaled_x in 0..downscaled_width as usize {
121140
let upscaled_x: usize = downscaled_x * ssaa_factor as usize;
122-
println!("Downscaled ({downscaled_x}, {downscaled_y}), Upscaled ({upscaled_x}, {upscaled_y})");
141+
//println!("Downscaled ({downscaled_x}, {downscaled_y}), Upscaled ({upscaled_x}, {upscaled_y})");
123142

124143
let mut neighbors: Vec<bool> = vec![];
125144
// Center pixel
@@ -167,11 +186,13 @@ pub fn render_antialiased_glyph_onto(
167186
// Filled percentage
168187
let fill_percentage = (neighbors.iter().filter(|&&v| v == true).count() as f64)
169188
/ (neighbors.len() as f64);
189+
/*
170190
println!(
171191
"\t\tFound {} neighbors with {:.2} fill",
172192
neighbors.len(),
173193
fill_percentage
174194
);
195+
*/
175196
downscaled_bounding_box[downscaled_y][downscaled_x] = fill_percentage;
176197
// Debug: Upscale the antialiased pixels to make them easier to see
177198
/*

0 commit comments

Comments
 (0)