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 error when drawing text #7290

Closed
nissansz opened this issue Jul 17, 2023 · 17 comments
Closed

FreeType error when drawing text #7290

nissansz opened this issue Jul 17, 2023 · 17 comments

Comments

@nissansz
Copy link

nissansz commented Jul 17, 2023

win10, python 3.8, pillow 10, draw text sometimes has error as below. How to solve?

 draw.text((x, y), char, fill=fontcolor, font=font2, stroke_width=strokewidth, stroke_fill=strokecolor)
  File "C:\Program Files\Python38\lib\site-packages\PIL\ImageDraw.py", line 520, in text
    draw_text(ink)
  File "C:\Program Files\Python38\lib\site-packages\PIL\ImageDraw.py", line 463, in draw_text
    mask, offset = font.getmask2(
  File "C:\Program Files\Python38\lib\site-packages\PIL\ImageFont.py", line 769, in getmask2
    size, offset = self.font.getsize(
OSError: invalid reference
@radarhere radarhere changed the title win10, python 3.8, pillow 10, draw text sometimes has error as below. win10, python 3.8, pillow 10, draw text sometimes has error as below Jul 17, 2023
@radarhere
Copy link
Member

Hi. It looks like this is an error from FreeType, a dependency that Pillow uses to render text - https://freetype.org/freetype2/docs/reference/ft2-error_code_values.html

You have said that the error is intermittent, which tends to be a hard type of problem to solve. To make life easier, a self-contained example could be helpful - code that we can run to trigger the problem, and all input files that the code uses. This would let us know what font you are using, and what text you are rendering.

It would also help to know how often the error occurs, and if there is any kind of pattern to when it occurs.

@radarhere radarhere changed the title win10, python 3.8, pillow 10, draw text sometimes has error as below Intermittent "OSError: invalid reference" when drawing text Jul 17, 2023
@nissansz
Copy link
Author

I collected many fonts, but forgot to print the error font name.
Now, I am using try catch to ignore the error.
Will let you know if I can capture font name next time.

@nissansz
Copy link
Author

nissansz commented Jul 17, 2023

方正悠黑系列_502L.zip
Here is a sample font, which caused the error.

from PIL import Image, ImageFont, ImageDraw


temp_canvas = Image.new("RGBA", (1200, 300), (255, 255, 255, 255))
draw_canvas = ImageDraw.Draw(temp_canvas, "RGBA")

path1 = r'F:\fonts\en\segoeui.ttf'
font = ImageFont.truetype(path1, size=50)

path2 = r'L:\paddle\字体制作\字体1000+款\01方正字体 (508款)\方正藏文新白体.TTF'
path2 = r'L:\paddle\字体制作\字体1000+款\01方正字体 (508款)\方正悠黑系列_512B.ttf'
path2 = r'L:\paddle\字体制作\字体1000+款\01方正字体 (508款)\方正悠黑系列_502L.ttf'
backup_font = ImageFont.truetype(path2, size=50)
path3 = r'F:\fonts\tianshiyanti2.0.ttf'
backup_font2 = ImageFont.truetype(path3, size=50)
font_family = ImageFont.FreeTypeFontFamily(backup_font, backup_font2)

text_string = "hello ಠಠ world"
text_string = "大家にほn中国制造12345잔치상 차렸어? 方正中倩简体.ttf"

draw_canvas.text((100, 100), text_string, fill="#000000", font=font_family)

temp_canvas.show()

@radarhere
Copy link
Member

I see that you're using FreeTypeFontFamily. This is a feature that only exists in #6926

Does this problem still happen in normal Pillow, not one that you've built from #6926?

@nissansz
Copy link
Author

I need to use FreeTypeFontFamily. Other versions don't have this function, don't know whether there is such error.

@radarhere
Copy link
Member

I've tested with normal Pillow, and I find that the error still occurs.

from PIL import Image, ImageFont, ImageDraw

im = Image.new("RGB", (100, 100))
draw = ImageDraw.Draw(im)
font = ImageFont.truetype('方正悠黑系列_502L.ttf')
draw.text((0, 0), "test", font=font)

I can confirm that it is indeed coming from FreeType. I expect the font is broken.

If you go to https://cloudconvert.com/ttf-converter and convert 方正悠黑系列_502L.ttf to an OTF font, and use that font instead, then it should work. I expect something in that conversion process has fixed the mistake in the font.

@radarhere radarhere changed the title Intermittent "OSError: invalid reference" when drawing text "OSError: invalid reference" when drawing text Jul 17, 2023
@nissansz
Copy link
Author

thank you for the info. I will try to convert failed fonts.
Will this function be included in new pillow?

@radarhere
Copy link
Member

I can't say when/if #6926 will be included in Pillow. It is not actually complete yet - it is still marked as a draft.

@radarhere
Copy link
Member

If we have given you all of that the help that you need, you can let us know by closing this issue.

@nissansz
Copy link
Author

ok. Thank you.

@nissansz
Copy link
Author

nissansz commented Jul 17, 2023

I got another error sometimes.
For example,
font1: Tetra decorative.otf
font2: Noto

string = "大家にほn中国制造12345잔치상 차렸어? 方正中倩简体.ttf"
    draw_canvas.text((x, y), text_string , fill="#000000", font=font_family, stroke_width=2, stroke_fill=(0, 0, 0))
  File "D:\Python3.8.8\lib\site-packages\PIL\ImageDraw.py", line 514, in text
    draw_text(stroke_ink, stroke_width)
  File "D:\Python3.8.8\lib\site-packages\PIL\ImageDraw.py", line 463, in draw_text
    mask, offset = font.getmask2(
  File "D:\Python3.8.8\lib\site-packages\PIL\ImageFont.py", line
[Tetra decorative.zip](https://github.com/python-pillow/Pillow/files/12074355/Tetra.decorative.zip)
 1155, in getmask2
    self.font.render(
OSError: array allocation size too large

@radarhere
Copy link
Member

radarhere commented Jul 18, 2023

That is also an error coming from FreeType. We recently had a report of this in #7287. I asked the FreeType support mailing list, and it turned out that the font was too complex. I would guess that the same thing is happening here.

I suggest you first figure out which font is causing the problem. Try running your code with only one of the fonts at a time, and see when the error occurs.

If you think that FreeType should work with this font as it currently exists, you can e-mail [email protected] (as per https://freetype.org/contact.html) to ask the FreeType community about it. If you would like help putting together that e-mail, please post the font files so that we can replicate the problem. There are many variations of the Noto font, so I don't know which one you're referring to specifically. However, as I said, the last time we contacted FreeType about a font with this error, they simply said it was there were too many points.

Alternatively, you could contact whoever created the font, and see if they are aware of this problem and if they are willing to update their font to be simpler.

@radarhere radarhere changed the title "OSError: invalid reference" when drawing text FreeType error when drawing text Jul 18, 2023
@nissansz
Copy link
Author

Thank you.
I used NotoOldShapeNormal.TTF

For some fonts, even after converted to otf, it does not work.

And it seems some characters do not work, some may work.
Not sure how to judge by char or string.

@radarhere
Copy link
Member

I'm unable to find NotoOldShapeNormal on Google. Are able to upload the font here? And Tetra decorative as well?

@nissansz
Copy link
Author

Attached some fonts for you to try. You can use any other backup font you like, or don't use backup.
Strokewidth = 0 may solve the problem for some fonts, but not all.

otf.zip

@radarhere
Copy link
Member

From the fonts you've attached

  • 方正悠黑系列_512B.ttf gives "invalid reference". However, this can be worked around by converting the font to OTF
  • If I try and draw the "大家にほn中国制造12345잔치상 차렸어? 方正中倩简体.ttf" string with 懐訪体.ttf and 天崩地裂-懐訪体.ttf, I get "Bitmap missing for glyph". This is actually a Pillow error, not a FreeType. However, I'm guessing that Perform font fallback #6926 is intended to work around this.

The scope of this issue is increasing. It was originally about one font, now it is about at least five. It was originally about one error, now it is about at least two errors.

What I'm trying to say is that we can't help you make every font work. Some fonts are broken. You are free to contact FreeType on your own about them if you think that FreeType should support them.

Also, I don't think it's correct for us to try and solve your problems with #6926 here. That PR hasn't been merged yet. This means it is not ready, and you are using it at your own risk. If you think you have found a problem with that PR, that you think is not simply a broken font, you can comment there to offer helpful feedback as the PR progresses. But I don't think it is reasonable to expect it to always work, as it is not finished yet.

@nissansz
Copy link
Author

Totally understand.
I will exclude failed fonts for the time being and wait for final pr.

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

No branches or pull requests

2 participants