Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Drawing Text Using a Quartz Context

With Mac OS X version 10.2, ATSUI renders text through Quartz, even if you do not attach a CGContext to a text layout object. In this default case, ATSUI retrieves the internal canonical CGContext of the current port, and renders to that port using Quartz at an anti-aliasing setting that simulates QuickDraw rendering. That is, a 4-bit pixel-aligned anti-aliasing. With this method of rendering, the origins of the glyphs always fall on integer positions and can lead to less-than-ideal glyph placements even after ATSUI makes fine adjustments to the integer positioning. This section shows you how to you set up ATSUI to instead use an 8-bit, subpixel rendering. Using this method of rendering, glyph origins are positioned on fractional points, resulting in superior rendering compared to ATSUI’s default 4-bit pixel-aligned rendering.

To set up ATSUI to use an 8-bit, subpixel rendering through Quartz, you must perform the following tasks:

  1. Set up a Quartz context (CGContext) for your application by using the QuickDraw function QDBeginCGContext. When you are done using the CGContext, you must call the function QDEndCGContext.

    You need to call the function QDBeginCGContext only once in your application, as you should use the same CGContext until all drawing is completed.

  2. Set the Quartz context as a layout attribute for the text layout object whose text you want to draw, using the tag kATSUCGContextTag, and by calling the function ATSUSetLayoutControls.

    For example, if you already set up a Quartz context named myCGContext, you use the following code to set the Quartz context as an attribute of a text layout object (myTextLayout) you created previously:

    ATSUAttributeTag        theTags[0] = kATSUCGContextTag;
    ByteCount               theSizes[0] = sizeof (CGContextRef);
    ATSUAttributeValuePtr   theValues[] = &myCGContext;
     
    ATSUSetLayoutControls (myTextLayout,
                        1,
                        theTags,
                        theSizes,
                        theValues);

When you use Quartz with ATSUI, you can use all the effects available through Quartz 2D. You can rotate the Quartz context to achieve a number of effects, such as the angled text as shown in Figure 3-1 and the rotated text shown in Figure 3-2.

Listing 3-3 shows the code necessary to draw rotated text using Quartz 2D. First, you call the Quartz 2D function CGContextRotateCTM to rotate the Quartz context by a specified angle. Then, when you call the function ATSUDrawText, the text is drawn into the rotated context. The text appears on the screen when you call the function CGContextFlush.


Figure 3-1  Text drawn at an angle

Text drawn at an angle

Listing 3-3  Using a Quartz context to rotate text

CGContextRotateCTM (myCGContext, myAngle);
ATSUDrawText (myTextLayout,
            kATSUFromTextBeginning,
            kATSUToTextEnd,
            myXLocation,
            myXLocation);
CGContextFlush (myCGContext);

Tip: You can draw more than once into a Quartz context without calling the function CGContextFlush. This is the most optimal way to do batch drawing using a Quartz context.


Figure 3-2  Rotated text

Rotated text



< Previous PageNext Page > Hide TOC


Last updated: 2007-07-10




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.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice