Skip to content

Conversation

@jpetrie
Copy link
Contributor

@jpetrie jpetrie commented May 30, 2015

This commit proposes a fix for issues #28 and #29, which both have to do with the handling of invalid characters in the Core Text renderer.

The method by which recurseDraw attempted to find fallback fonts for an untranslatable sequence of characters only used the first character of the sequence to find the fallback, assuming that subsequent iterations of the recursion would then render at least that one character and move on. In the case of invalid surrogate pair characters, however, the underlying Core Text APIs don't behave that way, apparently (CTFontCreateForString can return a font for a string that CTFontGetGlyphsForCharacters still won't convert).

In this change I first modified lookupFont to take a range, to try to find a font that can convert the entire given range, and verify that the returned font can actually convert the input range.

Then, when recurseDraw tries to render an untranslated glyph range, it first tries to find a font that can convert the entire range. If it fails, it tries half that range, and so on until either a renderable font/range is found or the range has been reduced to a single impossible-to-render character. After the range is drawn, the character/glyph/position pointers are adjusted so that subsequent iterations of the draw loop attempt to convert the upper half of the original untranslated range.

This should result in a slightly less-deep recursive callstack when dealing with a large range of untranslatable characters. It may have a slightly increased performance impact because the "slow" path of lookupFont may be getting hit slightly more often (my testing hasn't shown noticeable performance degradations, however). Obviously, the referenced crashes will be avoided.

The resulting invalid characters tend to display as boxes or other obvious "invalid character" glyphs. They sometimes consume multiple columns. I'm not sure I like this behavior, but it is consistent with the behavior of the regular Cocoa renderer in MacVim, so I'd consider it acceptable.

douglasdrumond added a commit that referenced this pull request Jul 6, 2015
Fix crash when rendering invalid Unicode characters.
@douglasdrumond douglasdrumond merged commit e878fe5 into macvim-dev:master Jul 6, 2015
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.

2 participants