Create GDI stock objects as normal objects instead of using magic
[wine/gsoc_dplay.git] / graphics / x11drv / xfont.c
blob79bdbf258d25d8ec8e63029d62d3f230717dd9ba
1 /*
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).
8 */
10 #include "config.h"
12 #include <X11/Xatom.h>
14 #include "ts_xlib.h"
16 #include <ctype.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <fcntl.h>
23 #include <math.h>
25 #include "windef.h"
26 #include "wingdi.h"
27 #include "winnls.h"
28 #include "winreg.h"
29 #include "font.h"
30 #include "debugtools.h"
31 #include "user.h" /* for TWEAK_WineLook (FIXME) */
32 #include "x11font.h"
33 #include "wine/server.h"
34 #include "wine/unicode.h"
36 DEFAULT_DEBUG_CHANNEL(font);
38 #define X_PFONT_MAGIC (0xFADE0000)
39 #define X_FMC_MAGIC (0x0000CAFE)
41 #define MAX_FONTS 1024*16
42 #define MAX_LFD_LENGTH 256
43 #define TILDE '~'
44 #define HYPHEN '-'
46 #define DEF_POINT_SIZE 8 /* CreateFont(0 .. ) gets this */
47 #define DEF_SCALABLE_HEIGHT 100 /* pixels */
48 #define MIN_FONT_SIZE 2 /* Min size in pixels */
49 #define MAX_FONT_SIZE 1000 /* Max size in pixels */
51 #define REMOVE_SUBSETS 1
52 #define UNMARK_SUBSETS 0
55 #define FF_FAMILY (FF_MODERN | FF_SWISS | FF_ROMAN | FF_DECORATIVE | FF_SCRIPT)
57 typedef struct __fontAlias
59 LPSTR faTypeFace;
60 LPSTR faAlias;
61 struct __fontAlias* next;
62 } fontAlias;
64 static fontAlias *aliasTable = NULL;
66 static UINT XTextCaps = (TC_OP_CHARACTER | TC_OP_STROKE | TC_CP_STROKE | TC_CR_ANY |
67 TC_SA_DOUBLE | TC_SA_INTEGER | TC_SA_CONTIN |
68 TC_UA_ABLE | TC_SO_ABLE | TC_RA_ABLE);
69 /* X11R6 adds TC_SF_X_YINDEP, maybe more... */
71 static const char* INIFontMetrics = "cachedmetrics.";
72 static const char* INIFontSection = "Software\\Wine\\Wine\\Config\\fonts";
73 static const char* INIAliasSection = "Alias";
74 static const char* INIIgnoreSection = "Ignore";
75 static const char* INIDefault = "Default";
76 static const char* INIDefaultFixed = "DefaultFixed";
77 static const char* INIResolution = "Resolution";
78 static const char* INIGlobalMetrics = "FontMetrics";
79 static const char* INIDefaultSerif = "DefaultSerif";
80 static const char* INIDefaultSansSerif = "DefaultSansSerif";
83 /* FIXME - are there any more Latin charsets ? */
84 /* FIXME - RUSSIAN, ARABIC, GREEK, HEBREW are NOT Latin */
85 #define IS_LATIN_CHARSET(ch) \
86 ((ch)==ANSI_CHARSET ||\
87 (ch)==EE_CHARSET ||\
88 (ch)==ISO3_CHARSET ||\
89 (ch)==ISO4_CHARSET ||\
90 (ch)==RUSSIAN_CHARSET ||\
91 (ch)==ARABIC_CHARSET ||\
92 (ch)==GREEK_CHARSET ||\
93 (ch)==HEBREW_CHARSET ||\
94 (ch)==TURKISH_CHARSET ||\
95 (ch)==ISO10_CHARSET ||\
96 (ch)==BALTIC_CHARSET ||\
97 (ch)==CELTIC_CHARSET)
99 /* suffix-charset mapping tables - must be less than 254 entries long */
101 typedef struct __sufch
103 LPCSTR psuffix;
104 WORD charset; /* hibyte != 0 means *internal* charset */
105 WORD codepage;
106 WORD cptable;
107 } SuffixCharset;
109 static const SuffixCharset sufch_ansi[] = {
110 { "0", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
111 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
113 static const SuffixCharset sufch_iso646[] = {
114 { "irv", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
115 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
117 static const SuffixCharset sufch_iso8859[] = {
118 { "1", ANSI_CHARSET, 28591, X11DRV_CPTABLE_SBCS },
119 { "2", EE_CHARSET, 28592, X11DRV_CPTABLE_SBCS },
120 { "3", ISO3_CHARSET, 28593, X11DRV_CPTABLE_SBCS },
121 { "4", ISO4_CHARSET, 28594, X11DRV_CPTABLE_SBCS },
122 { "5", RUSSIAN_CHARSET, 28595, X11DRV_CPTABLE_SBCS },
123 { "6", ARABIC_CHARSET, 28596, X11DRV_CPTABLE_SBCS },
124 { "7", GREEK_CHARSET, 28597, X11DRV_CPTABLE_SBCS },
125 { "8", HEBREW_CHARSET, 28598, X11DRV_CPTABLE_SBCS },
126 { "9", TURKISH_CHARSET, 28599, X11DRV_CPTABLE_SBCS },
127 { "10", ISO10_CHARSET, 28600, X11DRV_CPTABLE_SBCS },
128 { "11", THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }, /* FIXME */
129 { "12", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
130 { "13", BALTIC_CHARSET, 28603, X11DRV_CPTABLE_SBCS },
131 { "14", CELTIC_CHARSET, 28604, X11DRV_CPTABLE_SBCS },
132 { "15", ANSI_CHARSET, 28605, X11DRV_CPTABLE_SBCS },
133 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
135 static const SuffixCharset sufch_microsoft[] = {
136 { "cp1250", EE_CHARSET, 1250, X11DRV_CPTABLE_SBCS },
137 { "cp1251", RUSSIAN_CHARSET, 1251, X11DRV_CPTABLE_SBCS },
138 { "cp1252", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
139 { "cp1253", GREEK_CHARSET, 1253, X11DRV_CPTABLE_SBCS },
140 { "cp1254", TURKISH_CHARSET, 1254, X11DRV_CPTABLE_SBCS },
141 { "cp1255", HEBREW_CHARSET, 1255, X11DRV_CPTABLE_SBCS },
142 { "cp1256", ARABIC_CHARSET, 1256, X11DRV_CPTABLE_SBCS },
143 { "cp1257", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS },
144 { "fontspecific", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
145 { "symbol", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
146 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
148 static const SuffixCharset sufch_tcvn[] = {
149 { "0", TCVN_CHARSET, 1252, X11DRV_CPTABLE_SBCS }, /* FIXME */
150 { NULL, TCVN_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
152 static const SuffixCharset sufch_tis620[] = {
153 { "0", THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }, /* FIXME */
154 { NULL, THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }};
156 static const SuffixCharset sufch_viscii[] = {
157 { "1", VISCII_CHARSET, 1252, X11DRV_CPTABLE_SBCS }, /* FIXME */
158 { NULL, VISCII_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
160 static const SuffixCharset sufch_windows[] = {
161 { "1250", EE_CHARSET, 1250, X11DRV_CPTABLE_SBCS },
162 { "1251", RUSSIAN_CHARSET, 1251, X11DRV_CPTABLE_SBCS },
163 { "1252", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
164 { "1253", GREEK_CHARSET, 1253, X11DRV_CPTABLE_SBCS },
165 { "1254", TURKISH_CHARSET, 1254, X11DRV_CPTABLE_SBCS },
166 { "1255", HEBREW_CHARSET, 1255, X11DRV_CPTABLE_SBCS },
167 { "1256", ARABIC_CHARSET, 1256, X11DRV_CPTABLE_SBCS },
168 { "1257", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS },
169 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
171 static const SuffixCharset sufch_koi8[] = {
172 { "r", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
173 { "ru", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
174 { "u", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
175 { NULL, RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS }};
177 static const SuffixCharset sufch_jisx0201[] = {
178 { "0", X11FONT_JISX0201_CHARSET, 932, X11DRV_CPTABLE_SBCS },
179 { NULL, X11FONT_JISX0201_CHARSET, 932, X11DRV_CPTABLE_SBCS }};
181 static const SuffixCharset sufch_jisx0208[] = {
182 { "0", SHIFTJIS_CHARSET, 932, X11DRV_CPTABLE_CP932 },
183 { NULL, SHIFTJIS_CHARSET, 932, X11DRV_CPTABLE_CP932 }};
185 static const SuffixCharset sufch_jisx0212[] = {
186 { "0", X11FONT_JISX0212_CHARSET, 932, X11DRV_CPTABLE_CP932 },
187 { NULL, X11FONT_JISX0212_CHARSET, 932, X11DRV_CPTABLE_CP932 }};
189 static const SuffixCharset sufch_ksc5601[] = {
190 { "0", HANGEUL_CHARSET, 949, X11DRV_CPTABLE_CP949 },
191 { NULL, HANGEUL_CHARSET, 949, X11DRV_CPTABLE_CP949 }};
193 static const SuffixCharset sufch_gb2312[] = {
194 { "0", GB2312_CHARSET, 936, X11DRV_CPTABLE_CP936 },
195 { NULL, GB2312_CHARSET, 936, X11DRV_CPTABLE_CP936 }};
197 static const SuffixCharset sufch_big5[] = {
198 { "0", CHINESEBIG5_CHARSET, 950, X11DRV_CPTABLE_CP950 },
199 { NULL, CHINESEBIG5_CHARSET, 950, X11DRV_CPTABLE_CP950 }};
201 static const SuffixCharset sufch_unicode[] = {
202 { "0", DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE },
203 { NULL, DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE }};
205 static const SuffixCharset sufch_iso10646[] = {
206 { "1", DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE },
207 { NULL, DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE }};
209 /* Each of these must be matched explicitly */
210 static const SuffixCharset sufch_any[] = {
211 { "fontspecific", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
212 { NULL, 0, 0, X11DRV_CPTABLE_SBCS }};
215 typedef struct __fet
217 LPSTR prefix;
218 const SuffixCharset* sufch;
219 struct __fet* next;
220 } fontEncodingTemplate;
222 /* Note: we can attach additional encoding mappings to the end
223 * of this table at runtime.
225 static fontEncodingTemplate __fETTable[] = {
226 { "ansi", sufch_ansi, &__fETTable[1] },
227 { "ascii", sufch_ansi, &__fETTable[2] },
228 { "iso646.1991", sufch_iso646, &__fETTable[3] },
229 { "iso8859", sufch_iso8859, &__fETTable[4] },
230 { "microsoft", sufch_microsoft, &__fETTable[5] },
231 { "tcvn", sufch_tcvn, &__fETTable[6] },
232 { "tis620.2533", sufch_tis620, &__fETTable[7] },
233 { "viscii1.1", sufch_viscii, &__fETTable[8] },
234 { "windows", sufch_windows, &__fETTable[9] },
235 { "koi8", sufch_koi8, &__fETTable[10]},
236 { "jisx0201.1976",sufch_jisx0201, &__fETTable[11]},
237 { "jisc6226.1978",sufch_jisx0208, &__fETTable[12]},
238 { "jisx0208.1983",sufch_jisx0208, &__fETTable[13]},
239 { "jisx0208.1990",sufch_jisx0208, &__fETTable[14]},
240 { "jisx0212.1990",sufch_jisx0212, &__fETTable[15]},
241 { "ksc5601.1987", sufch_ksc5601, &__fETTable[16]},
242 { "gb2312.1980", sufch_gb2312, &__fETTable[17]},
243 { "big5", sufch_big5, &__fETTable[18]},
244 { "unicode", sufch_unicode, &__fETTable[19]},
245 { "iso10646", sufch_iso10646, &__fETTable[20]},
246 { "cp", sufch_windows, &__fETTable[21]},
247 /* NULL prefix matches anything so put it last */
248 { NULL, sufch_any, NULL },
250 static fontEncodingTemplate* fETTable = __fETTable;
252 /* a charset database for known facenames */
253 struct CharsetBindingInfo
255 const char* pszFaceName;
256 BYTE charset;
258 static const struct CharsetBindingInfo charsetbindings[] =
260 /* special facenames */
261 { "System", DEFAULT_CHARSET },
262 { "FixedSys", DEFAULT_CHARSET },
264 /* known facenames */
265 { "MS Serif", ANSI_CHARSET },
266 { "MS Sans Serif", ANSI_CHARSET },
267 { "Courier", ANSI_CHARSET },
268 { "Symbol", SYMBOL_CHARSET },
270 { "Arial", ANSI_CHARSET },
271 { "Arial Greek", GREEK_CHARSET },
272 { "Arial Tur", TURKISH_CHARSET },
273 { "Arial Baltic", BALTIC_CHARSET },
274 { "Arial CE", EASTEUROPE_CHARSET },
275 { "Arial Cyr", RUSSIAN_CHARSET },
276 { "Courier New", ANSI_CHARSET },
277 { "Courier New Greek", GREEK_CHARSET },
278 { "Courier New Tur", TURKISH_CHARSET },
279 { "Courier New Baltic", BALTIC_CHARSET },
280 { "Courier New CE", EASTEUROPE_CHARSET },
281 { "Courier New Cyr", RUSSIAN_CHARSET },
282 { "Times New Roman", ANSI_CHARSET },
283 { "Times New Roman Greek", GREEK_CHARSET },
284 { "Times New Roman Tur", TURKISH_CHARSET },
285 { "Times New Roman Baltic", BALTIC_CHARSET },
286 { "Times New Roman CE", EASTEUROPE_CHARSET },
287 { "Times New Roman Cyr", RUSSIAN_CHARSET },
289 { "\x82\x6c\x82\x72 \x83\x53\x83\x56\x83\x62\x83\x4e",
290 SHIFTJIS_CHARSET }, /* MS gothic */
291 { "\x82\x6c\x82\x72 \x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e",
292 SHIFTJIS_CHARSET }, /* MS P gothic */
293 { "\x82\x6c\x82\x72 \x96\xbe\x92\xa9",
294 SHIFTJIS_CHARSET }, /* MS mincho */
295 { "\x82\x6c\x82\x72 \x82\x6f\x96\xbe\x92\xa9",
296 SHIFTJIS_CHARSET }, /* MS P mincho */
297 { "GulimChe", HANGEUL_CHARSET },
298 { "MS Song", GB2312_CHARSET },
299 { "MS Hei", GB2312_CHARSET },
300 { "\xb7\x73\xb2\xd3\xa9\xfa\xc5\xe9", CHINESEBIG5_CHARSET },/*MS Mingliu*/
301 { "\xb2\xd3\xa9\xfa\xc5\xe9", CHINESEBIG5_CHARSET },
303 { NULL, 0 }
307 static int DefResolution = 0;
309 static CRITICAL_SECTION crtsc_fonts_X11 = CRITICAL_SECTION_INIT("crtsc_fonts_X11");
311 static fontResource* fontList = NULL;
312 static fontObject* fontCache = NULL; /* array */
313 static int fontCacheSize = FONTCACHE;
314 static int fontLF = -1, fontMRU = -1; /* last free, most recently used */
316 #define __PFONT(pFont) ( fontCache + ((UINT)(pFont) & 0x0000FFFF) )
317 #define CHECK_PFONT(pFont) ( (((UINT)(pFont) & 0xFFFF0000) == X_PFONT_MAGIC) &&\
318 (((UINT)(pFont) & 0x0000FFFF) < fontCacheSize) )
320 static Atom RAW_ASCENT;
321 static Atom RAW_DESCENT;
323 /***********************************************************************
324 * Helper macros from X distribution
327 #define CI_NONEXISTCHAR(cs) (((cs)->width == 0) && \
328 (((cs)->rbearing|(cs)->lbearing| \
329 (cs)->ascent|(cs)->descent) == 0))
331 #define CI_GET_CHAR_INFO(fs,col,def,cs) \
333 cs = def; \
334 if (col >= fs->min_char_or_byte2 && col <= fs->max_char_or_byte2) { \
335 if (fs->per_char == NULL) { \
336 cs = &fs->min_bounds; \
337 } else { \
338 cs = &fs->per_char[(col - fs->min_char_or_byte2)]; \
339 if (CI_NONEXISTCHAR(cs)) cs = def; \
344 #define CI_GET_DEFAULT_INFO(fs,cs) \
345 CI_GET_CHAR_INFO(fs, fs->default_char, NULL, cs)
348 /***********************************************************************
349 * is_stock_font
351 inline static BOOL is_stock_font( HFONT font )
353 int i;
354 for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
356 if (i != DEFAULT_PALETTE && font == GetStockObject(i)) return TRUE;
358 return FALSE;
362 /***********************************************************************
363 * Checksums
365 static UINT16 __lfCheckSum( LPLOGFONT16 plf )
367 CHAR font[LF_FACESIZE];
368 UINT16 checksum = 0;
369 UINT16 *ptr;
370 int i;
372 ptr = (UINT16 *)plf;
373 for (i = 0; i < 9; i++) checksum ^= *ptr++;
374 for (i = 0; i < LF_FACESIZE; i++)
376 font[i] = tolower(plf->lfFaceName[i]);
377 if (!font[i] || font[i] == ' ') break;
379 for (ptr = (UINT16 *)font, i >>= 1; i > 0; i-- ) checksum ^= *ptr++;
380 return checksum;
383 static UINT16 __genericCheckSum( const void *ptr, int size )
385 unsigned int checksum = 0;
386 const char *p = (const char *)ptr;
387 while (size-- > 0)
388 checksum ^= (checksum << 3) + (checksum >> 29) + *p++;
390 return checksum & 0xffff;
393 /*************************************************************************
394 * LFD parse/compose routines
396 * NB. LFD_Parse will use lpFont for its own ends, so if you want to keep it
397 * make a copy first
399 * These functions also do TILDE to HYPHEN conversion
401 static LFD* LFD_Parse(LPSTR lpFont)
403 LFD* lfd;
404 char *lpch = lpFont, *lfd_fld[LFD_FIELDS], *field_start;
405 int i;
406 if (*lpch != HYPHEN)
408 WARN("font '%s' doesn't begin with '%c'\n", lpFont, HYPHEN);
409 return NULL;
412 field_start = ++lpch;
413 for( i = 0; i < LFD_FIELDS; )
415 if (*lpch == HYPHEN)
417 *lpch = '\0';
418 lfd_fld[i] = field_start;
419 i++;
420 field_start = ++lpch;
422 else if (!*lpch)
424 lfd_fld[i] = field_start;
425 i++;
426 break;
428 else if (*lpch == TILDE)
430 *lpch = HYPHEN;
431 ++lpch;
433 else
434 ++lpch;
436 /* Fill in the empty fields with NULLS */
437 for (; i< LFD_FIELDS; i++)
438 lfd_fld[i] = NULL;
439 if (*lpch)
440 WARN("Extra ignored in font '%s'\n", lpFont);
442 lfd = HeapAlloc( GetProcessHeap(), 0, sizeof(LFD) );
443 if (lfd)
445 lfd->foundry = lfd_fld[0];
446 lfd->family = lfd_fld[1];
447 lfd->weight = lfd_fld[2];
448 lfd->slant = lfd_fld[3];
449 lfd->set_width = lfd_fld[4];
450 lfd->add_style = lfd_fld[5];
451 lfd->pixel_size = lfd_fld[6];
452 lfd->point_size = lfd_fld[7];
453 lfd->resolution_x = lfd_fld[8];
454 lfd->resolution_y = lfd_fld[9];
455 lfd->spacing = lfd_fld[10];
456 lfd->average_width = lfd_fld[11];
457 lfd->charset_registry = lfd_fld[12];
458 lfd->charset_encoding = lfd_fld[13];
460 return lfd;
464 static void LFD_UnParse(LPSTR dp, UINT buf_size, LFD* lfd)
466 const char* lfd_fld[LFD_FIELDS];
467 int i;
469 if (!buf_size)
470 return; /* Dont be silly */
472 lfd_fld[0] = lfd->foundry;
473 lfd_fld[1] = lfd->family;
474 lfd_fld[2] = lfd->weight ;
475 lfd_fld[3] = lfd->slant ;
476 lfd_fld[4] = lfd->set_width ;
477 lfd_fld[5] = lfd->add_style;
478 lfd_fld[6] = lfd->pixel_size;
479 lfd_fld[7] = lfd->point_size;
480 lfd_fld[8] = lfd->resolution_x;
481 lfd_fld[9] = lfd->resolution_y ;
482 lfd_fld[10] = lfd->spacing ;
483 lfd_fld[11] = lfd->average_width ;
484 lfd_fld[12] = lfd->charset_registry ;
485 lfd_fld[13] = lfd->charset_encoding ;
487 buf_size--; /* Room for the terminator */
489 for (i = 0; i < LFD_FIELDS; i++)
491 const char* sp = lfd_fld[i];
492 if (!sp || !buf_size)
493 break;
495 *dp++ = HYPHEN;
496 buf_size--;
497 while (buf_size > 0 && *sp)
499 *dp = (*sp == HYPHEN) ? TILDE : *sp;
500 buf_size--;
501 dp++; sp++;
504 *dp = '\0';
508 static void LFD_GetWeight( fontInfo* fi, LPCSTR lpStr)
510 int j = strlen(lpStr);
511 if( j == 1 && *lpStr == '0')
512 fi->fi_flags |= FI_POLYWEIGHT;
513 else if( j == 4 )
515 if( !strcasecmp( "bold", lpStr) )
516 fi->df.dfWeight = FW_BOLD;
517 else if( !strcasecmp( "demi", lpStr) )
519 fi->fi_flags |= FI_FW_DEMI;
520 fi->df.dfWeight = FW_DEMIBOLD;
522 else if( !strcasecmp( "book", lpStr) )
524 fi->fi_flags |= FI_FW_BOOK;
525 fi->df.dfWeight = FW_REGULAR;
528 else if( j == 5 )
530 if( !strcasecmp( "light", lpStr) )
531 fi->df.dfWeight = FW_LIGHT;
532 else if( !strcasecmp( "black", lpStr) )
533 fi->df.dfWeight = FW_BLACK;
535 else if( j == 6 && !strcasecmp( "medium", lpStr) )
536 fi->df.dfWeight = FW_REGULAR;
537 else if( j == 8 && !strcasecmp( "demibold", lpStr) )
538 fi->df.dfWeight = FW_DEMIBOLD;
539 else
540 fi->df.dfWeight = FW_DONTCARE; /* FIXME: try to get something
541 * from the weight property */
544 static BOOL LFD_GetSlant( fontInfo* fi, LPCSTR lpStr)
546 int l = strlen(lpStr);
547 if( l == 1 )
549 switch( tolower( *lpStr ) )
551 case '0': fi->fi_flags |= FI_POLYSLANT; /* haven't seen this one yet */
552 default:
553 case 'r': fi->df.dfItalic = 0;
554 break;
555 case 'o':
556 fi->fi_flags |= FI_OBLIQUE;
557 case 'i': fi->df.dfItalic = 1;
558 break;
560 return FALSE;
562 return TRUE;
565 static void LFD_GetStyle( fontInfo* fi, LPCSTR lpstr, int dec_style_check)
567 int j = strlen(lpstr);
568 if( j > 3 ) /* find out is there "sans" or "script" */
570 j = 0;
572 if( strstr(lpstr, "sans") )
574 fi->df.dfPitchAndFamily |= FF_SWISS;
575 j = 1;
577 if( strstr(lpstr, "script") )
579 fi->df.dfPitchAndFamily |= FF_SCRIPT;
580 j = 1;
582 if( !j && dec_style_check )
583 fi->df.dfPitchAndFamily |= FF_DECORATIVE;
587 /*************************************************************************
588 * LFD_InitFontInfo
590 * INIT ONLY
592 * Fill in some fields in the fontInfo struct.
594 static int LFD_InitFontInfo( fontInfo* fi, const LFD* lfd, LPCSTR fullname )
596 int i, j, dec_style_check, scalability;
597 fontEncodingTemplate* boba;
598 const char* ridiculous = "font '%s' has ridiculous %s\n";
599 const char* lpstr;
601 if (!lfd->charset_registry)
603 WARN("font '%s' does not have enough fields\n", fullname);
604 return FALSE;
607 memset(fi, 0, sizeof(fontInfo) );
609 /* weight name - */
610 LFD_GetWeight( fi, lfd->weight);
612 /* slant - */
613 dec_style_check = LFD_GetSlant( fi, lfd->slant);
615 /* width name - */
616 lpstr = lfd->set_width;
617 if( strcasecmp( "normal", lpstr) ) /* XXX 'narrow', 'condensed', etc... */
618 dec_style_check = TRUE;
619 else
620 fi->fi_flags |= FI_NORMAL;
622 /* style - */
623 LFD_GetStyle(fi, lfd->add_style, dec_style_check);
625 /* pixel & decipoint height, and res_x & y */
627 scalability = 0;
629 j = strlen(lfd->pixel_size);
630 if( j == 0 || j > 3 )
632 WARN(ridiculous, fullname, "pixel_size");
633 return FALSE;
635 if( !(fi->lfd_height = atoi(lfd->pixel_size)) )
636 scalability++;
638 j = strlen(lfd->point_size);
639 if( j == 0 || j > 3 )
641 WARN(ridiculous, fullname, "point_size");
642 return FALSE;
644 if( !(atoi(lfd->point_size)) )
645 scalability++;
647 j = strlen(lfd->resolution_x);
648 if( j == 0 || j > 3 )
650 WARN(ridiculous, fullname, "resolution_x");
651 return FALSE;
653 if( !(fi->lfd_resolution = atoi(lfd->resolution_x)) )
654 scalability++;
656 j = strlen(lfd->resolution_y);
657 if( j == 0 || j > 3 )
659 WARN(ridiculous, fullname, "resolution_y");
660 return FALSE;
662 if( !(atoi(lfd->resolution_y)) )
663 scalability++;
665 /* Check scalability */
666 switch (scalability)
668 case 0: /* Bitmap */
669 break;
670 case 4: /* Scalable */
671 fi->fi_flags |= FI_SCALABLE;
672 break;
673 case 2:
674 /* #$%^!!! X11R6 mutant garbage (scalable bitmap) */
675 TRACE("Skipping scalable bitmap '%s'\n", fullname);
676 return FALSE;
677 default:
678 WARN("Font '%s' has weird scalability\n", fullname);
679 return FALSE;
682 /* spacing - */
683 lpstr = lfd->spacing;
684 switch( *lpstr )
686 case '\0':
687 WARN("font '%s' has no spacing\n", fullname);
688 return FALSE;
690 case 'p': fi->fi_flags |= FI_VARIABLEPITCH;
691 break;
692 case 'c': fi->df.dfPitchAndFamily |= FF_MODERN;
693 fi->fi_flags |= FI_FIXEDEX;
694 /* fall through */
695 case 'm': fi->fi_flags |= FI_FIXEDPITCH;
696 break;
697 default:
698 /* Of course this line does nothing */
699 fi->df.dfPitchAndFamily |= DEFAULT_PITCH | FF_DONTCARE;
702 /* average width - */
703 lpstr = lfd->average_width;
704 j = strlen(lpstr);
705 if( j == 0 || j > 3 )
707 WARN(ridiculous, fullname, "average_width");
708 return FALSE;
711 if( !(atoi(lpstr)) && !scalability )
713 WARN("font '%s' has average_width 0 but is not scalable!!\n", fullname);
714 return FALSE;
717 /* charset registry, charset encoding - */
718 lpstr = lfd->charset_registry;
719 if( strstr(lpstr, "ksc") ||
720 strstr(lpstr, "gb2312") ||
721 strstr(lpstr, "big5") )
723 FIXME("DBCS fonts like '%s' are not working correctly now.\n", fullname);
726 fi->df.dfCharSet = ANSI_CHARSET;
728 for( i = 0, boba = fETTable; boba; boba = boba->next, i++ )
730 if (!boba->prefix || !strcasecmp(lpstr, boba->prefix))
732 if (lfd->charset_encoding)
734 for( j = 0; boba->sufch[j].psuffix; j++ )
736 if( !strcasecmp(lfd->charset_encoding, boba->sufch[j].psuffix ))
738 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
739 fi->internal_charset = boba->sufch[j].charset;
740 fi->codepage = boba->sufch[j].codepage;
741 fi->cptable = boba->sufch[j].cptable;
742 goto done;
746 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
747 fi->internal_charset = boba->sufch[j].charset;
748 fi->codepage = boba->sufch[j].codepage;
749 fi->cptable = boba->sufch[j].cptable;
750 if (boba->prefix)
752 FIXME("font '%s' has unknown character encoding '%s' in known registry '%s'\n",
753 fullname, lfd->charset_encoding, boba->prefix);
754 j = 254;
756 else
758 FIXME("font '%s' has unknown registry '%s' and character encoding '%s' \n",
759 fullname, lfd->charset_registry, lfd->charset_encoding);
760 j = 255;
763 WARN("Defaulting to: df.dfCharSet = %d, internal_charset = %d, codepage = %d, cptable = %d\n",
764 fi->df.dfCharSet,fi->internal_charset, fi->codepage, fi->cptable);
765 goto done;
767 else if (boba->prefix)
769 WARN("font '%s' has known registry '%s' and no character encoding\n",
770 fullname, lpstr);
771 for( j = 0; boba->sufch[j].psuffix; j++ )
773 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
774 fi->internal_charset = boba->sufch[j].charset;
775 fi->codepage = boba->sufch[j].codepage;
776 fi->cptable = boba->sufch[j].cptable;
777 j = 255;
778 goto done;
782 WARN("font '%s' has unknown character set '%s'\n", fullname, lpstr);
783 return FALSE;
785 done:
786 /* i - index into fETTable
787 * j - index into suffix array for fETTable[i]
788 * except:
789 * 254 - found encoding prefix, unknown suffix
790 * 255 - no encoding match at all.
792 fi->fi_encoding = 256 * (UINT16)i + (UINT16)j;
794 return TRUE;
798 /*************************************************************************
799 * LFD_AngleMatrix
801 * make a matrix suitable for LFD based on theta radians
803 static void LFD_AngleMatrix( char* buffer, int h, double theta)
805 double matrix[4];
806 matrix[0] = h*cos(theta);
807 matrix[1] = h*sin(theta);
808 matrix[2] = -h*sin(theta);
809 matrix[3] = h*cos(theta);
810 sprintf(buffer, "[%+f%+f%+f%+f]", matrix[0], matrix[1], matrix[2], matrix[3]);
813 /*************************************************************************
814 * LFD_ComposeLFD
816 * Note: uRelax is a treatment not a cure. Font mapping algorithm
817 * should be bulletproof enough to allow us to avoid hacks like
818 * this despite LFD being so braindead.
820 static BOOL LFD_ComposeLFD( const fontObject* fo,
821 INT height, LPSTR lpLFD, UINT uRelax )
823 int i, h;
824 char *any = "*";
825 char h_string[64], resx_string[64], resy_string[64];
826 LFD aLFD;
828 /* Get the worst case over with first */
830 /* RealizeFont() will call us repeatedly with increasing uRelax
831 * until XLoadFont() succeeds.
832 * to avoid an infinite loop; these will always match
834 if (uRelax >= 5)
836 if (uRelax == 5)
837 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-1" );
838 else
839 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" );
840 return TRUE;
843 /* read foundry + family from fo */
844 aLFD.foundry = fo->fr->resource->foundry;
845 aLFD.family = fo->fr->resource->family;
847 /* add weight */
848 switch( fo->fi->df.dfWeight )
850 case FW_BOLD:
851 aLFD.weight = "bold"; break;
852 case FW_REGULAR:
853 if( fo->fi->fi_flags & FI_FW_BOOK )
854 aLFD.weight = "book";
855 else
856 aLFD.weight = "medium";
857 break;
858 case FW_DEMIBOLD:
859 aLFD.weight = "demi";
860 if( !( fo->fi->fi_flags & FI_FW_DEMI) )
861 aLFD.weight = "bold";
862 break;
863 case FW_BLACK:
864 aLFD.weight = "black"; break;
865 case FW_LIGHT:
866 aLFD.weight = "light"; break;
867 default:
868 aLFD.weight = any;
871 /* add slant */
872 if( fo->fi->df.dfItalic )
873 if( fo->fi->fi_flags & FI_OBLIQUE )
874 aLFD.slant = "o";
875 else
876 aLFD.slant = "i";
877 else
878 aLFD.slant = (uRelax < 1) ? "r" : any;
880 /* add width */
881 if( fo->fi->fi_flags & FI_NORMAL )
882 aLFD.set_width = "normal";
883 else
884 aLFD.set_width = any;
886 /* ignore style */
887 aLFD.add_style = any;
889 /* add pixelheight
891 * FIXME: fill in lpXForm and lpPixmap for rotated fonts
893 if( fo->fo_flags & FO_SYNTH_HEIGHT )
894 h = fo->fi->lfd_height;
895 else
897 h = (fo->fi->lfd_height * height + (fo->fi->df.dfPixHeight>>1));
898 h /= fo->fi->df.dfPixHeight;
900 if (h < MIN_FONT_SIZE) /* Resist rounding down to 0 */
901 h = MIN_FONT_SIZE;
902 else if (h > MAX_FONT_SIZE) /* Sanity check as huge fonts can lock up the font server */
904 WARN("Huge font size %d pixels has been reduced to %d\n", h, MAX_FONT_SIZE);
905 h = MAX_FONT_SIZE;
908 if (uRelax <= 2)
909 /* handle rotated fonts */
910 if (fo->lf.lfEscapement) {
911 /* escapement is in tenths of degrees, theta is in radians */
912 double theta = M_PI*fo->lf.lfEscapement/1800.;
913 LFD_AngleMatrix(h_string, h, theta);
915 else
917 sprintf(h_string, "%d", h);
919 else
920 sprintf(h_string, "%d", fo->fi->lfd_height);
922 aLFD.pixel_size = h_string;
923 aLFD.point_size = any;
925 /* resolution_x,y, average width */
926 /* FOX ME - Why do some font servers ignore average width ?
927 * so that you have to mess around with res_y
929 aLFD.average_width = any;
930 if (uRelax <= 3)
932 sprintf(resx_string, "%d", fo->fi->lfd_resolution);
933 aLFD.resolution_x = resx_string;
935 strcpy(resy_string, resx_string);
936 if( uRelax == 0 && XTextCaps & TC_SF_X_YINDEP )
938 if( fo->lf.lfWidth && !(fo->fo_flags & FO_SYNTH_WIDTH))
940 int resy = 0.5 + fo->fi->lfd_resolution *
941 (fo->fi->df.dfAvgWidth * height) /
942 (fo->lf.lfWidth * fo->fi->df.dfPixHeight) ;
943 sprintf(resy_string, "%d", resy);
945 else
947 /* FIXME - synth width */
950 aLFD.resolution_y = resy_string;
952 else
954 aLFD.resolution_x = aLFD.resolution_y = any;
957 /* spacing */
959 char* w;
961 if( fo->fi->fi_flags & FI_FIXEDPITCH )
962 w = ( fo->fi->fi_flags & FI_FIXEDEX ) ? "c" : "m";
963 else
964 w = ( fo->fi->fi_flags & FI_VARIABLEPITCH ) ? "p" : any;
966 aLFD.spacing = (uRelax <= 1) ? w : any;
969 /* encoding */
971 if (uRelax <= 4)
973 fontEncodingTemplate* boba = fETTable;
975 for(i = fo->fi->fi_encoding >> 8; i; i--) boba = boba->next;
976 aLFD.charset_registry = boba->prefix ? boba->prefix : any;
978 i = fo->fi->fi_encoding & 255;
979 switch( i )
981 default:
982 aLFD.charset_encoding = boba->sufch[i].psuffix;
983 break;
985 case 254:
986 aLFD.charset_encoding = any;
987 break;
989 case 255: /* no suffix - it ends eg "-ascii" */
990 aLFD.charset_encoding = NULL;
991 break;
994 else
996 aLFD.charset_registry = any;
997 aLFD.charset_encoding = any;
1000 LFD_UnParse(lpLFD, MAX_LFD_LENGTH, &aLFD);
1002 TRACE("\tLFD(uRelax=%d): %s\n", uRelax, lpLFD );
1003 return TRUE;
1007 /***********************************************************************
1008 * X Font Resources
1010 * font info - http://www.microsoft.com/kb/articles/q65/1/23.htm
1011 * Windows font metrics - http://www.microsoft.com/kb/articles/q32/6/67.htm
1013 static void XFONT_GetLeading( const LPIFONTINFO16 pFI, const XFontStruct* x_fs,
1014 INT16* pIL, INT16* pEL, const XFONTTRANS *XFT )
1016 unsigned long height;
1017 unsigned min = (unsigned char)pFI->dfFirstChar;
1018 BOOL bIsLatin = IS_LATIN_CHARSET(pFI->dfCharSet);
1020 if( pEL ) *pEL = 0;
1022 if(XFT) {
1023 Atom RAW_CAP_HEIGHT = TSXInternAtom(gdi_display, "RAW_CAP_HEIGHT", TRUE);
1024 if(TSXGetFontProperty((XFontStruct*)x_fs, RAW_CAP_HEIGHT, &height))
1025 *pIL = XFT->ascent -
1026 (INT)(XFT->pixelsize / 1000.0 * height);
1027 else
1028 *pIL = 0;
1029 return;
1032 if( TSXGetFontProperty((XFontStruct*)x_fs, XA_CAP_HEIGHT, &height) == FALSE )
1034 if( x_fs->per_char )
1035 if( bIsLatin )
1036 height = x_fs->per_char['X' - min].ascent;
1037 else
1038 if (x_fs->ascent >= x_fs->max_bounds.ascent)
1039 height = x_fs->max_bounds.ascent;
1040 else
1042 height = x_fs->ascent;
1043 if( pEL )
1044 *pEL = x_fs->max_bounds.ascent - height;
1046 else
1047 height = x_fs->min_bounds.ascent;
1050 *pIL = x_fs->ascent - height;
1053 /***********************************************************************
1054 * XFONT_CharWidth
1056 static int XFONT_CharWidth(const XFontStruct* x_fs,
1057 const XFONTTRANS *XFT, int ch)
1059 if(!XFT)
1060 return x_fs->per_char[ch].width;
1061 else
1062 return x_fs->per_char[ch].attributes * XFT->pixelsize / 1000.0;
1065 /***********************************************************************
1066 * XFONT_GetAvgCharWidth
1068 static INT XFONT_GetAvgCharWidth( LPIFONTINFO16 pFI, const XFontStruct* x_fs,
1069 const XFONTTRANS *XFT)
1071 unsigned min = (unsigned char)pFI->dfFirstChar;
1072 unsigned max = (unsigned char)pFI->dfLastChar;
1074 INT avg;
1076 if( x_fs->per_char )
1078 int width = 0, chars = 0, j;
1079 if( IS_LATIN_CHARSET(pFI->dfCharSet) ||
1080 pFI->dfCharSet == DEFAULT_CHARSET )
1082 /* FIXME - should use a weighted average */
1083 for( j = 0; j < 26; j++ )
1084 width += XFONT_CharWidth(x_fs, XFT, 'a' + j - min) +
1085 XFONT_CharWidth(x_fs, XFT, 'A' + j - min);
1086 chars = 52;
1088 else /* unweighted average of everything */
1090 for( j = 0, max -= min; j <= max; j++ )
1091 if( !CI_NONEXISTCHAR(x_fs->per_char + j) )
1093 width += XFONT_CharWidth(x_fs, XFT, j);
1094 chars++;
1097 if (chars) avg = (width + (chars>>1))/ chars;
1098 else avg = 0; /* No characters exist at all */
1100 else /* uniform width */
1101 avg = x_fs->min_bounds.width;
1103 return avg;
1106 /***********************************************************************
1107 * XFONT_GetMaxCharWidth
1109 static INT XFONT_GetMaxCharWidth(const XFontStruct* xfs, const XFONTTRANS *XFT)
1111 unsigned min = (unsigned char)xfs->min_char_or_byte2;
1112 unsigned max = (unsigned char)xfs->max_char_or_byte2;
1113 int maxwidth, j;
1115 if(!XFT || !xfs->per_char)
1116 return abs(xfs->max_bounds.width);
1118 for( j = 0, maxwidth = 0, max -= min; j <= max; j++ )
1119 if( !CI_NONEXISTCHAR(xfs->per_char + j) )
1120 if(maxwidth < xfs->per_char[j].attributes)
1121 maxwidth = xfs->per_char[j].attributes;
1123 maxwidth *= XFT->pixelsize / 1000.0;
1124 return maxwidth;
1127 /***********************************************************************
1128 * XFONT_SetFontMetric
1130 * INIT ONLY
1132 * Initializes IFONTINFO16.
1134 static void XFONT_SetFontMetric(fontInfo* fi, const fontResource* fr, XFontStruct* xfs)
1136 unsigned min, max;
1137 fi->df.dfFirstChar = (BYTE)(min = xfs->min_char_or_byte2);
1138 fi->df.dfLastChar = (BYTE)(max = xfs->max_char_or_byte2);
1140 fi->df.dfDefaultChar = (BYTE)xfs->default_char;
1141 fi->df.dfBreakChar = (BYTE)(( ' ' < min || ' ' > max) ? xfs->default_char: ' ');
1143 fi->df.dfPixHeight = (INT16)((fi->df.dfAscent = (INT16)xfs->ascent) + xfs->descent);
1144 fi->df.dfPixWidth = (xfs->per_char) ? 0 : xfs->min_bounds.width;
1146 XFONT_GetLeading( &fi->df, xfs, &fi->df.dfInternalLeading, &fi->df.dfExternalLeading, NULL );
1147 fi->df.dfAvgWidth = (INT16)XFONT_GetAvgCharWidth(&fi->df, xfs, NULL );
1148 fi->df.dfMaxWidth = (INT16)XFONT_GetMaxCharWidth(xfs, NULL);
1150 if( xfs->min_bounds.width != xfs->max_bounds.width )
1151 fi->df.dfPitchAndFamily |= TMPF_FIXED_PITCH; /* au contraire! */
1152 if( fi->fi_flags & FI_SCALABLE )
1154 fi->df.dfType = DEVICE_FONTTYPE;
1155 fi->df.dfPitchAndFamily |= TMPF_DEVICE;
1157 else if( fi->fi_flags & FI_TRUETYPE )
1158 fi->df.dfType = TRUETYPE_FONTTYPE;
1159 else
1160 fi->df.dfType = RASTER_FONTTYPE;
1162 fi->df.dfFace = fr->lfFaceName;
1165 /***********************************************************************
1166 * XFONT_GetFontMetric
1168 * Retrieve font metric info (enumeration).
1170 static UINT XFONT_GetFontMetric( const fontInfo* pfi,
1171 const LPENUMLOGFONTEXW pLF,
1172 const LPNEWTEXTMETRICEXW pTM )
1174 memset( pLF, 0, sizeof(*pLF) );
1175 memset( pTM, 0, sizeof(*pTM) );
1177 #define plf ((LPLOGFONTW)pLF)
1178 #define ptm ((LPNEWTEXTMETRICW)pTM)
1179 plf->lfHeight = ptm->tmHeight = pfi->df.dfPixHeight;
1180 plf->lfWidth = ptm->tmAveCharWidth = pfi->df.dfAvgWidth;
1181 plf->lfWeight = ptm->tmWeight = pfi->df.dfWeight;
1182 plf->lfItalic = ptm->tmItalic = pfi->df.dfItalic;
1183 plf->lfUnderline = ptm->tmUnderlined = pfi->df.dfUnderline;
1184 plf->lfStrikeOut = ptm->tmStruckOut = pfi->df.dfStrikeOut;
1185 plf->lfCharSet = ptm->tmCharSet = pfi->df.dfCharSet;
1187 /* convert pitch values */
1189 ptm->tmPitchAndFamily = pfi->df.dfPitchAndFamily;
1190 plf->lfPitchAndFamily = (pfi->df.dfPitchAndFamily & 0xF1) + 1;
1192 MultiByteToWideChar(CP_ACP, 0, pfi->df.dfFace, -1,
1193 plf->lfFaceName, LF_FACESIZE);
1195 /* FIXME: fill in rest of plF values */
1196 strcpyW(pLF->elfFullName, plf->lfFaceName);
1197 MultiByteToWideChar(CP_ACP, 0, "Regular", -1,
1198 pLF->elfStyle, LF_FACESIZE);
1199 MultiByteToWideChar(CP_ACP, 0, plf->lfCharSet == SYMBOL_CHARSET ?
1200 "Symbol" : "Roman", -1,
1201 pLF->elfScript, LF_FACESIZE);
1203 #undef plf
1205 ptm->tmAscent = pfi->df.dfAscent;
1206 ptm->tmDescent = ptm->tmHeight - ptm->tmAscent;
1207 ptm->tmInternalLeading = pfi->df.dfInternalLeading;
1208 ptm->tmMaxCharWidth = pfi->df.dfMaxWidth;
1209 ptm->tmDigitizedAspectX = pfi->df.dfHorizRes;
1210 ptm->tmDigitizedAspectY = pfi->df.dfVertRes;
1212 ptm->tmFirstChar = pfi->df.dfFirstChar;
1213 ptm->tmLastChar = pfi->df.dfLastChar;
1214 ptm->tmDefaultChar = pfi->df.dfDefaultChar;
1215 ptm->tmBreakChar = pfi->df.dfBreakChar;
1217 TRACE("Calling Enum proc with FaceName %s FullName %s\n",
1218 debugstr_w(pLF->elfLogFont.lfFaceName),
1219 debugstr_w(pLF->elfFullName));
1221 TRACE("CharSet = %d type = %d\n", ptm->tmCharSet, pfi->df.dfType);
1222 /* return font type */
1223 return pfi->df.dfType;
1224 #undef ptm
1228 /***********************************************************************
1229 * XFONT_FixupFlags
1231 * INIT ONLY
1233 * dfPitchAndFamily flags for some common typefaces.
1235 static BYTE XFONT_FixupFlags( LPCSTR lfFaceName )
1237 switch( lfFaceName[0] )
1239 case 'a':
1240 case 'A': if(!strncasecmp(lfFaceName, "Arial", 5) )
1241 return FF_SWISS;
1242 break;
1243 case 'h':
1244 case 'H': if(!strcasecmp(lfFaceName, "Helvetica") )
1245 return FF_SWISS;
1246 break;
1247 case 'c':
1248 case 'C': if(!strncasecmp(lfFaceName, "Courier", 7))
1249 return FF_MODERN;
1251 if (!strcasecmp(lfFaceName, "Charter") )
1252 return FF_ROMAN;
1253 break;
1254 case 'p':
1255 case 'P': if( !strcasecmp(lfFaceName,"Palatino") )
1256 return FF_ROMAN;
1257 break;
1258 case 't':
1259 case 'T': if(!strncasecmp(lfFaceName, "Times", 5) )
1260 return FF_ROMAN;
1261 break;
1262 case 'u':
1263 case 'U': if(!strcasecmp(lfFaceName, "Utopia") )
1264 return FF_ROMAN;
1265 break;
1266 case 'z':
1267 case 'Z': if(!strcasecmp(lfFaceName, "Zapf Dingbats") )
1268 return FF_DECORATIVE;
1270 return 0;
1273 /***********************************************************************
1274 * XFONT_SameFoundryAndFamily
1276 * INIT ONLY
1278 static BOOL XFONT_SameFoundryAndFamily( const LFD* lfd1, const LFD* lfd2 )
1280 return ( !strcasecmp( lfd1->foundry, lfd2->foundry ) &&
1281 !strcasecmp( lfd1->family, lfd2->family ) );
1284 /***********************************************************************
1285 * XFONT_InitialCapitals
1287 * INIT ONLY
1289 * Upper case first letters of words & remove multiple spaces
1291 static void XFONT_InitialCapitals(LPSTR lpch)
1293 int i;
1294 BOOL up;
1295 char* lpstr = lpch;
1297 for( i = 0, up = TRUE; *lpch; lpch++, i++ )
1299 if( isspace(*lpch) )
1301 if (!up) /* Not already got one */
1303 *lpstr++ = ' ';
1304 up = TRUE;
1307 else if( isalpha(*lpch) && up )
1309 *lpstr++ = toupper(*lpch);
1310 up = FALSE;
1312 else
1314 *lpstr++ = *lpch;
1315 up = FALSE;
1319 /* Remove possible trailing space */
1320 if (up && i > 0)
1321 --lpstr;
1322 *lpstr = '\0';
1326 /***********************************************************************
1327 * XFONT_WindowsNames
1329 * INIT ONLY
1331 * Build generic Windows aliases for X font names.
1333 * -misc-fixed- -> "Fixed"
1334 * -sony-fixed- -> "Sony Fixed", etc...
1336 static void XFONT_WindowsNames(void)
1338 fontResource* fr;
1340 for( fr = fontList; fr ; fr = fr->next )
1342 fontResource* pfr;
1343 char* lpch;
1345 if( fr->fr_flags & FR_NAMESET ) continue; /* skip already assigned */
1347 for( pfr = fontList; pfr != fr ; pfr = pfr->next )
1348 if( pfr->fr_flags & FR_NAMESET )
1350 if (!strcasecmp( pfr->resource->family, fr->resource->family))
1351 break;
1354 lpch = fr->lfFaceName;
1355 snprintf( fr->lfFaceName, sizeof(fr->lfFaceName), "%s %s",
1356 /* prepend vendor name */
1357 (pfr==fr) ? "" : fr->resource->foundry,
1358 fr->resource->family);
1359 XFONT_InitialCapitals(fr->lfFaceName);
1361 BYTE bFamilyStyle = XFONT_FixupFlags( fr->lfFaceName );
1362 if( bFamilyStyle)
1364 fontInfo* fi;
1365 for( fi = fr->fi ; fi ; fi = fi->next )
1366 fi->df.dfPitchAndFamily |= bFamilyStyle;
1370 TRACE("typeface '%s'\n", fr->lfFaceName);
1372 fr->fr_flags |= FR_NAMESET;
1376 /***********************************************************************
1377 * XFONT_LoadDefaultLFD
1379 * Move lfd to the head of fontList to make it more likely to be matched
1381 static void XFONT_LoadDefaultLFD(LFD* lfd, LPCSTR fonttype)
1384 fontResource *fr, *pfr;
1385 for( fr = NULL, pfr = fontList; pfr; pfr = pfr->next )
1387 if( XFONT_SameFoundryAndFamily(pfr->resource, lfd) )
1389 if( fr )
1391 fr->next = pfr->next;
1392 pfr->next = fontList;
1393 fontList = pfr;
1395 break;
1397 fr = pfr;
1399 if (!pfr)
1400 WARN("Default %sfont '-%s-%s-' not available\n", fonttype,
1401 lfd->foundry, lfd->family);
1405 /***********************************************************************
1406 * XFONT_LoadDefault
1408 static void XFONT_LoadDefault(LPCSTR ini, LPCSTR fonttype)
1410 char buffer[MAX_LFD_LENGTH];
1411 HKEY hkey;
1413 buffer[0] = 0;
1414 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1416 DWORD type, count = sizeof(buffer);
1417 RegQueryValueExA(hkey, ini, 0, &type, buffer, &count);
1418 RegCloseKey(hkey);
1420 if (*buffer)
1422 LFD* lfd;
1423 char* pch = buffer;
1424 while( *pch && isspace(*pch) ) pch++;
1426 TRACE("Using '%s' as default %sfont\n", pch, fonttype);
1427 lfd = LFD_Parse(pch);
1428 if (lfd && lfd->foundry && lfd->family)
1429 XFONT_LoadDefaultLFD(lfd, fonttype);
1430 else
1431 WARN("Ini section [%s]%s is malformed\n", INIFontSection, ini);
1432 HeapFree(GetProcessHeap(), 0, lfd);
1437 /***********************************************************************
1438 * XFONT_LoadDefaults
1440 static void XFONT_LoadDefaults(void)
1442 XFONT_LoadDefault(INIDefaultFixed, "fixed ");
1443 XFONT_LoadDefault(INIDefault, "");
1446 /***********************************************************************
1447 * XFONT_CreateAlias
1449 static fontAlias* XFONT_CreateAlias( LPCSTR lpTypeFace, LPCSTR lpAlias )
1451 int j;
1452 fontAlias *pfa, *prev = NULL;
1454 for(pfa = aliasTable; pfa; pfa = pfa->next)
1456 /* check if we already got one */
1457 if( !strcasecmp( pfa->faTypeFace, lpAlias ) )
1459 TRACE("redundant alias '%s' -> '%s'\n",
1460 lpAlias, lpTypeFace );
1461 return NULL;
1463 prev = pfa;
1466 j = strlen(lpTypeFace) + 1;
1467 pfa = HeapAlloc( GetProcessHeap(), 0, sizeof(fontAlias) +
1468 j + strlen(lpAlias) + 1 );
1469 if (pfa)
1471 if (!prev)
1472 aliasTable = pfa;
1473 else
1474 prev->next = pfa;
1476 pfa->next = NULL;
1477 pfa->faTypeFace = (LPSTR)(pfa + 1);
1478 strcpy( pfa->faTypeFace, lpTypeFace );
1479 pfa->faAlias = pfa->faTypeFace + j;
1480 strcpy( pfa->faAlias, lpAlias );
1482 TRACE("added alias '%s' for '%s'\n", lpAlias, lpTypeFace );
1484 return pfa;
1486 return NULL;
1490 /***********************************************************************
1491 * XFONT_LoadAlias
1493 static void XFONT_LoadAlias(const LFD* lfd, LPCSTR lpAlias, BOOL bSubst)
1495 fontResource *fr, *frMatch = NULL;
1496 if (!lfd->foundry || ! lfd->family)
1498 WARN("Malformed font resource for alias '%s'\n", lpAlias);
1499 return;
1501 for (fr = fontList; fr ; fr = fr->next)
1503 if(!strcasecmp(fr->resource->family, lpAlias))
1505 /* alias is not needed since the real font is present */
1506 TRACE("Ignoring font alias '%s' as it is already available as a real font\n", lpAlias);
1507 return;
1509 if( XFONT_SameFoundryAndFamily( fr->resource, lfd ) )
1511 frMatch = fr;
1512 break;
1516 if( frMatch )
1518 if( bSubst )
1520 fontAlias *pfa, *prev = NULL;
1522 for(pfa = aliasTable; pfa; pfa = pfa->next)
1524 /* Remove lpAlias from aliasTable - we should free the old entry */
1525 if(!strcmp(lpAlias, pfa->faAlias))
1527 if(prev)
1528 prev->next = pfa->next;
1529 else
1530 aliasTable = pfa->next;
1533 /* Update any references to the substituted font in aliasTable */
1534 if(!strcmp(frMatch->lfFaceName, pfa->faTypeFace))
1536 pfa->faTypeFace = HeapAlloc( GetProcessHeap(), 0, strlen(lpAlias)+1 );
1537 strcpy( pfa->faTypeFace, lpAlias );
1539 prev = pfa;
1542 TRACE("\tsubstituted '%s' with '%s'\n", frMatch->lfFaceName, lpAlias );
1544 lstrcpynA( frMatch->lfFaceName, lpAlias, LF_FACESIZE );
1545 frMatch->fr_flags |= FR_NAMESET;
1547 else
1549 /* create new entry in the alias table */
1550 XFONT_CreateAlias( frMatch->lfFaceName, lpAlias );
1553 else
1555 WARN("Font alias '-%s-%s-' is not available\n", lfd->foundry, lfd->family);
1559 /***********************************************************************
1560 * Just a copy of PROFILE_GetStringItem
1562 * Convenience function that turns a string 'xxx, yyy, zzz' into
1563 * the 'xxx\0 yyy, zzz' and returns a pointer to the 'yyy, zzz'.
1565 static char *XFONT_GetStringItem( char *start )
1567 #define XFONT_isspace(c) (isspace(c) || (c == '\r') || (c == 0x1a))
1568 char *lpchX, *lpch;
1570 for (lpchX = start, lpch = NULL; *lpchX != '\0'; lpchX++ )
1572 if( *lpchX == ',' )
1574 if( lpch ) *lpch = '\0'; else *lpchX = '\0';
1575 while( *(++lpchX) )
1576 if( !XFONT_isspace(*lpchX) ) return lpchX;
1578 else if( XFONT_isspace( *lpchX ) && !lpch ) lpch = lpchX;
1579 else lpch = NULL;
1581 if( lpch ) *lpch = '\0';
1582 return NULL;
1583 #undef XFONT_isspace
1586 /***********************************************************************
1587 * XFONT_LoadAliases
1589 * INIT ONLY
1591 * Create font aliases for some standard windows fonts using user's
1592 * default choice of (sans-)serif fonts
1594 * Read user-defined aliases from wine.conf. Format is as follows
1596 * Alias# = [Windows font name],[LFD font name], <substitute original name>
1598 * Example:
1599 * Alias0 = Arial, -adobe-helvetica-
1600 * Alias1 = Times New Roman, -bitstream-courier-, 1
1601 * ...
1603 * Note that from 970817 and on we have built-in alias templates that take
1604 * care of the necessary Windows typefaces.
1606 typedef struct
1608 LPSTR fatResource;
1609 LPSTR fatAlias;
1610 } aliasTemplate;
1612 static void XFONT_LoadAliases(void)
1614 char *lpResource;
1615 char buffer[MAX_LFD_LENGTH];
1616 int i = 0;
1617 LFD* lfd;
1618 HKEY hkey;
1620 /* built-ins first */
1621 strcpy(buffer, "-bitstream-charter-");
1622 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1624 DWORD type, count = sizeof(buffer);
1625 RegQueryValueExA(hkey, INIDefaultSerif, 0, &type, buffer, &count);
1626 RegCloseKey(hkey);
1628 TRACE("Using '%s' as default serif font\n", buffer);
1629 lfd = LFD_Parse(buffer);
1630 /* NB XFONT_InitialCapitals should not change these standard aliases */
1631 if (lfd)
1633 XFONT_LoadAlias( lfd, "Tms Roman", FALSE);
1634 XFONT_LoadAlias( lfd, "MS Serif", FALSE);
1635 XFONT_LoadAlias( lfd, "Times New Roman", FALSE);
1637 XFONT_LoadDefaultLFD( lfd, "serif ");
1638 HeapFree(GetProcessHeap(), 0, lfd);
1641 strcpy(buffer, "-adobe-helvetica-");
1642 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1644 DWORD type, count = sizeof(buffer);
1645 RegQueryValueExA(hkey, INIDefaultSansSerif, 0, &type, buffer, &count);
1646 RegCloseKey(hkey);
1648 TRACE("Using '%s' as default sans serif font\n", buffer);
1649 lfd = LFD_Parse(buffer);
1650 if (lfd)
1652 XFONT_LoadAlias( lfd, "Helv", FALSE);
1653 XFONT_LoadAlias( lfd, "MS Sans Serif", FALSE);
1654 XFONT_LoadAlias( lfd, "MS Shell Dlg", FALSE);
1655 XFONT_LoadAlias( lfd, "System", FALSE);
1656 XFONT_LoadAlias( lfd, "Arial", FALSE);
1658 XFONT_LoadDefaultLFD( lfd, "sans serif ");
1659 HeapFree(GetProcessHeap(), 0, lfd);
1662 /* then user specified aliases */
1665 BOOL bSubst;
1666 char subsection[32];
1667 snprintf( subsection, sizeof subsection, "%s%i", INIAliasSection, i++ );
1669 buffer[0] = 0;
1670 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1672 DWORD type, count = sizeof(buffer);
1673 RegQueryValueExA(hkey, subsection, 0, &type, buffer, &count);
1674 RegCloseKey(hkey);
1677 if (!buffer[0])
1678 break;
1680 XFONT_InitialCapitals(buffer);
1681 lpResource = XFONT_GetStringItem( buffer );
1682 bSubst = (XFONT_GetStringItem( lpResource )) ? TRUE : FALSE;
1683 if( lpResource && *lpResource )
1685 lfd = LFD_Parse(lpResource);
1686 if (lfd)
1688 XFONT_LoadAlias(lfd, buffer, bSubst);
1689 HeapFree(GetProcessHeap(), 0, lfd);
1692 else
1693 WARN("malformed font alias '%s'\n", buffer );
1695 while(TRUE);
1698 /***********************************************************************
1699 * XFONT_UnAlias
1701 * Convert an (potential) alias into a real windows name
1704 static LPCSTR XFONT_UnAlias(char* font)
1706 if (font[0])
1708 fontAlias* fa;
1709 XFONT_InitialCapitals(font); /* to remove extra white space */
1711 for( fa = aliasTable; fa; fa = fa->next )
1712 /* use case insensitive matching to handle eg "MS Sans Serif" */
1713 if( !strcasecmp( fa->faAlias, font ) )
1715 TRACE("found alias '%s'->%s'\n", font, fa->faTypeFace );
1716 strcpy(font, fa->faTypeFace);
1717 return fa->faAlias;
1718 break;
1721 return NULL;
1724 /***********************************************************************
1725 * XFONT_RemoveFontResource
1727 * Caller should check if the font resource is in use. If it is it should
1728 * set FR_REMOVED flag to delay removal until the resource is not in use
1729 * any more.
1731 void XFONT_RemoveFontResource( fontResource** ppfr )
1733 fontResource* pfr = *ppfr;
1734 #if 0
1735 fontInfo* pfi;
1737 /* FIXME - if fonts were read from a cache, these HeapFrees will fail */
1738 while( pfr->fi )
1740 pfi = pfr->fi->next;
1741 HeapFree( GetProcessHeap(), 0, pfr->fi );
1742 pfr->fi = pfi;
1744 HeapFree( GetProcessHeap(), 0, pfr );
1745 #endif
1746 *ppfr = pfr->next;
1749 /***********************************************************************
1750 * XFONT_LoadIgnores
1752 * INIT ONLY
1754 * Removes specified fonts from the font table to prevent Wine from
1755 * using it.
1757 * Ignore# = [LFD font name]
1759 * Example:
1760 * Ignore0 = -misc-nil-
1761 * Ignore1 = -sun-open look glyph-
1762 * ...
1765 static void XFONT_LoadIgnore(char* lfdname)
1767 fontResource** ppfr;
1769 LFD* lfd = LFD_Parse(lfdname);
1770 if (lfd && lfd->foundry && lfd->family)
1772 for( ppfr = &fontList; *ppfr ; ppfr = &((*ppfr)->next))
1774 if( XFONT_SameFoundryAndFamily( (*ppfr)->resource, lfd) )
1776 TRACE("Ignoring '-%s-%s-'\n",
1777 (*ppfr)->resource->foundry, (*ppfr)->resource->family );
1779 XFONT_RemoveFontResource( ppfr );
1780 break;
1784 else
1785 WARN("Malformed font resource\n");
1787 HeapFree(GetProcessHeap(), 0, lfd);
1790 static void XFONT_LoadIgnores(void)
1792 int i = 0;
1793 char subsection[32];
1794 char buffer[MAX_LFD_LENGTH];
1796 /* Standard one that noone wants */
1797 strcpy(buffer, "-misc-nil-");
1798 XFONT_LoadIgnore(buffer);
1800 /* Others from INI file */
1803 HKEY hkey;
1804 sprintf( subsection, "%s%i", INIIgnoreSection, i++ );
1806 buffer[0] = 0;
1807 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1809 DWORD type, count = sizeof(buffer);
1810 RegQueryValueExA(hkey, subsection, 0, &type, buffer, &count);
1811 RegCloseKey(hkey);
1814 if( buffer[0] )
1816 char* pch = buffer;
1817 while( *pch && isspace(*pch) ) pch++;
1818 XFONT_LoadIgnore(pch);
1820 else
1821 break;
1822 } while(TRUE);
1826 /***********************************************************************
1827 * XFONT_UserMetricsCache
1829 * Returns expanded name for the cachedmetrics file.
1830 * Now it also appends the current value of the $DISPLAY variable.
1832 static char* XFONT_UserMetricsCache( char* buffer, int* buf_size )
1834 const char *confdir = get_config_dir();
1835 const char *display_name = XDisplayName(NULL);
1836 int len = strlen(confdir) + strlen(INIFontMetrics) + strlen(display_name) + 8;
1837 int display = 0;
1838 int screen = 0;
1839 char *p, *ext;
1842 ** Normalize the display name, since on Red Hat systems, DISPLAY
1843 ** is commonly set to one of either 'unix:0.0' or ':0' or ':0.0'.
1844 ** after this code, all of the above will resolve to ':0.0'.
1846 if (!strncmp( display_name, "unix:", 5 )) display_name += 4;
1847 p = strchr(display_name, ':');
1848 if (p) sscanf(p + 1, "%d.%d", &display, &screen);
1850 if ((len > *buf_size) &&
1851 !(buffer = HeapReAlloc( GetProcessHeap(), 0, buffer, *buf_size = len )))
1853 ERR("out of memory\n");
1854 ExitProcess(1);
1856 sprintf( buffer, "%s/%s", confdir, INIFontMetrics );
1858 ext = buffer + strlen(buffer);
1859 strcpy( ext, display_name );
1861 if (!(p = strchr( ext, ':' ))) p = ext + strlen(ext);
1862 sprintf( p, ":%d.%d", display, screen );
1863 return buffer;
1867 /***********************************************************************
1868 * X Font Matching
1870 * Compare two fonts (only parameters set by the XFONT_InitFontInfo()).
1872 static INT XFONT_IsSubset(const fontInfo* match, const fontInfo* fi)
1874 INT m;
1876 /* 0 - keep both, 1 - keep match, -1 - keep fi */
1878 /* Compare dfItalic, Underline, Strikeout, Weight, Charset */
1879 m = (BYTE*)&fi->df.dfPixWidth - (BYTE*)&fi->df.dfItalic;
1880 if( memcmp(&match->df.dfItalic, &fi->df.dfItalic, m )) return 0;
1882 if( (!((fi->fi_flags & FI_SCALABLE) + (match->fi_flags & FI_SCALABLE))
1883 && fi->lfd_height != match->lfd_height) ||
1884 (!((fi->fi_flags & FI_POLYWEIGHT) + (match->fi_flags & FI_POLYWEIGHT))
1885 && fi->df.dfWeight != match->df.dfWeight) ) return 0;
1887 m = (int)(match->fi_flags & (FI_POLYWEIGHT | FI_SCALABLE)) -
1888 (int)(fi->fi_flags & (FI_SCALABLE | FI_POLYWEIGHT));
1890 if( m == (FI_POLYWEIGHT - FI_SCALABLE) ||
1891 m == (FI_SCALABLE - FI_POLYWEIGHT) ) return 0; /* keep both */
1892 else if( m >= 0 ) return 1; /* 'match' is better */
1894 return -1; /* 'fi' is better */
1897 /***********************************************************************
1898 * XFONT_CheckFIList
1900 * REMOVE_SUBSETS - attach new fi and purge subsets
1901 * UNMARK_SUBSETS - remove subset flags from all fi entries
1903 static void XFONT_CheckFIList( fontResource* fr, fontInfo* fi, int action)
1905 int i = 0;
1906 fontInfo* pfi, *prev;
1908 for( prev = NULL, pfi = fr->fi; pfi; )
1910 if( action == REMOVE_SUBSETS )
1912 if( pfi->fi_flags & FI_SUBSET )
1914 fontInfo* subset = pfi;
1916 i++;
1917 fr->fi_count--;
1918 if( prev ) prev->next = pfi = pfi->next;
1919 else fr->fi = pfi = pfi->next;
1920 HeapFree( GetProcessHeap(), 0, subset );
1921 continue;
1924 else pfi->fi_flags &= ~FI_SUBSET;
1926 prev = pfi;
1927 pfi = pfi->next;
1930 if( action == REMOVE_SUBSETS ) /* also add the superset */
1932 if( fi->fi_flags & FI_SCALABLE )
1934 fi->next = fr->fi;
1935 fr->fi = fi;
1937 else if( prev ) prev->next = fi; else fr->fi = fi;
1938 fr->fi_count++;
1941 if( i ) TRACE("\t purged %i subsets [%i]\n", i , fr->fi_count);
1944 /***********************************************************************
1945 * XFONT_FindFIList
1947 static fontResource* XFONT_FindFIList( fontResource* pfr, const char* pTypeFace )
1949 while( pfr )
1951 if( !strcasecmp( pfr->lfFaceName, pTypeFace ) ) break;
1952 pfr = pfr->next;
1954 /* Give the app back the font name it asked for. Encarta checks this. */
1955 if (pfr) strcpy(pfr->lfFaceName,pTypeFace);
1956 return pfr;
1959 /***********************************************************************
1960 * XFONT_FixupPointSize
1962 static void XFONT_FixupPointSize(fontInfo* fi)
1964 #define df (fi->df)
1965 df.dfHorizRes = df.dfVertRes = fi->lfd_resolution;
1966 df.dfPoints = (INT16)
1967 (((INT)(df.dfPixHeight - df.dfInternalLeading) * 72 + (df.dfVertRes >> 1)) /
1968 df.dfVertRes );
1969 #undef df
1972 /***********************************************************************
1973 * XFONT_BuildMetrics
1975 * Build font metrics from X font
1977 static int XFONT_BuildMetrics(char** x_pattern, int res, unsigned x_checksum, int x_count)
1979 int i;
1980 fontInfo* fi = NULL;
1981 fontResource* fr, *pfr;
1982 int n_ff = 0;
1984 MESSAGE("Building font metrics. This may take some time...\n");
1985 for( i = 0; i < x_count; i++ )
1987 char* typeface;
1988 LFD* lfd;
1989 int j;
1990 char buffer[MAX_LFD_LENGTH];
1991 char* lpstr;
1992 XFontStruct* x_fs;
1993 fontInfo* pfi;
1995 if (!(typeface = HeapAlloc(GetProcessHeap(), 0, strlen(x_pattern[i])+1))) break;
1996 strcpy( typeface, x_pattern[i] );
1998 lfd = LFD_Parse(typeface);
1999 if (!lfd)
2001 HeapFree(GetProcessHeap(), 0, typeface);
2002 continue;
2005 /* find a family to insert into */
2007 for( pfr = NULL, fr = fontList; fr; fr = fr->next )
2009 if( XFONT_SameFoundryAndFamily(fr->resource, lfd))
2010 break;
2011 pfr = fr;
2014 if( !fi ) fi = (fontInfo*) HeapAlloc(GetProcessHeap(), 0, sizeof(fontInfo));
2016 if( !LFD_InitFontInfo( fi, lfd, x_pattern[i]) )
2017 goto nextfont;
2019 if( !fr ) /* add new family */
2021 n_ff++;
2022 fr = (fontResource*) HeapAlloc(GetProcessHeap(), 0, sizeof(fontResource));
2023 if (fr)
2025 memset(fr, 0, sizeof(fontResource));
2027 fr->resource = (LFD*) HeapAlloc(GetProcessHeap(), 0, sizeof(LFD));
2028 memset(fr->resource, 0, sizeof(LFD));
2030 TRACE("family: -%s-%s-\n", lfd->foundry, lfd->family );
2031 fr->resource->foundry = HeapAlloc(GetProcessHeap(), 0, strlen(lfd->foundry)+1);
2032 strcpy( (char *)fr->resource->foundry, lfd->foundry );
2033 fr->resource->family = HeapAlloc(GetProcessHeap(), 0, strlen(lfd->family)+1);
2034 strcpy( (char *)fr->resource->family, lfd->family );
2035 fr->resource->weight = "";
2037 if( pfr ) pfr->next = fr;
2038 else fontList = fr;
2040 else
2041 WARN("Not enough memory for a new font family\n");
2044 /* check if we already have something better than "fi" */
2046 for( pfi = fr->fi, j = 0; pfi && j <= 0; pfi = pfi->next )
2047 if( (j = XFONT_IsSubset( pfi, fi )) < 0 )
2048 pfi->fi_flags |= FI_SUBSET; /* superseded by "fi" */
2049 if( j > 0 ) goto nextfont;
2051 /* add new font instance "fi" to the "fr" font resource */
2053 if( fi->fi_flags & FI_SCALABLE )
2055 LFD lfd1;
2056 char pxl_string[4], res_string[4];
2057 /* set scalable font height to get an basis for extrapolation */
2059 fi->lfd_height = DEF_SCALABLE_HEIGHT;
2060 fi->lfd_resolution = res;
2062 sprintf(pxl_string, "%d", fi->lfd_height);
2063 sprintf(res_string, "%d", fi->lfd_resolution);
2065 lfd1 = *lfd;
2066 lfd1.pixel_size = pxl_string;
2067 lfd1.point_size = "*";
2068 lfd1.resolution_x = res_string;
2069 lfd1.resolution_y = res_string;
2071 LFD_UnParse(buffer, sizeof buffer, &lfd1);
2073 lpstr = buffer;
2075 else lpstr = x_pattern[i];
2077 if( (x_fs = TSXLoadQueryFont(gdi_display, lpstr)) )
2079 XFONT_SetFontMetric( fi, fr, x_fs );
2080 TSXFreeFont( gdi_display, x_fs );
2082 XFONT_FixupPointSize(fi);
2084 TRACE("\t[% 2ipt] '%s'\n", fi->df.dfPoints, x_pattern[i] );
2086 XFONT_CheckFIList( fr, fi, REMOVE_SUBSETS );
2087 fi = NULL; /* preventing reuse */
2089 else
2091 ERR("failed to load %s\n", lpstr );
2093 XFONT_CheckFIList( fr, fi, UNMARK_SUBSETS );
2095 nextfont:
2096 HeapFree(GetProcessHeap(), 0, lfd);
2097 HeapFree(GetProcessHeap(), 0, typeface);
2099 if( fi ) HeapFree(GetProcessHeap(), 0, fi);
2101 /* Scan through the font list and remove FontResorce(s) (fr)
2102 * that have no associated Fontinfo(s) (fi).
2103 * This code is necessary because XFONT_ReadCachedMetrics
2104 * assumes that there is at least one fi associated with a fr.
2105 * This assumption is invalid for TT font
2106 * -altsys-ms outlook-medium-r-normal--0-0-0-0-p-0-microsoft-symbol.
2109 fr = fontList;
2111 while (!fr->fi_count)
2113 fontList = fr->next;
2115 HeapFree(GetProcessHeap(), 0, fr->resource);
2116 HeapFree(GetProcessHeap(), 0, fr);
2118 fr = fontList;
2119 n_ff--;
2122 fr = fontList;
2124 while (fr->next)
2126 if (!fr->next->fi_count)
2128 pfr = fr->next;
2129 fr->next = fr->next->next;
2131 HeapFree(GetProcessHeap(), 0, pfr->resource);
2132 HeapFree(GetProcessHeap(), 0, pfr);
2134 n_ff--;
2136 else
2137 fr = fr->next;
2140 return n_ff;
2143 /***********************************************************************
2144 * XFONT_ReadCachedMetrics
2146 * INIT ONLY
2148 static BOOL XFONT_ReadCachedMetrics( int fd, int res, unsigned x_checksum, int x_count )
2150 if( fd >= 0 )
2152 unsigned u;
2153 int i, j;
2155 /* read checksums */
2156 read( fd, &u, sizeof(unsigned) );
2157 read( fd, &i, sizeof(int) );
2159 if( u == x_checksum && i == x_count )
2161 off_t length, offset = 3 * sizeof(int);
2163 /* read total size */
2164 read( fd, &i, sizeof(int) );
2165 length = lseek( fd, 0, SEEK_END );
2167 if( length == (i + offset) )
2169 lseek( fd, offset, SEEK_SET );
2170 fontList = (fontResource*)HeapAlloc( GetProcessHeap(), 0, i);
2171 if( fontList )
2173 fontResource* pfr = fontList;
2174 fontInfo* pfi = NULL;
2176 TRACE("Reading cached font metrics:\n");
2178 read( fd, fontList, i); /* read all metrics at once */
2179 while( offset < length )
2181 offset += sizeof(fontResource) + sizeof(fontInfo);
2182 pfr->fi = pfi = (fontInfo*)(pfr + 1);
2183 j = 1;
2184 while( TRUE )
2186 if( offset > length ||
2187 pfi->cptable >= (UINT16)X11DRV_CPTABLE_COUNT ||
2188 (int)(pfi->next) != j++ ) goto fail;
2190 if( pfi->df.dfPixHeight == 0 ) goto fail;
2192 pfi->df.dfFace = pfr->lfFaceName;
2193 if( pfi->fi_flags & FI_SCALABLE )
2195 /* we can pretend we got this font for any resolution */
2196 pfi->lfd_resolution = res;
2197 XFONT_FixupPointSize(pfi);
2199 pfi->next = pfi + 1;
2201 if( j > pfr->fi_count ) break;
2203 pfi = pfi->next;
2204 offset += sizeof(fontInfo);
2206 pfi->next = NULL;
2207 if( pfr->next )
2209 pfr->next = (fontResource*)(pfi + 1);
2210 pfr = pfr->next;
2212 else break;
2214 if( pfr->next == NULL &&
2215 *(int*)(pfi + 1) == X_FMC_MAGIC )
2217 /* read LFD stubs */
2218 char* lpch = (char*)((int*)(pfi + 1) + 1);
2219 offset += sizeof(int);
2220 for( pfr = fontList; pfr; pfr = pfr->next )
2222 size_t len = strlen(lpch) + 1;
2223 TRACE("\t%s, %i instances\n", lpch, pfr->fi_count );
2224 pfr->resource = LFD_Parse(lpch);
2225 lpch += len;
2226 offset += len;
2227 if (offset > length)
2228 goto fail;
2230 close( fd );
2231 return TRUE;
2236 fail:
2237 if( fontList ) HeapFree( GetProcessHeap(), 0, fontList );
2238 fontList = NULL;
2239 close( fd );
2241 return FALSE;
2244 /***********************************************************************
2245 * XFONT_WriteCachedMetrics
2247 * INIT ONLY
2249 static BOOL XFONT_WriteCachedMetrics( int fd, unsigned x_checksum, int x_count, int n_ff )
2251 fontResource* pfr;
2252 fontInfo* pfi;
2254 if( fd >= 0 )
2256 int i, j, k;
2257 char buffer[MAX_LFD_LENGTH];
2259 /* font metrics file:
2261 * +0000 x_checksum
2262 * +0004 x_count
2263 * +0008 total size to load
2264 * +000C prepackaged font metrics
2265 * ...
2266 * +...x X_FMC_MAGIC
2267 * +...x + 4 LFD stubs
2270 write( fd, &x_checksum, sizeof(unsigned) );
2271 write( fd, &x_count, sizeof(int) );
2273 for( j = i = 0, pfr = fontList; pfr; pfr = pfr->next )
2275 LFD_UnParse(buffer, sizeof buffer, pfr->resource);
2276 i += strlen( buffer) + 1;
2277 j += pfr->fi_count;
2279 i += n_ff * sizeof(fontResource) + j * sizeof(fontInfo) + sizeof(int);
2280 write( fd, &i, sizeof(int) );
2282 TRACE("Writing font cache:\n");
2284 for( pfr = fontList; pfr; pfr = pfr->next )
2286 fontInfo fi;
2288 TRACE("\t-%s-%s-, %i instances\n", pfr->resource->foundry, pfr->resource->family, pfr->fi_count );
2290 i = write( fd, pfr, sizeof(fontResource) );
2291 if( i == sizeof(fontResource) )
2293 for( k = 1, pfi = pfr->fi; pfi; pfi = pfi->next )
2295 fi = *pfi;
2297 fi.df.dfFace = NULL;
2298 fi.next = (fontInfo*)k; /* loader checks this */
2300 j = write( fd, &fi, sizeof(fi) );
2301 k++;
2303 if( j == sizeof(fontInfo) ) continue;
2305 break;
2307 if( i == sizeof(fontResource) && j == sizeof(fontInfo) )
2309 i = j = X_FMC_MAGIC;
2310 write( fd, &i, sizeof(int) );
2311 for( pfr = fontList; pfr && i == j; pfr = pfr->next )
2313 LFD_UnParse(buffer, sizeof buffer, pfr->resource);
2314 i = strlen( buffer ) + 1;
2315 j = write( fd, buffer, i );
2318 close( fd );
2319 return ( i == j );
2321 return FALSE;
2324 /***********************************************************************
2325 * XFONT_GetPointResolution()
2327 * INIT ONLY
2329 * Here we initialize DefResolution which is used in the
2330 * XFONT_Match() penalty function. We also load the point
2331 * resolution value (higher values result in larger fonts).
2333 static int XFONT_GetPointResolution( int *log_pixels_x, int *log_pixels_y )
2335 int i, j, point_resolution, num = 3;
2336 int allowed_xfont_resolutions[3] = { 72, 75, 100 };
2337 int best = 0, best_diff = 65536;
2338 HKEY hkey;
2340 point_resolution = 0;
2342 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
2344 char buffer[20];
2345 DWORD type, count = sizeof(buffer);
2346 if(!RegQueryValueExA(hkey, INIResolution, 0, &type, buffer, &count))
2347 point_resolution = atoi(buffer);
2348 RegCloseKey(hkey);
2351 if( !point_resolution )
2352 point_resolution = *log_pixels_y;
2353 else
2354 *log_pixels_x = *log_pixels_y = point_resolution;
2357 /* FIXME We can only really guess at a best DefResolution
2358 * - this should be configurable
2360 for( i = best = 0; i < num; i++ )
2362 j = abs( point_resolution - allowed_xfont_resolutions[i] );
2363 if( j < best_diff )
2365 best = i;
2366 best_diff = j;
2369 DefResolution = allowed_xfont_resolutions[best];
2370 return point_resolution;
2374 /***********************************************************************
2375 * XFONT_Match
2377 * Compute the matching score between the logical font and the device font.
2379 * contributions from highest to lowest:
2380 * charset
2381 * fixed pitch
2382 * height
2383 * family flags (only when the facename is not present)
2384 * width
2385 * weight, italics, underlines, strikeouts
2387 * NOTE: you can experiment with different penalty weights to see what happens.
2388 * http://premium.microsoft.com/msdn/library/techart/f365/f36b/f37b/d38b/sa8bf.htm
2390 static UINT XFONT_Match( fontMatch* pfm )
2392 fontInfo* pfi = pfm->pfi; /* device font to match */
2393 LPLOGFONT16 plf = pfm->plf; /* wanted logical font */
2394 UINT penalty = 0;
2395 BOOL bR6 = pfm->flags & FO_MATCH_XYINDEP; /* from TextCaps */
2396 BOOL bScale = pfi->fi_flags & FI_SCALABLE;
2397 int d = 0, height;
2399 TRACE("\t[ %-2ipt h=%-3i w=%-3i %s%s]\n", pfi->df.dfPoints,
2400 pfi->df.dfPixHeight, pfi->df.dfAvgWidth,
2401 (pfi->df.dfWeight > FW_NORMAL) ? "Bold " : "Normal ",
2402 (pfi->df.dfItalic) ? "Italic" : "" );
2404 pfm->flags &= FO_MATCH_MASK;
2406 /* Charset */
2407 /* pfm->internal_charset: given(required) charset */
2408 /* pfi->internal_charset: charset of this font */
2409 if (pfi->internal_charset == DEFAULT_CHARSET)
2411 /* special case(unicode font) */
2412 /* priority: unmatched charset < unicode < matched charset */
2413 penalty += 0x50;
2415 else
2417 if( pfm->internal_charset == DEFAULT_CHARSET )
2420 if (pfi->internal_charset != ANSI_CHARSET)
2421 penalty += 0x200;
2423 if ( pfi->codepage != GetACP() )
2424 penalty += 0x200;
2426 else if (pfm->internal_charset != pfi->internal_charset)
2428 if ( pfi->internal_charset & 0xff00 )
2429 penalty += 0x1000; /* internal charset - should not be used */
2430 else
2431 penalty += 0x200;
2435 /* Height */
2436 height = -1;
2438 if( plf->lfHeight > 0 )
2440 int h = pfi->df.dfPixHeight;
2441 d = h - plf->lfHeight;
2442 height = plf->lfHeight;
2444 else
2446 int h = pfi->df.dfPixHeight - pfi->df.dfInternalLeading;
2447 if (h)
2449 d = h + plf->lfHeight;
2450 height = (-plf->lfHeight * pfi->df.dfPixHeight) / h;
2452 else
2454 ERR("PixHeight == InternalLeading\n");
2455 penalty += 0x1000; /* dont want this */
2460 if( height == 0 )
2461 pfm->height = 1; /* Very small */
2462 else if( d )
2464 if( bScale )
2465 pfm->height = height;
2466 else if( (plf->lfQuality != PROOF_QUALITY) && bR6 )
2468 if( d > 0 ) /* do not shrink raster fonts */
2470 pfm->height = pfi->df.dfPixHeight;
2471 penalty += (pfi->df.dfPixHeight - height) * 0x4;
2473 else /* expand only in integer multiples */
2475 pfm->height = height - height%pfi->df.dfPixHeight;
2476 penalty += (height - pfm->height + 1) * height / pfi->df.dfPixHeight;
2479 else /* can't be scaled at all */
2481 if( plf->lfQuality != PROOF_QUALITY) pfm->flags |= FO_SYNTH_HEIGHT;
2482 pfm->height = pfi->df.dfPixHeight;
2483 penalty += (d > 0)? d * 0x8 : -d * 0x10;
2486 else
2487 pfm->height = pfi->df.dfPixHeight;
2489 /* Pitch and Family */
2490 if( pfm->flags & FO_MATCH_PAF ) {
2491 int family = plf->lfPitchAndFamily & FF_FAMILY;
2493 /* TMPF_FIXED_PITCH means exactly the opposite */
2494 if( plf->lfPitchAndFamily & FIXED_PITCH ) {
2495 if( pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH ) penalty += 0x100;
2496 } else /* Variable is the default */
2497 if( !(pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH) ) penalty += 0x2;
2499 if (family != FF_DONTCARE && family != (pfi->df.dfPitchAndFamily & FF_FAMILY) )
2500 penalty += 0x10;
2503 /* Width */
2504 if( plf->lfWidth )
2506 int h;
2507 if( bR6 || bScale ) h = 0;
2508 else
2510 /* FIXME: not complete */
2512 pfm->flags |= FO_SYNTH_WIDTH;
2513 h = abs(plf->lfWidth - (pfm->height * pfi->df.dfAvgWidth)/pfi->df.dfPixHeight);
2515 penalty += h * ( d ) ? 0x2 : 0x1 ;
2517 else if( !(pfi->fi_flags & FI_NORMAL) ) penalty++;
2519 /* Weight */
2520 if( plf->lfWeight != FW_DONTCARE )
2522 penalty += abs(plf->lfWeight - pfi->df.dfWeight) / 40;
2523 if( plf->lfWeight > pfi->df.dfWeight ) pfm->flags |= FO_SYNTH_BOLD;
2524 } else if( pfi->df.dfWeight >= FW_BOLD ) penalty++; /* choose normal by default */
2526 /* Italic */
2527 if( plf->lfItalic != pfi->df.dfItalic )
2529 penalty += 0x4;
2530 pfm->flags |= FO_SYNTH_ITALIC;
2532 /* Underline */
2533 if( plf->lfUnderline ) pfm->flags |= FO_SYNTH_UNDERLINE;
2535 /* Strikeout */
2536 if( plf->lfStrikeOut ) pfm->flags |= FO_SYNTH_STRIKEOUT;
2539 if( penalty && !bScale && pfi->lfd_resolution != DefResolution )
2540 penalty++;
2542 TRACE(" returning %i\n", penalty );
2544 return penalty;
2547 /***********************************************************************
2548 * XFONT_MatchFIList
2550 * Scan a particular font resource for the best match.
2552 static UINT XFONT_MatchFIList( fontMatch* pfm )
2554 BOOL skipRaster = (pfm->flags & FO_MATCH_NORASTER);
2555 UINT current_score, score = (UINT)(-1);
2556 fontMatch fm = *pfm;
2558 for( fm.pfi = pfm->pfr->fi; fm.pfi && score; fm.pfi = fm.pfi->next)
2560 if( skipRaster && !(fm.pfi->fi_flags & FI_SCALABLE) )
2561 continue;
2563 current_score = XFONT_Match( &fm );
2564 if( score > current_score )
2566 *pfm = fm;
2567 score = current_score;
2570 return score;
2573 /***********************************************************************
2574 * XFONT_MatchDeviceFont
2576 * Scan font resource tree.
2579 static void XFONT_MatchDeviceFont( fontResource* start, fontMatch* pfm)
2581 fontMatch fm = *pfm;
2582 UINT current_score, score = (UINT)(-1);
2583 fontResource** ppfr;
2585 TRACE("(%u) '%s' h=%i weight=%i %s\n",
2586 pfm->plf->lfCharSet, pfm->plf->lfFaceName, pfm->plf->lfHeight,
2587 pfm->plf->lfWeight, (pfm->plf->lfItalic) ? "Italic" : "" );
2589 pfm->pfi = NULL;
2590 if( fm.plf->lfFaceName[0] )
2592 fm.pfr = XFONT_FindFIList( start, fm.plf->lfFaceName);
2593 if( fm.pfr ) /* match family */
2595 TRACE("found facename '%s'\n", fm.pfr->lfFaceName );
2597 if( fm.pfr->fr_flags & FR_REMOVED )
2598 fm.pfr = 0;
2599 else
2601 XFONT_MatchFIList( &fm );
2602 *pfm = fm;
2603 if (pfm->pfi)
2604 return;
2608 /* get charset if lfFaceName is one of known facenames. */
2610 const struct CharsetBindingInfo* pcharsetbindings;
2612 pcharsetbindings = &charsetbindings[0];
2613 while ( pcharsetbindings->pszFaceName != NULL )
2615 if ( !strcmp( pcharsetbindings->pszFaceName,
2616 fm.plf->lfFaceName ) )
2618 fm.internal_charset = pcharsetbindings->charset;
2619 break;
2621 pcharsetbindings ++;
2623 TRACE( "%s charset %u\n", fm.plf->lfFaceName, fm.internal_charset );
2627 /* match all available fonts */
2629 fm.flags |= FO_MATCH_PAF;
2630 for( ppfr = &fontList; *ppfr && score; ppfr = &(*ppfr)->next )
2632 if( (*ppfr)->fr_flags & FR_REMOVED )
2634 if( (*ppfr)->fo_count == 0 )
2635 XFONT_RemoveFontResource( ppfr );
2636 continue;
2639 fm.pfr = *ppfr;
2641 TRACE("%s\n", fm.pfr->lfFaceName );
2643 current_score = XFONT_MatchFIList( &fm );
2644 if( current_score < score )
2646 score = current_score;
2647 *pfm = fm;
2653 /***********************************************************************
2654 * X Font Cache
2656 static void XFONT_GrowFreeList(int start, int end)
2658 /* add all entries from 'start' up to and including 'end' */
2660 memset( fontCache + start, 0, (end - start + 1) * sizeof(fontObject) );
2662 fontCache[end].lru = fontLF;
2663 fontCache[end].count = -1;
2664 fontLF = start;
2665 while( start < end )
2667 fontCache[start].count = -1;
2668 fontCache[start].lru = start + 1;
2669 start++;
2673 static fontObject* XFONT_LookupCachedFont( const LPLOGFONT16 plf, UINT16* checksum )
2675 UINT16 cs = __lfCheckSum( plf );
2676 int i = fontMRU, prev = -1;
2678 *checksum = cs;
2679 while( i >= 0 )
2681 if( fontCache[i].lfchecksum == cs &&
2682 !(fontCache[i].fo_flags & FO_REMOVED) )
2684 /* FIXME: something more intelligent here ? */
2686 if( !memcmp( plf, &fontCache[i].lf,
2687 sizeof(LOGFONT16) - LF_FACESIZE ) &&
2688 !strcmp( plf->lfFaceName, fontCache[i].lf.lfFaceName) )
2690 /* remove temporarily from the lru list */
2691 if( prev >= 0 )
2692 fontCache[prev].lru = fontCache[i].lru;
2693 else
2694 fontMRU = (INT16)fontCache[i].lru;
2695 return (fontCache + i);
2698 prev = i;
2699 i = (INT16)fontCache[i].lru;
2701 return NULL;
2704 static fontObject* XFONT_GetCacheEntry(void)
2706 int i;
2708 if( fontLF == -1 )
2710 int prev_i, prev_j, j;
2712 TRACE("font cache is full\n");
2714 /* lookup the least recently used font */
2716 for( prev_i = prev_j = j = -1, i = fontMRU; i >= 0; i = (INT16)fontCache[i].lru )
2718 if( fontCache[i].count <= 0 &&
2719 !(fontCache[i].fo_flags & FO_SYSTEM) )
2721 prev_j = prev_i;
2722 j = i;
2724 prev_i = i;
2727 if( j >= 0 ) /* unload font */
2729 /* detach from the lru list */
2731 TRACE("\tfreeing entry %i\n", j );
2733 fontCache[j].fr->fo_count--;
2735 if( prev_j >= 0 )
2736 fontCache[prev_j].lru = fontCache[j].lru;
2737 else fontMRU = (INT16)fontCache[j].lru;
2739 /* FIXME: lpXForm, lpPixmap */
2740 if(fontCache[j].lpX11Trans)
2741 HeapFree( GetProcessHeap(), 0, fontCache[j].lpX11Trans );
2743 TSXFreeFont( gdi_display, fontCache[j].fs );
2745 memset( fontCache + j, 0, sizeof(fontObject) );
2746 return (fontCache + j);
2748 else /* expand cache */
2750 fontObject* newCache;
2752 prev_i = fontCacheSize + FONTCACHE;
2754 TRACE("\tgrowing font cache from %i to %i\n", fontCacheSize, prev_i );
2756 if( (newCache = (fontObject*)HeapReAlloc(GetProcessHeap(), 0,
2757 fontCache, prev_i)) )
2759 i = fontCacheSize;
2760 fontCacheSize = prev_i;
2761 fontCache = newCache;
2762 XFONT_GrowFreeList( i, fontCacheSize - 1);
2764 else return NULL;
2768 /* detach from the free list */
2770 i = fontLF;
2771 fontLF = (INT16)fontCache[i].lru;
2772 fontCache[i].count = 0;
2773 return (fontCache + i);
2776 static int XFONT_ReleaseCacheEntry(const fontObject* pfo)
2778 UINT u = (UINT)(pfo - fontCache);
2779 int i;
2780 int ret;
2782 if( u < fontCacheSize )
2784 ret = --fontCache[u].count;
2785 if ( ret == 0 )
2787 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
2789 if( CHECK_PFONT(pfo->prefobjs[i]) )
2790 XFONT_ReleaseCacheEntry(__PFONT(pfo->prefobjs[i]));
2794 return ret;
2797 return -1;
2800 /***********************************************************************
2801 * X11DRV_FONT_Init
2803 * Initialize font resource list and allocate font cache.
2805 int X11DRV_FONT_Init( int *log_pixels_x, int *log_pixels_y )
2807 char** x_pattern;
2808 unsigned x_checksum;
2809 int i,res, x_count, fd, buf_size;
2810 char *buffer;
2811 HKEY hkey;
2813 res = XFONT_GetPointResolution( log_pixels_x, log_pixels_y );
2815 x_pattern = TSXListFonts(gdi_display, "*", MAX_FONTS, &x_count );
2817 TRACE("Font Mapper: initializing %i fonts [logical dpi=%i, default dpi=%i]\n",
2818 x_count, res, DefResolution);
2819 if (x_count == MAX_FONTS)
2820 MESSAGE("There may be more fonts available - try increasing the value of MAX_FONTS\n");
2822 for( i = x_checksum = 0; i < x_count; i++ )
2824 int j;
2825 #if 0
2826 printf("%i\t: %s\n", i, x_pattern[i] );
2827 #endif
2829 j = strlen( x_pattern[i] );
2830 if( j ) x_checksum ^= __genericCheckSum( x_pattern[i], j );
2832 x_checksum |= X_PFONT_MAGIC;
2833 buf_size = 128;
2834 buffer = HeapAlloc( GetProcessHeap(), 0, buf_size );
2836 /* deal with systemwide font metrics cache */
2838 buffer[0] = 0;
2839 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
2841 DWORD type, count = buf_size;
2842 RegQueryValueExA(hkey, INIGlobalMetrics, 0, &type, buffer, &count);
2843 RegCloseKey(hkey);
2846 if( buffer[0] )
2848 fd = open( buffer, O_RDONLY );
2849 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2851 if (fontList == NULL)
2853 /* try per-user */
2854 buffer = XFONT_UserMetricsCache( buffer, &buf_size );
2855 if( buffer[0] )
2857 fd = open( buffer, O_RDONLY );
2858 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2862 if( fontList == NULL ) /* build metrics from scratch */
2864 int n_ff = XFONT_BuildMetrics(x_pattern, DefResolution, x_checksum, x_count);
2865 if( buffer[0] ) /* update cached metrics */
2867 fd = open( buffer, O_CREAT | O_TRUNC | O_RDWR, 0644 ); /* -rw-r--r-- */
2868 if( XFONT_WriteCachedMetrics( fd, x_checksum, x_count, n_ff ) == FALSE )
2870 WARN("Unable to write to fontcache '%s'\n", buffer);
2871 if( fd >= 0) remove( buffer ); /* couldn't write entire file */
2876 TSXFreeFontNames(x_pattern);
2878 /* check if we're dealing with X11 R6 server */
2880 XFontStruct* x_fs;
2881 strcpy(buffer, "-*-*-*-*-normal-*-[12 0 0 12]-*-72-*-*-*-iso8859-1");
2882 if( (x_fs = TSXLoadQueryFont(gdi_display, buffer)) )
2884 XTextCaps |= TC_SF_X_YINDEP;
2885 TSXFreeFont(gdi_display, x_fs);
2888 HeapFree(GetProcessHeap(), 0, buffer);
2890 XFONT_WindowsNames();
2891 XFONT_LoadAliases();
2892 XFONT_LoadDefaults();
2893 XFONT_LoadIgnores();
2895 /* fontList initialization is over, allocate X font cache */
2897 fontCache = (fontObject*) HeapAlloc(GetProcessHeap(), 0, fontCacheSize * sizeof(fontObject));
2898 XFONT_GrowFreeList(0, fontCacheSize - 1);
2900 TRACE("done!\n");
2902 /* update text caps parameter */
2904 RAW_ASCENT = TSXInternAtom(gdi_display, "RAW_ASCENT", TRUE);
2905 RAW_DESCENT = TSXInternAtom(gdi_display, "RAW_DESCENT", TRUE);
2906 return XTextCaps;
2909 /**********************************************************************
2910 * XFONT_SetX11Trans
2912 static BOOL XFONT_SetX11Trans( fontObject *pfo )
2914 char *fontName;
2915 Atom nameAtom;
2916 LFD* lfd;
2918 TSXGetFontProperty( pfo->fs, XA_FONT, &nameAtom );
2919 fontName = TSXGetAtomName( gdi_display, nameAtom );
2920 lfd = LFD_Parse(fontName);
2921 if (!lfd)
2923 TSXFree(fontName);
2924 return FALSE;
2927 if (lfd->pixel_size[0] != '[') {
2928 HeapFree(GetProcessHeap(), 0, lfd);
2929 TSXFree(fontName);
2930 return FALSE;
2933 #define PX pfo->lpX11Trans
2935 sscanf(lfd->pixel_size, "[%f%f%f%f]", &PX->a, &PX->b, &PX->c, &PX->d);
2936 TSXFree(fontName);
2937 HeapFree(GetProcessHeap(), 0, lfd);
2939 TSXGetFontProperty( pfo->fs, RAW_ASCENT, &PX->RAW_ASCENT );
2940 TSXGetFontProperty( pfo->fs, RAW_DESCENT, &PX->RAW_DESCENT );
2942 PX->pixelsize = hypot(PX->a, PX->b);
2943 PX->ascent = PX->pixelsize / 1000.0 * PX->RAW_ASCENT;
2944 PX->descent = PX->pixelsize / 1000.0 * PX->RAW_DESCENT;
2946 TRACE("[%f %f %f %f] RA = %ld RD = %ld\n",
2947 PX->a, PX->b, PX->c, PX->d,
2948 PX->RAW_ASCENT, PX->RAW_DESCENT);
2950 #undef PX
2951 return TRUE;
2954 /***********************************************************************
2955 * X Device Font Objects
2957 static X_PHYSFONT XFONT_RealizeFont( const LPLOGFONT16 plf,
2958 LPCSTR* faceMatched, BOOL bSubFont,
2959 WORD internal_charset,
2960 WORD* pcharsetMatched )
2962 UINT16 checksum;
2963 INT index = 0;
2964 fontObject* pfo;
2966 pfo = XFONT_LookupCachedFont( plf, &checksum );
2967 if( !pfo )
2969 fontMatch fm;
2970 INT i;
2972 fm.pfr = NULL;
2973 fm.pfi = NULL;
2974 fm.height = 0;
2975 fm.flags = 0;
2976 fm.plf = plf;
2977 fm.internal_charset = internal_charset;
2979 if( XTextCaps & TC_SF_X_YINDEP ) fm.flags = FO_MATCH_XYINDEP;
2981 /* allocate new font cache entry */
2983 if( (pfo = XFONT_GetCacheEntry()) )
2985 /* initialize entry and load font */
2986 char lpLFD[MAX_LFD_LENGTH];
2987 UINT uRelaxLevel = 0;
2989 if(abs(plf->lfHeight) > MAX_FONT_SIZE) {
2990 ERR(
2991 "plf->lfHeight = %d, Creating a 100 pixel font and rescaling metrics \n",
2992 plf->lfHeight);
2993 pfo->rescale = fabs(plf->lfHeight / 100.0);
2994 if(plf->lfHeight > 0) plf->lfHeight = 100;
2995 else plf->lfHeight = -100;
2996 } else
2997 pfo->rescale = 1.0;
2999 XFONT_MatchDeviceFont( fontList, &fm );
3000 pfo->fr = fm.pfr;
3001 pfo->fi = fm.pfi;
3002 pfo->fr->fo_count++;
3003 pfo->fo_flags = fm.flags & ~FO_MATCH_MASK;
3005 pfo->lf = *plf;
3006 pfo->lfchecksum = checksum;
3010 LFD_ComposeLFD( pfo, fm.height, lpLFD, uRelaxLevel++ );
3011 if( (pfo->fs = TSXLoadQueryFont( gdi_display, lpLFD )) ) break;
3012 } while( uRelaxLevel );
3015 if(pfo->lf.lfEscapement != 0) {
3016 pfo->lpX11Trans = HeapAlloc(GetProcessHeap(), 0, sizeof(XFONTTRANS));
3017 if(!XFONT_SetX11Trans( pfo )) {
3018 HeapFree(GetProcessHeap(), 0, pfo->lpX11Trans);
3019 pfo->lpX11Trans = NULL;
3022 XFONT_GetLeading( &pfo->fi->df, pfo->fs,
3023 &pfo->foInternalLeading, NULL, pfo->lpX11Trans );
3024 pfo->foAvgCharWidth = (INT16)XFONT_GetAvgCharWidth(&pfo->fi->df, pfo->fs, pfo->lpX11Trans );
3025 pfo->foMaxCharWidth = (INT16)XFONT_GetMaxCharWidth(pfo->fs, pfo->lpX11Trans);
3027 /* FIXME: If we've got a soft font or
3028 * there are FO_SYNTH_... flags for the
3029 * non PROOF_QUALITY request, the engine
3030 * should rasterize characters into mono
3031 * pixmaps and store them in the pfo->lpPixmap
3032 * array (pfo->fs should be updated as well).
3033 * array (pfo->fs should be updated as well).
3034 * X11DRV_ExtTextOut() must be heavily modified
3035 * to support pixmap blitting and FO_SYNTH_...
3036 * styles.
3039 pfo->lpPixmap = NULL;
3041 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
3042 pfo->prefobjs[i] = (X_PHYSFONT)0xffffffff; /* invalid value */
3044 /* special treatment for DBCS that needs multiple fonts */
3045 /* All member of pfo must be set correctly. */
3046 if ( bSubFont == FALSE )
3048 WORD charset_sub;
3049 WORD charsetMatchedSub;
3050 LOGFONT16 lfSub;
3051 LPCSTR faceMatchedSub;
3053 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
3055 charset_sub = X11DRV_cptable[pfo->fi->cptable].
3056 penum_subfont_charset( i );
3057 if ( charset_sub == DEFAULT_CHARSET ) break;
3059 lfSub = *plf;
3060 lfSub.lfWidth = 0;
3061 lfSub.lfHeight=plf->lfHeight;
3062 lfSub.lfCharSet = (BYTE)(charset_sub & 0xff);
3063 lfSub.lfFaceName[0] = '\0'; /* FIXME? */
3064 /* this font has sub font */
3065 if ( i == 0 ) pfo->prefobjs[0] = (X_PHYSFONT)0;
3066 pfo->prefobjs[i] =
3067 XFONT_RealizeFont( &lfSub, &faceMatchedSub,
3068 TRUE, charset_sub,
3069 &charsetMatchedSub );
3070 /* FIXME: check charsetMatchedSub */
3075 if( !pfo ) /* couldn't get a new entry, get one of the cached fonts */
3077 UINT current_score, score = (UINT)(-1);
3079 i = index = fontMRU;
3080 fm.flags |= FO_MATCH_PAF;
3083 pfo = fontCache + i;
3084 fm.pfr = pfo->fr; fm.pfi = pfo->fi;
3086 current_score = XFONT_Match( &fm );
3087 if( current_score < score ) index = i;
3089 i = pfo->lru;
3090 } while( i >= 0 );
3091 pfo = fontCache + index;
3092 goto END;
3096 /* attach at the head of the lru list */
3097 pfo->lru = fontMRU;
3098 index = fontMRU = (pfo - fontCache);
3100 END:
3101 pfo->count++;
3103 TRACE("physfont %i\n", index);
3104 *faceMatched = pfo->fi->df.dfFace;
3105 *pcharsetMatched = pfo->fi->internal_charset;
3107 return (X_PHYSFONT)(X_PFONT_MAGIC | index);
3110 /***********************************************************************
3111 * XFONT_GetFontObject
3113 fontObject* XFONT_GetFontObject( X_PHYSFONT pFont )
3115 if( CHECK_PFONT(pFont) ) return __PFONT(pFont);
3116 return NULL;
3119 /***********************************************************************
3120 * XFONT_GetFontStruct
3122 XFontStruct* XFONT_GetFontStruct( X_PHYSFONT pFont )
3124 if( CHECK_PFONT(pFont) ) return __PFONT(pFont)->fs;
3125 return NULL;
3128 /***********************************************************************
3129 * XFONT_GetFontInfo
3131 LPIFONTINFO16 XFONT_GetFontInfo( X_PHYSFONT pFont )
3133 if( CHECK_PFONT(pFont) ) return &(__PFONT(pFont)->fi->df);
3134 return NULL;
3139 /* X11DRV Interface ****************************************************
3141 * Exposed via the dc->funcs dispatch table. *
3143 ***********************************************************************/
3144 /***********************************************************************
3145 * X11DRV_FONT_SelectObject
3147 HFONT X11DRV_FONT_SelectObject( DC* dc, HFONT hfont )
3149 HFONT hPrevFont = 0;
3150 LOGFONTW logfont;
3151 LOGFONT16 lf;
3152 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
3154 if (!GetObjectW( hfont, sizeof(logfont), &logfont )) return 0;
3156 EnterCriticalSection( &crtsc_fonts_X11 );
3158 if( CHECK_PFONT(physDev->font) )
3159 XFONT_ReleaseCacheEntry( __PFONT(physDev->font) );
3161 FONT_LogFontWTo16(&logfont, &lf);
3163 /* stock fonts ignore the mapping mode */
3164 if (!is_stock_font( hfont ))
3166 /* Make sure we don't change the sign when converting to device coords */
3167 /* FIXME - check that the other drivers do this correctly */
3168 if (lf.lfWidth)
3170 lf.lfWidth = GDI_ROUND((FLOAT)lf.lfWidth * fabs(dc->xformWorld2Vport.eM11));
3171 if (lf.lfWidth == 0)
3172 lf.lfWidth = 1; /* Minimum width */
3174 if (lf.lfHeight)
3176 lf.lfHeight = GDI_ROUND((FLOAT)lf.lfHeight * fabs(dc->xformWorld2Vport.eM22));
3178 if (lf.lfHeight == 0)
3179 lf.lfHeight = MIN_FONT_SIZE;
3183 if (!lf.lfHeight)
3184 lf.lfHeight = -(DEF_POINT_SIZE * GetDeviceCaps(dc->hSelf,LOGPIXELSY) + (72>>1)) / 72;
3187 /* Fixup aliases before passing to RealizeFont */
3188 /* alias = Windows name in the alias table */
3189 LPCSTR alias = XFONT_UnAlias( lf.lfFaceName );
3190 LPCSTR faceMatched;
3191 WORD charsetMatched;
3193 TRACE("hfont=%04x\n", hfont); /* to connect with the trace from RealizeFont */
3194 physDev->font = XFONT_RealizeFont( &lf, &faceMatched,
3195 FALSE, lf.lfCharSet,
3196 &charsetMatched );
3198 /* set face to the requested facename if it matched
3199 * so that GetTextFace can get the correct face name
3201 if (alias && !strcmp(faceMatched, lf.lfFaceName))
3202 MultiByteToWideChar(CP_ACP, 0, alias, -1,
3203 logfont.lfFaceName, LF_FACESIZE);
3204 else
3205 MultiByteToWideChar(CP_ACP, 0, faceMatched, -1,
3206 logfont.lfFaceName, LF_FACESIZE);
3209 * In X, some encodings may have the same lfFaceName.
3210 * for example:
3211 * -misc-fixed-*-iso8859-1
3212 * -misc-fixed-*-jisx0208.1990-0
3213 * so charset should be saved...
3215 logfont.lfCharSet = charsetMatched;
3218 hPrevFont = dc->hFont;
3219 dc->hFont = hfont;
3221 LeaveCriticalSection( &crtsc_fonts_X11 );
3223 return hPrevFont;
3227 /***********************************************************************
3229 * X11DRV_EnumDeviceFonts
3231 BOOL X11DRV_EnumDeviceFonts( HDC hdc, LPLOGFONTW plf,
3232 DEVICEFONTENUMPROC proc, LPARAM lp )
3234 ENUMLOGFONTEXW lf;
3235 NEWTEXTMETRICEXW tm;
3236 fontResource* pfr = fontList;
3237 BOOL b, bRet = 0;
3238 LOGFONT16 lf16;
3241 FONT_LogFontWTo16(plf, &lf16);
3243 if( lf16.lfFaceName[0] )
3245 /* enum all entries in this resource */
3246 pfr = XFONT_FindFIList( pfr, lf16.lfFaceName );
3247 if( pfr )
3249 fontInfo* pfi;
3250 for( pfi = pfr->fi; pfi; pfi = pfi->next )
3252 /* Note: XFONT_GetFontMetric() will have to
3253 release the crit section, font list will
3254 have to be retraversed on return */
3256 if(lf16.lfCharSet == DEFAULT_CHARSET ||
3257 lf16.lfCharSet == pfi->df.dfCharSet) {
3258 if( (b = (*proc)( &lf, &tm,
3259 XFONT_GetFontMetric( pfi, &lf, &tm ), lp )) )
3260 bRet = b;
3261 else break;
3266 else /* enum first entry in each resource */
3267 for( ; pfr ; pfr = pfr->next )
3269 if(pfr->fi)
3271 if( (b = (*proc)( &lf, &tm,
3272 XFONT_GetFontMetric( pfr->fi, &lf, &tm ), lp )) )
3273 bRet = b;
3274 else break;
3277 return bRet;
3281 /***********************************************************************
3282 * X11DRV_GetTextMetrics
3284 BOOL X11DRV_GetTextMetrics(DC *dc, TEXTMETRICW *metrics)
3286 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
3287 TEXTMETRICA tmA;
3289 if( CHECK_PFONT(physDev->font) )
3291 fontObject* pfo = __PFONT(physDev->font);
3292 X11DRV_cptable[pfo->fi->cptable].pGetTextMetricsA( pfo, &tmA );
3293 FONT_TextMetricAToW(&tmA, metrics);
3294 return TRUE;
3296 return FALSE;
3300 /***********************************************************************
3301 * X11DRV_GetCharWidth
3303 BOOL X11DRV_GetCharWidth( DC *dc, UINT firstChar, UINT lastChar,
3304 LPINT buffer )
3306 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
3307 fontObject* pfo = XFONT_GetFontObject( physDev->font );
3309 if( pfo )
3311 int i;
3313 if (pfo->fs->per_char == NULL)
3314 for (i = firstChar; i <= lastChar; i++)
3315 if(pfo->lpX11Trans)
3316 *buffer++ = pfo->fs->min_bounds.attributes *
3317 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3318 else
3319 *buffer++ = pfo->fs->min_bounds.width * pfo->rescale;
3320 else
3322 XCharStruct *cs, *def;
3323 static XCharStruct __null_char = { 0, 0, 0, 0, 0, 0 };
3325 CI_GET_CHAR_INFO(pfo->fs, pfo->fs->default_char, &__null_char,
3326 def);
3328 for (i = firstChar; i <= lastChar; i++)
3330 if (i >= pfo->fs->min_char_or_byte2 &&
3331 i <= pfo->fs->max_char_or_byte2)
3333 cs = &pfo->fs->per_char[(i - pfo->fs->min_char_or_byte2)];
3334 if (CI_NONEXISTCHAR(cs)) cs = def;
3335 } else cs = def;
3336 if(pfo->lpX11Trans)
3337 *buffer++ = max(cs->attributes, 0) *
3338 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3339 else
3340 *buffer++ = max(cs->width, 0 ) * pfo->rescale;
3344 return TRUE;
3346 return FALSE;