Next: , Previous: , Up: Top   [Index]


6 Text

Rendering text and glyphs

6.1 Overview

The functions with text in their name form cairo’s toy text API. The toy API takes UTF-8 encoded text and is limited in its functionality to rendering simple left-to-right text with no advanced features. That means for example that most complex scripts like Hebrew, Arabic, and Indic scripts are out of question. No kerning or correct positioning of diacritical marks either. The font selection is pretty limited too and doesn’t handle the case that the selected font does not cover the characters in the text. This set of functions are really that, a toy text API, for testing and demonstration purposes. Any serious application should avoid them.

The functions with glyphs in their name form cairo’s low-level text API. The low-level API relies on the user to convert text to a set of glyph indexes and positions. This is a very hard problem and is best handled by external libraries, like the pangocairo that is part of the Pango text layout and rendering library. Pango is available from http://www.pango.org/.

6.2 Usage

Function: cairo-select-font-face (cr <cairo-t>) (family <char>) (slant <cairo-font-slant-t>) (weight <cairo-font-weight-t>)

Note: The cairo-select-font-face function call is part of what the cairo designers call the "toy" text API. It is convenient for short demos and simple programs, but it is not expected to be adequate for serious text-using applications.

Selects a family and style of font from a simplified description as a family name, slant and weight. Cairo provides no operation to list available family names on the system (this is a "toy", remember), but the standard CSS2 generic family names, ("serif", "sans-serif", "cursive", "fantasy", "monospace"), are likely to work as expected.

If family starts with the string "cairo:", or if no native font backends are compiled in, cairo will use an internal font family. The internal font family recognizes many modifiers in the family string, most notably, it recognizes the string "monospace". That is, the family name "cairo:monospace" will use the monospace version of the internal font family.

For "real" font selection, see the font-backend-specific font_face_create functions for the font backend you are using. (For example, if you are using the freetype-based cairo-ft font backend, see cairo-ft-font-face-create-for-ft-face or cairo-ft-font-face-create-for-pattern.) The resulting font face could then be used with cairo-scaled-font-create and cairo-set-scaled-font.

Similarly, when using the "real" font support, you can call directly into the underlying font system, (such as fontconfig or freetype), for operations such as listing available fonts, etc.

It is expected that most applications will need to use a more comprehensive font handling and text layout library, (for example, pango), in conjunction with cairo.

If text is drawn without a call to cairo-select-font-face, (nor cairo-set-font-face nor cairo-set-scaled-font), the default family is platform-specific, but is essentially "sans-serif". Default slant is ‘CAIRO_FONT_SLANT_NORMAL’, and default weight is ‘CAIRO_FONT_WEIGHT_NORMAL’.

This function is equivalent to a call to cairo-toy-font-face-create followed by cairo-set-font-face.

cr

a <cairo-t>

family

a font family name, encoded in UTF-8

slant

the slant for the font

weight

the weight for the font

Function: cairo-set-font-size (cr <cairo-t>) (size <double>)

Sets the current font matrix to a scale by a factor of size, replacing any font matrix previously set with cairo-set-font-size or cairo-set-font-matrix. This results in a font size of size user space units. (More precisely, this matrix will result in the font’s em-square being a size by size square in user space.)

If text is drawn without a call to cairo-set-font-size, (nor cairo-set-font-matrix nor cairo-set-scaled-font), the default font size is 10.0.

cr

a <cairo-t>

size

the new font size, in user space units

Function: cairo-set-font-matrix (cr <cairo-t>) (matrix <cairo-matrix-t>)

Sets the current font matrix to matrix. The font matrix gives a transformation from the design space of the font (in this space, the em-square is 1 unit by 1 unit) to user space. Normally, a simple scale is used (see cairo-set-font-size), but a more complex font matrix can be used to shear the font or stretch it unequally along the two axes

cr

a <cairo-t>

matrix

a <cairo-matrix-t> describing a transform to be applied to the current font.

Function: cairo-get-font-matrix (cr <cairo-t>) (matrix <cairo-matrix-t>)

Stores the current font matrix into matrix. See cairo-set-font-matrix.

cr

a <cairo-t>

matrix

return value for the matrix

Function: cairo-set-font-options (cr <cairo-t>) (options <cairo-font-options-t>)

Sets a set of custom font rendering options for the <cairo-t>. Rendering options are derived by merging these options with the options derived from underlying surface; if the value in options has a default value (like ‘CAIRO_ANTIALIAS_DEFAULT’), then the value from the surface is used.

