Skip to content

Commit 70f9b62

Browse files
committed
Support format 5 for bitmap glyphs
This format was the only one unsupported, but it looks like it’s widely used, so…
1 parent b8a5d11 commit 70f9b62

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

weasyprint/pdf/__init__.py

+30-12
Original file line numberDiff line numberDiff line change
@@ -543,15 +543,36 @@ def generate_pdf(pages, url_fetcher, metadata, fonts, target, zoom,
543543
widths = [0] * (last - first + 1)
544544
glyphs_info = {}
545545
for key, glyph in font_glyphs.items():
546+
glyph_format = glyph.getFormat()
547+
glyph_id = font.ttfont.getGlyphID(key)
548+
546549
# Get and store glyph metrics
547-
height, width = glyph.data[0:2]
548-
bearing_x = int.from_bytes(
549-
glyph.data[2:3], 'big', signed=True)
550-
bearing_y = int.from_bytes(
551-
glyph.data[3:4], 'big', signed=True)
552-
advance = glyph.data[4]
550+
if glyph_format == 5:
551+
data = glyph.data
552+
subtables = font.ttfont['EBLC'].strikes[0].indexSubTables
553+
for subtable in subtables:
554+
first_index = subtable.firstGlyphIndex
555+
last_index = subtable.lastGlyphIndex
556+
if first_index <= glyph_id <= last_index:
557+
height = subtable.metrics.height
558+
advance = width = subtable.metrics.width
559+
bearing_x = subtable.metrics.horiBearingX
560+
bearing_y = subtable.metrics.horiBearingY
561+
break
562+
else:
563+
LOGGER.warning(
564+
f'Unknown bitmap metrics for glyph: {glyph_id}')
565+
continue
566+
else:
567+
data_start = 5 if glyph_format in (1, 2, 8) else 8
568+
data = glyph.data[data_start:]
569+
height, width = glyph.data[0:2]
570+
bearing_x = int.from_bytes(
571+
glyph.data[2:3], 'big', signed=True)
572+
bearing_y = int.from_bytes(
573+
glyph.data[3:4], 'big', signed=True)
574+
advance = glyph.data[4]
553575
position_y = bearing_y - height
554-
glyph_id = font.ttfont.getGlyphID(key)
555576
if glyph_id in chars:
556577
widths[glyph_id - first] = advance
557578
stride = math.ceil(width / 8)
@@ -565,13 +586,10 @@ def generate_pdf(pages, url_fetcher, metadata, fonts, target, zoom,
565586
'subglyphs': None,
566587
}
567588

568-
# Apply glyph format tweaks
569-
glyph_format = glyph.getFormat()
570-
data_start = 5 if glyph_format in (1, 2, 8) else 8
571-
data = glyph.data[data_start:]
589+
# Decode bitmaps
572590
if glyph_format in (1, 6):
573591
glyph_info['bitmap'] = data
574-
elif glyph_format in (2, 7):
592+
elif glyph_format in (2, 5, 7):
575593
padding = (8 - (width % 8)) % 8
576594
bits = bin(int(data.hex(), 16))[2:]
577595
bits = bits.zfill(8 * len(data))

0 commit comments

Comments
 (0)