1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-04 16:07:57 +03:00

Handle font collections

Fix #1393.
This commit is contained in:
Guillaume Ayoub 2021-07-29 20:42:22 +02:00
parent d5a7ec1f4a
commit 08583d39bb
3 changed files with 9 additions and 6 deletions

View File

@ -66,7 +66,7 @@ def _w3c_date_to_pdf(string, attr_name):
class Font:
def __init__(self, file_content, pango_font):
def __init__(self, file_content, pango_font, index):
pango_metrics = pango.pango_font_get_metrics(pango_font, ffi.NULL)
self._font_description = pango.pango_font_describe(pango_font)
self.family = ffi.string(pango.pango_font_description_get_family(
@ -79,7 +79,8 @@ class Font:
sha.update(description_string)
self.file_content = file_content
self.file_hash = hash(file_content)
self.index = index
self.file_hash = hash(file_content + bytes(index))
self.hash = ''.join(
chr(65 + letter % 26) for letter in sha.digest()[:6])
self.name = (
@ -210,8 +211,8 @@ class Stream(pydyf.Stream):
self._states[key] = pydyf.Dictionary({'ca': alpha})
super().set_state(key)
def add_font(self, font_hash, font_content, pango_font):
self._document.fonts[font_hash] = Font(font_content, pango_font)
def add_font(self, font_hash, font_content, pango_font, index):
self._document.fonts[font_hash] = Font(font_content, pango_font, index)
return self._document.fonts[font_hash]
def get_fonts(self):
@ -1246,7 +1247,7 @@ class Document:
full_font = io.BytesIO(fonts[0].file_content)
optimized_font = io.BytesIO()
try:
ttfont = TTFont(full_font)
ttfont = TTFont(full_font, fontNumber=fonts[0].index)
options = subset.Options(
retain_gids=True, passthrough_tables=True,
ignore_missing_glyphs=True, notdef_glyph=True)

View File

@ -1083,7 +1083,8 @@ def draw_first_line(stream, textbox, text_overflow, block_ellipsis, x, y):
hb_blob = harfbuzz.hb_face_reference_blob(hb_face)
hb_data = harfbuzz.hb_blob_get_data(hb_blob, stream.length)
file_content = ffi.unpack(hb_data, int(stream.length[0]))
font = stream.add_font(font_hash, file_content, pango_font)
index = harfbuzz.hb_face_get_index(hb_face)
font = stream.add_font(font_hash, file_content, pango_font, index)
# Positions of the glyphs in the UTF-8 string
utf8_positions = [offset + clusters[i] for i in range(1, num_glyphs)]

View File

@ -17,6 +17,7 @@ ffi.cdef('''
typedef ... hb_blob_t;
hb_face_t * hb_font_get_face (hb_font_t *font);
hb_blob_t * hb_face_reference_blob (hb_face_t *face);
unsigned int hb_face_get_index (const hb_face_t *face);
const char * hb_blob_get_data (hb_blob_t *blob, unsigned int *length);
// Pango