Skip to content

Commit

Permalink
Don’t store version and identifier in PDF object
Browse files Browse the repository at this point in the history
We don’t want to duplicate this information, let’s use it only when actually
writing the PDF.
  • Loading branch information
liZe committed Feb 26, 2024
1 parent 4b28713 commit 6bcbd51
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
16 changes: 12 additions & 4 deletions weasyprint/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from .layout import LayoutContext, layout_document
from .logger import PROGRESS_LOGGER
from .matrix import Matrix
from .pdf import generate_pdf
from .pdf import VARIANTS, generate_pdf
from .text.fonts import FontConfiguration


Expand Down Expand Up @@ -391,17 +391,25 @@ def write_pdf(self, target=None, zoom=1, finisher=None, **options):

identifier = options['pdf_identifier']
compress = not options['uncompressed_pdf']
version = options['pdf_version']
variant = options['pdf_variant']

# Set default PDF version for PDF variants.
if version is None and variant:
_, properties = VARIANTS[variant]
if 'version' in properties:
version = properties['version']

if finisher:
finisher(self, pdf)

if target is None:
output = io.BytesIO()
pdf.write(output, pdf.version, identifier, compress)
pdf.write(output, version, identifier, compress)
return output.getvalue()

if hasattr(target, 'write'):
pdf.write(target, pdf.version, identifier, compress)
pdf.write(target, version, identifier, compress)
else:
with open(target, 'wb') as fd:
pdf.write(fd, pdf.version, identifier, compress)
pdf.write(fd, version, identifier, compress)
10 changes: 3 additions & 7 deletions weasyprint/pdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,13 @@ def generate_pdf(document, target, zoom, **options):

# Set properties according to PDF variants
mark = False
variant, version = options['pdf_variant'], options['pdf_version']
variant = options['pdf_variant']
if variant:
variant_function, properties = VARIANTS[variant]
if 'version' in properties:
version = properties['version']
if 'mark' in properties:
mark = properties['mark']

identifier = options['pdf_identifier']
pdf = pydyf.PDF((version or '1.7'), identifier)
pdf = pydyf.PDF()
states = pydyf.Dictionary()
x_objects = pydyf.Dictionary()
patterns = pydyf.Dictionary()
Expand Down Expand Up @@ -268,9 +265,8 @@ def generate_pdf(document, target, zoom, **options):

# Embedded fonts
subset = not options['full_fonts']
hinting = options['hinting']
pdf_fonts = build_fonts_dictionary(
pdf, document.fonts, compress, subset, hinting)
pdf, document.fonts, compress, subset, options)
pdf.add_object(pdf_fonts)
if 'AcroForm' in pdf.catalog:
# Include Dingbats for forms
Expand Down
6 changes: 3 additions & 3 deletions weasyprint/pdf/fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ..logger import LOGGER


def build_fonts_dictionary(pdf, fonts, compress_pdf, subset, hinting):
def build_fonts_dictionary(pdf, fonts, compress_pdf, subset, options):
pdf_fonts = pydyf.Dictionary()
fonts_by_file_hash = {}
for font in fonts.values():
Expand All @@ -24,7 +24,7 @@ def build_fonts_dictionary(pdf, fonts, compress_pdf, subset, hinting):
if subset and not font.used_in_forms:
for file_font in file_fonts:
cmap = {**cmap, **file_font.cmap}
font.clean(cmap, hinting)
font.clean(cmap, options['hinting'])

# Include font
if font.type == 'otf':
Expand Down Expand Up @@ -115,7 +115,7 @@ def build_fonts_dictionary(pdf, fonts, compress_pdf, subset, hinting):
'StemH': font.stemh,
font_file: font_references_by_file_hash[font.hash],
})
if pdf.version <= b'1.4':
if str(options['pdf_version']) <= '1.4': # Cast for bytes and None
cids = sorted(font.widths)
padded_width = int(ceil((cids[-1] + 1) / 8))
bits = ['0'] * padded_width * 8
Expand Down

0 comments on commit 6bcbd51

Please sign in to comment.