Removed some unnecessary includes.
[wine.git] / graphics / x11drv / xfont.c
blob6b690404063d1155877d247c1d8a9f4593e265b6
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 <sys/stat.h>
23 #include <fcntl.h>
24 #include <math.h>
25 #include <assert.h>
27 #include "windef.h"
28 #include "wingdi.h"
29 #include "winnls.h"
30 #include "heap.h"
31 #include "options.h"
32 #include "font.h"
33 #include "debugtools.h"
34 #include "tweak.h"
35 #include "x11font.h"
36 #include "server.h"
38 DEFAULT_DEBUG_CHANNEL(font);
40 #define X_PFONT_MAGIC (0xFADE0000)
41 #define X_FMC_MAGIC (0x0000CAFE)
43 #define MAX_FONTS 1024*16
44 #define MAX_LFD_LENGTH 256
45 #define TILDE '~'
46 #define HYPHEN '-'
48 #define DEF_POINT_SIZE 8 /* CreateFont(0 .. ) gets this */
49 #define DEF_SCALABLE_HEIGHT 100 /* pixels */
50 #define MIN_FONT_SIZE 2 /* Min size in pixels */
51 #define MAX_FONT_SIZE 1000 /* Max size in pixels */
53 #define REMOVE_SUBSETS 1
54 #define UNMARK_SUBSETS 0
57 #define FF_FAMILY (FF_MODERN | FF_SWISS | FF_ROMAN | FF_DECORATIVE | FF_SCRIPT)
59 typedef struct __fontAlias
61 LPSTR faTypeFace;
62 LPSTR faAlias;
63 struct __fontAlias* next;
64 } fontAlias;
66 static fontAlias *aliasTable = NULL;
68 UINT16 XTextCaps = TC_OP_CHARACTER | TC_OP_STROKE |
69 TC_CP_STROKE | TC_CR_ANY |
70 TC_SA_DOUBLE | TC_SA_INTEGER | TC_SA_CONTIN |
71 TC_UA_ABLE | TC_SO_ABLE | TC_RA_ABLE;
73 /* X11R6 adds TC_SF_X_YINDEP, maybe more... */
75 static const char* INIFontMetrics = "/cachedmetrics.";
76 static const char* INIFontSection = "fonts";
77 static const char* INIAliasSection = "Alias";
78 static const char* INIIgnoreSection = "Ignore";
79 static const char* INIDefault = "Default";
80 static const char* INIDefaultFixed = "DefaultFixed";
81 static const char* INIResolution = "Resolution";
82 static const char* INIGlobalMetrics = "FontMetrics";
83 static const char* INIDefaultSerif = "DefaultSerif";
84 static const char* INIDefaultSansSerif = "DefaultSansSerif";
87 /* FIXME - are there any more Latin charsets ? */
88 /* FIXME - RUSSIAN, ARABIC, GREEK, HEBREW are NOT Latin */
89 #define IS_LATIN_CHARSET(ch) \
90 ((ch)==ANSI_CHARSET ||\
91 (ch)==EE_CHARSET ||\
92 (ch)==ISO3_CHARSET ||\
93 (ch)==ISO4_CHARSET ||\
94 (ch)==RUSSIAN_CHARSET ||\
95 (ch)==ARABIC_CHARSET ||\
96 (ch)==GREEK_CHARSET ||\
97 (ch)==HEBREW_CHARSET ||\
98 (ch)==TURKISH_CHARSET ||\
99 (ch)==ISO10_CHARSET ||\
100 (ch)==BALTIC_CHARSET ||\
101 (ch)==CELTIC_CHARSET)
103 /* suffix-charset mapping tables - must be less than 254 entries long */
105 typedef struct __sufch
107 LPCSTR psuffix;
108 WORD charset; /* hibyte != 0 means *internal* charset */
109 WORD codepage;
110 WORD cptable;
111 } SuffixCharset;
113 static const SuffixCharset sufch_ansi[] = {
114 { "0", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
115 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
117 static const SuffixCharset sufch_iso646[] = {
118 { "irv", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
119 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
121 static const SuffixCharset sufch_iso8859[] = {
122 { "1", ANSI_CHARSET, 28591, X11DRV_CPTABLE_SBCS },
123 { "2", EE_CHARSET, 28592, X11DRV_CPTABLE_SBCS },
124 { "3", ISO3_CHARSET, 28593, X11DRV_CPTABLE_SBCS },
125 { "4", ISO4_CHARSET, 28594, X11DRV_CPTABLE_SBCS },
126 { "5", RUSSIAN_CHARSET, 28595, X11DRV_CPTABLE_SBCS },
127 { "6", ARABIC_CHARSET, 28596, X11DRV_CPTABLE_SBCS },
128 { "7", GREEK_CHARSET, 28597, X11DRV_CPTABLE_SBCS },
129 { "8", HEBREW_CHARSET, 28598, X11DRV_CPTABLE_SBCS },
130 { "9", TURKISH_CHARSET, 28599, X11DRV_CPTABLE_SBCS },
131 { "10", ISO10_CHARSET, 28600, X11DRV_CPTABLE_SBCS },
132 { "11", THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }, /* FIXME */
133 { "12", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
134 { "13", BALTIC_CHARSET, 28603, X11DRV_CPTABLE_SBCS },
135 { "14", CELTIC_CHARSET, 28604, X11DRV_CPTABLE_SBCS },
136 { "15", ANSI_CHARSET, 28605, X11DRV_CPTABLE_SBCS },
137 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
139 static const SuffixCharset sufch_microsoft[] = {
140 { "cp1250", EE_CHARSET, 1250, X11DRV_CPTABLE_SBCS },
141 { "cp1251", RUSSIAN_CHARSET, 1251, X11DRV_CPTABLE_SBCS },
142 { "cp1252", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
143 { "cp1253", GREEK_CHARSET, 1253, X11DRV_CPTABLE_SBCS },
144 { "cp1254", TURKISH_CHARSET, 1254, X11DRV_CPTABLE_SBCS },
145 { "cp1255", HEBREW_CHARSET, 1255, X11DRV_CPTABLE_SBCS },
146 { "cp1256", ARABIC_CHARSET, 1256, X11DRV_CPTABLE_SBCS },
147 { "cp1257", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS },
148 { "fontspecific", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
149 { "symbol", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
150 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
152 static const SuffixCharset sufch_tcvn[] = {
153 { "0", TCVN_CHARSET, 1252, X11DRV_CPTABLE_SBCS }, /* FIXME */
154 { NULL, TCVN_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
156 static const SuffixCharset sufch_tis620[] = {
157 { "0", THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }, /* FIXME */
158 { NULL, THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }};
160 static const SuffixCharset sufch_viscii[] = {
161 { "1", VISCII_CHARSET, 1252, X11DRV_CPTABLE_SBCS }, /* FIXME */
162 { NULL, VISCII_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
164 static const SuffixCharset sufch_windows[] = {
165 { "1250", EE_CHARSET, 1250, X11DRV_CPTABLE_SBCS },
166 { "1251", RUSSIAN_CHARSET, 1251, X11DRV_CPTABLE_SBCS },
167 { "1252", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
168 { "1253", GREEK_CHARSET, 1253, X11DRV_CPTABLE_SBCS },
169 { "1254", TURKISH_CHARSET, 1254, X11DRV_CPTABLE_SBCS },
170 { "1255", HEBREW_CHARSET, 1255, X11DRV_CPTABLE_SBCS },
171 { "1256", ARABIC_CHARSET, 1256, X11DRV_CPTABLE_SBCS },
172 { "1257", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS },
173 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
175 static const SuffixCharset sufch_koi8[] = {
176 { "r", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
177 { "ru", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
178 { "u", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
179 { NULL, RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS }};
181 static const SuffixCharset sufch_jisx0201[] = {
182 { "0", X11FONT_JISX0201_CHARSET, 932, X11DRV_CPTABLE_SBCS },
183 { NULL, X11FONT_JISX0201_CHARSET, 932, X11DRV_CPTABLE_SBCS }};
185 static const SuffixCharset sufch_jisx0208[] = {
186 { "0", SHIFTJIS_CHARSET, 932, X11DRV_CPTABLE_CP932 },
187 { NULL, SHIFTJIS_CHARSET, 932, X11DRV_CPTABLE_CP932 }};
189 static const SuffixCharset sufch_jisx0212[] = {
190 { "0", X11FONT_JISX0212_CHARSET, 932, X11DRV_CPTABLE_CP932 },
191 { NULL, X11FONT_JISX0212_CHARSET, 932, X11DRV_CPTABLE_CP932 }};
193 static const SuffixCharset sufch_ksc5601[] = {
194 { "0", HANGEUL_CHARSET, 949, X11DRV_CPTABLE_CP949 },
195 { NULL, HANGEUL_CHARSET, 949, X11DRV_CPTABLE_CP949 }};
197 static const SuffixCharset sufch_gb2312[] = {
198 { "0", GB2312_CHARSET, 936, X11DRV_CPTABLE_CP936 },
199 { NULL, GB2312_CHARSET, 936, X11DRV_CPTABLE_CP936 }};
201 static const SuffixCharset sufch_big5[] = {
202 { "0", CHINESEBIG5_CHARSET, 950, X11DRV_CPTABLE_CP950 },
203 { NULL, CHINESEBIG5_CHARSET, 950, X11DRV_CPTABLE_CP950 }};
205 static const SuffixCharset sufch_unicode[] = {
206 { "0", DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE },
207 { NULL, DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE }};
209 static const SuffixCharset sufch_iso10646[] = {
210 { "1", DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE },
211 { NULL, DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE }};
213 /* Each of these must be matched explicitly */
214 static const SuffixCharset sufch_any[] = {
215 { "fontspecific", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
216 { NULL, 0, 0, X11DRV_CPTABLE_SBCS }};
219 typedef struct __fet
221 LPSTR prefix;
222 const SuffixCharset* sufch;
223 struct __fet* next;
224 } fontEncodingTemplate;
226 /* Note: we can attach additional encoding mappings to the end
227 * of this table at runtime.
229 static fontEncodingTemplate __fETTable[] = {
230 { "ansi", sufch_ansi, &__fETTable[1] },
231 { "ascii", sufch_ansi, &__fETTable[2] },
232 { "iso646.1991", sufch_iso646, &__fETTable[3] },
233 { "iso8859", sufch_iso8859, &__fETTable[4] },
234 { "microsoft", sufch_microsoft, &__fETTable[5] },
235 { "tcvn", sufch_tcvn, &__fETTable[6] },
236 { "tis620.2533", sufch_tis620, &__fETTable[7] },
237 { "viscii1.1", sufch_viscii, &__fETTable[8] },
238 { "windows", sufch_windows, &__fETTable[9] },
239 { "koi8", sufch_koi8, &__fETTable[10]},
240 { "jisx0201.1976",sufch_jisx0201, &__fETTable[11]},
241 { "jisc6226.1978",sufch_jisx0208, &__fETTable[12]},
242 { "jisx0208.1983",sufch_jisx0208, &__fETTable[13]},
243 { "jisx0208.1990",sufch_jisx0208, &__fETTable[14]},
244 { "jisx0212.1990",sufch_jisx0212, &__fETTable[15]},
245 { "ksc5601.1987", sufch_ksc5601, &__fETTable[16]},
246 { "gb2312.1980", sufch_gb2312, &__fETTable[17]},
247 { "big5.et", sufch_big5, &__fETTable[18]},
248 { "unicode", sufch_unicode, &__fETTable[19]},
249 { "iso10646", sufch_iso10646, &__fETTable[20]},
250 { "cp", sufch_windows, &__fETTable[21]},
251 /* NULL prefix matches anything so put it last */
252 { NULL, sufch_any, NULL },
254 static fontEncodingTemplate* fETTable = __fETTable;
256 /* a charset database for known facenames */
257 struct CharsetBindingInfo
259 const char* pszFaceName;
260 BYTE charset;
262 static const struct CharsetBindingInfo charsetbindings[] =
264 /* special facenames */
265 { "System", DEFAULT_CHARSET },
266 { "FixedSys", DEFAULT_CHARSET },
268 /* known facenames */
269 { "MS Serif", ANSI_CHARSET },
270 { "MS Sans Serif", ANSI_CHARSET },
271 { "Courier", ANSI_CHARSET },
272 { "Symbol", SYMBOL_CHARSET },
274 { "Arial", ANSI_CHARSET },
275 { "Arial Greek", GREEK_CHARSET },
276 { "Arial Tur", TURKISH_CHARSET },
277 { "Arial Baltic", BALTIC_CHARSET },
278 { "Arial CE", EASTEUROPE_CHARSET },
279 { "Arial Cyr", RUSSIAN_CHARSET },
280 { "Courier New", ANSI_CHARSET },
281 { "Courier New Greek", GREEK_CHARSET },
282 { "Courier New Tur", TURKISH_CHARSET },
283 { "Courier New Baltic", BALTIC_CHARSET },
284 { "Courier New CE", EASTEUROPE_CHARSET },
285 { "Courier New Cyr", RUSSIAN_CHARSET },
286 { "Times New Roman", ANSI_CHARSET },
287 { "Times New Roman Greek", GREEK_CHARSET },
288 { "Times New Roman Tur", TURKISH_CHARSET },
289 { "Times New Roman Baltic", BALTIC_CHARSET },
290 { "Times New Roman CE", EASTEUROPE_CHARSET },
291 { "Times New Roman Cyr", RUSSIAN_CHARSET },
293 { "\x82\x6c\x82\x72 \x83\x53\x83\x56\x83\x62\x83\x4e",
294 SHIFTJIS_CHARSET }, /* MS gothic */
295 { "\x82\x6c\x82\x72 \x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e",
296 SHIFTJIS_CHARSET }, /* MS P gothic */
297 { "\x82\x6c\x82\x72 \x96\xbe\x92\xa9",
298 SHIFTJIS_CHARSET }, /* MS mincho */
299 { "\x82\x6c\x82\x72 \x82\x6f\x96\xbe\x92\xa9",
300 SHIFTJIS_CHARSET }, /* MS P mincho */
301 { "GulimChe", HANGEUL_CHARSET },
302 { "MS Song", GB2312_CHARSET },
303 { "MS Hei", GB2312_CHARSET },
305 { NULL, 0 }
309 static int DefResolution = 0;
311 static CRITICAL_SECTION crtsc_fonts_X11 = CRITICAL_SECTION_INIT;
313 static fontResource* fontList = NULL;
314 static fontObject* fontCache = NULL; /* array */
315 static int fontCacheSize = FONTCACHE;
316 static int fontLF = -1, fontMRU = -1; /* last free, most recently used */
318 #define __PFONT(pFont) ( fontCache + ((UINT)(pFont) & 0x0000FFFF) )
319 #define CHECK_PFONT(pFont) ( (((UINT)(pFont) & 0xFFFF0000) == X_PFONT_MAGIC) &&\
320 (((UINT)(pFont) & 0x0000FFFF) < fontCacheSize) )
322 static Atom RAW_ASCENT;
323 static Atom RAW_DESCENT;
325 /***********************************************************************
326 * Helper macros from X distribution
329 #define CI_NONEXISTCHAR(cs) (((cs)->width == 0) && \
330 (((cs)->rbearing|(cs)->lbearing| \
331 (cs)->ascent|(cs)->descent) == 0))
333 #define CI_GET_CHAR_INFO(fs,col,def,cs) \
335 cs = def; \
336 if (col >= fs->min_char_or_byte2 && col <= fs->max_char_or_byte2) { \
337 if (fs->per_char == NULL) { \
338 cs = &fs->min_bounds; \
339 } else { \
340 cs = &fs->per_char[(col - fs->min_char_or_byte2)]; \
341 if (CI_NONEXISTCHAR(cs)) cs = def; \
346 #define CI_GET_DEFAULT_INFO(fs,cs) \
347 CI_GET_CHAR_INFO(fs, fs->default_char, NULL, cs)
349 /***********************************************************************
350 * Checksums
352 static UINT16 __lfCheckSum( LPLOGFONT16 plf )
354 CHAR font[LF_FACESIZE];
355 UINT16 checksum = 0;
356 UINT16 *ptr;
357 int i;
359 ptr = (UINT16 *)plf;
360 for (i = 0; i < 9; i++) checksum ^= *ptr++;
361 for (i = 0; i < LF_FACESIZE; i++)
363 font[i] = tolower(plf->lfFaceName[i]);
364 if (!font[i] || font[i] == ' ') break;
366 for (ptr = (UINT16 *)font, i >>= 1; i > 0; i-- ) checksum ^= *ptr++;
367 return checksum;
370 static UINT16 __genericCheckSum( const void *ptr, int size )
372 unsigned int checksum = 0;
373 const char *p = (const char *)ptr;
374 while (size-- > 0)
375 checksum ^= (checksum << 3) + (checksum >> 29) + *p++;
377 return checksum & 0xffff;
380 /*************************************************************************
381 * LFD parse/compose routines
383 * NB. LFD_Parse will use lpFont for its own ends, so if you want to keep it
384 * make a copy first
386 * These functions also do TILDE to HYPHEN conversion
388 static LFD* LFD_Parse(LPSTR lpFont)
390 LFD* lfd;
391 char *lpch = lpFont, *lfd_fld[LFD_FIELDS], *field_start;
392 int i;
393 if (*lpch != HYPHEN)
395 WARN("font '%s' doesn't begin with '%c'\n", lpFont, HYPHEN);
396 return NULL;
399 field_start = ++lpch;
400 for( i = 0; i < LFD_FIELDS; )
402 if (*lpch == HYPHEN)
404 *lpch = '\0';
405 lfd_fld[i] = field_start;
406 i++;
407 field_start = ++lpch;
409 else if (!*lpch)
411 lfd_fld[i] = field_start;
412 i++;
413 break;
415 else if (*lpch == TILDE)
417 *lpch = HYPHEN;
418 ++lpch;
420 else
421 ++lpch;
423 /* Fill in the empty fields with NULLS */
424 for (; i< LFD_FIELDS; i++)
425 lfd_fld[i] = NULL;
426 if (*lpch)
427 WARN("Extra ignored in font '%s'\n", lpFont);
429 lfd = HeapAlloc( GetProcessHeap(), 0, sizeof(LFD) );
430 if (lfd)
432 lfd->foundry = lfd_fld[0];
433 lfd->family = lfd_fld[1];
434 lfd->weight = lfd_fld[2];
435 lfd->slant = lfd_fld[3];
436 lfd->set_width = lfd_fld[4];
437 lfd->add_style = lfd_fld[5];
438 lfd->pixel_size = lfd_fld[6];
439 lfd->point_size = lfd_fld[7];
440 lfd->resolution_x = lfd_fld[8];
441 lfd->resolution_y = lfd_fld[9];
442 lfd->spacing = lfd_fld[10];
443 lfd->average_width = lfd_fld[11];
444 lfd->charset_registry = lfd_fld[12];
445 lfd->charset_encoding = lfd_fld[13];
447 return lfd;
451 static void LFD_UnParse(LPSTR dp, UINT buf_size, LFD* lfd)
453 const char* lfd_fld[LFD_FIELDS];
454 int i;
456 if (!buf_size)
457 return; /* Dont be silly */
459 lfd_fld[0] = lfd->foundry;
460 lfd_fld[1] = lfd->family;
461 lfd_fld[2] = lfd->weight ;
462 lfd_fld[3] = lfd->slant ;
463 lfd_fld[4] = lfd->set_width ;
464 lfd_fld[5] = lfd->add_style;
465 lfd_fld[6] = lfd->pixel_size;
466 lfd_fld[7] = lfd->point_size;
467 lfd_fld[8] = lfd->resolution_x;
468 lfd_fld[9] = lfd->resolution_y ;
469 lfd_fld[10] = lfd->spacing ;
470 lfd_fld[11] = lfd->average_width ;
471 lfd_fld[12] = lfd->charset_registry ;
472 lfd_fld[13] = lfd->charset_encoding ;
474 buf_size--; /* Room for the terminator */
476 for (i = 0; i < LFD_FIELDS; i++)
478 const char* sp = lfd_fld[i];
479 if (!sp || !buf_size)
480 break;
482 *dp++ = HYPHEN;
483 buf_size--;
484 while (buf_size > 0 && *sp)
486 *dp = (*sp == HYPHEN) ? TILDE : *sp;
487 buf_size--;
488 dp++; sp++;
491 *dp = '\0';
495 static void LFD_GetWeight( fontInfo* fi, LPCSTR lpStr)
497 int j = strlen(lpStr);
498 if( j == 1 && *lpStr == '0')
499 fi->fi_flags |= FI_POLYWEIGHT;
500 else if( j == 4 )
502 if( !strcasecmp( "bold", lpStr) )
503 fi->df.dfWeight = FW_BOLD;
504 else if( !strcasecmp( "demi", lpStr) )
506 fi->fi_flags |= FI_FW_DEMI;
507 fi->df.dfWeight = FW_DEMIBOLD;
509 else if( !strcasecmp( "book", lpStr) )
511 fi->fi_flags |= FI_FW_BOOK;
512 fi->df.dfWeight = FW_REGULAR;
515 else if( j == 5 )
517 if( !strcasecmp( "light", lpStr) )
518 fi->df.dfWeight = FW_LIGHT;
519 else if( !strcasecmp( "black", lpStr) )
520 fi->df.dfWeight = FW_BLACK;
522 else if( j == 6 && !strcasecmp( "medium", lpStr) )
523 fi->df.dfWeight = FW_REGULAR;
524 else if( j == 8 && !strcasecmp( "demibold", lpStr) )
525 fi->df.dfWeight = FW_DEMIBOLD;
526 else
527 fi->df.dfWeight = FW_DONTCARE; /* FIXME: try to get something
528 * from the weight property */
531 static BOOL LFD_GetSlant( fontInfo* fi, LPCSTR lpStr)
533 int l = strlen(lpStr);
534 if( l == 1 )
536 switch( tolower( *lpStr ) )
538 case '0': fi->fi_flags |= FI_POLYSLANT; /* haven't seen this one yet */
539 default:
540 case 'r': fi->df.dfItalic = 0;
541 break;
542 case 'o':
543 fi->fi_flags |= FI_OBLIQUE;
544 case 'i': fi->df.dfItalic = 1;
545 break;
547 return FALSE;
549 return TRUE;
552 static void LFD_GetStyle( fontInfo* fi, LPCSTR lpstr, int dec_style_check)
554 int j = strlen(lpstr);
555 if( j > 3 ) /* find out is there "sans" or "script" */
557 j = 0;
559 if( strstr(lpstr, "sans") )
561 fi->df.dfPitchAndFamily |= FF_SWISS;
562 j = 1;
564 if( strstr(lpstr, "script") )
566 fi->df.dfPitchAndFamily |= FF_SCRIPT;
567 j = 1;
569 if( !j && dec_style_check )
570 fi->df.dfPitchAndFamily |= FF_DECORATIVE;
574 /*************************************************************************
575 * LFD_InitFontInfo
577 * INIT ONLY
579 * Fill in some fields in the fontInfo struct.
581 static int LFD_InitFontInfo( fontInfo* fi, const LFD* lfd, LPCSTR fullname )
583 int i, j, dec_style_check, scalability;
584 fontEncodingTemplate* boba;
585 const char* ridiculous = "font '%s' has ridiculous %s\n";
586 const char* lpstr;
588 if (!lfd->charset_registry)
590 WARN("font '%s' does not have enough fields\n", fullname);
591 return FALSE;
594 memset(fi, 0, sizeof(fontInfo) );
596 /* weight name - */
597 LFD_GetWeight( fi, lfd->weight);
599 /* slant - */
600 dec_style_check = LFD_GetSlant( fi, lfd->slant);
602 /* width name - */
603 lpstr = lfd->set_width;
604 if( strcasecmp( "normal", lpstr) ) /* XXX 'narrow', 'condensed', etc... */
605 dec_style_check = TRUE;
606 else
607 fi->fi_flags |= FI_NORMAL;
609 /* style - */
610 LFD_GetStyle(fi, lfd->add_style, dec_style_check);
612 /* pixel & decipoint height, and res_x & y */
614 scalability = 0;
616 j = strlen(lfd->pixel_size);
617 if( j == 0 || j > 3 )
619 WARN(ridiculous, fullname, "pixel_size");
620 return FALSE;
622 if( !(fi->lfd_height = atoi(lfd->pixel_size)) )
623 scalability++;
625 j = strlen(lfd->point_size);
626 if( j == 0 || j > 3 )
628 WARN(ridiculous, fullname, "point_size");
629 return FALSE;
631 if( !(atoi(lfd->point_size)) )
632 scalability++;
634 j = strlen(lfd->resolution_x);
635 if( j == 0 || j > 3 )
637 WARN(ridiculous, fullname, "resolution_x");
638 return FALSE;
640 if( !(fi->lfd_resolution = atoi(lfd->resolution_x)) )
641 scalability++;
643 j = strlen(lfd->resolution_y);
644 if( j == 0 || j > 3 )
646 WARN(ridiculous, fullname, "resolution_y");
647 return FALSE;
649 if( !(atoi(lfd->resolution_y)) )
650 scalability++;
652 /* Check scalability */
653 switch (scalability)
655 case 0: /* Bitmap */
656 break;
657 case 4: /* Scalable */
658 fi->fi_flags |= FI_SCALABLE;
659 break;
660 case 2:
661 /* #$%^!!! X11R6 mutant garbage (scalable bitmap) */
662 TRACE("Skipping scalable bitmap '%s'\n", fullname);
663 return FALSE;
664 default:
665 WARN("Font '%s' has weird scalability\n", fullname);
666 return FALSE;
669 /* spacing - */
670 lpstr = lfd->spacing;
671 switch( *lpstr )
673 case '\0':
674 WARN("font '%s' has no spacing\n", fullname);
675 return FALSE;
677 case 'p': fi->fi_flags |= FI_VARIABLEPITCH;
678 break;
679 case 'c': fi->df.dfPitchAndFamily |= FF_MODERN;
680 fi->fi_flags |= FI_FIXEDEX;
681 /* fall through */
682 case 'm': fi->fi_flags |= FI_FIXEDPITCH;
683 break;
684 default:
685 /* Of course this line does nothing */
686 fi->df.dfPitchAndFamily |= DEFAULT_PITCH | FF_DONTCARE;
689 /* average width - */
690 lpstr = lfd->average_width;
691 j = strlen(lpstr);
692 if( j == 0 || j > 3 )
694 WARN(ridiculous, fullname, "average_width");
695 return FALSE;
698 if( !(atoi(lpstr)) && !scalability )
700 WARN("font '%s' has average_width 0 but is not scalable!!\n", fullname);
701 return FALSE;
704 /* charset registry, charset encoding - */
705 lpstr = lfd->charset_registry;
706 if( strstr(lpstr, "ksc") ||
707 strstr(lpstr, "gb2312") ||
708 strstr(lpstr, "big5") )
710 FIXME("DBCS fonts like '%s' are not working correctly now.\n", fullname);
713 fi->df.dfCharSet = ANSI_CHARSET;
715 for( i = 0, boba = fETTable; boba; boba = boba->next, i++ )
717 if (!boba->prefix || !strcasecmp(lpstr, boba->prefix))
719 if (lfd->charset_encoding)
721 for( j = 0; boba->sufch[j].psuffix; j++ )
723 if( !strcasecmp(lfd->charset_encoding, boba->sufch[j].psuffix ))
725 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
726 fi->internal_charset = boba->sufch[j].charset;
727 fi->codepage = boba->sufch[j].codepage;
728 fi->cptable = boba->sufch[j].cptable;
729 goto done;
733 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
734 fi->internal_charset = boba->sufch[j].charset;
735 fi->codepage = boba->sufch[j].codepage;
736 fi->cptable = boba->sufch[j].cptable;
737 if (boba->prefix)
739 FIXME("font '%s' has unknown character encoding '%s' in known registry '%s'\n",
740 fullname, lfd->charset_encoding, boba->prefix);
741 j = 254;
743 else
745 FIXME("font '%s' has unknown registry '%s' and character encoding '%s' \n",
746 fullname, lfd->charset_registry, lfd->charset_encoding);
747 j = 255;
750 WARN("Defaulting to: df.dfCharSet = %d, internal_charset = %d, codepage = %d, cptable = %d\n",
751 fi->df.dfCharSet,fi->internal_charset, fi->codepage, fi->cptable);
752 goto done;
754 else if (boba->prefix)
756 WARN("font '%s' has known registry '%s' and no character encoding\n",
757 fullname, lpstr);
758 for( j = 0; boba->sufch[j].psuffix; j++ )
760 fi->df.dfCharSet = (BYTE)(boba->sufch[j].charset & 0xff);
761 fi->internal_charset = boba->sufch[j].charset;
762 fi->codepage = boba->sufch[j].codepage;
763 fi->cptable = boba->sufch[j].cptable;
764 j = 255;
765 goto done;
769 WARN("font '%s' has unknown character set '%s'\n", fullname, lpstr);
770 return FALSE;
772 done:
773 /* i - index into fETTable
774 * j - index into suffix array for fETTable[i]
775 * except:
776 * 254 - found encoding prefix, unknown suffix
777 * 255 - no encoding match at all.
779 fi->fi_encoding = 256 * (UINT16)i + (UINT16)j;
781 return TRUE;
785 /*************************************************************************
786 * LFD_AngleMatrix
788 * make a matrix suitable for LFD based on theta radians
790 static void LFD_AngleMatrix( char* buffer, int h, double theta)
792 double matrix[4];
793 matrix[0] = h*cos(theta);
794 matrix[1] = h*sin(theta);
795 matrix[2] = -h*sin(theta);
796 matrix[3] = h*cos(theta);
797 sprintf(buffer, "[%+f%+f%+f%+f]", matrix[0], matrix[1], matrix[2], matrix[3]);
800 /*************************************************************************
801 * LFD_ComposeLFD
803 * Note: uRelax is a treatment not a cure. Font mapping algorithm
804 * should be bulletproof enough to allow us to avoid hacks like
805 * this despite LFD being so braindead.
807 static BOOL LFD_ComposeLFD( const fontObject* fo,
808 INT height, LPSTR lpLFD, UINT uRelax )
810 int i, h;
811 char *any = "*";
812 char h_string[64], resx_string[64], resy_string[64];
813 LFD aLFD;
815 /* Get the worst case over with first */
817 /* RealizeFont() will call us repeatedly with increasing uRelax
818 * until XLoadFont() succeeds.
819 * to avoid an infinite loop; these will always match
821 if (uRelax >= 5)
823 if (uRelax == 5)
824 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-1" );
825 else
826 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" );
827 return TRUE;
830 /* read foundry + family from fo */
831 aLFD.foundry = fo->fr->resource->foundry;
832 aLFD.family = fo->fr->resource->family;
834 /* add weight */
835 switch( fo->fi->df.dfWeight )
837 case FW_BOLD:
838 aLFD.weight = "bold"; break;
839 case FW_REGULAR:
840 if( fo->fi->fi_flags & FI_FW_BOOK )
841 aLFD.weight = "book";
842 else
843 aLFD.weight = "medium";
844 break;
845 case FW_DEMIBOLD:
846 aLFD.weight = "demi";
847 if( !( fo->fi->fi_flags & FI_FW_DEMI) )
848 aLFD.weight = "bold";
849 break;
850 case FW_BLACK:
851 aLFD.weight = "black"; break;
852 case FW_LIGHT:
853 aLFD.weight = "light"; break;
854 default:
855 aLFD.weight = any;
858 /* add slant */
859 if( fo->fi->df.dfItalic )
860 if( fo->fi->fi_flags & FI_OBLIQUE )
861 aLFD.slant = "o";
862 else
863 aLFD.slant = "i";
864 else
865 aLFD.slant = (uRelax < 1) ? "r" : any;
867 /* add width */
868 if( fo->fi->fi_flags & FI_NORMAL )
869 aLFD.set_width = "normal";
870 else
871 aLFD.set_width = any;
873 /* ignore style */
874 aLFD.add_style = any;
876 /* add pixelheight
878 * FIXME: fill in lpXForm and lpPixmap for rotated fonts
880 if( fo->fo_flags & FO_SYNTH_HEIGHT )
881 h = fo->fi->lfd_height;
882 else
884 h = (fo->fi->lfd_height * height + (fo->fi->df.dfPixHeight>>1));
885 h /= fo->fi->df.dfPixHeight;
887 if (h < MIN_FONT_SIZE) /* Resist rounding down to 0 */
888 h = MIN_FONT_SIZE;
889 else if (h > MAX_FONT_SIZE) /* Sanity check as huge fonts can lock up the font server */
891 WARN("Huge font size %d pixels has been reduced to %d\n", h, MAX_FONT_SIZE);
892 h = MAX_FONT_SIZE;
895 if (uRelax <= 2)
896 /* handle rotated fonts */
897 if (fo->lf.lfEscapement) {
898 /* escapement is in tenths of degrees, theta is in radians */
899 double theta = M_PI*fo->lf.lfEscapement/1800.;
900 LFD_AngleMatrix(h_string, h, theta);
902 else
904 sprintf(h_string, "%d", h);
906 else
907 sprintf(h_string, "%d", fo->fi->lfd_height);
909 aLFD.pixel_size = h_string;
910 aLFD.point_size = any;
912 /* resolution_x,y, average width */
913 /* FOX ME - Why do some font servers ignore average width ?
914 * so that you have to mess around with res_y
916 aLFD.average_width = any;
917 if (uRelax <= 3)
919 sprintf(resx_string, "%d", fo->fi->lfd_resolution);
920 aLFD.resolution_x = resx_string;
922 strcpy(resy_string, resx_string);
923 if( uRelax == 0 && XTextCaps & TC_SF_X_YINDEP )
925 if( fo->lf.lfWidth && !(fo->fo_flags & FO_SYNTH_WIDTH))
927 int resy = 0.5 + fo->fi->lfd_resolution *
928 (fo->fi->df.dfAvgWidth * height) /
929 (fo->lf.lfWidth * fo->fi->df.dfPixHeight) ;
930 sprintf(resy_string, "%d", resy);
932 else
934 /* FIXME - synth width */
937 aLFD.resolution_y = resy_string;
939 else
941 aLFD.resolution_x = aLFD.resolution_y = any;
944 /* spacing */
946 char* w;
948 if( fo->fi->fi_flags & FI_FIXEDPITCH )
949 w = ( fo->fi->fi_flags & FI_FIXEDEX ) ? "c" : "m";
950 else
951 w = ( fo->fi->fi_flags & FI_VARIABLEPITCH ) ? "p" : any;
953 aLFD.spacing = (uRelax <= 1) ? w : any;
956 /* encoding */
958 if (uRelax <= 4)
960 fontEncodingTemplate* boba;
962 i = fo->fi->fi_encoding >> 8;
963 for( boba = fETTable; i; i--, boba = boba->next );
965 aLFD.charset_registry = boba->prefix ? boba->prefix : any;
967 i = fo->fi->fi_encoding & 255;
968 switch( i )
970 default:
971 aLFD.charset_encoding = boba->sufch[i].psuffix;
972 break;
974 case 254:
975 aLFD.charset_encoding = any;
976 break;
978 case 255: /* no suffix - it ends eg "-ascii" */
979 aLFD.charset_encoding = NULL;
980 break;
983 else
985 aLFD.charset_registry = any;
986 aLFD.charset_encoding = any;
989 LFD_UnParse(lpLFD, MAX_LFD_LENGTH, &aLFD);
991 TRACE("\tLFD(uRelax=%d): %s\n", uRelax, lpLFD );
992 return TRUE;
996 /***********************************************************************
997 * X Font Resources
999 * font info - http://www.microsoft.com/kb/articles/q65/1/23.htm
1000 * Windows font metrics - http://www.microsoft.com/kb/articles/q32/6/67.htm
1002 static void XFONT_GetLeading( const LPIFONTINFO16 pFI, const XFontStruct* x_fs,
1003 INT16* pIL, INT16* pEL, const XFONTTRANS *XFT )
1005 unsigned long height;
1006 unsigned min = (unsigned char)pFI->dfFirstChar;
1007 BOOL bIsLatin = IS_LATIN_CHARSET(pFI->dfCharSet);
1009 if( pEL ) *pEL = 0;
1011 if(XFT) {
1012 Atom RAW_CAP_HEIGHT = TSXInternAtom(display, "RAW_CAP_HEIGHT", TRUE);
1013 if(TSXGetFontProperty((XFontStruct*)x_fs, RAW_CAP_HEIGHT, &height))
1014 *pIL = XFT->ascent -
1015 (INT)(XFT->pixelsize / 1000.0 * height);
1016 else
1017 *pIL = 0;
1018 return;
1021 if( TSXGetFontProperty((XFontStruct*)x_fs, XA_CAP_HEIGHT, &height) == FALSE )
1023 if( x_fs->per_char )
1024 if( bIsLatin )
1025 height = x_fs->per_char['X' - min].ascent;
1026 else
1027 if (x_fs->ascent >= x_fs->max_bounds.ascent)
1028 height = x_fs->max_bounds.ascent;
1029 else
1031 height = x_fs->ascent;
1032 if( pEL )
1033 *pEL = x_fs->max_bounds.ascent - height;
1035 else
1036 height = x_fs->min_bounds.ascent;
1039 *pIL = x_fs->ascent - height;
1042 /***********************************************************************
1043 * XFONT_CharWidth
1045 static int XFONT_CharWidth(const XFontStruct* x_fs,
1046 const XFONTTRANS *XFT, int ch)
1048 if(!XFT)
1049 return x_fs->per_char[ch].width;
1050 else
1051 return x_fs->per_char[ch].attributes * XFT->pixelsize / 1000.0;
1054 /***********************************************************************
1055 * XFONT_GetAvgCharWidth
1057 static INT XFONT_GetAvgCharWidth( LPIFONTINFO16 pFI, const XFontStruct* x_fs,
1058 const XFONTTRANS *XFT)
1060 unsigned min = (unsigned char)pFI->dfFirstChar;
1061 unsigned max = (unsigned char)pFI->dfLastChar;
1063 INT avg;
1065 if( x_fs->per_char )
1067 int width = 0, chars = 0, j;
1068 if( IS_LATIN_CHARSET(pFI->dfCharSet) ||
1069 pFI->dfCharSet == DEFAULT_CHARSET )
1071 /* FIXME - should use a weighted average */
1072 for( j = 0; j < 26; j++ )
1073 width += XFONT_CharWidth(x_fs, XFT, 'a' + j - min) +
1074 XFONT_CharWidth(x_fs, XFT, 'A' + j - min);
1075 chars = 52;
1077 else /* unweighted average of everything */
1079 for( j = 0, max -= min; j <= max; j++ )
1080 if( !CI_NONEXISTCHAR(x_fs->per_char + j) )
1082 width += XFONT_CharWidth(x_fs, XFT, j);
1083 chars++;
1086 if (chars) avg = (width + (chars>>1))/ chars;
1087 else avg = 0; /* No characters exist at all */
1089 else /* uniform width */
1090 avg = x_fs->min_bounds.width;
1092 return avg;
1095 /***********************************************************************
1096 * XFONT_GetMaxCharWidth
1098 static INT XFONT_GetMaxCharWidth(const XFontStruct* xfs, const XFONTTRANS *XFT)
1100 unsigned min = (unsigned char)xfs->min_char_or_byte2;
1101 unsigned max = (unsigned char)xfs->max_char_or_byte2;
1102 int maxwidth, j;
1104 if(!XFT || !xfs->per_char)
1105 return abs(xfs->max_bounds.width);
1107 for( j = 0, maxwidth = 0, max -= min; j <= max; j++ )
1108 if( !CI_NONEXISTCHAR(xfs->per_char + j) )
1109 if(maxwidth < xfs->per_char[j].attributes)
1110 maxwidth = xfs->per_char[j].attributes;
1112 maxwidth *= XFT->pixelsize / 1000.0;
1113 return maxwidth;
1116 /***********************************************************************
1117 * XFONT_SetFontMetric
1119 * INIT ONLY
1121 * Initializes IFONTINFO16.
1123 static void XFONT_SetFontMetric(fontInfo* fi, const fontResource* fr, XFontStruct* xfs)
1125 unsigned min, max;
1126 fi->df.dfFirstChar = (BYTE)(min = xfs->min_char_or_byte2);
1127 fi->df.dfLastChar = (BYTE)(max = xfs->max_char_or_byte2);
1129 fi->df.dfDefaultChar = (BYTE)xfs->default_char;
1130 fi->df.dfBreakChar = (BYTE)(( ' ' < min || ' ' > max) ? xfs->default_char: ' ');
1132 fi->df.dfPixHeight = (INT16)((fi->df.dfAscent = (INT16)xfs->ascent) + xfs->descent);
1133 fi->df.dfPixWidth = (xfs->per_char) ? 0 : xfs->min_bounds.width;
1135 XFONT_GetLeading( &fi->df, xfs, &fi->df.dfInternalLeading, &fi->df.dfExternalLeading, NULL );
1136 fi->df.dfAvgWidth = (INT16)XFONT_GetAvgCharWidth(&fi->df, xfs, NULL );
1137 fi->df.dfMaxWidth = (INT16)XFONT_GetMaxCharWidth(xfs, NULL);
1139 if( xfs->min_bounds.width != xfs->max_bounds.width )
1140 fi->df.dfPitchAndFamily |= TMPF_FIXED_PITCH; /* au contraire! */
1141 if( fi->fi_flags & FI_SCALABLE )
1143 fi->df.dfType = DEVICE_FONTTYPE;
1144 fi->df.dfPitchAndFamily |= TMPF_DEVICE;
1146 else if( fi->fi_flags & FI_TRUETYPE )
1147 fi->df.dfType = TRUETYPE_FONTTYPE;
1148 else
1149 fi->df.dfType = RASTER_FONTTYPE;
1151 fi->df.dfFace = fr->lfFaceName;
1154 /***********************************************************************
1155 * XFONT_GetFontMetric
1157 * Retrieve font metric info (enumeration).
1159 static UINT XFONT_GetFontMetric( const fontInfo* pfi, const LPENUMLOGFONTEX16 pLF,
1160 const LPNEWTEXTMETRIC16 pTM )
1162 memset( pLF, 0, sizeof(*pLF) );
1163 memset( pTM, 0, sizeof(*pTM) );
1165 #define plf ((LPLOGFONT16)pLF)
1166 plf->lfHeight = pTM->tmHeight = pfi->df.dfPixHeight;
1167 plf->lfWidth = pTM->tmAveCharWidth = pfi->df.dfAvgWidth;
1168 plf->lfWeight = pTM->tmWeight = pfi->df.dfWeight;
1169 plf->lfItalic = pTM->tmItalic = pfi->df.dfItalic;
1170 plf->lfUnderline = pTM->tmUnderlined = pfi->df.dfUnderline;
1171 plf->lfStrikeOut = pTM->tmStruckOut = pfi->df.dfStrikeOut;
1172 plf->lfCharSet = pTM->tmCharSet = pfi->df.dfCharSet;
1174 /* convert pitch values */
1176 pTM->tmPitchAndFamily = pfi->df.dfPitchAndFamily;
1177 plf->lfPitchAndFamily = (pfi->df.dfPitchAndFamily & 0xF1) + 1;
1179 lstrcpynA( plf->lfFaceName, pfi->df.dfFace, LF_FACESIZE );
1180 #undef plf
1182 /* FIXME: fill in rest of plF values
1183 lstrcpynA(plF->elfFullName, , LF_FULLFACESIZE);
1184 lstrcpynA(plF->elfStyle, , LF_FACESIZE);
1185 lstrcpynA(plF->elfScript, , LF_FACESIZE);
1188 pTM->tmAscent = pfi->df.dfAscent;
1189 pTM->tmDescent = pTM->tmHeight - pTM->tmAscent;
1190 pTM->tmInternalLeading = pfi->df.dfInternalLeading;
1191 pTM->tmMaxCharWidth = pfi->df.dfMaxWidth;
1192 pTM->tmDigitizedAspectX = pfi->df.dfHorizRes;
1193 pTM->tmDigitizedAspectY = pfi->df.dfVertRes;
1195 pTM->tmFirstChar = pfi->df.dfFirstChar;
1196 pTM->tmLastChar = pfi->df.dfLastChar;
1197 pTM->tmDefaultChar = pfi->df.dfDefaultChar;
1198 pTM->tmBreakChar = pfi->df.dfBreakChar;
1200 /* return font type */
1202 return pfi->df.dfType;
1206 /***********************************************************************
1207 * XFONT_FixupFlags
1209 * INIT ONLY
1211 * dfPitchAndFamily flags for some common typefaces.
1213 static BYTE XFONT_FixupFlags( LPCSTR lfFaceName )
1215 switch( lfFaceName[0] )
1217 case 'a':
1218 case 'A': if(!strncasecmp(lfFaceName, "Arial", 5) )
1219 return FF_SWISS;
1220 break;
1221 case 'h':
1222 case 'H': if(!strcasecmp(lfFaceName, "Helvetica") )
1223 return FF_SWISS;
1224 break;
1225 case 'c':
1226 case 'C': if(!strncasecmp(lfFaceName, "Courier", 7))
1227 return FF_MODERN;
1229 if (!strcasecmp(lfFaceName, "Charter") )
1230 return FF_ROMAN;
1231 break;
1232 case 'p':
1233 case 'P': if( !strcasecmp(lfFaceName,"Palatino") )
1234 return FF_ROMAN;
1235 break;
1236 case 't':
1237 case 'T': if(!strncasecmp(lfFaceName, "Times", 5) )
1238 return FF_ROMAN;
1239 break;
1240 case 'u':
1241 case 'U': if(!strcasecmp(lfFaceName, "Utopia") )
1242 return FF_ROMAN;
1243 break;
1244 case 'z':
1245 case 'Z': if(!strcasecmp(lfFaceName, "Zapf Dingbats") )
1246 return FF_DECORATIVE;
1248 return 0;
1251 /***********************************************************************
1252 * XFONT_SameFoundryAndFamily
1254 * INIT ONLY
1256 static BOOL XFONT_SameFoundryAndFamily( const LFD* lfd1, const LFD* lfd2 )
1258 return ( !strcasecmp( lfd1->foundry, lfd2->foundry ) &&
1259 !strcasecmp( lfd1->family, lfd2->family ) );
1262 /***********************************************************************
1263 * XFONT_InitialCapitals
1265 * INIT ONLY
1267 * Upper case first letters of words & remove multiple spaces
1269 static void XFONT_InitialCapitals(LPSTR lpch)
1271 int i;
1272 BOOL up;
1273 char* lpstr = lpch;
1275 for( i = 0, up = TRUE; *lpch; lpch++, i++ )
1277 if( isspace(*lpch) )
1279 if (!up) /* Not already got one */
1281 *lpstr++ = ' ';
1282 up = TRUE;
1285 else if( isalpha(*lpch) && up )
1287 *lpstr++ = toupper(*lpch);
1288 up = FALSE;
1290 else
1292 *lpstr++ = *lpch;
1293 up = FALSE;
1297 /* Remove possible trailing space */
1298 if (up && i > 0)
1299 --lpstr;
1300 *lpstr = '\0';
1304 /***********************************************************************
1305 * XFONT_WindowsNames
1307 * INIT ONLY
1309 * Build generic Windows aliases for X font names.
1311 * -misc-fixed- -> "Fixed"
1312 * -sony-fixed- -> "Sony Fixed", etc...
1314 static void XFONT_WindowsNames(void)
1316 fontResource* fr;
1318 for( fr = fontList; fr ; fr = fr->next )
1320 fontResource* pfr;
1321 char* lpch;
1323 if( fr->fr_flags & FR_NAMESET ) continue; /* skip already assigned */
1325 for( pfr = fontList; pfr != fr ; pfr = pfr->next )
1326 if( pfr->fr_flags & FR_NAMESET )
1328 if (!strcasecmp( pfr->resource->family, fr->resource->family))
1329 break;
1332 lpch = fr->lfFaceName;
1333 snprintf( fr->lfFaceName, sizeof(fr->lfFaceName), "%s %s",
1334 /* prepend vendor name */
1335 (pfr==fr) ? "" : fr->resource->foundry,
1336 fr->resource->family);
1337 XFONT_InitialCapitals(fr->lfFaceName);
1339 BYTE bFamilyStyle = XFONT_FixupFlags( fr->lfFaceName );
1340 if( bFamilyStyle)
1342 fontInfo* fi;
1343 for( fi = fr->fi ; fi ; fi = fi->next )
1344 fi->df.dfPitchAndFamily |= bFamilyStyle;
1348 TRACE("typeface '%s'\n", fr->lfFaceName);
1350 fr->fr_flags |= FR_NAMESET;
1354 /***********************************************************************
1355 * XFONT_LoadDefaultLFD
1357 * Move lfd to the head of fontList to make it more likely to be matched
1359 static void XFONT_LoadDefaultLFD(LFD* lfd, LPCSTR fonttype)
1362 fontResource *fr, *pfr;
1363 for( fr = NULL, pfr = fontList; pfr; pfr = pfr->next )
1365 if( XFONT_SameFoundryAndFamily(pfr->resource, lfd) )
1367 if( fr )
1369 fr->next = pfr->next;
1370 pfr->next = fontList;
1371 fontList = pfr;
1373 break;
1375 fr = pfr;
1377 if (!pfr)
1378 WARN("Default %sfont '-%s-%s-' not available\n", fonttype,
1379 lfd->foundry, lfd->family);
1383 /***********************************************************************
1384 * XFONT_LoadDefault
1386 static void XFONT_LoadDefault(LPCSTR ini, LPCSTR fonttype)
1388 char buffer[MAX_LFD_LENGTH];
1390 if( PROFILE_GetWineIniString( INIFontSection, ini, "", buffer, sizeof buffer ) )
1392 if (*buffer)
1394 LFD* lfd;
1395 char* pch = buffer;
1396 while( *pch && isspace(*pch) ) pch++;
1398 TRACE("Using '%s' as default %sfont\n", pch, fonttype);
1399 lfd = LFD_Parse(pch);
1400 if (lfd && lfd->foundry && lfd->family)
1401 XFONT_LoadDefaultLFD(lfd, fonttype);
1402 else
1403 WARN("Ini section [%s]%s is malformed\n", INIFontSection, ini);
1404 HeapFree(GetProcessHeap(), 0, lfd);
1409 /***********************************************************************
1410 * XFONT_LoadDefaults
1412 static void XFONT_LoadDefaults(void)
1414 XFONT_LoadDefault(INIDefaultFixed, "fixed ");
1415 XFONT_LoadDefault(INIDefault, "");
1418 /***********************************************************************
1419 * XFONT_CreateAlias
1421 static fontAlias* XFONT_CreateAlias( LPCSTR lpTypeFace, LPCSTR lpAlias )
1423 int j;
1424 fontAlias *pfa, *prev = NULL;
1426 for(pfa = aliasTable; pfa; pfa = pfa->next)
1428 /* check if we already got one */
1429 if( !strcasecmp( pfa->faTypeFace, lpAlias ) )
1431 TRACE("redundant alias '%s' -> '%s'\n",
1432 lpAlias, lpTypeFace );
1433 return NULL;
1435 prev = pfa;
1438 j = strlen(lpTypeFace) + 1;
1439 pfa = HeapAlloc( GetProcessHeap(), 0, sizeof(fontAlias) +
1440 j + strlen(lpAlias) + 1 );
1441 if (pfa)
1443 if (!prev)
1444 aliasTable = pfa;
1445 else
1446 prev->next = pfa;
1448 pfa->next = NULL;
1449 pfa->faTypeFace = (LPSTR)(pfa + 1);
1450 strcpy( pfa->faTypeFace, lpTypeFace );
1451 pfa->faAlias = pfa->faTypeFace + j;
1452 strcpy( pfa->faAlias, lpAlias );
1454 TRACE("added alias '%s' for '%s'\n", lpAlias, lpTypeFace );
1456 return pfa;
1458 return NULL;
1462 /***********************************************************************
1463 * XFONT_LoadAlias
1465 static void XFONT_LoadAlias(const LFD* lfd, LPCSTR lpAlias, BOOL bSubst)
1467 fontResource *fr, *frMatch = NULL;
1468 if (!lfd->foundry || ! lfd->family)
1470 WARN("Malformed font resource for alias '%s'\n", lpAlias);
1471 return;
1473 for (fr = fontList; fr ; fr = fr->next)
1475 if(!strcasecmp(fr->resource->family, lpAlias))
1477 /* alias is not needed since the real font is present */
1478 TRACE("Ignoring font alias '%s' as it is already available as a real font\n", lpAlias);
1479 return;
1481 if( XFONT_SameFoundryAndFamily( fr->resource, lfd ) )
1483 frMatch = fr;
1484 break;
1488 if( frMatch )
1490 if( bSubst )
1492 fontAlias *pfa, *prev = NULL;
1494 for(pfa = aliasTable; pfa; pfa = pfa->next)
1496 /* Remove lpAlias from aliasTable - we should free the old entry */
1497 if(!strcmp(lpAlias, pfa->faAlias))
1499 if(prev)
1500 prev->next = pfa->next;
1501 else
1502 aliasTable = pfa->next;
1505 /* Update any references to the substituted font in aliasTable */
1506 if(!strcmp(frMatch->lfFaceName, pfa->faTypeFace))
1507 pfa->faTypeFace = HEAP_strdupA( GetProcessHeap(), 0, lpAlias );
1508 prev = pfa;
1511 TRACE("\tsubstituted '%s' with '%s'\n", frMatch->lfFaceName, lpAlias );
1513 lstrcpynA( frMatch->lfFaceName, lpAlias, LF_FACESIZE );
1514 frMatch->fr_flags |= FR_NAMESET;
1516 else
1518 /* create new entry in the alias table */
1519 XFONT_CreateAlias( frMatch->lfFaceName, lpAlias );
1522 else
1524 WARN("Font alias '-%s-%s-' is not available\n", lfd->foundry, lfd->family);
1528 /***********************************************************************
1529 * XFONT_LoadAliases
1531 * INIT ONLY
1533 * Create font aliases for some standard windows fonts using user's
1534 * default choice of (sans-)serif fonts
1536 * Read user-defined aliases from wine.conf. Format is as follows
1538 * Alias# = [Windows font name],[LFD font name], <substitute original name>
1540 * Example:
1541 * Alias0 = Arial, -adobe-helvetica-
1542 * Alias1 = Times New Roman, -bitstream-courier-, 1
1543 * ...
1545 * Note that from 970817 and on we have built-in alias templates that take
1546 * care of the necessary Windows typefaces.
1548 typedef struct
1550 LPSTR fatResource;
1551 LPSTR fatAlias;
1552 } aliasTemplate;
1554 static void XFONT_LoadAliases(void)
1556 char *lpResource;
1557 char buffer[MAX_LFD_LENGTH];
1558 int i = 0;
1559 LFD* lfd;
1561 /* built-ins first */
1562 PROFILE_GetWineIniString( INIFontSection, INIDefaultSerif,
1563 "-bitstream-charter-", buffer, sizeof buffer );
1564 TRACE("Using '%s' as default serif font\n", buffer);
1565 lfd = LFD_Parse(buffer);
1566 /* NB XFONT_InitialCapitals should not change these standard aliases */
1567 if (lfd)
1569 XFONT_LoadAlias( lfd, "Tms Roman", FALSE);
1570 XFONT_LoadAlias( lfd, "MS Serif", FALSE);
1571 XFONT_LoadAlias( lfd, "Times New Roman", FALSE);
1573 XFONT_LoadDefaultLFD( lfd, "serif ");
1574 HeapFree(GetProcessHeap(), 0, lfd);
1577 PROFILE_GetWineIniString( INIFontSection, INIDefaultSansSerif,
1578 "-adobe-helvetica-", buffer, sizeof buffer);
1579 TRACE("Using '%s' as default sans serif font\n", buffer);
1580 lfd = LFD_Parse(buffer);
1581 if (lfd)
1583 XFONT_LoadAlias( lfd, "Helv", FALSE);
1584 XFONT_LoadAlias( lfd, "MS Sans Serif", FALSE);
1585 XFONT_LoadAlias( lfd, "Arial", FALSE);
1587 XFONT_LoadDefaultLFD( lfd, "sans serif ");
1588 HeapFree(GetProcessHeap(), 0, lfd);
1591 /* then user specified aliases */
1594 BOOL bHaveAlias, bSubst;
1595 char subsection[32];
1596 snprintf( subsection, sizeof subsection, "%s%i", INIAliasSection, i++ );
1598 bHaveAlias = PROFILE_GetWineIniString( INIFontSection,
1599 subsection, "", buffer, sizeof buffer);
1600 if (!bHaveAlias)
1601 break;
1603 XFONT_InitialCapitals(buffer);
1604 lpResource = PROFILE_GetStringItem( buffer );
1605 bSubst = (PROFILE_GetStringItem( lpResource )) ? TRUE : FALSE;
1606 if( lpResource && *lpResource )
1608 lfd = LFD_Parse(lpResource);
1609 if (lfd)
1611 XFONT_LoadAlias(lfd, buffer, bSubst);
1612 HeapFree(GetProcessHeap(), 0, lfd);
1615 else
1616 WARN("malformed font alias '%s'\n", buffer );
1618 while(TRUE);
1621 /***********************************************************************
1622 * XFONT_UnAlias
1624 * Convert an (potential) alias into a real windows name
1627 static LPCSTR XFONT_UnAlias(char* font)
1629 if (font[0])
1631 fontAlias* fa;
1632 XFONT_InitialCapitals(font); /* to remove extra white space */
1634 for( fa = aliasTable; fa; fa = fa->next )
1635 /* use case insensitive matching to handle eg "MS Sans Serif" */
1636 if( !strcasecmp( fa->faAlias, font ) )
1638 TRACE("found alias '%s'->%s'\n", font, fa->faTypeFace );
1639 strcpy(font, fa->faTypeFace);
1640 return fa->faAlias;
1641 break;
1644 return NULL;
1647 /***********************************************************************
1648 * XFONT_RemoveFontResource
1650 * Caller should check if the font resource is in use. If it is it should
1651 * set FR_REMOVED flag to delay removal until the resource is not in use
1652 * any more.
1654 void XFONT_RemoveFontResource( fontResource** ppfr )
1656 fontResource* pfr = *ppfr;
1657 #if 0
1658 fontInfo* pfi;
1660 /* FIXME - if fonts were read from a cache, these HeapFrees will fail */
1661 while( pfr->fi )
1663 pfi = pfr->fi->next;
1664 HeapFree( GetProcessHeap(), 0, pfr->fi );
1665 pfr->fi = pfi;
1667 HeapFree( GetProcessHeap(), 0, pfr );
1668 #endif
1669 *ppfr = pfr->next;
1672 /***********************************************************************
1673 * XFONT_LoadIgnores
1675 * INIT ONLY
1677 * Removes specified fonts from the font table to prevent Wine from
1678 * using it.
1680 * Ignore# = [LFD font name]
1682 * Example:
1683 * Ignore0 = -misc-nil-
1684 * Ignore1 = -sun-open look glyph-
1685 * ...
1688 static void XFONT_LoadIgnore(char* lfdname)
1690 fontResource** ppfr;
1692 LFD* lfd = LFD_Parse(lfdname);
1693 if (lfd && lfd->foundry && lfd->family)
1695 for( ppfr = &fontList; *ppfr ; ppfr = &((*ppfr)->next))
1697 if( XFONT_SameFoundryAndFamily( (*ppfr)->resource, lfd) )
1699 TRACE("Ignoring '-%s-%s-'\n",
1700 (*ppfr)->resource->foundry, (*ppfr)->resource->family );
1702 XFONT_RemoveFontResource( ppfr );
1703 break;
1707 else
1708 WARN("Malformed font resource\n");
1710 HeapFree(GetProcessHeap(), 0, lfd);
1713 static void XFONT_LoadIgnores(void)
1715 int i = 0;
1716 char subsection[32];
1717 char buffer[MAX_LFD_LENGTH];
1719 /* Standard one that noone wants */
1720 strcpy(buffer, "-misc-nil-");
1721 XFONT_LoadIgnore(buffer);
1723 /* Others from INI file */
1726 sprintf( subsection, "%s%i", INIIgnoreSection, i++ );
1728 if( PROFILE_GetWineIniString( INIFontSection,
1729 subsection, "", buffer, sizeof buffer) )
1731 char* pch = buffer;
1732 while( *pch && isspace(*pch) ) pch++;
1733 XFONT_LoadIgnore(pch);
1735 else
1736 break;
1737 } while(TRUE);
1741 /***********************************************************************
1742 * XFONT_UserMetricsCache
1744 * Returns expanded name for the cachedmetrics file.
1745 * Now it also appends the current value of the $DISPLAY variable.
1747 static char* XFONT_UserMetricsCache( char* buffer, int* buf_size )
1749 const char *confdir = get_config_dir();
1750 const char *display_name = Options.display;
1751 int len = strlen(confdir) + strlen(INIFontMetrics) + strlen(display_name) + 2;
1753 if ((len > *buf_size) &&
1754 !(buffer = HeapReAlloc( GetProcessHeap(), 0, buffer, *buf_size = len )))
1756 ERR("out of memory\n");
1757 ExitProcess(1);
1760 sprintf( buffer, "%s/%s%s", confdir, INIFontMetrics, display_name );
1761 return buffer;
1765 /***********************************************************************
1766 * X Font Matching
1768 * Compare two fonts (only parameters set by the XFONT_InitFontInfo()).
1770 static INT XFONT_IsSubset(const fontInfo* match, const fontInfo* fi)
1772 INT m;
1774 /* 0 - keep both, 1 - keep match, -1 - keep fi */
1776 /* Compare dfItalic, Underline, Strikeout, Weight, Charset */
1777 m = (BYTE*)&fi->df.dfPixWidth - (BYTE*)&fi->df.dfItalic;
1778 if( memcmp(&match->df.dfItalic, &fi->df.dfItalic, m )) return 0;
1780 if( (!((fi->fi_flags & FI_SCALABLE) + (match->fi_flags & FI_SCALABLE))
1781 && fi->lfd_height != match->lfd_height) ||
1782 (!((fi->fi_flags & FI_POLYWEIGHT) + (match->fi_flags & FI_POLYWEIGHT))
1783 && fi->df.dfWeight != match->df.dfWeight) ) return 0;
1785 m = (int)(match->fi_flags & (FI_POLYWEIGHT | FI_SCALABLE)) -
1786 (int)(fi->fi_flags & (FI_SCALABLE | FI_POLYWEIGHT));
1788 if( m == (FI_POLYWEIGHT - FI_SCALABLE) ||
1789 m == (FI_SCALABLE - FI_POLYWEIGHT) ) return 0; /* keep both */
1790 else if( m >= 0 ) return 1; /* 'match' is better */
1792 return -1; /* 'fi' is better */
1795 /***********************************************************************
1796 * XFONT_CheckFIList
1798 * REMOVE_SUBSETS - attach new fi and purge subsets
1799 * UNMARK_SUBSETS - remove subset flags from all fi entries
1801 static void XFONT_CheckFIList( fontResource* fr, fontInfo* fi, int action)
1803 int i = 0;
1804 fontInfo* pfi, *prev;
1806 for( prev = NULL, pfi = fr->fi; pfi; )
1808 if( action == REMOVE_SUBSETS )
1810 if( pfi->fi_flags & FI_SUBSET )
1812 fontInfo* subset = pfi;
1814 i++;
1815 fr->fi_count--;
1816 if( prev ) prev->next = pfi = pfi->next;
1817 else fr->fi = pfi = pfi->next;
1818 HeapFree( GetProcessHeap(), 0, subset );
1819 continue;
1822 else pfi->fi_flags &= ~FI_SUBSET;
1824 prev = pfi;
1825 pfi = pfi->next;
1828 if( action == REMOVE_SUBSETS ) /* also add the superset */
1830 if( fi->fi_flags & FI_SCALABLE )
1832 fi->next = fr->fi;
1833 fr->fi = fi;
1835 else if( prev ) prev->next = fi; else fr->fi = fi;
1836 fr->fi_count++;
1839 if( i ) TRACE("\t purged %i subsets [%i]\n", i , fr->fi_count);
1842 /***********************************************************************
1843 * XFONT_FindFIList
1845 static fontResource* XFONT_FindFIList( fontResource* pfr, const char* pTypeFace )
1847 while( pfr )
1849 if( !strcasecmp( pfr->lfFaceName, pTypeFace ) ) break;
1850 pfr = pfr->next;
1852 /* Give the app back the font name it asked for. Encarta checks this. */
1853 if (pfr) strcpy(pfr->lfFaceName,pTypeFace);
1854 return pfr;
1857 /***********************************************************************
1858 * XFONT_FixupPointSize
1860 static void XFONT_FixupPointSize(fontInfo* fi)
1862 #define df (fi->df)
1863 df.dfHorizRes = df.dfVertRes = fi->lfd_resolution;
1864 df.dfPoints = (INT16)
1865 (((INT)(df.dfPixHeight - df.dfInternalLeading) * 72 + (df.dfVertRes >> 1)) /
1866 df.dfVertRes );
1867 #undef df
1870 /***********************************************************************
1871 * XFONT_BuildMetrics
1873 * Build font metrics from X font
1875 static int XFONT_BuildMetrics(char** x_pattern, int res, unsigned x_checksum, int x_count)
1877 int i;
1878 fontInfo* fi = NULL;
1879 fontResource* fr, *pfr;
1880 int n_ff = 0;
1882 MESSAGE("Building font metrics. This may take some time...\n");
1883 for( i = 0; i < x_count; i++ )
1885 char* typeface;
1886 LFD* lfd;
1887 int j;
1888 char buffer[MAX_LFD_LENGTH];
1889 char* lpstr;
1890 XFontStruct* x_fs;
1891 fontInfo* pfi;
1893 typeface = HEAP_strdupA(GetProcessHeap(), 0, x_pattern[i]);
1894 if (!typeface)
1895 break;
1897 lfd = LFD_Parse(typeface);
1898 if (!lfd)
1900 HeapFree(GetProcessHeap(), 0, typeface);
1901 continue;
1904 /* find a family to insert into */
1906 for( pfr = NULL, fr = fontList; fr; fr = fr->next )
1908 if( XFONT_SameFoundryAndFamily(fr->resource, lfd))
1909 break;
1910 pfr = fr;
1913 if( !fi ) fi = (fontInfo*) HeapAlloc(GetProcessHeap(), 0, sizeof(fontInfo));
1915 if( !LFD_InitFontInfo( fi, lfd, x_pattern[i]) )
1916 goto nextfont;
1918 if( !fr ) /* add new family */
1920 n_ff++;
1921 fr = (fontResource*) HeapAlloc(GetProcessHeap(), 0, sizeof(fontResource));
1922 if (fr)
1924 memset(fr, 0, sizeof(fontResource));
1926 fr->resource = (LFD*) HeapAlloc(GetProcessHeap(), 0, sizeof(LFD));
1927 memset(fr->resource, 0, sizeof(LFD));
1929 TRACE("family: -%s-%s-\n", lfd->foundry, lfd->family );
1930 fr->resource->foundry = HEAP_strdupA(GetProcessHeap(), 0, lfd->foundry);
1931 fr->resource->family = HEAP_strdupA(GetProcessHeap(), 0, lfd->family);
1932 fr->resource->weight = "";
1934 if( pfr ) pfr->next = fr;
1935 else fontList = fr;
1937 else
1938 WARN("Not enough memory for a new font family\n");
1941 /* check if we already have something better than "fi" */
1943 for( pfi = fr->fi, j = 0; pfi && j <= 0; pfi = pfi->next )
1944 if( (j = XFONT_IsSubset( pfi, fi )) < 0 )
1945 pfi->fi_flags |= FI_SUBSET; /* superseded by "fi" */
1946 if( j > 0 ) goto nextfont;
1948 /* add new font instance "fi" to the "fr" font resource */
1950 if( fi->fi_flags & FI_SCALABLE )
1952 LFD lfd1;
1953 char pxl_string[4], res_string[4];
1954 /* set scalable font height to get an basis for extrapolation */
1956 fi->lfd_height = DEF_SCALABLE_HEIGHT;
1957 fi->lfd_resolution = res;
1959 sprintf(pxl_string, "%d", fi->lfd_height);
1960 sprintf(res_string, "%d", fi->lfd_resolution);
1962 lfd1 = *lfd;
1963 lfd1.pixel_size = pxl_string;
1964 lfd1.point_size = "*";
1965 lfd1.resolution_x = res_string;
1966 lfd1.resolution_y = res_string;
1968 LFD_UnParse(buffer, sizeof buffer, &lfd1);
1970 lpstr = buffer;
1972 else lpstr = x_pattern[i];
1974 if( (x_fs = TSXLoadQueryFont(display, lpstr)) )
1976 XFONT_SetFontMetric( fi, fr, x_fs );
1977 TSXFreeFont( display, x_fs );
1979 XFONT_FixupPointSize(fi);
1981 TRACE("\t[% 2ipt] '%s'\n", fi->df.dfPoints, x_pattern[i] );
1983 XFONT_CheckFIList( fr, fi, REMOVE_SUBSETS );
1984 fi = NULL; /* preventing reuse */
1986 else
1988 ERR("failed to load %s\n", lpstr );
1990 XFONT_CheckFIList( fr, fi, UNMARK_SUBSETS );
1992 nextfont:
1993 HeapFree(GetProcessHeap(), 0, lfd);
1994 HeapFree(GetProcessHeap(), 0, typeface);
1996 if( fi ) HeapFree(GetProcessHeap(), 0, fi);
1998 /* Scan through the font list and remove FontResorce(s) (fr)
1999 * that have no associated Fontinfo(s) (fi).
2000 * This code is necessary because XFONT_ReadCachedMetrics
2001 * assumes that there is at least one fi associated with a fr.
2002 * This assumption is invalid for TT font
2003 * -altsys-ms outlook-medium-r-normal--0-0-0-0-p-0-microsoft-symbol.
2006 fr = fontList;
2008 while (!fr->fi_count)
2010 fontList = fr->next;
2012 HeapFree(GetProcessHeap(), 0, fr->resource);
2013 HeapFree(GetProcessHeap(), 0, fr);
2015 fr = fontList;
2016 n_ff--;
2019 fr = fontList;
2021 while (fr->next)
2023 if (!fr->next->fi_count)
2025 pfr = fr->next;
2026 fr->next = fr->next->next;
2028 HeapFree(GetProcessHeap(), 0, pfr->resource);
2029 HeapFree(GetProcessHeap(), 0, pfr);
2031 n_ff--;
2033 else
2034 fr = fr->next;
2037 return n_ff;
2040 /***********************************************************************
2041 * XFONT_ReadCachedMetrics
2043 * INIT ONLY
2045 static BOOL XFONT_ReadCachedMetrics( int fd, int res, unsigned x_checksum, int x_count )
2047 if( fd >= 0 )
2049 unsigned u;
2050 int i, j;
2052 /* read checksums */
2053 read( fd, &u, sizeof(unsigned) );
2054 read( fd, &i, sizeof(int) );
2056 if( u == x_checksum && i == x_count )
2058 off_t length, offset = 3 * sizeof(int);
2060 /* read total size */
2061 read( fd, &i, sizeof(int) );
2062 length = lseek( fd, 0, SEEK_END );
2064 if( length == (i + offset) )
2066 lseek( fd, offset, SEEK_SET );
2067 fontList = (fontResource*)HeapAlloc( GetProcessHeap(), 0, i);
2068 if( fontList )
2070 fontResource* pfr = fontList;
2071 fontInfo* pfi = NULL;
2073 TRACE("Reading cached font metrics:\n");
2075 read( fd, fontList, i); /* read all metrics at once */
2076 while( offset < length )
2078 offset += sizeof(fontResource) + sizeof(fontInfo);
2079 pfr->fi = pfi = (fontInfo*)(pfr + 1);
2080 j = 1;
2081 while( TRUE )
2083 if( offset > length ||
2084 pfi->cptable >= (UINT16)X11DRV_CPTABLE_COUNT ||
2085 (int)(pfi->next) != j++ ) goto fail;
2087 if( pfi->df.dfPixHeight == 0 ) goto fail;
2089 pfi->df.dfFace = pfr->lfFaceName;
2090 if( pfi->fi_flags & FI_SCALABLE )
2092 /* we can pretend we got this font for any resolution */
2093 pfi->lfd_resolution = res;
2094 XFONT_FixupPointSize(pfi);
2096 pfi->next = pfi + 1;
2098 if( j > pfr->fi_count ) break;
2100 pfi = pfi->next;
2101 offset += sizeof(fontInfo);
2103 pfi->next = NULL;
2104 if( pfr->next )
2106 pfr->next = (fontResource*)(pfi + 1);
2107 pfr = pfr->next;
2109 else break;
2111 if( pfr->next == NULL &&
2112 *(int*)(pfi + 1) == X_FMC_MAGIC )
2114 /* read LFD stubs */
2115 char* lpch = (char*)((int*)(pfi + 1) + 1);
2116 offset += sizeof(int);
2117 for( pfr = fontList; pfr; pfr = pfr->next )
2119 size_t len = strlen(lpch) + 1;
2120 TRACE("\t%s, %i instances\n", lpch, pfr->fi_count );
2121 pfr->resource = LFD_Parse(lpch);
2122 lpch += len;
2123 offset += len;
2124 if (offset > length)
2125 goto fail;
2127 close( fd );
2128 return TRUE;
2133 fail:
2134 if( fontList ) HeapFree( GetProcessHeap(), 0, fontList );
2135 fontList = NULL;
2136 close( fd );
2138 return FALSE;
2141 /***********************************************************************
2142 * XFONT_WriteCachedMetrics
2144 * INIT ONLY
2146 static BOOL XFONT_WriteCachedMetrics( int fd, unsigned x_checksum, int x_count, int n_ff )
2148 fontResource* pfr;
2149 fontInfo* pfi;
2151 if( fd >= 0 )
2153 int i, j, k;
2154 char buffer[MAX_LFD_LENGTH];
2156 /* font metrics file:
2158 * +0000 x_checksum
2159 * +0004 x_count
2160 * +0008 total size to load
2161 * +000C prepackaged font metrics
2162 * ...
2163 * +...x X_FMC_MAGIC
2164 * +...x + 4 LFD stubs
2167 write( fd, &x_checksum, sizeof(unsigned) );
2168 write( fd, &x_count, sizeof(int) );
2170 for( j = i = 0, pfr = fontList; pfr; pfr = pfr->next )
2172 LFD_UnParse(buffer, sizeof buffer, pfr->resource);
2173 i += strlen( buffer) + 1;
2174 j += pfr->fi_count;
2176 i += n_ff * sizeof(fontResource) + j * sizeof(fontInfo) + sizeof(int);
2177 write( fd, &i, sizeof(int) );
2179 TRACE("Writing font cache:\n");
2181 for( pfr = fontList; pfr; pfr = pfr->next )
2183 fontInfo fi;
2185 TRACE("\t-%s-%s-, %i instances\n", pfr->resource->foundry, pfr->resource->family, pfr->fi_count );
2187 i = write( fd, pfr, sizeof(fontResource) );
2188 if( i == sizeof(fontResource) )
2190 for( k = 1, pfi = pfr->fi; pfi; pfi = pfi->next )
2192 fi = *pfi;
2194 fi.df.dfFace = NULL;
2195 fi.next = (fontInfo*)k; /* loader checks this */
2197 j = write( fd, &fi, sizeof(fi) );
2198 k++;
2200 if( j == sizeof(fontInfo) ) continue;
2202 break;
2204 if( i == sizeof(fontResource) && j == sizeof(fontInfo) )
2206 i = j = X_FMC_MAGIC;
2207 write( fd, &i, sizeof(int) );
2208 for( pfr = fontList; pfr && i == j; pfr = pfr->next )
2210 LFD_UnParse(buffer, sizeof buffer, pfr->resource);
2211 i = strlen( buffer ) + 1;
2212 j = write( fd, buffer, i );
2215 close( fd );
2216 return ( i == j );
2218 return FALSE;
2221 /***********************************************************************
2222 * XFONT_GetPointResolution()
2224 * INIT ONLY
2226 * Here we initialize DefResolution which is used in the
2227 * XFONT_Match() penalty function. We also load the point
2228 * resolution value (higher values result in larger fonts).
2230 static int XFONT_GetPointResolution( DeviceCaps* pDevCaps )
2232 int i, j, point_resolution, num = 3;
2233 int allowed_xfont_resolutions[3] = { 72, 75, 100 };
2234 int best = 0, best_diff = 65536;
2236 point_resolution = PROFILE_GetWineIniInt( INIFontSection, INIResolution, 0 );
2237 if( !point_resolution )
2238 point_resolution = pDevCaps->logPixelsY;
2239 else
2240 pDevCaps->logPixelsX = pDevCaps->logPixelsY = point_resolution;
2243 /* FIXME We can only really guess at a best DefResolution
2244 * - this should be configurable
2246 for( i = best = 0; i < num; i++ )
2248 j = abs( point_resolution - allowed_xfont_resolutions[i] );
2249 if( j < best_diff )
2251 best = i;
2252 best_diff = j;
2255 DefResolution = allowed_xfont_resolutions[best];
2257 /* FIXME - do win95,nt40,... do this as well ? */
2258 if (TWEAK_WineLook == WIN98_LOOK)
2260 /* Lie about the screen size, so that eg MM_LOMETRIC becomes MM_logical_LOMETRIC */
2261 int denom;
2262 denom = pDevCaps->logPixelsX * 10;
2263 pDevCaps->horzSize = (pDevCaps->horzRes * 254 + (denom>>1)) / denom;
2264 denom = pDevCaps->logPixelsY * 10;
2265 pDevCaps->vertSize = (pDevCaps->vertRes * 254 + (denom>>1)) / denom;
2268 return point_resolution;
2272 /***********************************************************************
2273 * XFONT_Match
2275 * Compute the matching score between the logical font and the device font.
2277 * contributions from highest to lowest:
2278 * charset
2279 * fixed pitch
2280 * height
2281 * family flags (only when the facename is not present)
2282 * width
2283 * weight, italics, underlines, strikeouts
2285 * NOTE: you can experiment with different penalty weights to see what happens.
2286 * http://premium.microsoft.com/msdn/library/techart/f365/f36b/f37b/d38b/sa8bf.htm
2288 static UINT XFONT_Match( fontMatch* pfm )
2290 fontInfo* pfi = pfm->pfi; /* device font to match */
2291 LPLOGFONT16 plf = pfm->plf; /* wanted logical font */
2292 UINT penalty = 0;
2293 BOOL bR6 = pfm->flags & FO_MATCH_XYINDEP; /* from TextCaps */
2294 BOOL bScale = pfi->fi_flags & FI_SCALABLE;
2295 int d = 0, height;
2297 TRACE("\t[ %-2ipt h=%-3i w=%-3i %s%s]\n", pfi->df.dfPoints,
2298 pfi->df.dfPixHeight, pfi->df.dfAvgWidth,
2299 (pfi->df.dfWeight > FW_NORMAL) ? "Bold " : "Normal ",
2300 (pfi->df.dfItalic) ? "Italic" : "" );
2302 pfm->flags &= FO_MATCH_MASK;
2304 /* Charset */
2305 /* pfm->internal_charset: given(required) charset */
2306 /* pfi->internal_charset: charset of this font */
2307 if (pfi->internal_charset == DEFAULT_CHARSET)
2309 /* special case(unicode font) */
2310 /* priority: unmatched charset < unicode < matched charset */
2311 penalty += 0x50;
2313 else
2315 if( pfm->internal_charset == DEFAULT_CHARSET )
2318 if (pfi->internal_charset != ANSI_CHARSET)
2319 penalty += 0x200;
2321 if ( pfi->codepage != GetACP() )
2322 penalty += 0x200;
2324 else if (pfm->internal_charset != pfi->internal_charset)
2326 if ( pfi->internal_charset & 0xff00 )
2327 penalty += 0x1000; /* internal charset - should not be used */
2328 else
2329 penalty += 0x200;
2333 /* Height */
2334 height = -1;
2336 if( plf->lfHeight > 0 )
2338 int h = pfi->df.dfPixHeight;
2339 d = h - plf->lfHeight;
2340 height = plf->lfHeight;
2342 else
2344 int h = pfi->df.dfPixHeight - pfi->df.dfInternalLeading;
2345 if (h)
2347 d = h + plf->lfHeight;
2348 height = (-plf->lfHeight * pfi->df.dfPixHeight) / h;
2350 else
2352 ERR("PixHeight == InternalLeading\n");
2353 penalty += 0x1000; /* dont want this */
2358 if( height == 0 )
2359 pfm->height = 1; /* Very small */
2360 else if( d )
2362 if( bScale )
2363 pfm->height = height;
2364 else if( (plf->lfQuality != PROOF_QUALITY) && bR6 )
2366 if( d > 0 ) /* do not shrink raster fonts */
2368 pfm->height = pfi->df.dfPixHeight;
2369 penalty += (pfi->df.dfPixHeight - height) * 0x4;
2371 else /* expand only in integer multiples */
2373 pfm->height = height - height%pfi->df.dfPixHeight;
2374 penalty += (height - pfm->height + 1) * height / pfi->df.dfPixHeight;
2377 else /* can't be scaled at all */
2379 if( plf->lfQuality != PROOF_QUALITY) pfm->flags |= FO_SYNTH_HEIGHT;
2380 pfm->height = pfi->df.dfPixHeight;
2381 penalty += (d > 0)? d * 0x8 : -d * 0x10;
2384 else
2385 pfm->height = pfi->df.dfPixHeight;
2387 /* Pitch and Family */
2388 if( pfm->flags & FO_MATCH_PAF ) {
2389 int family = plf->lfPitchAndFamily & FF_FAMILY;
2391 /* TMPF_FIXED_PITCH means exactly the opposite */
2392 if( plf->lfPitchAndFamily & FIXED_PITCH ) {
2393 if( pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH ) penalty += 0x100;
2394 } else /* Variable is the default */
2395 if( !(pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH) ) penalty += 0x2;
2397 if (family != FF_DONTCARE && family != (pfi->df.dfPitchAndFamily & FF_FAMILY) )
2398 penalty += 0x10;
2401 /* Width */
2402 if( plf->lfWidth )
2404 int h;
2405 if( bR6 || bScale ) h = 0;
2406 else
2408 /* FIXME: not complete */
2410 pfm->flags |= FO_SYNTH_WIDTH;
2411 h = abs(plf->lfWidth - (pfm->height * pfi->df.dfAvgWidth)/pfi->df.dfPixHeight);
2413 penalty += h * ( d ) ? 0x2 : 0x1 ;
2415 else if( !(pfi->fi_flags & FI_NORMAL) ) penalty++;
2417 /* Weight */
2418 if( plf->lfWeight != FW_DONTCARE )
2420 penalty += abs(plf->lfWeight - pfi->df.dfWeight) / 40;
2421 if( plf->lfWeight > pfi->df.dfWeight ) pfm->flags |= FO_SYNTH_BOLD;
2422 } else if( pfi->df.dfWeight >= FW_BOLD ) penalty++; /* choose normal by default */
2424 /* Italic */
2425 if( plf->lfItalic != pfi->df.dfItalic )
2427 penalty += 0x4;
2428 pfm->flags |= FO_SYNTH_ITALIC;
2430 /* Underline */
2431 if( plf->lfUnderline ) pfm->flags |= FO_SYNTH_UNDERLINE;
2433 /* Strikeout */
2434 if( plf->lfStrikeOut ) pfm->flags |= FO_SYNTH_STRIKEOUT;
2437 if( penalty && !bScale && pfi->lfd_resolution != DefResolution )
2438 penalty++;
2440 TRACE(" returning %i\n", penalty );
2442 return penalty;
2445 /***********************************************************************
2446 * XFONT_MatchFIList
2448 * Scan a particular font resource for the best match.
2450 static UINT XFONT_MatchFIList( fontMatch* pfm )
2452 BOOL skipRaster = (pfm->flags & FO_MATCH_NORASTER);
2453 UINT current_score, score = (UINT)(-1);
2454 fontMatch fm = *pfm;
2456 for( fm.pfi = pfm->pfr->fi; fm.pfi && score; fm.pfi = fm.pfi->next)
2458 if( skipRaster && !(fm.pfi->fi_flags & FI_SCALABLE) )
2459 continue;
2461 current_score = XFONT_Match( &fm );
2462 if( score > current_score )
2464 *pfm = fm;
2465 score = current_score;
2468 return score;
2471 /***********************************************************************
2472 * XFONT_MatchDeviceFont
2474 * Scan font resource tree.
2477 static void XFONT_MatchDeviceFont( fontResource* start, fontMatch* pfm)
2479 fontMatch fm = *pfm;
2480 UINT current_score, score = (UINT)(-1);
2481 fontResource** ppfr;
2483 TRACE("(%u) '%s' h=%i weight=%i %s\n",
2484 pfm->plf->lfCharSet, pfm->plf->lfFaceName, pfm->plf->lfHeight,
2485 pfm->plf->lfWeight, (pfm->plf->lfItalic) ? "Italic" : "" );
2487 pfm->pfi = NULL;
2488 if( fm.plf->lfFaceName[0] )
2490 fm.pfr = XFONT_FindFIList( start, fm.plf->lfFaceName);
2491 if( fm.pfr ) /* match family */
2493 TRACE("found facename '%s'\n", fm.pfr->lfFaceName );
2495 if( fm.pfr->fr_flags & FR_REMOVED )
2496 fm.pfr = 0;
2497 else
2499 XFONT_MatchFIList( &fm );
2500 *pfm = fm;
2501 if (pfm->pfi)
2502 return;
2506 /* get charset if lfFaceName is one of known facenames. */
2508 const struct CharsetBindingInfo* pcharsetbindings;
2510 pcharsetbindings = &charsetbindings[0];
2511 while ( pcharsetbindings->pszFaceName != NULL )
2513 if ( !strcmp( pcharsetbindings->pszFaceName,
2514 fm.plf->lfFaceName ) )
2516 fm.internal_charset = pcharsetbindings->charset;
2517 break;
2519 pcharsetbindings ++;
2521 TRACE( "%s charset %u\n", fm.plf->lfFaceName, fm.internal_charset );
2525 /* match all available fonts */
2527 fm.flags |= FO_MATCH_PAF;
2528 for( ppfr = &fontList; *ppfr && score; ppfr = &(*ppfr)->next )
2530 if( (*ppfr)->fr_flags & FR_REMOVED )
2532 if( (*ppfr)->fo_count == 0 )
2533 XFONT_RemoveFontResource( ppfr );
2534 continue;
2537 fm.pfr = *ppfr;
2539 TRACE("%s\n", fm.pfr->lfFaceName );
2541 current_score = XFONT_MatchFIList( &fm );
2542 if( current_score < score )
2544 score = current_score;
2545 *pfm = fm;
2551 /***********************************************************************
2552 * X Font Cache
2554 static void XFONT_GrowFreeList(int start, int end)
2556 /* add all entries from 'start' up to and including 'end' */
2558 memset( fontCache + start, 0, (end - start + 1) * sizeof(fontObject) );
2560 fontCache[end].lru = fontLF;
2561 fontCache[end].count = -1;
2562 fontLF = start;
2563 while( start < end )
2565 fontCache[start].count = -1;
2566 fontCache[start].lru = start + 1;
2567 start++;
2571 static fontObject* XFONT_LookupCachedFont( const LPLOGFONT16 plf, UINT16* checksum )
2573 UINT16 cs = __lfCheckSum( plf );
2574 int i = fontMRU, prev = -1;
2576 *checksum = cs;
2577 while( i >= 0 )
2579 if( fontCache[i].lfchecksum == cs &&
2580 !(fontCache[i].fo_flags & FO_REMOVED) )
2582 /* FIXME: something more intelligent here ? */
2584 if( !memcmp( plf, &fontCache[i].lf,
2585 sizeof(LOGFONT16) - LF_FACESIZE ) &&
2586 !strcmp( plf->lfFaceName, fontCache[i].lf.lfFaceName) )
2588 /* remove temporarily from the lru list */
2589 if( prev >= 0 )
2590 fontCache[prev].lru = fontCache[i].lru;
2591 else
2592 fontMRU = (INT16)fontCache[i].lru;
2593 return (fontCache + i);
2596 prev = i;
2597 i = (INT16)fontCache[i].lru;
2599 return NULL;
2602 static fontObject* XFONT_GetCacheEntry(void)
2604 int i;
2606 if( fontLF == -1 )
2608 int prev_i, prev_j, j;
2610 TRACE("font cache is full\n");
2612 /* lookup the least recently used font */
2614 for( prev_i = prev_j = j = -1, i = fontMRU; i >= 0; i = (INT16)fontCache[i].lru )
2616 if( fontCache[i].count <= 0 &&
2617 !(fontCache[i].fo_flags & FO_SYSTEM) )
2619 prev_j = prev_i;
2620 j = i;
2622 prev_i = i;
2625 if( j >= 0 ) /* unload font */
2627 /* detach from the lru list */
2629 TRACE("\tfreeing entry %i\n", j );
2631 fontCache[j].fr->fo_count--;
2633 if( prev_j >= 0 )
2634 fontCache[prev_j].lru = fontCache[j].lru;
2635 else fontMRU = (INT16)fontCache[j].lru;
2637 /* FIXME: lpXForm, lpPixmap */
2638 if(fontCache[j].lpX11Trans)
2639 HeapFree( GetProcessHeap(), 0, fontCache[j].lpX11Trans );
2641 TSXFreeFont( display, fontCache[j].fs );
2643 memset( fontCache + j, 0, sizeof(fontObject) );
2644 return (fontCache + j);
2646 else /* expand cache */
2648 fontObject* newCache;
2650 prev_i = fontCacheSize + FONTCACHE;
2652 TRACE("\tgrowing font cache from %i to %i\n", fontCacheSize, prev_i );
2654 if( (newCache = (fontObject*)HeapReAlloc(GetProcessHeap(), 0,
2655 fontCache, prev_i)) )
2657 i = fontCacheSize;
2658 fontCacheSize = prev_i;
2659 fontCache = newCache;
2660 XFONT_GrowFreeList( i, fontCacheSize - 1);
2662 else return NULL;
2666 /* detach from the free list */
2668 i = fontLF;
2669 fontLF = (INT16)fontCache[i].lru;
2670 fontCache[i].count = 0;
2671 return (fontCache + i);
2674 static int XFONT_ReleaseCacheEntry(const fontObject* pfo)
2676 UINT u = (UINT)(pfo - fontCache);
2677 int i;
2678 int ret;
2680 if( u < fontCacheSize )
2682 ret = --fontCache[u].count;
2683 if ( ret == 0 )
2685 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
2687 if( CHECK_PFONT(pfo->prefobjs[i]) )
2688 XFONT_ReleaseCacheEntry(__PFONT(pfo->prefobjs[i]));
2692 return ret;
2695 return -1;
2698 /***********************************************************************
2699 * X11DRV_FONT_Init
2701 * Initialize font resource list and allocate font cache.
2703 BOOL X11DRV_FONT_Init( DeviceCaps* pDevCaps )
2705 char** x_pattern;
2706 unsigned x_checksum;
2707 int i,res, x_count, fd, buf_size;
2708 char *buffer;
2710 res = XFONT_GetPointResolution( pDevCaps );
2712 x_pattern = TSXListFonts(display, "*", MAX_FONTS, &x_count );
2714 TRACE("Font Mapper: initializing %i fonts [logical dpi=%i, default dpi=%i]\n",
2715 x_count, res, DefResolution);
2716 if (x_count == MAX_FONTS)
2717 MESSAGE("There may be more fonts available - try increasing the value of MAX_FONTS\n");
2719 for( i = x_checksum = 0; i < x_count; i++ )
2721 int j;
2722 #if 0
2723 printf("%i\t: %s\n", i, x_pattern[i] );
2724 #endif
2726 j = strlen( x_pattern[i] );
2727 if( j ) x_checksum ^= __genericCheckSum( x_pattern[i], j );
2729 x_checksum |= X_PFONT_MAGIC;
2730 buf_size = 128;
2731 buffer = HeapAlloc( GetProcessHeap(), 0, buf_size );
2733 /* deal with systemwide font metrics cache */
2735 if( PROFILE_GetWineIniString( INIFontSection, INIGlobalMetrics, "", buffer, buf_size ) )
2737 fd = open( buffer, O_RDONLY );
2738 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2740 if (fontList == NULL)
2742 /* try per-user */
2743 buffer = XFONT_UserMetricsCache( buffer, &buf_size );
2744 if( buffer[0] )
2746 fd = open( buffer, O_RDONLY );
2747 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2751 if( fontList == NULL ) /* build metrics from scratch */
2753 int n_ff = XFONT_BuildMetrics(x_pattern, DefResolution, x_checksum, x_count);
2754 if( buffer[0] ) /* update cached metrics */
2756 fd = open( buffer, O_CREAT | O_TRUNC | O_RDWR, 0644 ); /* -rw-r--r-- */
2757 if( XFONT_WriteCachedMetrics( fd, x_checksum, x_count, n_ff ) == FALSE )
2759 WARN("Unable to write to fontcache '%s'\n", buffer);
2760 if( fd >= 0) remove( buffer ); /* couldn't write entire file */
2765 TSXFreeFontNames(x_pattern);
2767 /* check if we're dealing with X11 R6 server */
2769 XFontStruct* x_fs;
2770 strcpy(buffer, "-*-*-*-*-normal-*-[12 0 0 12]-*-72-*-*-*-iso8859-1");
2771 if( (x_fs = TSXLoadQueryFont(display, buffer)) )
2773 XTextCaps |= TC_SF_X_YINDEP;
2774 TSXFreeFont(display, x_fs);
2777 HeapFree(GetProcessHeap(), 0, buffer);
2779 XFONT_WindowsNames();
2780 XFONT_LoadAliases();
2781 XFONT_LoadDefaults();
2782 XFONT_LoadIgnores();
2784 /* fontList initialization is over, allocate X font cache */
2786 fontCache = (fontObject*) HeapAlloc(GetProcessHeap(), 0, fontCacheSize * sizeof(fontObject));
2787 XFONT_GrowFreeList(0, fontCacheSize - 1);
2789 TRACE("done!\n");
2791 /* update text caps parameter */
2793 pDevCaps->textCaps = XTextCaps;
2795 RAW_ASCENT = TSXInternAtom(display, "RAW_ASCENT", TRUE);
2796 RAW_DESCENT = TSXInternAtom(display, "RAW_DESCENT", TRUE);
2798 return TRUE;
2801 /**********************************************************************
2802 * XFONT_SetX11Trans
2804 static BOOL XFONT_SetX11Trans( fontObject *pfo )
2806 char *fontName;
2807 Atom nameAtom;
2808 LFD* lfd;
2810 TSXGetFontProperty( pfo->fs, XA_FONT, &nameAtom );
2811 fontName = TSXGetAtomName( display, nameAtom );
2812 lfd = LFD_Parse(fontName);
2813 if (!lfd)
2815 TSXFree(fontName);
2816 return FALSE;
2819 if (lfd->pixel_size[0] != '[') {
2820 HeapFree(GetProcessHeap(), 0, lfd);
2821 TSXFree(fontName);
2822 return FALSE;
2825 #define PX pfo->lpX11Trans
2827 sscanf(lfd->pixel_size, "[%f%f%f%f]", &PX->a, &PX->b, &PX->c, &PX->d);
2828 TSXFree(fontName);
2829 HeapFree(GetProcessHeap(), 0, lfd);
2831 TSXGetFontProperty( pfo->fs, RAW_ASCENT, &PX->RAW_ASCENT );
2832 TSXGetFontProperty( pfo->fs, RAW_DESCENT, &PX->RAW_DESCENT );
2834 PX->pixelsize = hypot(PX->a, PX->b);
2835 PX->ascent = PX->pixelsize / 1000.0 * PX->RAW_ASCENT;
2836 PX->descent = PX->pixelsize / 1000.0 * PX->RAW_DESCENT;
2838 TRACE("[%f %f %f %f] RA = %ld RD = %ld\n",
2839 PX->a, PX->b, PX->c, PX->d,
2840 PX->RAW_ASCENT, PX->RAW_DESCENT);
2842 #undef PX
2843 return TRUE;
2846 /***********************************************************************
2847 * X Device Font Objects
2849 static X_PHYSFONT XFONT_RealizeFont( const LPLOGFONT16 plf,
2850 LPCSTR* faceMatched, BOOL bSubFont,
2851 WORD internal_charset,
2852 WORD* pcharsetMatched )
2854 UINT16 checksum;
2855 INT index = 0;
2856 fontObject* pfo;
2858 pfo = XFONT_LookupCachedFont( plf, &checksum );
2859 if( !pfo )
2861 fontMatch fm;
2862 INT i;
2864 fm.pfr = NULL;
2865 fm.pfi = NULL;
2866 fm.height = 0;
2867 fm.flags = 0;
2868 fm.plf = plf;
2869 fm.internal_charset = internal_charset;
2871 if( XTextCaps & TC_SF_X_YINDEP ) fm.flags = FO_MATCH_XYINDEP;
2873 /* allocate new font cache entry */
2875 if( (pfo = XFONT_GetCacheEntry()) )
2877 /* initialize entry and load font */
2878 char lpLFD[MAX_LFD_LENGTH];
2879 UINT uRelaxLevel = 0;
2881 if(abs(plf->lfHeight) > MAX_FONT_SIZE) {
2882 ERR(
2883 "plf->lfHeight = %d, Creating a 100 pixel font and rescaling metrics \n",
2884 plf->lfHeight);
2885 pfo->rescale = fabs(plf->lfHeight / 100.0);
2886 if(plf->lfHeight > 0) plf->lfHeight = 100;
2887 else plf->lfHeight = -100;
2888 } else
2889 pfo->rescale = 1.0;
2891 XFONT_MatchDeviceFont( fontList, &fm );
2892 pfo->fr = fm.pfr;
2893 pfo->fi = fm.pfi;
2894 pfo->fr->fo_count++;
2895 pfo->fo_flags = fm.flags & ~FO_MATCH_MASK;
2897 pfo->lf = *plf;
2898 pfo->lfchecksum = checksum;
2902 LFD_ComposeLFD( pfo, fm.height, lpLFD, uRelaxLevel++ );
2903 if( (pfo->fs = TSXLoadQueryFont( display, lpLFD )) ) break;
2904 } while( uRelaxLevel );
2907 if(pfo->lf.lfEscapement != 0) {
2908 pfo->lpX11Trans = HeapAlloc(GetProcessHeap(), 0, sizeof(XFONTTRANS));
2909 if(!XFONT_SetX11Trans( pfo )) {
2910 HeapFree(GetProcessHeap(), 0, pfo->lpX11Trans);
2911 pfo->lpX11Trans = NULL;
2914 XFONT_GetLeading( &pfo->fi->df, pfo->fs,
2915 &pfo->foInternalLeading, NULL, pfo->lpX11Trans );
2916 pfo->foAvgCharWidth = (INT16)XFONT_GetAvgCharWidth(&pfo->fi->df, pfo->fs, pfo->lpX11Trans );
2917 pfo->foMaxCharWidth = (INT16)XFONT_GetMaxCharWidth(pfo->fs, pfo->lpX11Trans);
2919 /* FIXME: If we've got a soft font or
2920 * there are FO_SYNTH_... flags for the
2921 * non PROOF_QUALITY request, the engine
2922 * should rasterize characters into mono
2923 * pixmaps and store them in the pfo->lpPixmap
2924 * array (pfo->fs should be updated as well).
2925 * array (pfo->fs should be updated as well).
2926 * X11DRV_ExtTextOut() must be heavily modified
2927 * to support pixmap blitting and FO_SYNTH_...
2928 * styles.
2931 pfo->lpPixmap = NULL;
2933 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
2934 pfo->prefobjs[i] = (X_PHYSFONT)0xffffffff; /* invalid value */
2936 /* special treatment for DBCS that needs multiple fonts */
2937 /* All member of pfo must be set correctly. */
2938 if ( bSubFont == FALSE )
2940 WORD charset_sub;
2941 WORD charsetMatchedSub;
2942 LOGFONT16 lfSub;
2943 LPCSTR faceMatchedSub;
2945 for ( i = 0; i < X11FONT_REFOBJS_MAX; i++ )
2947 charset_sub = X11DRV_cptable[pfo->fi->cptable].
2948 penum_subfont_charset( i );
2949 if ( charset_sub == DEFAULT_CHARSET ) break;
2951 lfSub = *plf;
2952 lfSub.lfWidth = 0;
2953 lfSub.lfHeight = pfo->fi->df.dfPixHeight;
2954 if ( plf->lfHeight < 0 )
2955 lfSub.lfHeight = - lfSub.lfHeight;
2956 lfSub.lfCharSet = (BYTE)(charset_sub & 0xff);
2957 lfSub.lfFaceName[0] = '\0'; /* FIXME? */
2958 /* this font has sub font */
2959 if ( i == 0 ) pfo->prefobjs[0] = (X_PHYSFONT)0;
2960 pfo->prefobjs[i] =
2961 XFONT_RealizeFont( &lfSub, &faceMatchedSub,
2962 TRUE, charset_sub,
2963 &charsetMatchedSub );
2964 /* FIXME: check charsetMatchedSub */
2969 if( !pfo ) /* couldn't get a new entry, get one of the cached fonts */
2971 UINT current_score, score = (UINT)(-1);
2973 i = index = fontMRU;
2974 fm.flags |= FO_MATCH_PAF;
2977 pfo = fontCache + i;
2978 fm.pfr = pfo->fr; fm.pfi = pfo->fi;
2980 current_score = XFONT_Match( &fm );
2981 if( current_score < score ) index = i;
2983 i = pfo->lru;
2984 } while( i >= 0 );
2985 pfo = fontCache + index;
2986 goto END;
2990 /* attach at the head of the lru list */
2991 pfo->lru = fontMRU;
2992 index = fontMRU = (pfo - fontCache);
2994 END:
2995 pfo->count++;
2997 TRACE("physfont %i\n", index);
2998 *faceMatched = pfo->fi->df.dfFace;
2999 *pcharsetMatched = pfo->fi->internal_charset;
3001 return (X_PHYSFONT)(X_PFONT_MAGIC | index);
3004 /***********************************************************************
3005 * XFONT_GetFontObject
3007 fontObject* XFONT_GetFontObject( X_PHYSFONT pFont )
3009 if( CHECK_PFONT(pFont) ) return __PFONT(pFont);
3010 return NULL;
3013 /***********************************************************************
3014 * XFONT_GetFontStruct
3016 XFontStruct* XFONT_GetFontStruct( X_PHYSFONT pFont )
3018 if( CHECK_PFONT(pFont) ) return __PFONT(pFont)->fs;
3019 return NULL;
3022 /***********************************************************************
3023 * XFONT_GetFontInfo
3025 LPIFONTINFO16 XFONT_GetFontInfo( X_PHYSFONT pFont )
3027 if( CHECK_PFONT(pFont) ) return &(__PFONT(pFont)->fi->df);
3028 return NULL;
3033 /* X11DRV Interface ****************************************************
3035 * Exposed via the dc->funcs dispatch table. *
3037 ***********************************************************************/
3038 /***********************************************************************
3039 * X11DRV_FONT_SelectObject
3041 HFONT X11DRV_FONT_SelectObject( DC* dc, HFONT hfont, FONTOBJ* font )
3043 HFONT hPrevFont = 0;
3044 LOGFONT16 lf;
3045 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
3047 EnterCriticalSection( &crtsc_fonts_X11 );
3049 if( CHECK_PFONT(physDev->font) )
3050 XFONT_ReleaseCacheEntry( __PFONT(physDev->font) );
3052 lf = font->logfont;
3054 /* Make sure we don't change the sign when converting to device coords */
3055 /* FIXME - check that the other drivers do this correctly */
3056 if (lf.lfWidth)
3058 int vpt = abs(dc->vportExtX);
3059 int wnd = abs(dc->wndExtX);
3060 lf.lfWidth = (abs(lf.lfWidth) * vpt + (wnd>>1))/wnd;
3061 if (lf.lfWidth == 0)
3062 lf.lfWidth = 1; /* Minimum width */
3064 if (lf.lfHeight)
3066 int vpt = abs(dc->vportExtY);
3067 int wnd = abs(dc->wndExtY);
3068 if (lf.lfHeight > 0)
3069 lf.lfHeight = (lf.lfHeight * vpt + (wnd>>1))/wnd;
3070 else
3071 lf.lfHeight = (lf.lfHeight * vpt - (wnd>>1))/wnd;
3073 if (lf.lfHeight == 0)
3074 lf.lfHeight = MIN_FONT_SIZE;
3076 else
3077 lf.lfHeight = -(DEF_POINT_SIZE * dc->devCaps->logPixelsY + (72>>1)) / 72;
3080 /* Fixup aliases before passing to RealizeFont */
3081 /* alias = Windows name in the alias table */
3082 LPCSTR alias = XFONT_UnAlias( lf.lfFaceName );
3083 LPCSTR faceMatched;
3084 WORD charsetMatched;
3086 TRACE("hfont=%04x\n", hfont); /* to connect with the trace from RealizeFont */
3087 physDev->font = XFONT_RealizeFont( &lf, &faceMatched,
3088 FALSE, lf.lfCharSet,
3089 &charsetMatched );
3091 /* set face to the requested facename if it matched
3092 * so that GetTextFace can get the correct face name
3094 if (alias && !strcmp(faceMatched, lf.lfFaceName))
3095 strcpy( font->logfont.lfFaceName, alias );
3096 else
3097 strcpy( font->logfont.lfFaceName, faceMatched );
3100 * In X, some encodings may have the same lfFaceName.
3101 * for example:
3102 * -misc-fixed-*-iso8859-1
3103 * -misc-fixed-*-jisx0208.1990-0
3104 * so charset should be saved...
3106 font->logfont.lfCharSet = charsetMatched;
3109 hPrevFont = dc->hFont;
3110 dc->hFont = hfont;
3112 LeaveCriticalSection( &crtsc_fonts_X11 );
3114 return hPrevFont;
3118 /***********************************************************************
3120 * X11DRV_EnumDeviceFonts
3122 BOOL X11DRV_EnumDeviceFonts( HDC hdc, LPLOGFONT16 plf,
3123 DEVICEFONTENUMPROC proc, LPARAM lp )
3125 ENUMLOGFONTEX16 lf;
3126 NEWTEXTMETRIC16 tm;
3127 fontResource* pfr = fontList;
3128 BOOL b, bRet = 0;
3130 if( plf->lfFaceName[0] )
3132 /* enum all entries in this resource */
3133 pfr = XFONT_FindFIList( pfr, plf->lfFaceName );
3134 if( pfr )
3136 fontInfo* pfi;
3137 for( pfi = pfr->fi; pfi; pfi = pfi->next )
3139 /* Note: XFONT_GetFontMetric() will have to
3140 release the crit section, font list will
3141 have to be retraversed on return */
3143 if( (b = (*proc)( &lf, &tm,
3144 XFONT_GetFontMetric( pfi, &lf, &tm ), lp )) )
3145 bRet = b;
3146 else break;
3150 else /* enum first entry in each resource */
3151 for( ; pfr ; pfr = pfr->next )
3153 if(pfr->fi)
3155 if( (b = (*proc)( &lf, &tm,
3156 XFONT_GetFontMetric( pfr->fi, &lf, &tm ), lp )) )
3157 bRet = b;
3158 else break;
3161 return bRet;
3165 /***********************************************************************
3166 * X11DRV_GetTextMetrics
3168 BOOL X11DRV_GetTextMetrics(DC *dc, TEXTMETRICA *metrics)
3170 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
3172 if( CHECK_PFONT(physDev->font) )
3174 fontObject* pfo = __PFONT(physDev->font);
3175 X11DRV_cptable[pfo->fi->cptable].pGetTextMetricsA( pfo, metrics );
3177 return TRUE;
3179 return FALSE;
3183 /***********************************************************************
3184 * X11DRV_GetCharWidth
3186 BOOL X11DRV_GetCharWidth( DC *dc, UINT firstChar, UINT lastChar,
3187 LPINT buffer )
3189 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
3190 fontObject* pfo = XFONT_GetFontObject( physDev->font );
3192 if( pfo )
3194 int i;
3196 if (pfo->fs->per_char == NULL)
3197 for (i = firstChar; i <= lastChar; i++)
3198 if(pfo->lpX11Trans)
3199 *buffer++ = pfo->fs->min_bounds.attributes *
3200 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3201 else
3202 *buffer++ = pfo->fs->min_bounds.width * pfo->rescale;
3203 else
3205 XCharStruct *cs, *def;
3206 static XCharStruct __null_char = { 0, 0, 0, 0, 0, 0 };
3208 CI_GET_CHAR_INFO(pfo->fs, pfo->fs->default_char, &__null_char,
3209 def);
3211 for (i = firstChar; i <= lastChar; i++)
3213 if (i >= pfo->fs->min_char_or_byte2 &&
3214 i <= pfo->fs->max_char_or_byte2)
3216 cs = &pfo->fs->per_char[(i - pfo->fs->min_char_or_byte2)];
3217 if (CI_NONEXISTCHAR(cs)) cs = def;
3218 } else cs = def;
3219 if(pfo->lpX11Trans)
3220 *buffer++ = max(cs->attributes, 0) *
3221 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3222 else
3223 *buffer++ = max(cs->width, 0 ) * pfo->rescale;
3227 return TRUE;
3229 return FALSE;