Cairo: A Vector Graphics Library | ||||
---|---|---|---|---|
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);
Cairo has two sets of text rendering capabilities:
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 |
the offset in the X direction between the origin used for drawing or measuring the string and the origin of this glyph. |
double |
the offset in the Y direction between the origin used for drawing or measuring the string and the origin of this glyph. |
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.
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.
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
.
|
a cairo_t |
|
a font family name, encoded in UTF-8 |
|
the slant for the font |
|
the weight for the font |
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.
|
a cairo_t |
|
the new font size, in user space units |
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
|
a cairo_t |
|
a cairo_matrix_t describing a transform to be applied to the current font. |
void cairo_get_font_matrix (cairo_t *cr, cairo_matrix_t *matrix);
Stores the current font matrix into matrix
. See
cairo_set_font_matrix()
.
|
a cairo_t |
|
return value for the matrix |
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.
|
a cairo_t |
|
font options to use |
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()
.
|
a cairo_t |
|
a cairo_font_options_t object into which to store the retrieved options. All existing values are overwritten |
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.
|
a cairo_t |
|
a cairo_font_face_t, or NULL to restore to the default font
|
cairo_font_face_t* cairo_get_font_face (cairo_t *cr);
Gets the current font face for a cairo_t.
|
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).
|
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()
.
|
a cairo_t |
|
a cairo_scaled_font_t |
Since 1.2
cairo_scaled_font_t* cairo_get_scaled_font (cairo_t *cr);
Gets the current scaled font for a cairo_t.
|
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
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.
|
a cairo context |
|
a string of text encoded in UTF-8 |
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.
|
a cairo context |
|
array of glyphs to show |
|
number of glyphs to show |
void cairo_font_extents (cairo_t *cr, cairo_font_extents_t *extents);
Gets the font extents for the currently selected font.
|
a cairo_t |
|
a cairo_font_extents_t object into which the results will be stored. |
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.
|
a cairo_t |
|
a string of text, encoded in UTF-8 |
|
a cairo_text_extents_t object into which the results will be stored |
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).
|
a cairo_t |
|
an array of cairo_glyph_t objects |
|
the number of elements in glyphs
|
|
a cairo_text_extents_t object into which the results will be stored |