ADC Home > Reference Library > Technical Q&As > Graphics & Imaging > Quartz >


Q: What is SyncCGContextOriginWithPort and why do I need it?

A: The Core Graphics coordinate space is defined differently than that of QuickDraw. In QuickDraw, the origin is at the upper left of the CGrafPort and the positive y-axis points down. Core Graphics, on the other hand, defines the origin to be the bottom left corner of the CGContext and the positive y-axis points up.

SyncCGContextOriginWithPort is a convenience function that moves the CGContext origin from the bottom left to adjust for any calls to SetOrigin for the port.

//  Create the CGContext from a given QD port
CGContextRef context;
Rect portRect;
OSStatus err = CreateCGContextForPort( qdPort, &context );
if ( noErr == err )
    //  Adjust for any SetOrigin calls on qdPort
    SyncCGContextOriginWithPort( context, qdPort );

    //  Move the CG origin to the upper left of the port
    GetPortBounds( qdPort, &portRect );
    CGContextTranslateCTM( context, 0, (float)(portRect.bottom - );

    //  Flip the y axis so that positive Y points down
    //    Note that this will cause text drawn with Core Graphics
    //  to draw upside down
    CGContextScaleCTM( context, 1.0, -1.0 );

    //  The CG coordinate space now matches the QD coordinate space
    //  ...
    //  Do your CG drawing here
    //  ...

    //  Release the context now that we are done with it
    CGContextRelease( context );
//  Back to normal QuickDraw drawing

Listing 1. Setting up the Core Graphics Context

Note that SyncCGContextOriginWithPort does not flip the y-axis for you or move the origin from the bottom left to the upper left. Therefore you either need to account for the difference in your drawing code or call the code in Listing 1 to move the origin and flip the y-axis for all subsequent Core Graphics drawing operations on that context.

[Apr 11 2001]

Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.