-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathtkUnixFont.c
More file actions
3193 lines (2897 loc) · 92.4 KB
/
Copy pathtkUnixFont.c
File metadata and controls
3193 lines (2897 loc) · 92.4 KB
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
/*
* tkUnixFont.c --
*
* Contains the Unix implementation of the platform-independent font
* package interface.
*
* Copyright © 1996-1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tkUnixInt.h"
#include "tkFont.h"
/*
* The preferred font encodings.
*/
static const char encodingList[][10] = {
"iso8859-1", "jis0208", "jis0212"
};
/*
* The following structure represents a font family. It is assumed that all
* screen fonts constructed from the same "font family" share certain
* properties; all screen fonts with the same "font family" point to a shared
* instance of this structure. The most important shared property is the
* character existence metrics, used to determine if a screen font can display
* a given Unicode character.
*
* Under Unix, there are three attributes that uniquely identify a "font
* family": the foundry, face name, and charset.
*/
#define FONTMAP_SHIFT 10
#define FONTMAP_BITSPERPAGE (1 << FONTMAP_SHIFT)
#define FONTMAP_NUMCHARS 0x40000
#define FONTMAP_PAGES (FONTMAP_NUMCHARS / FONTMAP_BITSPERPAGE)
typedef struct FontFamily {
struct FontFamily *nextPtr; /* Next in list of all known font families. */
size_t refCount; /* How many SubFonts are referring to this
* FontFamily. When the refCount drops to
* zero, this FontFamily may be freed. */
/*
* Key.
*/
Tk_Uid foundry; /* Foundry key for this FontFamily. */
Tk_Uid faceName; /* Face name key for this FontFamily. */
Tcl_Encoding encoding; /* Encoding key for this FontFamily. */
/*
* Derived properties.
*/
int isTwoByteFont; /* 1 if this is a double-byte font, 0
* otherwise. */
char *fontMap[FONTMAP_PAGES];
/* Two-level sparse table used to determine
* quickly if the specified character exists.
* As characters are encountered, more pages
* in this table are dynamically allocated. The
* contents of each page is a bitmask
* consisting of FONTMAP_BITSPERPAGE bits,
* representing whether this font can be used
* to display the given character at the
* corresponding bit position. The high bits
* of the character are used to pick which
* page of the table is used. */
} FontFamily;
/*
* The following structure encapsulates an individual screen font. A font
* object is made up of however many SubFonts are necessary to display a
* stream of multilingual characters.
*/
typedef struct SubFont {
char **fontMap; /* Pointer to font map from the FontFamily,
* cached here to save a dereference. */
XFontStruct *fontStructPtr; /* The specific screen font that will be used
* when displaying/measuring chars belonging
* to the FontFamily. */
FontFamily *familyPtr; /* The FontFamily for this SubFont. */
} SubFont;
/*
* The following structure represents Unix's implementation of a font object.
*/
#define SUBFONT_SPACE 3
#define BASE_CHARS 256
typedef struct UnixFont {
TkFont font; /* Stuff used by generic font package. Must be
* first in structure. */
SubFont staticSubFonts[SUBFONT_SPACE];
/* Builtin space for a limited number of
* SubFonts. */
int numSubFonts; /* Length of following array. */
SubFont *subFontArray; /* Array of SubFonts that have been loaded in
* order to draw/measure all the characters
* encountered by this font so far. All fonts
* start off with one SubFont initialized by
* InitFont() from the original set of font
* attributes. Usually points to
* staticSubFonts, but may point to malloced
* space if there are lots of SubFonts. */
SubFont controlSubFont; /* Font to use to display control-character
* expansions. */
Display *display; /* Display that owns font. */
int pixelSize; /* Original pixel size used when font was
* constructed. */
TkXLFDAttributes xa; /* Additional attributes that specify the
* preferred foundry and encoding to use when
* constructing additional SubFonts. */
int widths[BASE_CHARS]; /* Widths of first 256 chars in the base font,
* for handling common case. */
int underlinePos; /* Offset from baseline to origin of underline
* bar (used when drawing underlined font)
* (pixels). */
int barHeight; /* Height of underline or overstrike bar (used
* when drawing underlined or strikeout font)
* (pixels). */
} UnixFont;
/*
* The following structure and definition is used to keep track of the
* alternative names for various encodings. Asking for an encoding that
* matches one of the alias patterns will result in actually getting the
* encoding by its real name.
*/
typedef struct EncodingAlias {
const char *realName; /* The real name of the encoding to load if
* the provided name matched the pattern. */
const char *aliasPattern; /* Pattern for encoding name, of the form that
* is acceptable to Tcl_StringMatch. */
} EncodingAlias;
/*
* Just some utility structures used for passing around values in helper
* functions.
*/
typedef struct FontAttributes {
TkFontAttributes fa;
TkXLFDAttributes xa;
} FontAttributes;
typedef struct {
FontFamily *fontFamilyList; /* The list of font families that are
* currently loaded. As screen fonts are
* loaded, this list grows to hold information
* about what characters exist in each font
* family. */
FontFamily controlFamily; /* FontFamily used to handle control character
* expansions. The encoding of this FontFamily
* converts UTF-8 to backslashed escape
* sequences. */
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
/*
* The set of builtin encoding alises to convert the XLFD names for the
* encodings into the names expected by the Tcl encoding package.
*/
static const EncodingAlias encodingAliases[] = {
{"gb2312-raw", "gb2312*"},
{"big5", "big5*"},
{"cns11643-1", "cns11643*-1"},
{"cns11643-1", "cns11643*.1-0"},
{"cns11643-2", "cns11643*-2"},
{"cns11643-2", "cns11643*.2-0"},
{"jis0201", "jisx0201*"},
{"jis0201", "jisx0202*"},
{"jis0208", "jisc6226*"},
{"jis0208", "jisx0208*"},
{"jis0212", "jisx0212*"},
{"tis620", "tis620*"},
{"ksc5601", "ksc5601*"},
{"dingbats", "*dingbats"},
{"ucs-2be", "iso10646-1"},
{NULL, NULL}
};
/*
* Functions used only in this file.
*/
static void FontPkgCleanup(void *clientData);
static FontFamily * AllocFontFamily(Display *display,
XFontStruct *fontStructPtr, int base);
static SubFont * CanUseFallback(UnixFont *fontPtr,
const char *fallbackName, int ch,
SubFont **fixSubFontPtrPtr);
static SubFont * CanUseFallbackWithAliases(UnixFont *fontPtr,
const char *fallbackName, int ch,
Tcl_DString *nameTriedPtr,
SubFont **fixSubFontPtrPtr);
static int ControlUtfProc(void *clientData, const char *src,
int srcLen, int flags, Tcl_EncodingState*statePtr,
char *dst, int dstLen, int *srcReadPtr,
int *dstWrotePtr, int *dstCharsPtr);
static XFontStruct * CreateClosestFont(Tk_Window tkwin,
const TkFontAttributes *faPtr,
const TkXLFDAttributes *xaPtr);
static SubFont * FindSubFontForChar(UnixFont *fontPtr, int ch,
SubFont **fixSubFontPtrPtr);
static void FontMapInsert(SubFont *subFontPtr, int ch);
static void FontMapLoadPage(SubFont *subFontPtr, int row);
static int FontMapLookup(SubFont *subFontPtr, int ch);
static void FreeFontFamily(FontFamily *afPtr);
static const char * GetEncodingAlias(const char *name);
static int GetFontAttributes(Display *display,
XFontStruct *fontStructPtr, FontAttributes *faPtr);
static XFontStruct * GetScreenFont(Display *display,
FontAttributes *wantPtr, char **nameList,
int bestIdx[], unsigned bestScore[]);
static XFontStruct * GetSystemFont(Display *display);
static int IdentifySymbolEncodings(FontAttributes *faPtr);
static void InitFont(Tk_Window tkwin, XFontStruct *fontStructPtr,
UnixFont *fontPtr);
static void InitSubFont(Display *display,
XFontStruct *fontStructPtr, int base,
SubFont *subFontPtr);
static char ** ListFonts(Display *display, const char *faceName,
int *numNamesPtr);
static char ** ListFontOrAlias(Display *display, const char*faceName,
int *numNamesPtr);
static unsigned RankAttributes(FontAttributes *wantPtr,
FontAttributes *gotPtr);
static void ReleaseFont(UnixFont *fontPtr);
static void ReleaseSubFont(Display *display, SubFont *subFontPtr);
static int SeenName(const char *name, Tcl_DString *dsPtr);
/*
*-------------------------------------------------------------------------
*
* XLoadQueryFontNoXError --
*
* This function is XLoadQueryFont wrapped in a NULL error handler.
* It is a temporary workaround for ticket [36e379c01b],
* "macOS Ventura, X11 build with XQuartz: crash in XLoadQueryFont",
* which actually is issue #216 in XQuartz:
* https://github.com/XQuartz/XQuartz/issues/216
*
*-------------------------------------------------------------------------
*/
static XFontStruct *
XLoadQueryFontNoXError(Display *display, const char *name)
{
XFontStruct *fontStructPtr = NULL;
Tk_ErrorHandler handler;
/* 45 is the major opcode of X_OpenFont */
handler = Tk_CreateErrorHandler(display, BadValue, 45, -1, NULL, NULL);
fontStructPtr = XLoadQueryFont(display, name);
Tk_DeleteErrorHandler(handler);
return fontStructPtr;
}
/*
*-------------------------------------------------------------------------
*
* FontPkgCleanup --
*
* This function is called when an application is created. It initializes
* all the structures that are used by the platform-dependent code on a
* per application basis.
*
* Results:
* None.
*
* Side effects:
* Releases thread-specific resources used by font pkg.
*
*-------------------------------------------------------------------------
*/
static void
FontPkgCleanup(
TCL_UNUSED(void *))
{
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
if (tsdPtr->controlFamily.encoding != NULL) {
FontFamily *familyPtr = &tsdPtr->controlFamily;
int i;
Tcl_FreeEncoding(familyPtr->encoding);
for (i = 0; i < FONTMAP_PAGES; i++) {
if (familyPtr->fontMap[i] != NULL) {
ckfree(familyPtr->fontMap[i]);
}
}
tsdPtr->controlFamily.encoding = NULL;
}
}
/*
*-------------------------------------------------------------------------
*
* TkpFontPkgInit --
*
* This function is called when an application is created. It initializes
* all the structures that are used by the platform-dependent code on a
* per application basis.
*
* Results:
* None.
*
* Side effects:
* None.
*
*-------------------------------------------------------------------------
*/
void
TkpFontPkgInit(
TCL_UNUSED(TkMainInfo *)) /* The application being created. */
{
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
SubFont dummy;
int i;
if (tsdPtr->controlFamily.encoding == NULL) {
Tcl_EncodingType type = {"X11ControlChars", ControlUtfProc, ControlUtfProc, NULL, NULL, 0};
tsdPtr->controlFamily.refCount = 2;
tsdPtr->controlFamily.encoding = Tcl_CreateEncoding(&type);
tsdPtr->controlFamily.isTwoByteFont = 0;
dummy.familyPtr = &tsdPtr->controlFamily;
dummy.fontMap = tsdPtr->controlFamily.fontMap;
for (i = 0x00; i < 0x20; i++) {
FontMapInsert(&dummy, i);
FontMapInsert(&dummy, i + 0x80);
}
/*
* UCS-2BE is unicode (UCS-2) in big-endian format. Define this if
* if it doesn't exist yet. It is used in iso10646 fonts.
*/
Tcl_CreateThreadExitHandler(FontPkgCleanup, NULL);
}
}
/*
*-------------------------------------------------------------------------
*
* ControlUtfProc --
*
* Convert from UTF-8 into the ASCII expansion of a control character.
*
* Results:
* Returns TCL_OK if conversion was successful.
*
* Side effects:
* None.
*
*-------------------------------------------------------------------------
*/
static int
ControlUtfProc(
TCL_UNUSED(void *), /* Not used. */
const char *src, /* Source string in UTF-8. */
int srcLen, /* Source string length in bytes. */
TCL_UNUSED(int), /* Conversion control flags. */
TCL_UNUSED(Tcl_EncodingState *),/* Place for conversion routine to store state
* information used during a piecewise
* conversion. Contents of statePtr are
* initialized and/or reset by conversion
* routine under control of flags argument. */
char *dst, /* Output buffer in which converted string is
* stored. */
int dstLen, /* The maximum length of output buffer in
* bytes. */
int *srcReadPtr, /* Filled with the number of bytes from the
* source string that were converted. This may
* be less than the original source length if
* there was a problem converting some source
* characters. */
int *dstWrotePtr, /* Filled with the number of bytes that were
* stored in the output buffer as a result of
* the conversion. */
int *dstCharsPtr) /* Filled with the number of characters that
* correspond to the bytes stored in the
* output buffer. */
{
const char *srcStart, *srcEnd;
char *dstStart, *dstEnd;
int ch, result;
static const char hexChars[] = "0123456789ABCDEF";
static const char mapChars[] = {
0, 0, 0, 0, 0, 0, 0,
'a', 'b', 't', 'n', 'v', 'f', 'r'
};
result = TCL_OK;
srcStart = src;
srcEnd = src + srcLen;
dstStart = dst;
dstEnd = dst + dstLen - 6;
for ( ; src < srcEnd; ) {
if (dst > dstEnd) {
result = TCL_CONVERT_NOSPACE;
break;
}
src += Tcl_UtfToUniChar(src, &ch);
dst[0] = '\\';
if (((size_t)ch < sizeof(mapChars)) && (mapChars[ch] != 0)) {
dst[1] = mapChars[ch];
dst += 2;
} else if ((size_t)ch < 256) {
dst[1] = 'x';
dst[2] = hexChars[(ch >> 4) & 0xF];
dst[3] = hexChars[ch & 0xF];
dst += 4;
} else if ((size_t)ch < 0x10000) {
dst[1] = 'u';
dst[2] = hexChars[(ch >> 12) & 0xF];
dst[3] = hexChars[(ch >> 8) & 0xF];
dst[4] = hexChars[(ch >> 4) & 0xF];
dst[5] = hexChars[ch & 0xF];
dst += 6;
} else {
/* TODO we can do better here */
dst[1] = 'u';
dst[2] = 'F';
dst[3] = 'F';
dst[4] = 'F';
dst[5] = 'D';
dst += 6;
}
}
*srcReadPtr = src - srcStart;
*dstWrotePtr = dst - dstStart;
*dstCharsPtr = dst - dstStart;
return result;
}
/*
*---------------------------------------------------------------------------
*
* 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 function 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:
* Memory allocated.
*
*---------------------------------------------------------------------------
*/
TkFont *
TkpGetNativeFont(
Tk_Window tkwin, /* For display where font will be used. */
const char *name) /* Platform-specific font name. */
{
UnixFont *fontPtr;
XFontStruct *fontStructPtr;
FontAttributes fa;
const 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 = XLoadQueryFontNoXError(Tk_Display(tkwin), (char *)name);
if (fontStructPtr == NULL) {
/*
* Handle all names that look like XLFDs here. Otherwise, when
* TkpGetFontFromAttributes is called from generic code, any foundry
* or encoding information specified in the XLFD will have been parsed
* out and lost. But make sure we don't have an "-option value" string
* since TkFontParseXLFD would return a false success when attempting
* to parse it.
*/
if (name[0] == '-') {
if (name[1] != '*') {
const char *dash;
dash = strchr(name + 1, '-');
if ((dash == NULL) || (isspace(UCHAR(dash[-1])))) {
return NULL;
}
}
} else if (name[0] != '*') {
return NULL;
}
if (TkFontParseXLFD(name, &fa.fa, &fa.xa) != TCL_OK) {
return NULL;
}
fontStructPtr = CreateClosestFont(tkwin, &fa.fa, &fa.xa);
}
fontPtr = (UnixFont *)ckalloc(sizeof(UnixFont));
InitFont(tkwin, fontStructPtr, fontPtr);
return (TkFont *) fontPtr;
}
/*
*---------------------------------------------------------------------------
*
* 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 function 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:
* Memory allocated.
*
*---------------------------------------------------------------------------
*/
TkFont *
TkpGetFontFromAttributes(
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. */
{
UnixFont *fontPtr;
TkXLFDAttributes xa;
XFontStruct *fontStructPtr;
TkInitXLFDAttributes(&xa);
fontStructPtr = CreateClosestFont(tkwin, faPtr, &xa);
fontPtr = (UnixFont *) tkFontPtr;
if (fontPtr == NULL) {
fontPtr = (UnixFont *)ckalloc(sizeof(UnixFont));
} else {
ReleaseFont(fontPtr);
}
InitFont(tkwin, fontStructPtr, fontPtr);
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(
TkFont *tkFontPtr) /* Token of font to be deleted. */
{
UnixFont *fontPtr = (UnixFont *) tkFontPtr;
ReleaseFont(fontPtr);
}
/*
*---------------------------------------------------------------------------
*
* TkpGetFontFamilies --
*
* Return information about the font families that are available on the
* display of the given window.
*
* Results:
* Modifies interp's result object to hold a list of all the available
* font families.
*
* Side effects:
* None.
*
*---------------------------------------------------------------------------
*/
void
TkpGetFontFamilies(
Tcl_Interp *interp, /* Interp to hold result. */
Tk_Window tkwin) /* For display to query. */
{
int i, isNew, numNames;
char *family, **nameList;
Tcl_HashTable familyTable;
Tcl_HashEntry *hPtr;
Tcl_HashSearch search;
Tcl_Obj *resultPtr, *strPtr;
Tcl_InitHashTable(&familyTable, TCL_STRING_KEYS);
nameList = ListFonts(Tk_Display(tkwin), "*", &numNames);
for (i = 0; i < numNames; i++) {
char *familyEnd;
family = strchr(nameList[i] + 1, '-');
if (family == NULL) {
/*
* Apparently, sometimes ListFonts() can return a font name with
* zero or one '-' character in it. This is probably indicative of
* a server misconfiguration, but crashing because of it is a very
* bad idea anyway. [Bug 1475865]
*/
continue;
}
family++; /* Advance to char after '-'. */
familyEnd = strchr(family, '-');
if (familyEnd == NULL) {
continue; /* See comment above. */
}
*familyEnd = '\0';
Tcl_CreateHashEntry(&familyTable, family, &isNew);
}
XFreeFontNames(nameList);
hPtr = Tcl_FirstHashEntry(&familyTable, &search);
resultPtr = Tcl_NewObj();
while (hPtr != NULL) {
strPtr = Tcl_NewStringObj((const char *)Tcl_GetHashKey(&familyTable, hPtr), TCL_INDEX_NONE);
Tcl_ListObjAppendElement(NULL, resultPtr, strPtr);
hPtr = Tcl_NextHashEntry(&search);
}
Tcl_SetObjResult(interp, resultPtr);
Tcl_DeleteHashTable(&familyTable);
}
/*
*-------------------------------------------------------------------------
*
* TkpGetSubFonts --
*
* A function used by the testing package for querying the actual screen
* fonts that make up a font object.
*
* Results:
* Modifies interp's result object to hold a list containing the names of
* the screen fonts that make up the given font object.
*
* Side effects:
* None.
*
*-------------------------------------------------------------------------
*/
void
TkpGetSubFonts(
Tcl_Interp *interp,
Tk_Font tkfont)
{
int i;
Tcl_Obj *objv[3], *resultPtr, *listPtr;
UnixFont *fontPtr;
FontFamily *familyPtr;
resultPtr = Tcl_NewObj();
fontPtr = (UnixFont *) tkfont;
for (i = 0; i < fontPtr->numSubFonts; i++) {
familyPtr = fontPtr->subFontArray[i].familyPtr;
objv[0] = Tcl_NewStringObj(familyPtr->faceName, TCL_INDEX_NONE);
objv[1] = Tcl_NewStringObj(familyPtr->foundry, TCL_INDEX_NONE);
objv[2] = Tcl_NewStringObj(
Tcl_GetEncodingName(familyPtr->encoding), TCL_INDEX_NONE);
listPtr = Tcl_NewListObj(3, objv);
Tcl_ListObjAppendElement(NULL, resultPtr, listPtr);
}
Tcl_SetObjResult(interp, resultPtr);
}
/*
*----------------------------------------------------------------------
*
* TkpGetFontAttrsForChar --
*
* Retrieve the font attributes of the actual font used to render a given
* character.
*
* Results:
* None.
*
* Side effects:
* The font attributes are stored in *faPtr.
*
*----------------------------------------------------------------------
*/
void
TkpGetFontAttrsForChar(
Tk_Window tkwin, /* Window on the font's display */
Tk_Font tkfont, /* Font to query */
int c, /* Character of interest */
TkFontAttributes *faPtr) /* Output: Font attributes */
{
FontAttributes atts;
UnixFont *fontPtr = (UnixFont *) tkfont;
/* Structure describing the logical font */
SubFont *lastSubFontPtr = &fontPtr->subFontArray[0];
/* Pointer to subfont array in case
* FindSubFontForChar needs to fix up the
* memory allocation */
SubFont *thisSubFontPtr = FindSubFontForChar(fontPtr, c, &lastSubFontPtr);
/* Pointer to the subfont to use for the given
* character */
GetFontAttributes(Tk_Display(tkwin), thisSubFontPtr->fontStructPtr, &atts);
*faPtr = atts.fa;
}
/*
*---------------------------------------------------------------------------
*
* 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 bytes 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(
Tk_Font tkfont, /* Font in which characters will be drawn. */
const char *source, /* UTF-8 string to be displayed. Need not be
* '\0' terminated. */
Tcl_Size numBytes, /* Maximum number of bytes to consider from
* source string. */
int maxLength, /* If >= 0, maxLength specifies the longest
* permissible line length in pixels; 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;
SubFont *lastSubFontPtr;
Tcl_Size curByte;
int curX, ch;
/*
* Unix does not use kerning or fractional character widths when
* displaying text on the screen. So that means we can safely measure
* individual characters or spans of characters and add up the widths w/o
* any "off-by-one-pixel" errors.
*/
fontPtr = (UnixFont *) tkfont;
lastSubFontPtr = &fontPtr->subFontArray[0];
if (numBytes == 0) {
curX = 0;
curByte = 0;
} else if (maxLength < 0) {
const char *p, *end, *next;
SubFont *thisSubFontPtr;
FontFamily *familyPtr;
Tcl_DString runString;
/*
* A three step process:
* 1. Find a contiguous range of characters that can all be
* represented by a single screen font.
* 2. Convert those chars to the encoding of that font.
* 3. Measure converted chars.
*/
curX = 0;
end = source + numBytes;
for (p = source; p < end; ) {
next = p + Tcl_UtfToUniChar(p, &ch);
thisSubFontPtr = FindSubFontForChar(fontPtr, ch, &lastSubFontPtr);
if (thisSubFontPtr != lastSubFontPtr) {
familyPtr = lastSubFontPtr->familyPtr;
(void)Tcl_UtfToExternalDString(familyPtr->encoding, source,
p - source, &runString);
if (familyPtr->isTwoByteFont) {
curX += XTextWidth16(lastSubFontPtr->fontStructPtr,
(XChar2b *) Tcl_DStringValue(&runString),
Tcl_DStringLength(&runString) / 2);
} else {
curX += XTextWidth(lastSubFontPtr->fontStructPtr,
Tcl_DStringValue(&runString),
Tcl_DStringLength(&runString));
}
Tcl_DStringFree(&runString);
lastSubFontPtr = thisSubFontPtr;
source = p;
}
p = next;
}
familyPtr = lastSubFontPtr->familyPtr;
(void)Tcl_UtfToExternalDString(familyPtr->encoding, source, p - source,
&runString);
if (familyPtr->isTwoByteFont) {
curX += XTextWidth16(lastSubFontPtr->fontStructPtr,
(XChar2b *) Tcl_DStringValue(&runString),
Tcl_DStringLength(&runString) >> 1);
} else {
curX += XTextWidth(lastSubFontPtr->fontStructPtr,
Tcl_DStringValue(&runString),
Tcl_DStringLength(&runString));
}
Tcl_DStringFree(&runString);
curByte = numBytes;
} else {
const char *p, *end, *next, *term;
int newX, termX, sawNonSpace, dstWrote;
FontFamily *familyPtr;
XChar2b buf[8];
/*
* How many chars will fit in the space allotted? This first version
* may be inefficient because it measures every character
* individually.
*/
next = source + Tcl_UtfToUniChar(source, &ch);
newX = curX = termX = 0;
term = source;
end = source + numBytes;
sawNonSpace = (ch > 255) || !isspace(ch);
familyPtr = lastSubFontPtr->familyPtr;
for (p = source; ; ) {
if ((ch < BASE_CHARS) && (fontPtr->widths[ch] != 0)) {
newX += fontPtr->widths[ch];
} else {
lastSubFontPtr = FindSubFontForChar(fontPtr, ch, NULL);
familyPtr = lastSubFontPtr->familyPtr;
Tcl_UtfToExternal(NULL, familyPtr->encoding, p, next - p, TCL_ENCODING_PROFILE_TCL8, NULL,
(char *)&buf[0].byte1, sizeof(buf), NULL, &dstWrote, NULL);
if (familyPtr->isTwoByteFont) {
newX += XTextWidth16(lastSubFontPtr->fontStructPtr,
buf, dstWrote >> 1);
} else {
newX += XTextWidth(lastSubFontPtr->fontStructPtr,
(char *)&buf[0].byte1, dstWrote);
}
}
if (newX > maxLength) {
break;
}
curX = newX;
p = next;
if (p >= end) {
term = end;
termX = curX;
break;
}
next += Tcl_UtfToUniChar(next, &ch);
if ((ch < 256) && isspace(ch)) {
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) && (p < end) && (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.
*/
curX = newX;
p += Tcl_UtfToUniChar(p, &ch);
}
if ((flags & TK_AT_LEAST_ONE) && (term == source) && (p < end)) {
term = p;
termX = curX;
if (term == source) {
term += Tcl_UtfToUniChar(term, &ch);
termX = newX;
}
} else if ((p >= end) || !(flags & TK_WHOLE_WORDS)) {
term = p;
termX = curX;
}
curX = termX;
curByte = term - source;
}
*lengthPtr = curX;
return curByte;
}