Skip to content

Commit 241b49d

Browse files
committed
Fix bounding box issues for text
1 parent b9f1513 commit 241b49d

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

src/render/text.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,22 @@ pub fn write_font(chunk: &mut Chunk, alloc: &mut RefAllocator, font: &mut Font)
107107
flags.insert(FontFlags::SYMBOLIC);
108108
flags.insert(FontFlags::SMALL_CAP);
109109

110+
let convert = |val| {
111+
(val / units_per_em as f32) * 1000.0
112+
};
113+
110114
let global_bbox = ttf.global_bounding_box();
111115
let bbox = pdf_writer::Rect::new(
112-
(global_bbox.x_min as f32 / units_per_em as f32) * 1000.0,
113-
(global_bbox.y_min as f32 / units_per_em as f32) * 1000.0,
114-
(global_bbox.x_max as f32 / units_per_em as f32) * 1000.0,
115-
(global_bbox.y_max as f32 / units_per_em as f32) * 1000.0,
116+
convert(global_bbox.x_min as f32),
117+
convert(global_bbox.y_min as f32),
118+
convert(global_bbox.x_max as f32),
119+
convert(global_bbox.y_max as f32),
116120
);
117121

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

124128
// Write the font descriptor (contains metrics about the font).

0 commit comments

Comments
 (0)