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

FreeType 2.7.1 and Fontconfig defaults #23819

Merged
merged 4 commits into from Mar 20, 2017
Merged

Conversation

ttuegel
Copy link
Member

@ttuegel ttuegel commented Mar 12, 2017

Motivation for this change

Besides updating FreeType to the latest version 2.7.1, a number of changes are made to the patches we carry on FreeType and Cairo which I believe will reduce maintenance burden and improve security. To account for the new TrueType interpreter in FreeType 2.7, the defaults provided by fontconfig-ultimate (tuned for the Infinality interpreter) have been replaced by my own derivative thereof, fontconfig-penultimate. Along the way, I discovered that fontconfig-ultimate wasn't doing the right thing in several cases (probably because it isn't properly maintained). The new defaults are significantly simpler.

The commit message has most of the relevant details:

The Infinality bytecode interpreter is removed in favor of the new v40 TrueType
interpreter. In the past, the Infinality interpreter provided support for
ClearType-style hinting instructions while the default interpreter (then v35)
provided support only for original TrueType-style instructions. The v40
interpreter corrects this deficiency, so the Infinality interpreter is no longer
necessary.

To understand why the Infinality interpreter is no longer necessary, we should
understand how ClearType differs from TrueType and how the v40 interpreter
works. The following is a summary of information available on the FreeType
website [1] mixed with my own editorializing.

TrueType instructions use horizontal and vertical hints to improve glyph
rendering. Before TrueType, fonts were only vertically hinted; horizontal hints
improved rendering by snapping stems to pixel boundaries. Horizontal hinting is
a risk because it can significantly distort glyph shapes and kerning. Extensive
testing at different resolutions is needed to perfect the TrueType
hints. Microsoft invested significant effort to do this with its "Core fonts for
the Web" project, but few other typefaces have seen this level of attention.

With the advent of subpixel rendering, the effective horizontal resolution of
most displays increased significantly. ClearType eschews horizontal hinting in
favor of horizontal supersampling. Most fonts are designed for the Microsoft
bytecode interpreter, which implements a compatibility mode with
TrueType-style (horizontal and vertical) instructions. However, applying the
full horizontal hints to subpixel-rendered fonts leads to color fringes and
inconsistent stem widths. The Infinality interpreter implements several
techniques to mitigate these problems, going so far as to embed font- and
glyph-specific hacks in the interpreter. On the other hand, the v40 interpreter
ignores the horizontal hinting instructions so that glyphs render as they are
intended to on the Microsoft interpreter. Without the horizontal hints, the
problems of glyph and kerning distortion, color fringes, and inconsistent stem
widths--the problems the Infinality interpreter was created to solve--simply
don't occur in the first place.

There are also security concerns which motivate removing the Infinality patches.
Although there is an updated version of the Infinality interpreter for FreeType
2.7, the lack of a consistent upstream maintainer is a security concern. The
interpreter is a Turing-complete virtual machine which has had security
vulnerabilities in the past. While the default interpreter is used in billions
of devices and is maintained by an active developer, the Infinality interpreter
is neither scrutinized nor maintained. We will probably never know if there are
defects in the Infinality interpreter, and if they were discovered they would
likely never be fixed. I do not think that is an acceptable situtation for a
core library like FreeType.

Dropping the Infinality patches means that font rendering will be less
customizable. I think this is an acceptable trade-off. The Infinality
interpreter made many compromises to mitigate the problems with horizontal
hinting; the main purpose of customization is to tailor these compromises to the
user's preferences. The new interpreter does not have to make these compromises
because it renders fonts as their designers intended, so this level of
customization is not necessary.

The Infinality-associated patches are also removed from cairo. These patches
only set the default rendering options in case they aren't set though
Fontconfig. On NixOS, the rendering options are always set in Fontconfig, so
these patches never actually did anything for us!

The Fontconfig test suite is patched to account for a quirk in the way PCF fonts
are named.

The fontconfig option hintstyle is no longer configurable in NixOS. This
option selects the TrueType interpreter; the v40 interpreter is hintslight and
the older v35 interpreter is hintmedium or hintfull (which have actually
always been the same thing). The setting may still be changed through the
localConf option or by creating a user Fontconfig file.

Users with HiDPI displays should probably disable hinting and antialiasing: at
best they have no visible effect.

The fontconfig-ultimate settings are still available in NixOS, but they are no
longer the default. They still work, but their main purpose is to set rendering
quirks which are no longer necessary and may actually be
detrimental (e.g. setting hintfull for some fonts). Also, the vast array of
font substitutions provided is not an appropriate default; the default setting
should be to give the user the font they asked for.

[1]. https://www.freetype.org/freetype2/docs/subpixel-hinting.html

