Programmer's Reference

String drawing operations with fonts

You can draw text by sending the drawString:, drawImageString:, or drawText: messages to a CgDrawable object. As in all drawing operations, the first parameter passed to the method must be a valid graphics context. This graphics context will contain either a default font or the font you have loaded and set in the graphics context.

In all string and text drawing methods the y-coordinate specifies the coordinate where the baseline of the string will be drawn, not the top of the string's bounding box.

Drawing a string

The drawString:x:y:string: method draws a string at a given point in the foreground color. Only the pixels for the characters are modified. Pixels between characters are left unchanged.

CgWindow default
    drawString: CgGC default
    x: 10
    y: 100
    string: 'Going global'.


Figure SP006120 not displayed.


Drawing an image string

The drawImageString:x:y:string: method draws a string at a given point. The text is drawn in the foreground color and the pixels in the remainder of the rectangle containing the string are set to the background color.

CgWindow default
    drawImageString: CgGC default
    x: 10
    y: 100
    string: 'Hello world'.


Figure SP006125 not displayed.


Drawing multifont strings using CgTextItem objects

The drawText:x:y:items: allows multiple strings to be drawn at once. Strings are drawn in the same manner as drawString:. The background is left unmodified. Each string is specified by a CgTextItem object.

A CgTextItem object contains character, delta and font components. The char component specifies the string value, delta represents the horizontal offset from the previous item and font represents the font to be used in the drawing operation. The font component is optional. If it is specified, the font in the graphics context is changed and the string is drawn in the new font. Otherwise, the string is drawn in the current font. The CgTextItem must be provided with at least the char and delta information. These values can be set individually, with chars:, delta:, or font:, or simultaneously, with chars:delta:font:.

| fontName1 font1 fontName2 font2 item1 item2 item3 |
fontName1 := (CgDisplay default
    listFonts: '-*-helvetica-bold-r-normal-*-*-240-*-*-*-*-*-*'
    maxnames: 1) at: 1.
font1 := CgDisplay default loadFont: fontName1.
 
fontName2 := (CgDisplay default
    listFonts: '-*-helvetica-bold-i-normal-*-*-240-*-*-*-*-*-*'
    maxnames: 1) at: 1.
font2 := CgDisplay default loadFont: fontName2.
 
item1 := CgTextItem chars: 'The ' delta: 0 font: font1.
item2 := CgTextItem chars: 'quick ' delta: 0 font: font2.
item3 := CgTextItem chars: 'brown fox' delta: 0 font: font1.
 
CgWindow default
    drawText: CgGC default
    x: 10
    y: 100
    items: (Array with: item1 with: item2 with: item3).


Figure SP006130 not displayed.



[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]