Skip to content

Commit

Permalink
Fix bounding box issue for text (part 2) (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV authored Jun 1, 2024
1 parent 241b49d commit 8d95917
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/render/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ pub fn write_font(chunk: &mut Chunk, alloc: &mut RefAllocator, font: &mut Font)
flags.insert(FontFlags::SYMBOLIC);
flags.insert(FontFlags::SMALL_CAP);

let convert = |val| {
(val / units_per_em as f32) * 1000.0
};
let convert = |val| (val / units_per_em as f32) * 1000.0;

let global_bbox = ttf.global_bounding_box();
let bbox = pdf_writer::Rect::new(
Expand All @@ -121,8 +119,13 @@ pub fn write_font(chunk: &mut Chunk, alloc: &mut RefAllocator, font: &mut Font)

let italic_angle = ttf.italic_angle().unwrap_or(0.0);
let ascender = convert(ttf.typographic_ascender().unwrap_or(ttf.ascender()) as f32);
let descender = convert(ttf.typographic_descender().unwrap_or(ttf.descender()) as f32);
let cap_height = ttf.capital_height().filter(|&h| h > 0).map(|h| convert(h as f32)).unwrap_or(ascender);
let descender =
convert(ttf.typographic_descender().unwrap_or(ttf.descender()) as f32);
let cap_height = ttf
.capital_height()
.filter(|&h| h > 0)
.map(|h| convert(h as f32))
.unwrap_or(ascender);
let stem_v = 10.0 + 0.244 * (f32::from(ttf.weight().to_number()) - 50.0);

// Write the font descriptor (contains metrics about the font).
Expand All @@ -132,9 +135,9 @@ pub fn write_font(chunk: &mut Chunk, alloc: &mut RefAllocator, font: &mut Font)
.flags(flags)
.bbox(bbox)
.italic_angle(italic_angle)
.ascent(ascender as f32)
.descent(descender as f32)
.cap_height(cap_height as f32)
.ascent(ascender)
.descent(descender)
.cap_height(cap_height)
.stem_v(stem_v);

if is_cff {
Expand Down

0 comments on commit 8d95917

Please sign in to comment.