Re: Stupid Newbie Font/Glyph Questions
Re: Stupid Newbie Font/Glyph Questions
- Subject: Re: Stupid Newbie Font/Glyph Questions
- From: Brock Brandenberg <email@hidden>
- Date: Sat, 02 Nov 2002 12:10:28 -0600
Hi Tom.
The fact that your text wraps to the next line on some machines could be
attributable to a change in typesetting behavior from 10.0.x to 10.2. Jag
has more sophisticated typesetting behavior and will lay out text in a text
view differently than 10.0.x does. Look at [NSLayoutManager
setTypesetterBehavior:] for more info.
>
Dumb, stupid question number 1.
>
>
How do I get the glyph of a particular character for this attributed string? I
>
want to look at the bounding rectangles for a "space" character. how do i get
>
the glyph?
You should probably use an NSLayoutManager to "typeset" the attributed
string with the proper glyphs for the attributes that you have specified,
then interrogate the NSLayoutManager for the dimensions that you want.
When using an NSTextView, the text hierarchy of NSLayoutManager,
NSTextContainer, NSTextStorage and NSTextView are automatically constructed
for you behind the scenes. But in your case, you seem to want to get some
metrics, then alter either your attributes or the text view based on those
metrics.
So, you may want to first construct an NSLayoutManager, NSTextContainer and
NSTextStorage manually with a long, single line text container, pass it the
string you want to get the dimensions of, have it lay out the string, then
ask the NSLayoutManager for the resulting metrics and alter your NSTextView
or attributes based on the metrics. In a nutshell:
NSTextStorage *textStorage = [[NSTextStorage alloc]
initWithAttributedString: blankLine];
[blankLine release]; // retained by textStorage
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
[layoutManager release]; // retained by textStorage
// Set options for layoutManager as needed. For example:
// [layoutManager setUsesScreenFonts:NO];
// [layoutManager setShowsControlCharacters:YES];
// [layoutManager setShowsInvisibleCharacters:YES];
// [layoutManager setTypesetterBehavior:NSTypesetterOriginalBehavior];
// This makes a very wide container by default (I believe width = 1,000,000)
NSTextContainer *textContainer = [[NSTextContainer alloc] init];
[layoutManager addTextContainer:textContainer];
[textContainer release]; // retained by layoutManager
Now you can interrogate the NSLayoutManager for whatever metrics you want.
You can walk the glyphs to get individual metrics (make sure you test for
valid glyph, NSNullGlyph, NSControlGlyph and
notShownAttributeForGlyphAtIndex: or you'll get things you don't want), or
get line fragment rects for entire lines. Realize that characters and glyphs
are not always the same. Depending on the attributes you specify, the
NSLayoutManager may have one glyph for two characters (ligatures) or more
than one glyph for a single character (glyphs other than a-z like an
accented "e").
NSRange glyphRange = [layoutManager
glyphRangeForTextContainer:textContainer];
for( i = glyphRange.location; i < NSMaxRange(glyphRange); i++ ) // for each
glyph
{
glyph = [layoutManager glyphAtIndex:i isValidIndex:&isValid];
// We only use its geometry if it's a useful, visible glyph. Although
you can't see them, spaces have a visble effect and are considered visible
glyphs.
if( isValid &&
![layoutManager notShownAttributeForGlyphAtIndex:i] &&
( NSNullGlyph != glyph ) &&
( NSControlGlyph != glyph ) )
{
// do whatever
// ask font for bounding rect
// ask layout manager for glyph location
// etc.
}
}
>
Dumb, stupid question number 2.
>
>
How can my NSTextView with a bounds width of 352 show my my attributed string
>
which has a width of 559?
>
>
testRect = [terminalView bounds]; NSLog(@"- bounds width: %.0f",
>
testRect.size.width);
>
>
Terminal Values: - bounds width: 579
>
>
NSLog(@"Attributed String Values: (expected values)"); testSize = [blankLine
>
size]; NSLog(@"- size: w:%0.f h:%0.f (29)", testSize.width, testSize.height);
>
>
Attributed String Values: - size: w:559
I haven't used this attributed string addition, but the docs don't specify
the units as pixels. Since the class description says "Methods for
calculating significant linguistic units," I wouldn't immediately assume
that the units are in pixels, but I can't suggest what else it could be.
Brock Brandenberg
----- industrial design @ www.bergdesign.com ------
_______________________________________________
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.