From 77a0ae7439bba5b442fc7c3bee5eb61ebfe24abb Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Wed, 24 Aug 2005 01:13:36 +0000 Subject: [PATCH] Fix cairo_show_text to advance the current point. Add documentation for cairo_show_text. Add test to verify that the current-point-advancing behavior of cairo_show_text is working. Remove bug about cairo_show_text not advancing the current point. --- BUGS | 4 -- ChangeLog | 15 ++++++ src/cairo.c | 54 ++++++++++++++++++--- test/.cvsignore | 1 + test/Makefile.am | 3 ++ test/show-text-current-point-ref.png | Bin 0 -> 1606 bytes test/show-text-current-point.c | 68 +++++++++++++++++++++++++++ 7 files changed, 134 insertions(+), 11 deletions(-) create mode 100644 test/show-text-current-point-ref.png create mode 100644 test/show-text-current-point.c diff --git a/BUGS b/BUGS index 20281bf46..bc15c249c 100644 --- a/BUGS +++ b/BUGS @@ -19,7 +19,3 @@ Recent improvements to make the intersection code more robust (using 128-bit arithmetic where needed), have exposed some of the weakness in the current tessellation implementation. So, for now, filling some polygons will cause "leaking" until we implement Hobby's algorithm. - --- - -cairo_show_text is not updating the current point by the string's advance values. diff --git a/ChangeLog b/ChangeLog index 3da86c4ee..2ba6c085e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2005-08-24 Carl Worth + + * src/cairo.c: (cairo_show_text): Fix cairo_show_text to advance + the current point. Add documentation for cairo_show_text. + + * test/.cvsignore: + * test/Makefile.am: + * test/show-text-current-point-ref.png: + * test/show-text-current-point.c: (draw), (main): Add test to + verify that the current-point-advancing behavior of + cairo_show_text is working. + + * BUGS: Remove bug about cairo_show_text not advancing the current + point. + 2005-08-24 Carl Worth * src/cairo.c: diff --git a/src/cairo.c b/src/cairo.c index de1a9839c..f32a68d3f 100644 --- a/src/cairo.c +++ b/src/cairo.c @@ -359,7 +359,7 @@ cairo_pop_group (cairo_t *cr) * * Sets the compositing operator to be used for all drawing * operations. See #cairo_operator_t for details on the semantics of - * each available drawing operator. + * each available compositing operator. * * XXX: I'd also like to direct the reader's attention to some * (not-yet-written) section on cairo's imaging model. How would I do @@ -2074,10 +2074,38 @@ cairo_glyph_extents (cairo_t *cr, _cairo_set_error (cr, cr->status); } +/** + * cairo_show_text: + * @cr: a cairo context + * @utf8: a string of text encoded in utf-8 + * + * 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 th 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 the + * most serious of text-using applications. See cairo_show_glyphs() + * for the "real" text display API in cairo. + **/ void cairo_show_text (cairo_t *cr, const char *utf8) { - cairo_glyph_t *glyphs = NULL; + cairo_text_extents_t extents; + cairo_glyph_t *glyphs = NULL, *last_glyph; int num_glyphs; double x, y; @@ -2092,16 +2120,28 @@ cairo_show_text (cairo_t *cr, const char *utf8) cr->status = _cairo_gstate_text_to_glyphs (cr->gstate, utf8, x, y, &glyphs, &num_glyphs); + if (cr->status) + goto BAIL; - if (cr->status) { - if (glyphs) - free (glyphs); - _cairo_set_error (cr, cr->status); + if (num_glyphs == 0) return; - } cr->status = _cairo_gstate_show_glyphs (cr->gstate, glyphs, num_glyphs); + if (cr->status) + goto BAIL; + last_glyph = &glyphs[num_glyphs - 1]; + cr->status = _cairo_gstate_glyph_extents (cr->gstate, + last_glyph, 1, + &extents); + if (cr->status) + goto BAIL; + + x = last_glyph->x + extents.x_advance; + y = last_glyph->y + extents.y_advance; + cairo_move_to (cr, x, y); + + BAIL: if (glyphs) free (glyphs); diff --git a/test/.cvsignore b/test/.cvsignore index 07eef00c4..1b255b35c 100644 --- a/test/.cvsignore +++ b/test/.cvsignore @@ -48,6 +48,7 @@ select-font-no-show-text self-copy self-intersecting set-source +show-text-current-point source-clip source-surface-scale-paint surface-finish-twice diff --git a/test/Makefile.am b/test/Makefile.am index bc75894e2..1cf987969 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -36,6 +36,7 @@ select-font-no-show-text \ self-copy \ self-intersecting \ set-source \ +show-text-current-point \ source-clip \ source-surface-scale-paint \ surface-finish-twice \ @@ -109,6 +110,7 @@ self-copy-ref.png \ self-intersecting-ref.png \ scale-source-surface-paint-ref.png \ set-source-ref.png \ +show-text-current-point-ref.png \ source-clip-ref.png \ source-surface-scale-paint-ref.png \ surface-pattern-ref.png \ @@ -220,6 +222,7 @@ select_font_no_show_text_LDADD = $(LDADDS) self_copy_LDADD = $(LDADDS) self_intersecting_LDADD = $(LDADDS) set_source_LDADD = $(LDADDS) +show_text_current_point_LDADD = $(LDADDS) source_clip_LDADD = $(LDADDS) source_surface_scale_paint_LDADD = $(LDADDS) surface_finish_twice_LDADD = $(LDADDS) diff --git a/test/show-text-current-point-ref.png b/test/show-text-current-point-ref.png new file mode 100644 index 0000000000000000000000000000000000000000..de2a13866d0d94d4f6fcc41314e6f455b0cbc285 GIT binary patch literal 1606 zcmV-M2D$l(P)$jvda#K>@x!y5ci-*o?rhI{dMfTedGpT9&OFcapLu5Hnb~E)fB^#r z3>YwAf8!-slSuc)8XSW|Ct=hC@KQ`Y4|EJzSuABvtT?`bGizn1GPv^fMf;5K{>uWqRKQJjpkuoXvP25-QZ@!br0GdKZX!FFsGq1^S@flpvngZ^n8 zhN}zxr&OLDS=g`x7uDMJ>65dt4Y%VK9G9u1iSwyAzf$iSoLk9%5-&^GF)hutiOaG0 zJYHDjSK(85bBnAxrV82H@qJuUdGiOSPr% z_%-=nE^l-AOYuKf$(M`6@p#5)_aGLNZzaf=>5XE79#U!ZJ$$agh88?Jh;xz0MleUgF9c`Rf<7wf5XNn29OGF{> z!959GCF)$exU9t=#p3;#@ddss#C;9^Db1a{#bJwa)XTc;Z|h_7!t@2>NAL6S>muFd zoAbsOho#!mXZ+fHuT|ITO8PpZ5RJHV_;iteEe;Zwg{z9+THN14;1yzLd!|x$p9lwY zQH@-3-?`L8ho@!~+h1A_wwJc{Wn)*0}raqgi z)cLLGcl!H!A6s(J#=pz=8nL>oLV?|QRKJU}-A3oE66=fdTs+rP-gC9*^?cKvL#l2Q zN*R6nqopG{~y!+knFxh8PFXdIU6>ut8A(0BZr ze6Lkk?uFz%V$iCWFjHF2DdPOU&ggRKa1o8%nW=NR<<;>tj_y&vPoEqmqLxMcSe#E& zcQ18xah?^6!I2{DpA{kNv0}F%7L4B%zw(Zv&b5onO=9Oi9e)s^aEZFCy1t#_Vz?@M zeI@;n(dF3@c(?x87yqoHi$jU(<>4h_ESaot=J;Ws@oV$Fl&*8}7V;N|J>ndDqX?~b zigVN%Eo~MHV{gST@PN3m*`BF$x#iXI1$-Hz(@V(ZzYr z=y*Xm^}}N4YYQ9eT-&&uExtQBUqnC`i#v`j4YKOkBQC=}D?;&KiO{yhjrgkQmphCW zwLaJv|EwZ+>~{+e_v7{AD9&VkGsl)b@@yHV!(g_1OET;UkITHoPx#07*qoM6N<$ Ef{Jq~_5c6? literal 0 HcmV?d00001 diff --git a/test/show-text-current-point.c b/test/show-text-current-point.c new file mode 100644 index 000000000..84fe597ee --- /dev/null +++ b/test/show-text-current-point.c @@ -0,0 +1,68 @@ +/* + * Copyright © 2005 Red Hat, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software + * and its documentation for any purpose is hereby granted without + * fee, provided that the above copyright notice appear in all copies + * and that both that copyright notice and this permission notice + * appear in supporting documentation, and that the name of + * Red Hat, Inc. not be used in advertising or publicity pertaining to + * distribution of the software without specific, written prior + * permission. Red Hat, Inc. makes no representations about the + * suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL, + * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR + * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Author: Carl D. Worth + */ + +#include "cairo-test.h" + +#define TEXT_SIZE 12 + +cairo_test_t test = { + "show-text-current-point", + "Test that cairo_show_text adjusts the current point properly", + 263, TEXT_SIZE + 4 +}; + +static cairo_test_status_t +draw (cairo_t *cr, int width, int height) +{ + cairo_font_options_t *font_options; + + cairo_select_font_face (cr, "Bitstream Vera Sans", + CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_NORMAL); + cairo_set_font_size (cr, TEXT_SIZE); + + font_options = cairo_font_options_create (); + + cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE); + cairo_font_options_set_antialias (font_options, CAIRO_ANTIALIAS_GRAY); + + cairo_set_font_options (cr, font_options); + cairo_font_options_destroy (font_options); + + cairo_set_source_rgb (cr, 0, 0, 0); /* black */ + + cairo_move_to (cr, 0, TEXT_SIZE); + cairo_show_text (cr, "Hello from the "); + cairo_show_text (cr, test.name); + cairo_show_text (cr, " test."); + + return CAIRO_TEST_SUCCESS; +} + +int +main (void) +{ + return cairo_test (&test, draw); +}