Skip to content

Commit

Permalink
Fix stability of font identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Oct 21, 2022
1 parent dcdc63d commit 2b05137
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions weasyprint/pdf/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io
import struct
from functools import lru_cache
from hashlib import md5

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

# Name
description_string = ffi.string(
pango.pango_font_description_to_string(description))
fields = description_string.split(b' ')
if fields and b'=' in fields[-1]:
fields.pop() # Remove variations
Expand Down

0 comments on commit 2b05137

Please sign in to comment.