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

support @font-face on Windows #592

Merged
merged 3 commits into from Mar 18, 2018
Merged

support @font-face on Windows #592

merged 3 commits into from Mar 18, 2018

Conversation

@Tontyna
Copy link
Contributor

@Tontyna Tontyna commented Mar 15, 2018

UPPs, the branch name is a bit ... misleading ...

Since at the moment I have no access to another OS than Windows and the existing code in weasyprint/fonts.py probably was ok for other platfoms, I implemented the fix in a way that only affects sys.platform.startswith('win').

Switching to FreeType rendering not only enables @font-face; suddenly other CSS properties, like font-stretch and font-variant, start to work as expected -- see #587 -- even the killer css font-size: 0 doesn't crash Pango anymore!

There is one minor drawback on Windows: FontConfiguration can't delete the temporary font files it creates during rendering. I think Pango keeps the file handles open (until the Python process is finished), couldn't find a way to persuade it to let them go...

FontConfig and PangoFT2 not only require the proper libraries but also a working font configuration.

Maybe on Nix it never happens that those config files are missing, but Tom Schoonjans' GTK3 Runtime, recommended in the WeasyPrint docs Installing on Windows, though installing all required DLLs, doesn't create the /etc/fonts folder. I created an issue, status == open.

Lucky me having Inkscape installed with an appropriate etc/fonts...

That's why (besides trying and catching dlopen), before actually using FontConfig, the code checks whether it has access to at least one font.
If that's the case, font rendering is done via FreeType, otherwise it falls back to the, @font-face-less, native system fonts rendering mode.

I tried to support the fontconfig library with an os.environ['FONTCONFIG_PATH'] = 'default/etc/fonts/we/could/add/to/weasyprint' when it complains about a missing fonts.conf.
Doesn't work. Probably again due to the way how DLLs are loaded in Python. The DLL only sees the global environment that the python script is running in. Changes during runtime don't reach the DLL.
Contemplated about FcConfigAppFontAddDir(<WINDOWSFONTDIR>) to circumvent such a situation...

Windows-testers welcome!

Tontyna added 3 commits Mar 14, 2018
When a working `libfontconfig` is detected on Windows, the font
rendering is done via FontConfig and FreeType instead of using the
native Win32 APIs.
@liZe
Copy link
Member

@liZe liZe commented Mar 18, 2018

Thank you ❤️

Since at the moment I have no access to another OS than Windows and the existing code in weasyprint/fonts.py probably was ok for other platfoms, I implemented the fix in a way that only affects sys.platform.startswith('win').

That's a good idea.

Switching to FreeType rendering not only enables @font-face; suddenly other CSS properties, like font-stretch and font-variant, start to work as expected -- see #587 -- even the killer css font-size: 0 doesn't crash Pango anymore!

Great! It would be a good idea in the future to find how to avoid crashes with font-size: 0, but that's another story. There were crashes on Unix too, but they were avoided by fed326d.

There is one minor drawback on Windows: FontConfiguration can't delete the temporary font files it creates during rendering. I think Pango keeps the file handles open (until the Python process is finished), couldn't find a way to persuade it to let them go...

That's not the first time we hit this problem, I'm not sure there's a solution.

Maybe on Nix it never happens that those config files are missing,

True.

but Tom Schoonjans' GTK3 Runtime, recommended in the WeasyPrint docs Installing on Windows, though installing all required DLLs, doesn't create the /etc/fonts folder. I created an issue, status == open.