Things done
  • Tested using sandboxing
    (nix.useSandbox on NixOS,
    or option build-use-sandbox in nix.conf
    on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • Linux
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Fits CONTRIBUTING.md.

I expect these changes to be controversial, so I will not merge immediately to allow for discussion.

The Infinality bytecode interpreter is removed in favor of the new v40 TrueType
interpreter. In the past, the Infinality interpreter provided support for
ClearType-style hinting instructions while the default interpreter (then v35)
provided support only for original TrueType-style instructions. The v40
interpreter corrects this deficiency, so the Infinality interpreter is no longer
necessary.

To understand why the Infinality interpreter is no longer necessary, we should
understand how ClearType differs from TrueType and how the v40 interpreter
works. The following is a summary of information available on the FreeType
website [1] mixed with my own editorializing.

TrueType instructions use horizontal and vertical hints to improve glyph
rendering. Before TrueType, fonts were only vertically hinted; horizontal hints
improved rendering by snapping stems to pixel boundaries. Horizontal hinting is
a risk because it can significantly distort glyph shapes and kerning. Extensive
testing at different resolutions is needed to perfect the TrueType
hints. Microsoft invested significant effort to do this with its "Core fonts for
the Web" project, but few other typefaces have seen this level of attention.

With the advent of subpixel rendering, the effective horizontal resolution of
most displays increased significantly. ClearType eschews horizontal hinting in
favor of horizontal supersampling. Most fonts are designed for the Microsoft
bytecode interpreter, which implements a compatibility mode with
TrueType-style (horizontal and vertical) instructions. However, applying the
full horizontal hints to subpixel-rendered fonts leads to color fringes and
inconsistent stem widths. The Infinality interpreter implements several
techniques to mitigate these problems, going so far as to embed font- and
glyph-specific hacks in the interpreter. On the other hand, the v40 interpreter
ignores the horizontal hinting instructions so that glyphs render as they are
intended to on the Microsoft interpreter. Without the horizontal hints, the
problems of glyph and kerning distortion, color fringes, and inconsistent stem
widths--the problems the Infinality interpreter was created to solve--simply
don't occur in the first place.

There are also security concerns which motivate removing the Infinality patches.
Although there is an updated version of the Infinality interpreter for FreeType
2.7, the lack of a consistent upstream maintainer is a security concern. The
interpreter is a Turing-complete virtual machine which has had security
vulnerabilities in the past. While the default interpreter is used in billions
of devices and is maintained by an active developer, the Infinality interpreter
is neither scrutinized nor maintained. We will probably never know if there are
defects in the Infinality interpreter, and if they were discovered they would
likely never be fixed. I do not think that is an acceptable situtation for a
core library like FreeType.

Dropping the Infinality patches means that font rendering will be less
customizable. I think this is an acceptable trade-off. The Infinality
interpreter made many compromises to mitigate the problems with horizontal
hinting; the main purpose of customization is to tailor these compromises to the
user's preferences. The new interpreter does not have to make these compromises
because it renders fonts as their designers intended, so this level of
customization is not necessary.

The Infinality-associated patches are also removed from cairo. These patches
only set the default rendering options in case they aren't set though
Fontconfig. On NixOS, the rendering options are always set in Fontconfig, so
these patches never actually did anything for us!

The Fontconfig test suite is patched to account for a quirk in the way PCF fonts
are named.

The fontconfig option `hintstyle` is no longer configurable in NixOS. This
option selects the TrueType interpreter; the v40 interpreter is `hintslight` and
the older v35 interpreter is `hintmedium` or `hintfull` (which have actually
always been the same thing). The setting may still be changed through the
`localConf` option or by creating a user Fontconfig file.

Users with HiDPI displays should probably disable hinting and antialiasing: at
best they have no visible effect.

The fontconfig-ultimate settings are still available in NixOS, but they are no
longer the default. They still work, but their main purpose is to set rendering
quirks which are no longer necessary and may actually be
detrimental (e.g. setting `hintfull` for some fonts). Also, the vast array of
font substitutions provided is not an appropriate default; the default setting
should be to give the user the font they asked for.

[1]. https://www.freetype.org/freetype2/docs/subpixel-hinting.html
@mention-bot
Copy link

@ttuegel, thanks for your PR! By analyzing the history of the files in this pull request, we identified @vcunat, @edolstra and @abbradar to be potential reviewers.

@abbradar
Copy link
Member

I, for one, welcome our new penultimate overlords!

More seriously, nice to have freetype-ultimate cleaned up with something from upstream, we've been even dragging along 3rd party patches to make it build lately. From your overview it's not immediately obvious what is fontconfig-penultimate and how it differs from fontconfig-ultimate (is it a maintained fork or something with different goals?). Personally I feel okay about reducing configurability traded for less hacks and more valid implementation, however this needs an entry in the changelog.

@ttuegel
Copy link
Member Author

ttuegel commented Mar 20, 2017

fontconfig-penultimate is my own fork; it's fontconfig-ultimate without substitutions or font-specific rendering hacks. Rather than being configurable, it's supposed to provide a set of static defaults.

For the Changelog, I said,

 Updated to FreeType 2.7.1, including a new TrueType engine.
 The new engine replaces the Infinality engine which was the default in
 NixOS. The default font rendering settings are now provided by
 fontconfig-penultimate, replacing fontconfig-ultimate; the new defaults
 are less invasive and provide rendering that is more consistent with
 other systems and hopefully with each font designer's intent. Some
 system-wide configuration has been removed from the Fontconfig NixOS
 module where user Fontconfig settings are available.

Should I be more detailed? I thought for the Changelog, a high-level summary would be sufficient.

@abbradar
Copy link
Member

I haven't tested your changes but high-level things now seem okay to me. Thanks!

@ttuegel
Copy link
Member Author

ttuegel commented Mar 20, 2017

I have tested this myself enough to be sure it's not obviously broken, but I think it will not see other testing until Hydra builds it, so in we go!

@ttuegel ttuegel merged commit 1b0d9e9 into NixOS:staging Mar 20, 2017
@ttuegel ttuegel deleted the freetype branch March 20, 2017 16:43
@abbradar
Copy link
Member

My rendered fonts has become thinner and... worse (subjectively). I'll try to find out differences later but maybe you have any immediate ideas? The fonts themselves has not changed, so this probably is something about hinting/antialiasing. I use default fontconfig settings sans DPI, difference is most visible with Source Code Pro (in Emacs).

@vcunat
Copy link
Member

vcunat commented Mar 27, 2017

It might depend on the particular screen, too. I've seen some screenshot of Apple-rendered fonts and it was ugly on my screen (visible color fringes etc.) but it reportedly looked perfect on the original device...

@abbradar
Copy link
Member

The screen is the same (a laptop screen) so it shouldn't make a difference...

@vcunat
Copy link
Member

vcunat commented Mar 27, 2017

@abbradar: I meant that it's possible that it now appears ugly on your screen but not on "ours".

@abbradar
Copy link
Member

Indeed. I'll just try to figure out differences in fc-pattern results.

@vcunat
Copy link
Member

vcunat commented Mar 27, 2017

I must say that at least new nixpkgs (updated freetype) + old nixos (old /etc/) -> thinner and significantly uglier rendering of Linux Libertine O (e.g. i glyph in running text).

@vcunat
Copy link
Member

vcunat commented Mar 27, 2017

@abbradar: oh, wait. Selecting "full hinting" is likely to be the culprit. Better use "slight hinting", AFAIK. EDIT: I don't have a clue why I had "full" on this system, but it looked OK only with infinality.

@abbradar
Copy link
Member

abbradar commented Mar 27, 2017

Seems it's already like that:

$ fc-pattern -c -d 'Source Code Pro'
Pattern has 27 elts (size 32)
	family: "Source Code Pro"(s) "Source Code Pro"(w) "Bitstream Vera Sans Mono"(w) "DejaVu Sans Mono"(w) "Inconsolata"(w) "Andale Mono"(w) "Courier New"(w) "Cumberland AMT"(w) "Luxi Mono"(w) "Nimbus Mono L"(w) "Nimbus Mono"(w) "Courier"(w) "Miriam Mono"(w) "VL Gothic"(w) "IPAMonaGothic"(w) "IPAGothic"(w) "Sazanami Gothic"(w) "Kochi Gothic"(w) "AR PL KaitiM GB"(w) "MS Gothic"(w) "UmePlus Gothic"(w) "NSimSun"(w) "MingLiu"(w) "AR PL ShanHeiSun Uni"(w) "AR PL New Sung Mono"(w) "HanyiSong"(w) "AR PL SungtiL GB"(w) "AR PL Mingti2L Big5"(w) "ZYSong18030"(w) "NanumGothicCoding"(w) "NanumGothic"(w) "UnDotum"(w) "Baekmuk Dotum"(w) "Baekmuk Gulim"(w) "TlwgTypo"(w) "TlwgTypist"(w) "TlwgTypewriter"(w) "TlwgMono"(w) "Hasida"(w) "Mitra Mono"(w) "GF Zemen Unicode"(w) "Hapax Berbère"(w) "Lohit Bengali"(w) "Lohit Gujarati"(w) "Lohit Hindi"(w) "Lohit Marathi"(w) "Lohit Maithili"(w) "Lohit Kashmiri"(w) "Lohit Konkani"(w) "Lohit Nepali"(w) "Lohit Sindhi"(w) "Lohit Punjabi"(w) "Lohit Tamil"(w) "Meera"(w) "Lohit Malayalam"(w) "Lohit Kannada"(w) "Lohit Telugu"(w) "Lohit Oriya"(w) "LKLUG"(w) "DejaVu Sans Mono"(w) "WenQuanYi Zen Hei Mono"(w) "FreeMono"(w) "monospace"(w) "Terafik"(w)
	familylang: "en"(s) "en-us"(w)
	stylelang: "en"(s) "en-us"(w)
	fullnamelang: "en"(s) "en-us"(w)
	slant: 0(i)(s)
	weight: 80(i)(s)
	width: 100(i)(s)
	size: 12(f)(s)
	pixelsize: 16(f)(s)
	antialias: True(w) True(w)
	hintstyle: 1(i)(w) 1(i)(w) 1(i)(w)
	hinting: True(w) True(w)
	verticallayout: False(s)
	autohint: True(w) False(w)
	globaladvance: True(s)
	dpi: 96(f)(s)
	rgba: 1(i)(w) 1(i)(w)
	scale: 1(f)(s)
	lang: "en"(w)
	fontversion: 2147483647(i)(s)
	embeddedbitmap: True(s)
	decorative: False(s)
	lcdfilter: 1(i)(w) 1(i)(w)
	namelang: "en"(s)
	prgname: "fc-pattern"(s)
	symbol: False(s)
	bitmap_monospace: False(w)

hintstyle=1 is "slight hinting". BTW I can't understand how to read this correctly (why some flags are repeated?).

@vcunat
Copy link
Member

vcunat commented Mar 27, 2017

Hmm, killing the "full hinting" global default fixed all visual issues I could see here, but maybe that's unrelated to what you're seeing.

@ttuegel
Copy link
Member Author

ttuegel commented Mar 27, 2017

I use default fontconfig settings sans DPI, difference is most visible with Source Code Pro (in Emacs).

@abbradar I too use Source Code Pro in Emacs. The old fontconfig-ultimate settings substitute Source Code Pro Medium for Source Code Pro. The new settings (correctly) resolve Source Code Pro to Source Code Pro Regular which is thinner and (in my opinion) uglier. I recommend setting your Emacs font to Source Code Pro Medium.

For debugging, you actually want to run FC_DEBUG=1 fc-match "Source Code Pro". fc-pattern only shows you the pattern that will be generated; it does not show actual results! For example, fc-pattern will happily report no error for fonts that don't exist.

@abbradar
Copy link
Member

The problem was actually in two places:

  1. What @ttuegel mentioned -- Source Code Pro became a different font;
  2. I had Full Hinting selected in XFCE settings D:

It seems much better now, thank you all!

@vcunat
Copy link
Member

vcunat commented Mar 27, 2017

🎉 I guess we should add those two points to the release notes for future/17.09?

@abbradar
Copy link
Member

abbradar commented Mar 27, 2017

Maybe think of some generic mechanism to find out how the font is named now? So that we can recommend "use fc-foo old-font, search for Name: new-font there" or something.

EDIT: this doesn't seem possible without pulling the whole fontconfig-ultimate to the user's machine and conjuring a script to find and report differences. So maybe a generic warning is good enough...

@ttuegel
Copy link
Member Author

ttuegel commented Mar 28, 2017

I think it's enough to suggest the user check their hinting settings if rendering seems off and use fc-list to see if a font name has changed.

@oxij
Copy link
Member

oxij commented Mar 31, 2017 via email

@ttuegel
Copy link
Member Author

ttuegel commented Apr 1, 2017

The diff of FC_DEBUG=1 fc-match 'unifont:size=12:antialias=false' outputs between configurations gives

Could you please pastebin the full before and after? The diff is missing important context.

Also, generally, changes like - autohint: False(s) + autohint: True(w) False(w) look suspicious to me. Not the "False -> True" part, but the multiplication part. I use the super-vanilla one-line default config, why does the default config produce some overrides? (I assume those sequences are overrides.)

They are not overrides, more like a default and a fallback. When there are multiple entries in a field, Fontconfig takes the first one it can satisfy. In this case, that is perfectly fine if it occurs in the pattern debugging, but without the omitted context I can't tell if that's line is from a pattern or from a match.

In the worst case, I can live with ugly glyphs in conky, but this feels like a regression to me.

It certainly is a regression. I didn't look hard at bitmap font rendering because, well, they're supposed to be bitmaps! I'm also a little confused why there's a unifont.ttf file...

@ttuegel
Copy link
Member Author

ttuegel commented Apr 1, 2017

@oxij Using unifont:size=12:antialias=false:hinting=false fixes the problem for me, could you confirm that it works for you? I suspect this is related to changes to the FreeType interpreter. I don't see why bitmap fonts should use hinting anyway. If this solves the problem for you, I will add a rule to our Fontconfig settings for Unifont.

@oxij
Copy link
Member

oxij commented Apr 3, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants