[2537] in Athena Bugs

home help back first fref pref prev next nref lref last post

XTextWidth

daemon@ATHENA.MIT.EDU (hkbirke@ATHENA.MIT.EDU)
Sun Jul 9 01:50:19 1989

From: <hkbirke@ATHENA.MIT.EDU>
Date: Sun, 9 Jul 89 01:49:57 -0400
To: bugs@ATHENA.MIT.EDU
Cc: hkbirke@movies, probe@ATHENA.MIT.EDU

The X11R3 implementation of XTextWidth seems quite inefficient. In fact,
one can write a must smaller and substantiall;y faster routine to calculate
the width of a string:

int TextWidth (font, chars, len)
     XFontStruct *font;
     char *chars;
     int len;

{
  register int width = 0, ch; 
  int i;

  for (i = 0; i < len; i++) {
    ch = chars[i] - font->min_char_or_byte2;
    if (ch < 0 || ch > font->max_char_or_byte2) continue;
    if (font->per_char == NULL) width += font->min_bounds.width;
    else width += font->per_char[ch].width;
  }
  return (width);
}

this routine should work for all 8bit fonts and can easily be extended to
16 bit fonts (and XTextExtents). to extend to 16bit, simply add
something like 

if (font->max_byte1 == 0) 

before the for loop and then replicate the entire loop in the else clause
modified to look up the value in the 16bit font. Sorry, I haven't done this
yet.

to be really fast, use registers and cache min_char_or_byte2, max_char_or_byte2
and min_bounds.width in local variables (to save repeated memory indirections)

much obliged,
--hal

home help back first fref pref prev next nref lref last post