(tschoonj/GTK-for-Windows-Runtime-Environment-Installer#14 for the record)

That's why (besides trying and catching dlopen), before actually using FontConfig, the code checks whether it has access to at least one font.
If that's the case, font rendering is done via FreeType, otherwise it falls back to the, @font-face-less, native system fonts rendering mode.

It's perfect.

I merge and I'll try to make sure that it doesn't hurt other OSes and Windows users with no usable FontConfig.

@liZe liZe merged commit f788ece into Kozea:master Mar 18, 2018
1 check passed
@liZe
Copy link
Member

@liZe liZe commented Mar 18, 2018

Fix #376.

@liZe liZe added this to the 43 milestone Mar 18, 2018
@liZe liZe added the feature label Mar 18, 2018
@Tontyna Tontyna deleted the win-font-family branch Mar 19, 2018
@Tontyna
Copy link
Contributor Author

@Tontyna Tontyna commented Jun 12, 2018

As promised, Tom Schoonjans' today's update of the GTK+ for Windows Runtime Environment Installer includes the fontconfig configuration files.

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Nov 14, 2018
Version 43
----------

Released on 2018-11-09.

Bug fixes:

* `#726 <https://github.com/Kozea/WeasyPrint/issues/726>`_:
  Make empty strings clear previous values of named strings
* `#729 <https://github.com/Kozea/WeasyPrint/issues/729>`_:
  Include tools in packaging

This version also includes the changes from unstable rc1 and rc2 versions
listed below.

Version 43rc2
-------------

Released on 2018-11-02.

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

Bug fixes:

* `#706 <https://github.com/Kozea/WeasyPrint/issues/706>`_:
  Fix text-indent at the beginning of a page
* `#687 <https://github.com/Kozea/WeasyPrint/issues/687>`_:
  Allow query strings in file:// URIs
* `#720 <https://github.com/Kozea/WeasyPrint/issues/720>`_:
  Optimize minimum size calculation of long inline elements
* `#717 <https://github.com/Kozea/WeasyPrint/issues/717>`_:
  Display <details> tags as blocks
* `#691 <https://github.com/Kozea/WeasyPrint/issues/691>`_:
  Don't recalculate max content widths when distributing extra space for tables
* `#722 <https://github.com/Kozea/WeasyPrint/issues/722>`_:
  Fix bookmarks and strings set on images
* `#723 <https://github.com/Kozea/WeasyPrint/issues/723>`_:
  Warn users when string() is not used in page margin


Version 43rc1
-------------

Released on 2018-10-15.

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

Dependencies:

* Python 3.4+ is now needed, Python 2.x is not supported anymore
* Cairo 1.15.4+ is now needed, but 1.10+ should work with missing features
  (such as links, outlines and metadata)
* Pdfrw is not needed anymore

New features:

* `Beautiful website <https://weasyprint.org>`_
* `#579 <https://github.com/Kozea/WeasyPrint/issues/579>`_:
  Initial support of flexbox
* `#592 <https://github.com/Kozea/WeasyPrint/pull/592>`_:
  Support @font-face on Windows
* `#306 <https://github.com/Kozea/WeasyPrint/issues/306>`_:
  Add a timeout parameter to the URL fetcher functions
* `#594 <https://github.com/Kozea/WeasyPrint/pull/594>`_:
  Split tests using modern pytest features
* `#599 <https://github.com/Kozea/WeasyPrint/pull/599>`_:
  Make tests pass on Windows
* `#604 <https://github.com/Kozea/WeasyPrint/pull/604>`_:
  Handle target counters and target texts
* `#631 <https://github.com/Kozea/WeasyPrint/pull/631>`_:
  Enable counter-increment and counter-reset in page context
* `#622 <https://github.com/Kozea/WeasyPrint/issues/622>`_:
  Allow pathlib.Path objects for HTML, CSS and Attachment classes
* `#674 <https://github.com/Kozea/WeasyPrint/issues/674>`_:
  Add extensive installation instructions for Windows

Bug fixes:

* `#558 <https://github.com/Kozea/WeasyPrint/issues/558>`_:
  Fix attachments
* `#565 <https://github.com/Kozea/WeasyPrint/issues/565>`_,
  `#596 <https://github.com/Kozea/WeasyPrint/issues/596>`_,
  `#539 <https://github.com/Kozea/WeasyPrint/issues/539>`_:
  Fix many PDF rendering, printing and compatibility problems
* `#614 <https://github.com/Kozea/WeasyPrint/issues/614>`_:
  Avoid crashes and endless loops caused by a Pango bug
* `#662 <https://github.com/Kozea/WeasyPrint/pull/662>`_:
  Fix warnings and errors when generating documentation
* `#666 <https://github.com/Kozea/WeasyPrint/issues/666>`_,
  `#685 <https://github.com/Kozea/WeasyPrint/issues/685>`_:
  Fix many table layout rendering problems
* `#680 <https://github.com/Kozea/WeasyPrint/pull/680>`_:
  Don't crash when there's no font available
* `#662 <https://github.com/Kozea/WeasyPrint/pull/662>`_:
  Fix support of some align values in tables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants