Skip to content

Commit 5512216

Browse files
committed
Calculate font BBox using simple metrics
Calculating the "real" values is longer, it’s broken for un-optimized fonts, and seems to be ignored by many PDF readers.
1 parent 2bc635d commit 5512216

File tree

3 files changed

+5
-16
lines changed

3 files changed

+5
-16
lines changed

weasyprint/draw.py

-13
Original file line numberDiff line numberDiff line change
@@ -1157,19 +1157,6 @@ def draw_first_line(stream, textbox, text_overflow, block_ellipsis, x, y,
11571157
if glyph not in font.widths:
11581158
pango.pango_font_get_glyph_extents(
11591159
pango_font, glyph, stream.ink_rect, stream.logical_rect)
1160-
x1, y1, x2, y2 = (
1161-
stream.ink_rect.x,
1162-
-stream.ink_rect.y - stream.ink_rect.height,
1163-
stream.ink_rect.x + stream.ink_rect.width,
1164-
-stream.ink_rect.y)
1165-
if x1 < font.bbox[0]:
1166-
font.bbox[0] = int(units_to_double(x1 * 1000) / font_size)
1167-
if y1 < font.bbox[1]:
1168-
font.bbox[1] = int(units_to_double(y1 * 1000) / font_size)
1169-
if x2 > font.bbox[2]:
1170-
font.bbox[2] = int(units_to_double(x2 * 1000) / font_size)
1171-
if y2 > font.bbox[3]:
1172-
font.bbox[3] = int(units_to_double(y2 * 1000) / font_size)
11731160
font.widths[glyph] = int(round(
11741161
units_to_double(stream.logical_rect.width * 1000) /
11751162
font_size))

weasyprint/pdf/fonts.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ def build_fonts_dictionary(pdf, fonts, optimize_size):
5555
width = font.ttfont.getGlyphSet()[key].width
5656
font_widths[glyph] = width * ratio
5757

58+
max_x = max(font_widths.values()) if font_widths else 0
59+
bbox = (0, font.descent, max_x, font.ascent)
60+
5861
widths = pydyf.Array()
5962
for i in sorted(font_widths):
6063
if i - 1 not in font_widths:
@@ -107,11 +110,11 @@ def build_fonts_dictionary(pdf, fonts, optimize_size):
107110
'FontName': font.name,
108111
'FontFamily': pydyf.String(font.family),
109112
'Flags': font.flags,
110-
'FontBBox': pydyf.Array(font.bbox),
113+
'FontBBox': pydyf.Array(bbox),
111114
'ItalicAngle': font.italic_angle,
112115
'Ascent': font.ascent,
113116
'Descent': font.descent,
114-
'CapHeight': font.bbox[3],
117+
'CapHeight': bbox[3],
115118
'StemV': font.stemv,
116119
'StemH': font.stemh,
117120
font_file: font_references_by_file_hash[font.hash],

weasyprint/pdf/stream.py

-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ def __init__(self, pango_font):
8484
self.svg = harfbuzz.hb_ot_color_has_svg(hb_face)
8585
self.stemv = 80
8686
self.stemh = 80
87-
self.bbox = [0, 0, 0, 0]
8887
self.widths = {}
8988
self.cmap = {}
9089
self.used_in_forms = False

0 commit comments

Comments
 (0)