Re: NSGlyph from Unicode
Re: NSGlyph from Unicode
- Subject: Re: NSGlyph from Unicode
- From: Pascal Goguey <email@hidden>
- Date: Thu, 14 Sep 2006 11:15:21 +0900
Hello Douglas!
Thanks for your reply.
Ok, I fixed the allocate and release part, and it seems there is no
major speed problem
anymore. The rest of the code is the same, so
Now, in the "number of things I am missing", I understand that the
actual shape of a
character depends on the font and may also depend on the surrounding
characters.
That's why I ended up using a layout manager because I think the
layout manager
should have the info I need (i.e. the right glyph at the right place).
What I am doing: I put the incoming text into a textstorage object.
The text storage
then gets a layout manager that should take care of its contents. Now
if I ask
the layout manager for its glyphs, shouldn't it give me the right
values?
Here is a simplified version of my method. All objects are supposed
to be created
somewhere and to exist. For simplicity purposes, I didn't add any
test for validity.
Which step is wrong? Is there a missing step?
- (NSBezierPath *) createBezierPathFromString:(NSString *)string
ofSize:(float)size {
int position = 0;
NSGlyph g;
NSBezierPath * bezier = [NSBezierPath bezierPath];
// Step 1: load the tectStorage object with my incoming string
[textStorage setAttributedString:[[NSAttributedString alloc]
initWithAttributes:attributes];
// Step 2 : set the layout manager for the text storage.
[textStorage addLayoutManager:layoutManager];
// Make a bezier path that will represent our text.
bezier = [[NSBezierPath alloc] init];
// Init the first drawing point
[bezier moveToPoint:NSMakePoint(0.0, 0.0)];
// Step 3 : Find each style run in the NSTextStorage. Note that
there might
// be a shorter way of getting all the bezier info of a text run.
But for the time being
// I would like to make this one work.
int count = [layoutManager numberOfGlyphs];
while(position < count) {
g = [layout glyphAtIndex:position];
[bezier appendBezierPathWithGlyph:g inFont: [NSFont
boldSystemFontOfSize:size]];
position++;
}
return bezier;
}
Pascal
On 14 sept. 06, at 01:27, Douglas Davidson wrote:
On Sep 13, 2006, at 3:51 AM, Pascal Goguey wrote:
I also have messed a lot with the NSLayoutManager without success,
My second example shows the use of NSLayoutManager, etc.
It's extremely slow (I don't know if the path calculation or the
drawing is slow...).
If you don't know what part is slow, Sampler or Shark will tell
you. If you are creating and destroying a new layout manager every
time, that will definitely be slow; try keeping the text system
objects around and changing the text only when necessary. The
process of glyph generation is fairly fast; if you need glyph
generation without NSLayoutManager, you can use NSGlyphGenerator.
However, there are a number of things that you are missing. In
general there is not a one-to-one mapping between Unicode
characters and glyphs in a given font. There may be multiple
glyphs required to represent a given character, or multiple
characters may be represented by a single glyph, or there may be
more complicated situations. The proper glyph to use may depend on
context--that is, on the surrounding characters. Furthermore, the
positioning of the glyphs is not always one after another; their
positions may need adjusting, or they may need reordering.
Using the glyph generation facilities of NSLayoutManager without
layout, or using NSGlyphGenerator, will give you a first-pass
minimal translation of characters to glyphs. It may not be one-to-
one, and there may be NSNullGlyphs inserted as padding in cases
where multiple characters would otherwise be represented by a
single glyph. For the full proper contextual glyphs, however, you
must do layout as well; this is also required for positioning and
reordering.
Another issue is font substitution. A single font may not suffice
to represent all of the characters in a given string, so
NSAttributedString goes through a process known as font fixing,
whereby a new suitable font is substituted whenever characters
cannot be represented in the specified font. When you examine the
glyphs in the layout manager, you should remember that they are
glyphs in the font given by the font attribute on the attributed
string after font fixing, not necessarily the font originally
specified. You will need to use the layout manager to convert back
from glyph indexes to character indexes and examine the text
storage's font attribute at the relevant character index.
See the conceptual documentation on the Cocoa text system for more
information.
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:
This email sent to email@hidden