Skip to content

Instantly share code, notes, and snippets.

@bvssvni
Created March 19, 2014 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bvssvni/9651312 to your computer and use it in GitHub Desktop.
Save bvssvni/9651312 to your computer and use it in GitHub Desktop.
/**
* cairo_show_text:
* @cr: a cairo context
* @utf8: a NUL-terminated string of text encoded in UTF-8, or %NULL
*
* 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.
*
* Since: 1.0
**/
void
cairo_show_text (cairo_t *cr, const char *utf8)
{
cairo_text_extents_t extents;
cairo_status_t status;
cairo_glyph_t *glyphs, *last_glyph;
cairo_text_cluster_t *clusters;
int utf8_len, num_glyphs, num_clusters;
cairo_text_cluster_flags_t cluster_flags;
double x, y;
cairo_bool_t has_show_text_glyphs;
cairo_glyph_t stack_glyphs[CAIRO_STACK_ARRAY_LENGTH (cairo_glyph_t)];
cairo_text_cluster_t stack_clusters[CAIRO_STACK_ARRAY_LENGTH (cairo_text_cluster_t)];
cairo_scaled_font_t *scaled_font;
cairo_glyph_text_info_t info, *i;
if (unlikely (cr->status))
return;
if (utf8 == NULL)
return;
scaled_font = cairo_get_scaled_font (cr);
if (unlikely (scaled_font->status)) {
_cairo_set_error (cr, scaled_font->status);
return;
}
utf8_len = strlen (utf8);
has_show_text_glyphs =
cairo_surface_has_show_text_glyphs (cairo_get_target (cr));
glyphs = stack_glyphs;
num_glyphs = ARRAY_LENGTH (stack_glyphs);
if (has_show_text_glyphs) {
clusters = stack_clusters;
num_clusters = ARRAY_LENGTH (stack_clusters);
} else {
clusters = NULL;
num_clusters = 0;
}
cairo_get_current_point (cr, &x, &y);
status = cairo_scaled_font_text_to_glyphs (scaled_font,
x, y,
utf8, utf8_len,
&glyphs, &num_glyphs,
has_show_text_glyphs ? &clusters : NULL, &num_clusters,
&cluster_flags);
if (unlikely (status))
goto BAIL;
if (num_glyphs == 0)
return;
i = NULL;
if (has_show_text_glyphs) {
info.utf8 = utf8;
info.utf8_len = utf8_len;
info.clusters = clusters;
info.num_clusters = num_clusters;
info.cluster_flags = cluster_flags;
i = &info;
}
status = cr->backend->glyphs (cr, glyphs, num_glyphs, i);
if (unlikely (status))
goto BAIL;
last_glyph = &glyphs[num_glyphs - 1];
status = cr->backend->glyph_extents (cr, last_glyph, 1, &extents);
if (unlikely (status))
goto BAIL;
x = last_glyph->x + extents.x_advance;
y = last_glyph->y + extents.y_advance;
cr->backend->move_to (cr, x, y);
BAIL:
if (glyphs != stack_glyphs)
cairo_glyph_free (glyphs);
if (clusters != stack_clusters)
cairo_text_cluster_free (clusters);
if (unlikely (status))
_cairo_set_error (cr, status);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment