/*1* tkFont.c --2*3* This file maintains a database of looked-up fonts for the Tk4* toolkit, in order to avoid round-trips to the server to map5* font names to XFontStructs. It also provides several utility6* procedures for measuring and displaying text.7*8* Copyright (c) 1990-1994 The Regents of the University of California.9* Copyright (c) 1994-1995 Sun Microsystems, Inc.10*11* See the file "license.terms" for information on usage and redistribution12* of this file, and for a DISCLAIMER OF ALL WARRANTIES.13*14* SCCS: @(#) tkFont.c 1.38 96/02/15 18:53:3115*/1617#include "tkInt.h"1819/*20* This module caches extra information about fonts in addition to21* what X already provides. The extra information is used by the22* TkMeasureChars procedure, and consists of two parts: a type and23* a width. The type is one of the following:24*25* NORMAL: Standard character.26* TAB: Tab character: output enough space to27* get to next tab stop.28* NEWLINE: Newline character: don't output anything more29* on this line (character has infinite width).30* REPLACE: This character doesn't print: instead of31* displaying character, display a replacement32* sequence like "\n" (for those characters where33* ANSI C defines such a sequence) or a sequence34* of the form "\xdd" where dd is the hex equivalent35* of the character.36* SKIP: Don't display anything for this character. This37* is only used where the font doesn't contain38* all the characters needed to generate39* replacement sequences.40* The width gives the total width of the displayed character or41* sequence: for replacement sequences, it gives the width of the42* sequence.43*/4445#define NORMAL 146#define TAB 247#define NEWLINE 348#define REPLACE 449#define SKIP 55051/*52* One of the following data structures exists for each font that is53* currently active. The structure is indexed with two hash tables,54* one based on font name and one based on XFontStruct address.55*/5657typedef struct {58XFontStruct *fontStructPtr; /* X information about font. */59Display *display; /* Display to which font belongs. */60int refCount; /* Number of active uses of this font. */61char *types; /* Malloc'ed array giving types of all62* chars in the font (may be NULL). */63unsigned char *widths; /* Malloc'ed array giving widths of all64* chars in the font (may be NULL). */65int tabWidth; /* Width of tabs in this font. */66Tcl_HashEntry *nameHashPtr; /* Entry in name-based hash table (needed67* when deleting this structure). */68} TkFont;6970/*71* Hash table for name -> TkFont mapping, and key structure used to72* index into that table:73*/7475static Tcl_HashTable nameTable;76typedef struct {77Tk_Uid name; /* Name of font. */78Display *display; /* Display for which font is valid. */79} NameKey;8081/*82* Hash table for font struct -> TkFont mapping. This table is83* indexed by the XFontStruct address.84*/8586static Tcl_HashTable fontTable;8788static int initialized = 0; /* 0 means static structures haven't been89* initialized yet. */9091/*92* To speed up TkMeasureChars, the variables below keep the last93* mapping from (XFontStruct *) to (TkFont *).94*/9596static TkFont *lastFontPtr = NULL;97static XFontStruct *lastFontStructPtr = NULL;9899/*100* Characters used when displaying control sequences.101*/102103static char hexChars[] = "0123456789abcdefxtnvr\\";104105/*106* The following table maps some control characters to sequences107* like '\n' rather than '\x10'. A zero entry in the table means108* no such mapping exists, and the table only maps characters109* less than 0x10.110*/111112static char mapChars[] = {1130, 0, 0, 0, 0, 0, 0,114'a', 'b', 't', 'n', 'v', 'f', 'r',1150116};117118/*119* Forward declarations for procedures defined in this file:120*/121122static void FontInit _ANSI_ARGS_((void));123static void SetFontMetrics _ANSI_ARGS_((TkFont *fontPtr));124125/*126*----------------------------------------------------------------------127*128* Tk_GetFontStruct --129*130* Given a string name for a font, map the name to an XFontStruct131* describing the font.132*133* Results:134* The return value is normally a pointer to the font description135* for the desired font. If an error occurs in mapping the string136* to a font, then an error message will be left in interp->result137* and NULL will be returned.138*139* Side effects:140* The font is added to an internal database with a reference count.141* For each call to this procedure, there should eventually be a call142* to Tk_FreeFontStruct, so that the database is cleaned up when fonts143* aren't in use anymore.144*145*----------------------------------------------------------------------146*/147148XFontStruct *149Tk_GetFontStruct(interp, tkwin, name)150Tcl_Interp *interp; /* Place to leave error message if151* font can't be found. */152Tk_Window tkwin; /* Window in which font will be used. */153Tk_Uid name; /* Name of font (in form suitable for154* passing to XLoadQueryFont). */155{156NameKey nameKey;157Tcl_HashEntry *nameHashPtr, *fontHashPtr;158int new;159register TkFont *fontPtr;160XFontStruct *fontStructPtr;161162if (!initialized) {163FontInit();164}165166/*167* First, check to see if there's already a mapping for this font168* name.169*/170171nameKey.name = name;172nameKey.display = Tk_Display(tkwin);173nameHashPtr = Tcl_CreateHashEntry(&nameTable, (char *) &nameKey, &new);174if (!new) {175fontPtr = (TkFont *) Tcl_GetHashValue(nameHashPtr);176fontPtr->refCount++;177return fontPtr->fontStructPtr;178}179180/*181* The name isn't currently known. Map from the name to a font, and182* add a new structure to the database.183*/184185fontStructPtr = XLoadQueryFont(nameKey.display, name);186if (fontStructPtr == NULL) {187Tcl_DeleteHashEntry(nameHashPtr);188Tcl_AppendResult(interp, "font \"", name, "\" doesn't exist",189(char *) NULL);190return NULL;191}192fontPtr = (TkFont *) ckalloc(sizeof(TkFont));193fontPtr->display = nameKey.display;194fontPtr->fontStructPtr = fontStructPtr;195fontPtr->refCount = 1;196fontPtr->types = NULL;197fontPtr->widths = NULL;198fontPtr->nameHashPtr = nameHashPtr;199fontHashPtr = Tcl_CreateHashEntry(&fontTable, (char *) fontStructPtr, &new);200if (!new) {201panic("XFontStruct already registered in Tk_GetFontStruct");202}203Tcl_SetHashValue(nameHashPtr, fontPtr);204Tcl_SetHashValue(fontHashPtr, fontPtr);205return fontPtr->fontStructPtr;206}207208/*209*--------------------------------------------------------------210*211* Tk_NameOfFontStruct --212*213* Given a font, return a textual string identifying it.214*215* Results:216* If font was created by Tk_GetFontStruct, then the return217* value is the "string" that was used to create it.218* Otherwise the return value is a string giving the X219* identifier for the font. The storage for the returned220* string is only guaranteed to persist up until the next221* call to this procedure.222*223* Side effects:224* None.225*226*--------------------------------------------------------------227*/228229char *230Tk_NameOfFontStruct(fontStructPtr)231XFontStruct *fontStructPtr; /* Font whose name is desired. */232{233Tcl_HashEntry *fontHashPtr;234TkFont *fontPtr;235void *ptr;236static char string[20];237238if (!initialized) {239printid:240sprintf(string, "font id 0x%x", (unsigned int) fontStructPtr->fid);241return string;242}243fontHashPtr = Tcl_FindHashEntry(&fontTable, (char *) fontStructPtr);244if (fontHashPtr == NULL) {245goto printid;246}247fontPtr = (TkFont *) Tcl_GetHashValue(fontHashPtr);248ptr = fontPtr->nameHashPtr->key.words;249return ((NameKey *) ptr)->name;250}251252/*253*----------------------------------------------------------------------254*255* Tk_FreeFontStruct --256*257* This procedure is called to release a font allocated by258* Tk_GetFontStruct.259*260* Results:261* None.262*263* Side effects:264* The reference count associated with font is decremented, and265* the font is officially deallocated if no-one is using it266* anymore.267*268*----------------------------------------------------------------------269*/270271void272Tk_FreeFontStruct(fontStructPtr)273XFontStruct *fontStructPtr; /* Font to be released. */274{275Tcl_HashEntry *fontHashPtr;276register TkFont *fontPtr;277278if (!initialized) {279panic("Tk_FreeFontStruct called before Tk_GetFontStruct");280}281282fontHashPtr = Tcl_FindHashEntry(&fontTable, (char *) fontStructPtr);283if (fontHashPtr == NULL) {284panic("Tk_FreeFontStruct received unknown font argument");285}286fontPtr = (TkFont *) Tcl_GetHashValue(fontHashPtr);287fontPtr->refCount--;288if (fontPtr->refCount == 0) {289/*290* We really should call Tk_FreeXId below to release the font's291* resource identifier, but this seems to cause problems on292* many X servers (as of 5/1/94) where the font resource isn't293* really released, which can cause the wrong font to be used294* later on. So, don't release the resource id after all, even295* though this results in an id leak.296*297* Tk_FreeXId(fontPtr->display, (XID) fontPtr->fontStructPtr->fid);298*/299300XFreeFont(fontPtr->display, fontPtr->fontStructPtr);301Tcl_DeleteHashEntry(fontPtr->nameHashPtr);302Tcl_DeleteHashEntry(fontHashPtr);303if (fontPtr->types != NULL) {304ckfree(fontPtr->types);305}306if (fontPtr->widths != NULL) {307ckfree((char *) fontPtr->widths);308}309ckfree((char *) fontPtr);310lastFontStructPtr = NULL;311}312}313314/*315*----------------------------------------------------------------------316*317* FontInit --318*319* Initialize the structure used for font management.320*321* Results:322* None.323*324* Side effects:325* Read the code.326*327*----------------------------------------------------------------------328*/329330static void331FontInit()332{333initialized = 1;334Tcl_InitHashTable(&nameTable, sizeof(NameKey)/sizeof(int));335Tcl_InitHashTable(&fontTable, TCL_ONE_WORD_KEYS);336}337338/*339*--------------------------------------------------------------340*341* SetFontMetrics --342*343* This procedure is called to fill in the "widths" and "types"344* arrays for a font.345*346* Results:347* None.348*349* Side effects:350* FontPtr gets modified to hold font metric information.351*352*--------------------------------------------------------------353*/354355static void356SetFontMetrics(fontPtr)357register TkFont *fontPtr; /* Font structure in which to358* set metrics. */359{360int i, replaceOK;361register XFontStruct *fontStructPtr = fontPtr->fontStructPtr;362char *p;363364/*365* Pass 1: initialize the arrays.366*/367368fontPtr->types = (char *) ckalloc(256);369fontPtr->widths = (unsigned char *) ckalloc(256);370for (i = 0; i < 256; i++) {371fontPtr->types[i] = REPLACE;372}373374/*375* Pass 2: for all characters that exist in the font and are376* not control characters, fill in the type and width377* information.378*/379380for (i = 0; i < 256; i++) {381if ((i == 0177) || (i < fontStructPtr->min_char_or_byte2)382|| (i > fontStructPtr->max_char_or_byte2)) {383continue;384}385fontPtr->types[i] = NORMAL;386if (fontStructPtr->per_char == NULL) {387fontPtr->widths[i] = fontStructPtr->min_bounds.width;388} else {389fontPtr->widths[i] = fontStructPtr->per_char[i390- fontStructPtr->min_char_or_byte2].width;391}392}393394/*395* Pass 3: fill in information for characters that have to396* be replaced with "\xhh" or "\n" strings. If the font doesn't397* have the characters needed for this, then just use the398* font's default character.399*/400401replaceOK = 1;402for (p = hexChars; *p != 0; p++) {403if (fontPtr->types[*p] != NORMAL) {404replaceOK = 0;405break;406}407}408for (i = 0; i < 256; i++) {409if (fontPtr->types[i] != REPLACE) {410continue;411}412if (replaceOK) {413if ((i < sizeof(mapChars)) && (mapChars[i] != 0)) {414fontPtr->widths[i] = fontPtr->widths['\\']415+ fontPtr->widths[mapChars[i]];416} else {417fontPtr->widths[i] = fontPtr->widths['\\']418+ fontPtr->widths['x']419+ fontPtr->widths[hexChars[i & 0xf]]420+ fontPtr->widths[hexChars[(i>>4) & 0xf]];421}422} else {423fontPtr->types[i] = SKIP;424fontPtr->widths[i] = 0;425}426}427428/*429* Lastly, fill in special information for newline and tab.430*/431432fontPtr->types['\n'] = NEWLINE;433fontPtr->types['\t'] = TAB;434fontPtr->widths['\t'] = 0;435if (fontPtr->types['0'] == NORMAL) {436fontPtr->tabWidth = 8*fontPtr->widths['0'];437} else {438fontPtr->tabWidth = 8*fontStructPtr->max_bounds.width;439}440441/*442* Make sure the tab width isn't zero (some fonts may not have enough443* information to set a reasonable tab width).444*/445446if (fontPtr->tabWidth == 0) {447fontPtr->tabWidth = 1;448}449}450451/*452*--------------------------------------------------------------453*454* TkMeasureChars --455*456* Measure the number of characters from a string that457* will fit in a given horizontal span. The measurement458* is done under the assumption that TkDisplayChars will459* be used to actually display the characters.460*461* Results:462* The return value is the number of characters from source463* that fit in the span given by startX and maxX. *nextXPtr464* is filled in with the x-coordinate at which the first465* character that didn't fit would be drawn, if it were to466* be drawn.467*468* Side effects:469* None.470*471*--------------------------------------------------------------472*/473474int475TkMeasureChars(fontStructPtr, source, maxChars, startX, maxX,476tabOrigin, flags, nextXPtr)477XFontStruct *fontStructPtr; /* Font in which to draw characters. */478char *source; /* Characters to be displayed. Need not479* be NULL-terminated. */480int maxChars; /* Maximum # of characters to consider from481* source. */482int startX; /* X-position at which first character will483* be drawn. */484int maxX; /* Don't consider any character that would485* cross this x-position. */486int tabOrigin; /* X-location that serves as "origin" for487* tab stops. */488int flags; /* Various flag bits OR-ed together.489* TK_WHOLE_WORDS means stop on a word boundary490* (just before a space character) if491* possible. TK_AT_LEAST_ONE means always492* return a value of at least one, even493* if the character doesn't fit.494* TK_PARTIAL_OK means it's OK to display only495* a part of the last character in the line.496* TK_NEWLINES_NOT_SPECIAL means that newlines497* are treated just like other control chars:498* they don't terminate the line.499* TK_IGNORE_TABS means give all tabs zero500* width. */501int *nextXPtr; /* Return x-position of terminating502* character here. */503{504register TkFont *fontPtr;505register char *p; /* Current character. */506register int c;507char *term; /* Pointer to most recent character that508* may legally be a terminating character. */509int termX; /* X-position just after term. */510int curX; /* X-position corresponding to p. */511int newX; /* X-position corresponding to p+1. */512int type;513int rem;514515/*516* Find the TkFont structure for this font, and make sure its517* font metrics exist.518*/519520if (lastFontStructPtr == fontStructPtr) {521fontPtr = lastFontPtr;522} else {523Tcl_HashEntry *fontHashPtr;524525if (!initialized) {526badArg:527panic("TkMeasureChars received unknown font argument");528}529530fontHashPtr = Tcl_FindHashEntry(&fontTable, (char *) fontStructPtr);531if (fontHashPtr == NULL) {532goto badArg;533}534fontPtr = (TkFont *) Tcl_GetHashValue(fontHashPtr);535lastFontStructPtr = fontPtr->fontStructPtr;536lastFontPtr = fontPtr;537}538if (fontPtr->types == NULL) {539SetFontMetrics(fontPtr);540}541542/*543* Scan the input string one character at a time, until a character544* is found that crosses maxX.545*/546547newX = curX = startX;548termX = 0; /* Not needed, but eliminates compiler warning. */549term = source;550for (p = source, c = *p & 0xff; maxChars > 0; p++, maxChars--) {551type = fontPtr->types[c];552if ((type == NORMAL) || (type == REPLACE)) {553newX += fontPtr->widths[c];554} else if (type == TAB) {555if (!(flags & TK_IGNORE_TABS)) {556newX += fontPtr->tabWidth;557rem = (newX - tabOrigin) % fontPtr->tabWidth;558if (rem < 0) {559rem += fontPtr->tabWidth;560}561newX -= rem;562}563} else if (type == NEWLINE) {564if (flags & TK_NEWLINES_NOT_SPECIAL) {565newX += fontPtr->widths[c];566} else {567break;568}569} else if (type != SKIP) {570panic("Unknown type %d in TkMeasureChars", type);571}572if (newX > maxX) {573break;574}575if (maxChars > 1) {576c = p[1] & 0xff;577} else {578/*579* Can't look at next character: it could be in uninitialized580* memory.581*/582583c = 0;584}585if (isspace(UCHAR(c)) || (c == 0)) {586term = p+1;587termX = newX;588}589curX = newX;590}591592/*593* P points to the first character that doesn't fit in the desired594* span. Use the flags to figure out what to return.595*/596597if ((flags & TK_PARTIAL_OK) && (curX < maxX)) {598curX = newX;599p++;600}601if ((flags & TK_AT_LEAST_ONE) && (term == source) && (maxChars > 0)602&& !isspace(UCHAR(*term))) {603term = p;604termX = curX;605if (term == source) {606term++;607termX = newX;608}609} else if ((maxChars == 0) || !(flags & TK_WHOLE_WORDS)) {610term = p;611termX = curX;612}613*nextXPtr = termX;614return term-source;615}616617/*618*--------------------------------------------------------------619*620* TkDisplayChars --621*622* Draw a string of characters on the screen, converting623* tabs to the right number of spaces and control characters624* to sequences of the form "\xhh" where hh are two hex625* digits.626*627* Results:628* None.629*630* Side effects:631* Information gets drawn on the screen.632*633*--------------------------------------------------------------634*/635636void637TkDisplayChars(display, drawable, gc, fontStructPtr, string, numChars,638x, y, tabOrigin, flags)639Display *display; /* Display on which to draw. */640Drawable drawable; /* Window or pixmap in which to draw. */641GC gc; /* Graphics context for actually drawing642* characters. */643XFontStruct *fontStructPtr; /* Font used in GC; must have been allocated644* by Tk_GetFontStruct. Used to compute sizes645* of tabs, etc. */646char *string; /* Characters to be displayed. */647int numChars; /* Number of characters to display from648* string. */649int x, y; /* Coordinates at which to draw string. */650int tabOrigin; /* X-location that serves as "origin" for651* tab stops. */652int flags; /* Flags to control display. Only653* TK_NEWLINES_NOT_SPECIAL and TK_IGNORE_TABS654* are supported right now. See655* TkMeasureChars for information about it. */656{657register TkFont *fontPtr;658register char *p; /* Current character being scanned. */659register int c;660int type;661char *start; /* First character waiting to be displayed. */662int startX; /* X-coordinate corresponding to start. */663int curX; /* X-coordinate corresponding to p. */664char replace[10];665int rem;666667/*668* Find the TkFont structure for this font, and make sure its669* font metrics exist.670*/671672if (lastFontStructPtr == fontStructPtr) {673fontPtr = lastFontPtr;674} else {675Tcl_HashEntry *fontHashPtr;676677if (!initialized) {678badArg:679panic("TkDisplayChars received unknown font argument");680}681682fontHashPtr = Tcl_FindHashEntry(&fontTable, (char *) fontStructPtr);683if (fontHashPtr == NULL) {684goto badArg;685}686fontPtr = (TkFont *) Tcl_GetHashValue(fontHashPtr);687lastFontStructPtr = fontPtr->fontStructPtr;688lastFontPtr = fontPtr;689}690if (fontPtr->types == NULL) {691SetFontMetrics(fontPtr);692}693694/*695* Scan the string one character at a time. Display control696* characters immediately, but delay displaying normal characters697* in order to pass many characters to the server all together.698*/699700startX = curX = x;701start = string;702for (p = string; numChars > 0; numChars--, p++) {703c = *p & 0xff;704type = fontPtr->types[c];705if (type == NORMAL) {706curX += fontPtr->widths[c];707continue;708}709if (p != start) {710XDrawString(display, drawable, gc, startX, y, start, p - start);711startX = curX;712}713if (type == TAB) {714if (!(flags & TK_IGNORE_TABS)) {715curX += fontPtr->tabWidth;716rem = (curX - tabOrigin) % fontPtr->tabWidth;717if (rem < 0) {718rem += fontPtr->tabWidth;719}720curX -= rem;721}722} else if (type == REPLACE ||723(type == NEWLINE && flags & TK_NEWLINES_NOT_SPECIAL)) {724if ((c < sizeof(mapChars)) && (mapChars[c] != 0)) {725replace[0] = '\\';726replace[1] = mapChars[c];727XDrawString(display, drawable, gc, startX, y, replace, 2);728curX += fontPtr->widths[replace[0]]729+ fontPtr->widths[replace[1]];730} else {731replace[0] = '\\';732replace[1] = 'x';733replace[2] = hexChars[(c >> 4) & 0xf];734replace[3] = hexChars[c & 0xf];735XDrawString(display, drawable, gc, startX, y, replace, 4);736curX += fontPtr->widths[replace[0]]737+ fontPtr->widths[replace[1]]738+ fontPtr->widths[replace[2]]739+ fontPtr->widths[replace[3]];740}741} else if (type == NEWLINE) {742y += fontStructPtr->ascent + fontStructPtr->descent;743curX = x;744} else if (type != SKIP) {745panic("Unknown type %d in TkDisplayChars", type);746}747startX = curX;748start = p+1;749}750751/*752* At the very end, there may be one last batch of normal characters753* to display.754*/755756if (p != start) {757XDrawString(display, drawable, gc, startX, y, start, p - start);758}759}760761/*762*----------------------------------------------------------------------763*764* TkUnderlineChars --765*766* This procedure draws an underline for a given range of characters767* in a given string, using appropriate information for the string's768* font. It doesn't draw the characters (which are assumed to have769* been displayed previously); it just draws the underline.770*771* Results:772* None.773*774* Side effects:775* Information gets displayed in "drawable".776*777*----------------------------------------------------------------------778*/779780void781TkUnderlineChars(display, drawable, gc, fontStructPtr, string, x, y,782tabOrigin, flags, firstChar, lastChar)783Display *display; /* Display on which to draw. */784Drawable drawable; /* Window or pixmap in which to draw. */785GC gc; /* Graphics context for actually drawing786* underline. */787XFontStruct *fontStructPtr; /* Font used in GC; must have been allocated788* by Tk_GetFontStruct. Used to character789* dimensions, etc. */790char *string; /* String containing characters to be791* underlined. */792int x, y; /* Coordinates at which first character of793* string is drawn. */794int tabOrigin; /* X-location that serves as "origin" for795* tab stops. */796int flags; /* Flags that were passed to TkDisplayChars. */797int firstChar; /* Index of first character to underline. */798int lastChar; /* Index of last character to underline. */799{800int xUnder, yUnder, width, height;801unsigned long value;802803/*804* First compute the vertical span of the underline, using font805* properties if they exist.806*/807808if (XGetFontProperty(fontStructPtr, XA_UNDERLINE_POSITION, &value)) {809yUnder = y + value;810} else {811yUnder = y + fontStructPtr->max_bounds.descent/2;812}813if (XGetFontProperty(fontStructPtr, XA_UNDERLINE_THICKNESS, &value)) {814height = value;815} else {816height = 2;817}818819/*820* Now compute the horizontal span of the underline.821*/822823TkMeasureChars(fontStructPtr, string, firstChar, x, (int) 1000000,824tabOrigin, flags, &xUnder);825TkMeasureChars(fontStructPtr, string+firstChar, lastChar+1-firstChar,826xUnder, (int) 1000000, tabOrigin, flags, &width);827width -= xUnder;828829XFillRectangle(display, drawable, gc, xUnder, yUnder,830(unsigned int) width, (unsigned int) height);831}832833/*834*----------------------------------------------------------------------835*836* TkComputeTextGeometry --837*838* This procedure computes the amount of screen space needed to839* display a multi-line string of text.840*841* Results:842* There is no return value. The dimensions of the screen area843* needed to display the text are returned in *widthPtr, and *heightPtr.844*845* Side effects:846* None.847*848*----------------------------------------------------------------------849*/850851void852TkComputeTextGeometry(fontStructPtr, string, numChars, wrapLength,853widthPtr, heightPtr)854XFontStruct *fontStructPtr; /* Font that will be used to display text. */855char *string; /* String whose dimensions are to be856* computed. */857int numChars; /* Number of characters to consider from858* string. */859int wrapLength; /* Longest permissible line length, in860* pixels. <= 0 means no automatic wrapping:861* just let lines get as long as needed. */862int *widthPtr; /* Store width of string here. */863int *heightPtr; /* Store height of string here. */864{865int thisWidth, maxWidth, numLines;866char *p;867868if (wrapLength <= 0) {869wrapLength = INT_MAX;870}871maxWidth = 0;872for (numLines = 1, p = string; (p - string) < numChars; numLines++) {873p += TkMeasureChars(fontStructPtr, p, numChars - (p - string), 0,874wrapLength, 0, TK_WHOLE_WORDS|TK_AT_LEAST_ONE, &thisWidth);875if (thisWidth > maxWidth) {876maxWidth = thisWidth;877}878if (*p == 0) {879break;880}881882/*883* If the character that didn't fit in this line was a white884* space character then skip it.885*/886887if (isspace(UCHAR(*p))) {888p++;889}890}891*widthPtr = maxWidth;892*heightPtr = numLines * (fontStructPtr->ascent + fontStructPtr->descent);893}894895/*896*----------------------------------------------------------------------897*898* TkDisplayText --899*900* Display a text string on one or more lines.901*902* Results:903* None.904*905* Side effects:906* The text given by "string" gets displayed at the given location907* in the given drawable with the given font etc.908*909*----------------------------------------------------------------------910*/911912void913TkDisplayText(display, drawable, fontStructPtr, string, numChars, x, y,914length, justify, underline, gc)915Display *display; /* X display to use for drawing text. */916Drawable drawable; /* Window or pixmap in which to draw the917* text. */918XFontStruct *fontStructPtr; /* Font that determines geometry of text919* (should be same as font in gc). */920char *string; /* String to display; may contain embedded921* newlines. */922int numChars; /* Number of characters to use from string. */923int x, y; /* Pixel coordinates within drawable of924* upper left corner of display area. */925int length; /* Line length in pixels; used to compute926* word wrap points and also for927* justification. Must be > 0. */928Tk_Justify justify; /* How to justify lines. */929int underline; /* Index of character to underline, or < 0930* for no underlining. */931GC gc; /* Graphics context to use for drawing text. */932{933char *p;934int charsThisLine, lengthThisLine, xThisLine;935936/*937* Work through the string one line at a time. Display each line938* in four steps:939* 1. Compute the line's length.940* 2. Figure out where to display the line for justification.941* 3. Display the line.942* 4. Underline one character if needed.943*/944945y += fontStructPtr->ascent;946for (p = string; numChars > 0; ) {947charsThisLine = TkMeasureChars(fontStructPtr, p, numChars, 0, length,9480, TK_WHOLE_WORDS|TK_AT_LEAST_ONE, &lengthThisLine);949if (justify == TK_JUSTIFY_LEFT) {950xThisLine = x;951} else if (justify == TK_JUSTIFY_CENTER) {952xThisLine = x + (length - lengthThisLine)/2;953} else {954xThisLine = x + (length - lengthThisLine);955}956TkDisplayChars(display, drawable, gc, fontStructPtr, p, charsThisLine,957xThisLine, y, xThisLine, 0);958if ((underline >= 0) && (underline < charsThisLine)) {959TkUnderlineChars(display, drawable, gc, fontStructPtr, p,960xThisLine, y, xThisLine, 0, underline, underline);961}962p += charsThisLine;963numChars -= charsThisLine;964underline -= charsThisLine;965y += fontStructPtr->ascent + fontStructPtr->descent;966967/*968* If the character that didn't fit was a space character, skip it.969*/970971if (isspace(UCHAR(*p))) {972p++;973numChars--;974underline--;975}976}977}978979980