• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: problems in measuring text rendering size
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: problems in measuring text rendering size


  • Subject: Re: problems in measuring text rendering size
  • From: Allan Odgaard <email@hidden>
  • Date: Tue, 24 Feb 2004 12:46:47 +0100

On 24. Feb 2004, at 3:59, Brian Burton wrote:

In order to set the size of the view and the window surrounding it, I
was measuring the text with the following bit of code

NSFont* the24Font = [NSFont fontWithName:"Courier" size:11];
float width = [the24Font advancementForGlyph:(NSGlyph)' '].width * 80;

You cannot convert a character to a glyph like that. But you can query the font for the maximum advancement of any glyph:

float GetMaxAdvancement (NSFont* aFont)
{
CFStringRef fontName = (CFStringRef)[aFont fontName];
ATSFontRef font = ATSFontFindFromPostScriptName(fontName, kATSOptionFlagsDefault);
if(!font)
throw font_unavailable();

ATSFontMetrics metrics;
OSStatus status = ATSFontGetHorizontalMetrics(font, 0, &metrics);
if(status != noErr)
throw std::bad_exception();

return metrics.maxAdvanceWidth * [aFont pointSize];
}

Why would I get one size for spaces and one size for '1' for a
monospace font like Courier? I would expect a width of 528.085938
and a height of 312 [...] but I end up with a width of 525 and height
of 624 if I use the '1' character. If I use the space character,
I end up with the size that I expect.

You are dealing with floats, so you cannot expect exact results. It seems that the last character of each line is wrapped, that explains why it is a bit smaller in width but exactly double the height when you use "1" in your strings.

Try to give a width of 540 or so to your text container and see if not your height is correct. And also how much the width then differ -- despite the precision of floats it should not be much, but even in monospaced fonts, the width of glyphs are not always exactly the same (at least I have had such problems in the past).
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: 
 >problems in measuring text rendering size (From: Brian Burton <email@hidden>)

  • Prev by Date: Re: Zlib compression
  • Next by Date: Re: NSSearchField search template through IB
  • Previous by thread: problems in measuring text rendering size
  • Next by thread: key value observing crash on removal
  • Index(es):
    • Date
    • Thread