janssen@parc.xerox.com (Bill Janssen) (06/06/90)
I find myself using this code over and over, and think something like it might be nice to have in the fontdesc code. It takes a font, graphic, and string, and returns the width and height of that string in that font on that graphic, in pixels. Its return value is the width, in pixels. long fontdesc_StringBoundingBox (font, graphic, string, width, height) struct fontdesc *font; struct graphic *graphic; char *string; int *width; int *height; { int w, a, d, ascent, descent, junk; register short *fwt, *fht; register char *p; static struct fontdesc_charInfo ci; #define MAX(a,b) (((a)>(b))?(a):(b)) fwt = fontdesc_WidthTable (font, graphic); fht = fontdesc_HeightTable (font, graphic); if (fwt == NULL || fht == NULL) return 0; fontdesc_StringSize (font, graphic, string, &w, &junk); for (p = string, a = 0, d = 0; *p != (char) 0; p += 1) { fontdesc_CharSummary (font, graphic, *p, &ci); ascent = ci.yOriginOffset; descent = ci.height - ascent; a = MAX(a,ascent); d = MAX(d,descent); } *width = w; *height = a + d; return (w); } [An Andrew ToolKit view (a raster image) was included here, but could not be displayed.]