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

one errors on windows for font #411

Closed
vastyao opened this issue May 9, 2019 · 5 comments
Closed

one errors on windows for font #411

vastyao opened this issue May 9, 2019 · 5 comments

Comments

@vastyao
Copy link

vastyao commented May 9, 2019

I set a font have chinese with draw.font, then when i use draw.get_font_metrics(), it would be errors with valueError? what can I do for this?

@emcconville
Copy link
Owner

Can you share an example of the code that produced the ValueError? OR perhaps what the ValueError message was?

@vastyao
Copy link
Author

vastyao commented May 9, 2019

ValueError: NULL pointer access. the errors is location in wand/drawing.py.
the code is
args = [result[i] for i in xrange(13)]
If transform chinese in the name of fontchinese to english , it can work well.

@emcconville
Copy link
Owner

I'm not able to reproduce (yet), but I would suspect that font library can not load the typeface. Usually the font-resolver will fallback to system fault if there's an issue, so we need to know what the actual error is. The following patch should at least emit ImageMagick's internal exception.

index 43f35eb..7f39dd3 100644
--- a/wand/drawing.py
+++ b/wand/drawing.py
@@ -1151,6 +1151,13 @@ class Drawing(Resource):
             else:
                 text = binary(text)
         result = font_metrics_f(image.wand, self.resource, text)
+        if not result:
+            # Possible error on drawing context
+            self.raise_exception()
+            # Or error on image canvas
+            image.raise_exception()
+            # Generate a generic error if ImageMagick couldn't emit one.
+            raise ValueError('Unable to render text with current font.')
         args = [result[i] for i in xrange(13)]
         library.MagickRelinquishMemory(result)
         return FontMetrics(*args)

Don't know if you're able to patch & test, but I would like a minimum test case. Both Simplified & Traditional Chinese fonts work for me.

with Image(filename='wizard:') as img:
    with Drawing() as traditional_chinese:
        traditional_chinese.font = '/path/to/Apple LiSung Light.ttf'
        traditional_chinese.font_size = 64
        metrics = traditional_chinese.get_font_metrics(img, u'你好,世界')
        print('Traditional Chinese', metrics)
    with Drawing() as simplified_chinese:
        simplified_chinese.font = '/path/to/FandolHei-Bold.otf'
        simplified_chinese.font_size = 64
        metrics = simplified_chinese.get_font_metrics(img, u'你好,世界')
        print('Simplified Chinese', metrics)

@emcconville
Copy link
Owner

Okay, this method will now raise the correct error. Should be a TypeError along the lines as...

wand.exceptions.TypeError: unable to read font `[INSERT BAD FONT VALUE HERE]' @ error/annotate.c/RenderFreetype/1362

As far as why it would fail, that's usually just a typo. Also ensure that freetype can read the font file.

@vastyao
Copy link
Author

vastyao commented May 10, 2019

such as the font's name is '方正兰亭黑.TTF', it would be error.when I change it to 'a.TTF',it will work well.

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

2 participants