You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Truetype fonts not being rendered correctly with the TEXT method #4177
Created an image suitable for the Pimoroni InkyHat display
Added italic text onto the image object at different places
What did you expect to happen?
The whole of the last character of the text to be displayed as it did to render correctly on Stretch on the Pi Zero
Looking at the font from a Mac (Mojave), the characters look fine.
What actually happened?
Depending on the length of the text, some or all of the last character is missing when using some italic fonts. If the text length is around 105 - 110 pixels then you can lose some / all of the very last character.
Padding out the text string with spaces so it is longer than 110 pixels fixes the issue
What are your OS, Python and Pillow versions?
OS: Raspbian Buster Lite (10.1) Kernel 4.19.75+ TIFF incorrect colours #1270 Tue Sep 24 18:38:54 BST 2019 armv6l GNU/Linux (running on a Pi Zero)
Python: 3.7.3 - not tested under Python 2
Pillow: 6.2.1 (installed using pip3)
The fonts used are available from Font Squirrel - the two are:
changed the title [-]Italic true type fonts not being rendered correctly with the TEXT method[/-][+]Italic truetype fonts not being rendered correctly with the TEXT method[/+]on Oct 29, 2019
changed the title [-]Italic truetype fonts not being rendered correctly with the TEXT method[/-][+]Truetype fonts not being rendered correctly with the TEXT method[/+]on Oct 29, 2019
I can replicate this error on macOS with updated libs. I've seen issues similar to this using Pillow to render text for little 1-bit OLED displays.
It looks like Pillow is using fontmode='1' since you have chosen mode 'P'.
What happens if you use 'L' or 'RGB' instead of 'P'?
If you add screen_image.fontmode='L' before rendering text does the problem go away?
This may be related to #2293 (i.e. it may be a libfreetype problem).
Apologies for the delay.
I had to go live with the quirks (padded the text with spaces) and not had another machine I can test on. I've got a Pi Zero now but no screen (and no money to buy one) but will try in the next week to get it set up as per the live box (I do still have the build document :-)) and test it.
I have a hospital appointment on Monday - not sue if they will be keeping me in or not so I may not get back to you for a week or so.
Andrew
I'm not able to replicate this on my macOS machine. Are you still able to?
Yes. ADBeasley's posted code produces exactly the same image as above on my macOS Catalina machine with Pillow 7.1.2 under brew's Python 3.7.7.
I played with it a bit. If you manually set screen_image.fontmode='P' the text isn't clipped, so maybe it comes down to the old fontmode '1' problem? And it appears that #2293 is not related to an outdated FreeType library since ImageFont.core.freetype2_version gives me 2.10.1 and I am still getting problems with vertical alignment with mode '1' TTF text:
I ended up converting the fonts I wanted into .bdf format to solve this, so no big deal, but it definitely points to a bug somewhere.
I can actually reproduce #2293 with 7.1.1 from PyPI on Windows 10 using verdanab.ttf (not verdana.ttf). I will comment here, as the other issue is closed. I have a pretty good idea of what might be causing this, I will investigate more when I get more time. The issue happens with all four combinations, both layout_engine RAQM and BASIC, as well as fontmode 1 and L. It is hard to see on L due to the thicker symbols, but the digit 0 is actually clipped at the top, indicating that this is clearly a bug. Additionally, all symbols in that font have atrocious kerning (I suspect this part of the problem):
My WIP branch (#4553) doesn't have these issues, although I'm not sure it is perfect either.
Edit: The poor hinting is fixed in the new PR.
I just realized that I forgot to actually test this bug specifically; I can reproduce this issue with both 7.1.1 and my WIP branch, getting practically the same images as OP. Note that it only happens with Basic layout.
I solved this one. The problems is that getsize is called without the mode parameter, thus performing layout with FT_LOAD_TARGET_NORMAL instead of FT_LOAD_TARGET_MONO hinting. The different hinting algorithms can in some circumstances give different results causing this bug.
Raqm layout always uses FT_LOAD_TARGET_NORMAL hinting for layout, this error doesn't accumulate and in the worst case only one pixel is clipped. Raqm doesn't do hinting during layout, only rendering. Hinting can nudge glyphs at most one pixel which may be clipped.
Activity
[-]Italic true type fonts not being rendered correctly with the TEXT method[/-][+]Italic truetype fonts not being rendered correctly with the TEXT method[/+][-]Italic truetype fonts not being rendered correctly with the TEXT method[/-][+]Truetype fonts not being rendered correctly with the TEXT method[/+]ADBeasley commentedon Oct 29, 2019
Have now discovered that:
BeauSlim commentedon Nov 5, 2019
I can replicate this error on macOS with updated libs. I've seen issues similar to this using Pillow to render text for little 1-bit OLED displays.
It looks like Pillow is using fontmode='1' since you have chosen mode 'P'.
What happens if you use 'L' or 'RGB' instead of 'P'?
If you add
screen_image.fontmode='L'before rendering text does the problem go away?This may be related to #2293 (i.e. it may be a libfreetype problem).
ADBeasley commentedon Nov 9, 2019
radarhere commentedon May 21, 2020
@BeauSlim I'm not able to replicate this on my macOS machine. Are you still able to?
BeauSlim commentedon May 25, 2020
Yes. ADBeasley's posted code produces exactly the same image as above on my macOS Catalina machine with Pillow 7.1.2 under brew's Python 3.7.7.
I played with it a bit. If you manually set screen_image.fontmode='P' the text isn't clipped, so maybe it comes down to the old fontmode '1' problem? And it appears that #2293 is not related to an outdated FreeType library since ImageFont.core.freetype2_version gives me 2.10.1 and I am still getting problems with vertical alignment with mode '1' TTF text:
I ended up converting the fonts I wanted into .bdf format to solve this, so no big deal, but it definitely points to a bug somewhere.
nulano commentedon May 25, 2020
I can actually reproduce #2293 with 7.1.1 from PyPI on Windows 10 using
verdanab.ttf(notverdana.ttf). I will comment here, as the other issue is closed. I have a pretty good idea of what might be causing this, I will investigate more when I get more time. The issue happens with all four combinations, both layout_engine RAQM and BASIC, as well as fontmode 1 and L. It is hard to see on L due to the thicker symbols, but the digit 0 is actually clipped at the top, indicating that this is clearly a bug. Additionally, all symbols in that font have atrocious kerning (I suspect this part of the problem):My WIP branch (#4553) doesn't have these issues, although I'm not sure it is perfect either.
Edit: The poor hinting is fixed in the new PR.
Edit: Corrected images.
nulano commentedon May 25, 2020
I just realized that I forgot to actually test this bug specifically; I can reproduce this issue with both 7.1.1 and my WIP branch, getting practically the same images as OP. Note that it only happens with Basic layout.
nulano commentedon May 27, 2020
I solved this one. The problems is that
getsizeis called without themodeparameter, thus performing layout withFT_LOAD_TARGET_NORMALinstead ofFT_LOAD_TARGET_MONOhinting. The different hinting algorithms can in some circumstances give different results causing this bug.Raqm layout always usesRaqm doesn't do hinting during layout, only rendering. Hinting can nudge glyphs at most one pixel which may be clipped.FT_LOAD_TARGET_NORMALhinting for layout, this error doesn't accumulate and in the worst case only one pixel is clipped.