Re: Fastest way to predict bounding box for a string
Re: Fastest way to predict bounding box for a string
- Subject: Re: Fastest way to predict bounding box for a string
- From: Keith Wilson <email@hidden>
- Date: Tue, 7 Feb 2006 12:34:23 +1100
Hi Ivan,
I use the following (verbose) method for small boxes of text (200 x 80)
Note the use of sqrt in the first pass to quickly home in on the
fontSize.
If anyone has a more elegant method then I'll use it.
Keith
//
************************************************************************
*****
-(void)calculateFontSize :(NSString*)fontName
{
float boxHeight = 0;
long count = 0;
// rclBox, textView, fontSize and textHeight are owned by the object
I'm drawing (self)
fontSize = [[pcd cdFont] pointSize]; //the diagram has a default
font called cdFont
NSLayoutManager *layoutManager = [textView layoutManager];
NSTextContainer *textContainer = [textView textContainer];
[textView setFont:[NSFont fontWithName:fontName size:fontSize]];
boxHeight = textHeight = rclBox.size.height;
if(rclBox.size.width <= 2)
{ [textView setFont:[NSFont fontWithName:fontName size:1]];
textHeight = rclBox.size.height;
}
else while(textHeight >= rclBox.size.height)
{ [layoutManager glyphRangeForTextContainer:textContainer];
textHeight = [layoutManager
usedRectForTextContainer:textContainer].size.height;
if(textHeight <= rclBox.size.height)
break;
if(textHeight <= 2)
break;
if(fontSize <= 1)
break;
if(count == 0)
fontSize = fontSize / sqrt(textHeight/boxHeight);//note square
root because font reduction affects both its height and width
else
fontSize -= 1;
count++;
[textView setFont:[NSFont fontWithName:fontName size:fontSize]];
}
}
//
************************************************************************
*****
On 07/02/2006, at 11:48 AM, Ivan Kourtev wrote:
Does your suggestion mean that if I take the bounding boxes of a
certain string in sizes 5, 10, and 20, of the same font, then the
widths and heights of these bounding boxes will be related to each
other roughly as 5:10:20 with any small inconsistencies arising
from the changing thickness of the drawing pen?
I thought of this approach briefly but dismissed it because I think
that's not how it works in the real typesetting world (my knowledge
of which is admittedly also limited).
--
ivan
On Feb 6, 2006, at 7:01 PM, Douglas Davidson wrote:
On Feb 6, 2006, at 3:56 PM, Ivan Kourtev wrote:
Any ideas on how to get this size quicker? Is there any way that
has escaped my attention that can return the biggest font size
such that the particular string does not exceed a given bounding
rectangle? Or should I just fall back to binary search of a
certain interval of font sizes? Maybe caching the the size of a
representative character in every size and getting a very good
first guess for the required font size (particularly if the font
is fixed width)? Any ideas or suggestions?
You should be able to do better than binary search with linear
extrapolation.
Douglas Davidson
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40bigpond.net.au
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden