Skip to content

Commit e287d95

Browse files
committed
[TTF] Tweak font renderer layout, scaling, and positioning
1 parent fb2984a commit e287d95

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

rust_programs/ttf_renderer/src/render.rs

+34-25
Original file line numberDiff line numberDiff line change
@@ -262,24 +262,24 @@ fn render_polygons_glyph(
262262
GlyphRenderInstructions::PolygonsGlyph(polygons) => polygons,
263263
_ => panic!("Expected a polygons glyph"),
264264
};
265+
//println!("Glyph bounding box {:?}", glyph.render_metrics.bounding_box);
265266
let scaled_polygons: Vec<Polygon> = polygons_description
266267
.polygons
267268
.iter()
268269
.map(|p| p.scale_by(scale_x, scale_y))
269270
.collect();
270-
// Ensure at a minimum we see the outline points
271-
// This helps smooth over missing outlines for very thin/small glyphs
272271
/*
273-
for p in scaled_polygons.iter() {
274-
p.draw_outline(&mut dest_slice, draw_color);
272+
println!("Scaled polygons:");
273+
for poly in scaled_polygons.iter() {
274+
println!("\t{poly:?}");
275275
}
276276
*/
277+
277278
let polygon_stack = PolygonStack::new(&scaled_polygons);
278-
polygon_stack.fill(onto, draw_color);
279279

280+
/*
280281
let instructions = glyph.hinting_program_bytes.as_ref().unwrap();
281282
let mut graphics_state = GraphicsState::new(font_size);
282-
/*
283283
parse_instructions(
284284
font,
285285
instructions,
@@ -297,16 +297,29 @@ pub fn render_glyph_onto(
297297
draw_color: Color,
298298
font_size: Size,
299299
) -> (Rect, GlyphMetrics) {
300-
let scale_x = font_size.width as f64 / (font.units_per_em as f64);
301-
let scale_y = font_size.height as f64 / (font.units_per_em as f64);
302-
let scaled_glyph_metrics = glyph.metrics().scale(scale_x, scale_y);
303-
let draw_loc = draw_loc
304-
+ Point::new(
305-
//scaled_glyph_metrics.left_side_bearing,
306-
//scaled_glyph_metrics.top_side_bearing,
307-
0, 0,
308-
);
309-
let draw_box = Rect::from_parts(draw_loc, font_size);
300+
let scaled_glyph_metrics = glyph
301+
.metrics()
302+
.scale_to_font_size(font.units_per_em, &font_size);
303+
let scale_factor = font_size.height as f64 / font.units_per_em as f64;
304+
let render_box = glyph.render_metrics.bounding_box;
305+
let scaled_render_box = Rect::from_parts(
306+
Point::new(
307+
(render_box.origin.x as f64 * scale_factor) as _,
308+
(render_box.origin.y as f64 * scale_factor) as _,
309+
),
310+
Size::new(
311+
(render_box.size.width as f64 * scale_factor) as _,
312+
(render_box.size.height as f64 * scale_factor) as _,
313+
),
314+
);
315+
let draw_loc = draw_loc + Point::new(0, 0);
316+
let draw_box = Rect::from_parts(
317+
draw_loc,
318+
Size::new(
319+
(font.bounding_box.width() as f64 * scale_factor) as isize,
320+
(font.bounding_box.height() as f64 * scale_factor) as isize,
321+
),
322+
);
310323
let mut dest_slice = onto.get_slice(draw_box);
311324

312325
match &glyph.render_instructions {
@@ -315,8 +328,8 @@ pub fn render_glyph_onto(
315328
glyph,
316329
font,
317330
font_size,
318-
scale_x,
319-
scale_y,
331+
scale_factor,
332+
scale_factor,
320333
&mut dest_slice,
321334
draw_color,
322335
);
@@ -329,10 +342,9 @@ pub fn render_glyph_onto(
329342
let child_glyph = &font.glyphs[child_description.glyph_index];
330343
let origin = child_description.origin;
331344
let scaled_origin = Point::new(
332-
(origin.x as f64 * scale_x) as isize,
333-
(origin.y as f64 * scale_y) as isize,
345+
(origin.x as f64 * scale_factor) as isize,
346+
(origin.y as f64 * scale_factor) as isize,
334347
);
335-
//let subslice = dest_slice.get_slice(Rect::from_parts(scaled_origin), font_size);
336348
render_glyph_onto(
337349
child_glyph,
338350
font,
@@ -345,10 +357,7 @@ pub fn render_glyph_onto(
345357
}
346358
}
347359

348-
(
349-
Rect::from_parts(draw_box.origin, font_size),
350-
scaled_glyph_metrics,
351-
)
360+
(draw_box, scaled_glyph_metrics)
352361
}
353362

354363
fn lerp(a: f64, b: f64, percent: f64) -> f64 {

0 commit comments

Comments
 (0)