Ask a Question related to Mac Programming, Design and Development.
-
C Lund #1
boundingRectForGlyph: does...?
I'm trying to find a way to determine the width of a given glyph of a
specified font. I assumed
fontWidth[charIndex] = [[theFont boundingRectForGlyph:
charIndex].size.width];
would give me the values I want, but that is not the case. While the
values are in the right range, they are usually wrong. The above line
gives values that are several pixels too large or small - and sometimes
correct.
Any suggestions?
--
C Lund, [url]www.notam02.no/~clund[/url]
C Lund Guest
-
matt neuburg #2
Re: boundingRectForGlyph: does...?
In <clund-3735B5.11500009082003@amstwist00.chello.com> C Lund wrote:
Try NSLayoutManager's boundingRectForGlyphRange. m.> I'm trying to find a way to determine the width of a given glyph of a
> specified font. I assumed
>
> fontWidth[charIndex] = [[theFont boundingRectForGlyph:
> charIndex].size.width];
>
> would give me the values I want, but that is not the case. While the
> values are in the right range, they are usually wrong. The above line
> gives values that are several pixels too large or small - and
> sometimes correct.
>
> Any suggestions?
--
matt neuburg, phd = [email]matt@tidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
REALbasic: The Definitive Guide! 2nd edition!
[url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
Subscribe to TidBITS. It's free and smart.
matt neuburg Guest
-
Just #3
Re: boundingRectForGlyph: does...?
In article <clund-F81E8E.14034010082003@amstwist00.chello.com>,
C Lund <clund@NOSPAMnotam02.no> wrote:
boundingRectForGlyph: returns the bounding box of the shape, but I think> In article <20030809073212250-0700@news.la.sbcglobal.net>,
> matt neuburg <matt@tidbits.com> wrote:
>>> > In <clund-3735B5.11500009082003@amstwist00.chello.com> C Lund wrote:> >> > > I'm trying to find a way to determine the width of a given glyph of a
> > > specified font. I assumed
> > >
> > > fontWidth[charIndex] = [[theFont boundingRectForGlyph:
> > > charIndex].size.width];
> > >
> > > would give me the values I want, but that is not the case. While the
> > > values are in the right range, they are usually wrong. The above line
> > > gives values that are several pixels too large or small - and
> > > sometimes correct.
> > >
> > > Any suggestions?
> > Try NSLayoutManager's boundingRectForGlyphRange. m.
> So you're saying NSFont's boundingRectForGlyph doesn't work? Or does
> something else? According to apple:
>
> "boundingRectForGlyph:
>
> - (NSRect)boundingRectForGlyph:(NSGlyph)aGlyph
>
> Returns the bounding rectangle for aGlyph, scaled to the receiveršs
> size. "
>
> Sounds very much like what I need. Yet it seems to return more or less
> random values.
>
>
> Haven't managed to get your suggestion to work either - not yet, anyway.
> But it seems to be quite a lot of work just to get the width of a given
> glyph.
you're looking for the "advance width". Have a look at the
advancementForGlyph: method of NSFont, maybe that's what you need. (Note
that this does not suffice to calculate the width of a string since it
disregards kerning and layout, so as soon as you're working with
(unicode) text as opposed to single glyphs, it really is better to work
with NSLayoutManager.
Just
Just Guest
-
Just #4
Re: boundingRectForGlyph: does...?
In article <clund-58AC4C.18140411082003@amstwist00.chello.com>,
C Lund <clund@NOSPAMnotam02.no> wrote:
Ah, basic assumption error: glyph != character. I can't seem to find a> Maybe I should ask Apple about this. 'advancementForGlyph:' and
> 'boundingRectForGlyph:' don't seem to be doing what I thought they were
> supposed to do.
>
> Just in case the flaw is in my code; this is the line I use for drawing
> a character in an NSView:
>
> [[NSString stringWithFormat: @"%c",charIndex] drawAtPoint:
> NSMakePoint(xPos,yPos) withAttributes: fontAttributes];
>
> And this is where I set the width of the glyph:
>
> glyphWidth[charIndex] = [theFont advancementForGlyph:
> charIndex].width;
>
> 'charIndex' is an unsigned int with a value between 0 and 255.
>
> Is there any obvious reason why this would give the wrong results for
> glyphWidth[]?
>
> I would add that there is some consistency to the errors; the "m", for
> isntance, is usually given a width that is too small, while the "!" is
> usually given too large a value. "h", otoh, usually gets a width that
> looks right.
quick way to get the glyph id from a character, so perhaps you'll have
to use NSLayoutManager anyway.
(Note that there isn't always a 1 on 1 mapping between glyphs and
characters, especially in complex scripts. One character may be
represented with multiple glyphs, several characters may be represented
by one glyph. This may not be relevant to what you're trying to do, but
may explain the complexity of the text subsystem.)
Just
Just Guest
-
matt neuburg #5
Re: boundingRectForGlyph: does...?
In <just-07F930.20244311082003@news1.news.xs4all.nl> Just wrote:
Right - this was my point. The problem of a string's width is well> In article <clund-58AC4C.18140411082003@amstwist00.chello.com>,
> C Lund <clund@NOSPAMnotam02.no> wrote:
>>>> [[NSString stringWithFormat: @"%c",charIndex] drawAtPoint:
>> NSMakePoint(xPos,yPos) withAttributes: fontAttributes];
>> glyphWidth[charIndex] = [theFont advancementForGlyph:
>> charIndex].width;
>>
>> 'charIndex' is an unsigned int with a value between 0 and 255.
>> Is there any obvious reason why this would give the wrong results for
>> glyphWidth[]?
> Ah, basic assumption error: glyph != character.
solved by sizeWithAttributes (and by boundingRectForGlyphRange, which I
mentioned earlier), and a string is exactly what you've got, so why
aren't you even trying them? m.
--
matt neuburg, phd = [email]matt@tidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
REALbasic: The Definitive Guide! 2nd edition!
[url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
Subscribe to TidBITS. It's free and smart.
matt neuburg Guest
-
C Lund #6
Re: boundingRectForGlyph: does...?
In article <just-07F930.20244311082003@news1.news.xs4all.nl>,
Just <just@xs4all.nl> wrote:
So you're saying that this:> Ah, basic assumption error: glyph != character. I can't seem to find a
> quick way to get the glyph id from a character, so perhaps you'll have
> to use NSLayoutManager anyway.
[[NSString stringWithFormat: @"%c",charIndex] drawAtPoint:
NSMakePoint(xPos,yPos) withAttributes: fontAttributes];
will not necessarily produce the same symbol that is referred to with
this:
glyphWidth[charIndex] = [theFont advancementForGlyph:
charIndex].width;
In other words, charIndex = 70 in the above line will not necessarily
refer to the glyph for capital 'F'?
Guess I'd better start looking for sample code involving
NSLayoutManager...
What I'm trying to do is print all 256 characters of a given font and> (Note that there isn't always a 1 on 1 mapping between glyphs and
> characters, especially in complex scripts. One character may be
> represented with multiple glyphs, several characters may be represented
> by one glyph. This may not be relevant to what you're trying to do, but
> may explain the complexity of the text subsystem.)
given size in a 16x16 grid, and then save this matrix as an image
accompanied with an array holding the width of all the symbols in the
grid. The graphical bit is done, but I'm trying to figure out how to do
the width bit without making it necessary for the user (me) to go
manually set the width on each and every symbol on the grid.
--> Just
C Lund, [url]www.notam02.no/~clund[/url]
C Lund Guest
-
Just #7
Re: boundingRectForGlyph: does...?
In article <clund-40B570.20525211082003@amstwist00.chello.com>,
C Lund <clund@NOSPAMnotam02.no> wrote:
Correct. It will actually very unlikely be the glyph for capital 'F' ;-)> In other words, charIndex = 70 in the above line will not necessarily
> refer to the glyph for capital 'F'?
Or follow Matt Neuburg's advice.> Guess I'd better start looking for sample code involving
> NSLayoutManager...
What makes you think a font has 256 characters? Many have fewer, many> What I'm trying to do is print all 256 characters of a given font
have more, many have many more. Think Unicode. Also: a font doesn't
contain characters, but glyphs...
Here's a brief intro:
[url]http://developer.apple.com/documentation/Carbon/Conceptual/ATSUI_Concepts[/url]
/atsui_chap2/chapter_2_section_2.html
Just
Just Guest
-
C Lund #8
Re: boundingRectForGlyph: does...?
In article <just-E8DDF9.21402911082003@news1.news.xs4all.nl>,
Just <just@xs4all.nl> wrote:That pretty much was his advice.. ;)> In article <clund-40B570.20525211082003@amstwist00.chello.com>,
> C Lund <clund@NOSPAMnotam02.no> wrote:> Correct. It will actually very unlikely be the glyph for capital 'F' ;-)> > In other words, charIndex = 70 in the above line will not necessarily
> > refer to the glyph for capital 'F'?> Or follow Matt Neuburg's advice.> > Guess I'd better start looking for sample code involving
> > NSLayoutManager...
Thanks. I'll look into it. B)> Here's a brief intro:
> [url]http://developer.apple.com/documentation/Carbon/Conceptual/ATSUI_Concepts[/url]
> /atsui_chap2/chapter_2_section_2.html
--> Just
C Lund, [url]www.notam02.no/~clund[/url]
C Lund Guest
-
matt neuburg #9
Re: boundingRectForGlyph: does...?
In <clund-BAE45E.10040512082003@amstwist00.chello.com> C Lund wrote:
In what sense not gone well? boundingRectForGlyphRange works great for> In article <20030811114548678-0700@news.la.sbcglobal.net>,
> matt neuburg <matt@tidbits.com> wrote:
>>>>>> >> 'charIndex' is an unsigned int with a value between 0 and 255.
>> >> Is there any obvious reason why this would give the wrong results
>> >> for glyphWidth[]?
>> >
>> > Ah, basic assumption error: glyph != character.
>> Right - this was my point. The problem of a string's width is well
>> solved by sizeWithAttributes (and by boundingRectForGlyphRange, which
>> I mentioned earlier), and a string is exactly what you've got, so
>> why aren't you even trying them? m.
> I have been trying them. It just hasn't gone very well so far.
me; that's why I suggested it. m.
--
matt neuburg, phd = [email]matt@tidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
REALbasic: The Definitive Guide! 2nd edition!
[url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
Subscribe to TidBITS. It's free and smart.
matt neuburg Guest
-
C Lund #10
Re: boundingRectForGlyph: does...?
In article <20030812075058912-0700@news.la.sbcglobal.net>,
matt neuburg <matt@tidbits.com> wrote:
> >> Right - this was my point. The problem of a string's width is well
> >> solved by sizeWithAttributes (and by boundingRectForGlyphRange, which
> >> I mentioned earlier), and a string is exactly what you've got, so
> >> why aren't you even trying them? m.> > I have been trying them. It just hasn't gone very well so far.In order to use boundingRectForGlyphRange I have to declare> In what sense not gone well? boundingRectForGlyphRange works great for
> me; that's why I suggested it. m.
textstoragers, layoutmanagers, and so on. I get the thing to compile,
but it crashes with an internal inconsistancy exception when I try to
run it.
But I have some sample code to look at, so I suppose I'll get it to work
eventually.
--
C Lund, [url]www.notam02.no/~clund[/url]
C Lund Guest
-
C Lund #11
Re: boundingRectForGlyph: does...?
In article <20030813124444353-0700@news.la.sbcglobal.net>,
matt neuburg <matt@tidbits.com> wrote:
> > In order to use boundingRectForGlyphRange I have to declare
> > textstoragers, layoutmanagers, and so on. I get the thing to compile,
> > but it crashes with an internal inconsistancy exception when I try to
> > run it.Well, I have tried to get it to run.> So you have *not* tried boundingRectForGlyphRange (because you haven't
> been able to get your code to run).
I missed that the first time you mentioned it. And that works perfectly.> And what about sizeWithAttributes?
Thanks. B)
--
C Lund, [url]www.notam02.no/~clund[/url]
C Lund Guest



Reply With Quote

