[cairo] cairo-0.3: Bug in text extends

Anselm R. Garbe garbeam at gmail.com
Fri Feb 18 19:45:59 PST 2005


Hi there,

There seems to be a bug in the text extents of cairo-0.3:

I use follwoing function to determine the text-length for a given String:

---8<---
int text_width(char *font, double size, unsigned char *utf8txt)
{
    cairo_text_extents_t text;
    cairo_t *cr;

    if (!utf8txt) return 0;
    cr = cairo_create();

    if (!cr) {
        fatal("wmii: cannot create cairo context");
    }

    /* query font */
    cairo_select_font(cr, font, CAIRO_FONT_SLANT_NORMAL,
                            CAIRO_FONT_WEIGHT_NORMAL);
    cairo_scale_font(cr, size);

    /* query text extends */
    cairo_text_extents(cr, utf8txt, &text);
    cairo_destroy(cr);
    return (int)text.width;
}
---8<---

Well, it works perfect for most strings, but it returns wrong results
of strings which contain a whitespace at the end, e.g.:

---8<---
[garbeam at xxiii] ./textwidth 
usage: textwidth <font family> <font size> '<text>'
[garbeam at xxiii] ./textwidth monospace 12 'test'
26
[garbeam at xxiii] ./textwidth monospace 12 ' test'
34
[garbeam at xxiii] ./textwidth monospace 12 'test '
27 <-------!!!!!!!
[garbeam at xxiii] ./textwidth monospace 12 'test  '
34
---8<---

It seems that one whitespace in the end of any string is ignored.
The source of the textwidth tool can be found at 

http://wmi.modprobe.de/repos/wmii/tools/textwidth.c

(Note, that this text tool is a hybrid, because my window manager rendering
lib provides both, Xlib-only based and cairo-based rendering.)

Regards, 
-- 
  Anselm R. Garbe  ><><  www.ebrag.de  ><><  GPG key: 0D73F361



More information about the cairo mailing list