FROM : Ricky Sharp
DATE : Mon Jan 21 20:15:03 2008
On Jan 21, 2008, at 12:15 PM, Uli Kusterer wrote:
> does anybody know how to draw international and English text in
> Cocoa and have it correctly centered vertically? I tried
> NSLayoutManager/NSTextStorage/NSTextContainer, but the measurements
> I get from -usedRectForTextContainer: always include a highly
> inflated descent for Japanese text, which means my text is off
> towards the top as soon as it contains a Japanese word.
>
> I presume it's the descent, because when I tried to use ATSUI for
> text measuring it gave me the same odd measurements. The descent is
> almost as high as the text itself, while for Latin-only text it's
> about a quarter. Which is funny, considering Japanese doesn't have
> any descenders as far as I know.
>
> It seems that NSButtonCell correctly does its measuring, because
> there Japanese text is properly adjusted. Does anyone know what
> clever thing NSButtonCell might be doing so it can properly center
> text vertically?
For me, it was as simple as using NSAttributedString instances,
sending a "size" message to get the NSSize and then exactly centering
a box of that width/height within my cell's bounds. Then, I just use
drawInRect: to do the rendering.
Using the baseline approach (computing, e.g. the "x" height, or
working with ascenders/descenders) always yielding incorrect results
(at least in my eyes). Probably similar "wrong" results as you're
seeing.
I've been using this approach extensively with Unicode text (past 3
years). I always use systemFontOfSize: which ultimately renders in
the appropriate font for a particular language. I've used many
different sizes ranging from 18 pt to 48 pt. Since my baseline is
Tiger, I've only though looked at this on 10.4 and later. I would
hope 10.3 and even earlier would produce similar, if not identical
output.
Output also looked good no matter what the res-ind scaling factor (0.5
through 3.0 to include non-integral ones like 1.33333).
I've also specifically looked at Japanese (Kanji, Hiragana and
Katakana characters) and haven't noticed anything strange.
I do also modify my centering algorithm if the text includes a
shadow. Because of what I'll call existing bugs, the size of an
attributed string doesn't always take into account it's shadow
metrics. I thus modify the height and width of the bounds returned by
size by a factor of the sum of shadow blur and shadow offset in a
particular direction.
BTW, When I did this work (end of 2004), I did quite a bit of
Photoshop comparisons of Aqua output and my own. The approach I'm
using seems to be right on.
Finally, here are some screen shots from my app you can use to
determine if my centering logic would appeal to you:
<http://www.instantinteractive.com/drills_slides.html>
___________________________________________________________
Ricky A. Sharp mailto:<email_removed>
Instant Interactive(tm) http://www.instantinteractive.com
DATE : Mon Jan 21 20:15:03 2008
On Jan 21, 2008, at 12:15 PM, Uli Kusterer wrote:
> does anybody know how to draw international and English text in
> Cocoa and have it correctly centered vertically? I tried
> NSLayoutManager/NSTextStorage/NSTextContainer, but the measurements
> I get from -usedRectForTextContainer: always include a highly
> inflated descent for Japanese text, which means my text is off
> towards the top as soon as it contains a Japanese word.
>
> I presume it's the descent, because when I tried to use ATSUI for
> text measuring it gave me the same odd measurements. The descent is
> almost as high as the text itself, while for Latin-only text it's
> about a quarter. Which is funny, considering Japanese doesn't have
> any descenders as far as I know.
>
> It seems that NSButtonCell correctly does its measuring, because
> there Japanese text is properly adjusted. Does anyone know what
> clever thing NSButtonCell might be doing so it can properly center
> text vertically?
For me, it was as simple as using NSAttributedString instances,
sending a "size" message to get the NSSize and then exactly centering
a box of that width/height within my cell's bounds. Then, I just use
drawInRect: to do the rendering.
Using the baseline approach (computing, e.g. the "x" height, or
working with ascenders/descenders) always yielding incorrect results
(at least in my eyes). Probably similar "wrong" results as you're
seeing.
I've been using this approach extensively with Unicode text (past 3
years). I always use systemFontOfSize: which ultimately renders in
the appropriate font for a particular language. I've used many
different sizes ranging from 18 pt to 48 pt. Since my baseline is
Tiger, I've only though looked at this on 10.4 and later. I would
hope 10.3 and even earlier would produce similar, if not identical
output.
Output also looked good no matter what the res-ind scaling factor (0.5
through 3.0 to include non-integral ones like 1.33333).
I've also specifically looked at Japanese (Kanji, Hiragana and
Katakana characters) and haven't noticed anything strange.
I do also modify my centering algorithm if the text includes a
shadow. Because of what I'll call existing bugs, the size of an
attributed string doesn't always take into account it's shadow
metrics. I thus modify the height and width of the bounds returned by
size by a factor of the sum of shadow blur and shadow offset in a
particular direction.
BTW, When I did this work (end of 2004), I did quite a bit of
Photoshop comparisons of Aqua output and my own. The approach I'm
using seems to be right on.
Finally, here are some screen shots from my app you can use to
determine if my centering logic would appeal to you:
<http://www.instantinteractive.com/drills_slides.html>
___________________________________________________________
Ricky A. Sharp mailto:<email_removed>
Instant Interactive(tm) http://www.instantinteractive.com
| Related mails | Author | Date |
|---|---|---|
| Uli Kusterer | Jan 21, 19:15 | |
| Ricky Sharp | Jan 21, 20:15 | |
| Uli Kusterer | Feb 1, 12:00 | |
| RICKY SHARP | Feb 1, 13:33 | |
| Ricky Sharp | Feb 1, 18:46 |






Cocoa mail archive

