## Text Text — Rendering text and glyphs |

## Synopsis

                        cairo_glyph_t;
    enum                cairo_font_slant_t;
    enum                cairo_font_weight_t;
    void                cairo_select_font_face              (cairo_t *cr,
                                                             const char *family,
                                                             cairo_font_slant_t slant,
                                                             cairo_font_weight_t weight);
    void                cairo_set_font_size                 (cairo_t *cr,
                                                             double size);
    void                cairo_set_font_matrix               (cairo_t *cr,
                                                             const cairo_matrix_t *matrix);
    void                cairo_get_font_matrix               (cairo_t *cr,
                                                             cairo_matrix_t *matrix);
    void                cairo_set_font_options              (cairo_t *cr,
                                                             const cairo_font_options_t *options);
    void                cairo_get_font_options              (cairo_t *cr,
                                                             cairo_font_options_t *options);
    void                cairo_set_font_face                 (cairo_t *cr,
                                                             cairo_font_face_t *font_face);
    cairo_font_face_t*  cairo_get_font_face                 (cairo_t *cr);
    void                cairo_set_scaled_font               (cairo_t *cr,
                                                             const cairo_scaled_font_t *scaled_font);
    cairo_scaled_font_t* cairo_get_scaled_font              (cairo_t *cr);
    void                cairo_show_text                     (cairo_t *cr,
                                                             const char *utf8);
    void                cairo_show_glyphs                   (cairo_t *cr,
                                                             const cairo_glyph_t *glyphs,
                                                             int num_glyphs);
    void                cairo_font_extents                  (cairo_t *cr,
                                                             cairo_font_extents_t *extents);
    void                cairo_text_extents                  (cairo_t *cr,
                                                             const char *utf8,
                                                             cairo_text_extents_t *extents);
    void                cairo_glyph_extents                 (cairo_t *cr,
                                                             const cairo_glyph_t *glyphs,
                                                             int num_glyphs,
                                                             cairo_text_extents_t *extents);

## Description

Cairo has two sets of text rendering capabilities:

- 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/.

## Details

### cairo_glyph_t

    typedef struct {
      unsigned long        index;
      double               x;
      double               y;
    } cairo_glyph_t;

The cairo_glyph_t structure holds information about a single glyph when drawing or measuring text. A font is (in simple terms) a collection of shapes used to draw text. A glyph is one of these shapes. There can be multiple glyphs for a single character (alternates to be used in different contexts, for example), or a glyph can be a ligature of multiple characters. Cairo doesn't expose any way of converting input text into glyphs, so in order to use the Cairo interfaces that take arrays of glyphs, you must directly access the appropriate underlying font system.

Note that the offsets given by `x` and `y` are not cumulative. When drawing or measuring text, each glyph is individually positioned with respect to the overall origin

double `x`; | the offset in the X direction between the origin used for drawing or measuring the string and the origin of this glyph.
double `y`; | the offset in the Y direction between the origin used for drawing or measuring the string and the origin of this glyph.

### enum cairo_font_slant_t

    typedef enum _cairo_font_slant {
      CAIRO_FONT_SLANT_NORMAL,
      CAIRO_FONT_SLANT_ITALIC,
      CAIRO_FONT_SLANT_OBLIQUE
    } cairo_font_slant_t;

Specifies variants of a font face based on their slant.

`CAIRO_FONT_SLANT_NORMAL` | Upright font style
`CAIRO_FONT_SLANT_ITALIC` | Italic font style
`CAIRO_FONT_SLANT_OBLIQUE` | Oblique font style

### enum cairo_font_weight_t

    typedef enum _cairo_font_weight {
      CAIRO_FONT_WEIGHT_NORMAL,
      CAIRO_FONT_WEIGHT_BOLD
    } cairo_font_weight_t;

Specifies variants of a font face based on their weight.

`CAIRO_FONT_WEIGHT_NORMAL` | Normal font weight
`CAIRO_FONT_WEIGHT_BOLD` | Bold font weight

### cairo_select_font_face ()

    void                cairo_select_font_face              (cairo_t *cr,
                                                             const char *family,
                                                             cairo_font_slant_t slant,
                                                             cairo_font_weight_t weight);

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.

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 "sans", slant is `CAIRO_FONT_SLANT_NORMAL`, and weight is `CAIRO_FONT_WEIGHT_NORMAL`.

`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

### cairo_set_font_size ()

    void                cairo_set_font_size                 (cairo_t *cr,
                                                             double size);

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

### cairo_set_font_matrix ()

    void                cairo_set_font_matrix               (cairo_t *cr,
                                                             const cairo_matrix_t *matrix);

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.

### cairo_get_font_matrix ()

    void                cairo_get_font_matrix               (cairo_t *cr,
                                                             cairo_matrix_t *matrix);

Stores the current font matrix into `matrix`. See `cairo_set_font_matrix()`.

`cr` : | a cairo_t
`matrix` : | return value for the matrix

### cairo_set_font_options ()

    void                cairo_set_font_options              (cairo_t *cr,
                                                             const cairo_font_options_t *options);

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

### cairo_get_font_options ()

    void                cairo_get_font_options              (cairo_t *cr,
                                                             cairo_font_options_t *options);

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

### cairo_set_font_face ()

    void                cairo_set_font_face                 (cairo_t *cr,
                                                             cairo_font_face_t *font_face);

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 `NULL` to restore to the default font

### cairo_get_font_face ()

    cairo_font_face_t*  cairo_get_font_face                 (cairo_t *cr);

Gets the current font face for a cairo_t.

`cr` : | a cairo_t
Returns : | 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 `NULL`. 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).

### cairo_set_scaled_font ()

    void                cairo_set_scaled_font               (cairo_t *cr,
                                                             const cairo_scaled_font_t *scaled_font);

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

### cairo_get_scaled_font ()

    cairo_scaled_font_t* cairo_get_scaled_font              (cairo_t *cr);

Gets the current scaled font for a cairo_t.

`cr` : | a cairo_t
Returns : | 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 `NULL`. 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

### cairo_show_text ()

    void                cairo_show_text                     (cairo_t *cr,
                                                             const char *utf8);

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 string of text encoded in UTF-8

### cairo_show_glyphs ()

    void                cairo_show_glyphs                   (cairo_t *cr,
                                                             const cairo_glyph_t *glyphs,
                                                             int num_glyphs);

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

### cairo_font_extents ()

    void                cairo_font_extents                  (cairo_t *cr,
                                                             cairo_font_extents_t *extents);

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.

### cairo_text_extents ()

    void                cairo_text_extents                  (cairo_t *cr,
                                                             const char *utf8,
                                                             cairo_text_extents_t *extents);

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 string of text, encoded in UTF-8
`extents` : | a cairo_text_extents_t object into which the results will be stored

### cairo_glyph_extents ()

    void                cairo_glyph_extents                 (cairo_t *cr,
                                                             const cairo_glyph_t *glyphs,
                                                             int num_glyphs,
                                                             cairo_text_extents_t *extents);

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

## See Also

- cairo_font_face_t
- cairo_scaled_font_t
- `cairo_text_path()`
- `cairo_glyph_path()`