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

Render text using kerning pairs information #6175

Closed
chrisnoessel opened this issue Apr 2, 2022 · 13 comments
Closed

Render text using kerning pairs information #6175

chrisnoessel opened this issue Apr 2, 2022 · 13 comments

Comments

@chrisnoessel
Copy link

chrisnoessel commented Apr 2, 2022

Font files contain information about how to ideally separate pairs of letters called kerning tables. (This is different than letterspacing, which is the distance each letter has between any other letter.) Kerning tables are what make VA look correct rather than V A. Whenever I set text using Pillow, it disregards the kerning tables and creates very awkward letterspacing of some letter pairs that I can’t fix without opening the font, exporting the existing tables, and manually recreating the draw feature; which it not really a scalable option. Can the Image.text function be updated to register kerning tables?

(This is my first request, please forgive if I’m doing it wrong.)

@nulano
Copy link
Contributor

nulano commented Apr 2, 2022

Do you use basic or Raqm layout? The default is Raqm, but it might not be available for you, you can check by running python -m PIL.

Edit: Kerning should work correctly in Raqm layout, but has been broken in basic layout since #2576 (about 6 years) because the kerning offset is scaled twice.

@chrisnoessel
Copy link
Author

I am checking now.

@chrisnoessel
Copy link
Author

*** RAQM (Bidirectional Text) support not installed

Per instructions at https://github.com/HOST-Oman/libraqm
I installed brew install freetype harfbuzz fribidi meson gtk-doc
but still that result. :(

@nulano
Copy link
Contributor

nulano commented Apr 2, 2022

How did you install Pillow? Depending on the answer, there are different ways to enable Raqm support.

@chrisnoessel
Copy link
Author

It was a while back but I think via pip3 in MacOS terminal.app.

@nulano
Copy link
Contributor

nulano commented Apr 2, 2022

It was a while back

I don't have macOS, but I fixed a bug two days ago for Alpine that made it into yesterday's release: #6165
Could you check whether upgrading to Pillow 9.1.0 (by running python3 -m pip install -U Pillow) also fixes Raqm support on macOS?

Edit:
The above command should install the latest wheel which should automatically detect and load libfribidi.dylib.
If it doesn't detect it, you can try a source build. The full instructions are here: https://pillow.readthedocs.io/en/stable/installation.html#building-on-macos
Assuming you have the dependencies installed, you just need to run:

python3 -m pip uninstall Pillow
python3 -m pip install --no-binary :all: Pillow

@khaledhosny
Copy link

Raqm is available on Homebrew, so it can be installed by brew install libraqm.

@radarhere
Copy link
Member

@chrisnoessel did you have any luck?

If none of the previous suggestions helped, I would suggest installing from Pillow main, to see if #6182 fixes the problem. If you're having trouble installing from source, let us know whether you are M1 or Intel, and your Python version, and we can put together a wheel for you.

@nulano
Copy link
Contributor

nulano commented Apr 8, 2022

#6182 will not affect source builds, the fribidi shim is not used in that case, so there is no need to install from main.

I think the wheels from https://github.com/radarhere/pillow-wheels/actions/runs/2093139164 (linked from #6182) should show whether #6182 fixes the problem. (perhaps it might be worth reverting the symlink part of python-pillow/pillow-wheels#281 to make sure the fix is tested on CI)

@radarhere
Copy link
Member

Removing the symlink part of python-pillow/pillow-wheels#281 leads to https://github.com/radarhere/pillow-wheels/actions/runs/2117770741, where latest Pillow passes with #6182 and Pillow 9.1.0 fails without, as expected. I haven't created that PR yet just to avoid marking pillow-wheels as failing in GitHub Actions from now until the next release. If you think it should go in now though, happy for that.

@nulano
Copy link
Contributor

nulano commented Apr 9, 2022

I would probably open the PR and make it a draft until the next release (to make sure it isn't forgotten about), but I don't have a strong preference either way.

@radarhere
Copy link
Member

I've now opened it. python-pillow/pillow-wheels#285

@radarhere
Copy link
Member

Running the following code on my machine,

from PIL import Image, ImageDraw, ImageFont
im = Image.new("RGB", (100, 100))
d = ImageDraw.Draw(im)
font = ImageFont.truetype("/System/Library/Fonts/Supplemental/Times New Roman.ttf", size=48, layout_engine=ImageFont.Layout.BASIC)
d.text((10, 0), "VA", font=font, fill="#f00")
font = ImageFont.truetype("/System/Library/Fonts/Supplemental/Times New Roman.ttf", size=48, layout_engine=ImageFont.Layout.RAQM)
d.text((10, 45), "VA", font=font, fill="#f00")
im.save("out.png")

gives me
out

So with RAQM, VA is not just V and then A.

Regarding the problem you're having detecting RAQM in your Pillow installation, #6182 should improve this situation when installing from wheels in the next Pillow release.

Unless you would like further help, there doesn't seem to be anything more to be done here.

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

No branches or pull requests

4 participants