Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

font_info not updated in Document's write_pdf font descriptor loop #1357

Closed
aschmitz opened this issue May 18, 2021 · 2 comments
Closed

font_info not updated in Document's write_pdf font descriptor loop #1357

aschmitz opened this issue May 18, 2021 · 2 comments
Labels
bug Existing features not working as expected
Milestone

Comments

@aschmitz
Copy link
Contributor

aschmitz commented May 18, 2021

When embedding fonts, write_pdf uses a variable for font_type to determine whether to describe the font as FontFile3 (with OpenType subtype) or FontFile2, and which CIDFontType to give. It's used on several lines, but a representative sample is:

(f'FontFile{"3" if font_type == "otf" else "2"}'):

However, this variable is only set in the loop before these fonts are output:

font_type = 'otf' if content[:4] == b'OTTO' else 'ttf'

As a result, if both TTF and OTF fonts are included in a document, at least one of them will be output with the wrong type. Some PDF viewers (at a quick test: PDF.js, PDFium, and Poppler) seem to not mind this discrepancy, but macOS' Preview.app appears to substitute a slightly-deterministic built-in font.

I had first noticed this on a PDF file with a rendering issue in Preview.app. A minimized sample HTML document is as follows, and requires the Inter font and Ubuntu Mono font (the former installed from the GitHub release, the latter from the fonts-ubuntu Ubuntu package). There is a tab character before the final "a":

<style>
  html {
    font-family: Inter;
  }
  code {
    font-family: Ubuntu Mono;
    white-space: pre-wrap;
  }
</style>
<code>Example</code>
text
<code>	a</code>

That produces this PDF.

In PDFium, this renders as expected:

1357-pdfium

However, in Preview.app, it appears to use a built-in PDF font (Helvetica?):

1357-previewapp

A couple curiosities: using a different body font (such as Noto Sans) can make the problem disappear (which tracks with the same file type in both fonts fixing the problem), and removing the white-space: pre-wrap line (or the tab before the a) appears to make Preview.app render the code blocks in Courier Bold(?).

My current build process makes it a pain to actually modify WeasyPrint to test changes, but I'm hopeful that fixing this will resolve the issue, since it seems to be the root of the only nontrivial differences I can find between the PDF files when swapping fonts.

@liZe liZe added the bug Existing features not working as expected label May 18, 2021
@liZe liZe closed this as completed in 5005459 May 18, 2021
@liZe
Copy link
Member

liZe commented May 18, 2021

Thanks a lot for this perfect bug report ❤️.

@liZe liZe added this to the 53.0b2 milestone May 18, 2021
@aschmitz
Copy link
Contributor Author

Awesome, thanks! I can confirm that 5005459 fixes the issue with my larger test documents as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Existing features not working as expected
Projects
None yet
Development

No branches or pull requests

2 participants