1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
|
/*
* tkUnixFont.c --
*
* Contains the Unix implementation of the platform-independant
* font package interface.
*
* Copyright (c) 1996 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tkUnixFont.c,v 1.4 1998/11/25 01:48:54 stanton Exp $
*/
#include "tkPort.h"
#include "tkInt.h"
#include "tkUnixInt.h"
#include "tkFont.h"
#ifndef ABS
#define ABS(n) (((n) < 0) ? -(n) : (n))
#endif
/*
* The following structure represents Unix's implementation of a font.
*/
typedef struct UnixFont {
TkFont font; /* Stuff used by generic font package. Must
* be first in structure. */
Display *display; /* The display to which font belongs. */
XFontStruct *fontStructPtr; /* X information about font. */
char types[256]; /* Array giving types of all characters in
* the font, used when displaying control
* characters. See below for definition. */
int widths[256]; /* Array giving widths of all possible
* characters in the font. */
int underlinePos; /* Offset from baseline to origin of
* underline bar (used for simulating a native
* underlined font). */
int barHeight; /* Height of underline or overstrike bar
* (used for simulating a native underlined or
* strikeout font). */
} UnixFont;
/*
* Possible values for entries in the "types" field in a UnixFont structure,
* which classifies the types of all characters in the given font. This
* information is used when measuring and displaying characters.
*
* NORMAL: Standard character.
* REPLACE: This character doesn't print: instead of
* displaying character, display a replacement
* sequence like "\n" (for those characters where
* ANSI C defines such a sequence) or a sequence
* of the form "\xdd" where dd is the hex equivalent
* of the character.
* SKIP: Don't display anything for this character. This
* is only used where the font doesn't contain
* all the characters needed to generate
* replacement sequences.
*/
#define NORMAL 0
#define REPLACE 1
#define SKIP 2
/*
* Characters used when displaying control sequences.
*/
static char hexChars[] = "0123456789abcdefxtnvr\\";
/*
* The following table maps some control characters to sequences like '\n'
* rather than '\x10'. A zero entry in the table means no such mapping
* exists, and the table only maps characters less than 0x10.
*/
static char mapChars[] = {
0, 0, 0, 0, 0, 0, 0,
'a', 'b', 't', 'n', 'v', 'f', 'r',
0
};
static UnixFont * AllocFont _ANSI_ARGS_((TkFont *tkFontPtr,
Tk_Window tkwin, XFontStruct *fontStructPtr,
CONST char *fontName));
static void DrawChars _ANSI_ARGS_((Display *display,
Drawable drawable, GC gc, UnixFont *fontPtr,
CONST char *source, int numChars, int x,
int y));
static int GetControlCharSubst _ANSI_ARGS_((int c, char buf[4]));
/*
*---------------------------------------------------------------------------
*
* TkpGetNativeFont --
*
* Map a platform-specific native font name to a TkFont.
*
* Results:
* The return value is a pointer to a TkFont that represents the
* native font. If a native font by the given name could not be
* found, the return value is NULL.
*
* Every call to this procedure returns a new TkFont structure,
* even if the name has already been seen before. The caller should
* call TkpDeleteFont() when the font is no longer needed.
*
* The caller is responsible for initializing the memory associated
* with the generic TkFont when this function returns and releasing
* the contents of the generic TkFont before calling TkpDeleteFont().
*
* Side effects:
* None.
*
*---------------------------------------------------------------------------
*/
TkFont *
TkpGetNativeFont(tkwin, name)
Tk_Window tkwin; /* For display where font will be used. */
CONST char *name; /* Platform-specific font name. */
{
XFontStruct *fontStructPtr;
char *p;
int hasSpace, dashes, hasWild;
/*
* The behavior of X when given a name that isn't an XLFD is unspecified.
* For example, Exceed 6 returns a valid font for any random string. This
* is awkward since system names have higher priority than the other Tk
* font syntaxes. So, we need to perform a quick sanity check on the
* name and fail if it looks suspicious. We fail if the name:
* - contains a space immediately before a dash
* - contains a space, but no '*' characters and fewer than 14 dashes
*/
hasSpace = dashes = hasWild = 0;
for (p = name; *p != '\0'; p++) {
if (*p == ' ') {
if (p[1] == '-') {
return NULL;
}
hasSpace = 1;
} else if (*p == '-') {
dashes++;
} else if (*p == '*') {
hasWild = 1;
}
}
if ((dashes < 14) && !hasWild && hasSpace) {
return NULL;
}
fontStructPtr = XLoadQueryFont(Tk_Display(tkwin), name);
if (fontStructPtr == NULL) {
return NULL;
}
return (TkFont *) AllocFont(NULL, tkwin, fontStructPtr, name);
}
/*
*---------------------------------------------------------------------------
*
* TkpGetFontFromAttributes --
*
* Given a desired set of attributes for a font, find a font with
* the closest matching attributes.
*
* Results:
* The return value is a pointer to a TkFont that represents the
* font with the desired attributes. If a font with the desired
* attributes could not be constructed, some other font will be
* substituted automatically.
*
* Every call to this procedure returns a new TkFont structure,
* even if the specified attributes have already been seen before.
* The caller should call TkpDeleteFont() to free the platform-
* specific data when the font is no longer needed.
*
* The caller is responsible for initializing the memory associated
* with the generic TkFont when this function returns and releasing
* the contents of the generic TkFont before calling TkpDeleteFont().
*
* Side effects:
* None.
*
*---------------------------------------------------------------------------
*/
TkFont *
TkpGetFontFromAttributes(tkFontPtr, tkwin, faPtr)
TkFont *tkFontPtr; /* If non-NULL, store the information in
* this existing TkFont structure, rather than
* allocating a new structure to hold the
* font; the existing contents of the font
* will be released. If NULL, a new TkFont
* structure is allocated. */
Tk_Window tkwin; /* For display where font will be used. */
CONST TkFontAttributes *faPtr; /* Set of attributes to match. */
{
int numNames, score, i, scaleable, pixelsize, xaPixelsize;
int bestIdx, bestScore, bestScaleableIdx, bestScaleableScore;
TkXLFDAttributes xa;
char buf[256];
UnixFont *fontPtr;
char **nameList;
XFontStruct *fontStructPtr;
CONST char *fmt, *family;
double d;
family = faPtr->family;
if (family == NULL) {
family = "*";
}
pixelsize = -faPtr->pointsize;
if (pixelsize < 0) {
d = -pixelsize * 25.4 / 72;
d *= WidthOfScreen(Tk_Screen(tkwin));
d /= WidthMMOfScreen(Tk_Screen(tkwin));
d += 0.5;
pixelsize = (int) d;
}
/*
* Replace the standard Windows and Mac family names with the names that
* X likes.
*/
if ((strcasecmp("Times New Roman", family) == 0)
|| (strcasecmp("New York", family) == 0)) {
family = "Times";
} else if ((strcasecmp("Courier New", family) == 0)
|| (strcasecmp("Monaco", family) == 0)) {
family = "Courier";
} else if ((strcasecmp("Arial", family) == 0)
|| (strcasecmp("Geneva", family) == 0)) {
family = "Helvetica";
}
/*
* First try for the Q&D exact match.
*/
#if 0
sprintf(buf, "-*-%.200s-%s-%c-normal-*-*-%d-*-*-*-*-iso8859-1", family,
((faPtr->weight > TK_FW_NORMAL) ? "bold" : "medium"),
((faPtr->slant == TK_FS_ROMAN) ? 'r' :
(faPtr->slant == TK_FS_ITALIC) ? 'i' : 'o'),
faPtr->pointsize * 10);
fontStructPtr = XLoadQueryFont(Tk_Display(tkwin), buf);
#else
fontStructPtr = NULL;
#endif
if (fontStructPtr != NULL) {
goto end;
}
/*
* Couldn't find exact match. Now fall back to other available
* physical fonts.
*/
fmt = "-*-%.240s-*-*-*-*-*-*-*-*-*-*-*-*";
sprintf(buf, fmt, family);
nameList = XListFonts(Tk_Display(tkwin), buf, 10000, &numNames);
if (numNames == 0) {
/*
* Try getting some system font.
*/
sprintf(buf, fmt, "fixed");
nameList = XListFonts(Tk_Display(tkwin), buf, 10000, &numNames);
if (numNames == 0) {
getsystem:
fontStructPtr = XLoadQueryFont(Tk_Display(tkwin), "fixed");
if (fontStructPtr == NULL) {
fontStructPtr = XLoadQueryFont(Tk_Display(tkwin), "*");
if (fontStructPtr == NULL) {
panic("TkpGetFontFromAttributes: cannot get any font");
}
}
goto end;
}
}
/*
* Inspect each of the XLFDs and pick the one that most closely
* matches the desired attributes.
*/
bestIdx = 0;
bestScore = INT_MAX;
bestScaleableIdx = 0;
bestScaleableScore = INT_MAX;
for (i = 0; i < numNames; i++) {
score = 0;
scaleable = 0;
if (TkParseXLFD(nameList[i], &xa) != TCL_OK) {
continue;
}
xaPixelsize = -xa.fa.pointsize;
/*
* Since most people used to use -adobe-* in their XLFDs,
* preserve the preference for "adobe" foundry. Otherwise
* some applications looks may change slightly if another foundry
* is chosen.
*/
if (strcasecmp(xa.foundry, "adobe") != 0) {
score += 3000;
}
if (xa.fa.pointsize == 0) {
/*
* A scaleable font is almost always acceptable, but the
* corresponding bitmapped font would be better.
*/
score += 10;
scaleable = 1;
} else {
/*
* A font that is too small is better than one that is too
* big.
*/
if (xaPixelsize > pixelsize) {
score += (xaPixelsize - pixelsize) * 120;
} else {
score += (pixelsize - xaPixelsize) * 100;
}
}
score += ABS(xa.fa.weight - faPtr->weight) * 30;
score += ABS(xa.fa.slant - faPtr->slant) * 25;
if (xa.slant == TK_FS_OBLIQUE) {
/*
* Italic fonts are preferred over oblique. */
score += 4;
}
if (xa.setwidth != TK_SW_NORMAL) {
/*
* The normal setwidth is highly preferred.
*/
score += 2000;
}
if (xa.charset == TK_CS_OTHER) {
/*
* The standard character set is highly preferred over
* foreign languages charsets (because we don't support
* other languages yet).
*/
score += 11000;
}
if ((xa.charset == TK_CS_NORMAL) && (xa.encoding != 1)) {
/*
* The '1' encoding for the characters above 0x7f is highly
* preferred over the other encodings.
*/
score += 8000;
}
if (scaleable) {
if (score < bestScaleableScore) {
bestScaleableIdx = i;
bestScaleableScore = score;
}
} else {
if (score < bestScore) {
bestIdx = i;
bestScore = score;
}
}
if (score == 0) {
break;
}
}
/*
* Now we know which is the closest matching scaleable font and the
* closest matching bitmapped font. If the scaleable font was a
* better match, try getting the scaleable font; however, if the
* scalable font was not actually available in the desired
* pointsize, fall back to the closest bitmapped font.
*/
fontStructPtr = NULL;
if (bestScaleableScore < bestScore) {
char *str, *rest;
/*
* Fill in the desired pointsize info for this font.
*/
tryscale:
str = nameList[bestScaleableIdx];
for (i = 0; i < XLFD_PIXEL_SIZE - 1; i++) {
str = strchr(str + 1, '-');
}
rest = str;
for (i = XLFD_PIXEL_SIZE - 1; i < XLFD_REGISTRY; i++) {
rest = strchr(rest + 1, '-');
}
*str = '\0';
sprintf(buf, "%.240s-*-%d-*-*-*-*-*%s", nameList[bestScaleableIdx],
pixelsize, rest);
*str = '-';
fontStructPtr = XLoadQueryFont(Tk_Display(tkwin), buf);
bestScaleableScore = INT_MAX;
}
if (fontStructPtr == NULL) {
strcpy(buf, nameList[bestIdx]);
fontStructPtr = XLoadQueryFont(Tk_Display(tkwin), buf);
if (fontStructPtr == NULL) {
/*
* This shouldn't happen because the font name is one of the
* names that X gave us to use, but it does anyhow.
*/
if (bestScaleableScore < INT_MAX) {
goto tryscale;
} else {
XFreeFontNames(nameList);
goto getsystem;
}
}
}
XFreeFontNames(nameList);
end:
fontPtr = AllocFont(tkFontPtr, tkwin, fontStructPtr, buf);
fontPtr->font.fa.underline = faPtr->underline;
fontPtr->font.fa.overstrike = faPtr->overstrike;
return (TkFont *) fontPtr;
}
/*
*---------------------------------------------------------------------------
*
* TkpDeleteFont --
*
* Called to release a font allocated by TkpGetNativeFont() or
* TkpGetFontFromAttributes(). The caller should have already
* released the fields of the TkFont that are used exclusively by
* the generic TkFont code.
*
* Results:
* None.
*
* Side effects:
* TkFont is deallocated.
*
*---------------------------------------------------------------------------
*/
void
TkpDeleteFont(tkFontPtr)
TkFont *tkFontPtr; /* Token of font to be deleted. */
{
UnixFont *fontPtr;
fontPtr = (UnixFont *) tkFontPtr;
XFreeFont(fontPtr->display, fontPtr->fontStructPtr);
ckfree((char *) fontPtr);
}
/*
*---------------------------------------------------------------------------
*
* TkpGetFontFamilies --
*
* Return information about the font families that are available
* on the display of the given window.
*
* Results:
* interp->result is modified to hold a list of all the available
* font families.
*
* Side effects:
* None.
*
*---------------------------------------------------------------------------
*/
void
TkpGetFontFamilies(interp, tkwin)
Tcl_Interp *interp;
Tk_Window tkwin;
{
int i, new, numNames;
char *family, *end, *p;
Tcl_HashTable familyTable;
Tcl_HashEntry *hPtr;
Tcl_HashSearch search;
char **nameList;
Tcl_InitHashTable(&familyTable, TCL_STRING_KEYS);
nameList = XListFonts(Tk_Display(tkwin), "*", 10000, &numNames);
for (i = 0; i < numNames; i++) {
if (nameList[i][0] != '-') {
continue;
}
family = strchr(nameList[i] + 1, '-');
if (family == NULL) {
continue;
}
family++;
end = strchr(family, '-');
if (end == NULL) {
continue;
}
*end = '\0';
for (p = family; *p != '\0'; p++) {
if (isupper(UCHAR(*p))) {
*p = tolower(UCHAR(*p));
}
}
Tcl_CreateHashEntry(&familyTable, family, &new);
}
hPtr = Tcl_FirstHashEntry(&familyTable, &search);
while (hPtr != NULL) {
Tcl_AppendElement(interp, Tcl_GetHashKey(&familyTable, hPtr));
hPtr = Tcl_NextHashEntry(&search);
}
Tcl_DeleteHashTable(&familyTable);
XFreeFontNames(nameList);
}
/*
*---------------------------------------------------------------------------
*
* Tk_MeasureChars --
*
* Determine the number of characters from the string that will fit
* in the given horizontal span. The measurement is done under the
* assumption that Tk_DrawChars() will be used to actually display
* the characters.
*
* Results:
* The return value is the number of characters from source that
* fit into the span that extends from 0 to maxLength. *lengthPtr is
* filled with the x-coordinate of the right edge of the last
* character that did fit.
*
* Side effects:
* None.
*
*---------------------------------------------------------------------------
*/
int
Tk_MeasureChars(tkfont, source, numChars, maxLength, flags, lengthPtr)
Tk_Font tkfont; /* Font in which characters will be drawn. */
CONST char *source; /* Characters to be displayed. Need not be
* '\0' terminated. */
int numChars; /* Maximum number of characters to consider
* from source string. */
int maxLength; /* If > 0, maxLength specifies the longest
* permissible line length; don't consider any
* character that would cross this
* x-position. If <= 0, then line length is
* unbounded and the flags argument is
* ignored. */
int flags; /* Various flag bits OR-ed together:
* TK_PARTIAL_OK means include the last char
* which only partially fit on this line.
* TK_WHOLE_WORDS means stop on a word
* boundary, if possible.
* TK_AT_LEAST_ONE means return at least one
* character even if no characters fit. */
int *lengthPtr; /* Filled with x-location just after the
* terminating character. */
{
UnixFont *fontPtr;
CONST char *p; /* Current character. */
CONST char *term; /* Pointer to most recent character that
* may legally be a terminating character. */
int termX; /* X-position just after term. */
int curX; /* X-position corresponding to p. */
int newX; /* X-position corresponding to p+1. */
int c, sawNonSpace;
fontPtr = (UnixFont *) tkfont;
if (numChars == 0) {
*lengthPtr = 0;
return 0;
}
if (maxLength <= 0) {
maxLength = INT_MAX;
}
newX = curX = termX = 0;
p = term = source;
sawNonSpace = !isspace(UCHAR(*p));
/*
* Scan the input string one character at a time, calculating width.
*/
for (c = UCHAR(*p); ; ) {
newX += fontPtr->widths[c];
if (newX > maxLength) {
break;
}
curX = newX;
numChars--;
p++;
if (numChars == 0) {
term = p;
termX = curX;
break;
}
c = UCHAR(*p);
if (isspace(c)) {
if (sawNonSpace) {
term = p;
termX = curX;
sawNonSpace = 0;
}
} else {
sawNonSpace = 1;
}
}
/*
* P points to the first character that doesn't fit in the desired
* span. Use the flags to figure out what to return.
*/
if ((flags & TK_PARTIAL_OK) && (numChars > 0) && (curX < maxLength)) {
/*
* Include the first character that didn't quite fit in the desired
* span. The width returned will include the width of that extra
* character.
*/
numChars--;
curX = newX;
p++;
}
if ((flags & TK_AT_LEAST_ONE) && (term == source) && (numChars > 0)) {
term = p;
termX = curX;
if (term == source) {
term++;
termX = newX;
}
} else if ((numChars == 0) || !(flags & TK_WHOLE_WORDS)) {
term = p;
termX = curX;
}
*lengthPtr = termX;
return term-source;
}
/*
*---------------------------------------------------------------------------
*
* Tk_DrawChars, DrawChars --
*
* Draw a string of characters on the screen. Tk_DrawChars()
* expands control characters that occur in the string to \X or
* \xXX sequences. DrawChars() just draws the strings.
*
* Results:
* None.
*
* Side effects:
* Information gets drawn on the screen.
*
*---------------------------------------------------------------------------
*/
void
Tk_DrawChars(display, drawable, gc, tkfont, source, numChars, x, y)
Display *display; /* Display on which to draw. */
Drawable drawable; /* Window or pixmap in which to draw. */
GC gc; /* Graphics context for drawing characters. */
Tk_Font tkfont; /* Font in which characters will be drawn;
* must be the same as font used in GC. */
CONST char *source; /* Characters to be displayed. Need not be
* '\0' terminated. All Tk meta-characters
* (tabs, control characters, and newlines)
* should be stripped out of the string that
* is passed to this function. If they are
* not stripped out, they will be displayed as
* regular printing characters. */
int numChars; /* Number of characters in string. */
int x, y; /* Coordinates at which to place origin of
* string when drawing. */
{
UnixFont *fontPtr;
CONST char *p;
int i, type;
char buf[4];
fontPtr = (UnixFont *) tkfont;
p = source;
for (i = 0; i < numChars; i++) {
type = fontPtr->types[UCHAR(*p)];
if (type != NORMAL) {
DrawChars(display, drawable, gc, fontPtr, source, p - source, x, y);
x += XTextWidth(fontPtr->fontStructPtr, source, p - source);
if (type == REPLACE) {
DrawChars(display, drawable, gc, fontPtr, buf,
GetControlCharSubst(UCHAR(*p), buf), x, y);
x += fontPtr->widths[UCHAR(*p)];
}
source = p + 1;
}
p++;
}
DrawChars(display, drawable, gc, fontPtr, source, p - source, x, y);
}
static void
DrawChars(display, drawable, gc, fontPtr, source, numChars, x, y)
Display *display; /* Display on which to draw. */
Drawable drawable; /* Window or pixmap in which to draw. */
GC gc; /* Graphics context for drawing characters. */
UnixFont *fontPtr; /* Font in which characters will be drawn;
* must be the same as font used in GC. */
CONST char *source; /* Characters to be displayed. Need not be
* '\0' terminated. All Tk meta-characters
* (tabs, control characters, and newlines)
* should be stripped out of the string that
* is passed to this function. If they are
* not stripped out, they will be displayed as
* regular printing characters. */
int numChars; /* Number of characters in string. */
int x, y; /* Coordinates at which to place origin of
* string when drawing. */
{
/*
* Perform a quick sanity check to ensure we won't overflow the X
* coordinate space.
*/
if ((x + (fontPtr->fontStructPtr->max_bounds.width * numChars) > 0x7fff)) {
int length;
/*
* The string we are being asked to draw is too big and would overflow
* the X coordinate space. Unfortunatley X servers aren't too bright
* and so they won't deal with this case cleanly. We need to truncate
* the string before sending it to X.
*/
numChars = Tk_MeasureChars((Tk_Font) fontPtr, source, numChars,
0x7fff - x, 0, &length);
}
XDrawString(display, drawable, gc, x, y, source, numChars);
if (fontPtr->font.fa.underline != 0) {
XFillRectangle(display, drawable, gc, x,
y + fontPtr->underlinePos,
(unsigned) XTextWidth(fontPtr->fontStructPtr, source, numChars),
(unsigned) fontPtr->barHeight);
}
if (fontPtr->font.fa.overstrike != 0) {
y -= fontPtr->font.fm.descent + (fontPtr->font.fm.ascent) / 10;
XFillRectangle(display, drawable, gc, x, y,
(unsigned) XTextWidth(fontPtr->fontStructPtr, source, numChars),
(unsigned) fontPtr->barHeight);
}
}
/*
*---------------------------------------------------------------------------
*
* AllocFont --
*
* Helper for TkpGetNativeFont() and TkpGetFontFromAttributes().
* Allocates and intializes the memory for a new TkFont that
* wraps the platform-specific data.
*
* Results:
* Returns pointer to newly constructed TkFont.
*
* The caller is responsible for initializing the fields of the
* TkFont that are used exclusively by the generic TkFont code, and
* for releasing those fields before calling TkpDeleteFont().
*
* Side effects:
* Memory allocated.
*
*---------------------------------------------------------------------------
*/
static UnixFont *
AllocFont(tkFontPtr, tkwin, fontStructPtr, fontName)
TkFont *tkFontPtr; /* If non-NULL, store the information in
* this existing TkFont structure, rather than
* allocating a new structure to hold the
* font; the existing contents of the font
* will be released. If NULL, a new TkFont
* structure is allocated. */
Tk_Window tkwin; /* For display where font will be used. */
XFontStruct *fontStructPtr; /* X information about font. */
CONST char *fontName; /* The string passed to XLoadQueryFont() to
* construct the fontStructPtr. */
{
UnixFont *fontPtr;
unsigned long value;
int i, width, firstChar, lastChar, n, replaceOK;
char *name, *p;
char buf[4];
TkXLFDAttributes xa;
double d;
if (tkFontPtr != NULL) {
fontPtr = (UnixFont *) tkFontPtr;
XFreeFont(fontPtr->display, fontPtr->fontStructPtr);
} else {
fontPtr = (UnixFont *) ckalloc(sizeof(UnixFont));
}
/*
* Encapsulate the generic stuff in the TkFont.
*/
fontPtr->font.fid = fontStructPtr->fid;
if (XGetFontProperty(fontStructPtr, XA_FONT, &value) && (value != 0)) {
name = Tk_GetAtomName(tkwin, (Atom) value);
TkInitFontAttributes(&xa.fa);
if (TkParseXLFD(name, &xa) == TCL_OK) {
goto ok;
}
}
TkInitFontAttributes(&xa.fa);
if (TkParseXLFD(fontName, &xa) != TCL_OK) {
TkInitFontAttributes(&fontPtr->font.fa);
fontPtr->font.fa.family = Tk_GetUid(fontName);
} else {
ok:
fontPtr->font.fa = xa.fa;
}
if (fontPtr->font.fa.pointsize < 0) {
d = -fontPtr->font.fa.pointsize * 72 / 25.4;
d *= WidthMMOfScreen(Tk_Screen(tkwin));
d /= WidthOfScreen(Tk_Screen(tkwin));
d += 0.5;
fontPtr->font.fa.pointsize = (int) d;
}
fontPtr->font.fm.ascent = fontStructPtr->ascent;
fontPtr->font.fm.descent = fontStructPtr->descent;
fontPtr->font.fm.maxWidth = fontStructPtr->max_bounds.width;
fontPtr->font.fm.fixed = 1;
fontPtr->display = Tk_Display(tkwin);
fontPtr->fontStructPtr = fontStructPtr;
/*
* Classify the characters.
*/
firstChar = fontStructPtr->min_char_or_byte2;
lastChar = fontStructPtr->max_char_or_byte2;
for (i = 0; i < 256; i++) {
if ((i == 0177) || (i < firstChar) || (i > lastChar)) {
fontPtr->types[i] = REPLACE;
} else {
fontPtr->types[i] = NORMAL;
}
}
/*
* Compute the widths for all the normal characters. Any other
* characters are given an initial width of 0. Also, this determines
* if this is a fixed or variable width font, by comparing the widths
* of all the normal characters.
*/
width = 0;
for (i = 0; i < 256; i++) {
if (fontPtr->types[i] != NORMAL) {
n = 0;
} else if (fontStructPtr->per_char == NULL) {
n = fontStructPtr->max_bounds.width;
} else {
n = fontStructPtr->per_char[i - firstChar].width;
}
fontPtr->widths[i] = n;
if (n != 0) {
if (width == 0) {
width = n;
} else if (width != n) {
fontPtr->font.fm.fixed = 0;
}
}
}
/*
* Compute the widths of the characters that should be replaced with
* control character expansions. If the appropriate chars are not
* available in this font, then control character expansions will not
* be used; control chars will be invisible & zero-width.
*/
replaceOK = 1;
for (p = hexChars; *p != '\0'; p++) {
if ((UCHAR(*p) < firstChar) || (UCHAR(*p) > lastChar)) {
replaceOK = 0;
break;
}
}
for (i = 0; i < 256; i++) {
if (fontPtr->types[i] == REPLACE) {
if (replaceOK) {
n = GetControlCharSubst(i, buf);
for ( ; --n >= 0; ) {
fontPtr->widths[i] += fontPtr->widths[UCHAR(buf[n])];
}
} else {
fontPtr->types[i] = SKIP;
}
}
}
if (XGetFontProperty(fontStructPtr, XA_UNDERLINE_POSITION, &value)) {
fontPtr->underlinePos = value;
} else {
/*
* If the XA_UNDERLINE_POSITION property does not exist, the X
* manual recommends using the following value:
*/
fontPtr->underlinePos = fontStructPtr->descent / 2;
}
fontPtr->barHeight = 0;
if (XGetFontProperty(fontStructPtr, XA_UNDERLINE_THICKNESS, &value)) {
/*
* Sometimes this is 0 even though it shouldn't be.
*/
fontPtr->barHeight = value;
}
if (fontPtr->barHeight == 0) {
/*
* If the XA_UNDERLINE_THICKNESS property does not exist, the X
* manual recommends using the width of the stem on a capital
* letter. I don't know of a way to get the stem width of a letter,
* so guess and use 1/3 the width of a capital I.
*/
fontPtr->barHeight = fontPtr->widths['I'] / 3;
if (fontPtr->barHeight == 0) {
fontPtr->barHeight = 1;
}
}
if (fontPtr->underlinePos + fontPtr->barHeight > fontStructPtr->descent) {
/*
* If this set of cobbled together values would cause the bottom of
* the underline bar to stick below the descent of the font, jack
* the underline up a bit higher.
*/
fontPtr->barHeight = fontStructPtr->descent - fontPtr->underlinePos;
if (fontPtr->barHeight == 0) {
fontPtr->underlinePos--;
fontPtr->barHeight = 1;
}
}
return fontPtr;
}
/*
*---------------------------------------------------------------------------
*
* GetControlCharSubst --
*
* When displaying text in a widget, a backslashed escape sequence
* is substituted for control characters that occur in the text.
* Given a control character, fill in a buffer with the replacement
* string that should be displayed.
*
* Results:
* The return value is the length of the substitute string. buf is
* filled with the substitute string; it is not '\0' terminated.
*
* Side effects:
* None.
*
*---------------------------------------------------------------------------
*/
static int
GetControlCharSubst(c, buf)
int c; /* The control character to be replaced. */
char buf[4]; /* Buffer that gets replacement string. It
* only needs to be 4 characters long. */
{
buf[0] = '\\';
if ((c < sizeof(mapChars)) && (mapChars[c] != 0)) {
buf[1] = mapChars[c];
return 2;
} else {
buf[1] = 'x';
buf[2] = hexChars[(c >> 4) & 0xf];
buf[3] = hexChars[c & 0xf];
return 4;
}
}
|