cr

a <cairo-t>

options

font options to use

Function: cairo-get-font-options (cr <cairo-t>) (options <cairo-font-options-t>)

Retrieves font rendering options set via <cairo-set-font-options>. Note that the returned options do not include any options derived from the underlying surface; they are literally the options passed to cairo-set-font-options.

cr

a <cairo-t>

options

a <cairo-font-options-t> object into which to store the retrieved options. All existing values are overwritten

Function: cairo-set-font-face (cr <cairo-t>) (font-face <cairo-font-face-t>)

Replaces the current <cairo-font-face-t> object in the <cairo-t> with font-face. The replaced font face in the <cairo-t> will be destroyed if there are no other references to it.

cr

a <cairo-t>

font-face

a <cairo-font-face-t>, or ‘#f’ to restore to the default font

Function: cairo-get-font-face (cr <cairo-t>) ⇒  (ret <cairo-font-face-t >)

Gets the current font face for a <cairo-t>.

cr

a <cairo-t>

ret

the current font face. This object is owned by cairo. To keep a reference to it, you must call cairo-font-face-reference. This function never returns ‘#f’. If memory cannot be allocated, a special "nil" <cairo-font-face-t> object will be returned on which cairo-font-face-status returns ‘CAIRO_STATUS_NO_MEMORY’. Using this nil object will cause its error state to propagate to other objects it is passed to, (for example, calling cairo-set-font-face with a nil font will trigger an error that will shutdown the <cairo-t> object).

Function: cairo-set-scaled-font (cr <cairo-t>) (scaled-font <cairo-scaled-font-t>)

Replaces the current font face, font matrix, and font options in the <cairo-t> with those of the <cairo-scaled-font-t>. Except for some translation, the current CTM of the <cairo-t> should be the same as that of the <cairo-scaled-font-t>, which can be accessed using cairo-scaled-font-get-ctm.

cr

a <cairo-t>

scaled-font

a <cairo-scaled-font-t>

Since 1.2

Function: cairo-get-scaled-font (cr <cairo-t>) ⇒  (ret <cairo-scaled-font-t >)

Gets the current scaled font for a <cairo-t>.

cr

a <cairo-t>

ret

the current scaled font. This object is owned by cairo. To keep a reference to it, you must call cairo-scaled-font-reference. This function never returns ‘#f’. If memory cannot be allocated, a special "nil" <cairo-scaled-font-t> object will be returned on which cairo-scaled-font-status returns ‘CAIRO_STATUS_NO_MEMORY’. Using this nil object will cause its error state to propagate to other objects it is passed to, (for example, calling cairo-set-scaled-font with a nil font will trigger an error that will shutdown the <cairo-t> object).

Since 1.4

Function: cairo-show-text (cr <cairo-t>) (utf8 <char>)

A drawing operator that generates the shape from a string of UTF-8 characters, rendered according to the current font_face, font_size (font_matrix), and font_options.

This function first computes a set of glyphs for the string of text. The first glyph is placed so that its origin is at the current point. The origin of each subsequent glyph is offset from that of the previous glyph by the advance values of the previous glyph.

After this call the current point is moved to the origin of where the next glyph would be placed in this same progression. That is, the current point will be at the origin of the final glyph offset by its advance values. This allows for easy display of a single logical string with multiple calls to cairo-show-text.

Note: The cairo-show-text function call is part of what the cairo designers call the "toy" text API. It is convenient for short demos and simple programs, but it is not expected to be adequate for serious text-using applications. See cairo-show-glyphs for the "real" text display API in cairo.

cr

a cairo context

utf8

a NUL-terminated string of text encoded in UTF-8, or ‘#f

Function: cairo-show-glyphs (cr <cairo-t>) (glyphs <cairo-glyph-t>) (num-glyphs <int>)

A drawing operator that generates the shape from an array of glyphs, rendered according to the current font face, font size (font matrix), and font options.

cr

a cairo context

glyphs

array of glyphs to show

num-glyphs

number of glyphs to show

Function: cairo-show-text-glyphs (cr <cairo-t>) (utf8 <char>) (utf8-len <int>) (glyphs <cairo-glyph-t>) (num-glyphs <int>) (clusters <cairo-text-cluster-t>) (num-clusters <int>) (cluster-flags <cairo-text-cluster-flags-t>)

