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

TTF hinting ignored #1858

Closed
500cm opened this issue Apr 8, 2023 · 10 comments
Closed

TTF hinting ignored #1858

500cm opened this issue Apr 8, 2023 · 10 comments
Labels
feature New feature that should be supported
Milestone

Comments

@500cm
Copy link

500cm commented Apr 8, 2023

WeasyPrint seems to be ignoring hinted TTF fonts and resulting in blurry and unclear text. Even when fonts are properly hinted, the output is still not as sharp as expected. Additionally, I couldn't find any option to create proper sharp texts in the documentation or options list. It would be great if WeasyPrint could improve text rendering by properly handling hinted fonts or adding an option to create sharp text output.

Tested with WeasyPrint latest version.

@liZe
Copy link
Member

liZe commented Apr 8, 2023

Hi!

Hinting is done by the PDF renderer, not by WeasyPrint. WeasyPrint includes letters and fonts in the PDF, and the PDF renderer displays the text on the screen, possibly adjusting the position of the letters to the screen pixels, according to the position of the document on the screen, the zoom level and the screen resolution (and many other things such as the hinting preferences of the user). Hinting depends on these parameters that are not fixed when the PDF is generated, I’m not sure there’s something WeasyPrint can do here.

If you have two equivalent documents, one blurry generated by WeasyPrint and another one generated by another tool, don’t hesitate to share them! Maybe there’s a feature we don’t know.

@500cm
Copy link
Author

500cm commented Apr 9, 2023

Hinting is crucial for Windows users to enhance text readability, particularly at smaller font sizes. However, it is not relevant for users of other systems, such as Apple devices. Both TTF and OTF font files contain hinting instructions, and applications like Adobe Acrobat, web browsers, and other PDF renderers on Windows do not optimize fonts on their own, so you can't give up hinting built into fonts.

Most PDF producers that I have used before (PDF libs, Adobe software, etc) generate PDFs with hinting embedded in fonts and do not allow to do it otherwise, because it is a standard and important technology.

The issue arises when creating PDFs using WeasyPrint, as font optimization (enabled by default) disables hinting for fonts. If font optimization is disabled (e.g., optimize_size=("images")), the PDF is generated with hinting, but the file size increases significantly due to the lack of optimization and full font inclusion.

Disabling hinting altogether by default is not a viable solution. It's essential to either set hinting=true in the library or provide an option to use the variable. Below are the WeasyPrint generated PDFs comparing cases with and without font optimization.

To reproduce this problem, create a PDF file containing text with optimization turned on and off. You will see a clear difference on Windows systems with a 92 PPI monitor (not 4K).

pdf

@500cm
Copy link
Author

500cm commented Apr 9, 2023

Two files, with and without hinting

hinting on.pdf
hinting off.pdf

liZe added a commit that referenced this issue Apr 9, 2023
Hinting is now enabled by default. Fix #1858.
@liZe
Copy link
Member

liZe commented Apr 9, 2023

Thanks for sharing your documents and the screenshot.

Disabling hinting altogether by default is not a viable solution.

WeasyPrint is downloaded 10 million times a year, so it may be viable, at least for some people, even without hinting 😄. Funny that nobody ever complained before.

We’ve never met this problem, probably because Poppler (the library used by most PDF readers on Linux) ignores hinting information on purpose. Hopefully, pdf.js does follow hinting tables, so we’re able to test the difference, even on Linux.

