2 * X11 physical font objects
4 * Copyright 1997 Alex Korobka
6 * TODO: Mapping algorithm tweaks, FO_SYNTH_... flags (ExtTextOut() will
7 * have to be changed for that), dynamic font loading (FreeType).
12 #include <X11/Xatom.h>
21 #include <sys/types.h>
33 #include "debugtools.h"
39 DEFAULT_DEBUG_CHANNEL(font);
41 #define X_PFONT_MAGIC (0xFADE0000)
42 #define X_FMC_MAGIC (0x0000CAFE)
44 #define MAX_FONTS 1024*16
45 #define MAX_LFD_LENGTH 256
49 #define DEF_POINT_SIZE 8 /* CreateFont(0 .. ) gets this */
50 #define DEF_SCALABLE_HEIGHT 100 /* pixels */
51 #define MIN_FONT_SIZE 2 /* Min size in pixels */
52 #define MAX_FONT_SIZE 1000 /* Max size in pixels */
54 #define REMOVE_SUBSETS 1
55 #define UNMARK_SUBSETS 0
58 #define FF_FAMILY (FF_MODERN | FF_SWISS | FF_ROMAN | FF_DECORATIVE | FF_SCRIPT)
60 typedef struct __fontAlias
64 struct __fontAlias* next;
67 static fontAlias *aliasTable = NULL;
69 UINT16 XTextCaps = TC_OP_CHARACTER | TC_OP_STROKE |
70 TC_CP_STROKE | TC_CR_ANY |
71 TC_SA_DOUBLE | TC_SA_INTEGER | TC_SA_CONTIN |
72 TC_UA_ABLE | TC_SO_ABLE | TC_RA_ABLE;
74 /* X11R6 adds TC_SF_X_YINDEP, maybe more... */
76 static const char* INIFontMetrics = "/cachedmetrics.";
77 static const char* INIFontSection = "fonts";
78 static const char* INIAliasSection = "Alias";
79 static const char* INIIgnoreSection = "Ignore";
80 static const char* INIDefault = "Default";
81 static const char* INIDefaultFixed = "DefaultFixed";
82 static const char* INIResolution = "Resolution";
83 static const char* INIGlobalMetrics = "FontMetrics";
84 static const char* INIDefaultSerif = "DefaultSerif";
85 static const char* INIDefaultSansSerif = "DefaultSansSerif";
88 /* FIXME - are there any more Latin charsets ? */
89 #define IS_LATIN_CHARSET(ch) \
90 ((ch)==ANSI_CHARSET ||\
92 (ch)==ISO3_CHARSET ||\
93 (ch)==ISO4_CHARSET ||\
94 (ch)==RUSSIAN_CHARSET ||\
95 (ch)==ARABIC_CHARSET ||\
96 (ch)==GREEK_CHARSET ||\
97 (ch)==HEBREW_CHARSET ||\
98 (ch)==TURKISH_CHARSET ||\
101 /* suffix-charset mapping tables - must be less than 254 entries long */
103 typedef struct __sufch
106 WORD charset; /* hibyte != 0 means *internal* charset */
111 static const SuffixCharset sufch_ansi[] = {
112 { "0", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
113 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
115 static const SuffixCharset sufch_iso646[] = {
116 { "irv", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
117 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
119 static const SuffixCharset sufch_iso8859[] = {
120 { "1", ANSI_CHARSET, 28591, X11DRV_CPTABLE_SBCS },
121 { "2", EE_CHARSET, 28592, X11DRV_CPTABLE_SBCS },
122 { "3", ISO3_CHARSET, 28593, X11DRV_CPTABLE_SBCS },
123 { "4", ISO4_CHARSET, 28594, X11DRV_CPTABLE_SBCS },
124 { "5", RUSSIAN_CHARSET, 28595, X11DRV_CPTABLE_SBCS },
125 { "6", ARABIC_CHARSET, 28596, X11DRV_CPTABLE_SBCS },
126 { "7", GREEK_CHARSET, 28597, X11DRV_CPTABLE_SBCS },
127 { "8", HEBREW_CHARSET, 28598, X11DRV_CPTABLE_SBCS },
128 { "9", TURKISH_CHARSET, 28599, X11DRV_CPTABLE_SBCS },
129 { "10", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS }, /* FIXME */
130 { "11", THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }, /* FIXME */
131 { "12", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
132 { "13", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
133 { "14", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
134 { "15", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
135 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
137 static const SuffixCharset sufch_microsoft[] = {
138 { "cp1250", EE_CHARSET, 1250, X11DRV_CPTABLE_SBCS },
139 { "cp1251", RUSSIAN_CHARSET, 1251, X11DRV_CPTABLE_SBCS },
140 { "cp1252", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
141 { "cp1253", GREEK_CHARSET, 1253, X11DRV_CPTABLE_SBCS },
142 { "cp1254", TURKISH_CHARSET, 1254, X11DRV_CPTABLE_SBCS },
143 { "cp1255", HEBREW_CHARSET, 1255, X11DRV_CPTABLE_SBCS },
144 { "cp1256", ARABIC_CHARSET, 1256, X11DRV_CPTABLE_SBCS },
145 { "cp1257", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS },
146 { "fontspecific", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
147 { "symbol", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
148 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
150 static const SuffixCharset sufch_tcvn[] = {
151 { "0", TCVN_CHARSET, 1252, X11DRV_CPTABLE_SBCS }, /* FIXME */
152 { NULL, TCVN_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
154 static const SuffixCharset sufch_tis620[] = {
155 { "0", THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }, /* FIXME */
156 { NULL, THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }};
158 static const SuffixCharset sufch_viscii[] = {
159 { "1", VISCII_CHARSET, 1252, X11DRV_CPTABLE_SBCS }, /* FIXME */
160 { NULL, VISCII_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
162 static const SuffixCharset sufch_windows[] = {
163 { "1250", EE_CHARSET, 1250, X11DRV_CPTABLE_SBCS },
164 { "1251", RUSSIAN_CHARSET, 1251, X11DRV_CPTABLE_SBCS },
165 { "1252", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
166 { "1253", GREEK_CHARSET, 1253, X11DRV_CPTABLE_SBCS },
167 { "1254", TURKISH_CHARSET, 1254, X11DRV_CPTABLE_SBCS },
168 { "1255", HEBREW_CHARSET, 1255, X11DRV_CPTABLE_SBCS },
169 { "1256", ARABIC_CHARSET, 1256, X11DRV_CPTABLE_SBCS },
170 { "1257", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS },
171 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
173 static const SuffixCharset sufch_koi8[] = {
174 { "r", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
175 { "u", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
176 { NULL, RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS }};
178 static const SuffixCharset sufch_jisx0201[] = {
179 { "0", X11FONT_JISX0201_CHARSET, 932, X11DRV_CPTABLE_SBCS },
180 { NULL, X11FONT_JISX0201_CHARSET, 932, X11DRV_CPTABLE_SBCS }};
182 static const SuffixCharset sufch_jisx0208[] = {
183 { "0", SHIFTJIS_CHARSET, 932, X11DRV_CPTABLE_CP932 },
184 { NULL, SHIFTJIS_CHARSET, 932, X11DRV_CPTABLE_CP932 }};
186 static const SuffixCharset sufch_jisx0212[] = {
187 { "0", X11FONT_JISX0212_CHARSET, 932, X11DRV_CPTABLE_CP932 },
188 { NULL, X11FONT_JISX0212_CHARSET, 932, X11DRV_CPTABLE_CP932 }};
190 static const SuffixCharset sufch_ksc5601[] = {
191 { "0", HANGEUL_CHARSET, 949, X11DRV_CPTABLE_CP949 },
192 { NULL, HANGEUL_CHARSET, 949, X11DRV_CPTABLE_CP949 }};
194 static const SuffixCharset sufch_gb2312[] = {
195 { "0", GB2312_CHARSET, 936, X11DRV_CPTABLE_CP936 },
196 { NULL, GB2312_CHARSET, 936, X11DRV_CPTABLE_CP936 }};
198 static const SuffixCharset sufch_big5[] = {
199 { "0", CHINESEBIG5_CHARSET, 950, X11DRV_CPTABLE_CP950 },
200 { NULL, CHINESEBIG5_CHARSET, 950, X11DRV_CPTABLE_CP950 }};
202 static const SuffixCharset sufch_unicode[] = {
203 { "0", DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE },
204 { NULL, DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE }};
206 static const SuffixCharset sufch_iso10646[] = {
207 { "1", DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE },
208 { NULL, DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE }};
210 /* Each of these must be matched explicitly */
211 static const SuffixCharset sufch_any[] = {
212 { "fontspecific", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
213 { NULL, 0, 0, X11DRV_CPTABLE_SBCS }};
219 const SuffixCharset* sufch;
221 } fontEncodingTemplate;
223 /* Note: we can attach additional encoding mappings to the end
224 * of this table at runtime.
226 static fontEncodingTemplate __fETTable[] = {
227 { "ansi", sufch_ansi, &__fETTable[1] },
228 { "ascii", sufch_ansi, &__fETTable[2] },
229 { "iso646.1991", sufch_iso646, &__fETTable[3] },
230 { "iso8859", sufch_iso8859, &__fETTable[4] },
231 { "microsoft", sufch_microsoft, &__fETTable[5] },
232 { "tcvn", sufch_tcvn, &__fETTable[6] },
233 { "tis620.2533", sufch_tis620, &__fETTable[7] },
234 { "viscii1.1", sufch_viscii, &__fETTable[8] },
235 { "windows", sufch_windows, &__fETTable[9] },
236 { "koi8", sufch_koi8, &__fETTable[10]},
237 { "jisx0201.1976",sufch_jisx0201, &__fETTable[11]},
238 { "jisc6226.1978",sufch_jisx0208, &__fETTable[12]},
239 { "jisx0208.1983",sufch_jisx0208, &__fETTable[13]},
240 { "jisx0208.1990",sufch_jisx0208, &__fETTable[14]},
241 { "jisx0212.1990",sufch_jisx0212, &__fETTable[15]},
242 { "ksc5601.1987", sufch_ksc5601, &__fETTable[16]},
243 { "gb2312.1980", sufch_gb2312, &__fETTable[17]},
244 { "big5.et", sufch_big5, &__fETTable[18]},
245 { "unicode", sufch_unicode, &__fETTable[19]},
246 { "iso10646", sufch_iso10646, &__fETTable[20]},
247 { "cp", sufch_windows, &__fETTable[21]},
248 /* NULL prefix matches anything so put it last */
249 { NULL, sufch_any, NULL },
251 static fontEncodingTemplate* fETTable = __fETTable;
253 /* a charset database for known facenames */
254 struct CharsetBindingInfo
256 const char* pszFaceName;
259 static const struct CharsetBindingInfo charsetbindings[] =
261 /* special facenames */
262 { "System", DEFAULT_CHARSET },
263 { "FixedSys", DEFAULT_CHARSET },
265 /* known facenames */
266 { "MS Serif", ANSI_CHARSET },
267 { "MS Sans Serif", ANSI_CHARSET },
268 { "Courier", ANSI_CHARSET },
269 { "Symbol", SYMBOL_CHARSET },
271 { "Arial", ANSI_CHARSET },
272 { "Arial Greek", GREEK_CHARSET },
273 { "Arial Tur", TURKISH_CHARSET },
274 { "Arial Baltic", BALTIC_CHARSET },
275 { "Arial CE", EASTEUROPE_CHARSET },
276 { "Arial Cyr", RUSSIAN_CHARSET },
277 { "Courier New", ANSI_CHARSET },
278 { "Courier New Greek", GREEK_CHARSET },
279 { "Courier New Tur", TURKISH_CHARSET },
280 { "Courier New Baltic", BALTIC_CHARSET },
281 { "Courier New CE", EASTEUROPE_CHARSET },
282 { "Courier New Cyr", RUSSIAN_CHARSET },
283 { "Times New Roman", ANSI_CHARSET },
284 { "Times New Roman Greek", GREEK_CHARSET },
285 { "Times New Roman Tur", TURKISH_CHARSET },
286 { "Times New Roman Baltic", BALTIC_CHARSET },
287 { "Times New Roman CE", EASTEUROPE_CHARSET },
288 { "Times New Roman Cyr", RUSSIAN_CHARSET },
290 { "\x82\x6c\x82\x72 \x83\x53\x83\x56\x83\x62\x83\x4e",
291 SHIFTJIS_CHARSET }, /* MS gothic */
292 { "\x82\x6c\x82\x72 \x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e",
293 SHIFTJIS_CHARSET }, /* MS P gothic */
294 { "\x82\x6c\x82\x72 \x96\xbe\x92\xa9",
295 SHIFTJIS_CHARSET }, /* MS mincho */
296 { "\x82\x6c\x82\x72 \x82\x6f\x96\xbe\x92\xa9",
297 SHIFTJIS_CHARSET }, /* MS P mincho */
298 { "GulimChe", HANGEUL_CHARSET },
299 { "MS Song", GB2312_CHARSET },
300 { "MS Hei", GB2312_CHARSET },
306 static int DefResolution = 0;
308 static CRITICAL_SECTION crtsc_fonts_X11 = CRITICAL_SECTION_INIT;
310 static fontResource* fontList = NULL;
311 static fontObject* fontCache = NULL; /* array */
312 static int fontCacheSize = FONTCACHE;
313 static int fontLF = -1, fontMRU = -1; /* last free, most recently used */
315 #define __PFONT(pFont) ( fontCache + ((UINT)(pFont) & 0x0000FFFF) )
316 #define CHECK_PFONT(pFont) ( (((UINT)(pFont) & 0xFFFF0000) == X_PFONT_MAGIC) &&\
317 (((UINT)(pFont) & 0x0000FFFF) < fontCacheSize) )
319 static Atom RAW_ASCENT;
320 static Atom RAW_DESCENT;
322 /***********************************************************************
323 * Helper macros from X distribution
326 #define CI_NONEXISTCHAR(cs) (((cs)->width == 0) && \
327 (((cs)->rbearing|(cs)->lbearing| \
328 (cs)->ascent|(cs)->descent) == 0))
330 #define CI_GET_CHAR_INFO(fs,col,def,cs) \
333 if (col >= fs->min_char_or_byte2 && col <= fs->max_char_or_byte2) { \
334 if (fs->per_char == NULL) { \
335 cs = &fs->min_bounds; \
337 cs = &fs->per_char[(col - fs->min_char_or_byte2)]; \
338 if (CI_NONEXISTCHAR(cs)) cs = def; \
343 #define CI_GET_DEFAULT_INFO(fs,cs) \
344 CI_GET_CHAR_INFO(fs, fs->default_char, NULL, cs)
346 /***********************************************************************
349 static UINT16 __lfCheckSum( LPLOGFONT16 plf )
351 CHAR font[LF_FACESIZE];
357 for (i = 0; i < 9; i++) checksum ^= *ptr++;
358 for (i = 0; i < LF_FACESIZE; i++)
360 font[i] = tolower(plf->lfFaceName[i]);
361 if (!font[i] || font[i] == ' ') break;
363 for (ptr = (UINT16 *)font, i >>= 1; i > 0; i-- ) checksum ^= *ptr++;
367 static UINT16 __genericCheckSum( const void *ptr, int size )
369 unsigned int checksum = 0;
370 const char *p = (const char *)ptr;
372 checksum ^= (checksum << 3) + (checksum >> 29) + *p++;
374 return checksum & 0xffff;
377 /*************************************************************************
378 * LFD parse/compose routines
380 * NB. LFD_Parse will use lpFont for its own ends, so if you want to keep it
383 * These functions also do TILDE to HYPHEN conversion
385 static LFD* LFD_Parse(LPSTR lpFont)
388 char *lpch = lpFont, *lfd_fld[LFD_FIELDS], *field_start;
392 WARN("font '%s' doesn't begin with '%c'\n", lpFont, HYPHEN);
396 field_start = ++lpch;
397 for( i = 0; i < LFD_FIELDS; )
402 lfd_fld[i] = field_start;
404 field_start = ++lpch;
408 lfd_fld[i] = field_start;
412 else if (*lpch == TILDE)
420 /* Fill in the empty fields with NULLS */
421 for (; i< LFD_FIELDS; i++)
424 WARN("Extra ignored in font '%s'\n", lpFont);
426 lfd = HeapAlloc( GetProcessHeap(), 0, sizeof(LFD) );
429 lfd->foundry = lfd_fld[0];
430 lfd->family = lfd_fld[1];
431 lfd->weight = lfd_fld[2];
432 lfd->slant = lfd_fld[3];
433 lfd->set_width = lfd_fld[4];
434 lfd->add_style = lfd_fld[5];
435 lfd->pixel_size = lfd_fld[6];
436 lfd->point_size = lfd_fld[7];
437 lfd->resolution_x = lfd_fld[8];
438 lfd->resolution_y = lfd_fld[9];
439 lfd->spacing = lfd_fld[10];
440 lfd->average_width = lfd_fld[11];
441 lfd->charset_registry = lfd_fld[12];
442 lfd->charset_encoding = lfd_fld[13];
448 static void LFD_UnParse(LPSTR dp, UINT buf_size, LFD* lfd)
450 const char* lfd_fld[LFD_FIELDS];
454 return; /* Dont be silly */
456 lfd_fld[0] = lfd->foundry;
457 lfd_fld[1] = lfd->family;
458 lfd_fld[2] = lfd->weight ;
459 lfd_fld[3] = lfd->slant ;
460 lfd_fld[4] = lfd->set_width ;
461 lfd_fld[5] = lfd->add_style;
462 lfd_fld[6] = lfd->pixel_size;
463 lfd_fld[7] = lfd->point_size;
464 lfd_fld[8] = lfd->resolution_x;
465 lfd_fld[9] = lfd->resolution_y ;
466 lfd_fld[10] = lfd->spacing ;
467 lfd_fld[11] = lfd->average_width ;
468 lfd_fld[12] = lfd->charset_registry ;
469 lfd_fld[13] = lfd->charset_encoding ;
471 buf_size--; /* Room for the terminator */
473 for (i = 0; i < LFD_FIELDS; i++)
475 const char* sp = lfd_fld[i];
476 if (!sp || !buf_size)
481 while (buf_size > 0 && *sp)
483 *dp = (*sp == HYPHEN) ? TILDE : *sp;
492 static void LFD_GetWeight( fontInfo* fi, LPCSTR lpStr)
494 int j = strlen(lpStr);
495 if( j == 1 && *lpStr == '0')
496 fi->fi_flags |= FI_POLYWEIGHT;
499 if( !strcasecmp( "bold", lpStr) )
500 fi->df.dfWeight = FW_BOLD;
501 else if( !strcasecmp( "demi", lpStr) )
503 fi->fi_flags |= FI_FW_DEMI;
504 fi->df.dfWeight = FW_DEMIBOLD;
506 else if( !strcasecmp( "book", lpStr) )
508 fi->fi_flags |= FI_FW_BOOK;
509 fi->df.dfWeight = FW_REGULAR;
514 if( !strcasecmp( "light", lpStr) )
515 fi->df.dfWeight = FW_LIGHT;
516 else if( !strcasecmp( "black", lpStr) )
517 fi->df.dfWeight = FW_BLACK;
519 else if( j == 6 && !strcasecmp( "medium", lpStr) )
520 fi->df.dfWeight = FW_REGULAR;
521 else if( j == 8 && !strcasecmp( "demibold", lpStr) )
522 fi->df.dfWeight = FW_DEMIBOLD;
524 fi->df.dfWeight = FW_DONTCARE; /* FIXME: try to get something
525 * from the weight property */
528 static BOOL LFD_GetSlant( fontInfo* fi, LPCSTR lpStr)
530 int l = strlen(lpStr);
533 switch( tolower( *lpStr ) )
535 case '0': fi->fi_flags |= FI_POLYSLANT; /* haven't seen this one yet */
537 case 'r': fi->df.dfItalic = 0;
540 fi->fi_flags |= FI_OBLIQUE;
541 case 'i': fi->df.dfItalic = 1;
549 static void LFD_GetStyle( fontInfo* fi, LPCSTR lpstr, int dec_style_check)
551 int j = strlen(lpstr);
552 if( j > 3 ) /* find out is there "sans" or "script" */
556 if( strstr(lpstr, "sans") )
558 fi->df.dfPitchAndFamily |= FF_SWISS;
561 if( strstr(lpstr, "script") )
563 fi->df.dfPitchAndFamily |= FF_SCRIPT;
566 if( !j && dec_style_check )
567 fi->df.dfPitchAndFamily |= FF_DECORATIVE;
571 /*************************************************************************
576 * Fill in some fields in the fontInfo struct.
578 static int LFD_InitFontInfo( fontInfo* fi, const LFD* lfd, LPCSTR fullname )
580 int i, j, dec_style_check, scalability;
581 fontEncodingTemplate* boba;
582 const char* ridiculous = "font '%s' has ridiculous %s\n";
585 if (!lfd->charset_registry)
587 WARN("font '%s' does not have enough fields\n", fullname);
591 memset(fi, 0, sizeof(fontInfo) );
594 LFD_GetWeight( fi, lfd->weight);
597 dec_style_check = LFD_GetSlant( fi, lfd->slant);
600 lpstr = lfd->set_width;
601 if( strcasecmp( "normal", lpstr) ) /* XXX 'narrow', 'condensed', etc... */
602 dec_style_check = TRUE;
604 fi->fi_flags |= FI_NORMAL;
607 LFD_GetStyle(fi, lfd->add_style, dec_style_check);
609 /* pixel & decipoint height, and res_x & y */
613 j = strlen(lfd->pixel_size);
614 if( j == 0 || j > 3 )
616 WARN(ridiculous, fullname, "pixel_size");
619 if( !(fi->lfd_height = atoi(lfd->pixel_size)) )
622 j = strlen(lfd->point_size);
623 if( j == 0 || j > 3 )
625 WARN(ridiculous, fullname, "point_size");
628 if( !(atoi(lfd->point_size)) )
631 j = strlen(lfd->resolution_x);
632 if( j == 0 || j > 3 )
634 WARN(ridiculous, fullname, "resolution_x");
637 if( !(fi->lfd_resolution = atoi(lfd->resolution_x)) )
640 j = strlen(lfd->resolution_y);
641 if( j == 0 || j > 3 )
643 WARN(ridiculous, fullname, "resolution_y");
646 if( !(atoi(lfd->resolution_y)) )
649 /* Check scalability */
654 case 4: /* Scalable */
655 fi->fi_flags |= FI_SCALABLE;
658 /* #$%^!!! X11R6 mutant garbage (scalable bitmap) */
659 TRACE("Skipping scalable bitmap '%s'\n", fullname);
662 WARN("Font '%s' has weird scalability\n", fullname);
667 lpstr = lfd->spacing;
671 WARN("font '%s' has no spacing\n", fullname);
674 case 'p': fi->fi_flags |= FI_VARIABLEPITCH;
676 case 'c': fi->df.dfPitchAndFamily |= FF_MODERN;
677 fi->fi_flags |= FI_FIXEDEX;
679 case 'm': fi->fi_flags |= FI_FIXEDPITCH;
682 /* Of course this line does nothing */
683 fi->df.dfPitchAndFamily |= DEFAULT_PITCH | FF_DONTCARE;
686 /* average width - */
687 lpstr = lfd->average_width;
689 if( j == 0 || j > 3 )
691 WARN(ridiculous, fullname, "average_width");
695 if( !(atoi(lpstr)) && !scalability )
697 WARN("font '%s' has average_width 0 but is not scalable!!\n", fullname);
701 /* charset registry, charset encoding - */
702 lpstr = lfd->charset_registry;
703 if( strstr(lpstr, "ksc") ||
704 strstr(lpstr, "gb2312") ||
705 strstr(lpstr, "big5") )
707 FIXME("DBCS fonts like '%s' are not working correctly now.\n", fullname);
710 fi->df.dfCharSet = ANSI_CHARSET;
712 for( i = 0, boba = fETTable; boba; boba = boba->next, i++ )
714 if (!boba->prefix || !strcasecmp(lpstr, boba->prefix))
716 if (lfd->charset_encoding)
718 for( j = 0; boba->sufch[j].psuffix; j++ )
720 if( !strcasecmp(lfd->charset_encoding, boba->sufch[j].psuffix ))
722 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
723 fi->internal_charset = boba->sufch[j].charset;
724 fi->codepage = boba->sufch[j].codepage;
725 fi->cptable = boba->sufch[j].cptable;
730 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
731 fi->internal_charset = boba->sufch[j].charset;
732 fi->codepage = boba->sufch[j].codepage;
733 fi->cptable = boba->sufch[j].cptable;
736 FIXME("font '%s' has unknown character encoding '%s' in known registry '%s'\n",
737 fullname, lfd->charset_encoding, boba->prefix);
742 FIXME("font '%s' has unknown registry '%s' and character encoding '%s' \n",
743 fullname, lfd->charset_registry, lfd->charset_encoding);
747 WARN("Defaulting to: df.dfCharSet = %d, internal_charset = %d, codepage = %d, cptable = %d\n",
748 fi->df.dfCharSet,fi->internal_charset, fi->codepage, fi->cptable);
751 else if (boba->prefix)
753 WARN("font '%s' has known registry '%s' and no character encoding\n",
755 for( j = 0; boba->sufch[j].psuffix; j++ )
757 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
758 fi->internal_charset = boba->sufch[j].charset;
759 fi->codepage = boba->sufch[j].codepage;
760 fi->cptable = boba->sufch[j].cptable;
766 WARN("font '%s' has unknown character set '%s'\n", fullname, lpstr);
770 /* i - index into fETTable
771 * j - index into suffix array for fETTable[i]
773 * 254 - found encoding prefix, unknown suffix
774 * 255 - no encoding match at all.
776 fi->fi_encoding = 256 * (UINT16)i + (UINT16)j;
782 /*************************************************************************
785 * make a matrix suitable for LFD based on theta radians
787 static void LFD_AngleMatrix( char* buffer, int h, double theta)
790 matrix[0] = h*cos(theta);
791 matrix[1] = h*sin(theta);
792 matrix[2] = -h*sin(theta);
793 matrix[3] = h*cos(theta);
794 sprintf(buffer, "[%+f%+f%+f%+f]", matrix[0], matrix[1], matrix[2], matrix[3]);
797 /*************************************************************************
800 * Note: uRelax is a treatment not a cure. Font mapping algorithm
801 * should be bulletproof enough to allow us to avoid hacks like
802 * this despite LFD being so braindead.
804 static BOOL LFD_ComposeLFD( const fontObject* fo,
805 INT height, LPSTR lpLFD, UINT uRelax )
809 char h_string[64], resx_string[64], resy_string[64];
812 /* Get the worst case over with first */
814 /* RealizeFont() will call us repeatedly with increasing uRelax
815 * until XLoadFont() succeeds.
816 * to avoid an infinite loop; these will always match
821 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-1" );
823 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" );
827 /* read foundry + family from fo */
828 aLFD.foundry = fo->fr->resource->foundry;
829 aLFD.family = fo->fr->resource->family;
832 switch( fo->fi->df.dfWeight )
835 aLFD.weight = "bold"; break;
837 if( fo->fi->fi_flags & FI_FW_BOOK )
838 aLFD.weight = "book";
840 aLFD.weight = "medium";
843 aLFD.weight = "demi";
844 if( !( fo->fi->fi_flags & FI_FW_DEMI) )
845 aLFD.weight = "bold";
848 aLFD.weight = "black"; break;
850 aLFD.weight = "light"; break;
856 if( fo->fi->df.dfItalic )
857 if( fo->fi->fi_flags & FI_OBLIQUE )
862 aLFD.slant = (uRelax < 1) ? "r" : any;
865 if( fo->fi->fi_flags & FI_NORMAL )
866 aLFD.set_width = "normal";
868 aLFD.set_width = any;
871 aLFD.add_style = any;
875 * FIXME: fill in lpXForm and lpPixmap for rotated fonts
877 if( fo->fo_flags & FO_SYNTH_HEIGHT )
878 h = fo->fi->lfd_height;
881 h = (fo->fi->lfd_height * height + (fo->fi->df.dfPixHeight>>1));
882 h /= fo->fi->df.dfPixHeight;
884 if (h < MIN_FONT_SIZE) /* Resist rounding down to 0 */
886 else if (h > MAX_FONT_SIZE) /* Sanity check as huge fonts can lock up the font server */
888 WARN("Huge font size %d pixels has been reduced to %d\n", h, MAX_FONT_SIZE);
893 /* handle rotated fonts */
894 if (fo->lf.lfEscapement) {
895 /* escapement is in tenths of degrees, theta is in radians */
896 double theta = M_PI*fo->lf.lfEscapement/1800.;
897 LFD_AngleMatrix(h_string, h, theta);
901 sprintf(h_string, "%d", h);
904 sprintf(h_string, "%d", fo->fi->lfd_height);
906 aLFD.pixel_size = h_string;
907 aLFD.point_size = any;
909 /* resolution_x,y, average width */
910 /* FOX ME - Why do some font servers ignore average width ?
911 * so that you have to mess around with res_y
913 aLFD.average_width = any;
916 sprintf(resx_string, "%d", fo->fi->lfd_resolution);
917 aLFD.resolution_x = resx_string;
919 strcpy(resy_string, resx_string);
920 if( uRelax == 0 && XTextCaps & TC_SF_X_YINDEP )
922 if( fo->lf.lfWidth && !(fo->fo_flags & FO_SYNTH_WIDTH))
924 int resy = 0.5 + fo->fi->lfd_resolution *
925 (fo->fi->df.dfAvgWidth * height) /
926 (fo->lf.lfWidth * fo->fi->df.dfPixHeight) ;
927 sprintf(resy_string, "%d", resy);
931 /* FIXME - synth width */
934 aLFD.resolution_y = resy_string;
938 aLFD.resolution_x = aLFD.resolution_y = any;
945 if( fo->fi->fi_flags & FI_FIXEDPITCH )
946 w = ( fo->fi->fi_flags & FI_FIXEDEX ) ? "c" : "m";
948 w = ( fo->fi->fi_flags & FI_VARIABLEPITCH ) ? "p" : any;
950 aLFD.spacing = (uRelax <= 1) ? w : any;
957 fontEncodingTemplate* boba;
959 i = fo->fi->fi_encoding >> 8;
960 for( boba = fETTable; i; i--, boba = boba->next );
962 aLFD.charset_registry = boba->prefix ? boba->prefix : any;
964 i = fo->fi->fi_encoding & 255;
968 aLFD.charset_encoding = boba->sufch[i].psuffix;
972 aLFD.charset_encoding = any;
975 case 255: /* no suffix - it ends eg "-ascii" */
976 aLFD.charset_encoding = NULL;
982 aLFD.charset_registry = any;
983 aLFD.charset_encoding = any;
986 LFD_UnParse(lpLFD, MAX_LFD_LENGTH, &aLFD);
988 TRACE("\tLFD(uRelax=%d): %s\n", uRelax, lpLFD );
993 /***********************************************************************
996 * font info - http://www.microsoft.com/kb/articles/q65/1/23.htm
997 * Windows font metrics - http://www.microsoft.com/kb/articles/q32/6/67.htm
999 static void XFONT_GetLeading( const LPIFONTINFO16 pFI, const XFontStruct* x_fs,
1000 INT16* pIL, INT16* pEL, const XFONTTRANS *XFT )
1002 unsigned long height;
1003 unsigned min = (unsigned char)pFI->dfFirstChar;
1004 BOOL bIsLatin = IS_LATIN_CHARSET(pFI->dfCharSet);
1009 Atom RAW_CAP_HEIGHT = TSXInternAtom(display, "RAW_CAP_HEIGHT", TRUE);
1010 if(TSXGetFontProperty((XFontStruct*)x_fs, RAW_CAP_HEIGHT, &height))
1011 *pIL = XFT->ascent -
1012 (INT)(XFT->pixelsize / 1000.0 * height);
1018 if( TSXGetFontProperty((XFontStruct*)x_fs, XA_CAP_HEIGHT, &height) == FALSE )
1020 if( x_fs->per_char )
1022 height = x_fs->per_char['X' - min].ascent;
1024 if (x_fs->ascent >= x_fs->max_bounds.ascent)
1025 height = x_fs->max_bounds.ascent;
1028 height = x_fs->ascent;
1030 *pEL = x_fs->max_bounds.ascent - height;
1033 height = x_fs->min_bounds.ascent;
1036 *pIL = x_fs->ascent - height;
1039 /***********************************************************************
1042 static int XFONT_CharWidth(const XFontStruct* x_fs,
1043 const XFONTTRANS *XFT, int ch)
1046 return x_fs->per_char[ch].width;
1048 return x_fs->per_char[ch].attributes * XFT->pixelsize / 1000.0;
1051 /***********************************************************************
1052 * XFONT_GetAvgCharWidth
1054 static INT XFONT_GetAvgCharWidth( LPIFONTINFO16 pFI, const XFontStruct* x_fs,
1055 const XFONTTRANS *XFT)
1057 unsigned min = (unsigned char)pFI->dfFirstChar;
1058 unsigned max = (unsigned char)pFI->dfLastChar;
1062 if( x_fs->per_char )
1064 int width = 0, chars = 0, j;
1065 if( IS_LATIN_CHARSET(pFI->dfCharSet) ||
1066 pFI->dfCharSet == DEFAULT_CHARSET )
1068 /* FIXME - should use a weighted average */
1069 for( j = 0; j < 26; j++ )
1070 width += XFONT_CharWidth(x_fs, XFT, 'a' + j - min) +
1071 XFONT_CharWidth(x_fs, XFT, 'A' + j - min);
1074 else /* unweighted average of everything */
1076 for( j = 0, max -= min; j <= max; j++ )
1077 if( !CI_NONEXISTCHAR(x_fs->per_char + j) )
1079 width += XFONT_CharWidth(x_fs, XFT, j);
1083 if (chars) avg = (width + (chars>>1))/ chars;
1084 else avg = 0; /* No characters exist at all */
1086 else /* uniform width */
1087 avg = x_fs->min_bounds.width;
1092 /***********************************************************************
1093 * XFONT_GetMaxCharWidth
1095 static INT XFONT_GetMaxCharWidth(const XFontStruct* xfs, const XFONTTRANS *XFT)
1097 unsigned min = (unsigned char)xfs->min_char_or_byte2;
1098 unsigned max = (unsigned char)xfs->max_char_or_byte2;
1101 if(!XFT || !xfs->per_char)
1102 return abs(xfs->max_bounds.width);
1104 for( j = 0, maxwidth = 0, max -= min; j <= max; j++ )
1105 if( !CI_NONEXISTCHAR(xfs->per_char + j) )
1106 if(maxwidth < xfs->per_char[j].attributes)
1107 maxwidth = xfs->per_char[j].attributes;
1109 maxwidth *= XFT->pixelsize / 1000.0;
1113 /***********************************************************************
1114 * XFONT_SetFontMetric
1118 * Initializes IFONTINFO16.
1120 static void XFONT_SetFontMetric(fontInfo* fi, const fontResource* fr, XFontStruct* xfs)
1123 fi->df.dfFirstChar = (BYTE)(min = xfs->min_char_or_byte2);
1124 fi->df.dfLastChar = (BYTE)(max = xfs->max_char_or_byte2);
1126 fi->df.dfDefaultChar = (BYTE)xfs->default_char;
1127 fi->df.dfBreakChar = (BYTE)(( ' ' < min || ' ' > max) ? xfs->default_char: ' ');
1129 fi->df.dfPixHeight = (INT16)((fi->df.dfAscent = (INT16)xfs->ascent) + xfs->descent);
1130 fi->df.dfPixWidth = (xfs->per_char) ? 0 : xfs->min_bounds.width;
1132 XFONT_GetLeading( &fi->df, xfs, &fi->df.dfInternalLeading, &fi->df.dfExternalLeading, NULL );
1133 fi->df.dfAvgWidth = (INT16)XFONT_GetAvgCharWidth(&fi->df, xfs, NULL );
1134 fi->df.dfMaxWidth = (INT16)XFONT_GetMaxCharWidth(xfs, NULL);
1136 if( xfs->min_bounds.width != xfs->max_bounds.width )
1137 fi->df.dfPitchAndFamily |= TMPF_FIXED_PITCH; /* au contraire! */
1138 if( fi->fi_flags & FI_SCALABLE )
1140 fi->df.dfType = DEVICE_FONTTYPE;
1141 fi->df.dfPitchAndFamily |= TMPF_DEVICE;
1143 else if( fi->fi_flags & FI_TRUETYPE )
1144 fi->df.dfType = TRUETYPE_FONTTYPE;
1146 fi->df.dfType = RASTER_FONTTYPE;
1148 fi->df.dfFace = fr->lfFaceName;
1151 /***********************************************************************
1152 * XFONT_GetFontMetric
1154 * Retrieve font metric info (enumeration).
1156 static UINT XFONT_GetFontMetric( const fontInfo* pfi, const LPENUMLOGFONTEX16 pLF,
1157 const LPNEWTEXTMETRIC16 pTM )
1159 memset( pLF, 0, sizeof(*pLF) );
1160 memset( pTM, 0, sizeof(*pTM) );
1162 #define plf ((LPLOGFONT16)pLF)
1163 plf->lfHeight = pTM->tmHeight = pfi->df.dfPixHeight;
1164 plf->lfWidth = pTM->tmAveCharWidth = pfi->df.dfAvgWidth;
1165 plf->lfWeight = pTM->tmWeight = pfi->df.dfWeight;
1166 plf->lfItalic = pTM->tmItalic = pfi->df.dfItalic;
1167 plf->lfUnderline = pTM->tmUnderlined = pfi->df.dfUnderline;
1168 plf->lfStrikeOut = pTM->tmStruckOut = pfi->df.dfStrikeOut;
1169 plf->lfCharSet = pTM->tmCharSet = pfi->df.dfCharSet;
1171 /* convert pitch values */
1173 pTM->tmPitchAndFamily = pfi->df.dfPitchAndFamily;
1174 plf->lfPitchAndFamily = (pfi->df.dfPitchAndFamily & 0xF1) + 1;
1176 lstrcpynA( plf->lfFaceName, pfi->df.dfFace, LF_FACESIZE );
1179 /* FIXME: fill in rest of plF values
1180 lstrcpynA(plF->elfFullName, , LF_FULLFACESIZE);
1181 lstrcpynA(plF->elfStyle, , LF_FACESIZE);
1182 lstrcpynA(plF->elfScript, , LF_FACESIZE);
1185 pTM->tmAscent = pfi->df.dfAscent;
1186 pTM->tmDescent = pTM->tmHeight - pTM->tmAscent;
1187 pTM->tmInternalLeading = pfi->df.dfInternalLeading;
1188 pTM->tmMaxCharWidth = pfi->df.dfMaxWidth;
1189 pTM->tmDigitizedAspectX = pfi->df.dfHorizRes;
1190 pTM->tmDigitizedAspectY = pfi->df.dfVertRes;
1192 pTM->tmFirstChar = pfi->df.dfFirstChar;
1193 pTM->tmLastChar = pfi->df.dfLastChar;
1194 pTM->tmDefaultChar = pfi->df.dfDefaultChar;
1195 pTM->tmBreakChar = pfi->df.dfBreakChar;
1197 /* return font type */
1199 return pfi->df.dfType;
1203 /***********************************************************************
1208 * dfPitchAndFamily flags for some common typefaces.
1210 static BYTE XFONT_FixupFlags( LPCSTR lfFaceName )
1212 switch( lfFaceName[0] )
1215 case 'A': if(!strncasecmp(lfFaceName, "Arial", 5) )
1219 case 'H': if(!strcasecmp(lfFaceName, "Helvetica") )
1223 case 'C': if(!strncasecmp(lfFaceName, "Courier", 7))
1226 if (!strcasecmp(lfFaceName, "Charter") )
1230 case 'P': if( !strcasecmp(lfFaceName,"Palatino") )
1234 case 'T': if(!strncasecmp(lfFaceName, "Times", 5) )
1238 case 'U': if(!strcasecmp(lfFaceName, "Utopia") )
1242 case 'Z': if(!strcasecmp(lfFaceName, "Zapf Dingbats") )
1243 return FF_DECORATIVE;
1248 /***********************************************************************
1249 * XFONT_SameFoundryAndFamily
1253 static BOOL XFONT_SameFoundryAndFamily( const LFD* lfd1, const LFD* lfd2 )
1255 return ( !strcasecmp( lfd1->foundry, lfd2->foundry ) &&
1256 !strcasecmp( lfd1->family, lfd2->family ) );
1259 /***********************************************************************
1260 * XFONT_InitialCapitals
1264 * Upper case first letters of words & remove multiple spaces
1266 static void XFONT_InitialCapitals(LPSTR lpch)
1272 for( i = 0, up = TRUE; *lpch; lpch++, i++ )
1274 if( isspace(*lpch) )
1276 if (!up) /* Not already got one */
1282 else if( isalpha(*lpch) && up )
1284 *lpstr++ = toupper(*lpch);
1294 /* Remove possible trailing space */
1301 /***********************************************************************
1302 * XFONT_WindowsNames
1306 * Build generic Windows aliases for X font names.
1308 * -misc-fixed- -> "Fixed"
1309 * -sony-fixed- -> "Sony Fixed", etc...
1311 static void XFONT_WindowsNames(void)
1315 for( fr = fontList; fr ; fr = fr->next )
1320 if( fr->fr_flags & FR_NAMESET ) continue; /* skip already assigned */
1322 for( pfr = fontList; pfr != fr ; pfr = pfr->next )
1323 if( pfr->fr_flags & FR_NAMESET )
1325 if (!strcasecmp( pfr->resource->family, fr->resource->family))
1329 lpch = fr->lfFaceName;
1330 snprintf( fr->lfFaceName, sizeof(fr->lfFaceName), "%s %s",
1331 /* prepend vendor name */
1332 (pfr==fr) ? "" : fr->resource->foundry,
1333 fr->resource->family);
1334 XFONT_InitialCapitals(fr->lfFaceName);
1336 BYTE bFamilyStyle = XFONT_FixupFlags( fr->lfFaceName );
1340 for( fi = fr->fi ; fi ; fi = fi->next )
1341 fi->df.dfPitchAndFamily |= bFamilyStyle;
1345 TRACE("typeface '%s'\n", fr->lfFaceName);
1347 fr->fr_flags |= FR_NAMESET;
1351 /***********************************************************************
1352 * XFONT_LoadDefaultLFD
1354 * Move lfd to the head of fontList to make it more likely to be matched
1356 static void XFONT_LoadDefaultLFD(LFD* lfd, LPCSTR fonttype)
1359 fontResource *fr, *pfr;
1360 for( fr = NULL, pfr = fontList; pfr; pfr = pfr->next )
1362 if( XFONT_SameFoundryAndFamily(pfr->resource, lfd) )
1366 fr->next = pfr->next;
1367 pfr->next = fontList;
1375 WARN("Default %sfont '-%s-%s-' not available\n", fonttype,
1376 lfd->foundry, lfd->family);
1380 /***********************************************************************
1383 static void XFONT_LoadDefault(LPCSTR ini, LPCSTR fonttype)
1385 char buffer[MAX_LFD_LENGTH];
1387 if( PROFILE_GetWineIniString( INIFontSection, ini, "", buffer, sizeof buffer ) )
1393 while( *pch && isspace(*pch) ) pch++;
1395 TRACE("Using '%s' as default %sfont\n", pch, fonttype);
1396 lfd = LFD_Parse(pch);
1397 if (lfd && lfd->foundry && lfd->family)
1398 XFONT_LoadDefaultLFD(lfd, fonttype);
1400 WARN("Ini section [%s]%s is malformed\n", INIFontSection, ini);
1401 HeapFree(GetProcessHeap(), 0, lfd);
1406 /***********************************************************************
1407 * XFONT_LoadDefaults
1409 static void XFONT_LoadDefaults(void)
1411 XFONT_LoadDefault(INIDefaultFixed, "fixed ");
1412 XFONT_LoadDefault(INIDefault, "");
1415 /***********************************************************************
1418 static fontAlias* XFONT_CreateAlias( LPCSTR lpTypeFace, LPCSTR lpAlias )
1421 fontAlias *pfa, *prev = NULL;
1423 for(pfa = aliasTable; pfa; pfa = pfa->next)
1425 /* check if we already got one */
1426 if( !strcasecmp( pfa->faTypeFace, lpAlias ) )
1428 TRACE("redundant alias '%s' -> '%s'\n",
1429 lpAlias, lpTypeFace );
1435 j = strlen(lpTypeFace) + 1;
1436 pfa = HeapAlloc( GetProcessHeap(), 0, sizeof(fontAlias) +
1437 j + strlen(lpAlias) + 1 );
1446 pfa->faTypeFace = (LPSTR)(pfa + 1);
1447 strcpy( pfa->faTypeFace, lpTypeFace );
1448 pfa->faAlias = pfa->faTypeFace + j;
1449 strcpy( pfa->faAlias, lpAlias );
1451 TRACE("added alias '%s' for '%s'\n", lpAlias, lpTypeFace );
1459 /***********************************************************************
1462 static void XFONT_LoadAlias(const LFD* lfd, LPCSTR lpAlias, BOOL bSubst)
1464 fontResource *fr, *frMatch = NULL;
1465 if (!lfd->foundry || ! lfd->family)
1467 WARN("Malformed font resource for alias '%s'\n", lpAlias);
1470 for (fr = fontList; fr ; fr = fr->next)
1472 if(!strcasecmp(fr->resource->family, lpAlias))
1474 /* alias is not needed since the real font is present */
1475 TRACE("Ignoring font alias '%s' as it is already available as a real font\n", lpAlias);
1478 if( XFONT_SameFoundryAndFamily( fr->resource, lfd ) )
1489 fontAlias *pfa, *prev = NULL;
1491 for(pfa = aliasTable; pfa; pfa = pfa->next)
1493 /* Remove lpAlias from aliasTable - we should free the old entry */
1494 if(!strcmp(lpAlias, pfa->faAlias))
1497 prev->next = pfa->next;
1499 aliasTable = pfa->next;
1502 /* Update any references to the substituted font in aliasTable */
1503 if(!strcmp(frMatch->lfFaceName, pfa->faTypeFace))
1504 pfa->faTypeFace = HEAP_strdupA( GetProcessHeap(), 0, lpAlias );
1508 TRACE("\tsubstituted '%s' with '%s'\n", frMatch->lfFaceName, lpAlias );
1510 lstrcpynA( frMatch->lfFaceName, lpAlias, LF_FACESIZE );
1511 frMatch->fr_flags |= FR_NAMESET;
1515 /* create new entry in the alias table */
1516 XFONT_CreateAlias( frMatch->lfFaceName, lpAlias );
1521 WARN("Font alias '-%s-%s-' is not available\n", lfd->foundry, lfd->family);
1525 /***********************************************************************
1530 * Create font aliases for some standard windows fonts using user's
1531 * default choice of (sans-)serif fonts
1533 * Read user-defined aliases from wine.conf. Format is as follows
1535 * Alias# = [Windows font name],[LFD font name], <substitute original name>
1538 * Alias0 = Arial, -adobe-helvetica-
1539 * Alias1 = Times New Roman, -bitstream-courier-, 1
1542 * Note that from 970817 and on we have built-in alias templates that take
1543 * care of the necessary Windows typefaces.
1551 static void XFONT_LoadAliases(void)
1554 char buffer[MAX_LFD_LENGTH];
1558 /* built-ins first */
1559 PROFILE_GetWineIniString( INIFontSection, INIDefaultSerif,
1560 "-bitstream-charter-", buffer, sizeof buffer );
1561 TRACE("Using '%s' as default serif font\n", buffer);
1562 lfd = LFD_Parse(buffer);
1563 /* NB XFONT_InitialCapitals should not change these standard aliases */
1566 XFONT_LoadAlias( lfd, "Tms Roman", FALSE);
1567 XFONT_LoadAlias( lfd, "MS Serif", FALSE);
1568 XFONT_LoadAlias( lfd, "Times New Roman", FALSE);
1570 XFONT_LoadDefaultLFD( lfd, "serif ");
1571 HeapFree(GetProcessHeap(), 0, lfd);
1574 PROFILE_GetWineIniString( INIFontSection, INIDefaultSansSerif,
1575 "-adobe-helvetica-", buffer, sizeof buffer);
1576 TRACE("Using '%s' as default sans serif font\n", buffer);
1577 lfd = LFD_Parse(buffer);
1580 XFONT_LoadAlias( lfd, "Helv", FALSE);
1581 XFONT_LoadAlias( lfd, "MS Sans Serif", FALSE);
1582 XFONT_LoadAlias( lfd, "Arial", FALSE);
1584 XFONT_LoadDefaultLFD( lfd, "sans serif ");
1585 HeapFree(GetProcessHeap(), 0, lfd);
1588 /* then user specified aliases */
1591 BOOL bHaveAlias, bSubst;
1592 char subsection[32];
1593 snprintf( subsection, sizeof subsection, "%s%i", INIAliasSection, i++ );
1595 bHaveAlias = PROFILE_GetWineIniString( INIFontSection,
1596 subsection, "", buffer, sizeof buffer);
1600 XFONT_InitialCapitals(buffer);
1601 lpResource = PROFILE_GetStringItem( buffer );
1602 bSubst = (PROFILE_GetStringItem( lpResource )) ? TRUE : FALSE;
1603 if( lpResource && *lpResource )
1605 lfd = LFD_Parse(lpResource);
1608 XFONT_LoadAlias(lfd, buffer, bSubst);
1609 HeapFree(GetProcessHeap(), 0, lfd);
1613 WARN("malformed font alias '%s'\n", buffer );
1618 /***********************************************************************
1621 * Convert an (potential) alias into a real windows name
1624 static LPCSTR XFONT_UnAlias(char* font)
1629 XFONT_InitialCapitals(font); /* to remove extra white space */
1631 for( fa = aliasTable; fa; fa = fa->next )
1632 /* use case insensitive matching to handle eg "MS Sans Serif" */
1633 if( !strcasecmp( fa->faAlias, font ) )
1635 TRACE("found alias '%s'->%s'\n", font, fa->faTypeFace );
1636 strcpy(font, fa->faTypeFace);
1644 /***********************************************************************
1645 * XFONT_RemoveFontResource
1647 * Caller should check if the font resource is in use. If it is it should
1648 * set FR_REMOVED flag to delay removal until the resource is not in use
1651 void XFONT_RemoveFontResource( fontResource** ppfr )
1653 fontResource* pfr = *ppfr;
1657 /* FIXME - if fonts were read from a cache, these HeapFrees will fail */
1660 pfi = pfr->fi->next;
1661 HeapFree( GetProcessHeap(), 0, pfr->fi );
1664 HeapFree( GetProcessHeap(), 0, pfr );
1669 /***********************************************************************
1674 * Removes specified fonts from the font table to prevent Wine from
1677 * Ignore# = [LFD font name]
1680 * Ignore0 = -misc-nil-
1681 * Ignore1 = -sun-open look glyph-
1685 static void XFONT_LoadIgnore(char* lfdname)
1687 fontResource** ppfr;
1689 LFD* lfd = LFD_Parse(lfdname);
1690 if (lfd && lfd->foundry && lfd->family)
1692 for( ppfr = &fontList; *ppfr ; ppfr = &((*ppfr)->next))
1694 if( XFONT_SameFoundryAndFamily( (*ppfr)->resource, lfd) )
1696 TRACE("Ignoring '-%s-%s-'\n",
1697 (*ppfr)->resource->foundry, (*ppfr)->resource->family );
1699 XFONT_RemoveFontResource( ppfr );
1705 WARN("Malformed font resource\n");
1707 HeapFree(GetProcessHeap(), 0, lfd);
1710 static void XFONT_LoadIgnores(void)
1713 char subsection[32];
1714 char buffer[MAX_LFD_LENGTH];
1716 /* Standard one that noone wants */
1717 strcpy(buffer, "-misc-nil-");
1718 XFONT_LoadIgnore(buffer);
1720 /* Others from INI file */
1723 sprintf( subsection, "%s%i", INIIgnoreSection, i++ );
1725 if( PROFILE_GetWineIniString( INIFontSection,
1726 subsection, "", buffer, sizeof buffer) )
1729 while( *pch && isspace(*pch) ) pch++;
1730 XFONT_LoadIgnore(pch);
1738 /***********************************************************************
1739 * XFONT_UserMetricsCache
1741 * Returns expanded name for the cachedmetrics file.
1742 * Now it also appends the current value of the $DISPLAY variable.
1744 static char* XFONT_UserMetricsCache( char* buffer, int* buf_size )
1746 const char *confdir = get_config_dir();
1747 const char *display_name = Options.display;
1748 int len = strlen(confdir) + strlen(INIFontMetrics) + strlen(display_name) + 2;
1750 if ((len > *buf_size) &&
1751 !(buffer = HeapReAlloc( GetProcessHeap(), 0, buffer, *buf_size = len )))
1753 ERR("out of memory\n");
1757 sprintf( buffer, "%s/%s%s", confdir, INIFontMetrics, display_name );
1762 /***********************************************************************
1765 * Compare two fonts (only parameters set by the XFONT_InitFontInfo()).
1767 static INT XFONT_IsSubset(const fontInfo* match, const fontInfo* fi)
1771 /* 0 - keep both, 1 - keep match, -1 - keep fi */
1773 /* Compare dfItalic, Underline, Strikeout, Weight, Charset */
1774 m = (BYTE*)&fi->df.dfPixWidth - (BYTE*)&fi->df.dfItalic;
1775 if( memcmp(&match->df.dfItalic, &fi->df.dfItalic, m )) return 0;
1777 if( (!((fi->fi_flags & FI_SCALABLE) + (match->fi_flags & FI_SCALABLE))
1778 && fi->lfd_height != match->lfd_height) ||
1779 (!((fi->fi_flags & FI_POLYWEIGHT) + (match->fi_flags & FI_POLYWEIGHT))
1780 && fi->df.dfWeight != match->df.dfWeight) ) return 0;
1782 m = (int)(match->fi_flags & (FI_POLYWEIGHT | FI_SCALABLE)) -
1783 (int)(fi->fi_flags & (FI_SCALABLE | FI_POLYWEIGHT));
1785 if( m == (FI_POLYWEIGHT - FI_SCALABLE) ||
1786 m == (FI_SCALABLE - FI_POLYWEIGHT) ) return 0; /* keep both */
1787 else if( m >= 0 ) return 1; /* 'match' is better */
1789 return -1; /* 'fi' is better */
1792 /***********************************************************************
1795 * REMOVE_SUBSETS - attach new fi and purge subsets
1796 * UNMARK_SUBSETS - remove subset flags from all fi entries
1798 static void XFONT_CheckFIList( fontResource* fr, fontInfo* fi, int action)
1801 fontInfo* pfi, *prev;
1803 for( prev = NULL, pfi = fr->fi; pfi; )
1805 if( action == REMOVE_SUBSETS )
1807 if( pfi->fi_flags & FI_SUBSET )
1809 fontInfo* subset = pfi;
1813 if( prev ) prev->next = pfi = pfi->next;
1814 else fr->fi = pfi = pfi->next;
1815 HeapFree( GetProcessHeap(), 0, subset );
1819 else pfi->fi_flags &= ~FI_SUBSET;
1825 if( action == REMOVE_SUBSETS ) /* also add the superset */
1827 if( fi->fi_flags & FI_SCALABLE )
1832 else if( prev ) prev->next = fi; else fr->fi = fi;
1836 if( i ) TRACE("\t purged %i subsets [%i]\n", i , fr->fi_count);
1839 /***********************************************************************
1842 static fontResource* XFONT_FindFIList( fontResource* pfr, const char* pTypeFace )
1846 if( !strcasecmp( pfr->lfFaceName, pTypeFace ) ) break;
1849 /* Give the app back the font name it asked for. Encarta checks this. */
1850 if (pfr) strcpy(pfr->lfFaceName,pTypeFace);
1854 /***********************************************************************
1855 * XFONT_FixupPointSize
1857 static void XFONT_FixupPointSize(fontInfo* fi)
1860 df.dfHorizRes = df.dfVertRes = fi->lfd_resolution;
1861 df.dfPoints = (INT16)
1862 (((INT)(df.dfPixHeight - df.dfInternalLeading) * 72 + (df.dfVertRes >> 1)) /
1867 /***********************************************************************
1868 * XFONT_BuildMetrics
1870 * Build font metrics from X font
1872 static int XFONT_BuildMetrics(char** x_pattern, int res, unsigned x_checksum, int x_count)
1875 fontInfo* fi = NULL;
1876 fontResource* fr, *pfr;
1879 MESSAGE("Building font metrics. This may take some time...\n");
1880 for( i = 0; i < x_count; i++ )
1885 char buffer[MAX_LFD_LENGTH];
1890 typeface = HEAP_strdupA(GetProcessHeap(), 0, x_pattern[i]);
1894 lfd = LFD_Parse(typeface);
1897 HeapFree(GetProcessHeap(), 0, typeface);
1901 /* find a family to insert into */
1903 for( pfr = NULL, fr = fontList; fr; fr = fr->next )
1905 if( XFONT_SameFoundryAndFamily(fr->resource, lfd))
1910 if( !fi ) fi = (fontInfo*) HeapAlloc(GetProcessHeap(), 0, sizeof(fontInfo));
1912 if( !LFD_InitFontInfo( fi, lfd, x_pattern[i]) )
1915 if( !fr ) /* add new family */
1918 fr = (fontResource*) HeapAlloc(GetProcessHeap(), 0, sizeof(fontResource));
1921 memset(fr, 0, sizeof(fontResource));
1923 fr->resource = (LFD*) HeapAlloc(GetProcessHeap(), 0, sizeof(LFD));
1924 memset(fr->resource, 0, sizeof(LFD));
1926 TRACE("family: -%s-%s-\n", lfd->foundry, lfd->family );
1927 fr->resource->foundry = HEAP_strdupA(GetProcessHeap(), 0, lfd->foundry);
1928 fr->resource->family = HEAP_strdupA(GetProcessHeap(), 0, lfd->family);
1929 fr->resource->weight = "";
1931 if( pfr ) pfr->next = fr;
1935 WARN("Not enough memory for a new font family\n");
1938 /* check if we already have something better than "fi" */
1940 for( pfi = fr->fi, j = 0; pfi && j <= 0; pfi = pfi->next )
1941 if( (j = XFONT_IsSubset( pfi, fi )) < 0 )
1942 pfi->fi_flags |= FI_SUBSET; /* superseded by "fi" */
1943 if( j > 0 ) goto nextfont;
1945 /* add new font instance "fi" to the "fr" font resource */
1947 if( fi->fi_flags & FI_SCALABLE )
1950 char pxl_string[4], res_string[4];
1951 /* set scalable font height to get an basis for extrapolation */
1953 fi->lfd_height = DEF_SCALABLE_HEIGHT;
1954 fi->lfd_resolution = res;
1956 sprintf(pxl_string, "%d", fi->lfd_height);
1957 sprintf(res_string, "%d", fi->lfd_resolution);
1960 lfd1.pixel_size = pxl_string;
1961 lfd1.point_size = "*";
1962 lfd1.resolution_x = res_string;
1963 lfd1.resolution_y = res_string;
1965 LFD_UnParse(buffer, sizeof buffer, &lfd1);
1969 else lpstr = x_pattern[i];
1971 if( (x_fs = TSXLoadQueryFont(display, lpstr)) )
1973 XFONT_SetFontMetric( fi, fr, x_fs );
1974 TSXFreeFont( display, x_fs );
1976 XFONT_FixupPointSize(fi);
1978 TRACE("\t[% 2ipt] '%s'\n", fi->df.dfPoints, x_pattern[i] );
1980 XFONT_CheckFIList( fr, fi, REMOVE_SUBSETS );
1981 fi = NULL; /* preventing reuse */
1985 ERR("failed to load %s\n", lpstr );
1987 XFONT_CheckFIList( fr, fi, UNMARK_SUBSETS );
1990 HeapFree(GetProcessHeap(), 0, lfd);
1991 HeapFree(GetProcessHeap(), 0, typeface);
1993 if( fi ) HeapFree(GetProcessHeap(), 0, fi);
1995 /* Scan through the font list and remove FontResorce(s) (fr)
1996 * that have no associated Fontinfo(s) (fi).
1997 * This code is necessary because XFONT_ReadCachedMetrics
1998 * assumes that there is at least one fi associated with a fr.
1999 * This assumption is invalid for TT font
2000 * -altsys-ms outlook-medium-r-normal--0-0-0-0-p-0-microsoft-symbol.
2005 while (!fr->fi_count)
2007 fontList = fr->next;
2009 HeapFree(GetProcessHeap(), 0, fr->resource);
2010 HeapFree(GetProcessHeap(), 0, fr);
2020 if (!fr->next->fi_count)
2023 fr->next = fr->next->next;
2025 HeapFree(GetProcessHeap(), 0, pfr->resource);
2026 HeapFree(GetProcessHeap(), 0, pfr);
2037 /***********************************************************************
2038 * XFONT_ReadCachedMetrics
2042 static BOOL XFONT_ReadCachedMetrics( int fd, int res, unsigned x_checksum, int x_count )
2049 /* read checksums */
2050 read( fd, &u, sizeof(unsigned) );
2051 read( fd, &i, sizeof(int) );
2053 if( u == x_checksum && i == x_count )
2055 off_t length, offset = 3 * sizeof(int);
2057 /* read total size */
2058 read( fd, &i, sizeof(int) );
2059 length = lseek( fd, 0, SEEK_END );
2061 if( length == (i + offset) )
2063 lseek( fd, offset, SEEK_SET );
2064 fontList = (fontResource*)HeapAlloc( GetProcessHeap(), 0, i);
2067 fontResource* pfr = fontList;
2068 fontInfo* pfi = NULL;
2070 TRACE("Reading cached font metrics:\n");
2072 read( fd, fontList, i); /* read all metrics at once */
2073 while( offset < length )
2075 offset += sizeof(fontResource) + sizeof(fontInfo);
2076 pfr->fi = pfi = (fontInfo*)(pfr + 1);
2080 if( offset > length ||
2081 pfi->cptable >= (UINT16)X11DRV_CPTABLE_COUNT ||
2082 (int)(pfi->next) != j++ ) goto fail;
2084 if( pfi->df.dfPixHeight == 0 ) goto fail;
2086 pfi->df.dfFace = pfr->lfFaceName;
2087 if( pfi->fi_flags & FI_SCALABLE )
2089 /* we can pretend we got this font for any resolution */
2090 pfi->lfd_resolution = res;
2091 XFONT_FixupPointSize(pfi);
2093 pfi->next = pfi + 1;
2095 if( j > pfr->fi_count ) break;
2098 offset += sizeof(fontInfo);
2103 pfr->next = (fontResource*)(pfi + 1);
2108 if( pfr->next == NULL &&
2109 *(int*)(pfi + 1) == X_FMC_MAGIC )
2111 /* read LFD stubs */
2112 char* lpch = (char*)((int*)(pfi + 1) + 1);
2113 offset += sizeof(int);
2114 for( pfr = fontList; pfr; pfr = pfr->next )
2116 size_t len = strlen(lpch) + 1;
2117 TRACE("\t%s, %i instances\n", lpch, pfr->fi_count );
2118 pfr->resource = LFD_Parse(lpch);
2121 if (offset > length)
2131 if( fontList ) HeapFree( GetProcessHeap(), 0, fontList );
2138 /***********************************************************************
2139 * XFONT_WriteCachedMetrics
2143 static BOOL XFONT_WriteCachedMetrics( int fd, unsigned x_checksum, int x_count, int n_ff )
2151 char buffer[MAX_LFD_LENGTH];
2153 /* font metrics file:
2157 * +0008 total size to load
2158 * +000C prepackaged font metrics
2161 * +...x + 4 LFD stubs
2164 write( fd, &x_checksum, sizeof(unsigned) );
2165 write( fd, &x_count, sizeof(int) );
2167 for( j = i = 0, pfr = fontList; pfr; pfr = pfr->next )
2169 LFD_UnParse(buffer, sizeof buffer, pfr->resource);
2170 i += strlen( buffer) + 1;
2173 i += n_ff * sizeof(fontResource) + j * sizeof(fontInfo) + sizeof(int);
2174 write( fd, &i, sizeof(int) );
2176 TRACE("Writing font cache:\n");
2178 for( pfr = fontList; pfr; pfr = pfr->next )
2182 TRACE("\t-%s-%s-, %i instances\n", pfr->resource->foundry, pfr->resource->family, pfr->fi_count );
2184 i = write( fd, pfr, sizeof(fontResource) );
2185 if( i == sizeof(fontResource) )
2187 for( k = 1, pfi = pfr->fi; pfi; pfi = pfi->next )
2191 fi.df.dfFace = NULL;
2192 fi.next = (fontInfo*)k; /* loader checks this */
2194 j = write( fd, &fi, sizeof(fi) );
2197 if( j == sizeof(fontInfo) ) continue;
2201 if( i == sizeof(fontResource) && j == sizeof(fontInfo) )
2203 i = j = X_FMC_MAGIC;
2204 write( fd, &i, sizeof(int) );
2205 for( pfr = fontList; pfr && i == j; pfr = pfr->next )
2207 LFD_UnParse(buffer, sizeof buffer, pfr->resource);
2208 i = strlen( buffer ) + 1;
2209 j = write( fd, buffer, i );
2218 /***********************************************************************
2219 * XFONT_GetPointResolution()
2223 * Here we initialize DefResolution which is used in the
2224 * XFONT_Match() penalty function. We also load the point
2225 * resolution value (higher values result in larger fonts).
2227 static int XFONT_GetPointResolution( DeviceCaps* pDevCaps )
2229 int i, j, point_resolution, num = 3;
2230 int allowed_xfont_resolutions[3] = { 72, 75, 100 };
2231 int best = 0, best_diff = 65536;
2233 point_resolution = PROFILE_GetWineIniInt( INIFontSection, INIResolution, 0 );
2234 if( !point_resolution )
2235 point_resolution = pDevCaps->logPixelsY;
2237 pDevCaps->logPixelsX = pDevCaps->logPixelsY = point_resolution;
2240 /* FIXME We can only really guess at a best DefResolution
2241 * - this should be configurable
2243 for( i = best = 0; i < num; i++ )
2245 j = abs( point_resolution - allowed_xfont_resolutions[i] );
2252 DefResolution = allowed_xfont_resolutions[best];
2254 /* FIXME - do win95,nt40,... do this as well ? */
2255 if (TWEAK_WineLook == WIN98_LOOK)
2257 /* Lie about the screen size, so that eg MM_LOMETRIC becomes MM_logical_LOMETRIC */
2259 denom = pDevCaps->logPixelsX * 10;
2260 pDevCaps->horzSize = (pDevCaps->horzRes * 254 + (denom>>1)) / denom;
2261 denom = pDevCaps->logPixelsY * 10;
2262 pDevCaps->vertSize = (pDevCaps->vertRes * 254 + (denom>>1)) / denom;
2265 return point_resolution;
2269 /***********************************************************************
2272 * Compute the matching score between the logical font and the device font.
2274 * contributions from highest to lowest:
2278 * family flags (only when the facename is not present)
2280 * weight, italics, underlines, strikeouts
2282 * NOTE: you can experiment with different penalty weights to see what happens.
2283 * http://premium.microsoft.com/msdn/library/techart/f365/f36b/f37b/d38b/sa8bf.htm
2285 static UINT XFONT_Match( fontMatch* pfm )
2287 fontInfo* pfi = pfm->pfi; /* device font to match */
2288 LPLOGFONT16 plf = pfm->plf; /* wanted logical font */
2290 BOOL bR6 = pfm->flags & FO_MATCH_XYINDEP; /* from TextCaps */
2291 BOOL bScale = pfi->fi_flags & FI_SCALABLE;
2294 TRACE("\t[ %-2ipt h=%-3i w=%-3i %s%s]\n", pfi->df.dfPoints,
2295 pfi->df.dfPixHeight, pfi->df.dfAvgWidth,
2296 (pfi->df.dfWeight > FW_NORMAL) ? "Bold " : "Normal ",
2297 (pfi->df.dfItalic) ? "Italic" : "" );
2299 pfm->flags &= FO_MATCH_MASK;
2302 /* pfm->internal_charset: given(required) charset */
2303 /* pfi->internal_charset: charset of this font */
2304 if (pfi->internal_charset == DEFAULT_CHARSET)
2306 /* special case(unicode font) */
2307 /* priority: unmatched charset < unicode < matched charset */
2312 if( pfm->internal_charset == DEFAULT_CHARSET )
2315 if (pfi->internal_charset != ANSI_CHARSET)
2318 if ( pfi->codepage != GetACP() )
2321 else if (pfm->internal_charset != pfi->internal_charset)
2323 if ( pfi->internal_charset & 0xff00 )
2324 penalty += 0x1000; /* internal charset - should not be used */
2333 if( plf->lfHeight > 0 )
2335 int h = pfi->df.dfPixHeight;
2336 d = h - plf->lfHeight;
2337 height = plf->lfHeight;
2341 int h = pfi->df.dfPixHeight - pfi->df.dfInternalLeading;
2344 d = h + plf->lfHeight;
2345 height = (-plf->lfHeight * pfi->df.dfPixHeight) / h;
2349 ERR("PixHeight == InternalLeading\n");
2350 penalty += 0x1000; /* dont want this */
2356 pfm->height = 1; /* Very small */
2360 pfm->height = height;
2361 else if( (plf->lfQuality != PROOF_QUALITY) && bR6 )
2363 if( d > 0 ) /* do not shrink raster fonts */
2365 pfm->height = pfi->df.dfPixHeight;
2366 penalty += (pfi->df.dfPixHeight - height) * 0x4;
2368 else /* expand only in integer multiples */
2370 pfm->height = height - height%pfi->df.dfPixHeight;
2371 penalty += (height - pfm->height + 1) * height / pfi->df.dfPixHeight;
2374 else /* can't be scaled at all */
2376 if( plf->lfQuality != PROOF_QUALITY) pfm->flags |= FO_SYNTH_HEIGHT;
2377 pfm->height = pfi->df.dfPixHeight;
2378 penalty += (d > 0)? d * 0x8 : -d * 0x10;
2382 pfm->height = pfi->df.dfPixHeight;
2384 /* Pitch and Family */
2385 if( pfm->flags & FO_MATCH_PAF ) {
2386 int family = plf->lfPitchAndFamily & FF_FAMILY;
2388 /* TMPF_FIXED_PITCH means exactly the opposite */
2389 if( plf->lfPitchAndFamily & FIXED_PITCH ) {
2390 if( pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH ) penalty += 0x100;
2391 } else /* Variable is the default */
2392 if( !(pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH) ) penalty += 0x2;
2394 if (family != FF_DONTCARE && family != (pfi->df.dfPitchAndFamily & FF_FAMILY) )
2402 if( bR6 || bScale ) h = 0;
2405 /* FIXME: not complete */
2407 pfm->flags |= FO_SYNTH_WIDTH;
2408 h = abs(plf->lfWidth - (pfm->height * pfi->df.dfAvgWidth)/pfi->df.dfPixHeight);
2410 penalty += h * ( d ) ? 0x2 : 0x1 ;
2412 else if( !(pfi->fi_flags & FI_NORMAL) ) penalty++;
2415 if( plf->lfWeight != FW_DONTCARE )
2417 penalty += abs(plf->lfWeight - pfi->df.dfWeight) / 40;
2418 if( plf->lfWeight > pfi->df.dfWeight ) pfm->flags |= FO_SYNTH_BOLD;
2419 } else if( pfi->df.dfWeight >= FW_BOLD ) penalty++; /* choose normal by default */
2422 if( plf->lfItalic != pfi->df.dfItalic )
2425 pfm->flags |= FO_SYNTH_ITALIC;
2428 if( plf->lfUnderline ) pfm->flags |= FO_SYNTH_UNDERLINE;
2431 if( plf->lfStrikeOut ) pfm->flags |= FO_SYNTH_STRIKEOUT;
2434 if( penalty && !bScale && pfi->lfd_resolution != DefResolution )
2437 TRACE(" returning %i\n", penalty );
2442 /***********************************************************************
2445 * Scan a particular font resource for the best match.
2447 static UINT XFONT_MatchFIList( fontMatch* pfm )
2449 BOOL skipRaster = (pfm->flags & FO_MATCH_NORASTER);
2450 UINT current_score, score = (UINT)(-1);
2451 fontMatch fm = *pfm;
2453 for( fm.pfi = pfm->pfr->fi; fm.pfi && score; fm.pfi = fm.pfi->next)
2455 if( skipRaster && !(fm.pfi->fi_flags & FI_SCALABLE) )
2458 current_score = XFONT_Match( &fm );
2459 if( score > current_score )
2462 score = current_score;
2468 /***********************************************************************
2469 * XFONT_MatchDeviceFont
2471 * Scan font resource tree.
2474 static void XFONT_MatchDeviceFont( fontResource* start, fontMatch* pfm)
2476 fontMatch fm = *pfm;
2477 UINT current_score, score = (UINT)(-1);
2478 fontResource** ppfr;
2480 TRACE("(%u) '%s' h=%i weight=%i %s\n",
2481 pfm->plf->lfCharSet, pfm->plf->lfFaceName, pfm->plf->lfHeight,
2482 pfm->plf->lfWeight, (pfm->plf->lfItalic) ? "Italic" : "" );
2485 if( fm.plf->lfFaceName[0] )
2487 fm.pfr = XFONT_FindFIList( start, fm.plf->lfFaceName);
2488 if( fm.pfr ) /* match family */
2490 TRACE("found facename '%s'\n", fm.pfr->lfFaceName );
2492 if( fm.pfr->fr_flags & FR_REMOVED )
2496 XFONT_MatchFIList( &fm );
2503 /* get charset if lfFaceName is one of known facenames. */
2505 const struct CharsetBindingInfo* pcharsetbindings;
2507 pcharsetbindings = &charsetbindings[0];
2508 while ( pcharsetbindings->pszFaceName != NULL )
2510 if ( !strcmp( pcharsetbindings->pszFaceName,
2511 fm.plf->lfFaceName ) )
2513 fm.internal_charset = pcharsetbindings->charset;
2516 pcharsetbindings ++;
2518 TRACE( "%s charset %u\n", fm.plf->lfFaceName, fm.internal_charset );
2522 /* match all available fonts */
2524 fm.flags |= FO_MATCH_PAF;
2525 for( ppfr = &fontList; *ppfr && score; ppfr = &(*ppfr)->next )
2527 if( (*ppfr)->fr_flags & FR_REMOVED )
2529 if( (*ppfr)->fo_count == 0 )
2530 XFONT_RemoveFontResource( ppfr );
2536 TRACE("%s\n", fm.pfr->lfFaceName );
2538 current_score = XFONT_MatchFIList( &fm );
2539 if( current_score < score )
2541 score = current_score;
2548 /***********************************************************************
2551 static void XFONT_GrowFreeList(int start, int end)
2553 /* add all entries from 'start' up to and including 'end' */
2555 memset( fontCache + start, 0, (end - start + 1) * sizeof(fontObject) );
2557 fontCache[end].lru = fontLF;
2558 fontCache[end].count = -1;
2560 while( start < end )
2562 fontCache[start].count = -1;
2563 fontCache[start].lru = start + 1;
2568 static fontObject* XFONT_LookupCachedFont( const LPLOGFONT16 plf, UINT16* checksum )
2570 UINT16 cs = __lfCheckSum( plf );
2571 int i = fontMRU, prev = -1;
2576 if( fontCache[i].lfchecksum == cs &&
2577 !(fontCache[i].fo_flags & FO_REMOVED) )
2579 /* FIXME: something more intelligent here ? */
2581 if( !memcmp( plf, &fontCache[i].lf,
2582 sizeof(LOGFONT16) - LF_FACESIZE ) &&
2583 !strcmp( plf->lfFaceName, fontCache[i].lf.lfFaceName) )
2585 /* remove temporarily from the lru list */
2587 fontCache[prev].lru = fontCache[i].lru;
2589 fontMRU = (INT16)fontCache[i].lru;
2590 return (fontCache + i);
2594 i = (INT16)fontCache[i].lru;
2599 static fontObject* XFONT_GetCacheEntry(void)
2605 int prev_i, prev_j, j;
2607 TRACE("font cache is full\n");
2609 /* lookup the least recently used font */
2611 for( prev_i = prev_j = j = -1, i = fontMRU; i >= 0; i = (INT16)fontCache[i].lru )
2613 if( fontCache[i].count <= 0 &&
2614 !(fontCache[i].fo_flags & FO_SYSTEM) )
2622 if( j >= 0 ) /* unload font */
2624 /* detach from the lru list */
2626 TRACE("\tfreeing entry %i\n", j );
2628 fontCache[j].fr->fo_count--;
2631 fontCache[prev_j].lru = fontCache[j].lru;
2632 else fontMRU = (INT16)fontCache[j].lru;
2634 /* FIXME: lpXForm, lpPixmap */
2635 if(fontCache[j].lpX11Trans)
2636 HeapFree( GetProcessHeap(), 0, fontCache[j].lpX11Trans );
2638 TSXFreeFont( display, fontCache[j].fs );
2640 memset( fontCache + j, 0, sizeof(fontObject) );
2641 return (fontCache + j);
2643 else /* expand cache */
2645 fontObject* newCache;
2647 prev_i = fontCacheSize + FONTCACHE;
2649 TRACE("\tgrowing font cache from %i to %i\n", fontCacheSize, prev_i );
2651 if( (newCache = (fontObject*)HeapReAlloc(GetProcessHeap(), 0,
2652 fontCache, prev_i)) )
2655 fontCacheSize = prev_i;
2656 fontCache = newCache;
2657 XFONT_GrowFreeList( i, fontCacheSize - 1);
2663 /* detach from the free list */
2666 fontLF = (INT16)fontCache[i].lru;
2667 fontCache[i].count = 0;
2668 return (fontCache + i);
2671 static int XFONT_ReleaseCacheEntry(const fontObject* pfo)
2673 UINT u = (UINT)(pfo - fontCache);
2677 if( u < fontCacheSize )
2679 ret = --fontCache[u].count;
2682 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
2684 if( CHECK_PFONT(pfo->prefobjs[i]) )
2685 XFONT_ReleaseCacheEntry(__PFONT(pfo->prefobjs[i]));
2695 /***********************************************************************
2698 * Initialize font resource list and allocate font cache.
2700 BOOL X11DRV_FONT_Init( DeviceCaps* pDevCaps )
2703 unsigned x_checksum;
2704 int i,res, x_count, fd, buf_size;
2707 res = XFONT_GetPointResolution( pDevCaps );
2709 x_pattern = TSXListFonts(display, "*", MAX_FONTS, &x_count );
2711 TRACE("Font Mapper: initializing %i fonts [logical dpi=%i, default dpi=%i]\n",
2712 x_count, res, DefResolution);
2713 if (x_count == MAX_FONTS)
2714 MESSAGE("There may be more fonts available - try increasing the value of MAX_FONTS\n");
2716 for( i = x_checksum = 0; i < x_count; i++ )
2720 printf("%i\t: %s\n", i, x_pattern[i] );
2723 j = strlen( x_pattern[i] );
2724 if( j ) x_checksum ^= __genericCheckSum( x_pattern[i], j );
2726 x_checksum |= X_PFONT_MAGIC;
2728 buffer = HeapAlloc( GetProcessHeap(), 0, buf_size );
2730 /* deal with systemwide font metrics cache */
2732 if( PROFILE_GetWineIniString( INIFontSection, INIGlobalMetrics, "", buffer, buf_size ) )
2734 fd = open( buffer, O_RDONLY );
2735 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2737 if (fontList == NULL)
2740 buffer = XFONT_UserMetricsCache( buffer, &buf_size );
2743 fd = open( buffer, O_RDONLY );
2744 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2748 if( fontList == NULL ) /* build metrics from scratch */
2750 int n_ff = XFONT_BuildMetrics(x_pattern, DefResolution, x_checksum, x_count);
2751 if( buffer[0] ) /* update cached metrics */
2753 fd = open( buffer, O_CREAT | O_TRUNC | O_RDWR, 0644 ); /* -rw-r--r-- */
2754 if( XFONT_WriteCachedMetrics( fd, x_checksum, x_count, n_ff ) == FALSE )
2756 WARN("Unable to write to fontcache '%s'\n", buffer);
2757 if( fd >= 0) remove( buffer ); /* couldn't write entire file */
2762 TSXFreeFontNames(x_pattern);
2764 /* check if we're dealing with X11 R6 server */
2767 strcpy(buffer, "-*-*-*-*-normal-*-[12 0 0 12]-*-72-*-*-*-iso8859-1");
2768 if( (x_fs = TSXLoadQueryFont(display, buffer)) )
2770 XTextCaps |= TC_SF_X_YINDEP;
2771 TSXFreeFont(display, x_fs);
2774 HeapFree(GetProcessHeap(), 0, buffer);
2776 XFONT_WindowsNames();
2777 XFONT_LoadAliases();
2778 XFONT_LoadDefaults();
2779 XFONT_LoadIgnores();
2781 /* fontList initialization is over, allocate X font cache */
2783 fontCache = (fontObject*) HeapAlloc(GetProcessHeap(), 0, fontCacheSize * sizeof(fontObject));
2784 XFONT_GrowFreeList(0, fontCacheSize - 1);
2788 /* update text caps parameter */
2790 pDevCaps->textCaps = XTextCaps;
2792 RAW_ASCENT = TSXInternAtom(display, "RAW_ASCENT", TRUE);
2793 RAW_DESCENT = TSXInternAtom(display, "RAW_DESCENT", TRUE);
2798 /**********************************************************************
2801 static BOOL XFONT_SetX11Trans( fontObject *pfo )
2807 TSXGetFontProperty( pfo->fs, XA_FONT, &nameAtom );
2808 fontName = TSXGetAtomName( display, nameAtom );
2809 lfd = LFD_Parse(fontName);
2816 if (lfd->pixel_size[0] != '[') {
2817 HeapFree(GetProcessHeap(), 0, lfd);
2822 #define PX pfo->lpX11Trans
2824 sscanf(lfd->pixel_size, "[%f%f%f%f]", &PX->a, &PX->b, &PX->c, &PX->d);
2826 HeapFree(GetProcessHeap(), 0, lfd);
2828 TSXGetFontProperty( pfo->fs, RAW_ASCENT, &PX->RAW_ASCENT );
2829 TSXGetFontProperty( pfo->fs, RAW_DESCENT, &PX->RAW_DESCENT );
2831 PX->pixelsize = hypot(PX->a, PX->b);
2832 PX->ascent = PX->pixelsize / 1000.0 * PX->RAW_ASCENT;
2833 PX->descent = PX->pixelsize / 1000.0 * PX->RAW_DESCENT;
2835 TRACE("[%f %f %f %f] RA = %ld RD = %ld\n",
2836 PX->a, PX->b, PX->c, PX->d,
2837 PX->RAW_ASCENT, PX->RAW_DESCENT);
2843 /***********************************************************************
2844 * X Device Font Objects
2846 static X_PHYSFONT XFONT_RealizeFont( const LPLOGFONT16 plf,
2847 LPCSTR* faceMatched, BOOL bSubFont,
2848 WORD internal_charset,
2849 WORD* pcharsetMatched )
2855 pfo = XFONT_LookupCachedFont( plf, &checksum );
2866 fm.internal_charset = internal_charset;
2868 if( XTextCaps & TC_SF_X_YINDEP ) fm.flags = FO_MATCH_XYINDEP;
2870 /* allocate new font cache entry */
2872 if( (pfo = XFONT_GetCacheEntry()) )
2874 /* initialize entry and load font */
2875 char lpLFD[MAX_LFD_LENGTH];
2876 UINT uRelaxLevel = 0;
2878 if(abs(plf->lfHeight) > MAX_FONT_SIZE) {
2880 "plf->lfHeight = %d, Creating a 100 pixel font and rescaling metrics \n",
2882 pfo->rescale = fabs(plf->lfHeight / 100.0);
2883 if(plf->lfHeight > 0) plf->lfHeight = 100;
2884 else plf->lfHeight = -100;
2888 XFONT_MatchDeviceFont( fontList, &fm );
2891 pfo->fr->fo_count++;
2892 pfo->fo_flags = fm.flags & ~FO_MATCH_MASK;
2895 pfo->lfchecksum = checksum;
2899 LFD_ComposeLFD( pfo, fm.height, lpLFD, uRelaxLevel++ );
2900 if( (pfo->fs = TSXLoadQueryFont( display, lpLFD )) ) break;
2901 } while( uRelaxLevel );
2904 if(pfo->lf.lfEscapement != 0) {
2905 pfo->lpX11Trans = HeapAlloc(GetProcessHeap(), 0, sizeof(XFONTTRANS));
2906 if(!XFONT_SetX11Trans( pfo )) {
2907 HeapFree(GetProcessHeap(), 0, pfo->lpX11Trans);
2908 pfo->lpX11Trans = NULL;
2911 XFONT_GetLeading( &pfo->fi->df, pfo->fs,
2912 &pfo->foInternalLeading, NULL, pfo->lpX11Trans );
2913 pfo->foAvgCharWidth = (INT16)XFONT_GetAvgCharWidth(&pfo->fi->df, pfo->fs, pfo->lpX11Trans );
2914 pfo->foMaxCharWidth = (INT16)XFONT_GetMaxCharWidth(pfo->fs, pfo->lpX11Trans);
2916 /* FIXME: If we've got a soft font or
2917 * there are FO_SYNTH_... flags for the
2918 * non PROOF_QUALITY request, the engine
2919 * should rasterize characters into mono
2920 * pixmaps and store them in the pfo->lpPixmap
2921 * array (pfo->fs should be updated as well).
2922 * array (pfo->fs should be updated as well).
2923 * X11DRV_ExtTextOut() must be heavily modified
2924 * to support pixmap blitting and FO_SYNTH_...
2928 pfo->lpPixmap = NULL;
2930 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
2931 pfo->prefobjs[i] = (X_PHYSFONT)0xffffffff; /* invalid value */
2933 /* special treatment for DBCS that needs multiple fonts */
2934 /* All member of pfo must be set correctly. */
2935 if ( bSubFont == FALSE )
2938 WORD charsetMatchedSub;
2940 LPCSTR faceMatchedSub;
2942 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
2944 charset_sub = X11DRV_cptable[pfo->fi->cptable].
2945 penum_subfont_charset( i );
2946 if ( charset_sub == DEFAULT_CHARSET ) break;
2950 lfSub.lfHeight = pfo->fi->df.dfPixHeight;
2951 if ( plf->lfHeight < 0 )
2952 lfSub.lfHeight = - lfSub.lfHeight;
2953 lfSub.lfCharSet = (BYTE)(charset_sub & 0xff);
2954 lfSub.lfFaceName[0] = '\0'; /* FIXME? */
2955 /* this font has sub font */
2956 if ( i == 0 ) pfo->prefobjs[0] = (X_PHYSFONT)0;
2958 XFONT_RealizeFont( &lfSub, &faceMatchedSub,
2960 &charsetMatchedSub );
2961 /* FIXME: check charsetMatchedSub */
2966 if( !pfo ) /* couldn't get a new entry, get one of the cached fonts */
2968 UINT current_score, score = (UINT)(-1);
2970 i = index = fontMRU;
2971 fm.flags |= FO_MATCH_PAF;
2974 pfo = fontCache + i;
2975 fm.pfr = pfo->fr; fm.pfi = pfo->fi;
2977 current_score = XFONT_Match( &fm );
2978 if( current_score < score ) index = i;
2982 pfo = fontCache + index;
2987 /* attach at the head of the lru list */
2989 index = fontMRU = (pfo - fontCache);
2994 TRACE("physfont %i\n", index);
2995 *faceMatched = pfo->fi->df.dfFace;
2996 *pcharsetMatched = pfo->fi->internal_charset;
2998 return (X_PHYSFONT)(X_PFONT_MAGIC | index);
3001 /***********************************************************************
3002 * XFONT_GetFontObject
3004 fontObject* XFONT_GetFontObject( X_PHYSFONT pFont )
3006 if( CHECK_PFONT(pFont) ) return __PFONT(pFont);
3010 /***********************************************************************
3011 * XFONT_GetFontStruct
3013 XFontStruct* XFONT_GetFontStruct( X_PHYSFONT pFont )
3015 if( CHECK_PFONT(pFont) ) return __PFONT(pFont)->fs;
3019 /***********************************************************************
3022 LPIFONTINFO16 XFONT_GetFontInfo( X_PHYSFONT pFont )
3024 if( CHECK_PFONT(pFont) ) return &(__PFONT(pFont)->fi->df);
3030 /* X11DRV Interface ****************************************************
3032 * Exposed via the dc->funcs dispatch table. *
3034 ***********************************************************************/
3035 /***********************************************************************
3036 * X11DRV_FONT_SelectObject
3038 HFONT X11DRV_FONT_SelectObject( DC* dc, HFONT hfont, FONTOBJ* font )
3040 HFONT hPrevFont = 0;
3042 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
3044 EnterCriticalSection( &crtsc_fonts_X11 );
3046 if( CHECK_PFONT(physDev->font) )
3047 XFONT_ReleaseCacheEntry( __PFONT(physDev->font) );
3051 /* Make sure we don't change the sign when converting to device coords */
3052 /* FIXME - check that the other drivers do this correctly */
3055 int vpt = abs(dc->vportExtX);
3056 int wnd = abs(dc->wndExtX);
3057 lf.lfWidth = (abs(lf.lfWidth) * vpt + (wnd>>1))/wnd;
3058 if (lf.lfWidth == 0)
3059 lf.lfWidth = 1; /* Minimum width */
3063 int vpt = abs(dc->vportExtY);
3064 int wnd = abs(dc->wndExtY);
3065 if (lf.lfHeight > 0)
3066 lf.lfHeight = (lf.lfHeight * vpt + (wnd>>1))/wnd;
3068 lf.lfHeight = (lf.lfHeight * vpt - (wnd>>1))/wnd;
3070 if (lf.lfHeight == 0)
3071 lf.lfHeight = MIN_FONT_SIZE;
3074 lf.lfHeight = -(DEF_POINT_SIZE * dc->devCaps->logPixelsY + (72>>1)) / 72;
3077 /* Fixup aliases before passing to RealizeFont */
3078 /* alias = Windows name in the alias table */
3079 LPCSTR alias = XFONT_UnAlias( lf.lfFaceName );
3081 WORD charsetMatched;
3083 TRACE("hfont=%04x\n", hfont); /* to connect with the trace from RealizeFont */
3084 physDev->font = XFONT_RealizeFont( &lf, &faceMatched,
3085 FALSE, lf.lfCharSet,
3088 /* set face to the requested facename if it matched
3089 * so that GetTextFace can get the correct face name
3091 if (alias && !strcmp(faceMatched, lf.lfFaceName))
3092 strcpy( font->logfont.lfFaceName, alias );
3094 strcpy( font->logfont.lfFaceName, faceMatched );
3097 * In X, some encodings may have the same lfFaceName.
3099 * -misc-fixed-*-iso8859-1
3100 * -misc-fixed-*-jisx0208.1990-0
3101 * so charset should be saved...
3103 font->logfont.lfCharSet = charsetMatched;
3106 hPrevFont = dc->hFont;
3109 LeaveCriticalSection( &crtsc_fonts_X11 );
3115 /***********************************************************************
3117 * X11DRV_EnumDeviceFonts
3119 BOOL X11DRV_EnumDeviceFonts( HDC hdc, LPLOGFONT16 plf,
3120 DEVICEFONTENUMPROC proc, LPARAM lp )
3124 fontResource* pfr = fontList;
3127 if( plf->lfFaceName[0] )
3129 /* enum all entries in this resource */
3130 pfr = XFONT_FindFIList( pfr, plf->lfFaceName );
3134 for( pfi = pfr->fi; pfi; pfi = pfi->next )
3136 /* Note: XFONT_GetFontMetric() will have to
3137 release the crit section, font list will
3138 have to be retraversed on return */
3140 if( (b = (*proc)( &lf, &tm,
3141 XFONT_GetFontMetric( pfi, &lf, &tm ), lp )) )
3147 else /* enum first entry in each resource */
3148 for( ; pfr ; pfr = pfr->next )
3152 if( (b = (*proc)( &lf, &tm,
3153 XFONT_GetFontMetric( pfr->fi, &lf, &tm ), lp )) )
3162 /***********************************************************************
3163 * X11DRV_GetTextMetrics
3165 BOOL X11DRV_GetTextMetrics(DC *dc, TEXTMETRICA *metrics)
3167 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
3169 if( CHECK_PFONT(physDev->font) )
3171 fontObject* pfo = __PFONT(physDev->font);
3172 X11DRV_cptable[pfo->fi->cptable].pGetTextMetricsA( pfo, metrics );
3180 /***********************************************************************
3181 * X11DRV_GetCharWidth
3183 BOOL X11DRV_GetCharWidth( DC *dc, UINT firstChar, UINT lastChar,
3186 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
3187 fontObject* pfo = XFONT_GetFontObject( physDev->font );
3193 if (pfo->fs->per_char == NULL)
3194 for (i = firstChar; i <= lastChar; i++)
3196 *buffer++ = pfo->fs->min_bounds.attributes *
3197 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3199 *buffer++ = pfo->fs->min_bounds.width * pfo->rescale;
3202 XCharStruct *cs, *def;
3203 static XCharStruct __null_char = { 0, 0, 0, 0, 0, 0 };
3205 CI_GET_CHAR_INFO(pfo->fs, pfo->fs->default_char, &__null_char,
3208 for (i = firstChar; i <= lastChar; i++)
3210 if (i >= pfo->fs->min_char_or_byte2 &&
3211 i <= pfo->fs->max_char_or_byte2)
3213 cs = &pfo->fs->per_char[(i - pfo->fs->min_char_or_byte2)];
3214 if (CI_NONEXISTCHAR(cs)) cs = def;
3217 *buffer++ = max(cs->attributes, 0) *
3218 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3220 *buffer++ = max(cs->width, 0 ) * pfo->rescale;