This operation has rendering effects similar to cairo-show-glyphs but, if the target surface supports it, uses the provided text and cluster mapping to embed the text for the glyphs shown in the output. If the target does not support the extended attributes, this function acts like the basic cairo-show-glyphs as if it had been passed glyphs and num-glyphs.

The mapping between utf8 and glyphs is provided by an array of clusters. Each cluster covers a number of text bytes and glyphs, and neighboring clusters cover neighboring areas of utf8 and glyphs. The clusters should collectively cover utf8 and glyphs in entirety.

The first cluster always covers bytes from the beginning of utf8. If cluster-flags do not have the ‘CAIRO_TEXT_CLUSTER_FLAG_BACKWARD’ set, the first cluster also covers the beginning of glyphs, otherwise it covers the end of the glyphs array and following clusters move backward.

See <cairo-text-cluster-t> for constraints on valid clusters.

cr

a cairo context

utf8

a string of text encoded in UTF-8

utf8-len

length of utf8 in bytes, or -1 if it is NUL-terminated

glyphs

array of glyphs to show

num-glyphs

number of glyphs to show

clusters

array of cluster mapping information

num-clusters

number of clusters in the mapping

cluster-flags

cluster mapping flags

Since 1.8

Function: cairo-font-extents (cr <cairo-t>) (extents <cairo-font-extents-t>)

Gets the font extents for the currently selected font.

cr

a <cairo-t>

extents

a <cairo-font-extents-t> object into which the results will be stored.

Function: cairo-text-extents (cr <cairo-t>) (utf8 <char>) (extents <cairo-text-extents-t>)

Gets the extents for a string of text. The extents describe a user-space rectangle that encloses the "inked" portion of the text, (as it would be drawn by cairo-show-text). Additionally, the x_advance and y_advance values indicate the amount by which the current point would be advanced by cairo-show-text.

Note that whitespace characters do not directly contribute to the size of the rectangle (extents.width and extents.height). They do contribute indirectly by changing the position of non-whitespace characters. In particular, trailing whitespace characters are likely to not affect the size of the rectangle, though they will affect the x_advance and y_advance values.

cr

a <cairo-t>

utf8

a NUL-terminated string of text encoded in UTF-8, or ‘#f

extents

a <cairo-text-extents-t> object into which the results will be stored

Function: cairo-glyph-extents (cr <cairo-t>) (glyphs <cairo-glyph-t>) (num-glyphs <int>) (extents <cairo-text-extents-t>)

Gets the extents for an array of glyphs. The extents describe a user-space rectangle that encloses the "inked" portion of the glyphs, (as they would be drawn by cairo-show-glyphs). Additionally, the x_advance and y_advance values indicate the amount by which the current point would be advanced by cairo-show-glyphs.

Note that whitespace glyphs do not contribute to the size of the rectangle (extents.width and extents.height).

cr

a <cairo-t>

glyphs

an array of <cairo-glyph-t> objects

num-glyphs

the number of elements in glyphs

extents

a <cairo-text-extents-t> object into which the results will be stored

Function: cairo-toy-font-face-create (family <char>) (slant <cairo-font-slant-t>) (weight <cairo-font-weight-t>) ⇒  (ret <cairo-font-face-t >)

Creates a font face from a triplet of family, slant, and weight. These font faces are used in implementation of the the <cairo-t> "toy" font API.

If family is the zero-length string "", the platform-specific default family is assumed. The default family then can be queried using cairo-toy-font-face-get-family.

The cairo-select-font-face function uses this to create font faces. See that function for limitations and other details of toy font faces.

family

a font family name, encoded in UTF-8

slant

the slant for the font

weight

the weight for the font

ret

a newly created <cairo-font-face-t>. Free with cairo-font-face-destroy when you are done using it.

Since 1.8

Function: cairo-toy-font-face-get-family (font-face <cairo-font-face-t>) ⇒  (ret <char >)

Gets the familly name of a toy font.

font-face

A toy font face

ret

The family name. This string is owned by the font face and remains valid as long as the font face is alive (referenced).

Since 1.8

Function: cairo-toy-font-face-get-slant (font-face <cairo-font-face-t>) ⇒  (ret <cairo-font-slant-t>)

Gets the slant a toy font.

font-face

A toy font face

ret

The slant value

Since 1.8

Function: cairo-toy-font-face-get-weight (font-face <cairo-font-face-t>) ⇒  (ret <cairo-font-weight-t>)

Gets the weight a toy font.

font-face

A toy font face

ret

The weight value

Since 1.8


Next: , Previous: , Up: Top   [Index]