@@ -29,6 +29,25 @@ pub fn render_char_onto(
29
29
render_glyph_onto ( glyph, font, onto, draw_loc, draw_color, font_size)
30
30
}
31
31
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
+
32
51
pub fn render_antialiased_glyph_onto (
33
52
glyph : & GlyphRenderDescription ,
34
53
font : & Font ,
@@ -77,8 +96,8 @@ pub fn render_antialiased_glyph_onto(
77
96
let scaled_polygon_stack = PolygonStack :: new ( & scaled_polygons) ;
78
97
let scaled_edges = scaled_polygon_stack. lines ( ) ;
79
98
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}");
82
101
let upscaled_bounding_box_width =
83
102
superscaled_polygon_bounding_box. size . width . ceil ( ) as usize ;
84
103
let upscaled_bounding_box_height =
@@ -98,7 +117,7 @@ pub fn render_antialiased_glyph_onto(
98
117
assert_eq ! ( line. p1. y, line. p2. y, "Expect horizontal scanlines" ) ;
99
118
let line_y = line. p1 . y - superscaled_polygon_bounding_box. origin . y ;
100
119
//let line_y = line.p1.y;
101
- println ! ( "Line {line}, origin {line_y}" ) ;
120
+ // println!("Line {line}, origin {line_y}");
102
121
for line_x in line. min_x ( ) . round ( ) as usize ..line. max_x ( ) . round ( ) as usize {
103
122
upscaled_bounding_box[ line_y as usize ] [ line_x] = true ;
104
123
onto. putpixel (
@@ -112,14 +131,14 @@ pub fn render_antialiased_glyph_onto(
112
131
let downscaled_height = scaled_polygon_bounding_box. size . height . ceil ( ) as usize ;
113
132
//println!("downscaled width {downscaled_width} height {downscaled_height}");
114
133
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}");
116
135
117
136
// Compare every pixel with its neighbors
118
137
for downscaled_y in 0 ..downscaled_height as usize {
119
138
let upscaled_y: usize = downscaled_y * ssaa_factor as usize ;
120
139
for downscaled_x in 0 ..downscaled_width as usize {
121
140
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})");
123
142
124
143
let mut neighbors: Vec < bool > = vec ! [ ] ;
125
144
// Center pixel
@@ -167,11 +186,13 @@ pub fn render_antialiased_glyph_onto(
167
186
// Filled percentage
168
187
let fill_percentage = ( neighbors. iter ( ) . filter ( |& & v| v == true ) . count ( ) as f64 )
169
188
/ ( neighbors. len ( ) as f64 ) ;
189
+ /*
170
190
println!(
171
191
"\t\tFound {} neighbors with {:.2} fill",
172
192
neighbors.len(),
173
193
fill_percentage
174
194
);
195
+ */
175
196
downscaled_bounding_box[ downscaled_y] [ downscaled_x] = fill_percentage;
176
197
// Debug: Upscale the antialiased pixels to make them easier to see
177
198
/*
0 commit comments