Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bounding box issue for text (part 2) #73

Merged
merged 2 commits into from
Jun 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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