Modifying glyph storage in NSLayoutManager
Modifying glyph storage in NSLayoutManager
- Subject: Modifying glyph storage in NSLayoutManager
- From: Ross Carter <email@hidden>
- Date: Mon, 17 Mar 2008 12:31:49 -0400
I have some text items whose glyphs cannot be determined until layout.
The text string might contain a marker to draw the current page
number, or to sequentially number paragraphs, etc. The glyphs can be
determined only by the layout manager; different layout managers for
the same text storage might display different page numbers, for example.
Because the text storage cannot know the values to display, there is
not necessarily a one-to-one mapping between the marker character and
the glyphs; e.g. a page number marker might be one character and the
page number might be “10.”
I would like to educate NSLayoutManager to assign the correct set of
glyphs to the marker character. I expect that the best way to do this
is by subclassing NSGlyphGenerator. Here’s one of several things I’ve
tried:
- (void)generateGlyphsForGlyphStorage:(id < NSGlyphStorage
>)glyphStorage
desiredNumberOfCharacters:(NSUInteger)nChars
glyphIndex:(NSUInteger *)glyphIndex
characterIndex:(NSUInteger *)charIndex {
NSAttributedString *attrStr = [glyphStorage attributedString];
// Loop through attrStr looking for character with dynamicText
attribute:
NSRange charRange = NSMakeRange(*charIndex, nChars);
NSRange effRange = NSMakeRange(charRange.location, 0);
while (NSMaxRange(effRange) < NSMaxRange(charRange)) {
NSUInteger currentCharIndex = NSMaxRange(effRange);
NSNumber *dynamicText = [attrStr attribute:kDynamicTextAttributeName
atIndex:currentCharIndex effectiveRange:&effRange];
if (dynamicText == nil) {
// Invoke default behavior for the range:
[[NSGlyphGenerator sharedGlyphGenerator]
generateGlyphsForGlyphStorage:self
desiredNumberOfCharacters:effRange.length glyphIndex:glyphIndex
characterIndex:charIndex];
} else {
// Insert glyphs in place of the default glyph for the character.
// For testing, use 3 arbitrary glyphs:
NSUInteger newGlyphsLength = 3;
NSUInteger newGlyphs[newGlyphsLength] = {70, 71, 72};
NSUInteger newGlyphIndex = *glyphIndex;
NSUInteger newCharIndex = *charIndex;
[glyphStorage insertGlyphs:newGlyphs length:newGlyphsLength
forStartingGlyphAtIndex:newGlyphIndex characterIndex:newCharIndex];
// If we invoke default behavior again, it will need these values
for glyphIndex and charIndex (this seems to cause the errors):
*glyphIndex = newGlyphIndex + newGlyphsLength + effRange.length;
*charIndex = newCharIndex + effRange.length;
}
}
}
That produces a variety of errors and warnings, including “!!!
_NSGlyphTreeInsertGlyphs glyph index issue 1,” “!!!
_NSGlyphTreeInsertGlyphs glyph index issue 2,” “!!!
_NSGlyphTreeSetIntAttributeForGlyphAtIndex missing glyphs,” “!!!
_NSGlyphTreeInsertGlyphs invalid char index,” and others.
What is the correct approach to take when you need NSLayoutManager to
make on-the-fly adjustments to the glyphs it returns for particular
characters?_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden