conflict between Core Graphics and NSView graphics

  • hi all

    i converted a bunch of quickdraw code to core graphics successfully -- it
    worked

    now i am converting a bunch of carbon UI code to cocoa

    but when I draw text, my fonts are wrong and my  font sizes are much too big

    i am getting the CGContextRef from the NSWindow in the drawRect handler, and
    then setting the CG font and size, etc. like before

    but it is as if I need to also set the NSFont or something like that

    other graphics (lines, bitmaps, etc.) are drawing correctly

    can someone shed some light on this and/or point me to some documentation?

    thanks,

    bill appleton
  • On Apr 26, 2010, at 12:24 PM, Bill Appleton wrote:

    > but when I draw text, my fonts are wrong and my  font sizes are much too big
    >
    > i am getting the CGContextRef from the NSWindow in the drawRect handler, and
    > then setting the CG font and size, etc. like before

    Try checking the text matrix. Cocoa Text will often use the text matrix to scale rather than setting the font size, so if you do both you get text that is way too big. Since the text matrix isn't saved/restored with the GState, this can often propagate from other text drawing you don't control (such as for other controls).
    --
    David Duncan
    Apple DTS Animation and Printing
  • Bill,

    How are you rendering the text ?

    We recommend sticking to Cocoa text rendering API in order to support proper Unicode rendering.

    Thanks,

    Aki

    On Apr 26, 2010, at 12:24 PM, Bill Appleton wrote:

    > hi all
    >
    > i converted a bunch of quickdraw code to core graphics successfully -- it
    > worked
    >
    > now i am converting a bunch of carbon UI code to cocoa
    >
    > but when I draw text, my fonts are wrong and my  font sizes are much too big
    >
    > i am getting the CGContextRef from the NSWindow in the drawRect handler, and
    > then setting the CG font and size, etc. like before
    >
    > but it is as if I need to also set the NSFont or something like that
    >
    > other graphics (lines, bitmaps, etc.) are drawing correctly
    >
    > can someone shed some light on this and/or point me to some documentation?
    >
    >
    > thanks,
    >
    > bill appleton
  • hi Aki,

    i am using CGContextShowGlyphsWithAdvances

    i have a very large amount of core graphics code, i thought it was
    compatible with cocoa

    i see the text in the right spot but the size is gigantic

    thanks,

    bill

    On Mon, Apr 26, 2010 at 12:34 PM, Aki Inoue <aki...> wrote:

    > Bill,
    >
    > How are you rendering the text ?
    >
    > We recommend sticking to Cocoa text rendering API in order to support
    > proper Unicode rendering.
    >
    > Thanks,
    >
    > Aki
    >
    > On Apr 26, 2010, at 12:24 PM, Bill Appleton wrote:
    >
    >> hi all
    >>
    >> i converted a bunch of quickdraw code to core graphics successfully -- it
    >> worked
    >>
    >> now i am converting a bunch of carbon UI code to cocoa
    >>
    >> but when I draw text, my fonts are wrong and my  font sizes are much too
    > big
    >>
    >> i am getting the CGContextRef from the NSWindow in the drawRect handler,
    > and
    >> then setting the CG font and size, etc. like before
    >>
    >> but it is as if I need to also set the NSFont or something like that
    >>
    >> other graphics (lines, bitmaps, etc.) are drawing correctly
    >>
    >> can someone shed some light on this and/or point me to some
    > documentation?
    >>
    >>
    >> thanks,
    >>
    >> bill appleton
    >
    >
  • OK, sounds like you're being affected by the text matrix.

    In CG, there are 3 font properties that determine the font rendering: font name, font size, and text matrix.
    You need to manage all 3.

    The easiest approach is to use -[NSFont setInContext:] to let the Cocoa object set all 3 properties for you.
    Another approach is to reset the text matrix directly using CGContextSetTextMatrix.

    Note that the translation part (tx and ty components) are directly tied to the text position (CGContextSetTextPosition).

    You need to reset the text matrix before calling CGContextSetTextPosition, or you could just update the text position using CGContextSetTextMatrix.

    Aki

    On Apr 26, 2010, at 12:39 PM, Bill Appleton wrote:

    > hi Aki,
    >
    > i am using CGContextShowGlyphsWithAdvances
    >
    > i have a very large amount of core graphics code, i thought it was compatible with cocoa
    >
    > i see the text in the right spot but the size is gigantic
    >
    >
    >
    > thanks,
    >
    > bill
    >
    >
    >
    >
    >
    > On Mon, Apr 26, 2010 at 12:34 PM, Aki Inoue <aki...> wrote:
    > Bill,
    >
    > How are you rendering the text ?
    >
    > We recommend sticking to Cocoa text rendering API in order to support proper Unicode rendering.
    >
    > Thanks,
    >
    > Aki
    >
    > On Apr 26, 2010, at 12:24 PM, Bill Appleton wrote:
    >
    >> hi all
    >>
    >> i converted a bunch of quickdraw code to core graphics successfully -- it
    >> worked
    >>
    >> now i am converting a bunch of carbon UI code to cocoa
    >>
    >> but when I draw text, my fonts are wrong and my  font sizes are much too big
    >>
    >> i am getting the CGContextRef from the NSWindow in the drawRect handler, and
    >> then setting the CG font and size, etc. like before
    >>
    >> but it is as if I need to also set the NSFont or something like that
    >>
    >> other graphics (lines, bitmaps, etc.) are drawing correctly
    >>
    >> can someone shed some light on this and/or point me to some documentation?
    >>
    >>
    >> thanks,
    >>
    >> bill appleton
    >
    >
  • thanks all!

    On Mon, Apr 26, 2010 at 12:50 PM, Aki Inoue <aki...> wrote:

    > OK, sounds like you're being affected by the text matrix.
    >
    > In CG, there are 3 font properties that determine the font rendering: font
    > name, font size, and text matrix.
    > You need to manage all 3.
    >
    > The easiest approach is to use -[NSFont setInContext:] to let the Cocoa
    > object set all 3 properties for you.
    > Another approach is to reset the text matrix directly using
    > CGContextSetTextMatrix.
    >
    > Note that the translation part (tx and ty components) are directly tied to
    > the text position (CGContextSetTextPosition).
    >
    > You need to reset the text matrix before calling CGContextSetTextPosition,
    > or you could just update the text position using CGContextSetTextMatrix.
    >
    > Aki
    >
    > On Apr 26, 2010, at 12:39 PM, Bill Appleton wrote:
    >
    > hi Aki,
    >
    > i am using CGContextShowGlyphsWithAdvances
    >
    > i have a very large amount of core graphics code, i thought it was
    > compatible with cocoa
    >
    > i see the text in the right spot but the size is gigantic
    >
    >
    >
    > thanks,
    >
    > bill
    >
    >
    >
    >
    >
    > On Mon, Apr 26, 2010 at 12:34 PM, Aki Inoue <aki...> wrote:
    >
    >> Bill,
    >>
    >> How are you rendering the text ?
    >>
    >> We recommend sticking to Cocoa text rendering API in order to support
    >> proper Unicode rendering.
    >>
    >> Thanks,
    >>
    >> Aki
    >>
    >> On Apr 26, 2010, at 12:24 PM, Bill Appleton wrote:
    >>
    >>> hi all
    >>>
    >>> i converted a bunch of quickdraw code to core graphics successfully --
    >> it
    >>> worked
    >>>
    >>> now i am converting a bunch of carbon UI code to cocoa
    >>>
    >>> but when I draw text, my fonts are wrong and my  font sizes are much too
    >> big
    >>>
    >>> i am getting the CGContextRef from the NSWindow in the drawRect handler,
    >> and
    >>> then setting the CG font and size, etc. like before
    >>>
    >>> but it is as if I need to also set the NSFont or something like that
    >>>
    >>> other graphics (lines, bitmaps, etc.) are drawing correctly
    >>>
    >>> can someone shed some light on this and/or point me to some
    >> documentation?
    >>>
    >>>
    >>> thanks,
    >>>
    >>> bill appleton
    >>
    >>
    >
    >
  • On Apr 26, 2010, at 12:50 PM, Aki Inoue wrote:

    > In CG, there are 3 font properties that determine the font
    > rendering: font name, font size, and text matrix.
    > You need to manage all 3.

    Is the text matrix just used for special effects like obliquing? Using
    it to scale the text could produce the wrong results if the font has
    behaviors that are nonlinear with the point size, since those
    behaviors would be based on the font size not the scaling in the text
    matrix.

    In other words, it's possible that a glyph at 24pt is not identical to
    the same glyph at 12pt scaled up by a factor of 2. Some fonts change
    their proportions and stroke widths at different point sizes to
    improve legibility. (This "optical scaling" was actually common in the
    days of metal type, vanished with optical and computer typesetting,
    and has I believe returned with OpenType.)

    The thing developers need to keep in mind is that, if you're drawing
    12pt text that's been zoomed in to 200%, you should keep the font size
    at 12 and scale the drawing coords by 2, instead of just changing the
    font size to 24.

    —Jens
  • We always us the text matrix instead of font size.

    While it is true that the advancements and glyph outline could be affected by the font point size down in the font system, the CG graphics model doesn't work that way.

    What we recommend is to stick to the Cocoa Text System that tries very hard to keep these inconsistencies hidden from the apps 8-).

    Aki

    On Apr 26, 2010, at 1:29 PM, Jens Alfke wrote:

    >
    > On Apr 26, 2010, at 12:50 PM, Aki Inoue wrote:
    >
    >> In CG, there are 3 font properties that determine the font rendering: font name, font size, and text matrix.
    >> You need to manage all 3.
    >
    > Is the text matrix just used for special effects like obliquing? Using it to scale the text could produce the wrong results if the font has behaviors that are nonlinear with the point size, since those behaviors would be based on the font size not the scaling in the text matrix.
    >
    > In other words, it's possible that a glyph at 24pt is not identical to the same glyph at 12pt scaled up by a factor of 2. Some fonts change their proportions and stroke widths at different point sizes to improve legibility. (This "optical scaling" was actually common in the days of metal type, vanished with optical and computer typesetting, and has I believe returned with OpenType.)
    >
    > The thing developers need to keep in mind is that, if you're drawing 12pt text that's been zoomed in to 200%, you should keep the font size at 12 and scale the drawing coords by 2, instead of just changing the font size to 24.
    >
    > —Jens
  • On 26 Apr 2010, at 20:24, Bill Appleton wrote:

    > hi all
    >
    > i converted a bunch of quickdraw code to core graphics successfully -- it
    > worked
    >
    > now i am converting a bunch of carbon UI code to cocoa
    >
    > but when I draw text, my fonts are wrong and my  font sizes are much too big
    >
    > i am getting the CGContextRef from the NSWindow in the drawRect handler, and
    > then setting the CG font and size, etc. like before

    Further to the other answers, this part surprises me. I think you want [[NSGraphicsContext currentContext] graphicsPort] instead to get to the CGContext.

    Mike.
  • that CGContextRef is the same as [[thewind graphicsContext] graphicsPort] in
    the scope if the drawRect routine

    (i am away from that machine, the code is probably wrong, but you can see
    what i mean)

    so the two methods return the same context

    On Mon, Apr 26, 2010 at 3:58 PM, Mike Abdullah <cocoadev...>wrote:

    >
    > On 26 Apr 2010, at 20:24, Bill Appleton wrote:
    >
    >> hi all
    >>
    >> i converted a bunch of quickdraw code to core graphics successfully -- it
    >> worked
    >>
    >> now i am converting a bunch of carbon UI code to cocoa
    >>
    >> but when I draw text, my fonts are wrong and my  font sizes are much too
    > big
    >>
    >> i am getting the CGContextRef from the NSWindow in the drawRect handler,
    > and
    >> then setting the CG font and size, etc. like before
    >
    > Further to the other answers, this part surprises me. I think you want
    > [[NSGraphicsContext currentContext] graphicsPort] instead to get to the
    > CGContext.
    >
    > Mike.
previous month april 2010 next month
MTWTFSS
      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    
Go to today