Thursday, October 23, 2008

Text Effects - Debugging bounds problems

I have been looking at a strange problem where glyphs, when revealed by an animation, fail to redraw properly. What happens is when a part of an animation moves over a character shape the shape that is revealed sometimes fails to redraw. You can see this in the snapshot below - most obviously there is a bit missing from the A.


When I first saw the problem I presumed that it was down to a bug in the player and dismissed it as "just something that happened" however as time has gone on I have noticed other situations where the same problem. Going back to the PC version of Cello I can see that the problem does not exist - so obviously it is a problem that I have introduced.

I must admit to being daunted when I first looked at the problem - I had little idea why the problem would happen, but like most bugs in the end it was not as bad as I thought. The key things are to take the time, think it through, and get a plan that will at the very least narrow the problem down.

The example animations I was working with were, in terms of the output, quite complex. The examples had a fair handful of glyph shapes. The first thing I did was to create a sample that was as simple as possible. I found I could get the problem to occur with just two characters that were on two different lines. I could also further simplify the example by using an effect on simple shape that was on top of the characters. If I made the characters the same this was a further simplification. These simplifications were important - the type of the shape moving seemed to me unimportant - the problem seemed to be in the glyphs that were being revealed.

The next part of the debugging was to compare the output against that of the PC version. The Engineer who originally wrote Cello did a really good job. Amongst the things he did was to add in debugging code that would parse the resulting file and output it as text. What this means is that I can run the PC version of Cello, then the OSX version and diff the two files. I have done this in the past when working on glyphs and text and it is invaluable.

Looking at the diff of the file I found a few things. The PC version of Cello had an extra redundant curve segment - effectively a moveto(0,0). I changed the code so it was added. The OSX version output an empty glyph (for the so I changed the code to stop this from happening). Working through the diff the next thing was the defineTextTag - the bounds were different - and looked wrong. And this turned out to be wrong. My basic error was in constructing a CGRect I hat the origin of the Rect at the top rather than the bottom. The player will happily draw outside the bounds - but it respects the bounds when calculating a redraw area. Seems reasonable enough to me.

No comments: