Make the testing framework thread safe.
[wine/gsoc_dplay.git] / graphics / x11drv / xfont.c
blobc866343255200b73e929b609f6bdbe81c41488a7
1 /*
2 * X11 physical font objects
4 * Copyright 1997 Alex Korobka
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * TODO: Mapping algorithm tweaks, FO_SYNTH_... flags (ExtTextOut() will
21 * have to be changed for that), dynamic font loading (FreeType).
24 #include "config.h"
26 #include <X11/Xatom.h>
28 #include "ts_xlib.h"
30 #include <ctype.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #include <math.h>
39 #include "windef.h"
40 #include "wingdi.h"
41 #include "winnls.h"
42 #include "winreg.h"
43 #include "font.h"
44 #include "wine/debug.h"
45 #include "user.h" /* for TWEAK_WineLook (FIXME) */
46 #include "x11font.h"
47 #include "wine/server.h"
48 #include "wine/unicode.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(font);
52 #define X_PFONT_MAGIC (0xFADE0000)
53 #define X_FMC_MAGIC (0x0000CAFE)
55 #define MAX_FONTS 1024*16
56 #define MAX_LFD_LENGTH 256
57 #define TILDE '~'
58 #define HYPHEN '-'
60 #define DEF_POINT_SIZE 8 /* CreateFont(0 .. ) gets this */
61 #define DEF_SCALABLE_HEIGHT 100 /* pixels */
62 #define MIN_FONT_SIZE 2 /* Min size in pixels */
63 #define MAX_FONT_SIZE 1000 /* Max size in pixels */
65 #define REMOVE_SUBSETS 1
66 #define UNMARK_SUBSETS 0
69 #define FF_FAMILY (FF_MODERN | FF_SWISS | FF_ROMAN | FF_DECORATIVE | FF_SCRIPT)
71 typedef struct __fontAlias
73 LPSTR faTypeFace;
74 LPSTR faAlias;
75 struct __fontAlias* next;
76 } fontAlias;
78 static fontAlias *aliasTable = NULL;
80 static UINT XTextCaps = (TC_OP_CHARACTER | TC_OP_STROKE | TC_CP_STROKE | TC_CR_ANY |
81 TC_SA_DOUBLE | TC_SA_INTEGER | TC_SA_CONTIN |
82 TC_UA_ABLE | TC_SO_ABLE | TC_RA_ABLE);
83 /* X11R6 adds TC_SF_X_YINDEP, maybe more... */
85 static const char* INIFontMetrics = "cachedmetrics.";
86 static const char* INIFontSection = "Software\\Wine\\Wine\\Config\\fonts";
87 static const char* INIAliasSection = "Alias";
88 static const char* INIIgnoreSection = "Ignore";
89 static const char* INIDefault = "Default";
90 static const char* INIDefaultFixed = "DefaultFixed";
91 static const char* INIResolution = "Resolution";
92 static const char* INIGlobalMetrics = "FontMetrics";
93 static const char* INIDefaultSerif = "DefaultSerif";
94 static const char* INIDefaultSansSerif = "DefaultSansSerif";
97 /* FIXME - are there any more Latin charsets ? */
98 /* FIXME - RUSSIAN, ARABIC, GREEK, HEBREW are NOT Latin */
99 #define IS_LATIN_CHARSET(ch) \
100 ((ch)==ANSI_CHARSET ||\
101 (ch)==EE_CHARSET ||\
102 (ch)==ISO3_CHARSET ||\
103 (ch)==ISO4_CHARSET ||\
104 (ch)==RUSSIAN_CHARSET ||\
105 (ch)==ARABIC_CHARSET ||\
106 (ch)==GREEK_CHARSET ||\
107 (ch)==HEBREW_CHARSET ||\
108 (ch)==TURKISH_CHARSET ||\
109 (ch)==ISO10_CHARSET ||\
110 (ch)==BALTIC_CHARSET ||\
111 (ch)==CELTIC_CHARSET)
113 /* suffix-charset mapping tables - must be less than 254 entries long */
115 typedef struct __sufch
117 LPCSTR psuffix;
118 WORD charset; /* hibyte != 0 means *internal* charset */
119 WORD codepage;
120 WORD cptable;
121 } SuffixCharset;
123 static const SuffixCharset sufch_ansi[] = {
124 { "0", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
125 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
127 static const SuffixCharset sufch_iso646[] = {
128 { "irv", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
129 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
131 static const SuffixCharset sufch_iso8859[] = {
132 { "1", ANSI_CHARSET, 28591, X11DRV_CPTABLE_SBCS },
133 { "2", EE_CHARSET, 28592, X11DRV_CPTABLE_SBCS },
134 { "3", ISO3_CHARSET, 28593, X11DRV_CPTABLE_SBCS },
135 { "4", ISO4_CHARSET, 28594, X11DRV_CPTABLE_SBCS },
136 { "5", RUSSIAN_CHARSET, 28595, X11DRV_CPTABLE_SBCS },
137 { "6", ARABIC_CHARSET, 28596, X11DRV_CPTABLE_SBCS },
138 { "7", GREEK_CHARSET, 28597, X11DRV_CPTABLE_SBCS },
139 { "8", HEBREW_CHARSET, 28598, X11DRV_CPTABLE_SBCS },
140 { "9", TURKISH_CHARSET, 28599, X11DRV_CPTABLE_SBCS },
141 { "10", ISO10_CHARSET, 28600, X11DRV_CPTABLE_SBCS },
142 { "11", THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }, /* FIXME */
143 { "12", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
144 { "13", BALTIC_CHARSET, 28603, X11DRV_CPTABLE_SBCS },
145 { "14", CELTIC_CHARSET, 28604, X11DRV_CPTABLE_SBCS },
146 { "15", ANSI_CHARSET, 28605, X11DRV_CPTABLE_SBCS },
147 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
149 static const SuffixCharset sufch_microsoft[] = {
150 { "cp1250", EE_CHARSET, 1250, X11DRV_CPTABLE_SBCS },
151 { "cp1251", RUSSIAN_CHARSET, 1251, X11DRV_CPTABLE_SBCS },
152 { "cp1252", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
153 { "cp1253", GREEK_CHARSET, 1253, X11DRV_CPTABLE_SBCS },
154 { "cp1254", TURKISH_CHARSET, 1254, X11DRV_CPTABLE_SBCS },
155 { "cp1255", HEBREW_CHARSET, 1255, X11DRV_CPTABLE_SBCS },
156 { "cp1256", ARABIC_CHARSET, 1256, X11DRV_CPTABLE_SBCS },
157 { "cp1257", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS },
158 { "fontspecific", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SYMBOL },
159 { "symbol", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SYMBOL },
160 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
162 static const SuffixCharset sufch_tcvn[] = {
163 { "0", TCVN_CHARSET, 1252, X11DRV_CPTABLE_SBCS }, /* FIXME */
164 { NULL, TCVN_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
166 static const SuffixCharset sufch_tis620[] = {
167 { "0", THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }, /* FIXME */
168 { NULL, THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }};
170 static const SuffixCharset sufch_viscii[] = {
171 { "1", VISCII_CHARSET, 1252, X11DRV_CPTABLE_SBCS }, /* FIXME */
172 { NULL, VISCII_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
174 static const SuffixCharset sufch_windows[] = {
175 { "1250", EE_CHARSET, 1250, X11DRV_CPTABLE_SBCS },
176 { "1251", RUSSIAN_CHARSET, 1251, X11DRV_CPTABLE_SBCS },
177 { "1252", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
178 { "1253", GREEK_CHARSET, 1253, X11DRV_CPTABLE_SBCS },
179 { "1254", TURKISH_CHARSET, 1254, X11DRV_CPTABLE_SBCS },
180 { "1255", HEBREW_CHARSET, 1255, X11DRV_CPTABLE_SBCS },
181 { "1256", ARABIC_CHARSET, 1256, X11DRV_CPTABLE_SBCS },
182 { "1257", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS },
183 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
185 static const SuffixCharset sufch_koi8[] = {
186 { "r", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
187 { "ru", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
188 { "u", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
189 { NULL, RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS }};
191 static const SuffixCharset sufch_jisx0201[] = {
192 { "0", X11FONT_JISX0201_CHARSET, 932, X11DRV_CPTABLE_SBCS },
193 { NULL, X11FONT_JISX0201_CHARSET, 932, X11DRV_CPTABLE_SBCS }};
195 static const SuffixCharset sufch_jisx0208[] = {
196 { "0", SHIFTJIS_CHARSET, 932, X11DRV_CPTABLE_CP932 },
197 { NULL, SHIFTJIS_CHARSET, 932, X11DRV_CPTABLE_CP932 }};
199 static const SuffixCharset sufch_jisx0212[] = {
200 { "0", X11FONT_JISX0212_CHARSET, 932, X11DRV_CPTABLE_CP932 },
201 { NULL, X11FONT_JISX0212_CHARSET, 932, X11DRV_CPTABLE_CP932 }};
203 static const SuffixCharset sufch_ksc5601[] = {
204 { "0", HANGEUL_CHARSET, 949, X11DRV_CPTABLE_CP949 },
205 { NULL, HANGEUL_CHARSET, 949, X11DRV_CPTABLE_CP949 }};
207 static const SuffixCharset sufch_gb2312[] = {
208 { "0", GB2312_CHARSET, 936, X11DRV_CPTABLE_CP936 },
209 { NULL, GB2312_CHARSET, 936, X11DRV_CPTABLE_CP936 }};
211 static const SuffixCharset sufch_big5[] = {
212 { "0", CHINESEBIG5_CHARSET, 950, X11DRV_CPTABLE_CP950 },
213 { NULL, CHINESEBIG5_CHARSET, 950, X11DRV_CPTABLE_CP950 }};
215 static const SuffixCharset sufch_unicode[] = {
216 { "0", DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE },
217 { NULL, DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE }};
219 static const SuffixCharset sufch_iso10646[] = {
220 { "1", DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE },
221 { NULL, DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE }};
223 static const SuffixCharset sufch_dec[] = {
224 { "dectech", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
225 { NULL, 0, 0, X11DRV_CPTABLE_SBCS }};
227 /* Each of these must be matched explicitly */
228 static const SuffixCharset sufch_any[] = {
229 { "fontspecific", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
230 { NULL, 0, 0, X11DRV_CPTABLE_SBCS }};
233 typedef struct __fet
235 LPSTR prefix;
236 const SuffixCharset* sufch;
237 struct __fet* next;
238 } fontEncodingTemplate;
240 /* Note: we can attach additional encoding mappings to the end
241 * of this table at runtime.
243 static fontEncodingTemplate __fETTable[] = {
244 { "ansi", sufch_ansi, &__fETTable[1] },
245 { "ascii", sufch_ansi, &__fETTable[2] },
246 { "iso646.1991", sufch_iso646, &__fETTable[3] },
247 { "iso8859", sufch_iso8859, &__fETTable[4] },
248 { "microsoft", sufch_microsoft, &__fETTable[5] },
249 { "tcvn", sufch_tcvn, &__fETTable[6] },
250 { "tis620.2533", sufch_tis620, &__fETTable[7] },
251 { "viscii1.1", sufch_viscii, &__fETTable[8] },
252 { "windows", sufch_windows, &__fETTable[9] },
253 { "koi8", sufch_koi8, &__fETTable[10]},
254 { "jisx0201.1976",sufch_jisx0201, &__fETTable[11]},
255 { "jisc6226.1978",sufch_jisx0208, &__fETTable[12]},
256 { "jisx0208.1983",sufch_jisx0208, &__fETTable[13]},
257 { "jisx0208.1990",sufch_jisx0208, &__fETTable[14]},
258 { "jisx0212.1990",sufch_jisx0212, &__fETTable[15]},
259 { "ksc5601.1987", sufch_ksc5601, &__fETTable[16]},
260 { "gb2312.1980", sufch_gb2312, &__fETTable[17]},
261 { "big5", sufch_big5, &__fETTable[18]},
262 { "unicode", sufch_unicode, &__fETTable[19]},
263 { "iso10646", sufch_iso10646, &__fETTable[20]},
264 { "cp", sufch_windows, &__fETTable[21]},
265 { "dec", sufch_dec, &__fETTable[22]},
266 /* NULL prefix matches anything so put it last */
267 { NULL, sufch_any, NULL },
269 static fontEncodingTemplate* fETTable = __fETTable;
271 /* a charset database for known facenames */
272 struct CharsetBindingInfo
274 const char* pszFaceName;
275 BYTE charset;
277 static const struct CharsetBindingInfo charsetbindings[] =
279 /* special facenames */
280 { "System", DEFAULT_CHARSET },
281 { "FixedSys", DEFAULT_CHARSET },
283 /* known facenames */
284 { "MS Serif", ANSI_CHARSET },
285 { "MS Sans Serif", ANSI_CHARSET },
286 { "Courier", ANSI_CHARSET },
287 { "Symbol", SYMBOL_CHARSET },
289 { "Arial", ANSI_CHARSET },
290 { "Arial Greek", GREEK_CHARSET },
291 { "Arial Tur", TURKISH_CHARSET },
292 { "Arial Baltic", BALTIC_CHARSET },
293 { "Arial CE", EASTEUROPE_CHARSET },
294 { "Arial Cyr", RUSSIAN_CHARSET },
295 { "Courier New", ANSI_CHARSET },
296 { "Courier New Greek", GREEK_CHARSET },
297 { "Courier New Tur", TURKISH_CHARSET },
298 { "Courier New Baltic", BALTIC_CHARSET },
299 { "Courier New CE", EASTEUROPE_CHARSET },
300 { "Courier New Cyr", RUSSIAN_CHARSET },
301 { "Times New Roman", ANSI_CHARSET },
302 { "Times New Roman Greek", GREEK_CHARSET },
303 { "Times New Roman Tur", TURKISH_CHARSET },
304 { "Times New Roman Baltic", BALTIC_CHARSET },
305 { "Times New Roman CE", EASTEUROPE_CHARSET },
306 { "Times New Roman Cyr", RUSSIAN_CHARSET },
308 { "\x82\x6c\x82\x72 \x83\x53\x83\x56\x83\x62\x83\x4e",
309 SHIFTJIS_CHARSET }, /* MS gothic */
310 { "\x82\x6c\x82\x72 \x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e",
311 SHIFTJIS_CHARSET }, /* MS P gothic */
312 { "\x82\x6c\x82\x72 \x96\xbe\x92\xa9",
313 SHIFTJIS_CHARSET }, /* MS mincho */
314 { "\x82\x6c\x82\x72 \x82\x6f\x96\xbe\x92\xa9",
315 SHIFTJIS_CHARSET }, /* MS P mincho */
316 { "GulimChe", HANGEUL_CHARSET },
317 { "MS Song", GB2312_CHARSET },
318 { "MS Hei", GB2312_CHARSET },
319 { "\xb7\x73\xb2\xd3\xa9\xfa\xc5\xe9", CHINESEBIG5_CHARSET },/*MS Mingliu*/
320 { "\xb2\xd3\xa9\xfa\xc5\xe9", CHINESEBIG5_CHARSET },
322 { NULL, 0 }
326 static int DefResolution = 0;
328 static CRITICAL_SECTION crtsc_fonts_X11 = CRITICAL_SECTION_INIT("crtsc_fonts_X11");
330 static fontResource* fontList = NULL;
331 static fontObject* fontCache = NULL; /* array */
332 static int fontCacheSize = FONTCACHE;
333 static int fontLF = -1, fontMRU = -1; /* last free, most recently used */
335 #define __PFONT(pFont) ( fontCache + ((UINT)(pFont) & 0x0000FFFF) )
336 #define CHECK_PFONT(pFont) ( (((UINT)(pFont) & 0xFFFF0000) == X_PFONT_MAGIC) &&\
337 (((UINT)(pFont) & 0x0000FFFF) < fontCacheSize) )
339 static Atom RAW_ASCENT;
340 static Atom RAW_DESCENT;
342 /***********************************************************************
343 * Helper macros from X distribution
346 #define CI_NONEXISTCHAR(cs) (((cs)->width == 0) && \
347 (((cs)->rbearing|(cs)->lbearing| \
348 (cs)->ascent|(cs)->descent) == 0))
350 #define CI_GET_CHAR_INFO(fs,col,def,cs) \
352 cs = def; \
353 if (col >= fs->min_char_or_byte2 && col <= fs->max_char_or_byte2) { \
354 if (fs->per_char == NULL) { \
355 cs = &fs->min_bounds; \
356 } else { \
357 cs = &fs->per_char[(col - fs->min_char_or_byte2)]; \
358 if (CI_NONEXISTCHAR(cs)) cs = def; \
363 #define CI_GET_DEFAULT_INFO(fs,cs) \
364 CI_GET_CHAR_INFO(fs, fs->default_char, NULL, cs)
367 /***********************************************************************
368 * is_stock_font
370 inline static BOOL is_stock_font( HFONT font )
372 int i;
373 for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
375 if (i != DEFAULT_PALETTE && font == GetStockObject(i)) return TRUE;
377 return FALSE;
381 /***********************************************************************
382 * Checksums
384 static UINT16 __lfCheckSum( LPLOGFONT16 plf )
386 CHAR font[LF_FACESIZE];
387 UINT16 checksum = 0;
388 UINT16 *ptr;
389 int i;
391 ptr = (UINT16 *)plf;
392 for (i = 0; i < 9; i++) checksum ^= *ptr++;
393 for (i = 0; i < LF_FACESIZE; i++)
395 font[i] = tolower(plf->lfFaceName[i]);
396 if (!font[i] || font[i] == ' ') break;
398 for (ptr = (UINT16 *)font, i >>= 1; i > 0; i-- ) checksum ^= *ptr++;
399 return checksum;
402 static UINT16 __genericCheckSum( const void *ptr, int size )
404 unsigned int checksum = 0;
405 const char *p = (const char *)ptr;
406 while (size-- > 0)
407 checksum ^= (checksum << 3) + (checksum >> 29) + *p++;
409 return checksum & 0xffff;
412 /*************************************************************************
413 * LFD parse/compose routines
415 * NB. LFD_Parse will use lpFont for its own ends, so if you want to keep it
416 * make a copy first
418 * These functions also do TILDE to HYPHEN conversion
420 static LFD* LFD_Parse(LPSTR lpFont)
422 LFD* lfd;
423 char *lpch = lpFont, *lfd_fld[LFD_FIELDS], *field_start;
424 int i;
425 if (*lpch != HYPHEN)
427 WARN("font '%s' doesn't begin with '%c'\n", lpFont, HYPHEN);
428 return NULL;
431 field_start = ++lpch;
432 for( i = 0; i < LFD_FIELDS; )
434 if (*lpch == HYPHEN)
436 *lpch = '\0';
437 lfd_fld[i] = field_start;
438 i++;
439 field_start = ++lpch;
441 else if (!*lpch)
443 lfd_fld[i] = field_start;
444 i++;
445 break;
447 else if (*lpch == TILDE)
449 *lpch = HYPHEN;
450 ++lpch;
452 else
453 ++lpch;
455 /* Fill in the empty fields with NULLS */
456 for (; i< LFD_FIELDS; i++)
457 lfd_fld[i] = NULL;
458 if (*lpch)
459 WARN("Extra ignored in font '%s'\n", lpFont);
461 lfd = HeapAlloc( GetProcessHeap(), 0, sizeof(LFD) );
462 if (lfd)
464 lfd->foundry = lfd_fld[0];
465 lfd->family = lfd_fld[1];
466 lfd->weight = lfd_fld[2];
467 lfd->slant = lfd_fld[3];
468 lfd->set_width = lfd_fld[4];
469 lfd->add_style = lfd_fld[5];
470 lfd->pixel_size = lfd_fld[6];
471 lfd->point_size = lfd_fld[7];
472 lfd->resolution_x = lfd_fld[8];
473 lfd->resolution_y = lfd_fld[9];
474 lfd->spacing = lfd_fld[10];
475 lfd->average_width = lfd_fld[11];
476 lfd->charset_registry = lfd_fld[12];
477 lfd->charset_encoding = lfd_fld[13];
479 return lfd;
483 static void LFD_UnParse(LPSTR dp, UINT buf_size, LFD* lfd)
485 const char* lfd_fld[LFD_FIELDS];
486 int i;
488 if (!buf_size)
489 return; /* Dont be silly */
491 lfd_fld[0] = lfd->foundry;
492 lfd_fld[1] = lfd->family;
493 lfd_fld[2] = lfd->weight ;
494 lfd_fld[3] = lfd->slant ;
495 lfd_fld[4] = lfd->set_width ;
496 lfd_fld[5] = lfd->add_style;
497 lfd_fld[6] = lfd->pixel_size;
498 lfd_fld[7] = lfd->point_size;
499 lfd_fld[8] = lfd->resolution_x;
500 lfd_fld[9] = lfd->resolution_y ;
501 lfd_fld[10] = lfd->spacing ;
502 lfd_fld[11] = lfd->average_width ;
503 lfd_fld[12] = lfd->charset_registry ;
504 lfd_fld[13] = lfd->charset_encoding ;
506 buf_size--; /* Room for the terminator */
508 for (i = 0; i < LFD_FIELDS; i++)
510 const char* sp = lfd_fld[i];
511 if (!sp || !buf_size)
512 break;
514 *dp++ = HYPHEN;
515 buf_size--;
516 while (buf_size > 0 && *sp)
518 *dp = (*sp == HYPHEN) ? TILDE : *sp;
519 buf_size--;
520 dp++; sp++;
523 *dp = '\0';
527 static void LFD_GetWeight( fontInfo* fi, LPCSTR lpStr)
529 int j = strlen(lpStr);
530 if( j == 1 && *lpStr == '0')
531 fi->fi_flags |= FI_POLYWEIGHT;
532 else if( j == 4 )
534 if( !strcasecmp( "bold", lpStr) )
535 fi->df.dfWeight = FW_BOLD;
536 else if( !strcasecmp( "demi", lpStr) )
538 fi->fi_flags |= FI_FW_DEMI;
539 fi->df.dfWeight = FW_DEMIBOLD;
541 else if( !strcasecmp( "book", lpStr) )
543 fi->fi_flags |= FI_FW_BOOK;
544 fi->df.dfWeight = FW_REGULAR;
547 else if( j == 5 )
549 if( !strcasecmp( "light", lpStr) )
550 fi->df.dfWeight = FW_LIGHT;
551 else if( !strcasecmp( "black", lpStr) )
552 fi->df.dfWeight = FW_BLACK;
554 else if( j == 6 && !strcasecmp( "medium", lpStr) )
555 fi->df.dfWeight = FW_REGULAR;
556 else if( j == 8 && !strcasecmp( "demibold", lpStr) )
557 fi->df.dfWeight = FW_DEMIBOLD;
558 else
559 fi->df.dfWeight = FW_DONTCARE; /* FIXME: try to get something
560 * from the weight property */
563 static BOOL LFD_GetSlant( fontInfo* fi, LPCSTR lpStr)
565 int l = strlen(lpStr);
566 if( l == 1 )
568 switch( tolower( *lpStr ) )
570 case '0': fi->fi_flags |= FI_POLYSLANT; /* haven't seen this one yet */
571 default:
572 case 'r': fi->df.dfItalic = 0;
573 break;
574 case 'o':
575 fi->fi_flags |= FI_OBLIQUE;
576 case 'i': fi->df.dfItalic = 1;
577 break;
579 return FALSE;
581 return TRUE;
584 static void LFD_GetStyle( fontInfo* fi, LPCSTR lpstr, int dec_style_check)
586 int j = strlen(lpstr);
587 if( j > 3 ) /* find out is there "sans" or "script" */
589 j = 0;
591 if( strstr(lpstr, "sans") )
593 fi->df.dfPitchAndFamily |= FF_SWISS;
594 j = 1;
596 if( strstr(lpstr, "script") )
598 fi->df.dfPitchAndFamily |= FF_SCRIPT;
599 j = 1;
601 if( !j && dec_style_check )
602 fi->df.dfPitchAndFamily |= FF_DECORATIVE;
606 /*************************************************************************
607 * LFD_InitFontInfo
609 * INIT ONLY
611 * Fill in some fields in the fontInfo struct.
613 static int LFD_InitFontInfo( fontInfo* fi, const LFD* lfd, LPCSTR fullname )
615 int i, j, dec_style_check, scalability;
616 fontEncodingTemplate* boba;
617 const char* ridiculous = "font '%s' has ridiculous %s\n";
618 const char* lpstr;
620 if (!lfd->charset_registry)
622 WARN("font '%s' does not have enough fields\n", fullname);
623 return FALSE;
626 memset(fi, 0, sizeof(fontInfo) );
628 /* weight name - */
629 LFD_GetWeight( fi, lfd->weight);
631 /* slant - */
632 dec_style_check = LFD_GetSlant( fi, lfd->slant);
634 /* width name - */
635 lpstr = lfd->set_width;
636 if( strcasecmp( "normal", lpstr) ) /* XXX 'narrow', 'condensed', etc... */
637 dec_style_check = TRUE;
638 else
639 fi->fi_flags |= FI_NORMAL;
641 /* style - */
642 LFD_GetStyle(fi, lfd->add_style, dec_style_check);
644 /* pixel & decipoint height, and res_x & y */
646 scalability = 0;
648 j = strlen(lfd->pixel_size);
649 if( j == 0 || j > 3 )
651 WARN(ridiculous, fullname, "pixel_size");
652 return FALSE;
654 if( !(fi->lfd_height = atoi(lfd->pixel_size)) )
655 scalability++;
657 j = strlen(lfd->point_size);
658 if( j == 0 || j > 3 )
660 WARN(ridiculous, fullname, "point_size");
661 return FALSE;
663 if( !(atoi(lfd->point_size)) )
664 scalability++;
666 j = strlen(lfd->resolution_x);
667 if( j == 0 || j > 3 )
669 WARN(ridiculous, fullname, "resolution_x");
670 return FALSE;
672 if( !(fi->lfd_resolution = atoi(lfd->resolution_x)) )
673 scalability++;
675 j = strlen(lfd->resolution_y);
676 if( j == 0 || j > 3 )
678 WARN(ridiculous, fullname, "resolution_y");
679 return FALSE;
681 if( !(atoi(lfd->resolution_y)) )
682 scalability++;
684 /* Check scalability */
685 switch (scalability)
687 case 0: /* Bitmap */
688 break;
689 case 4: /* Scalable */
690 fi->fi_flags |= FI_SCALABLE;
691 break;
692 case 2:
693 /* #$%^!!! X11R6 mutant garbage (scalable bitmap) */
694 TRACE("Skipping scalable bitmap '%s'\n", fullname);
695 return FALSE;
696 default:
697 WARN("Font '%s' has weird scalability\n", fullname);
698 return FALSE;
701 /* spacing - */
702 lpstr = lfd->spacing;
703 switch( *lpstr )
705 case '\0':
706 WARN("font '%s' has no spacing\n", fullname);
707 return FALSE;
709 case 'p': fi->fi_flags |= FI_VARIABLEPITCH;
710 break;
711 case 'c': fi->df.dfPitchAndFamily |= FF_MODERN;
712 fi->fi_flags |= FI_FIXEDEX;
713 /* fall through */
714 case 'm': fi->fi_flags |= FI_FIXEDPITCH;
715 break;
716 default:
717 /* Of course this line does nothing */
718 fi->df.dfPitchAndFamily |= DEFAULT_PITCH | FF_DONTCARE;
721 /* average width - */
722 lpstr = lfd->average_width;
723 j = strlen(lpstr);
724 if( j == 0 || j > 3 )
726 WARN(ridiculous, fullname, "average_width");
727 return FALSE;
730 if( !(atoi(lpstr)) && !scalability )
732 WARN("font '%s' has average_width 0 but is not scalable!!\n", fullname);
733 return FALSE;
736 /* charset registry, charset encoding - */
737 lpstr = lfd->charset_registry;
738 if( strstr(lpstr, "ksc") ||
739 strstr(lpstr, "gb2312") ||
740 strstr(lpstr, "big5") )
742 FIXME("DBCS fonts like '%s' are not working correctly now.\n", fullname);
745 fi->df.dfCharSet = ANSI_CHARSET;
747 for( i = 0, boba = fETTable; boba; boba = boba->next, i++ )
749 if (!boba->prefix || !strcasecmp(lpstr, boba->prefix))
751 if (lfd->charset_encoding)
753 for( j = 0; boba->sufch[j].psuffix; j++ )
755 if( !strcasecmp(lfd->charset_encoding, boba->sufch[j].psuffix ))
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;
761 goto done;
765 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
766 fi->internal_charset = boba->sufch[j].charset;
767 fi->codepage = boba->sufch[j].codepage;
768 fi->cptable = boba->sufch[j].cptable;
769 if (boba->prefix)
771 FIXME("font '%s' has unknown character encoding '%s' in known registry '%s'\n",
772 fullname, lfd->charset_encoding, boba->prefix);
773 j = 254;
775 else
777 FIXME("font '%s' has unknown registry '%s' and character encoding '%s' \n",
778 fullname, lfd->charset_registry, lfd->charset_encoding);
779 j = 255;
782 WARN("Defaulting to: df.dfCharSet = %d, internal_charset = %d, codepage = %d, cptable = %d\n",
783 fi->df.dfCharSet,fi->internal_charset, fi->codepage, fi->cptable);
784 goto done;
786 else if (boba->prefix)
788 WARN("font '%s' has known registry '%s' and no character encoding\n",
789 fullname, lpstr);
790 for( j = 0; boba->sufch[j].psuffix; j++ )
792 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
793 fi->internal_charset = boba->sufch[j].charset;
794 fi->codepage = boba->sufch[j].codepage;
795 fi->cptable = boba->sufch[j].cptable;
796 j = 255;
797 goto done;
801 WARN("font '%s' has unknown character set '%s'\n", fullname, lpstr);
802 return FALSE;
804 done:
805 /* i - index into fETTable
806 * j - index into suffix array for fETTable[i]
807 * except:
808 * 254 - found encoding prefix, unknown suffix
809 * 255 - no encoding match at all.
811 fi->fi_encoding = 256 * (UINT16)i + (UINT16)j;
813 return TRUE;
817 /*************************************************************************
818 * LFD_AngleMatrix
820 * make a matrix suitable for LFD based on theta radians
822 static void LFD_AngleMatrix( char* buffer, int h, double theta)
824 double matrix[4];
825 matrix[0] = h*cos(theta);
826 matrix[1] = h*sin(theta);
827 matrix[2] = -h*sin(theta);
828 matrix[3] = h*cos(theta);
829 sprintf(buffer, "[%+f%+f%+f%+f]", matrix[0], matrix[1], matrix[2], matrix[3]);
832 /*************************************************************************
833 * LFD_ComposeLFD
835 * Note: uRelax is a treatment not a cure. Font mapping algorithm
836 * should be bulletproof enough to allow us to avoid hacks like
837 * this despite LFD being so braindead.
839 static BOOL LFD_ComposeLFD( const fontObject* fo,
840 INT height, LPSTR lpLFD, UINT uRelax )
842 int i, h;
843 char *any = "*";
844 char h_string[64], resx_string[64], resy_string[64];
845 LFD aLFD;
847 /* Get the worst case over with first */
849 /* RealizeFont() will call us repeatedly with increasing uRelax
850 * until XLoadFont() succeeds.
851 * to avoid an infinite loop; these will always match
853 if (uRelax >= 5)
855 if (uRelax == 5)
856 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-1" );
857 else
858 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" );
859 return TRUE;
862 /* read foundry + family from fo */
863 aLFD.foundry = fo->fr->resource->foundry;
864 aLFD.family = fo->fr->resource->family;
866 /* add weight */
867 switch( fo->fi->df.dfWeight )
869 case FW_BOLD:
870 aLFD.weight = "bold"; break;
871 case FW_REGULAR:
872 if( fo->fi->fi_flags & FI_FW_BOOK )
873 aLFD.weight = "book";
874 else
875 aLFD.weight = "medium";
876 break;
877 case FW_DEMIBOLD:
878 aLFD.weight = "demi";
879 if( !( fo->fi->fi_flags & FI_FW_DEMI) )
880 aLFD.weight = "bold";
881 break;
882 case FW_BLACK:
883 aLFD.weight = "black"; break;
884 case FW_LIGHT:
885 aLFD.weight = "light"; break;
886 default:
887 aLFD.weight = any;
890 /* add slant */
891 if( fo->fi->df.dfItalic )
892 if( fo->fi->fi_flags & FI_OBLIQUE )
893 aLFD.slant = "o";
894 else
895 aLFD.slant = "i";
896 else
897 aLFD.slant = (uRelax < 1) ? "r" : any;
899 /* add width */
900 if( fo->fi->fi_flags & FI_NORMAL )
901 aLFD.set_width = "normal";
902 else
903 aLFD.set_width = any;
905 /* ignore style */
906 aLFD.add_style = any;
908 /* add pixelheight
910 * FIXME: fill in lpXForm and lpPixmap for rotated fonts
912 if( fo->fo_flags & FO_SYNTH_HEIGHT )
913 h = fo->fi->lfd_height;
914 else
916 h = (fo->fi->lfd_height * height + (fo->fi->df.dfPixHeight>>1));
917 h /= fo->fi->df.dfPixHeight;
919 if (h < MIN_FONT_SIZE) /* Resist rounding down to 0 */
920 h = MIN_FONT_SIZE;
921 else if (h > MAX_FONT_SIZE) /* Sanity check as huge fonts can lock up the font server */
923 WARN("Huge font size %d pixels has been reduced to %d\n", h, MAX_FONT_SIZE);
924 h = MAX_FONT_SIZE;
927 if (uRelax <= 2)
928 /* handle rotated fonts */
929 if (fo->lf.lfEscapement) {
930 /* escapement is in tenths of degrees, theta is in radians */
931 double theta = M_PI*fo->lf.lfEscapement/1800.;
932 LFD_AngleMatrix(h_string, h, theta);
934 else
936 sprintf(h_string, "%d", h);
938 else
939 sprintf(h_string, "%d", fo->fi->lfd_height);
941 aLFD.pixel_size = h_string;
942 aLFD.point_size = any;
944 /* resolution_x,y, average width */
945 /* FOX ME - Why do some font servers ignore average width ?
946 * so that you have to mess around with res_y
948 aLFD.average_width = any;
949 if (uRelax <= 3)
951 sprintf(resx_string, "%d", fo->fi->lfd_resolution);
952 aLFD.resolution_x = resx_string;
954 strcpy(resy_string, resx_string);
955 if( uRelax == 0 && XTextCaps & TC_SF_X_YINDEP )
957 if( fo->lf.lfWidth && !(fo->fo_flags & FO_SYNTH_WIDTH))
959 int resy = 0.5 + fo->fi->lfd_resolution *
960 (fo->fi->df.dfAvgWidth * height) /
961 (fo->lf.lfWidth * fo->fi->df.dfPixHeight) ;
962 sprintf(resy_string, "%d", resy);
964 else
966 /* FIXME - synth width */
969 aLFD.resolution_y = resy_string;
971 else
973 aLFD.resolution_x = aLFD.resolution_y = any;
976 /* spacing */
978 char* w;
980 if( fo->fi->fi_flags & FI_FIXEDPITCH )
981 w = ( fo->fi->fi_flags & FI_FIXEDEX ) ? "c" : "m";
982 else
983 w = ( fo->fi->fi_flags & FI_VARIABLEPITCH ) ? "p" : any;
985 aLFD.spacing = (uRelax <= 1) ? w : any;
988 /* encoding */
990 if (uRelax <= 4)
992 fontEncodingTemplate* boba = fETTable;
994 for(i = fo->fi->fi_encoding >> 8; i; i--) boba = boba->next;
995 aLFD.charset_registry = boba->prefix ? boba->prefix : any;
997 i = fo->fi->fi_encoding & 255;
998 switch( i )
1000 default:
1001 aLFD.charset_encoding = boba->sufch[i].psuffix;
1002 break;
1004 case 254:
1005 aLFD.charset_encoding = any;
1006 break;
1008 case 255: /* no suffix - it ends eg "-ascii" */
1009 aLFD.charset_encoding = NULL;
1010 break;
1013 else
1015 aLFD.charset_registry = any;
1016 aLFD.charset_encoding = any;
1019 LFD_UnParse(lpLFD, MAX_LFD_LENGTH, &aLFD);
1021 TRACE("\tLFD(uRelax=%d): %s\n", uRelax, lpLFD );
1022 return TRUE;
1026 /***********************************************************************
1027 * X Font Resources
1029 * font info - http://www.microsoft.com/kb/articles/q65/1/23.htm
1030 * Windows font metrics - http://www.microsoft.com/kb/articles/q32/6/67.htm
1032 static void XFONT_GetLeading( const LPIFONTINFO16 pFI, const XFontStruct* x_fs,
1033 INT16* pIL, INT16* pEL, const XFONTTRANS *XFT )
1035 unsigned long height;
1036 unsigned min = (unsigned char)pFI->dfFirstChar;
1037 BOOL bIsLatin = IS_LATIN_CHARSET(pFI->dfCharSet);
1039 if( pEL ) *pEL = 0;
1041 if(XFT) {
1042 Atom RAW_CAP_HEIGHT = TSXInternAtom(gdi_display, "RAW_CAP_HEIGHT", TRUE);
1043 if(TSXGetFontProperty((XFontStruct*)x_fs, RAW_CAP_HEIGHT, &height))
1044 *pIL = XFT->ascent -
1045 (INT)(XFT->pixelsize / 1000.0 * height);
1046 else
1047 *pIL = 0;
1048 return;
1051 if( TSXGetFontProperty((XFontStruct*)x_fs, XA_CAP_HEIGHT, &height) == FALSE )
1053 if( x_fs->per_char )
1054 if( bIsLatin )
1055 height = x_fs->per_char['X' - min].ascent;
1056 else
1057 if (x_fs->ascent >= x_fs->max_bounds.ascent)
1058 height = x_fs->max_bounds.ascent;
1059 else
1061 height = x_fs->ascent;
1062 if( pEL )
1063 *pEL = x_fs->max_bounds.ascent - height;
1065 else
1066 height = x_fs->min_bounds.ascent;
1069 *pIL = x_fs->ascent - height;
1072 /***********************************************************************
1073 * XFONT_CharWidth
1075 static int XFONT_CharWidth(const XFontStruct* x_fs,
1076 const XFONTTRANS *XFT, int ch)
1078 if(!XFT)
1079 return x_fs->per_char[ch].width;
1080 else
1081 return x_fs->per_char[ch].attributes * XFT->pixelsize / 1000.0;
1084 /***********************************************************************
1085 * XFONT_GetAvgCharWidth
1087 static INT XFONT_GetAvgCharWidth( LPIFONTINFO16 pFI, const XFontStruct* x_fs,
1088 const XFONTTRANS *XFT)
1090 unsigned min = (unsigned char)pFI->dfFirstChar;
1091 unsigned max = (unsigned char)pFI->dfLastChar;
1093 INT avg;
1095 if( x_fs->per_char )
1097 int width = 0, chars = 0, j;
1098 if( IS_LATIN_CHARSET(pFI->dfCharSet) ||
1099 pFI->dfCharSet == DEFAULT_CHARSET )
1101 /* FIXME - should use a weighted average */
1102 for( j = 0; j < 26; j++ )
1103 width += XFONT_CharWidth(x_fs, XFT, 'a' + j - min) +
1104 XFONT_CharWidth(x_fs, XFT, 'A' + j - min);
1105 chars = 52;
1107 else /* unweighted average of everything */
1109 for( j = 0, max -= min; j <= max; j++ )
1110 if( !CI_NONEXISTCHAR(x_fs->per_char + j) )
1112 width += XFONT_CharWidth(x_fs, XFT, j);
1113 chars++;
1116 if (chars) avg = (width + (chars-1))/ chars; /* always round up*/
1117 else avg = 0; /* No characters exist at all */
1119 else /* uniform width */
1120 avg = x_fs->min_bounds.width;
1122 TRACE(" retuning %d\n",avg);
1123 return avg;
1126 /***********************************************************************
1127 * XFONT_GetMaxCharWidth
1129 static INT XFONT_GetMaxCharWidth(const XFontStruct* xfs, const XFONTTRANS *XFT)
1131 unsigned min = (unsigned char)xfs->min_char_or_byte2;
1132 unsigned max = (unsigned char)xfs->max_char_or_byte2;
1133 int maxwidth, j;
1135 if(!XFT || !xfs->per_char)
1136 return abs(xfs->max_bounds.width);
1138 for( j = 0, maxwidth = 0, max -= min; j <= max; j++ )
1139 if( !CI_NONEXISTCHAR(xfs->per_char + j) )
1140 if(maxwidth < xfs->per_char[j].attributes)
1141 maxwidth = xfs->per_char[j].attributes;
1143 maxwidth *= XFT->pixelsize / 1000.0;
1144 return maxwidth;
1147 /***********************************************************************
1148 * XFONT_SetFontMetric
1150 * INIT ONLY
1152 * Initializes IFONTINFO16.
1154 static void XFONT_SetFontMetric(fontInfo* fi, const fontResource* fr, XFontStruct* xfs)
1156 unsigned min, max;
1157 fi->df.dfFirstChar = (BYTE)(min = xfs->min_char_or_byte2);
1158 fi->df.dfLastChar = (BYTE)(max = xfs->max_char_or_byte2);
1160 fi->df.dfDefaultChar = (BYTE)xfs->default_char;
1161 fi->df.dfBreakChar = (BYTE)(( ' ' < min || ' ' > max) ? xfs->default_char: ' ');
1163 fi->df.dfPixHeight = (INT16)((fi->df.dfAscent = (INT16)xfs->ascent) + xfs->descent);
1164 fi->df.dfPixWidth = (xfs->per_char) ? 0 : xfs->min_bounds.width;
1166 XFONT_GetLeading( &fi->df, xfs, &fi->df.dfInternalLeading, &fi->df.dfExternalLeading, NULL );
1167 fi->df.dfAvgWidth = (INT16)XFONT_GetAvgCharWidth(&fi->df, xfs, NULL );
1168 fi->df.dfMaxWidth = (INT16)XFONT_GetMaxCharWidth(xfs, NULL);
1170 if( xfs->min_bounds.width != xfs->max_bounds.width )
1171 fi->df.dfPitchAndFamily |= TMPF_FIXED_PITCH; /* au contraire! */
1172 if( fi->fi_flags & FI_SCALABLE )
1174 fi->df.dfType = DEVICE_FONTTYPE;
1175 fi->df.dfPitchAndFamily |= TMPF_DEVICE;
1177 else if( fi->fi_flags & FI_TRUETYPE )
1178 fi->df.dfType = TRUETYPE_FONTTYPE;
1179 else
1180 fi->df.dfType = RASTER_FONTTYPE;
1182 fi->df.dfFace = fr->lfFaceName;
1185 /***********************************************************************
1186 * XFONT_GetFontMetric
1188 * Retrieve font metric info (enumeration).
1190 static UINT XFONT_GetFontMetric( const fontInfo* pfi,
1191 const LPENUMLOGFONTEXW pLF,
1192 const LPNEWTEXTMETRICEXW pTM )
1194 memset( pLF, 0, sizeof(*pLF) );
1195 memset( pTM, 0, sizeof(*pTM) );
1197 #define plf ((LPLOGFONTW)pLF)
1198 #define ptm ((LPNEWTEXTMETRICW)pTM)
1199 plf->lfHeight = ptm->tmHeight = pfi->df.dfPixHeight;
1200 plf->lfWidth = ptm->tmAveCharWidth = pfi->df.dfAvgWidth;
1201 plf->lfWeight = ptm->tmWeight = pfi->df.dfWeight;
1202 plf->lfItalic = ptm->tmItalic = pfi->df.dfItalic;
1203 plf->lfUnderline = ptm->tmUnderlined = pfi->df.dfUnderline;
1204 plf->lfStrikeOut = ptm->tmStruckOut = pfi->df.dfStrikeOut;
1205 plf->lfCharSet = ptm->tmCharSet = pfi->df.dfCharSet;
1207 /* convert pitch values */
1209 ptm->tmPitchAndFamily = pfi->df.dfPitchAndFamily;
1210 plf->lfPitchAndFamily = (pfi->df.dfPitchAndFamily & 0xF1) + 1;
1212 MultiByteToWideChar(CP_ACP, 0, pfi->df.dfFace, -1,
1213 plf->lfFaceName, LF_FACESIZE);
1215 /* FIXME: fill in rest of plF values */
1216 strcpyW(pLF->elfFullName, plf->lfFaceName);
1217 MultiByteToWideChar(CP_ACP, 0, "Regular", -1,
1218 pLF->elfStyle, LF_FACESIZE);
1219 MultiByteToWideChar(CP_ACP, 0, plf->lfCharSet == SYMBOL_CHARSET ?
1220 "Symbol" : "Roman", -1,
1221 pLF->elfScript, LF_FACESIZE);
1223 #undef plf
1225 ptm->tmAscent = pfi->df.dfAscent;
1226 ptm->tmDescent = ptm->tmHeight - ptm->tmAscent;
1227 ptm->tmInternalLeading = pfi->df.dfInternalLeading;
1228 ptm->tmMaxCharWidth = pfi->df.dfMaxWidth;
1229 ptm->tmDigitizedAspectX = pfi->df.dfHorizRes;
1230 ptm->tmDigitizedAspectY = pfi->df.dfVertRes;
1232 ptm->tmFirstChar = pfi->df.dfFirstChar;
1233 ptm->tmLastChar = pfi->df.dfLastChar;
1234 ptm->tmDefaultChar = pfi->df.dfDefaultChar;
1235 ptm->tmBreakChar = pfi->df.dfBreakChar;
1237 TRACE("Calling Enum proc with FaceName %s FullName %s\n",
1238 debugstr_w(pLF->elfLogFont.lfFaceName),
1239 debugstr_w(pLF->elfFullName));
1241 TRACE("CharSet = %d type = %d\n", ptm->tmCharSet, pfi->df.dfType);
1242 /* return font type */
1243 return pfi->df.dfType;
1244 #undef ptm
1248 /***********************************************************************
1249 * XFONT_FixupFlags
1251 * INIT ONLY
1253 * dfPitchAndFamily flags for some common typefaces.
1255 static BYTE XFONT_FixupFlags( LPCSTR lfFaceName )
1257 switch( lfFaceName[0] )
1259 case 'a':
1260 case 'A': if(!strncasecmp(lfFaceName, "Arial", 5) )
1261 return FF_SWISS;
1262 break;
1263 case 'h':
1264 case 'H': if(!strcasecmp(lfFaceName, "Helvetica") )
1265 return FF_SWISS;
1266 break;
1267 case 'c':
1268 case 'C': if(!strncasecmp(lfFaceName, "Courier", 7))
1269 return FF_MODERN;
1271 if (!strcasecmp(lfFaceName, "Charter") )
1272 return FF_ROMAN;
1273 break;
1274 case 'p':
1275 case 'P': if( !strcasecmp(lfFaceName,"Palatino") )
1276 return FF_ROMAN;
1277 break;
1278 case 't':
1279 case 'T': if(!strncasecmp(lfFaceName, "Times", 5) )
1280 return FF_ROMAN;
1281 break;
1282 case 'u':
1283 case 'U': if(!strcasecmp(lfFaceName, "Utopia") )
1284 return FF_ROMAN;
1285 break;
1286 case 'z':
1287 case 'Z': if(!strcasecmp(lfFaceName, "Zapf Dingbats") )
1288 return FF_DECORATIVE;
1290 return 0;
1293 /***********************************************************************
1294 * XFONT_SameFoundryAndFamily
1296 * INIT ONLY
1298 static BOOL XFONT_SameFoundryAndFamily( const LFD* lfd1, const LFD* lfd2 )
1300 return ( !strcasecmp( lfd1->foundry, lfd2->foundry ) &&
1301 !strcasecmp( lfd1->family, lfd2->family ) );
1304 /***********************************************************************
1305 * XFONT_InitialCapitals
1307 * INIT ONLY
1309 * Upper case first letters of words & remove multiple spaces
1311 static void XFONT_InitialCapitals(LPSTR lpch)
1313 int i;
1314 BOOL up;
1315 char* lpstr = lpch;
1317 for( i = 0, up = TRUE; *lpch; lpch++, i++ )
1319 if( isspace(*lpch) )
1321 if (!up) /* Not already got one */
1323 *lpstr++ = ' ';
1324 up = TRUE;
1327 else if( isalpha(*lpch) && up )
1329 *lpstr++ = toupper(*lpch);
1330 up = FALSE;
1332 else
1334 *lpstr++ = *lpch;
1335 up = FALSE;
1339 /* Remove possible trailing space */
1340 if (up && i > 0)
1341 --lpstr;
1342 *lpstr = '\0';
1346 /***********************************************************************
1347 * XFONT_WindowsNames
1349 * INIT ONLY
1351 * Build generic Windows aliases for X font names.
1353 * -misc-fixed- -> "Fixed"
1354 * -sony-fixed- -> "Sony Fixed", etc...
1356 static void XFONT_WindowsNames(void)
1358 fontResource* fr;
1360 for( fr = fontList; fr ; fr = fr->next )
1362 fontResource* pfr;
1363 char* lpch;
1365 if( fr->fr_flags & FR_NAMESET ) continue; /* skip already assigned */
1367 for( pfr = fontList; pfr != fr ; pfr = pfr->next )
1368 if( pfr->fr_flags & FR_NAMESET )
1370 if (!strcasecmp( pfr->resource->family, fr->resource->family))
1371 break;
1374 lpch = fr->lfFaceName;
1375 snprintf( fr->lfFaceName, sizeof(fr->lfFaceName), "%s %s",
1376 /* prepend vendor name */
1377 (pfr==fr) ? "" : fr->resource->foundry,
1378 fr->resource->family);
1379 XFONT_InitialCapitals(fr->lfFaceName);
1381 BYTE bFamilyStyle = XFONT_FixupFlags( fr->lfFaceName );
1382 if( bFamilyStyle)
1384 fontInfo* fi;
1385 for( fi = fr->fi ; fi ; fi = fi->next )
1386 fi->df.dfPitchAndFamily |= bFamilyStyle;
1390 TRACE("typeface '%s'\n", fr->lfFaceName);
1392 fr->fr_flags |= FR_NAMESET;
1396 /***********************************************************************
1397 * XFONT_LoadDefaultLFD
1399 * Move lfd to the head of fontList to make it more likely to be matched
1401 static void XFONT_LoadDefaultLFD(LFD* lfd, LPCSTR fonttype)
1404 fontResource *fr, *pfr;
1405 for( fr = NULL, pfr = fontList; pfr; pfr = pfr->next )
1407 if( XFONT_SameFoundryAndFamily(pfr->resource, lfd) )
1409 if( fr )
1411 fr->next = pfr->next;
1412 pfr->next = fontList;
1413 fontList = pfr;
1415 break;
1417 fr = pfr;
1419 if (!pfr)
1420 WARN("Default %sfont '-%s-%s-' not available\n", fonttype,
1421 lfd->foundry, lfd->family);
1425 /***********************************************************************
1426 * XFONT_LoadDefault
1428 static void XFONT_LoadDefault(LPCSTR ini, LPCSTR fonttype)
1430 char buffer[MAX_LFD_LENGTH];
1431 HKEY hkey;
1433 buffer[0] = 0;
1434 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1436 DWORD type, count = sizeof(buffer);
1437 RegQueryValueExA(hkey, ini, 0, &type, buffer, &count);
1438 RegCloseKey(hkey);
1440 if (*buffer)
1442 LFD* lfd;
1443 char* pch = buffer;
1444 while( *pch && isspace(*pch) ) pch++;
1446 TRACE("Using '%s' as default %sfont\n", pch, fonttype);
1447 lfd = LFD_Parse(pch);
1448 if (lfd && lfd->foundry && lfd->family)
1449 XFONT_LoadDefaultLFD(lfd, fonttype);
1450 else
1451 WARN("Ini section [%s]%s is malformed\n", INIFontSection, ini);
1452 HeapFree(GetProcessHeap(), 0, lfd);
1457 /***********************************************************************
1458 * XFONT_LoadDefaults
1460 static void XFONT_LoadDefaults(void)
1462 XFONT_LoadDefault(INIDefaultFixed, "fixed ");
1463 XFONT_LoadDefault(INIDefault, "");
1466 /***********************************************************************
1467 * XFONT_CreateAlias
1469 static fontAlias* XFONT_CreateAlias( LPCSTR lpTypeFace, LPCSTR lpAlias )
1471 int j;
1472 fontAlias *pfa, *prev = NULL;
1474 for(pfa = aliasTable; pfa; pfa = pfa->next)
1476 /* check if we already got one */
1477 if( !strcasecmp( pfa->faTypeFace, lpAlias ) )
1479 TRACE("redundant alias '%s' -> '%s'\n",
1480 lpAlias, lpTypeFace );
1481 return NULL;
1483 prev = pfa;
1486 j = strlen(lpTypeFace) + 1;
1487 pfa = HeapAlloc( GetProcessHeap(), 0, sizeof(fontAlias) +
1488 j + strlen(lpAlias) + 1 );
1489 if (pfa)
1491 if (!prev)
1492 aliasTable = pfa;
1493 else
1494 prev->next = pfa;
1496 pfa->next = NULL;
1497 pfa->faTypeFace = (LPSTR)(pfa + 1);
1498 strcpy( pfa->faTypeFace, lpTypeFace );
1499 pfa->faAlias = pfa->faTypeFace + j;
1500 strcpy( pfa->faAlias, lpAlias );
1502 TRACE("added alias '%s' for '%s'\n", lpAlias, lpTypeFace );
1504 return pfa;
1506 return NULL;
1510 /***********************************************************************
1511 * XFONT_LoadAlias
1513 static void XFONT_LoadAlias(const LFD* lfd, LPCSTR lpAlias, BOOL bSubst)
1515 fontResource *fr, *frMatch = NULL;
1516 if (!lfd->foundry || ! lfd->family)
1518 WARN("Malformed font resource for alias '%s'\n", lpAlias);
1519 return;
1521 for (fr = fontList; fr ; fr = fr->next)
1523 if(!strcasecmp(fr->resource->family, lpAlias))
1525 /* alias is not needed since the real font is present */
1526 TRACE("Ignoring font alias '%s' as it is already available as a real font\n", lpAlias);
1527 return;
1529 if( XFONT_SameFoundryAndFamily( fr->resource, lfd ) )
1531 frMatch = fr;
1532 break;
1536 if( frMatch )
1538 if( bSubst )
1540 fontAlias *pfa, *prev = NULL;
1542 for(pfa = aliasTable; pfa; pfa = pfa->next)
1544 /* Remove lpAlias from aliasTable - we should free the old entry */
1545 if(!strcmp(lpAlias, pfa->faAlias))
1547 if(prev)
1548 prev->next = pfa->next;
1549 else
1550 aliasTable = pfa->next;
1553 /* Update any references to the substituted font in aliasTable */
1554 if(!strcmp(frMatch->lfFaceName, pfa->faTypeFace))
1556 pfa->faTypeFace = HeapAlloc( GetProcessHeap(), 0, strlen(lpAlias)+1 );
1557 strcpy( pfa->faTypeFace, lpAlias );
1559 prev = pfa;
1562 TRACE("\tsubstituted '%s' with '%s'\n", frMatch->lfFaceName, lpAlias );
1564 lstrcpynA( frMatch->lfFaceName, lpAlias, LF_FACESIZE );
1565 frMatch->fr_flags |= FR_NAMESET;
1567 else
1569 /* create new entry in the alias table */
1570 XFONT_CreateAlias( frMatch->lfFaceName, lpAlias );
1573 else
1575 WARN("Font alias '-%s-%s-' is not available\n", lfd->foundry, lfd->family);
1579 /***********************************************************************
1580 * Just a copy of PROFILE_GetStringItem
1582 * Convenience function that turns a string 'xxx, yyy, zzz' into
1583 * the 'xxx\0 yyy, zzz' and returns a pointer to the 'yyy, zzz'.
1585 static char *XFONT_GetStringItem( char *start )
1587 #define XFONT_isspace(c) (isspace(c) || (c == '\r') || (c == 0x1a))
1588 char *lpchX, *lpch;
1590 for (lpchX = start, lpch = NULL; *lpchX != '\0'; lpchX++ )
1592 if( *lpchX == ',' )
1594 if( lpch ) *lpch = '\0'; else *lpchX = '\0';
1595 while( *(++lpchX) )
1596 if( !XFONT_isspace(*lpchX) ) return lpchX;
1598 else if( XFONT_isspace( *lpchX ) && !lpch ) lpch = lpchX;
1599 else lpch = NULL;
1601 if( lpch ) *lpch = '\0';
1602 return NULL;
1603 #undef XFONT_isspace
1606 /***********************************************************************
1607 * XFONT_LoadAliases
1609 * INIT ONLY
1611 * Create font aliases for some standard windows fonts using user's
1612 * default choice of (sans-)serif fonts
1614 * Read user-defined aliases from wine.conf. Format is as follows
1616 * Alias# = [Windows font name],[LFD font name], <substitute original name>
1618 * Example:
1619 * Alias0 = Arial, -adobe-helvetica-
1620 * Alias1 = Times New Roman, -bitstream-courier-, 1
1621 * ...
1623 * Note that from 970817 and on we have built-in alias templates that take
1624 * care of the necessary Windows typefaces.
1626 typedef struct
1628 LPSTR fatResource;
1629 LPSTR fatAlias;
1630 } aliasTemplate;
1632 static void XFONT_LoadAliases(void)
1634 char *lpResource;
1635 char buffer[MAX_LFD_LENGTH];
1636 int i = 0;
1637 LFD* lfd;
1638 HKEY hkey;
1640 /* built-ins first */
1641 strcpy(buffer, "-bitstream-charter-");
1642 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1644 DWORD type, count = sizeof(buffer);
1645 RegQueryValueExA(hkey, INIDefaultSerif, 0, &type, buffer, &count);
1646 RegCloseKey(hkey);
1648 TRACE("Using '%s' as default serif font\n", buffer);
1649 lfd = LFD_Parse(buffer);
1650 /* NB XFONT_InitialCapitals should not change these standard aliases */
1651 if (lfd)
1653 XFONT_LoadAlias( lfd, "Tms Roman", FALSE);
1654 XFONT_LoadAlias( lfd, "MS Serif", FALSE);
1655 XFONT_LoadAlias( lfd, "Times New Roman", FALSE);
1657 XFONT_LoadDefaultLFD( lfd, "serif ");
1658 HeapFree(GetProcessHeap(), 0, lfd);
1661 strcpy(buffer, "-adobe-helvetica-");
1662 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1664 DWORD type, count = sizeof(buffer);
1665 RegQueryValueExA(hkey, INIDefaultSansSerif, 0, &type, buffer, &count);
1666 RegCloseKey(hkey);
1668 TRACE("Using '%s' as default sans serif font\n", buffer);
1669 lfd = LFD_Parse(buffer);
1670 if (lfd)
1672 XFONT_LoadAlias( lfd, "Helv", FALSE);
1673 XFONT_LoadAlias( lfd, "MS Sans Serif", FALSE);
1674 XFONT_LoadAlias( lfd, "MS Shell Dlg", FALSE);
1675 XFONT_LoadAlias( lfd, "System", FALSE);
1676 XFONT_LoadAlias( lfd, "Arial", FALSE);
1678 XFONT_LoadDefaultLFD( lfd, "sans serif ");
1679 HeapFree(GetProcessHeap(), 0, lfd);
1682 /* then user specified aliases */
1685 BOOL bSubst;
1686 char subsection[32];
1687 snprintf( subsection, sizeof subsection, "%s%i", INIAliasSection, i++ );
1689 buffer[0] = 0;
1690 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1692 DWORD type, count = sizeof(buffer);
1693 RegQueryValueExA(hkey, subsection, 0, &type, buffer, &count);
1694 RegCloseKey(hkey);
1697 if (!buffer[0])
1698 break;
1700 XFONT_InitialCapitals(buffer);
1701 lpResource = XFONT_GetStringItem( buffer );
1702 bSubst = (XFONT_GetStringItem( lpResource )) ? TRUE : FALSE;
1703 if( lpResource && *lpResource )
1705 lfd = LFD_Parse(lpResource);
1706 if (lfd)
1708 XFONT_LoadAlias(lfd, buffer, bSubst);
1709 HeapFree(GetProcessHeap(), 0, lfd);
1712 else
1713 WARN("malformed font alias '%s'\n", buffer );
1715 while(TRUE);
1718 /***********************************************************************
1719 * XFONT_UnAlias
1721 * Convert an (potential) alias into a real windows name
1724 static LPCSTR XFONT_UnAlias(char* font)
1726 if (font[0])
1728 fontAlias* fa;
1729 XFONT_InitialCapitals(font); /* to remove extra white space */
1731 for( fa = aliasTable; fa; fa = fa->next )
1732 /* use case insensitive matching to handle eg "MS Sans Serif" */
1733 if( !strcasecmp( fa->faAlias, font ) )
1735 TRACE("found alias '%s'->%s'\n", font, fa->faTypeFace );
1736 strcpy(font, fa->faTypeFace);
1737 return fa->faAlias;
1738 break;
1741 return NULL;
1744 /***********************************************************************
1745 * XFONT_RemoveFontResource
1747 * Caller should check if the font resource is in use. If it is it should
1748 * set FR_REMOVED flag to delay removal until the resource is not in use
1749 * any more.
1751 void XFONT_RemoveFontResource( fontResource** ppfr )
1753 fontResource* pfr = *ppfr;
1754 #if 0
1755 fontInfo* pfi;
1757 /* FIXME - if fonts were read from a cache, these HeapFrees will fail */
1758 while( pfr->fi )
1760 pfi = pfr->fi->next;
1761 HeapFree( GetProcessHeap(), 0, pfr->fi );
1762 pfr->fi = pfi;
1764 HeapFree( GetProcessHeap(), 0, pfr );
1765 #endif
1766 *ppfr = pfr->next;
1769 /***********************************************************************
1770 * XFONT_LoadIgnores
1772 * INIT ONLY
1774 * Removes specified fonts from the font table to prevent Wine from
1775 * using it.
1777 * Ignore# = [LFD font name]
1779 * Example:
1780 * Ignore0 = -misc-nil-
1781 * Ignore1 = -sun-open look glyph-
1782 * ...
1785 static void XFONT_LoadIgnore(char* lfdname)
1787 fontResource** ppfr;
1789 LFD* lfd = LFD_Parse(lfdname);
1790 if (lfd && lfd->foundry && lfd->family)
1792 for( ppfr = &fontList; *ppfr ; ppfr = &((*ppfr)->next))
1794 if( XFONT_SameFoundryAndFamily( (*ppfr)->resource, lfd) )
1796 TRACE("Ignoring '-%s-%s-'\n",
1797 (*ppfr)->resource->foundry, (*ppfr)->resource->family );
1799 XFONT_RemoveFontResource( ppfr );
1800 break;
1804 else
1805 WARN("Malformed font resource\n");
1807 HeapFree(GetProcessHeap(), 0, lfd);
1810 static void XFONT_LoadIgnores(void)
1812 int i = 0;
1813 char subsection[32];
1814 char buffer[MAX_LFD_LENGTH];
1816 /* Standard one that noone wants */
1817 strcpy(buffer, "-misc-nil-");
1818 XFONT_LoadIgnore(buffer);
1820 /* Others from INI file */
1823 HKEY hkey;
1824 sprintf( subsection, "%s%i", INIIgnoreSection, i++ );
1826 buffer[0] = 0;
1827 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
1829 DWORD type, count = sizeof(buffer);
1830 RegQueryValueExA(hkey, subsection, 0, &type, buffer, &count);
1831 RegCloseKey(hkey);
1834 if( buffer[0] )
1836 char* pch = buffer;
1837 while( *pch && isspace(*pch) ) pch++;
1838 XFONT_LoadIgnore(pch);
1840 else
1841 break;
1842 } while(TRUE);
1846 /***********************************************************************
1847 * XFONT_UserMetricsCache
1849 * Returns expanded name for the cachedmetrics file.
1850 * Now it also appends the current value of the $DISPLAY variable.
1852 static char* XFONT_UserMetricsCache( char* buffer, int* buf_size )
1854 const char *confdir = get_config_dir();
1855 const char *display_name = XDisplayName(NULL);
1856 int len = strlen(confdir) + strlen(INIFontMetrics) + strlen(display_name) + 8;
1857 int display = 0;
1858 int screen = 0;
1859 char *p, *ext;
1862 ** Normalize the display name, since on Red Hat systems, DISPLAY
1863 ** is commonly set to one of either 'unix:0.0' or ':0' or ':0.0'.
1864 ** after this code, all of the above will resolve to ':0.0'.
1866 if (!strncmp( display_name, "unix:", 5 )) display_name += 4;
1867 p = strchr(display_name, ':');
1868 if (p) sscanf(p + 1, "%d.%d", &display, &screen);
1870 if ((len > *buf_size) &&
1871 !(buffer = HeapReAlloc( GetProcessHeap(), 0, buffer, *buf_size = len )))
1873 ERR("out of memory\n");
1874 ExitProcess(1);
1876 sprintf( buffer, "%s/%s", confdir, INIFontMetrics );
1878 ext = buffer + strlen(buffer);
1879 strcpy( ext, display_name );
1881 if (!(p = strchr( ext, ':' ))) p = ext + strlen(ext);
1882 sprintf( p, ":%d.%d", display, screen );
1883 return buffer;
1887 /***********************************************************************
1888 * X Font Matching
1890 * Compare two fonts (only parameters set by the XFONT_InitFontInfo()).
1892 static INT XFONT_IsSubset(const fontInfo* match, const fontInfo* fi)
1894 INT m;
1896 /* 0 - keep both, 1 - keep match, -1 - keep fi */
1898 /* Compare dfItalic, Underline, Strikeout, Weight, Charset */
1899 m = (BYTE*)&fi->df.dfPixWidth - (BYTE*)&fi->df.dfItalic;
1900 if( memcmp(&match->df.dfItalic, &fi->df.dfItalic, m )) return 0;
1902 if( (!((fi->fi_flags & FI_SCALABLE) + (match->fi_flags & FI_SCALABLE))
1903 && fi->lfd_height != match->lfd_height) ||
1904 (!((fi->fi_flags & FI_POLYWEIGHT) + (match->fi_flags & FI_POLYWEIGHT))
1905 && fi->df.dfWeight != match->df.dfWeight) ) return 0;
1907 m = (int)(match->fi_flags & (FI_POLYWEIGHT | FI_SCALABLE)) -
1908 (int)(fi->fi_flags & (FI_SCALABLE | FI_POLYWEIGHT));
1910 if( m == (FI_POLYWEIGHT - FI_SCALABLE) ||
1911 m == (FI_SCALABLE - FI_POLYWEIGHT) ) return 0; /* keep both */
1912 else if( m >= 0 ) return 1; /* 'match' is better */
1914 return -1; /* 'fi' is better */
1917 /***********************************************************************
1918 * XFONT_CheckFIList
1920 * REMOVE_SUBSETS - attach new fi and purge subsets
1921 * UNMARK_SUBSETS - remove subset flags from all fi entries
1923 static void XFONT_CheckFIList( fontResource* fr, fontInfo* fi, int action)
1925 int i = 0;
1926 fontInfo* pfi, *prev;
1928 for( prev = NULL, pfi = fr->fi; pfi; )
1930 if( action == REMOVE_SUBSETS )
1932 if( pfi->fi_flags & FI_SUBSET )
1934 fontInfo* subset = pfi;
1936 i++;
1937 fr->fi_count--;
1938 if( prev ) prev->next = pfi = pfi->next;
1939 else fr->fi = pfi = pfi->next;
1940 HeapFree( GetProcessHeap(), 0, subset );
1941 continue;
1944 else pfi->fi_flags &= ~FI_SUBSET;
1946 prev = pfi;
1947 pfi = pfi->next;
1950 if( action == REMOVE_SUBSETS ) /* also add the superset */
1952 if( fi->fi_flags & FI_SCALABLE )
1954 fi->next = fr->fi;
1955 fr->fi = fi;
1957 else if( prev ) prev->next = fi; else fr->fi = fi;
1958 fr->fi_count++;
1961 if( i ) TRACE("\t purged %i subsets [%i]\n", i , fr->fi_count);
1964 /***********************************************************************
1965 * XFONT_FindFIList
1967 static fontResource* XFONT_FindFIList( fontResource* pfr, const char* pTypeFace )
1969 while( pfr )
1971 if( !strcasecmp( pfr->lfFaceName, pTypeFace ) ) break;
1972 pfr = pfr->next;
1974 /* Give the app back the font name it asked for. Encarta checks this. */
1975 if (pfr) strcpy(pfr->lfFaceName,pTypeFace);
1976 return pfr;
1979 /***********************************************************************
1980 * XFONT_FixupPointSize
1982 static void XFONT_FixupPointSize(fontInfo* fi)
1984 #define df (fi->df)
1985 df.dfHorizRes = df.dfVertRes = fi->lfd_resolution;
1986 df.dfPoints = (INT16)
1987 (((INT)(df.dfPixHeight - df.dfInternalLeading) * 72 + (df.dfVertRes >> 1)) /
1988 df.dfVertRes );
1989 #undef df
1992 /***********************************************************************
1993 * XFONT_BuildMetrics
1995 * Build font metrics from X font
1997 static int XFONT_BuildMetrics(char** x_pattern, int res, unsigned x_checksum, int x_count)
1999 int i;
2000 fontInfo* fi = NULL;
2001 fontResource* fr, *pfr;
2002 int n_ff = 0;
2004 MESSAGE("Building font metrics. This may take some time...\n");
2005 for( i = 0; i < x_count; i++ )
2007 char* typeface;
2008 LFD* lfd;
2009 int j;
2010 char buffer[MAX_LFD_LENGTH];
2011 char* lpstr;
2012 XFontStruct* x_fs;
2013 fontInfo* pfi;
2015 if (!(typeface = HeapAlloc(GetProcessHeap(), 0, strlen(x_pattern[i])+1))) break;
2016 strcpy( typeface, x_pattern[i] );
2018 lfd = LFD_Parse(typeface);
2019 if (!lfd)
2021 HeapFree(GetProcessHeap(), 0, typeface);
2022 continue;
2025 /* find a family to insert into */
2027 for( pfr = NULL, fr = fontList; fr; fr = fr->next )
2029 if( XFONT_SameFoundryAndFamily(fr->resource, lfd))
2030 break;
2031 pfr = fr;
2034 if( !fi ) fi = (fontInfo*) HeapAlloc(GetProcessHeap(), 0, sizeof(fontInfo));
2036 if( !LFD_InitFontInfo( fi, lfd, x_pattern[i]) )
2037 goto nextfont;
2039 if( !fr ) /* add new family */
2041 n_ff++;
2042 fr = (fontResource*) HeapAlloc(GetProcessHeap(), 0, sizeof(fontResource));
2043 if (fr)
2045 memset(fr, 0, sizeof(fontResource));
2047 fr->resource = (LFD*) HeapAlloc(GetProcessHeap(), 0, sizeof(LFD));
2048 memset(fr->resource, 0, sizeof(LFD));
2050 TRACE("family: -%s-%s-\n", lfd->foundry, lfd->family );
2051 fr->resource->foundry = HeapAlloc(GetProcessHeap(), 0, strlen(lfd->foundry)+1);
2052 strcpy( (char *)fr->resource->foundry, lfd->foundry );
2053 fr->resource->family = HeapAlloc(GetProcessHeap(), 0, strlen(lfd->family)+1);
2054 strcpy( (char *)fr->resource->family, lfd->family );
2055 fr->resource->weight = "";
2057 if( pfr ) pfr->next = fr;
2058 else fontList = fr;
2060 else
2061 WARN("Not enough memory for a new font family\n");
2064 /* check if we already have something better than "fi" */
2066 for( pfi = fr->fi, j = 0; pfi && j <= 0; pfi = pfi->next )
2067 if( (j = XFONT_IsSubset( pfi, fi )) < 0 )
2068 pfi->fi_flags |= FI_SUBSET; /* superseded by "fi" */
2069 if( j > 0 ) goto nextfont;
2071 /* add new font instance "fi" to the "fr" font resource */
2073 if( fi->fi_flags & FI_SCALABLE )
2075 LFD lfd1;
2076 char pxl_string[4], res_string[4];
2077 /* set scalable font height to get an basis for extrapolation */
2079 fi->lfd_height = DEF_SCALABLE_HEIGHT;
2080 fi->lfd_resolution = res;
2082 sprintf(pxl_string, "%d", fi->lfd_height);
2083 sprintf(res_string, "%d", fi->lfd_resolution);
2085 lfd1 = *lfd;
2086 lfd1.pixel_size = pxl_string;
2087 lfd1.point_size = "*";
2088 lfd1.resolution_x = res_string;
2089 lfd1.resolution_y = res_string;
2091 LFD_UnParse(buffer, sizeof buffer, &lfd1);
2093 lpstr = buffer;
2095 else lpstr = x_pattern[i];
2097 if( (x_fs = TSXLoadQueryFont(gdi_display, lpstr)) )
2099 XFONT_SetFontMetric( fi, fr, x_fs );
2100 TSXFreeFont( gdi_display, x_fs );
2102 XFONT_FixupPointSize(fi);
2104 TRACE("\t[% 2ipt] '%s'\n", fi->df.dfPoints, x_pattern[i] );
2106 XFONT_CheckFIList( fr, fi, REMOVE_SUBSETS );
2107 fi = NULL; /* preventing reuse */
2109 else
2111 ERR("failed to load %s\n", lpstr );
2113 XFONT_CheckFIList( fr, fi, UNMARK_SUBSETS );
2115 nextfont:
2116 HeapFree(GetProcessHeap(), 0, lfd);
2117 HeapFree(GetProcessHeap(), 0, typeface);
2119 if( fi ) HeapFree(GetProcessHeap(), 0, fi);
2121 /* Scan through the font list and remove FontResource(s) (fr)
2122 * that have no associated Fontinfo(s) (fi).
2123 * This code is necessary because XFONT_ReadCachedMetrics
2124 * assumes that there is at least one fi associated with a fr.
2125 * This assumption is invalid for TT font
2126 * -altsys-ms outlook-medium-r-normal--0-0-0-0-p-0-microsoft-symbol.
2129 fr = fontList;
2131 while (!fr->fi_count)
2133 fontList = fr->next;
2135 HeapFree(GetProcessHeap(), 0, fr->resource);
2136 HeapFree(GetProcessHeap(), 0, fr);
2138 fr = fontList;
2139 n_ff--;
2142 fr = fontList;
2144 while (fr->next)
2146 if (!fr->next->fi_count)
2148 pfr = fr->next;
2149 fr->next = fr->next->next;
2151 HeapFree(GetProcessHeap(), 0, pfr->resource);
2152 HeapFree(GetProcessHeap(), 0, pfr);
2154 n_ff--;
2156 else
2157 fr = fr->next;
2160 return n_ff;
2163 /***********************************************************************
2164 * XFONT_ReadCachedMetrics
2166 * INIT ONLY
2168 static BOOL XFONT_ReadCachedMetrics( int fd, int res, unsigned x_checksum, int x_count )
2170 if( fd >= 0 )
2172 unsigned u;
2173 int i, j;
2175 /* read checksums */
2176 read( fd, &u, sizeof(unsigned) );
2177 read( fd, &i, sizeof(int) );
2179 if( u == x_checksum && i == x_count )
2181 off_t length, offset = 3 * sizeof(int);
2183 /* read total size */
2184 read( fd, &i, sizeof(int) );
2185 length = lseek( fd, 0, SEEK_END );
2187 if( length == (i + offset) )
2189 lseek( fd, offset, SEEK_SET );
2190 fontList = (fontResource*)HeapAlloc( GetProcessHeap(), 0, i);
2191 if( fontList )
2193 fontResource* pfr = fontList;
2194 fontInfo* pfi = NULL;
2196 TRACE("Reading cached font metrics:\n");
2198 read( fd, fontList, i); /* read all metrics at once */
2199 while( offset < length )
2201 offset += sizeof(fontResource) + sizeof(fontInfo);
2202 pfr->fi = pfi = (fontInfo*)(pfr + 1);
2203 j = 1;
2204 while( TRUE )
2206 if( offset > length ||
2207 pfi->cptable >= (UINT16)X11DRV_CPTABLE_COUNT ||
2208 (int)(pfi->next) != j++ )
2210 TRACE("error: offset=%ld length=%ld cptable=%d pfi->next=%d j=%d\n",(long)offset,(long)length,pfi->cptable,(int)pfi->next,j-1);
2211 goto fail;
2214 if( pfi->df.dfPixHeight == 0 )
2216 TRACE("error: dfPixHeight==0\n");
2217 goto fail;
2220 pfi->df.dfFace = pfr->lfFaceName;
2221 if( pfi->fi_flags & FI_SCALABLE )
2223 /* we can pretend we got this font for any resolution */
2224 pfi->lfd_resolution = res;
2225 XFONT_FixupPointSize(pfi);
2227 pfi->next = pfi + 1;
2229 if( j > pfr->fi_count ) break;
2231 pfi = pfi->next;
2232 offset += sizeof(fontInfo);
2234 pfi->next = NULL;
2235 if( pfr->next )
2237 pfr->next = (fontResource*)(pfi + 1);
2238 pfr = pfr->next;
2240 else break;
2242 if( pfr->next == NULL &&
2243 *(int*)(pfi + 1) == X_FMC_MAGIC )
2245 /* read LFD stubs */
2246 char* lpch = (char*)((int*)(pfi + 1) + 1);
2247 offset += sizeof(int);
2248 for( pfr = fontList; pfr; pfr = pfr->next )
2250 size_t len = strlen(lpch) + 1;
2251 TRACE("\t%s, %i instances\n", lpch, pfr->fi_count );
2252 pfr->resource = LFD_Parse(lpch);
2253 lpch += len;
2254 offset += len;
2255 if (offset > length)
2257 TRACE("error: offset=%ld length=%ld\n",(long)offset,(long)length);
2258 goto fail;
2261 close( fd );
2262 return TRUE;
2265 } else {
2266 TRACE("Wrong length: %ld!=%ld\n",(long)length,(long)(i+offset));
2268 } else {
2269 TRACE("Checksum (%x vs. %x) or count (%d vs. %d) mismatch\n",
2270 u,x_checksum,i,x_count);
2272 fail:
2273 if( fontList ) HeapFree( GetProcessHeap(), 0, fontList );
2274 fontList = NULL;
2275 close( fd );
2277 return FALSE;
2280 /***********************************************************************
2281 * XFONT_WriteCachedMetrics
2283 * INIT ONLY
2285 static BOOL XFONT_WriteCachedMetrics( int fd, unsigned x_checksum, int x_count, int n_ff )
2287 fontResource* pfr;
2288 fontInfo* pfi;
2290 if( fd >= 0 )
2292 int i, j, k;
2293 char buffer[MAX_LFD_LENGTH];
2295 /* font metrics file:
2297 * +0000 x_checksum
2298 * +0004 x_count
2299 * +0008 total size to load
2300 * +000C prepackaged font metrics
2301 * ...
2302 * +...x X_FMC_MAGIC
2303 * +...x + 4 LFD stubs
2306 write( fd, &x_checksum, sizeof(unsigned) );
2307 write( fd, &x_count, sizeof(int) );
2309 for( j = i = 0, pfr = fontList; pfr; pfr = pfr->next )
2311 LFD_UnParse(buffer, sizeof buffer, pfr->resource);
2312 i += strlen( buffer) + 1;
2313 j += pfr->fi_count;
2315 i += n_ff * sizeof(fontResource) + j * sizeof(fontInfo) + sizeof(int);
2316 write( fd, &i, sizeof(int) );
2318 TRACE("Writing font cache:\n");
2320 for( pfr = fontList; pfr; pfr = pfr->next )
2322 fontInfo fi;
2324 TRACE("\t-%s-%s-, %i instances\n", pfr->resource->foundry, pfr->resource->family, pfr->fi_count );
2326 i = write( fd, pfr, sizeof(fontResource) );
2327 if( i == sizeof(fontResource) )
2329 for( k = 1, pfi = pfr->fi; pfi; pfi = pfi->next )
2331 fi = *pfi;
2333 fi.df.dfFace = NULL;
2334 fi.next = (fontInfo*)k; /* loader checks this */
2336 j = write( fd, &fi, sizeof(fi) );
2337 k++;
2339 if( j == sizeof(fontInfo) ) continue;
2341 break;
2343 if( i == sizeof(fontResource) && j == sizeof(fontInfo) )
2345 i = j = X_FMC_MAGIC;
2346 write( fd, &i, sizeof(int) );
2347 for( pfr = fontList; pfr && i == j; pfr = pfr->next )
2349 LFD_UnParse(buffer, sizeof buffer, pfr->resource);
2350 i = strlen( buffer ) + 1;
2351 j = write( fd, buffer, i );
2354 close( fd );
2355 return ( i == j );
2357 return FALSE;
2360 /***********************************************************************
2361 * XFONT_GetPointResolution()
2363 * INIT ONLY
2365 * Here we initialize DefResolution which is used in the
2366 * XFONT_Match() penalty function. We also load the point
2367 * resolution value (higher values result in larger fonts).
2369 static int XFONT_GetPointResolution( int *log_pixels_x, int *log_pixels_y )
2371 int i, j, point_resolution, num = 3;
2372 int allowed_xfont_resolutions[3] = { 72, 75, 100 };
2373 int best = 0, best_diff = 65536;
2374 HKEY hkey;
2376 point_resolution = 0;
2378 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
2380 char buffer[20];
2381 DWORD type, count = sizeof(buffer);
2382 if(!RegQueryValueExA(hkey, INIResolution, 0, &type, buffer, &count))
2383 point_resolution = atoi(buffer);
2384 RegCloseKey(hkey);
2387 if( !point_resolution )
2388 point_resolution = *log_pixels_y;
2389 else
2390 *log_pixels_x = *log_pixels_y = point_resolution;
2393 /* FIXME We can only really guess at a best DefResolution
2394 * - this should be configurable
2396 for( i = best = 0; i < num; i++ )
2398 j = abs( point_resolution - allowed_xfont_resolutions[i] );
2399 if( j < best_diff )
2401 best = i;
2402 best_diff = j;
2405 DefResolution = allowed_xfont_resolutions[best];
2406 return point_resolution;
2410 /***********************************************************************
2411 * XFONT_Match
2413 * Compute the matching score between the logical font and the device font.
2415 * contributions from highest to lowest:
2416 * charset
2417 * fixed pitch
2418 * height
2419 * family flags (only when the facename is not present)
2420 * width
2421 * weight, italics, underlines, strikeouts
2423 * NOTE: you can experiment with different penalty weights to see what happens.
2424 * http://premium.microsoft.com/msdn/library/techart/f365/f36b/f37b/d38b/sa8bf.htm
2426 static UINT XFONT_Match( fontMatch* pfm )
2428 fontInfo* pfi = pfm->pfi; /* device font to match */
2429 LPLOGFONT16 plf = pfm->plf; /* wanted logical font */
2430 UINT penalty = 0;
2431 BOOL bR6 = pfm->flags & FO_MATCH_XYINDEP; /* from TextCaps */
2432 BOOL bScale = pfi->fi_flags & FI_SCALABLE;
2433 int d = 0, height;
2435 TRACE("\t[ %-2ipt h=%-3i w=%-3i %s%s]\n", pfi->df.dfPoints,
2436 pfi->df.dfPixHeight, pfi->df.dfAvgWidth,
2437 (pfi->df.dfWeight > FW_NORMAL) ? "Bold " : "Normal ",
2438 (pfi->df.dfItalic) ? "Italic" : "" );
2440 pfm->flags &= FO_MATCH_MASK;
2442 /* Charset */
2443 /* pfm->internal_charset: given(required) charset */
2444 /* pfi->internal_charset: charset of this font */
2445 if (pfi->internal_charset == DEFAULT_CHARSET)
2447 /* special case(unicode font) */
2448 /* priority: unmatched charset < unicode < matched charset */
2449 penalty += 0x50;
2451 else
2453 if( pfm->internal_charset == DEFAULT_CHARSET )
2456 if (pfi->internal_charset != ANSI_CHARSET)
2457 penalty += 0x200;
2459 if ( pfi->codepage != GetACP() )
2460 penalty += 0x200;
2462 else if (pfm->internal_charset != pfi->internal_charset)
2464 if ( pfi->internal_charset & 0xff00 )
2465 penalty += 0x1000; /* internal charset - should not be used */
2466 else
2467 penalty += 0x200;
2471 /* Height */
2472 height = -1;
2474 if( plf->lfHeight > 0 )
2476 int h = pfi->df.dfPixHeight;
2477 d = h - plf->lfHeight;
2478 height = plf->lfHeight;
2480 else
2482 int h = pfi->df.dfPixHeight - pfi->df.dfInternalLeading;
2483 if (h)
2485 d = h + plf->lfHeight;
2486 height = (-plf->lfHeight * pfi->df.dfPixHeight) / h;
2488 else
2490 ERR("PixHeight == InternalLeading\n");
2491 penalty += 0x1000; /* dont want this */
2496 if( height == 0 )
2497 pfm->height = 1; /* Very small */
2498 else if( d )
2500 if( bScale )
2501 pfm->height = height;
2502 else if( (plf->lfQuality != PROOF_QUALITY) && bR6 )
2504 if( d > 0 ) /* do not shrink raster fonts */
2506 pfm->height = pfi->df.dfPixHeight;
2507 penalty += (pfi->df.dfPixHeight - height) * 0x4;
2509 else /* expand only in integer multiples */
2511 pfm->height = height - height%pfi->df.dfPixHeight;
2512 penalty += (height - pfm->height + 1) * height / pfi->df.dfPixHeight;
2515 else /* can't be scaled at all */
2517 if( plf->lfQuality != PROOF_QUALITY) pfm->flags |= FO_SYNTH_HEIGHT;
2518 pfm->height = pfi->df.dfPixHeight;
2519 penalty += (d > 0)? d * 0x8 : -d * 0x10;
2522 else
2523 pfm->height = pfi->df.dfPixHeight;
2525 /* Pitch and Family */
2526 if( pfm->flags & FO_MATCH_PAF ) {
2527 int family = plf->lfPitchAndFamily & FF_FAMILY;
2529 /* TMPF_FIXED_PITCH means exactly the opposite */
2530 if( plf->lfPitchAndFamily & FIXED_PITCH ) {
2531 if( pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH ) penalty += 0x100;
2532 } else /* Variable is the default */
2533 if( !(pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH) ) penalty += 0x2;
2535 if (family != FF_DONTCARE && family != (pfi->df.dfPitchAndFamily & FF_FAMILY) )
2536 penalty += 0x10;
2539 /* Width */
2540 if( plf->lfWidth )
2542 int h;
2543 if( bR6 || bScale ) h = 0;
2544 else
2546 /* FIXME: not complete */
2548 pfm->flags |= FO_SYNTH_WIDTH;
2549 h = abs(plf->lfWidth - (pfm->height * pfi->df.dfAvgWidth)/pfi->df.dfPixHeight);
2551 penalty += h * ( d ) ? 0x2 : 0x1 ;
2553 else if( !(pfi->fi_flags & FI_NORMAL) ) penalty++;
2555 /* Weight */
2556 if( plf->lfWeight != FW_DONTCARE )
2558 penalty += abs(plf->lfWeight - pfi->df.dfWeight) / 40;
2559 if( plf->lfWeight > pfi->df.dfWeight ) pfm->flags |= FO_SYNTH_BOLD;
2560 } else if( pfi->df.dfWeight >= FW_BOLD ) penalty++; /* choose normal by default */
2562 /* Italic */
2563 if( plf->lfItalic != pfi->df.dfItalic )
2565 penalty += 0x4;
2566 pfm->flags |= FO_SYNTH_ITALIC;
2568 /* Underline */
2569 if( plf->lfUnderline ) pfm->flags |= FO_SYNTH_UNDERLINE;
2571 /* Strikeout */
2572 if( plf->lfStrikeOut ) pfm->flags |= FO_SYNTH_STRIKEOUT;
2575 if( penalty && !bScale && pfi->lfd_resolution != DefResolution )
2576 penalty++;
2578 TRACE(" returning %i\n", penalty );
2580 return penalty;
2583 /***********************************************************************
2584 * XFONT_MatchFIList
2586 * Scan a particular font resource for the best match.
2588 static UINT XFONT_MatchFIList( fontMatch* pfm )
2590 BOOL skipRaster = (pfm->flags & FO_MATCH_NORASTER);
2591 UINT current_score, score = (UINT)(-1);
2592 fontMatch fm = *pfm;
2594 for( fm.pfi = pfm->pfr->fi; fm.pfi && score; fm.pfi = fm.pfi->next)
2596 if( skipRaster && !(fm.pfi->fi_flags & FI_SCALABLE) )
2597 continue;
2599 current_score = XFONT_Match( &fm );
2600 if( score > current_score )
2602 *pfm = fm;
2603 score = current_score;
2606 return score;
2609 /***********************************************************************
2610 * XFONT_MatchDeviceFont
2612 * Scan font resource tree.
2615 static void XFONT_MatchDeviceFont( fontResource* start, fontMatch* pfm)
2617 fontMatch fm = *pfm;
2618 UINT current_score, score = (UINT)(-1);
2619 fontResource** ppfr;
2621 TRACE("(%u) '%s' h=%i weight=%i %s\n",
2622 pfm->plf->lfCharSet, pfm->plf->lfFaceName, pfm->plf->lfHeight,
2623 pfm->plf->lfWeight, (pfm->plf->lfItalic) ? "Italic" : "" );
2625 pfm->pfi = NULL;
2626 if( fm.plf->lfFaceName[0] )
2628 fm.pfr = XFONT_FindFIList( start, fm.plf->lfFaceName);
2629 if( fm.pfr ) /* match family */
2631 TRACE("found facename '%s'\n", fm.pfr->lfFaceName );
2633 if( fm.pfr->fr_flags & FR_REMOVED )
2634 fm.pfr = 0;
2635 else
2637 XFONT_MatchFIList( &fm );
2638 *pfm = fm;
2639 if (pfm->pfi)
2640 return;
2644 /* get charset if lfFaceName is one of known facenames. */
2646 const struct CharsetBindingInfo* pcharsetbindings;
2648 pcharsetbindings = &charsetbindings[0];
2649 while ( pcharsetbindings->pszFaceName != NULL )
2651 if ( !strcmp( pcharsetbindings->pszFaceName,
2652 fm.plf->lfFaceName ) )
2654 fm.internal_charset = pcharsetbindings->charset;
2655 break;
2657 pcharsetbindings ++;
2659 TRACE( "%s charset %u\n", fm.plf->lfFaceName, fm.internal_charset );
2663 /* match all available fonts */
2665 fm.flags |= FO_MATCH_PAF;
2666 for( ppfr = &fontList; *ppfr && score; ppfr = &(*ppfr)->next )
2668 if( (*ppfr)->fr_flags & FR_REMOVED )
2670 if( (*ppfr)->fo_count == 0 )
2671 XFONT_RemoveFontResource( ppfr );
2672 continue;
2675 fm.pfr = *ppfr;
2677 TRACE("%s\n", fm.pfr->lfFaceName );
2679 current_score = XFONT_MatchFIList( &fm );
2680 if( current_score < score )
2682 score = current_score;
2683 *pfm = fm;
2689 /***********************************************************************
2690 * X Font Cache
2692 static void XFONT_GrowFreeList(int start, int end)
2694 /* add all entries from 'start' up to and including 'end' */
2696 memset( fontCache + start, 0, (end - start + 1) * sizeof(fontObject) );
2698 fontCache[end].lru = fontLF;
2699 fontCache[end].count = -1;
2700 fontLF = start;
2701 while( start < end )
2703 fontCache[start].count = -1;
2704 fontCache[start].lru = start + 1;
2705 start++;
2709 static fontObject* XFONT_LookupCachedFont( const LPLOGFONT16 plf, UINT16* checksum )
2711 UINT16 cs = __lfCheckSum( plf );
2712 int i = fontMRU, prev = -1;
2714 *checksum = cs;
2715 while( i >= 0 )
2717 if( fontCache[i].lfchecksum == cs &&
2718 !(fontCache[i].fo_flags & FO_REMOVED) )
2720 /* FIXME: something more intelligent here ? */
2722 if( !memcmp( plf, &fontCache[i].lf,
2723 sizeof(LOGFONT16) - LF_FACESIZE ) &&
2724 !strcmp( plf->lfFaceName, fontCache[i].lf.lfFaceName) )
2726 /* remove temporarily from the lru list */
2727 if( prev >= 0 )
2728 fontCache[prev].lru = fontCache[i].lru;
2729 else
2730 fontMRU = (INT16)fontCache[i].lru;
2731 return (fontCache + i);
2734 prev = i;
2735 i = (INT16)fontCache[i].lru;
2737 return NULL;
2740 static fontObject* XFONT_GetCacheEntry(void)
2742 int i;
2744 if( fontLF == -1 )
2746 int prev_i, prev_j, j;
2748 TRACE("font cache is full\n");
2750 /* lookup the least recently used font */
2752 for( prev_i = prev_j = j = -1, i = fontMRU; i >= 0; i = (INT16)fontCache[i].lru )
2754 if( fontCache[i].count <= 0 &&
2755 !(fontCache[i].fo_flags & FO_SYSTEM) )
2757 prev_j = prev_i;
2758 j = i;
2760 prev_i = i;
2763 if( j >= 0 ) /* unload font */
2765 /* detach from the lru list */
2767 TRACE("\tfreeing entry %i\n", j );
2769 fontCache[j].fr->fo_count--;
2771 if( prev_j >= 0 )
2772 fontCache[prev_j].lru = fontCache[j].lru;
2773 else fontMRU = (INT16)fontCache[j].lru;
2775 /* FIXME: lpXForm, lpPixmap */
2776 if(fontCache[j].lpX11Trans)
2777 HeapFree( GetProcessHeap(), 0, fontCache[j].lpX11Trans );
2779 TSXFreeFont( gdi_display, fontCache[j].fs );
2781 memset( fontCache + j, 0, sizeof(fontObject) );
2782 return (fontCache + j);
2784 else /* expand cache */
2786 fontObject* newCache;
2788 prev_i = fontCacheSize + FONTCACHE;
2790 TRACE("\tgrowing font cache from %i to %i\n", fontCacheSize, prev_i );
2792 if( (newCache = (fontObject*)HeapReAlloc(GetProcessHeap(), 0,
2793 fontCache, prev_i)) )
2795 i = fontCacheSize;
2796 fontCacheSize = prev_i;
2797 fontCache = newCache;
2798 XFONT_GrowFreeList( i, fontCacheSize - 1);
2800 else return NULL;
2804 /* detach from the free list */
2806 i = fontLF;
2807 fontLF = (INT16)fontCache[i].lru;
2808 fontCache[i].count = 0;
2809 return (fontCache + i);
2812 static int XFONT_ReleaseCacheEntry(const fontObject* pfo)
2814 UINT u = (UINT)(pfo - fontCache);
2815 int i;
2816 int ret;
2818 if( u < fontCacheSize )
2820 ret = --fontCache[u].count;
2821 if ( ret == 0 )
2823 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
2825 if( CHECK_PFONT(pfo->prefobjs[i]) )
2826 XFONT_ReleaseCacheEntry(__PFONT(pfo->prefobjs[i]));
2830 return ret;
2833 return -1;
2836 /***********************************************************************
2837 * X11DRV_FONT_Init
2839 * Initialize font resource list and allocate font cache.
2841 int X11DRV_FONT_Init( int *log_pixels_x, int *log_pixels_y )
2843 char** x_pattern;
2844 unsigned x_checksum;
2845 int i,res, x_count, fd, buf_size;
2846 char *buffer;
2847 HKEY hkey;
2849 res = XFONT_GetPointResolution( log_pixels_x, log_pixels_y );
2851 x_pattern = TSXListFonts(gdi_display, "*", MAX_FONTS, &x_count );
2853 TRACE("Font Mapper: initializing %i fonts [logical dpi=%i, default dpi=%i]\n",
2854 x_count, res, DefResolution);
2855 if (x_count == MAX_FONTS)
2856 MESSAGE("There may be more fonts available - try increasing the value of MAX_FONTS\n");
2858 for( i = x_checksum = 0; i < x_count; i++ )
2860 int j;
2861 #if 0
2862 printf("%i\t: %s\n", i, x_pattern[i] );
2863 #endif
2865 j = strlen( x_pattern[i] );
2866 if( j ) x_checksum ^= __genericCheckSum( x_pattern[i], j );
2868 x_checksum |= X_PFONT_MAGIC;
2869 buf_size = 128;
2870 buffer = HeapAlloc( GetProcessHeap(), 0, buf_size );
2872 /* deal with systemwide font metrics cache */
2874 buffer[0] = 0;
2875 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, INIFontSection, &hkey))
2877 DWORD type, count = buf_size;
2878 RegQueryValueExA(hkey, INIGlobalMetrics, 0, &type, buffer, &count);
2879 RegCloseKey(hkey);
2882 if( buffer[0] )
2884 fd = open( buffer, O_RDONLY );
2885 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2887 if (fontList == NULL)
2889 /* try per-user */
2890 buffer = XFONT_UserMetricsCache( buffer, &buf_size );
2891 if( buffer[0] )
2893 fd = open( buffer, O_RDONLY );
2894 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2898 if( fontList == NULL ) /* build metrics from scratch */
2900 int n_ff = XFONT_BuildMetrics(x_pattern, DefResolution, x_checksum, x_count);
2901 if( buffer[0] ) /* update cached metrics */
2903 fd = open( buffer, O_CREAT | O_TRUNC | O_RDWR, 0644 ); /* -rw-r--r-- */
2904 if( XFONT_WriteCachedMetrics( fd, x_checksum, x_count, n_ff ) == FALSE )
2906 WARN("Unable to write to fontcache '%s'\n", buffer);
2907 if( fd >= 0) remove( buffer ); /* couldn't write entire file */
2912 TSXFreeFontNames(x_pattern);
2914 /* check if we're dealing with X11 R6 server */
2916 XFontStruct* x_fs;
2917 strcpy(buffer, "-*-*-*-*-normal-*-[12 0 0 12]-*-72-*-*-*-iso8859-1");
2918 if( (x_fs = TSXLoadQueryFont(gdi_display, buffer)) )
2920 XTextCaps |= TC_SF_X_YINDEP;
2921 TSXFreeFont(gdi_display, x_fs);
2924 HeapFree(GetProcessHeap(), 0, buffer);
2926 XFONT_WindowsNames();
2927 XFONT_LoadAliases();
2928 XFONT_LoadDefaults();
2929 XFONT_LoadIgnores();
2931 /* fontList initialization is over, allocate X font cache */
2933 fontCache = (fontObject*) HeapAlloc(GetProcessHeap(), 0, fontCacheSize * sizeof(fontObject));
2934 XFONT_GrowFreeList(0, fontCacheSize - 1);
2936 TRACE("done!\n");
2938 /* update text caps parameter */
2940 RAW_ASCENT = TSXInternAtom(gdi_display, "RAW_ASCENT", TRUE);
2941 RAW_DESCENT = TSXInternAtom(gdi_display, "RAW_DESCENT", TRUE);
2943 if(X11DRV_XRender_Installed)
2944 XTextCaps |= TC_VA_ABLE;
2946 return XTextCaps;
2949 /**********************************************************************
2950 * XFONT_SetX11Trans
2952 static BOOL XFONT_SetX11Trans( fontObject *pfo )
2954 char *fontName;
2955 Atom nameAtom;
2956 LFD* lfd;
2958 TSXGetFontProperty( pfo->fs, XA_FONT, &nameAtom );
2959 fontName = TSXGetAtomName( gdi_display, nameAtom );
2960 lfd = LFD_Parse(fontName);
2961 if (!lfd)
2963 TSXFree(fontName);
2964 return FALSE;
2967 if (lfd->pixel_size[0] != '[') {
2968 HeapFree(GetProcessHeap(), 0, lfd);
2969 TSXFree(fontName);
2970 return FALSE;
2973 #define PX pfo->lpX11Trans
2975 sscanf(lfd->pixel_size, "[%f%f%f%f]", &PX->a, &PX->b, &PX->c, &PX->d);
2976 TSXFree(fontName);
2977 HeapFree(GetProcessHeap(), 0, lfd);
2979 TSXGetFontProperty( pfo->fs, RAW_ASCENT, &PX->RAW_ASCENT );
2980 TSXGetFontProperty( pfo->fs, RAW_DESCENT, &PX->RAW_DESCENT );
2982 PX->pixelsize = hypot(PX->a, PX->b);
2983 PX->ascent = PX->pixelsize / 1000.0 * PX->RAW_ASCENT;
2984 PX->descent = PX->pixelsize / 1000.0 * PX->RAW_DESCENT;
2986 TRACE("[%f %f %f %f] RA = %ld RD = %ld\n",
2987 PX->a, PX->b, PX->c, PX->d,
2988 PX->RAW_ASCENT, PX->RAW_DESCENT);
2990 #undef PX
2991 return TRUE;
2994 /***********************************************************************
2995 * X Device Font Objects
2997 static X_PHYSFONT XFONT_RealizeFont( const LPLOGFONT16 plf,
2998 LPCSTR* faceMatched, BOOL bSubFont,
2999 WORD internal_charset,
3000 WORD* pcharsetMatched )
3002 UINT16 checksum;
3003 INT index = 0;
3004 fontObject* pfo;
3006 pfo = XFONT_LookupCachedFont( plf, &checksum );
3007 if( !pfo )
3009 fontMatch fm;
3010 INT i;
3012 fm.pfr = NULL;
3013 fm.pfi = NULL;
3014 fm.height = 0;
3015 fm.flags = 0;
3016 fm.plf = plf;
3017 fm.internal_charset = internal_charset;
3019 if( XTextCaps & TC_SF_X_YINDEP ) fm.flags = FO_MATCH_XYINDEP;
3021 /* allocate new font cache entry */
3023 if( (pfo = XFONT_GetCacheEntry()) )
3025 /* initialize entry and load font */
3026 char lpLFD[MAX_LFD_LENGTH];
3027 UINT uRelaxLevel = 0;
3029 if(abs(plf->lfHeight) > MAX_FONT_SIZE) {
3030 ERR(
3031 "plf->lfHeight = %d, Creating a 100 pixel font and rescaling metrics \n",
3032 plf->lfHeight);
3033 pfo->rescale = fabs(plf->lfHeight / 100.0);
3034 if(plf->lfHeight > 0) plf->lfHeight = 100;
3035 else plf->lfHeight = -100;
3036 } else
3037 pfo->rescale = 1.0;
3039 XFONT_MatchDeviceFont( fontList, &fm );
3040 pfo->fr = fm.pfr;
3041 pfo->fi = fm.pfi;
3042 pfo->fr->fo_count++;
3043 pfo->fo_flags = fm.flags & ~FO_MATCH_MASK;
3045 pfo->lf = *plf;
3046 pfo->lfchecksum = checksum;
3050 LFD_ComposeLFD( pfo, fm.height, lpLFD, uRelaxLevel++ );
3051 if( (pfo->fs = TSXLoadQueryFont( gdi_display, lpLFD )) ) break;
3052 } while( uRelaxLevel );
3055 if(pfo->lf.lfEscapement != 0) {
3056 pfo->lpX11Trans = HeapAlloc(GetProcessHeap(), 0, sizeof(XFONTTRANS));
3057 if(!XFONT_SetX11Trans( pfo )) {
3058 HeapFree(GetProcessHeap(), 0, pfo->lpX11Trans);
3059 pfo->lpX11Trans = NULL;
3062 XFONT_GetLeading( &pfo->fi->df, pfo->fs,
3063 &pfo->foInternalLeading, NULL, pfo->lpX11Trans );
3064 pfo->foAvgCharWidth = (INT16)XFONT_GetAvgCharWidth(&pfo->fi->df, pfo->fs, pfo->lpX11Trans );
3065 pfo->foMaxCharWidth = (INT16)XFONT_GetMaxCharWidth(pfo->fs, pfo->lpX11Trans);
3067 /* FIXME: If we've got a soft font or
3068 * there are FO_SYNTH_... flags for the
3069 * non PROOF_QUALITY request, the engine
3070 * should rasterize characters into mono
3071 * pixmaps and store them in the pfo->lpPixmap
3072 * array (pfo->fs should be updated as well).
3073 * array (pfo->fs should be updated as well).
3074 * X11DRV_ExtTextOut() must be heavily modified
3075 * to support pixmap blitting and FO_SYNTH_...
3076 * styles.
3079 pfo->lpPixmap = NULL;
3081 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
3082 pfo->prefobjs[i] = (X_PHYSFONT)0xffffffff; /* invalid value */
3084 /* special treatment for DBCS that needs multiple fonts */
3085 /* All member of pfo must be set correctly. */
3086 if ( bSubFont == FALSE )
3088 WORD charset_sub;
3089 WORD charsetMatchedSub;
3090 LOGFONT16 lfSub;
3091 LPCSTR faceMatchedSub;
3093 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
3095 charset_sub = X11DRV_cptable[pfo->fi->cptable].
3096 penum_subfont_charset( i );
3097 if ( charset_sub == DEFAULT_CHARSET ) break;
3099 lfSub = *plf;
3100 lfSub.lfWidth = 0;
3101 lfSub.lfHeight=plf->lfHeight;
3102 lfSub.lfCharSet = (BYTE)(charset_sub & 0xff);
3103 lfSub.lfFaceName[0] = '\0'; /* FIXME? */
3104 /* this font has sub font */
3105 if ( i == 0 ) pfo->prefobjs[0] = (X_PHYSFONT)0;
3106 pfo->prefobjs[i] =
3107 XFONT_RealizeFont( &lfSub, &faceMatchedSub,
3108 TRUE, charset_sub,
3109 &charsetMatchedSub );
3110 /* FIXME: check charsetMatchedSub */
3115 if( !pfo ) /* couldn't get a new entry, get one of the cached fonts */
3117 UINT current_score, score = (UINT)(-1);
3119 i = index = fontMRU;
3120 fm.flags |= FO_MATCH_PAF;
3123 pfo = fontCache + i;
3124 fm.pfr = pfo->fr; fm.pfi = pfo->fi;
3126 current_score = XFONT_Match( &fm );
3127 if( current_score < score ) index = i;
3129 i = pfo->lru;
3130 } while( i >= 0 );
3131 pfo = fontCache + index;
3132 goto END;
3136 /* attach at the head of the lru list */
3137 pfo->lru = fontMRU;
3138 index = fontMRU = (pfo - fontCache);
3140 END:
3141 pfo->count++;
3143 TRACE("physfont %i\n", index);
3144 *faceMatched = pfo->fi->df.dfFace;
3145 *pcharsetMatched = pfo->fi->internal_charset;
3147 return (X_PHYSFONT)(X_PFONT_MAGIC | index);
3150 /***********************************************************************
3151 * XFONT_GetFontObject
3153 fontObject* XFONT_GetFontObject( X_PHYSFONT pFont )
3155 if( CHECK_PFONT(pFont) ) return __PFONT(pFont);
3156 return NULL;
3159 /***********************************************************************
3160 * XFONT_GetFontStruct
3162 XFontStruct* XFONT_GetFontStruct( X_PHYSFONT pFont )
3164 if( CHECK_PFONT(pFont) ) return __PFONT(pFont)->fs;
3165 return NULL;
3168 /***********************************************************************
3169 * XFONT_GetFontInfo
3171 LPIFONTINFO16 XFONT_GetFontInfo( X_PHYSFONT pFont )
3173 if( CHECK_PFONT(pFont) ) return &(__PFONT(pFont)->fi->df);
3174 return NULL;
3179 /* X11DRV Interface ****************************************************
3181 * Exposed via the dc->funcs dispatch table. *
3183 ***********************************************************************/
3184 /***********************************************************************
3185 * X11DRV_SelectFont (X11DRV.@)
3187 HFONT X11DRV_SelectFont( X11DRV_PDEVICE *physDev, HFONT hfont )
3189 LOGFONTW logfont;
3190 LOGFONT16 lf;
3191 DC *dc = physDev->dc;
3193 TRACE("dc=%p, hfont=%04x\n", dc, hfont);
3195 if (!GetObjectW( hfont, sizeof(logfont), &logfont )) return GDI_ERROR;
3197 TRACE("dc->gdiFont = %p\n", dc->gdiFont);
3199 if(dc->gdiFont && X11DRV_XRender_Installed) {
3200 X11DRV_XRender_SelectFont(physDev, hfont);
3201 return FALSE;
3204 EnterCriticalSection( &crtsc_fonts_X11 );
3206 if( CHECK_PFONT(physDev->font) )
3207 XFONT_ReleaseCacheEntry( __PFONT(physDev->font) );
3209 FONT_LogFontWTo16(&logfont, &lf);
3211 /* stock fonts ignore the mapping mode */
3212 if (!is_stock_font( hfont ))
3214 /* Make sure we don't change the sign when converting to device coords */
3215 /* FIXME - check that the other drivers do this correctly */
3216 if (lf.lfWidth)
3218 lf.lfWidth = GDI_ROUND((FLOAT)lf.lfWidth * fabs(dc->xformWorld2Vport.eM11));
3219 if (lf.lfWidth == 0)
3220 lf.lfWidth = 1; /* Minimum width */
3222 if (lf.lfHeight)
3224 lf.lfHeight = GDI_ROUND((FLOAT)lf.lfHeight * fabs(dc->xformWorld2Vport.eM22));
3226 if (lf.lfHeight == 0)
3227 lf.lfHeight = MIN_FONT_SIZE;
3231 if (!lf.lfHeight)
3232 lf.lfHeight = -(DEF_POINT_SIZE * GetDeviceCaps(dc->hSelf,LOGPIXELSY) + (72>>1)) / 72;
3235 /* Fixup aliases before passing to RealizeFont */
3236 /* alias = Windows name in the alias table */
3237 LPCSTR alias = XFONT_UnAlias( lf.lfFaceName );
3238 LPCSTR faceMatched;
3239 WORD charsetMatched;
3241 TRACE("hfont=%04x\n", hfont); /* to connect with the trace from RealizeFont */
3242 physDev->font = XFONT_RealizeFont( &lf, &faceMatched,
3243 FALSE, lf.lfCharSet,
3244 &charsetMatched );
3246 /* set face to the requested facename if it matched
3247 * so that GetTextFace can get the correct face name
3249 if (alias && !strcmp(faceMatched, lf.lfFaceName))
3250 MultiByteToWideChar(CP_ACP, 0, alias, -1,
3251 logfont.lfFaceName, LF_FACESIZE);
3252 else
3253 MultiByteToWideChar(CP_ACP, 0, faceMatched, -1,
3254 logfont.lfFaceName, LF_FACESIZE);
3257 * In X, some encodings may have the same lfFaceName.
3258 * for example:
3259 * -misc-fixed-*-iso8859-1
3260 * -misc-fixed-*-jisx0208.1990-0
3261 * so charset should be saved...
3263 logfont.lfCharSet = charsetMatched;
3266 LeaveCriticalSection( &crtsc_fonts_X11 );
3268 return TRUE; /* Use a device font */
3272 /***********************************************************************
3274 * X11DRV_EnumDeviceFonts
3276 BOOL X11DRV_EnumDeviceFonts( HDC hdc, LPLOGFONTW plf,
3277 DEVICEFONTENUMPROC proc, LPARAM lp )
3279 ENUMLOGFONTEXW lf;
3280 NEWTEXTMETRICEXW tm;
3281 fontResource* pfr = fontList;
3282 BOOL b, bRet = 0;
3283 LOGFONT16 lf16;
3286 FONT_LogFontWTo16(plf, &lf16);
3288 if( lf16.lfFaceName[0] )
3290 /* enum all entries in this resource */
3291 pfr = XFONT_FindFIList( pfr, lf16.lfFaceName );
3292 if( pfr )
3294 fontInfo* pfi;
3295 for( pfi = pfr->fi; pfi; pfi = pfi->next )
3297 /* Note: XFONT_GetFontMetric() will have to
3298 release the crit section, font list will
3299 have to be retraversed on return */
3301 if(lf16.lfCharSet == DEFAULT_CHARSET ||
3302 lf16.lfCharSet == pfi->df.dfCharSet) {
3303 if( (b = (*proc)( &lf, &tm,
3304 XFONT_GetFontMetric( pfi, &lf, &tm ), lp )) )
3305 bRet = b;
3306 else break;
3311 else /* enum first entry in each resource */
3312 for( ; pfr ; pfr = pfr->next )
3314 if(pfr->fi)
3316 if( (b = (*proc)( &lf, &tm,
3317 XFONT_GetFontMetric( pfr->fi, &lf, &tm ), lp )) )
3318 bRet = b;
3319 else break;
3322 return bRet;
3326 /***********************************************************************
3327 * X11DRV_GetTextMetrics
3329 BOOL X11DRV_GetTextMetrics(X11DRV_PDEVICE *physDev, TEXTMETRICW *metrics)
3331 TEXTMETRICA tmA;
3333 if( CHECK_PFONT(physDev->font) )
3335 fontObject* pfo = __PFONT(physDev->font);
3336 X11DRV_cptable[pfo->fi->cptable].pGetTextMetricsA( pfo, &tmA );
3337 FONT_TextMetricAToW(&tmA, metrics);
3338 return TRUE;
3340 return FALSE;
3344 /***********************************************************************
3345 * X11DRV_GetCharWidth
3347 BOOL X11DRV_GetCharWidth( X11DRV_PDEVICE *physDev, UINT firstChar, UINT lastChar,
3348 LPINT buffer )
3350 fontObject* pfo = XFONT_GetFontObject( physDev->font );
3352 if( pfo )
3354 int i;
3356 if (pfo->fs->per_char == NULL)
3357 for (i = firstChar; i <= lastChar; i++)
3358 if(pfo->lpX11Trans)
3359 *buffer++ = pfo->fs->min_bounds.attributes *
3360 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3361 else
3362 *buffer++ = pfo->fs->min_bounds.width * pfo->rescale;
3363 else
3365 XCharStruct *cs, *def;
3366 static XCharStruct __null_char = { 0, 0, 0, 0, 0, 0 };
3368 CI_GET_CHAR_INFO(pfo->fs, pfo->fs->default_char, &__null_char,
3369 def);
3371 for (i = firstChar; i <= lastChar; i++)
3373 WCHAR wch = i;
3374 BYTE ch;
3375 UINT ch_f; /* character code in the font encoding */
3376 WideCharToMultiByte( pfo->fi->codepage, 0, &wch, 1, &ch, 1, NULL, NULL );
3377 ch_f = ch;
3378 if (ch_f >= pfo->fs->min_char_or_byte2 &&
3379 ch_f <= pfo->fs->max_char_or_byte2)
3381 cs = &pfo->fs->per_char[(ch_f - pfo->fs->min_char_or_byte2)];
3382 if (CI_NONEXISTCHAR(cs)) cs = def;
3383 } else cs = def;
3384 if(pfo->lpX11Trans)
3385 *buffer++ = max(cs->attributes, 0) *
3386 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3387 else
3388 *buffer++ = max(cs->width, 0 ) * pfo->rescale;
3392 return TRUE;
3394 return FALSE;