NXP Home > News Archives > BOOM and fonts
2006-07-11 22:20:32 - BOOM and fonts
YAY my program's first crash! Should I report it to Apple? :-p
Seriously though, I figured out what was causing errors with the larger book examples I had on hand, they no longer make my newton package parser go nuts. Both of them however DO happen to make my package importer choke - because of BOLD fonts. Yes, I had completely missed the obvious - that the 4 byte ref telling what font to use should not be parsed as a number, but a set of bit fields and an embedded short.
I had noticed that the basic System font, at 12 point -was represented by the number 12288. At 10 pt, 10240 at 9 point 9216... my immediate conclusion was that the first couple of digits represented the size, and the last three were always that size multiplied by 24... 12-288, 10-240 , 9-216.
Brilliant, but what if I made it simple or fancy? the numbers went up by 1 or 2, say 12289 or 10242... so my algorithm was to divide by 1000 to get the size, then subtract from the remainder by size x 24, leaving me with the values 0, 1, or 2 - each signifying a different font.
Then came Crime And Punishment - whose 2.3 mg pkg file starts out with a 9pt BOLD line of text not 12 letters long. The numeric value of this font description as stored in the .pkg was: 1057792... F***.
attempting to use a "size" of 1057 was obviously not going to work, and the NSTextStorage into which I was placing the content of the book became horribly confused and declined to accept any new text after taking such a malformed font specification on an NSAttributedString.
As it turns out, 1057792 - 9216 (for 9 point font) = 1048576 - and subsequent tests show that whenever I bolded a font, this value was added to the font descriptor. So - its not some arcane decimal tomfoolery, its binary flags and 1048576 just happens to be 2 ^ 20 -or a single bit, at the 20th position was flipped. Italic is the 21st bit, and underlined is the 22nd. Outline is 23rd, Superscript is 27th, and Subscrip is 28th... I haven't yet seen 24t, 25th, or 26th in use.
It would be SOO much nicer if these things were written down somewhere...
Comments:
Add a comment:
[url=http://blah.com]clickhere[/url], [quote=bob]blah[/quote], [quote]anonymous blah[/quote], [img=http://blah.com/smily.gif], [b]bold[/b],and [i]italic[/i].