Fixing a crash on mutli-byte Unicode characters in MOAIFreetypeFont:dimensionsOfLine
#132
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
While working on a way to let
SizeConstrainedTextArea
find the optimal font size without actually rendering any textures, I ran into an issue withMOAIFreeTypeFont
and decided to address that first instead. 🔠🔧Basically, the method
_dimensionsOfLine
crashes (intermittently) when called on strings that contain multi-byte Unicode characters. 💣💥 For example, callingmoaiFont:dimensionsOfLine("åpple", 10.0)
will (sometimes) cause a crash. 🙈 That's probably why we've avoided usingdimensionsOfLine
in our entire codebase. 💁♂️It turns out the crash was happening because there were glyphs that were getting freed by
FT_Done_Glyph
that weren't allocated to begin with. 🙅 I fixed it by changing afor
loop so that it uses the correct index variable in its condition statement. Previously, thefor
loop was comparing to a different index variable... which got incremented by more than1
whenu8_nextchar
encountered a multi-byte Unicode character. ❌ That threw off the loop that initialized the glyphs... which is why some of them weren't initialized when we calledFT_Done_Glyph
on them. 💣After that fix, I also had to switch the calculation for
maxGlyphs
fromstrlen
(which doesn't handle multi-byte Unicode characters properly) to a different function defined inMOAIFreeTypeFont
(glyphsInText
). Before I changed that, labels that included multi-byte Unicode characters would sometimes render with garbage characters at the end. 🗑️The code in this file is pretty sloppy (to say the least 😬), so after I tested out the fixes, I started to try renaming variables and doing some light refactoring and tidying up. 🕳️🐇 I got through the two methods I modified, but I ended up changing practically every line of code in them. 🧹🧹🧹 I still have that cleanup on a separate branch, but I figured we should start with just these three lines for now to keep the important change isolated and minimize risk. 🎲 If we do decide to go ahead with code cleanup in
MOAIFreeTypeFont
, I would advocate for rolling it out over the course of several sprints so we can have more confidence we're not introducing bugs to such a sensitive part of the games stack. 🗿🚫🐛For what it's worth, I made good progress on the no-render-
SizeConstrainedTextArea
project, but since it depends on_dimensionsOfLine
, it'll have to wait for a future 5% Day. 😁🔜📆