(There’s actually another bug in WeasyPrint that’s been fixed in 8a800a0, while solving #1855. This bug sets wrong widths for characters when fonts are not optimized, so you won’t get the exact same rendering with the fixed master branch than what you got with v58.1 + non-optimized fonts.)

Here’s what I get with the fix:

I find the result better with hinting, at least with pdf.js, but a bit different than with full font. Feedback is welcome!

If you want to test various options, you can tweak options here:

options = subset.Options(
retain_gids=True, passthrough_tables=True,
ignore_missing_glyphs=True, hinting=hinting,
desubroutinize=True)
options.drop_tables += ['GSUB', 'GPOS', 'SVG']

@500cm
Copy link
Author

500cm commented Apr 10, 2023

Funny that nobody ever complained before

This is the most surprising part

Thank you, I'll check it

@liZe
Copy link
Member

liZe commented Apr 12, 2023

As the feature was not asked before, we’ve finally disabled the option by default. It’s still available for users who want to keep the hinting information.

With all these new size optimization options, I’m not convinced by the -O option anymore, mainly because it’s complicated to disable one default option. Maybe we should change this.

@500cm
Copy link
Author

500cm commented Apr 12, 2023

Strategically this is not correct. Hinting should not be disabled by the library. It should remain under the control of the font itself. Those who don't need hinting can convert the font to a format without hinting or do automatic hinting, which is common practice.

@500cm
Copy link
Author

500cm commented Apr 12, 2023

By the way, disabling font optimization causes a number of bugs with fonts. They are converted to UTF characters, which are not readable in PDF file (it turns out: 􀍏􀍘.􀍡􀍛) and after some characters are added unnecessary space. Apparently, a separate task should be created for this.

@liZe
Copy link
Member

liZe commented Apr 12, 2023

Strategically this is not correct. Hinting should not be disabled by the library. It should remain under the control of the font itself.

I have to disagree on that point.

I understand that hinting is important for you, and I agree that it may be important for others. But the fact that this issue was opened just 4 days ago is a proof that most users don’t care and will prefer a smaller PDF than correct hinting. That’s sad (I spend so much time adding hinting information in the fonts I create 😢), but that’s a fact. By the way, many PDF readers do a decent job at automatically hinting fonts, and some even voluntarily discard hinting information in embedded fonts.

The feature is now available for the people who care.

Those who don't need hinting can convert the font to a format without hinting or do automatic hinting, which is common practice.

I think that users don’t want to learn how to convert fonts. Many don’t even want to optimize and resize their images themselves! Having these features in WeasyPrint is important in my opinion, they help users to let the tool take care of the topics they don’t want to spend time on. Pre-processing (HTML, CSS, images, fonts…) is always possible, but we have to take care of common options when possible.

They are converted to UTF characters, which are not readable in PDF file (it turns out: 􀍏􀍘.􀍡􀍛)

I can’t easily reproduce this problem. Could you please send an HTML sample and the corresponding PDF in another issue?

after some characters are added unnecessary space

Maybe that’s fixed by 8a800a0. If it’s not, you can open another issue 😄.

@liZe liZe added the feature New feature that should be supported label Apr 12, 2023
@liZe liZe closed this as completed in 2358a01 Apr 12, 2023
@liZe
Copy link
Member

liZe commented Apr 12, 2023

(The issue is now closed because the PR is merged, but feel free to add comments!)

@liZe liZe added this to the 59.0 milestone Apr 13, 2023
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Jun 30, 2023
Version 59.0
------------

Released on 2023-05-11.

This version also includes the changes from unstable b1 version listed
below.

Bug fixes:

* `#1864 <https://github.com/Kozea/WeasyPrint/issues/1864>`_:
  Handle overflow for svg and symbol tags in SVG images
* `#1867 <https://github.com/Kozea/WeasyPrint/pull/1867>`_:
  Remove duplicate compression of attachments
* `d0ad5c1 <https://github.com/Kozea/WeasyPrint/commit/d0ad5c1>`_:
  Override use tag children instead of drawing their references
* `93df1a5 <https://github.com/Kozea/WeasyPrint/commit/93df1a5>`_:
  Don’t resize the same image twice when the --dpi option is set
* `#1874 <https://github.com/Kozea/WeasyPrint/pull/1874>`_:
  Drawn underline and overline behind text


Version 59.0b1
--------------

Released on 2023-04-14.

**This version is experimental, don't use it in production. If you find bugs,
please report them!**

Command-line API:

* The ``--optimize-size`` option and its short equivalent ``-O`` have been
  deprecated. To activate or deactivate different size optimizations, you can
  now use:

  * ``--uncompressed-pdf``,
  * ``--optimize-images``,
  * ``--full-fonts``,
  * ``--hinting``,
  * ``--dpi <resolution>``, and
  * ``--jpeg-quality <quality>``.

* A new ``--cache-folder <folder>`` option has been added to store temporary
  data in the given folder on the disk instead of keeping them in memory.

Python API:

* Global rendering options are now given in ``**options`` instead of dedicated
  parameters, with slightly different names. It means that the signature of the
  ``HTML.render()``, ``HTML.write_pdf()`` and ``Document.write_pdf()`` has
  changed. Here are the steps to port your Python code to v59.0:

  1. Use named parameters for these functions, not positioned parameters.
  2. Rename some the parameters:

     * ``image_cache`` becomes ``cache`` (see below),
     * ``identifier`` becomes ``pdf_identifier``,
     * ``variant`` becomes ``pdf_variant``,
     * ``version`` becomes ``pdf_version``,
     * ``forms`` becomes ``pdf_forms``.

* The ``optimize_size`` parameter of ``HTML.render()``, ``HTML.write_pdf()``
  and ``Document()`` has been removed and will be ignored. You can now use the
  ``uncompressed_pdf``, ``full_fonts``, ``hinting``, ``dpi`` and
  ``jpeg_quality`` parameters that are included in ``**options``.

* The ``cache`` parameter can be included in ``**options`` to replace
  ``image_cache``. If it is a dictionary, this dictionary will be used to store
  temporary data in memory, and can be even shared between multiple documents.
  If it’s a folder Path or string, WeasyPrint stores temporary data in the
  given temporary folder on disk instead of keeping them in memory.

New features:

* `#1853 <https://github.com/Kozea/WeasyPrint/pull/1853>`_,
  `#1854 <https://github.com/Kozea/WeasyPrint/issues/1854>`_:
  Reduce PDF size, with financial support from Code & Co.
* `#1824 <https://github.com/Kozea/WeasyPrint/issues/1824>`_,
  `#1829 <https://github.com/Kozea/WeasyPrint/pull/1829>`_:
  Reduce memory use for images
* `#1858 <https://github.com/Kozea/WeasyPrint/issues/1858>`_:
  Add an option to keep hinting information in embedded fonts

Bug fixes:

* `#1855 <https://github.com/Kozea/WeasyPrint/issues/1855>`_:
  Fix position of emojis in justified text
* `#1852 <https://github.com/Kozea/WeasyPrint/issues/1852>`_:
  Don’t crash when line can be split before trailing spaces
* `#1843 <https://github.com/Kozea/WeasyPrint/issues/1843>`_:
  Fix syntax of dates in metadata
* `#1827 <https://github.com/Kozea/WeasyPrint/issues/1827>`_,
  `#1832 <https://github.com/Kozea/WeasyPrint/pull/1832>`_:
  Fix word-spacing problems with nested tags

Documentation:

* `#1841 <https://github.com/Kozea/WeasyPrint/issues/1841>`_:
  Add a paragraph about unsupported calc() function


Version 58.1
------------

Released on 2023-03-07.

Bug fixes:

* `#1815 <https://github.com/Kozea/WeasyPrint/issues/1815>`_:
  Fix bookmarks coordinates
* `#1822 <https://github.com/Kozea/WeasyPrint/issues/1822>`_,
  `#1823 <https://github.com/Kozea/WeasyPrint/pull/1823>`_:
  Fix vertical positioning for absolute replaced elements

Documentation:

* `#1814 <https://github.com/Kozea/WeasyPrint/pull/1814>`_:
  Fix broken link pointing to samples


Version 58.0
------------

Released on 2023-02-17.

This version also includes the changes from unstable b1 version listed
below.

Bug fixes:

* `#1807 <https://github.com/Kozea/WeasyPrint/issues/1807>`_:
  Don’t crash when out-of-flow box is split in out-of-flow parent
* `#1806 <https://github.com/Kozea/WeasyPrint/issues/1806>`_:
  Don’t crash when fixed elements aren’t displayed yet in aborted line
* `#1809 <https://github.com/Kozea/WeasyPrint/issues/1809>`_:
  Fix background drawing for out-of-the-page transformed boxes


Version 58.0b1
--------------

Released on 2023-02-03.

**This version is experimental, don't use it in production. If you find bugs,
please report them!**

New features:

* `#61 <https://github.com/Kozea/WeasyPrint/issues/61>`_,
  `#1796 <https://github.com/Kozea/WeasyPrint/pull/1796>`_:
  Support PDF forms, with financial support from Personalkollen
* `#1173 <https://github.com/Kozea/WeasyPrint/issues/1173>`_:
  Add style for form fields

Bug fixes:

* `#1777 <https://github.com/Kozea/WeasyPrint/issues/1777>`_:
  Detect JPEG/MPO images as normal JPEG files
* `#1771 <https://github.com/Kozea/WeasyPrint/pull/1771>`_:
  Improve SVG gradients


Version 57.2
------------

Released on 2022-12-23.

Bug fixes:

* `0f2e377 <https://github.com/Kozea/WeasyPrint/commit/0f2e377>`_:
  Print annotations with PDF/A
* `0e9426f <https://github.com/Kozea/WeasyPrint/commit/0e9426f>`_:
  Hide annotations with PDF/UA
* `#1764 <https://github.com/Kozea/WeasyPrint/issues/1764>`_:
  Use reference instead of stream for annotation appearance stream
* `#1783 <https://github.com/Kozea/WeasyPrint/pull/1783>`_:
  Fix multiple font weights for @font-face declarations


Version 57.1
------------

Released on 2022-11-04.

Dependencies:

* `#1754 <https://github.com/Kozea/WeasyPrint/pull/1754>`_:
  Pillow 9.1.0 is now needed

Bug fixes:

* `#1756 <https://github.com/Kozea/WeasyPrint/pull/1756>`_:
  Fix rem font size for SVG images
* `#1755 <https://github.com/Kozea/WeasyPrint/issues/1755>`_:
  Keep format when transposing images
* `#1753 <https://github.com/Kozea/WeasyPrint/issues/1753>`_:
  Don’t use deprecated ``read_text`` function when ``files`` is available
* `#1741 <https://github.com/Kozea/WeasyPrint/issues/1741>`_:
  Generate better manpage
* `#1747 <https://github.com/Kozea/WeasyPrint/issues/1747>`_:
  Correctly set target counters in pages’ absolute elements
* `#1748 <https://github.com/Kozea/WeasyPrint/issues/1748>`_:
  Always set font size when font is changed in line
* `2b05137 <https://github.com/Kozea/WeasyPrint/commit/2b05137>`_:
  Fix stability of font identifiers

Documentation:

* `#1750 <https://github.com/Kozea/WeasyPrint/pull/1750>`_:
  Fix documentation spelling


Version 57.0
------------

Released on 2022-10-18.

This version also includes the changes from unstable b1 version listed
below.

New features:

* `a4fc7a1 <https://github.com/Kozea/WeasyPrint/commit/a4fc7a1>`_:
  Support image-orientation

Bug fixes:

* `#1739 <https://github.com/Kozea/WeasyPrint/issues/1739>`_:
  Set baseline on all flex containers
* `#1740 <https://github.com/Kozea/WeasyPrint/issues/1740>`_:
  Don’t crash when currentColor is set on root svg tag
* `#1718 <https://github.com/Kozea/WeasyPrint/issues/1718>`_:
  Don’t crash with empty bitmap glyphs
* `#1736 <https://github.com/Kozea/WeasyPrint/issues/1736>`_:
  Always use the font’s vector variant when possible
* `eef8b4d <https://github.com/Kozea/WeasyPrint/commit/eef8b4d>`_:
  Always set color and state before drawing
* `#1662 <https://github.com/Kozea/WeasyPrint/issues/1662>`_:
  Use a stable key to store stream fonts
* `#1733 <https://github.com/Kozea/WeasyPrint/issues/1733>`_:
  Don’t remove attachments when adding internal anchors
* `3c4fa50 <https://github.com/Kozea/WeasyPrint/commit/3c4fa50>`_,
  `c215697 <https://github.com/Kozea/WeasyPrint/commit/c215697>`_,
  `d275dac <https://github.com/Kozea/WeasyPrint/commit/d275dac>`_,
  `b04bfff <https://github.com/Kozea/WeasyPrint/commit/b04bfff>`_:
  Fix many bugs related to PDF/UA structure

Performance:

* `dfccf1b <https://github.com/Kozea/WeasyPrint/commit/dfccf1b>`_:
  Use faces as fonts dictionary keys
* `0dc12b6 <https://github.com/Kozea/WeasyPrint/commit/0dc12b6>`_:
  Cache add_font to avoid calling get_face too often
* `75e17bf <https://github.com/Kozea/WeasyPrint/commit/75e17bf>`_:
  Don’t call process_whitespace twice on many children
* `498d3e1 <https://github.com/Kozea/WeasyPrint/commit/498d3e1>`_:
  Optimize __missing__ functions

Documentation:

* `863b3d6 <https://github.com/Kozea/WeasyPrint/commit/863b3d6>`_:
  Update documentation of installation on macOS with Homebrew


Version 57.0b1
--------------

Released on 2022-09-22.

**This version is experimental, don't use it in production. If you find bugs,
please report them!**

New features:

* `#1704 <https://github.com/Kozea/WeasyPrint/pull/1704>`_:
  Support PDF/UA, with financial support from Novareto
* `#1454 <https://github.com/Kozea/WeasyPrint/issues/1454>`_:
  Support variable fonts

Bug fixes:

* `#1058 <https://github.com/Kozea/WeasyPrint/issues/1058>`_:
  Fix bullet position after page break, with financial support from OpenZeppelin
* `#1707 <https://github.com/Kozea/WeasyPrint/issues/1707>`_:
  Fix footnote positioning in multicolumn layout, with financial support from Code & Co.
* `#1722 <https://github.com/Kozea/WeasyPrint/issues/1722>`_:
  Handle skew transformation with only one parameter
* `#1715 <https://github.com/Kozea/WeasyPrint/issues/1715>`_:
  Don’t crash when images are truncated
* `#1697 <https://github.com/Kozea/WeasyPrint/issues/1697>`_:
  Don’t crash when attr() is used in text-decoration-color
* `#1695 <https://github.com/Kozea/WeasyPrint/pull/1695>`_:
  Include language information in PDF metadata
* `#1612 <https://github.com/Kozea/WeasyPrint/issues/1612>`_:
  Don’t lowercase letters when capitalizing text
* `#1700 <https://github.com/Kozea/WeasyPrint/issues/1700>`_:
  Fix crash when rendering footnote with repagination
* `#1667 <https://github.com/Kozea/WeasyPrint/issues/1667>`_:
  Follow EXIF metadata for image rotation
* `#1669 <https://github.com/Kozea/WeasyPrint/issues/1669>`_:
  Take care of floats when remvoving placeholders
* `#1638 <https://github.com/Kozea/WeasyPrint/issues/1638>`_:
  Use the original box when breaking waiting children
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature that should be supported
Projects
None yet
Development

No branches or pull requests

2 participants