mirror of
https://github.com/Kozea/WeasyPrint.git
synced 2024-10-05 00:21:15 +03:00
Optimize fonts
This commit is contained in:
parent
5fea96e5ca
commit
30cc2c1105
@ -16,6 +16,8 @@ from subprocess import run
|
||||
from urllib.parse import unquote, urlsplit
|
||||
|
||||
import pydyf
|
||||
from fontTools import subset
|
||||
from fontTools.ttLib import TTFont
|
||||
from PIL import Image
|
||||
from weasyprint.layout import LayoutContext
|
||||
|
||||
@ -933,9 +935,21 @@ class Document:
|
||||
# Embeded fonts
|
||||
resources['Font'] = pydyf.Dictionary()
|
||||
for font_hash, font in stream._fonts.items():
|
||||
font_type = 'otf' if font.file_content[:4] == b'OTTO' else 'ttf'
|
||||
# Optimize font
|
||||
full_font = io.BytesIO(font.file_content)
|
||||
optimized_font = io.BytesIO()
|
||||
ttfont = TTFont(full_font)
|
||||
options = subset.Options(retain_gids=True)
|
||||
subsetter = subset.Subsetter(options)
|
||||
subsetter.populate(gids=font.cmap)
|
||||
subsetter.subset(ttfont)
|
||||
ttfont.save(optimized_font)
|
||||
content = optimized_font.getvalue()
|
||||
|
||||
# Include font
|
||||
font_type = 'otf' if content[:4] == b'OTTO' else 'ttf'
|
||||
compressobj = zlib.compressobj()
|
||||
compressed = compressobj.compress(font.file_content)
|
||||
compressed = compressobj.compress(content)
|
||||
compressed += compressobj.flush()
|
||||
font_extra = pydyf.Dictionary({
|
||||
'Filter': '/FlateDecode',
|
||||
@ -943,7 +957,7 @@ class Document:
|
||||
if font_type == 'otf':
|
||||
font_extra['Subtype'] = '/OpenType'
|
||||
else:
|
||||
font_extra['Length1'] = len(font.file_content)
|
||||
font_extra['Length1'] = len(content)
|
||||
font_stream = pydyf.Stream([compressed], font_extra)
|
||||
pdf.add_object(font_stream)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user