Professional Web Applications Themes

boundingRectForGlyph: does...? - Mac Programming

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]...

  1. #1

    Default 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

  2. #2

    Default Re: boundingRectForGlyph: does...?

    In <clund-3735B5.11500009082003amstwist00.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.

    --
    matt neuburg, phd = [email]matttidbits.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

  3. #3

    Default Re: boundingRectForGlyph: does...?

    In article <clund-F81E8E.14034010082003amstwist00.chello.com>,
    C Lund <clundNOSPAMnotam02.no> wrote:
    > In article <20030809073212250-0700news.la.sbcglobal.net>,
    > matt neuburg <matttidbits.com> wrote:
    >
    > > In <clund-3735B5.11500009082003amstwist00.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.
    boundingRectForGlyph: returns the bounding box of the shape, but I think
    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

  4. #4

    Default Re: boundingRectForGlyph: does...?

    In article <clund-58AC4C.18140411082003amstwist00.chello.com>,
    C Lund <clundNOSPAMnotam02.no> wrote:

    > 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.
    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.

    (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

  5. #5

    Default Re: boundingRectForGlyph: does...?

    In <just-07F930.20244311082003news1.news.xs4all.nl> Just wrote:
    > In article <clund-58AC4C.18140411082003amstwist00.chello.com>,
    > C Lund <clundNOSPAMnotam02.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.
    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.

    --
    matt neuburg, phd = [email]matttidbits.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

  6. #6

    Default Re: boundingRectForGlyph: does...?

    In article <just-07F930.20244311082003news1.news.xs4all.nl>,
    Just <justxs4all.nl> wrote:
    > 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.
    So you're saying that this:

    [[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...
    > (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.)
    What I'm trying to do is print all 256 characters of a given font and
    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

  7. #7

    Default Re: boundingRectForGlyph: does...?

    In article <clund-40B570.20525211082003amstwist00.chello.com>,
    C Lund <clundNOSPAMnotam02.no> wrote:
    > In other words, charIndex = 70 in the above line will not necessarily
    > refer to the glyph for capital 'F'?
    Correct. It will actually very unlikely be the glyph for capital 'F' ;-)
    > Guess I'd better start looking for sample code involving
    > NSLayoutManager...
    Or follow Matt Neuburg's advice.
    > What I'm trying to do is print all 256 characters of a given font
    What makes you think a font has 256 characters? Many have fewer, many
    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/doentation/Carbon/Conceptual/ATSUI_Concepts[/url]
    /atsui_chap2/chapter_2_section_2.html

    Just
    Just Guest

  8. #8

    Default Re: boundingRectForGlyph: does...?

    In article <just-E8DDF9.21402911082003news1.news.xs4all.nl>,
    Just <justxs4all.nl> wrote:
    > In article <clund-40B570.20525211082003amstwist00.chello.com>,
    > C Lund <clundNOSPAMnotam02.no> wrote:
    > > In other words, charIndex = 70 in the above line will not necessarily
    > > refer to the glyph for capital 'F'?
    > Correct. It will actually very unlikely be the glyph for capital 'F' ;-)
    > > Guess I'd better start looking for sample code involving
    > > NSLayoutManager...
    > Or follow Matt Neuburg's advice.
    That pretty much was his advice.. ;)
    > Here's a brief intro:
    > [url]http://developer.apple.com/doentation/Carbon/Conceptual/ATSUI_Concepts[/url]
    > /atsui_chap2/chapter_2_section_2.html
    Thanks. I'll look into it. B)
    > Just
    --
    C Lund, [url]www.notam02.no/~clund[/url]
    C Lund Guest

  9. #9

    Default Re: boundingRectForGlyph: does...?

    In <clund-BAE45E.10040512082003amstwist00.chello.com> C Lund wrote:
    > In article <20030811114548678-0700news.la.sbcglobal.net>,
    > matt neuburg <matttidbits.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.
    In what sense not gone well? boundingRectForGlyphRange works great for
    me; that's why I suggested it. m.

    --
    matt neuburg, phd = [email]matttidbits.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

  10. #10

    Default Re: boundingRectForGlyph: does...?

    In article <20030812075058912-0700news.la.sbcglobal.net>,
    matt neuburg <matttidbits.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 what sense not gone well? boundingRectForGlyphRange works great for
    > me; that's why I suggested it. m.
    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.

    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

  11. #11

    Default Re: boundingRectForGlyph: does...?

    In article <20030813124444353-0700news.la.sbcglobal.net>,
    matt neuburg <matttidbits.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.
    > So you have *not* tried boundingRectForGlyphRange (because you haven't
    > been able to get your code to run).
    Well, I have tried to get it to run.
    > And what about sizeWithAttributes?
    I missed that the first time you mentioned it. And that works perfectly.
    Thanks. B)

    --
    C Lund, [url]www.notam02.no/~clund[/url]
    C Lund Guest

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139