Skip to content

Commit 2b05137

Browse files
committed
Fix stability of font identifiers
1 parent dcdc63d commit 2b05137

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

weasyprint/pdf/stream.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io
44
import struct
55
from functools import lru_cache
6+
from hashlib import md5
67

78
import pydyf
89
from fontTools import subset
@@ -34,14 +35,14 @@ def __init__(self, pango_font):
3435
self.style = pango.pango_font_description_get_style(description)
3536
self.family = ffi.string(
3637
pango.pango_font_description_get_family(description))
37-
digest = pango.pango_font_description_hash(description)
38+
description_string = ffi.string(
39+
pango.pango_font_description_to_string(description))
40+
# Never use the built-in hash function here: it’s not stable
3841
self.hash = ''.join(
39-
chr(65 + letter % 26) + chr(65 + (letter << 6) % 26) for letter
40-
in (hash(str(digest)) % (2 ** (3 * 8))).to_bytes(3, 'big'))
42+
chr(65 + letter % 26) for letter
43+
in md5(description_string).digest()[:6])
4144

4245
# Name
43-
description_string = ffi.string(
44-
pango.pango_font_description_to_string(description))
4546
fields = description_string.split(b' ')
4647
if fields and b'=' in fields[-1]:
4748
fields.pop() # Remove variations

0 commit comments

Comments
 (0)