Skip to content

FIX: KeyError while parsing ttfFontProperty#365

Merged
JCorson merged 19 commits into
masterfrom
fix/363-font-keyerror
Sep 25, 2019
Merged

FIX: KeyError while parsing ttfFontProperty#365
JCorson merged 19 commits into
masterfrom
fix/363-font-keyerror

Conversation

@JCorson
Copy link
Copy Markdown
Contributor

@JCorson JCorson commented Sep 22, 2019

I think this might help the KeyErrors that were exposed when logging was introduced in ttfFontProperty. It continues searching through the names until one can be decoded with either utf-8 or utf-16-be. Rather than using the platformID, platEncID, langID, and nameID, this only uses the nameID. It also removes the sfnt2 prop. AFAICT, it wasn't actually needed. style = 'normal' would be set regardless.

I would prefer to get a font in the repo to test on that was producing the KeyError, but testing locally with createFontList(findSystemFonts()) on OSX, Windows, and Ubuntu ends up with all fonts parsed without raising an exception.

Let me know what you think.

Closes #363

@JCorson JCorson requested a review from mdickinson September 22, 2019 02:30
@rahulporuri
Copy link
Copy Markdown
Contributor

the travis job for python 2.7 and null toolkit failed with the following traceback -

======================================================================

ERROR: test_text (kiva.tests.test_gl_drawing.TestGLDrawing)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "/home/travis/.edm/envs/enable-test-2.7-null-pillow/lib/python2.7/site-packages/enable-4.8.1.dev19-py2.7-linux-x86_64.egg/kiva/tests/drawing_tester.py", line 72, in test_text

    self.gc.show_text("hello kiva")

  File "/home/travis/.edm/envs/enable-test-2.7-null-pillow/lib/python2.7/site-packages/enable-4.8.1.dev19-py2.7-linux-x86_64.egg/kiva/gl.py", line 343, in show_text

    return self.show_text_at_point(text, *point)

  File "/home/travis/.edm/envs/enable-test-2.7-null-pillow/lib/python2.7/site-packages/enable-4.8.1.dev19-py2.7-linux-x86_64.egg/kiva/gl.py", line 349, in show_text_at_point

    pyglet_font = GetFont(self._current_font)

  File "/home/travis/.edm/envs/enable-test-2.7-null-pillow/lib/python2.7/site-packages/enable-4.8.1.dev19-py2.7-linux-x86_64.egg/kiva/gl.py", line 277, in GetFont

    pyglet_font = load_font(font.findfontname(), font.size, bold,

  File "/home/travis/.edm/envs/enable-test-2.7-null-pillow/lib/python2.7/site-packages/enable-4.8.1.dev19-py2.7-linux-x86_64.egg/kiva/fonttools/font.py", line 115, in findfontname

    return fp.get_name()

  File "/home/travis/.edm/envs/enable-test-2.7-null-pillow/lib/python2.7/site-packages/enable-4.8.1.dev19-py2.7-linux-x86_64.egg/kiva/fonttools/font_manager.py", line 878, in get_name

    return getPropDict(TTFont(str(font))['name'])

  File "/home/travis/.edm/envs/enable-test-2.7-null-pillow/lib/python2.7/site-packages/enable-4.8.1.dev19-py2.7-linux-x86_64.egg/kiva/fonttools/font_manager.py", line 246, in getPropDict

    n = font['name']

TypeError: 'table__n_a_m_e' object has no attribute '__getitem__'

-------------------- >> begin captured logging << --------------------

fontTools.ttLib.ttFont: DEBUG: Reading 'name' table from disk

fontTools.ttLib.ttFont: DEBUG: Decompiling 'name' table

--------------------- >> end captured logging << ---------------------

----------------------------------------------------------------------

@JCorson
Copy link
Copy Markdown
Contributor Author

JCorson commented Sep 24, 2019

Interesting...

the travis job for python 2.7 and null toolkit failed with the following traceback -

======================================================================

ERROR: test_text (kiva.tests.test_gl_drawing.TestGLDrawing)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "/home/travis/.edm/envs/enable-test-2.7-null-pillow/lib/python2.7/site-packages/enable-4.8.1.dev19-py2.7-linux-x86_64.egg/kiva/tests/drawing_tester.py", line 72, in test_text

    self.gc.show_text("hello kiva")

  File "/home/travis/.edm/envs/enable-test-2.7-null-pillow/lib/python2.7/site-packages/enable-4.8.1.dev19-py2.7-linux-x86_64.egg/kiva/gl.py", line 343, in show_text

    return self.show_text_at_point(text, *point)

  File "/home/travis/.edm/envs/enable-test-2.7-null-pillow/lib/python2.7/site-packages/enable-4.8.1.dev19-py2.7-linux-x86_64.egg/kiva/gl.py", line 349, in show_text_at_point

    pyglet_font = GetFont(self._current_font)

  File "/home/travis/.edm/envs/enable-test-2.7-null-pillow/lib/python2.7/site-packages/enable-4.8.1.dev19-py2.7-linux-x86_64.egg/kiva/gl.py", line 277, in GetFont

    pyglet_font = load_font(font.findfontname(), font.size, bold,

  File "/home/travis/.edm/envs/enable-test-2.7-null-pillow/lib/python2.7/site-packages/enable-4.8.1.dev19-py2.7-linux-x86_64.egg/kiva/fonttools/font.py", line 115, in findfontname

    return fp.get_name()

  File "/home/travis/.edm/envs/enable-test-2.7-null-pillow/lib/python2.7/site-packages/enable-4.8.1.dev19-py2.7-linux-x86_64.egg/kiva/fonttools/font_manager.py", line 878, in get_name

    return getPropDict(TTFont(str(font))['name'])

  File "/home/travis/.edm/envs/enable-test-2.7-null-pillow/lib/python2.7/site-packages/enable-4.8.1.dev19-py2.7-linux-x86_64.egg/kiva/fonttools/font_manager.py", line 246, in getPropDict

    n = font['name']

TypeError: 'table__n_a_m_e' object has no attribute '__getitem__'

-------------------- >> begin captured logging << --------------------

fontTools.ttLib.ttFont: DEBUG: Reading 'name' table from disk

fontTools.ttLib.ttFont: DEBUG: Decompiling 'name' table

--------------------- >> end captured logging << ---------------------

----------------------------------------------------------------------

Interesting... That is failing on one of the few lines in getPropDict that wasn't changed... Let me dig in some to see what I can find.

@rahulporuri
Copy link
Copy Markdown
Contributor

rahulporuri commented Sep 24, 2019

Note that apart from the KeyError, I'm also seeing a UnicodeDecodeError like the following but this is probably a separate issue altogether.

2019-09-21 15:16:19,324 ERROR    MainThread [kiva.fonttools.font_manager:734] Cannot handle unicode file: /System/Library/Fonts/ヒラギノ角ゴシック W5.ttc

Traceback (most recent call last):

  File "/Users/travis/.edm/envs/XXX/lib/python2.7/site-packages/kiva/fonttools/font_manager.py", line 713, in createFontList

    collection = TTCollection(str(fpath))

UnicodeEncodeError: 'ascii' codec can't encode characters in position 22-32: ordinal not in range(128)

@codecov-io
Copy link
Copy Markdown

codecov-io commented Sep 24, 2019

Codecov Report

Merging #365 into master will increase coverage by 0.46%.
The diff coverage is 77.77%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #365      +/-   ##
==========================================
+ Coverage   32.91%   33.37%   +0.46%     
==========================================
  Files         206      206              
  Lines       18303    18302       -1     
  Branches     2415     2416       +1     
==========================================
+ Hits         6024     6108      +84     
+ Misses      11893    11807      -86     
- Partials      386      387       +1
Impacted Files Coverage Δ
kiva/fonttools/font_manager.py 51.19% <77.77%> (+10.27%) ⬆️
kiva/fonttools/font.py 56.79% <0%> (+0.54%) ⬆️
enable/tools/hover_tool.py 37.31% <0%> (+2.98%) ⬆️
kiva/trait_defs/kiva_font_trait.py 76.56% <0%> (+3.12%) ⬆️
enable/colors.py 25% <0%> (+3.26%) ⬆️
enable/component_editor.py 79.16% <0%> (+4.16%) ⬆️
enable/null/image.py 85.71% <0%> (+85.71%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a843b37...893273a. Read the comment docs.

@JCorson
Copy link
Copy Markdown
Contributor Author

JCorson commented Sep 24, 2019

I think I found the bug. I had the ['name'] on the TTFont rather than the return of getPropDict. Fixed in 841fbcb

@JCorson
Copy link
Copy Markdown
Contributor Author

JCorson commented Sep 24, 2019

I also changed some str calls to six.text_type to ensure that filenames with unicode characters are handled on Python 2.7 (f73dfba)

Comment thread kiva/fonttools/font_manager.py Outdated

font = fontManager.findfont(self)
return getPropDict(TTFont(six.text_type(font))['name'])
return getPropDict(TTFont(six.text_type(font)))['name']
Copy link
Copy Markdown
Contributor

@rahulporuri rahulporuri Sep 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think it will help readability if you change the formatting here to

return getPropDict(
    TTFont(six.text_type(font))
)['name']

or something of that sort?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe even breaking that out of the return altogether to make it more readable. Something like:

prop_dict = getPropDict(TTFont(six.text_type(font))
return prop_dict['name']

What do you think @rahulporuri ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@JCorson JCorson requested a review from jwiggins September 24, 2019 17:55
@mdickinson
Copy link
Copy Markdown
Member

Code changes LGTM; I'm running into bandwidth issues with local testing, though.

@mdickinson
Copy link
Copy Markdown
Member

Local test results LGTM, too.

@JCorson
Copy link
Copy Markdown
Contributor Author

JCorson commented Sep 25, 2019

Thanks all for the reviews!

@JCorson JCorson merged commit 5a9b2d1 into master Sep 25, 2019
@JCorson JCorson deleted the fix/363-font-keyerror branch September 25, 2019 13:18
jwiggins pushed a commit that referenced this pull request Sep 30, 2019
FIX: KeyError while parsing ttfFontProperty
@jwiggins jwiggins mentioned this pull request Sep 30, 2019
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

Successfully merging this pull request may close these issues.

KeyError from ttfFontProperty while parsing

4 participants