Added support for special treatments (use 2 or more fonts, codepage
[wine.git] / graphics / x11drv / xfont.c
blob7f659fcb20a65f6a8d2a96b3fe444ff7dc02e063
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 "ldt.h"
35 #include "tweak.h"
36 #include "x11font.h"
37 #include "server.h"
39 DEFAULT_DEBUG_CHANNEL(font);
41 #define X_PFONT_MAGIC (0xFADE0000)
42 #define X_FMC_MAGIC (0x0000CAFE)
44 #define MAX_FONTS 1024*16
45 #define MAX_LFD_LENGTH 256
46 #define TILDE '~'
47 #define HYPHEN '-'
49 #define DEF_POINT_SIZE 8 /* CreateFont(0 .. ) gets this */
50 #define DEF_SCALABLE_HEIGHT 100 /* pixels */
51 #define MIN_FONT_SIZE 2 /* Min size in pixels */
52 #define MAX_FONT_SIZE 1000 /* Max size in pixels */
54 #define REMOVE_SUBSETS 1
55 #define UNMARK_SUBSETS 0
58 #define FF_FAMILY (FF_MODERN | FF_SWISS | FF_ROMAN | FF_DECORATIVE | FF_SCRIPT)
60 typedef struct __fontAlias
62 LPSTR faTypeFace;
63 LPSTR faAlias;
64 struct __fontAlias* next;
65 } fontAlias;
67 static fontAlias *aliasTable = NULL;
69 UINT16 XTextCaps = TC_OP_CHARACTER | TC_OP_STROKE |
70 TC_CP_STROKE | TC_CR_ANY |
71 TC_SA_DOUBLE | TC_SA_INTEGER | TC_SA_CONTIN |
72 TC_UA_ABLE | TC_SO_ABLE | TC_RA_ABLE;
74 /* X11R6 adds TC_SF_X_YINDEP, maybe more... */
76 static const char* INIFontMetrics = "/cachedmetrics.";
77 static const char* INIFontSection = "fonts";
78 static const char* INIAliasSection = "Alias";
79 static const char* INIIgnoreSection = "Ignore";
80 static const char* INIDefault = "Default";
81 static const char* INIDefaultFixed = "DefaultFixed";
82 static const char* INIResolution = "Resolution";
83 static const char* INIGlobalMetrics = "FontMetrics";
84 static const char* INIDefaultSerif = "DefaultSerif";
85 static const char* INIDefaultSansSerif = "DefaultSansSerif";
88 /* FIXME - are there any more Latin charsets ? */
89 #define IS_LATIN_CHARSET(ch) \
90 ((ch)==ANSI_CHARSET ||\
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)==BALTIC_CHARSET)
101 /* suffix-charset mapping tables - must be less than 254 entries long */
103 typedef struct __sufch
105 LPCSTR psuffix;
106 BYTE charset;
107 WORD codepage;
108 WORD cptable;
109 } SuffixCharset;
111 static const SuffixCharset sufch_ansi[] = {
112 { "0", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
113 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
115 static const SuffixCharset sufch_iso646[] = {
116 { "irv", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
117 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
119 static const SuffixCharset sufch_iso8859[] = {
120 { "1", ANSI_CHARSET, 28591, X11DRV_CPTABLE_SBCS },
121 { "2", EE_CHARSET, 28592, X11DRV_CPTABLE_SBCS },
122 { "3", ISO3_CHARSET, 28593, X11DRV_CPTABLE_SBCS },
123 { "4", ISO4_CHARSET, 28594, X11DRV_CPTABLE_SBCS },
124 { "5", RUSSIAN_CHARSET, 28595, X11DRV_CPTABLE_SBCS },
125 { "6", ARABIC_CHARSET, 28596, X11DRV_CPTABLE_SBCS },
126 { "7", GREEK_CHARSET, 28597, X11DRV_CPTABLE_SBCS },
127 { "8", HEBREW_CHARSET, 28598, X11DRV_CPTABLE_SBCS },
128 { "9", TURKISH_CHARSET, 28599, X11DRV_CPTABLE_SBCS },
129 { "10", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS }, /* FIXME */
130 { "11", THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }, /* FIXME */
131 { "12", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
132 { "13", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
133 { "14", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
134 { "15", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
135 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
137 static const SuffixCharset sufch_microsoft[] = {
138 { "cp1250", EE_CHARSET, 1250, X11DRV_CPTABLE_SBCS },
139 { "cp1251", RUSSIAN_CHARSET, 1251, X11DRV_CPTABLE_SBCS },
140 { "cp1252", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
141 { "cp1253", GREEK_CHARSET, 1253, X11DRV_CPTABLE_SBCS },
142 { "cp1254", TURKISH_CHARSET, 1254, X11DRV_CPTABLE_SBCS },
143 { "cp1255", HEBREW_CHARSET, 1255, X11DRV_CPTABLE_SBCS },
144 { "cp1256", ARABIC_CHARSET, 1256, X11DRV_CPTABLE_SBCS },
145 { "cp1257", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS },
146 { "fontspecific", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
147 { "symbol", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
148 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
150 static const SuffixCharset sufch_tcvn[] = {
151 { "0", TCVN_CHARSET, 1252, X11DRV_CPTABLE_SBCS }, /* FIXME */
152 { NULL, TCVN_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
154 static const SuffixCharset sufch_tis620[] = {
155 { "0", THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }, /* FIXME */
156 { NULL, THAI_CHARSET, 874, X11DRV_CPTABLE_SBCS }};
158 static const SuffixCharset sufch_viscii[] = {
159 { "1", VISCII_CHARSET, 1252, X11DRV_CPTABLE_SBCS }, /* FIXME */
160 { NULL, VISCII_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
162 static const SuffixCharset sufch_windows[] = {
163 { "1250", EE_CHARSET, 1250, X11DRV_CPTABLE_SBCS },
164 { "1251", RUSSIAN_CHARSET, 1251, X11DRV_CPTABLE_SBCS },
165 { "1252", ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS },
166 { "1253", GREEK_CHARSET, 1253, X11DRV_CPTABLE_SBCS },
167 { "1254", TURKISH_CHARSET, 1254, X11DRV_CPTABLE_SBCS },
168 { "1255", HEBREW_CHARSET, 1255, X11DRV_CPTABLE_SBCS },
169 { "1256", ARABIC_CHARSET, 1256, X11DRV_CPTABLE_SBCS },
170 { "1257", BALTIC_CHARSET, 1257, X11DRV_CPTABLE_SBCS },
171 { NULL, ANSI_CHARSET, 1252, X11DRV_CPTABLE_SBCS }};
173 static const SuffixCharset sufch_koi8[] = {
174 { "r", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
175 { "u", RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS },
176 { NULL, RUSSIAN_CHARSET, 20866, X11DRV_CPTABLE_SBCS }};
178 /* FIXME: DBCS charsets need 2 or more fonts */
179 static const SuffixCharset sufch_jisx0201[] = {
180 { "0", ANSI_CHARSET, 932, X11DRV_CPTABLE_SBCS },
181 { NULL, ANSI_CHARSET, 932, X11DRV_CPTABLE_SBCS }};
183 static const SuffixCharset sufch_jisx0208[] = {
184 { "0", SHIFTJIS_CHARSET, 932, X11DRV_CPTABLE_CP932 },
185 { NULL, SHIFTJIS_CHARSET, 932, X11DRV_CPTABLE_CP932 }};
187 static const SuffixCharset sufch_ksc5601[] = {
188 { "0", HANGEUL_CHARSET, 949, X11DRV_CPTABLE_CP949 },
189 { NULL, HANGEUL_CHARSET, 949, X11DRV_CPTABLE_CP949 }};
191 static const SuffixCharset sufch_gb2312[] = {
192 { "0", GB2312_CHARSET, 936, X11DRV_CPTABLE_CP936 },
193 { NULL, GB2312_CHARSET, 936, X11DRV_CPTABLE_CP936 }};
195 static const SuffixCharset sufch_big5[] = {
196 { "0", CHINESEBIG5_CHARSET, 950, X11DRV_CPTABLE_CP950 },
197 { NULL, CHINESEBIG5_CHARSET, 950, X11DRV_CPTABLE_CP950 }};
199 static const SuffixCharset sufch_unicode[] = {
200 { "0", DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE },
201 { NULL, DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE }};
203 static const SuffixCharset sufch_iso10646[] = {
204 { "1", DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE },
205 { NULL, DEFAULT_CHARSET, 0, X11DRV_CPTABLE_UNICODE }};
207 /* Each of these must be matched explicitly */
208 static const SuffixCharset sufch_any[] = {
209 { "fontspecific", SYMBOL_CHARSET, CP_SYMBOL, X11DRV_CPTABLE_SBCS },
210 { NULL, 0, 0, X11DRV_CPTABLE_SBCS }};
213 typedef struct __fet
215 LPSTR prefix;
216 const SuffixCharset* sufch;
217 struct __fet* next;
218 } fontEncodingTemplate;
220 /* Note: we can attach additional encoding mappings to the end
221 * of this table at runtime.
223 static fontEncodingTemplate __fETTable[] = {
224 { "ansi", sufch_ansi, &__fETTable[1] },
225 { "ascii", sufch_ansi, &__fETTable[2] },
226 { "iso646.1991", sufch_iso646, &__fETTable[3] },
227 { "iso8859", sufch_iso8859, &__fETTable[4] },
228 { "microsoft", sufch_microsoft, &__fETTable[5] },
229 { "tcvn", sufch_tcvn, &__fETTable[6] },
230 { "tis620.2533", sufch_tis620, &__fETTable[7] },
231 { "viscii1.1", sufch_viscii, &__fETTable[8] },
232 { "windows", sufch_windows, &__fETTable[9] },
233 { "koi8", sufch_koi8, &__fETTable[10]},
234 { "jisx0201.1976",sufch_jisx0201, &__fETTable[11]},
235 { "jisc6226.1978",sufch_jisx0208, &__fETTable[12]},
236 { "jisx0208.1983",sufch_jisx0208, &__fETTable[13]},
237 { "jisx0208.1990",sufch_jisx0208, &__fETTable[14]},
238 { "ksc5601.1987", sufch_ksc5601, &__fETTable[15]},
239 { "gb2312.1980", sufch_gb2312, &__fETTable[16]},
240 { "big5.et", sufch_big5, &__fETTable[17]},
241 { "unicode", sufch_unicode, &__fETTable[18]},
242 { "iso10646", sufch_iso10646, &__fETTable[19]},
243 /* NULL prefix matches anything so put it last */
244 { NULL, sufch_any, NULL },
246 static fontEncodingTemplate* fETTable = __fETTable;
248 static int DefResolution = 0;
250 static CRITICAL_SECTION crtsc_fonts_X11 = CRITICAL_SECTION_INIT;
252 static fontResource* fontList = NULL;
253 static fontObject* fontCache = NULL; /* array */
254 static int fontCacheSize = FONTCACHE;
255 static int fontLF = -1, fontMRU = -1; /* last free, most recently used */
257 #define __PFONT(pFont) ( fontCache + ((UINT)(pFont) & 0x0000FFFF) )
258 #define CHECK_PFONT(pFont) ( (((UINT)(pFont) & 0xFFFF0000) == X_PFONT_MAGIC) &&\
259 (((UINT)(pFont) & 0x0000FFFF) < fontCacheSize) )
261 static Atom RAW_ASCENT;
262 static Atom RAW_DESCENT;
264 /***********************************************************************
265 * Helper macros from X distribution
268 #define CI_NONEXISTCHAR(cs) (((cs)->width == 0) && \
269 (((cs)->rbearing|(cs)->lbearing| \
270 (cs)->ascent|(cs)->descent) == 0))
272 #define CI_GET_CHAR_INFO(fs,col,def,cs) \
274 cs = def; \
275 if (col >= fs->min_char_or_byte2 && col <= fs->max_char_or_byte2) { \
276 if (fs->per_char == NULL) { \
277 cs = &fs->min_bounds; \
278 } else { \
279 cs = &fs->per_char[(col - fs->min_char_or_byte2)]; \
280 if (CI_NONEXISTCHAR(cs)) cs = def; \
285 #define CI_GET_DEFAULT_INFO(fs,cs) \
286 CI_GET_CHAR_INFO(fs, fs->default_char, NULL, cs)
288 /***********************************************************************
289 * Checksums
291 static UINT16 __lfCheckSum( LPLOGFONT16 plf )
293 CHAR font[LF_FACESIZE];
294 UINT16 checksum = 0;
295 UINT16 i;
297 #define ptr ((UINT16*)plf)
298 for( i = 0; i < 9; i++ ) checksum ^= *ptr++;
299 #undef ptr
300 i = 0;
301 #define ptr ((CHAR*)plf)
302 do { font[i++] = tolower(*ptr++); } while (( i < LF_FACESIZE) && (*ptr) && (*ptr!=' '));
303 for( ptr = font, i >>= 1; i > 0; i-- )
304 #undef ptr
305 #define ptr ((UINT16*)plf)
306 checksum ^= *ptr++;
307 #undef ptr
308 return checksum;
311 static UINT16 __genericCheckSum( const void *ptr, int size )
313 unsigned int checksum = 0;
314 const char *p = (const char *)ptr;
315 while (size-- > 0)
316 checksum ^= (checksum << 3) + (checksum >> 29) + *p++;
318 return checksum & 0xffff;
321 /*************************************************************************
322 * LFD parse/compose routines
324 * NB. LFD_Parse will use lpFont for its own ends, so if you want to keep it
325 * make a copy first
327 * These functions also do TILDE to HYPHEN conversion
329 static LFD* LFD_Parse(LPSTR lpFont)
331 LFD* lfd;
332 char *lpch = lpFont, *lfd_fld[LFD_FIELDS], *field_start;
333 int i;
334 if (*lpch != HYPHEN)
336 WARN("font '%s' doesn't begin with '%c'\n", lpFont, HYPHEN);
337 return NULL;
340 field_start = ++lpch;
341 for( i = 0; i < LFD_FIELDS; )
343 if (*lpch == HYPHEN)
345 *lpch = '\0';
346 lfd_fld[i] = field_start;
347 i++;
348 field_start = ++lpch;
350 else if (!*lpch)
352 lfd_fld[i] = field_start;
353 i++;
354 break;
356 else if (*lpch == TILDE)
358 *lpch = HYPHEN;
359 ++lpch;
361 else
362 ++lpch;
364 /* Fill in the empty fields with NULLS */
365 for (; i< LFD_FIELDS; i++)
366 lfd_fld[i] = NULL;
367 if (*lpch)
368 WARN("Extra ignored in font '%s'\n", lpFont);
370 lfd = HeapAlloc( GetProcessHeap(), 0, sizeof(LFD) );
371 if (lfd)
373 lfd->foundry = lfd_fld[0];
374 lfd->family = lfd_fld[1];
375 lfd->weight = lfd_fld[2];
376 lfd->slant = lfd_fld[3];
377 lfd->set_width = lfd_fld[4];
378 lfd->add_style = lfd_fld[5];
379 lfd->pixel_size = lfd_fld[6];
380 lfd->point_size = lfd_fld[7];
381 lfd->resolution_x = lfd_fld[8];
382 lfd->resolution_y = lfd_fld[9];
383 lfd->spacing = lfd_fld[10];
384 lfd->average_width = lfd_fld[11];
385 lfd->charset_registry = lfd_fld[12];
386 lfd->charset_encoding = lfd_fld[13];
388 return lfd;
392 static void LFD_UnParse(LPSTR dp, UINT buf_size, LFD* lfd)
394 const char* lfd_fld[LFD_FIELDS];
395 int i;
397 if (!buf_size)
398 return; /* Dont be silly */
400 lfd_fld[0] = lfd->foundry;
401 lfd_fld[1] = lfd->family;
402 lfd_fld[2] = lfd->weight ;
403 lfd_fld[3] = lfd->slant ;
404 lfd_fld[4] = lfd->set_width ;
405 lfd_fld[5] = lfd->add_style;
406 lfd_fld[6] = lfd->pixel_size;
407 lfd_fld[7] = lfd->point_size;
408 lfd_fld[8] = lfd->resolution_x;
409 lfd_fld[9] = lfd->resolution_y ;
410 lfd_fld[10] = lfd->spacing ;
411 lfd_fld[11] = lfd->average_width ;
412 lfd_fld[12] = lfd->charset_registry ;
413 lfd_fld[13] = lfd->charset_encoding ;
415 buf_size--; /* Room for the terminator */
417 for (i = 0; i < LFD_FIELDS; i++)
419 const char* sp = lfd_fld[i];
420 if (!sp || !buf_size)
421 break;
423 *dp++ = HYPHEN;
424 buf_size--;
425 while (buf_size > 0 && *sp)
427 *dp = (*sp == HYPHEN) ? TILDE : *sp;
428 buf_size--;
429 dp++; sp++;
432 *dp = '\0';
436 static void LFD_GetWeight( fontInfo* fi, LPCSTR lpStr)
438 int j = strlen(lpStr);
439 if( j == 1 && *lpStr == '0')
440 fi->fi_flags |= FI_POLYWEIGHT;
441 else if( j == 4 )
443 if( !strcasecmp( "bold", lpStr) )
444 fi->df.dfWeight = FW_BOLD;
445 else if( !strcasecmp( "demi", lpStr) )
447 fi->fi_flags |= FI_FW_DEMI;
448 fi->df.dfWeight = FW_DEMIBOLD;
450 else if( !strcasecmp( "book", lpStr) )
452 fi->fi_flags |= FI_FW_BOOK;
453 fi->df.dfWeight = FW_REGULAR;
456 else if( j == 5 )
458 if( !strcasecmp( "light", lpStr) )
459 fi->df.dfWeight = FW_LIGHT;
460 else if( !strcasecmp( "black", lpStr) )
461 fi->df.dfWeight = FW_BLACK;
463 else if( j == 6 && !strcasecmp( "medium", lpStr) )
464 fi->df.dfWeight = FW_REGULAR;
465 else if( j == 8 && !strcasecmp( "demibold", lpStr) )
466 fi->df.dfWeight = FW_DEMIBOLD;
467 else
468 fi->df.dfWeight = FW_DONTCARE; /* FIXME: try to get something
469 * from the weight property */
472 static BOOL LFD_GetSlant( fontInfo* fi, LPCSTR lpStr)
474 int l = strlen(lpStr);
475 if( l == 1 )
477 switch( tolower( *lpStr ) )
479 case '0': fi->fi_flags |= FI_POLYSLANT; /* haven't seen this one yet */
480 default:
481 case 'r': fi->df.dfItalic = 0;
482 break;
483 case 'o':
484 fi->fi_flags |= FI_OBLIQUE;
485 case 'i': fi->df.dfItalic = 1;
486 break;
488 return FALSE;
490 return TRUE;
493 static void LFD_GetStyle( fontInfo* fi, LPCSTR lpstr, int dec_style_check)
495 int j = strlen(lpstr);
496 if( j > 3 ) /* find out is there "sans" or "script" */
498 j = 0;
500 if( strstr(lpstr, "sans") )
502 fi->df.dfPitchAndFamily |= FF_SWISS;
503 j = 1;
505 if( strstr(lpstr, "script") )
507 fi->df.dfPitchAndFamily |= FF_SCRIPT;
508 j = 1;
510 if( !j && dec_style_check )
511 fi->df.dfPitchAndFamily |= FF_DECORATIVE;
515 /*************************************************************************
516 * LFD_InitFontInfo
518 * INIT ONLY
520 * Fill in some fields in the fontInfo struct.
522 static int LFD_InitFontInfo( fontInfo* fi, const LFD* lfd, LPCSTR fullname )
524 int i, j, dec_style_check, scalability;
525 fontEncodingTemplate* boba;
526 const char* ridiculous = "font '%s' has ridiculous %s\n";
527 const char* lpstr;
529 if (!lfd->charset_registry)
531 WARN("font '%s' does not have enough fields\n", fullname);
532 return FALSE;
535 memset(fi, 0, sizeof(fontInfo) );
537 /* weight name - */
538 LFD_GetWeight( fi, lfd->weight);
540 /* slant - */
541 dec_style_check = LFD_GetSlant( fi, lfd->slant);
543 /* width name - */
544 lpstr = lfd->set_width;
545 if( strcasecmp( "normal", lpstr) ) /* XXX 'narrow', 'condensed', etc... */
546 dec_style_check = TRUE;
547 else
548 fi->fi_flags |= FI_NORMAL;
550 /* style - */
551 LFD_GetStyle(fi, lfd->add_style, dec_style_check);
553 /* pixel & decipoint height, and res_x & y */
555 scalability = 0;
557 j = strlen(lfd->pixel_size);
558 if( j == 0 || j > 3 )
560 WARN(ridiculous, fullname, "pixel_size");
561 return FALSE;
563 if( !(fi->lfd_height = atoi(lfd->pixel_size)) )
564 scalability++;
566 j = strlen(lfd->point_size);
567 if( j == 0 || j > 3 )
569 WARN(ridiculous, fullname, "point_size");
570 return FALSE;
572 if( !(atoi(lfd->point_size)) )
573 scalability++;
575 j = strlen(lfd->resolution_x);
576 if( j == 0 || j > 3 )
578 WARN(ridiculous, fullname, "resolution_x");
579 return FALSE;
581 if( !(fi->lfd_resolution = atoi(lfd->resolution_x)) )
582 scalability++;
584 j = strlen(lfd->resolution_y);
585 if( j == 0 || j > 3 )
587 WARN(ridiculous, fullname, "resolution_y");
588 return FALSE;
590 if( !(atoi(lfd->resolution_y)) )
591 scalability++;
593 /* Check scalability */
594 switch (scalability)
596 case 0: /* Bitmap */
597 break;
598 case 4: /* Scalable */
599 fi->fi_flags |= FI_SCALABLE;
600 break;
601 case 2:
602 /* #$%^!!! X11R6 mutant garbage (scalable bitmap) */
603 TRACE("Skipping scalable bitmap '%s'\n", fullname);
604 return FALSE;
605 default:
606 WARN("Font '%s' has weird scalability\n", fullname);
607 return FALSE;
610 /* spacing - */
611 lpstr = lfd->spacing;
612 switch( *lpstr )
614 case '\0':
615 WARN("font '%s' has no spacing\n", fullname);
616 return FALSE;
618 case 'p': fi->fi_flags |= FI_VARIABLEPITCH;
619 break;
620 case 'c': fi->df.dfPitchAndFamily |= FF_MODERN;
621 fi->fi_flags |= FI_FIXEDEX;
622 /* fall through */
623 case 'm': fi->fi_flags |= FI_FIXEDPITCH;
624 break;
625 default:
626 /* Of course this line does nothing */
627 fi->df.dfPitchAndFamily |= DEFAULT_PITCH | FF_DONTCARE;
630 /* average width - */
631 lpstr = lfd->average_width;
632 j = strlen(lpstr);
633 if( j == 0 || j > 3 )
635 WARN(ridiculous, fullname, "average_width");
636 return FALSE;
639 if( !(atoi(lpstr)) && !scalability )
641 WARN("font '%s' has average_width 0 but is not scalable!!\n", fullname);
642 return FALSE;
645 /* charset registry, charset encoding - */
646 lpstr = lfd->charset_registry;
647 if( strstr(lpstr, "jisx") ||
648 strstr(lpstr, "jisc") ||
649 strstr(lpstr, "ksc") ||
650 strstr(lpstr, "gb2312") ||
651 strstr(lpstr, "big5") )
653 FIXME("DBCS fonts like '%s' are not working correctly now.\n", fullname);
656 fi->df.dfCharSet = ANSI_CHARSET;
658 for( i = 0, boba = fETTable; boba; boba = boba->next, i++ )
660 if (!boba->prefix || !strcasecmp(lpstr, boba->prefix))
662 if (lfd->charset_encoding)
664 for( j = 0; boba->sufch[j].psuffix; j++ )
666 if( !strcasecmp(lfd->charset_encoding, boba->sufch[j].psuffix ))
668 fi->df.dfCharSet = boba->sufch[j].charset;
669 fi->codepage = boba->sufch[j].codepage;
670 fi->cptable = boba->sufch[j].cptable;
671 goto done;
674 if (boba->prefix)
676 WARN("font '%s' has unknown character encoding '%s'\n",
677 fullname, lfd->charset_encoding);
678 fi->df.dfCharSet = boba->sufch[j].charset;
679 fi->codepage = boba->sufch[j].codepage;
680 fi->cptable = boba->sufch[j].cptable;
681 j = 254;
682 goto done;
685 else if (boba->prefix)
687 for( j = 0; boba->sufch[j].psuffix; j++ )
689 fi->df.dfCharSet = boba->sufch[j].charset;
690 fi->codepage = boba->sufch[j].codepage;
691 fi->cptable = boba->sufch[j].cptable;
692 j = 255;
693 goto done;
697 WARN("font '%s' has unknown character set '%s'\n", fullname, lpstr);
698 return FALSE;
700 done:
701 /* i - index into fETTable
702 * j - index into suffix array for fETTable[i]
703 * except:
704 * 254 - unknown suffix
705 * 255 - no suffix at all.
707 fi->fi_encoding = 256 * (UINT16)i + (UINT16)j;
709 return TRUE;
713 /*************************************************************************
714 * LFD_AngleMatrix
716 * make a matrix suitable for LFD based on theta radians
718 static void LFD_AngleMatrix( char* buffer, int h, double theta)
720 double matrix[4];
721 matrix[0] = h*cos(theta);
722 matrix[1] = h*sin(theta);
723 matrix[2] = -h*sin(theta);
724 matrix[3] = h*cos(theta);
725 sprintf(buffer, "[%+f%+f%+f%+f]", matrix[0], matrix[1], matrix[2], matrix[3]);
728 /*************************************************************************
729 * LFD_ComposeLFD
731 * Note: uRelax is a treatment not a cure. Font mapping algorithm
732 * should be bulletproof enough to allow us to avoid hacks like
733 * this despite LFD being so braindead.
735 static BOOL LFD_ComposeLFD( const fontObject* fo,
736 INT height, LPSTR lpLFD, UINT uRelax )
738 int i, h;
739 char *any = "*";
740 char h_string[64], resx_string[64], resy_string[64];
741 LFD aLFD;
743 /* Get the worst case over with first */
745 /* RealizeFont() will call us repeatedly with increasing uRelax
746 * until XLoadFont() succeeds.
747 * to avoid an infinite loop; these will always match
749 if (uRelax >= 5)
751 if (uRelax == 5)
752 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-1" );
753 else
754 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" );
755 return TRUE;
758 /* read foundry + family from fo */
759 aLFD.foundry = fo->fr->resource->foundry;
760 aLFD.family = fo->fr->resource->family;
762 /* add weight */
763 switch( fo->fi->df.dfWeight )
765 case FW_BOLD:
766 aLFD.weight = "bold"; break;
767 case FW_REGULAR:
768 if( fo->fi->fi_flags & FI_FW_BOOK )
769 aLFD.weight = "book";
770 else
771 aLFD.weight = "medium";
772 break;
773 case FW_DEMIBOLD:
774 aLFD.weight = "demi";
775 if( !( fo->fi->fi_flags & FI_FW_DEMI) )
776 aLFD.weight = "bold";
777 break;
778 case FW_BLACK:
779 aLFD.weight = "black"; break;
780 case FW_LIGHT:
781 aLFD.weight = "light"; break;
782 default:
783 aLFD.weight = any;
786 /* add slant */
787 if( fo->fi->df.dfItalic )
788 if( fo->fi->fi_flags & FI_OBLIQUE )
789 aLFD.slant = "o";
790 else
791 aLFD.slant = "i";
792 else
793 aLFD.slant = (uRelax < 1) ? "r" : any;
795 /* add width */
796 if( fo->fi->fi_flags & FI_NORMAL )
797 aLFD.set_width = "normal";
798 else
799 aLFD.set_width = any;
801 /* ignore style */
802 aLFD.add_style = any;
804 /* add pixelheight
806 * FIXME: fill in lpXForm and lpPixmap for rotated fonts
808 if( fo->fo_flags & FO_SYNTH_HEIGHT )
809 h = fo->fi->lfd_height;
810 else
812 h = (fo->fi->lfd_height * height + (fo->fi->df.dfPixHeight>>1));
813 h /= fo->fi->df.dfPixHeight;
815 if (h < MIN_FONT_SIZE) /* Resist rounding down to 0 */
816 h = MIN_FONT_SIZE;
817 else if (h > MAX_FONT_SIZE) /* Sanity check as huge fonts can lock up the font server */
819 WARN("Huge font size %d pixels has been reduced to %d\n", h, MAX_FONT_SIZE);
820 h = MAX_FONT_SIZE;
823 if (uRelax <= 2)
824 /* handle rotated fonts */
825 if (fo->lf.lfEscapement) {
826 /* escapement is in tenths of degrees, theta is in radians */
827 double theta = M_PI*fo->lf.lfEscapement/1800.;
828 LFD_AngleMatrix(h_string, h, theta);
830 else
832 sprintf(h_string, "%d", h);
834 else
835 sprintf(h_string, "%d", fo->fi->lfd_height);
837 aLFD.pixel_size = h_string;
838 aLFD.point_size = any;
840 /* resolution_x,y, average width */
841 /* FOX ME - Why do some font servers ignore average width ?
842 * so that you have to mess around with res_y
844 aLFD.average_width = any;
845 if (uRelax <= 3)
847 sprintf(resx_string, "%d", fo->fi->lfd_resolution);
848 aLFD.resolution_x = resx_string;
850 strcpy(resy_string, resx_string);
851 if( uRelax == 0 && XTextCaps & TC_SF_X_YINDEP )
853 if( fo->lf.lfWidth && !(fo->fo_flags & FO_SYNTH_WIDTH))
855 int resy = 0.5 + fo->fi->lfd_resolution *
856 (fo->fi->df.dfAvgWidth * height) /
857 (fo->lf.lfWidth * fo->fi->df.dfPixHeight) ;
858 sprintf(resy_string, "%d", resy);
860 else
861 ; /* FIXME - synth width */
863 aLFD.resolution_y = resy_string;
865 else
867 aLFD.resolution_x = aLFD.resolution_y = any;
870 /* spacing */
872 char* w;
874 if( fo->fi->fi_flags & FI_FIXEDPITCH )
875 w = ( fo->fi->fi_flags & FI_FIXEDEX ) ? "c" : "m";
876 else
877 w = ( fo->fi->fi_flags & FI_VARIABLEPITCH ) ? "p" : any;
879 aLFD.spacing = (uRelax <= 1) ? w : any;
882 /* encoding */
884 if (uRelax <= 4)
886 fontEncodingTemplate* boba;
888 i = fo->fi->fi_encoding >> 8;
889 for( boba = fETTable; i; i--, boba = boba->next );
891 aLFD.charset_registry = boba->prefix ? boba->prefix : any;
893 i = fo->fi->fi_encoding & 255;
894 switch( i )
896 default:
897 aLFD.charset_encoding = boba->sufch[i].psuffix;
898 break;
900 case 254:
901 aLFD.charset_encoding = any;
902 break;
904 case 255: /* no suffix - it ends eg "-ascii" */
905 aLFD.charset_encoding = NULL;
906 break;
909 else
911 aLFD.charset_registry = any;
912 aLFD.charset_encoding = any;
915 LFD_UnParse(lpLFD, MAX_LFD_LENGTH, &aLFD);
917 TRACE("\tLFD(uRelax=%d): %s\n", uRelax, lpLFD );
918 return TRUE;
922 /***********************************************************************
923 * X Font Resources
925 * font info - http://www.microsoft.com/kb/articles/q65/1/23.htm
926 * Windows font metrics - http://www.microsoft.com/kb/articles/q32/6/67.htm
928 static void XFONT_GetLeading( const LPIFONTINFO16 pFI, const XFontStruct* x_fs,
929 INT16* pIL, INT16* pEL, const XFONTTRANS *XFT )
931 unsigned long height;
932 unsigned min = (unsigned char)pFI->dfFirstChar;
933 BOOL bIsLatin = IS_LATIN_CHARSET(pFI->dfCharSet);
935 if( pEL ) *pEL = 0;
937 if(XFT) {
938 Atom RAW_CAP_HEIGHT = TSXInternAtom(display, "RAW_CAP_HEIGHT", TRUE);
939 if(TSXGetFontProperty((XFontStruct*)x_fs, RAW_CAP_HEIGHT, &height))
940 *pIL = XFT->ascent -
941 (INT)(XFT->pixelsize / 1000.0 * height);
942 else
943 *pIL = 0;
944 return;
947 if( TSXGetFontProperty((XFontStruct*)x_fs, XA_CAP_HEIGHT, &height) == FALSE )
949 if( x_fs->per_char )
950 if( bIsLatin )
951 height = x_fs->per_char['X' - min].ascent;
952 else
953 if (x_fs->ascent >= x_fs->max_bounds.ascent)
954 height = x_fs->max_bounds.ascent;
955 else
957 height = x_fs->ascent;
958 if( pEL )
959 *pEL = x_fs->max_bounds.ascent - height;
961 else
962 height = x_fs->min_bounds.ascent;
965 *pIL = x_fs->ascent - height;
968 /***********************************************************************
969 * XFONT_CharWidth
971 static int XFONT_CharWidth(const XFontStruct* x_fs,
972 const XFONTTRANS *XFT, int ch)
974 if(!XFT)
975 return x_fs->per_char[ch].width;
976 else
977 return x_fs->per_char[ch].attributes * XFT->pixelsize / 1000.0;
980 /***********************************************************************
981 * XFONT_GetAvgCharWidth
983 static INT XFONT_GetAvgCharWidth( LPIFONTINFO16 pFI, const XFontStruct* x_fs,
984 const XFONTTRANS *XFT)
986 unsigned min = (unsigned char)pFI->dfFirstChar;
987 unsigned max = (unsigned char)pFI->dfLastChar;
989 INT avg;
991 if( x_fs->per_char )
993 int width = 0, chars = 0, j;
994 if( IS_LATIN_CHARSET(pFI->dfCharSet))
996 /* FIXME - should use a weighted average */
997 for( j = 0; j < 26; j++ )
998 width += XFONT_CharWidth(x_fs, XFT, 'a' + j - min) +
999 XFONT_CharWidth(x_fs, XFT, 'A' + j - min);
1000 chars = 52;
1002 else /* unweighted average of everything */
1004 for( j = 0, max -= min; j <= max; j++ )
1005 if( !CI_NONEXISTCHAR(x_fs->per_char + j) )
1007 width += XFONT_CharWidth(x_fs, XFT, j);
1008 chars++;
1011 if (chars) avg = (width + (chars>>1))/ chars;
1012 else avg = 0; /* No characters exist at all */
1014 else /* uniform width */
1015 avg = x_fs->min_bounds.width;
1017 return avg;
1020 /***********************************************************************
1021 * XFONT_GetMaxCharWidth
1023 static INT XFONT_GetMaxCharWidth(const XFontStruct* xfs, const XFONTTRANS *XFT)
1025 unsigned min = (unsigned char)xfs->min_char_or_byte2;
1026 unsigned max = (unsigned char)xfs->max_char_or_byte2;
1027 int maxwidth, j;
1029 if(!XFT || !xfs->per_char)
1030 return abs(xfs->max_bounds.width);
1032 for( j = 0, maxwidth = 0, max -= min; j <= max; j++ )
1033 if( !CI_NONEXISTCHAR(xfs->per_char + j) )
1034 if(maxwidth < xfs->per_char[j].attributes)
1035 maxwidth = xfs->per_char[j].attributes;
1037 maxwidth *= XFT->pixelsize / 1000.0;
1038 return maxwidth;
1041 /***********************************************************************
1042 * XFONT_SetFontMetric
1044 * INIT ONLY
1046 * Initializes IFONTINFO16.
1048 static void XFONT_SetFontMetric(fontInfo* fi, const fontResource* fr, XFontStruct* xfs)
1050 unsigned min, max;
1051 fi->df.dfFirstChar = (BYTE)(min = xfs->min_char_or_byte2);
1052 fi->df.dfLastChar = (BYTE)(max = xfs->max_char_or_byte2);
1054 fi->df.dfDefaultChar = (BYTE)xfs->default_char;
1055 fi->df.dfBreakChar = (BYTE)(( ' ' < min || ' ' > max) ? xfs->default_char: ' ');
1057 fi->df.dfPixHeight = (INT16)((fi->df.dfAscent = (INT16)xfs->ascent) + xfs->descent);
1058 fi->df.dfPixWidth = (xfs->per_char) ? 0 : xfs->min_bounds.width;
1060 XFONT_GetLeading( &fi->df, xfs, &fi->df.dfInternalLeading, &fi->df.dfExternalLeading, NULL );
1061 fi->df.dfAvgWidth = (INT16)XFONT_GetAvgCharWidth(&fi->df, xfs, NULL );
1062 fi->df.dfMaxWidth = (INT16)XFONT_GetMaxCharWidth(xfs, NULL);
1064 if( xfs->min_bounds.width != xfs->max_bounds.width )
1065 fi->df.dfPitchAndFamily |= TMPF_FIXED_PITCH; /* au contraire! */
1066 if( fi->fi_flags & FI_SCALABLE )
1068 fi->df.dfType = DEVICE_FONTTYPE;
1069 fi->df.dfPitchAndFamily |= TMPF_DEVICE;
1071 else if( fi->fi_flags & FI_TRUETYPE )
1072 fi->df.dfType = TRUETYPE_FONTTYPE;
1073 else
1074 fi->df.dfType = RASTER_FONTTYPE;
1076 fi->df.dfFace = fr->lfFaceName;
1079 /***********************************************************************
1080 * XFONT_GetTextMetrics
1082 * GetTextMetrics() back end.
1084 static void XFONT_GetTextMetrics( const fontObject* pfo, const LPTEXTMETRICA pTM )
1086 LPIFONTINFO16 pdf = &pfo->fi->df;
1088 if( ! pfo->lpX11Trans ) {
1089 pTM->tmAscent = pfo->fs->ascent;
1090 pTM->tmDescent = pfo->fs->descent;
1091 } else {
1092 pTM->tmAscent = pfo->lpX11Trans->ascent;
1093 pTM->tmDescent = pfo->lpX11Trans->descent;
1096 pTM->tmAscent *= pfo->rescale;
1097 pTM->tmDescent *= pfo->rescale;
1099 pTM->tmHeight = pTM->tmAscent + pTM->tmDescent;
1101 pTM->tmAveCharWidth = pfo->foAvgCharWidth * pfo->rescale;
1102 pTM->tmMaxCharWidth = pfo->foMaxCharWidth * pfo->rescale;
1104 pTM->tmInternalLeading = pfo->foInternalLeading * pfo->rescale;
1105 pTM->tmExternalLeading = pdf->dfExternalLeading * pfo->rescale;
1107 pTM->tmStruckOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT )
1108 ? 1 : pdf->dfStrikeOut;
1109 pTM->tmUnderlined = (pfo->fo_flags & FO_SYNTH_UNDERLINE )
1110 ? 1 : pdf->dfUnderline;
1112 pTM->tmOverhang = 0;
1113 if( pfo->fo_flags & FO_SYNTH_ITALIC )
1115 pTM->tmOverhang += pTM->tmHeight/3;
1116 pTM->tmItalic = 1;
1117 } else
1118 pTM->tmItalic = pdf->dfItalic;
1120 pTM->tmWeight = pdf->dfWeight;
1121 if( pfo->fo_flags & FO_SYNTH_BOLD )
1123 pTM->tmOverhang++;
1124 pTM->tmWeight += 100;
1127 pTM->tmFirstChar = pdf->dfFirstChar;
1128 pTM->tmLastChar = pdf->dfLastChar;
1129 pTM->tmDefaultChar = pdf->dfDefaultChar;
1130 pTM->tmBreakChar = pdf->dfBreakChar;
1132 pTM->tmCharSet = pdf->dfCharSet;
1133 pTM->tmPitchAndFamily = pdf->dfPitchAndFamily;
1135 pTM->tmDigitizedAspectX = pdf->dfHorizRes;
1136 pTM->tmDigitizedAspectY = pdf->dfVertRes;
1139 /***********************************************************************
1140 * XFONT_GetFontMetric
1142 * Retrieve font metric info (enumeration).
1144 static UINT XFONT_GetFontMetric( const fontInfo* pfi, const LPENUMLOGFONTEX16 pLF,
1145 const LPNEWTEXTMETRIC16 pTM )
1147 memset( pLF, 0, sizeof(*pLF) );
1148 memset( pTM, 0, sizeof(*pTM) );
1150 #define plf ((LPLOGFONT16)pLF)
1151 plf->lfHeight = pTM->tmHeight = pfi->df.dfPixHeight;
1152 plf->lfWidth = pTM->tmAveCharWidth = pfi->df.dfAvgWidth;
1153 plf->lfWeight = pTM->tmWeight = pfi->df.dfWeight;
1154 plf->lfItalic = pTM->tmItalic = pfi->df.dfItalic;
1155 plf->lfUnderline = pTM->tmUnderlined = pfi->df.dfUnderline;
1156 plf->lfStrikeOut = pTM->tmStruckOut = pfi->df.dfStrikeOut;
1157 plf->lfCharSet = pTM->tmCharSet = pfi->df.dfCharSet;
1159 /* convert pitch values */
1161 pTM->tmPitchAndFamily = pfi->df.dfPitchAndFamily;
1162 plf->lfPitchAndFamily = (pfi->df.dfPitchAndFamily & 0xF1) + 1;
1164 lstrcpynA( plf->lfFaceName, pfi->df.dfFace, LF_FACESIZE );
1165 #undef plf
1167 /* FIXME: fill in rest of plF values
1168 lstrcpynA(plF->elfFullName, , LF_FULLFACESIZE);
1169 lstrcpynA(plF->elfStyle, , LF_FACESIZE);
1170 lstrcpynA(plF->elfScript, , LF_FACESIZE);
1173 pTM->tmAscent = pfi->df.dfAscent;
1174 pTM->tmDescent = pTM->tmHeight - pTM->tmAscent;
1175 pTM->tmInternalLeading = pfi->df.dfInternalLeading;
1176 pTM->tmMaxCharWidth = pfi->df.dfMaxWidth;
1177 pTM->tmDigitizedAspectX = pfi->df.dfHorizRes;
1178 pTM->tmDigitizedAspectY = pfi->df.dfVertRes;
1180 pTM->tmFirstChar = pfi->df.dfFirstChar;
1181 pTM->tmLastChar = pfi->df.dfLastChar;
1182 pTM->tmDefaultChar = pfi->df.dfDefaultChar;
1183 pTM->tmBreakChar = pfi->df.dfBreakChar;
1185 /* return font type */
1187 return pfi->df.dfType;
1191 /***********************************************************************
1192 * XFONT_FixupFlags
1194 * INIT ONLY
1196 * dfPitchAndFamily flags for some common typefaces.
1198 static BYTE XFONT_FixupFlags( LPCSTR lfFaceName )
1200 switch( lfFaceName[0] )
1202 case 'a':
1203 case 'A': if(!strncasecmp(lfFaceName, "Arial", 5) )
1204 return FF_SWISS;
1205 break;
1206 case 'h':
1207 case 'H': if(!strcasecmp(lfFaceName, "Helvetica") )
1208 return FF_SWISS;
1209 break;
1210 case 'c':
1211 case 'C': if(!strncasecmp(lfFaceName, "Courier", 7))
1212 return FF_MODERN;
1214 if (!strcasecmp(lfFaceName, "Charter") )
1215 return FF_ROMAN;
1216 break;
1217 case 'p':
1218 case 'P': if( !strcasecmp(lfFaceName,"Palatino") )
1219 return FF_ROMAN;
1220 break;
1221 case 't':
1222 case 'T': if(!strncasecmp(lfFaceName, "Times", 5) )
1223 return FF_ROMAN;
1224 break;
1225 case 'u':
1226 case 'U': if(!strcasecmp(lfFaceName, "Utopia") )
1227 return FF_ROMAN;
1228 break;
1229 case 'z':
1230 case 'Z': if(!strcasecmp(lfFaceName, "Zapf Dingbats") )
1231 return FF_DECORATIVE;
1233 return 0;
1236 /***********************************************************************
1237 * XFONT_SameFoundryAndFamily
1239 * INIT ONLY
1241 static BOOL XFONT_SameFoundryAndFamily( const LFD* lfd1, const LFD* lfd2 )
1243 return ( !strcasecmp( lfd1->foundry, lfd2->foundry ) &&
1244 !strcasecmp( lfd1->family, lfd2->family ) );
1247 /***********************************************************************
1248 * XFONT_InitialCapitals
1250 * INIT ONLY
1252 * Upper case first letters of words & remove multiple spaces
1254 static void XFONT_InitialCapitals(LPSTR lpch)
1256 int i;
1257 BOOL up;
1258 char* lpstr = lpch;
1260 for( i = 0, up = TRUE; *lpch; lpch++, i++ )
1262 if( isspace(*lpch) )
1264 if (!up) /* Not already got one */
1266 *lpstr++ = ' ';
1267 up = TRUE;
1270 else if( isalpha(*lpch) && up )
1272 *lpstr++ = toupper(*lpch);
1273 up = FALSE;
1275 else
1277 *lpstr++ = *lpch;
1278 up = FALSE;
1282 /* Remove possible trailing space */
1283 if (up && i > 0)
1284 --lpstr;
1285 *lpstr = '\0';
1289 /***********************************************************************
1290 * XFONT_WindowsNames
1292 * INIT ONLY
1294 * Build generic Windows aliases for X font names.
1296 * -misc-fixed- -> "Fixed"
1297 * -sony-fixed- -> "Sony Fixed", etc...
1299 static void XFONT_WindowsNames(void)
1301 fontResource* fr;
1303 for( fr = fontList; fr ; fr = fr->next )
1305 fontResource* pfr;
1306 char* lpch;
1308 if( fr->fr_flags & FR_NAMESET ) continue; /* skip already assigned */
1310 for( pfr = fontList; pfr != fr ; pfr = pfr->next )
1311 if( pfr->fr_flags & FR_NAMESET )
1313 if (!strcasecmp( pfr->resource->family, fr->resource->family))
1314 break;
1317 lpch = fr->lfFaceName;
1318 snprintf( fr->lfFaceName, sizeof(fr->lfFaceName), "%s %s",
1319 /* prepend vendor name */
1320 (pfr==fr) ? "" : fr->resource->foundry,
1321 fr->resource->family);
1322 XFONT_InitialCapitals(fr->lfFaceName);
1324 BYTE bFamilyStyle = XFONT_FixupFlags( fr->lfFaceName );
1325 if( bFamilyStyle)
1327 fontInfo* fi;
1328 for( fi = fr->fi ; fi ; fi = fi->next )
1329 fi->df.dfPitchAndFamily |= bFamilyStyle;
1333 TRACE("typeface '%s'\n", fr->lfFaceName);
1335 fr->fr_flags |= FR_NAMESET;
1339 /***********************************************************************
1340 * XFONT_LoadDefaultLFD
1342 * Move lfd to the head of fontList to make it more likely to be matched
1344 static void XFONT_LoadDefaultLFD(LFD* lfd, LPCSTR fonttype)
1347 fontResource *fr, *pfr;
1348 for( fr = NULL, pfr = fontList; pfr; pfr = pfr->next )
1350 if( XFONT_SameFoundryAndFamily(pfr->resource, lfd) )
1352 if( fr )
1354 fr->next = pfr->next;
1355 pfr->next = fontList;
1356 fontList = pfr;
1358 break;
1360 fr = pfr;
1362 if (!pfr)
1363 WARN("Default %sfont '-%s-%s-' not available\n", fonttype,
1364 lfd->foundry, lfd->family);
1368 /***********************************************************************
1369 * XFONT_LoadDefault
1371 static void XFONT_LoadDefault(LPCSTR ini, LPCSTR fonttype)
1373 char buffer[MAX_LFD_LENGTH];
1375 if( PROFILE_GetWineIniString( INIFontSection, ini, "", buffer, sizeof buffer ) )
1377 if (*buffer)
1379 LFD* lfd;
1380 char* pch = buffer;
1381 while( *pch && isspace(*pch) ) pch++;
1383 TRACE("Using '%s' as default %sfont\n", pch, fonttype);
1384 lfd = LFD_Parse(pch);
1385 if (lfd && lfd->foundry && lfd->family)
1386 XFONT_LoadDefaultLFD(lfd, fonttype);
1387 else
1388 WARN("Ini section [%s]%s is malformed\n", INIFontSection, ini);
1389 HeapFree(GetProcessHeap(), 0, lfd);
1394 /***********************************************************************
1395 * XFONT_LoadDefaults
1397 static void XFONT_LoadDefaults(void)
1399 XFONT_LoadDefault(INIDefaultFixed, "fixed ");
1400 XFONT_LoadDefault(INIDefault, "");
1403 /***********************************************************************
1404 * XFONT_CreateAlias
1406 static fontAlias* XFONT_CreateAlias( LPCSTR lpTypeFace, LPCSTR lpAlias )
1408 int j;
1409 fontAlias *pfa, *prev = NULL;
1411 for(pfa = aliasTable; pfa; pfa = pfa->next)
1413 /* check if we already got one */
1414 if( !strcasecmp( pfa->faTypeFace, lpAlias ) )
1416 TRACE("redundant alias '%s' -> '%s'\n",
1417 lpAlias, lpTypeFace );
1418 return NULL;
1420 prev = pfa;
1423 j = strlen(lpTypeFace) + 1;
1424 pfa = HeapAlloc( GetProcessHeap(), 0, sizeof(fontAlias) +
1425 j + strlen(lpAlias) + 1 );
1426 if (pfa)
1428 if (!prev)
1429 aliasTable = pfa;
1430 else
1431 prev->next = pfa;
1433 pfa->next = NULL;
1434 pfa->faTypeFace = (LPSTR)(pfa + 1);
1435 strcpy( pfa->faTypeFace, lpTypeFace );
1436 pfa->faAlias = pfa->faTypeFace + j;
1437 strcpy( pfa->faAlias, lpAlias );
1439 TRACE("added alias '%s' for '%s'\n", lpAlias, lpTypeFace );
1441 return pfa;
1443 return NULL;
1447 /***********************************************************************
1448 * XFONT_LoadAlias
1450 static void XFONT_LoadAlias(const LFD* lfd, LPCSTR lpAlias, BOOL bSubst)
1452 fontResource *fr, *frMatch = NULL;
1453 if (!lfd->foundry || ! lfd->family)
1455 WARN("Malformed font resource for alias '%s'\n", lpAlias);
1456 return;
1458 for (fr = fontList; fr ; fr = fr->next)
1460 if(!strcasecmp(fr->resource->family, lpAlias))
1462 /* alias is not needed since the real font is present */
1463 TRACE("Ignoring font alias '%s' as it is already available as a real font\n", lpAlias);
1464 return;
1466 if( XFONT_SameFoundryAndFamily( fr->resource, lfd ) )
1468 frMatch = fr;
1469 break;
1473 if( frMatch )
1475 if( bSubst )
1477 fontAlias *pfa, *prev = NULL;
1479 for(pfa = aliasTable; pfa; pfa = pfa->next)
1481 /* Remove lpAlias from aliasTable - we should free the old entry */
1482 if(!strcmp(lpAlias, pfa->faAlias))
1484 if(prev)
1485 prev->next = pfa->next;
1486 else
1487 aliasTable = pfa->next;
1490 /* Update any references to the substituted font in aliasTable */
1491 if(!strcmp(frMatch->lfFaceName, pfa->faTypeFace))
1492 pfa->faTypeFace = HEAP_strdupA( GetProcessHeap(), 0, lpAlias );
1493 prev = pfa;
1496 TRACE("\tsubstituted '%s' with '%s'\n", frMatch->lfFaceName, lpAlias );
1498 lstrcpynA( frMatch->lfFaceName, lpAlias, LF_FACESIZE );
1499 frMatch->fr_flags |= FR_NAMESET;
1501 else
1503 /* create new entry in the alias table */
1504 XFONT_CreateAlias( frMatch->lfFaceName, lpAlias );
1507 else
1509 WARN("Font alias '-%s-%s-' is not available\n", lfd->foundry, lfd->family);
1513 /***********************************************************************
1514 * XFONT_LoadAliases
1516 * INIT ONLY
1518 * Create font aliases for some standard windows fonts using user's
1519 * default choice of (sans-)serif fonts
1521 * Read user-defined aliases from wine.conf. Format is as follows
1523 * Alias# = [Windows font name],[LFD font name], <substitute original name>
1525 * Example:
1526 * Alias0 = Arial, -adobe-helvetica-
1527 * Alias1 = Times New Roman, -bitstream-courier-, 1
1528 * ...
1530 * Note that from 970817 and on we have built-in alias templates that take
1531 * care of the necessary Windows typefaces.
1533 typedef struct
1535 LPSTR fatResource;
1536 LPSTR fatAlias;
1537 } aliasTemplate;
1539 static void XFONT_LoadAliases(void)
1541 char *lpResource;
1542 char buffer[MAX_LFD_LENGTH];
1543 int i = 0;
1544 LFD* lfd;
1546 /* built-ins first */
1547 PROFILE_GetWineIniString( INIFontSection, INIDefaultSerif,
1548 "-bitstream-charter-", buffer, sizeof buffer );
1549 TRACE("Using '%s' as default serif font\n", buffer);
1550 lfd = LFD_Parse(buffer);
1551 /* NB XFONT_InitialCapitals should not change these standard aliases */
1552 if (lfd)
1554 XFONT_LoadAlias( lfd, "Tms Roman", FALSE);
1555 XFONT_LoadAlias( lfd, "MS Serif", FALSE);
1556 XFONT_LoadAlias( lfd, "Times New Roman", FALSE);
1558 XFONT_LoadDefaultLFD( lfd, "serif ");
1559 HeapFree(GetProcessHeap(), 0, lfd);
1562 PROFILE_GetWineIniString( INIFontSection, INIDefaultSansSerif,
1563 "-adobe-helvetica-", buffer, sizeof buffer);
1564 TRACE("Using '%s' as default sans serif font\n", buffer);
1565 lfd = LFD_Parse(buffer);
1566 if (lfd)
1568 XFONT_LoadAlias( lfd, "Helv", FALSE);
1569 XFONT_LoadAlias( lfd, "MS Sans Serif", FALSE);
1570 XFONT_LoadAlias( lfd, "Arial", FALSE);
1572 XFONT_LoadDefaultLFD( lfd, "sans serif ");
1573 HeapFree(GetProcessHeap(), 0, lfd);
1576 /* then user specified aliases */
1579 BOOL bHaveAlias, bSubst;
1580 char subsection[32];
1581 snprintf( subsection, sizeof subsection, "%s%i", INIAliasSection, i++ );
1583 bHaveAlias = PROFILE_GetWineIniString( INIFontSection,
1584 subsection, "", buffer, sizeof buffer);
1585 if (!bHaveAlias)
1586 break;
1588 XFONT_InitialCapitals(buffer);
1589 lpResource = PROFILE_GetStringItem( buffer );
1590 bSubst = (PROFILE_GetStringItem( lpResource )) ? TRUE : FALSE;
1591 if( lpResource && *lpResource )
1593 lfd = LFD_Parse(lpResource);
1594 if (lfd)
1596 XFONT_LoadAlias(lfd, buffer, bSubst);
1597 HeapFree(GetProcessHeap(), 0, lfd);
1600 else
1601 WARN("malformed font alias '%s'\n", buffer );
1603 while(TRUE);
1606 /***********************************************************************
1607 * XFONT_UnAlias
1609 * Convert an (potential) alias into a real windows name
1612 static LPCSTR XFONT_UnAlias(char* font)
1614 if (font[0])
1616 fontAlias* fa;
1617 XFONT_InitialCapitals(font); /* to remove extra white space */
1619 for( fa = aliasTable; fa; fa = fa->next )
1620 /* use case insensitive matching to handle eg "MS Sans Serif" */
1621 if( !strcasecmp( fa->faAlias, font ) )
1623 TRACE("found alias '%s'->%s'\n", font, fa->faTypeFace );
1624 strcpy(font, fa->faTypeFace);
1625 return fa->faAlias;
1626 break;
1629 return NULL;
1632 /***********************************************************************
1633 * XFONT_RemoveFontResource
1635 * Caller should check if the font resource is in use. If it is it should
1636 * set FR_REMOVED flag to delay removal until the resource is not in use
1637 * any more.
1639 void XFONT_RemoveFontResource( fontResource** ppfr )
1641 fontResource* pfr = *ppfr;
1642 #if 0
1643 fontInfo* pfi;
1645 /* FIXME - if fonts were read from a cache, these HeapFrees will fail */
1646 while( pfr->fi )
1648 pfi = pfr->fi->next;
1649 HeapFree( GetProcessHeap(), 0, pfr->fi );
1650 pfr->fi = pfi;
1652 HeapFree( GetProcessHeap(), 0, pfr );
1653 #endif
1654 *ppfr = pfr->next;
1657 /***********************************************************************
1658 * XFONT_LoadIgnores
1660 * INIT ONLY
1662 * Removes specified fonts from the font table to prevent Wine from
1663 * using it.
1665 * Ignore# = [LFD font name]
1667 * Example:
1668 * Ignore0 = -misc-nil-
1669 * Ignore1 = -sun-open look glyph-
1670 * ...
1673 static void XFONT_LoadIgnore(char* lfdname)
1675 fontResource** ppfr;
1677 LFD* lfd = LFD_Parse(lfdname);
1678 if (lfd && lfd->foundry && lfd->family)
1680 for( ppfr = &fontList; *ppfr ; ppfr = &((*ppfr)->next))
1682 if( XFONT_SameFoundryAndFamily( (*ppfr)->resource, lfd) )
1684 TRACE("Ignoring '-%s-%s-'\n",
1685 (*ppfr)->resource->foundry, (*ppfr)->resource->family );
1687 XFONT_RemoveFontResource( ppfr );
1688 break;
1692 else
1693 WARN("Malformed font resource\n");
1695 HeapFree(GetProcessHeap(), 0, lfd);
1698 static void XFONT_LoadIgnores(void)
1700 int i = 0;
1701 char subsection[32];
1702 char buffer[MAX_LFD_LENGTH];
1704 /* Standard one that noone wants */
1705 strcpy(buffer, "-misc-nil-");
1706 XFONT_LoadIgnore(buffer);
1708 /* Others from INI file */
1711 sprintf( subsection, "%s%i", INIIgnoreSection, i++ );
1713 if( PROFILE_GetWineIniString( INIFontSection,
1714 subsection, "", buffer, sizeof buffer) )
1716 char* pch = buffer;
1717 while( *pch && isspace(*pch) ) pch++;
1718 XFONT_LoadIgnore(pch);
1720 else
1721 break;
1722 } while(TRUE);
1726 /***********************************************************************
1727 * XFONT_UserMetricsCache
1729 * Returns expanded name for the cachedmetrics file.
1730 * Now it also appends the current value of the $DISPLAY variable.
1732 static char* XFONT_UserMetricsCache( char* buffer, int* buf_size )
1734 const char *confdir = get_config_dir();
1735 const char *display_name = Options.display;
1736 int len = strlen(confdir) + strlen(INIFontMetrics) + strlen(display_name) + 2;
1738 if ((len > *buf_size) &&
1739 !(buffer = HeapReAlloc( GetProcessHeap(), 0, buffer, *buf_size = len )))
1741 ERR("out of memory\n");
1742 ExitProcess(1);
1745 sprintf( buffer, "%s/%s%s", confdir, INIFontMetrics, display_name );
1746 return buffer;
1750 /***********************************************************************
1751 * X Font Matching
1753 * Compare two fonts (only parameters set by the XFONT_InitFontInfo()).
1755 static INT XFONT_IsSubset(const fontInfo* match, const fontInfo* fi)
1757 INT m;
1759 /* 0 - keep both, 1 - keep match, -1 - keep fi */
1761 /* Compare dfItalic, Underline, Strikeout, Weight, Charset */
1762 m = (BYTE*)&fi->df.dfPixWidth - (BYTE*)&fi->df.dfItalic;
1763 if( memcmp(&match->df.dfItalic, &fi->df.dfItalic, m )) return 0;
1765 if( (!((fi->fi_flags & FI_SCALABLE) + (match->fi_flags & FI_SCALABLE))
1766 && fi->lfd_height != match->lfd_height) ||
1767 (!((fi->fi_flags & FI_POLYWEIGHT) + (match->fi_flags & FI_POLYWEIGHT))
1768 && fi->df.dfWeight != match->df.dfWeight) ) return 0;
1770 m = (int)(match->fi_flags & (FI_POLYWEIGHT | FI_SCALABLE)) -
1771 (int)(fi->fi_flags & (FI_SCALABLE | FI_POLYWEIGHT));
1773 if( m == (FI_POLYWEIGHT - FI_SCALABLE) ||
1774 m == (FI_SCALABLE - FI_POLYWEIGHT) ) return 0; /* keep both */
1775 else if( m >= 0 ) return 1; /* 'match' is better */
1777 return -1; /* 'fi' is better */
1780 /***********************************************************************
1781 * XFONT_CheckFIList
1783 * REMOVE_SUBSETS - attach new fi and purge subsets
1784 * UNMARK_SUBSETS - remove subset flags from all fi entries
1786 static void XFONT_CheckFIList( fontResource* fr, fontInfo* fi, int action)
1788 int i = 0;
1789 fontInfo* pfi, *prev;
1791 for( prev = NULL, pfi = fr->fi; pfi; )
1793 if( action == REMOVE_SUBSETS )
1795 if( pfi->fi_flags & FI_SUBSET )
1797 fontInfo* subset = pfi;
1799 i++;
1800 fr->fi_count--;
1801 if( prev ) prev->next = pfi = pfi->next;
1802 else fr->fi = pfi = pfi->next;
1803 HeapFree( GetProcessHeap(), 0, subset );
1804 continue;
1807 else pfi->fi_flags &= ~FI_SUBSET;
1809 prev = pfi;
1810 pfi = pfi->next;
1813 if( action == REMOVE_SUBSETS ) /* also add the superset */
1815 if( fi->fi_flags & FI_SCALABLE )
1817 fi->next = fr->fi;
1818 fr->fi = fi;
1820 else if( prev ) prev->next = fi; else fr->fi = fi;
1821 fr->fi_count++;
1824 if( i ) TRACE("\t purged %i subsets [%i]\n", i , fr->fi_count);
1827 /***********************************************************************
1828 * XFONT_FindFIList
1830 static fontResource* XFONT_FindFIList( fontResource* pfr, const char* pTypeFace )
1832 while( pfr )
1834 if( !strcasecmp( pfr->lfFaceName, pTypeFace ) ) break;
1835 pfr = pfr->next;
1837 /* Give the app back the font name it asked for. Encarta checks this. */
1838 if (pfr) strcpy(pfr->lfFaceName,pTypeFace);
1839 return pfr;
1842 /***********************************************************************
1843 * XFONT_FixupPointSize
1845 static void XFONT_FixupPointSize(fontInfo* fi)
1847 #define df (fi->df)
1848 df.dfHorizRes = df.dfVertRes = fi->lfd_resolution;
1849 df.dfPoints = (INT16)
1850 (((INT)(df.dfPixHeight - df.dfInternalLeading) * 72 + (df.dfVertRes >> 1)) /
1851 df.dfVertRes );
1852 #undef df
1855 /***********************************************************************
1856 * XFONT_BuildMetrics
1858 * Build font metrics from X font
1860 static int XFONT_BuildMetrics(char** x_pattern, int res, unsigned x_checksum, int x_count)
1862 int i;
1863 fontInfo* fi = NULL;
1864 fontResource* fr, *pfr;
1865 int n_ff = 0;
1867 MESSAGE("Building font metrics. This may take some time...\n");
1868 for( i = 0; i < x_count; i++ )
1870 char* typeface;
1871 LFD* lfd;
1872 int j;
1873 char buffer[MAX_LFD_LENGTH];
1874 char* lpstr;
1875 XFontStruct* x_fs;
1876 fontInfo* pfi;
1878 typeface = HEAP_strdupA(GetProcessHeap(), 0, x_pattern[i]);
1879 if (!typeface)
1880 break;
1882 lfd = LFD_Parse(typeface);
1883 if (!lfd)
1885 HeapFree(GetProcessHeap(), 0, typeface);
1886 continue;
1889 /* find a family to insert into */
1891 for( pfr = NULL, fr = fontList; fr; fr = fr->next )
1893 if( XFONT_SameFoundryAndFamily(fr->resource, lfd))
1894 break;
1895 pfr = fr;
1898 if( !fi ) fi = (fontInfo*) HeapAlloc(GetProcessHeap(), 0, sizeof(fontInfo));
1900 if( !LFD_InitFontInfo( fi, lfd, x_pattern[i]) )
1901 goto nextfont;
1903 if( !fr ) /* add new family */
1905 n_ff++;
1906 fr = (fontResource*) HeapAlloc(GetProcessHeap(), 0, sizeof(fontResource));
1907 if (fr)
1909 memset(fr, 0, sizeof(fontResource));
1911 fr->resource = (LFD*) HeapAlloc(GetProcessHeap(), 0, sizeof(LFD));
1912 memset(fr->resource, 0, sizeof(LFD));
1914 TRACE("family: -%s-%s-\n", lfd->foundry, lfd->family );
1915 fr->resource->foundry = HEAP_strdupA(GetProcessHeap(), 0, lfd->foundry);
1916 fr->resource->family = HEAP_strdupA(GetProcessHeap(), 0, lfd->family);
1917 fr->resource->weight = "";
1919 if( pfr ) pfr->next = fr;
1920 else fontList = fr;
1922 else
1923 WARN("Not enough memory for a new font family\n");
1926 /* check if we already have something better than "fi" */
1928 for( pfi = fr->fi, j = 0; pfi && j <= 0; pfi = pfi->next )
1929 if( (j = XFONT_IsSubset( pfi, fi )) < 0 )
1930 pfi->fi_flags |= FI_SUBSET; /* superseded by "fi" */
1931 if( j > 0 ) goto nextfont;
1933 /* add new font instance "fi" to the "fr" font resource */
1935 if( fi->fi_flags & FI_SCALABLE )
1937 LFD lfd1;
1938 char pxl_string[4], res_string[4];
1939 /* set scalable font height to get an basis for extrapolation */
1941 fi->lfd_height = DEF_SCALABLE_HEIGHT;
1942 fi->lfd_resolution = res;
1944 sprintf(pxl_string, "%d", fi->lfd_height);
1945 sprintf(res_string, "%d", fi->lfd_resolution);
1947 lfd1 = *lfd;
1948 lfd1.pixel_size = pxl_string;
1949 lfd1.point_size = "*";
1950 lfd1.resolution_x = res_string;
1951 lfd1.resolution_y = res_string;
1953 LFD_UnParse(buffer, sizeof buffer, &lfd1);
1955 lpstr = buffer;
1957 else lpstr = x_pattern[i];
1959 if( (x_fs = TSXLoadQueryFont(display, lpstr)) )
1961 XFONT_SetFontMetric( fi, fr, x_fs );
1962 TSXFreeFont( display, x_fs );
1964 XFONT_FixupPointSize(fi);
1966 TRACE("\t[% 2ipt] '%s'\n", fi->df.dfPoints, x_pattern[i] );
1968 XFONT_CheckFIList( fr, fi, REMOVE_SUBSETS );
1969 fi = NULL; /* preventing reuse */
1971 else
1973 ERR("failed to load %s\n", lpstr );
1975 XFONT_CheckFIList( fr, fi, UNMARK_SUBSETS );
1977 nextfont:
1978 HeapFree(GetProcessHeap(), 0, lfd);
1979 HeapFree(GetProcessHeap(), 0, typeface);
1981 if( fi ) HeapFree(GetProcessHeap(), 0, fi);
1983 /* Scan through the font list and remove FontResorce(s) (fr)
1984 * that have no associated Fontinfo(s) (fi).
1985 * This code is necessary because XFONT_ReadCachedMetrics
1986 * assumes that there is at least one fi associated with a fr.
1987 * This assumption is invalid for TT font
1988 * -altsys-ms outlook-medium-r-normal--0-0-0-0-p-0-microsoft-symbol.
1991 fr = fontList;
1993 while (!fr->fi_count)
1995 fontList = fr->next;
1997 HeapFree(GetProcessHeap(), 0, fr->resource);
1998 HeapFree(GetProcessHeap(), 0, fr);
2000 fr = fontList;
2001 n_ff--;
2004 fr = fontList;
2006 while (fr->next)
2008 if (!fr->next->fi_count)
2010 pfr = fr->next;
2011 fr->next = fr->next->next;
2013 HeapFree(GetProcessHeap(), 0, pfr->resource);
2014 HeapFree(GetProcessHeap(), 0, pfr);
2016 n_ff--;
2018 else
2019 fr = fr->next;
2022 return n_ff;
2025 /***********************************************************************
2026 * XFONT_ReadCachedMetrics
2028 * INIT ONLY
2030 static BOOL XFONT_ReadCachedMetrics( int fd, int res, unsigned x_checksum, int x_count )
2032 if( fd >= 0 )
2034 unsigned u;
2035 int i, j;
2037 /* read checksums */
2038 read( fd, &u, sizeof(unsigned) );
2039 read( fd, &i, sizeof(int) );
2041 if( u == x_checksum && i == x_count )
2043 off_t length, offset = 3 * sizeof(int);
2045 /* read total size */
2046 read( fd, &i, sizeof(int) );
2047 length = lseek( fd, 0, SEEK_END );
2049 if( length == (i + offset) )
2051 lseek( fd, offset, SEEK_SET );
2052 fontList = (fontResource*)HeapAlloc( GetProcessHeap(), 0, i);
2053 if( fontList )
2055 fontResource* pfr = fontList;
2056 fontInfo* pfi = NULL;
2058 TRACE("Reading cached font metrics:\n");
2060 read( fd, fontList, i); /* read all metrics at once */
2061 while( offset < length )
2063 offset += sizeof(fontResource) + sizeof(fontInfo);
2064 pfr->fi = pfi = (fontInfo*)(pfr + 1);
2065 j = 1;
2066 while( TRUE )
2068 if( offset > length ||
2069 pfi->cptable >= (UINT16)X11DRV_CPTABLE_COUNT ||
2070 (int)(pfi->next) != j++ ) goto fail;
2072 if( pfi->df.dfPixHeight == 0 ) goto fail;
2074 pfi->df.dfFace = pfr->lfFaceName;
2075 if( pfi->fi_flags & FI_SCALABLE )
2077 /* we can pretend we got this font for any resolution */
2078 pfi->lfd_resolution = res;
2079 XFONT_FixupPointSize(pfi);
2081 pfi->next = pfi + 1;
2083 if( j > pfr->fi_count ) break;
2085 pfi = pfi->next;
2086 offset += sizeof(fontInfo);
2088 pfi->next = NULL;
2089 if( pfr->next )
2091 pfr->next = (fontResource*)(pfi + 1);
2092 pfr = pfr->next;
2094 else break;
2096 if( pfr->next == NULL &&
2097 *(int*)(pfi + 1) == X_FMC_MAGIC )
2099 /* read LFD stubs */
2100 char* lpch = (char*)((int*)(pfi + 1) + 1);
2101 offset += sizeof(int);
2102 for( pfr = fontList; pfr; pfr = pfr->next )
2104 size_t len = strlen(lpch) + 1;
2105 TRACE("\t%s, %i instances\n", lpch, pfr->fi_count );
2106 pfr->resource = LFD_Parse(lpch);
2107 lpch += len;
2108 offset += len;
2109 if (offset > length)
2110 goto fail;
2112 close( fd );
2113 return TRUE;
2118 fail:
2119 if( fontList ) HeapFree( GetProcessHeap(), 0, fontList );
2120 fontList = NULL;
2121 close( fd );
2123 return FALSE;
2126 /***********************************************************************
2127 * XFONT_WriteCachedMetrics
2129 * INIT ONLY
2131 static BOOL XFONT_WriteCachedMetrics( int fd, unsigned x_checksum, int x_count, int n_ff )
2133 fontResource* pfr;
2134 fontInfo* pfi;
2136 if( fd >= 0 )
2138 int i, j, k;
2139 char buffer[MAX_LFD_LENGTH];
2141 /* font metrics file:
2143 * +0000 x_checksum
2144 * +0004 x_count
2145 * +0008 total size to load
2146 * +000C prepackaged font metrics
2147 * ...
2148 * +...x X_FMC_MAGIC
2149 * +...x + 4 LFD stubs
2152 write( fd, &x_checksum, sizeof(unsigned) );
2153 write( fd, &x_count, sizeof(int) );
2155 for( j = i = 0, pfr = fontList; pfr; pfr = pfr->next )
2157 LFD_UnParse(buffer, sizeof buffer, pfr->resource);
2158 i += strlen( buffer) + 1;
2159 j += pfr->fi_count;
2161 i += n_ff * sizeof(fontResource) + j * sizeof(fontInfo) + sizeof(int);
2162 write( fd, &i, sizeof(int) );
2164 TRACE("Writing font cache:\n");
2166 for( pfr = fontList; pfr; pfr = pfr->next )
2168 fontInfo fi;
2170 TRACE("\t-%s-%s-, %i instances\n", pfr->resource->foundry, pfr->resource->family, pfr->fi_count );
2172 i = write( fd, pfr, sizeof(fontResource) );
2173 if( i == sizeof(fontResource) )
2175 for( k = 1, pfi = pfr->fi; pfi; pfi = pfi->next )
2177 fi = *pfi;
2179 fi.df.dfFace = NULL;
2180 fi.next = (fontInfo*)k; /* loader checks this */
2182 j = write( fd, &fi, sizeof(fi) );
2183 k++;
2185 if( j == sizeof(fontInfo) ) continue;
2187 break;
2189 if( i == sizeof(fontResource) && j == sizeof(fontInfo) )
2191 i = j = X_FMC_MAGIC;
2192 write( fd, &i, sizeof(int) );
2193 for( pfr = fontList; pfr && i == j; pfr = pfr->next )
2195 LFD_UnParse(buffer, sizeof buffer, pfr->resource);
2196 i = strlen( buffer ) + 1;
2197 j = write( fd, buffer, i );
2200 close( fd );
2201 return ( i == j );
2203 return FALSE;
2206 /***********************************************************************
2207 * XFONT_GetPointResolution()
2209 * INIT ONLY
2211 * Here we initialize DefResolution which is used in the
2212 * XFONT_Match() penalty function. We also load the point
2213 * resolution value (higher values result in larger fonts).
2215 static int XFONT_GetPointResolution( DeviceCaps* pDevCaps )
2217 int i, j, point_resolution, num = 3;
2218 int allowed_xfont_resolutions[3] = { 72, 75, 100 };
2219 int best = 0, best_diff = 65536;
2221 point_resolution = PROFILE_GetWineIniInt( INIFontSection, INIResolution, 0 );
2222 if( !point_resolution )
2223 point_resolution = pDevCaps->logPixelsY;
2224 else
2225 pDevCaps->logPixelsX = pDevCaps->logPixelsY = point_resolution;
2228 /* FIXME We can only really guess at a best DefResolution
2229 * - this should be configurable
2231 for( i = best = 0; i < num; i++ )
2233 j = abs( point_resolution - allowed_xfont_resolutions[i] );
2234 if( j < best_diff )
2236 best = i;
2237 best_diff = j;
2240 DefResolution = allowed_xfont_resolutions[best];
2242 /* FIXME - do win95,nt40,... do this as well ? */
2243 if (TWEAK_WineLook == WIN98_LOOK)
2245 /* Lie about the screen size, so that eg MM_LOMETRIC becomes MM_logical_LOMETRIC */
2246 int denom;
2247 denom = pDevCaps->logPixelsX * 10;
2248 pDevCaps->horzSize = (pDevCaps->horzRes * 254 + (denom>>1)) / denom;
2249 denom = pDevCaps->logPixelsY * 10;
2250 pDevCaps->vertSize = (pDevCaps->vertRes * 254 + (denom>>1)) / denom;
2253 return point_resolution;
2257 /***********************************************************************
2258 * XFONT_Match
2260 * Compute the matching score between the logical font and the device font.
2262 * contributions from highest to lowest:
2263 * charset
2264 * fixed pitch
2265 * height
2266 * family flags (only when the facename is not present)
2267 * width
2268 * weight, italics, underlines, strikeouts
2270 * NOTE: you can experiment with different penalty weights to see what happens.
2271 * http://premium.microsoft.com/msdn/library/techart/f365/f36b/f37b/d38b/sa8bf.htm
2273 static UINT XFONT_Match( fontMatch* pfm )
2275 fontInfo* pfi = pfm->pfi; /* device font to match */
2276 LPLOGFONT16 plf = pfm->plf; /* wanted logical font */
2277 UINT penalty = 0;
2278 BOOL bR6 = pfm->flags & FO_MATCH_XYINDEP; /* from TextCaps */
2279 BOOL bScale = pfi->fi_flags & FI_SCALABLE;
2280 int d = 0, height;
2282 TRACE("\t[ %-2ipt h=%-3i w=%-3i %s%s]\n", pfi->df.dfPoints,
2283 pfi->df.dfPixHeight, pfi->df.dfAvgWidth,
2284 (pfi->df.dfWeight > FW_NORMAL) ? "Bold " : "Normal ",
2285 (pfi->df.dfItalic) ? "Italic" : "" );
2287 pfm->flags &= FO_MATCH_MASK;
2289 /* Charset */
2290 if (pfi->df.dfCharSet == DEFAULT_CHARSET)
2292 /* special case(unicode font) */
2293 /* priority: unmatched charset < unicode < matched charset */
2294 penalty += 0x50;
2296 else
2298 if( plf->lfCharSet == DEFAULT_CHARSET )
2300 if (pfi->df.dfCharSet != ANSI_CHARSET)
2301 penalty += 0x200;
2303 else if (plf->lfCharSet != pfi->df.dfCharSet) penalty += 0x200;
2306 /* Height */
2307 height = -1;
2309 if( plf->lfHeight > 0 )
2311 int h = pfi->df.dfPixHeight;
2312 d = h - plf->lfHeight;
2313 height = plf->lfHeight;
2315 else
2317 int h = pfi->df.dfPixHeight - pfi->df.dfInternalLeading;
2318 if (h)
2320 d = h + plf->lfHeight;
2321 height = (-plf->lfHeight * pfi->df.dfPixHeight) / h;
2323 else
2325 ERR("PixHeight == InternalLeading\n");
2326 penalty += 0x1000; /* dont want this */
2331 if( height == 0 )
2332 pfm->height = 1; /* Very small */
2333 else if( d )
2335 if( bScale )
2336 pfm->height = height;
2337 else if( (plf->lfQuality != PROOF_QUALITY) && bR6 )
2339 if( d > 0 ) /* do not shrink raster fonts */
2341 pfm->height = pfi->df.dfPixHeight;
2342 penalty += (pfi->df.dfPixHeight - height) * 0x4;
2344 else /* expand only in integer multiples */
2346 pfm->height = height - height%pfi->df.dfPixHeight;
2347 penalty += (height - pfm->height + 1) * height / pfi->df.dfPixHeight;
2350 else /* can't be scaled at all */
2352 if( plf->lfQuality != PROOF_QUALITY) pfm->flags |= FO_SYNTH_HEIGHT;
2353 pfm->height = pfi->df.dfPixHeight;
2354 penalty += (d > 0)? d * 0x8 : -d * 0x10;
2357 else
2358 pfm->height = pfi->df.dfPixHeight;
2360 /* Pitch and Family */
2361 if( pfm->flags & FO_MATCH_PAF ) {
2362 int family = plf->lfPitchAndFamily & FF_FAMILY;
2364 /* TMPF_FIXED_PITCH means exactly the opposite */
2365 if( plf->lfPitchAndFamily & FIXED_PITCH ) {
2366 if( pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH ) penalty += 0x100;
2367 } else /* Variable is the default */
2368 if( !(pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH) ) penalty += 0x2;
2370 if (family != FF_DONTCARE && family != (pfi->df.dfPitchAndFamily & FF_FAMILY) )
2371 penalty += 0x10;
2374 /* Width */
2375 if( plf->lfWidth )
2377 int h;
2378 if( bR6 || bScale ) h = 0;
2379 else
2381 /* FIXME: not complete */
2383 pfm->flags |= FO_SYNTH_WIDTH;
2384 h = abs(plf->lfWidth - (pfm->height * pfi->df.dfAvgWidth)/pfi->df.dfPixHeight);
2386 penalty += h * ( d ) ? 0x2 : 0x1 ;
2388 else if( !(pfi->fi_flags & FI_NORMAL) ) penalty++;
2390 /* Weight */
2391 if( plf->lfWeight != FW_DONTCARE )
2393 penalty += abs(plf->lfWeight - pfi->df.dfWeight) / 40;
2394 if( plf->lfWeight > pfi->df.dfWeight ) pfm->flags |= FO_SYNTH_BOLD;
2395 } else if( pfi->df.dfWeight >= FW_BOLD ) penalty++; /* choose normal by default */
2397 /* Italic */
2398 if( plf->lfItalic != pfi->df.dfItalic )
2400 penalty += 0x4;
2401 pfm->flags |= FO_SYNTH_ITALIC;
2403 /* Underline */
2404 if( plf->lfUnderline ) pfm->flags |= FO_SYNTH_UNDERLINE;
2406 /* Strikeout */
2407 if( plf->lfStrikeOut ) pfm->flags |= FO_SYNTH_STRIKEOUT;
2410 if( penalty && !bScale && pfi->lfd_resolution != DefResolution )
2411 penalty++;
2413 TRACE(" returning %i\n", penalty );
2415 return penalty;
2418 /***********************************************************************
2419 * XFONT_MatchFIList
2421 * Scan a particular font resource for the best match.
2423 static UINT XFONT_MatchFIList( fontMatch* pfm )
2425 BOOL skipRaster = (pfm->flags & FO_MATCH_NORASTER);
2426 UINT current_score, score = (UINT)(-1);
2427 fontMatch fm = *pfm;
2429 for( fm.pfi = pfm->pfr->fi; fm.pfi && score; fm.pfi = fm.pfi->next)
2431 if( skipRaster && !(fm.pfi->fi_flags & FI_SCALABLE) )
2432 continue;
2434 current_score = XFONT_Match( &fm );
2435 if( score > current_score )
2437 *pfm = fm;
2438 score = current_score;
2441 return score;
2444 /***********************************************************************
2445 * XFONT_MatchDeviceFont
2447 * Scan font resource tree.
2450 static void XFONT_MatchDeviceFont( fontResource* start, fontMatch* pfm)
2452 fontMatch fm = *pfm;
2453 UINT current_score, score = (UINT)(-1);
2454 fontResource** ppfr;
2456 TRACE("(%u) '%s' h=%i weight=%i %s\n",
2457 pfm->plf->lfCharSet, pfm->plf->lfFaceName, pfm->plf->lfHeight,
2458 pfm->plf->lfWeight, (pfm->plf->lfItalic) ? "Italic" : "" );
2460 pfm->pfi = NULL;
2461 if( fm.plf->lfFaceName[0] )
2463 fm.pfr = XFONT_FindFIList( start, fm.plf->lfFaceName);
2464 if( fm.pfr ) /* match family */
2466 TRACE("found facename '%s'\n", fm.pfr->lfFaceName );
2468 if( fm.pfr->fr_flags & FR_REMOVED )
2469 fm.pfr = 0;
2470 else
2472 XFONT_MatchFIList( &fm );
2473 *pfm = fm;
2474 if (pfm->pfi)
2475 return;
2480 /* match all available fonts */
2482 fm.flags |= FO_MATCH_PAF;
2483 for( ppfr = &fontList; *ppfr && score; ppfr = &(*ppfr)->next )
2485 if( (*ppfr)->fr_flags & FR_REMOVED )
2487 if( (*ppfr)->fo_count == 0 )
2488 XFONT_RemoveFontResource( ppfr );
2489 continue;
2492 fm.pfr = *ppfr;
2494 TRACE("%s\n", fm.pfr->lfFaceName );
2496 current_score = XFONT_MatchFIList( &fm );
2497 if( current_score < score )
2499 score = current_score;
2500 *pfm = fm;
2506 /***********************************************************************
2507 * X Font Cache
2509 static void XFONT_GrowFreeList(int start, int end)
2511 /* add all entries from 'start' up to and including 'end' */
2513 memset( fontCache + start, 0, (end - start + 1) * sizeof(fontObject) );
2515 fontCache[end].lru = fontLF;
2516 fontCache[end].count = -1;
2517 fontLF = start;
2518 while( start < end )
2520 fontCache[start].count = -1;
2521 fontCache[start].lru = start + 1;
2522 start++;
2526 static fontObject* XFONT_LookupCachedFont( const LPLOGFONT16 plf, UINT16* checksum )
2528 UINT16 cs = __lfCheckSum( plf );
2529 int i = fontMRU, prev = -1;
2531 *checksum = cs;
2532 while( i >= 0 )
2534 if( fontCache[i].lfchecksum == cs &&
2535 !(fontCache[i].fo_flags & FO_REMOVED) )
2537 /* FIXME: something more intelligent here ? */
2539 if( !memcmp( plf, &fontCache[i].lf,
2540 sizeof(LOGFONT16) - LF_FACESIZE ) &&
2541 !strcmp( plf->lfFaceName, fontCache[i].lf.lfFaceName) )
2543 /* remove temporarily from the lru list */
2544 if( prev >= 0 )
2545 fontCache[prev].lru = fontCache[i].lru;
2546 else
2547 fontMRU = (INT16)fontCache[i].lru;
2548 return (fontCache + i);
2551 prev = i;
2552 i = (INT16)fontCache[i].lru;
2554 return NULL;
2557 static fontObject* XFONT_GetCacheEntry(void)
2559 int i;
2561 if( fontLF == -1 )
2563 int prev_i, prev_j, j;
2565 TRACE("font cache is full\n");
2567 /* lookup the least recently used font */
2569 for( prev_i = prev_j = j = -1, i = fontMRU; i >= 0; i = (INT16)fontCache[i].lru )
2571 if( fontCache[i].count <= 0 &&
2572 !(fontCache[i].fo_flags & FO_SYSTEM) )
2574 prev_j = prev_i;
2575 j = i;
2577 prev_i = i;
2580 if( j >= 0 ) /* unload font */
2582 /* detach from the lru list */
2584 TRACE("\tfreeing entry %i\n", j );
2586 fontCache[j].fr->fo_count--;
2588 if( prev_j >= 0 )
2589 fontCache[prev_j].lru = fontCache[j].lru;
2590 else fontMRU = (INT16)fontCache[j].lru;
2592 /* FIXME: lpXForm, lpPixmap */
2593 if(fontCache[j].lpX11Trans)
2594 HeapFree( GetProcessHeap(), 0, fontCache[j].lpX11Trans );
2596 TSXFreeFont( display, fontCache[j].fs );
2598 memset( fontCache + j, 0, sizeof(fontObject) );
2599 return (fontCache + j);
2601 else /* expand cache */
2603 fontObject* newCache;
2605 prev_i = fontCacheSize + FONTCACHE;
2607 TRACE("\tgrowing font cache from %i to %i\n", fontCacheSize, prev_i );
2609 if( (newCache = (fontObject*)HeapReAlloc(GetProcessHeap(), 0,
2610 fontCache, prev_i)) )
2612 i = fontCacheSize;
2613 fontCacheSize = prev_i;
2614 fontCache = newCache;
2615 XFONT_GrowFreeList( i, fontCacheSize - 1);
2617 else return NULL;
2621 /* detach from the free list */
2623 i = fontLF;
2624 fontLF = (INT16)fontCache[i].lru;
2625 fontCache[i].count = 0;
2626 return (fontCache + i);
2629 static int XFONT_ReleaseCacheEntry(const fontObject* pfo)
2631 UINT u = (UINT)(pfo - fontCache);
2633 if( u < fontCacheSize ) return (--fontCache[u].count);
2634 return -1;
2637 /***********************************************************************
2638 * X11DRV_FONT_Init
2640 * Initialize font resource list and allocate font cache.
2642 BOOL X11DRV_FONT_Init( DeviceCaps* pDevCaps )
2644 char** x_pattern;
2645 unsigned x_checksum;
2646 int i,res, x_count, fd, buf_size;
2647 char *buffer;
2649 res = XFONT_GetPointResolution( pDevCaps );
2651 x_pattern = TSXListFonts(display, "*", MAX_FONTS, &x_count );
2653 TRACE("Font Mapper: initializing %i fonts [logical dpi=%i, default dpi=%i]\n",
2654 x_count, res, DefResolution);
2655 if (x_count == MAX_FONTS)
2656 MESSAGE("There may be more fonts available - try increasing the value of MAX_FONTS\n");
2658 for( i = x_checksum = 0; i < x_count; i++ )
2660 int j;
2661 #if 0
2662 printf("%i\t: %s\n", i, x_pattern[i] );
2663 #endif
2665 j = strlen( x_pattern[i] );
2666 if( j ) x_checksum ^= __genericCheckSum( x_pattern[i], j );
2668 x_checksum |= X_PFONT_MAGIC;
2669 buf_size = 128;
2670 buffer = HeapAlloc( GetProcessHeap(), 0, buf_size );
2672 /* deal with systemwide font metrics cache */
2674 if( PROFILE_GetWineIniString( INIFontSection, INIGlobalMetrics, "", buffer, buf_size ) )
2676 fd = open( buffer, O_RDONLY );
2677 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2679 if (fontList == NULL)
2681 /* try per-user */
2682 buffer = XFONT_UserMetricsCache( buffer, &buf_size );
2683 if( buffer[0] )
2685 fd = open( buffer, O_RDONLY );
2686 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2690 if( fontList == NULL ) /* build metrics from scratch */
2692 int n_ff = XFONT_BuildMetrics(x_pattern, DefResolution, x_checksum, x_count);
2693 if( buffer[0] ) /* update cached metrics */
2695 fd = open( buffer, O_CREAT | O_TRUNC | O_RDWR, 0644 ); /* -rw-r--r-- */
2696 if( XFONT_WriteCachedMetrics( fd, x_checksum, x_count, n_ff ) == FALSE )
2698 WARN("Unable to write to fontcache '%s'\n", buffer);
2699 if( fd >= 0) remove( buffer ); /* couldn't write entire file */
2704 TSXFreeFontNames(x_pattern);
2706 /* check if we're dealing with X11 R6 server */
2708 XFontStruct* x_fs;
2709 strcpy(buffer, "-*-*-*-*-normal-*-[12 0 0 12]-*-72-*-*-*-iso8859-1");
2710 if( (x_fs = TSXLoadQueryFont(display, buffer)) )
2712 XTextCaps |= TC_SF_X_YINDEP;
2713 TSXFreeFont(display, x_fs);
2716 HeapFree(GetProcessHeap(), 0, buffer);
2718 XFONT_WindowsNames();
2719 XFONT_LoadAliases();
2720 XFONT_LoadDefaults();
2721 XFONT_LoadIgnores();
2723 /* fontList initialization is over, allocate X font cache */
2725 fontCache = (fontObject*) HeapAlloc(GetProcessHeap(), 0, fontCacheSize * sizeof(fontObject));
2726 XFONT_GrowFreeList(0, fontCacheSize - 1);
2728 TRACE("done!\n");
2730 /* update text caps parameter */
2732 pDevCaps->textCaps = XTextCaps;
2734 RAW_ASCENT = TSXInternAtom(display, "RAW_ASCENT", TRUE);
2735 RAW_DESCENT = TSXInternAtom(display, "RAW_DESCENT", TRUE);
2737 return TRUE;
2740 /**********************************************************************
2741 * XFONT_SetX11Trans
2743 static BOOL XFONT_SetX11Trans( fontObject *pfo )
2745 char *fontName;
2746 Atom nameAtom;
2747 LFD* lfd;
2749 TSXGetFontProperty( pfo->fs, XA_FONT, &nameAtom );
2750 fontName = TSXGetAtomName( display, nameAtom );
2751 lfd = LFD_Parse(fontName);
2752 if (!lfd)
2754 TSXFree(fontName);
2755 return FALSE;
2758 if (lfd->pixel_size[0] != '[') {
2759 HeapFree(GetProcessHeap(), 0, lfd);
2760 TSXFree(fontName);
2761 return FALSE;
2764 #define PX pfo->lpX11Trans
2766 sscanf(lfd->pixel_size, "[%f%f%f%f]", &PX->a, &PX->b, &PX->c, &PX->d);
2767 TSXFree(fontName);
2768 HeapFree(GetProcessHeap(), 0, lfd);
2770 TSXGetFontProperty( pfo->fs, RAW_ASCENT, &PX->RAW_ASCENT );
2771 TSXGetFontProperty( pfo->fs, RAW_DESCENT, &PX->RAW_DESCENT );
2773 PX->pixelsize = hypot(PX->a, PX->b);
2774 PX->ascent = PX->pixelsize / 1000.0 * PX->RAW_ASCENT;
2775 PX->descent = PX->pixelsize / 1000.0 * PX->RAW_DESCENT;
2777 TRACE("[%f %f %f %f] RA = %ld RD = %ld\n",
2778 PX->a, PX->b, PX->c, PX->d,
2779 PX->RAW_ASCENT, PX->RAW_DESCENT);
2781 #undef PX
2782 return TRUE;
2785 /***********************************************************************
2786 * X Device Font Objects
2788 static X_PHYSFONT XFONT_RealizeFont( const LPLOGFONT16 plf, LPCSTR* faceMatched)
2790 UINT16 checksum;
2791 INT index = 0;
2792 fontObject* pfo;
2794 pfo = XFONT_LookupCachedFont( plf, &checksum );
2795 if( !pfo )
2797 fontMatch fm;
2798 INT i;
2800 fm.pfr = NULL;
2801 fm.pfi = NULL;
2802 fm.height = 0;
2803 fm.flags = 0;
2804 fm.plf = plf;
2806 if( XTextCaps & TC_SF_X_YINDEP ) fm.flags = FO_MATCH_XYINDEP;
2808 /* allocate new font cache entry */
2810 if( (pfo = XFONT_GetCacheEntry()) )
2812 /* initialize entry and load font */
2813 char lpLFD[MAX_LFD_LENGTH];
2814 UINT uRelaxLevel = 0;
2816 if(abs(plf->lfHeight) > MAX_FONT_SIZE) {
2817 ERR(
2818 "plf->lfHeight = %d, Creating a 100 pixel font and rescaling metrics \n",
2819 plf->lfHeight);
2820 pfo->rescale = fabs(plf->lfHeight / 100.0);
2821 if(plf->lfHeight > 0) plf->lfHeight = 100;
2822 else plf->lfHeight = -100;
2823 } else
2824 pfo->rescale = 1.0;
2826 XFONT_MatchDeviceFont( fontList, &fm );
2827 pfo->fr = fm.pfr;
2828 pfo->fi = fm.pfi;
2829 pfo->fr->fo_count++;
2830 pfo->fo_flags = fm.flags & ~FO_MATCH_MASK;
2832 pfo->lf = *plf;
2833 pfo->lfchecksum = checksum;
2837 LFD_ComposeLFD( pfo, fm.height, lpLFD, uRelaxLevel++ );
2838 if( (pfo->fs = TSXLoadQueryFont( display, lpLFD )) ) break;
2839 } while( uRelaxLevel );
2842 if(pfo->lf.lfEscapement != 0) {
2843 pfo->lpX11Trans = HeapAlloc(GetProcessHeap(), 0, sizeof(XFONTTRANS));
2844 if(!XFONT_SetX11Trans( pfo )) {
2845 HeapFree(GetProcessHeap(), 0, pfo->lpX11Trans);
2846 pfo->lpX11Trans = NULL;
2849 XFONT_GetLeading( &pfo->fi->df, pfo->fs,
2850 &pfo->foInternalLeading, NULL, pfo->lpX11Trans );
2851 pfo->foAvgCharWidth = (INT16)XFONT_GetAvgCharWidth(&pfo->fi->df, pfo->fs, pfo->lpX11Trans );
2852 pfo->foMaxCharWidth = (INT16)XFONT_GetMaxCharWidth(pfo->fs, pfo->lpX11Trans);
2854 /* FIXME: If we've got a soft font or
2855 * there are FO_SYNTH_... flags for the
2856 * non PROOF_QUALITY request, the engine
2857 * should rasterize characters into mono
2858 * pixmaps and store them in the pfo->lpPixmap
2859 * array (pfo->fs should be updated as well).
2860 * array (pfo->fs should be updated as well).
2861 * X11DRV_ExtTextOut() must be heavily modified
2862 * to support pixmap blitting and FO_SYNTH_...
2863 * styles.
2866 pfo->lpPixmap = NULL;
2869 if( !pfo ) /* couldn't get a new entry, get one of the cached fonts */
2871 UINT current_score, score = (UINT)(-1);
2873 i = index = fontMRU;
2874 fm.flags |= FO_MATCH_PAF;
2877 pfo = fontCache + i;
2878 fm.pfr = pfo->fr; fm.pfi = pfo->fi;
2880 current_score = XFONT_Match( &fm );
2881 if( current_score < score ) index = i;
2883 i = pfo->lru;
2884 } while( i >= 0 );
2885 pfo = fontCache + index;
2886 goto END;
2890 /* attach at the head of the lru list */
2891 pfo->lru = fontMRU;
2892 index = fontMRU = (pfo - fontCache);
2894 END:
2895 pfo->count++;
2897 TRACE("physfont %i\n", index);
2898 *faceMatched = pfo->fi->df.dfFace;
2900 return (X_PHYSFONT)(X_PFONT_MAGIC | index);
2903 /***********************************************************************
2904 * XFONT_GetFontObject
2906 fontObject* XFONT_GetFontObject( X_PHYSFONT pFont )
2908 if( CHECK_PFONT(pFont) ) return __PFONT(pFont);
2909 return NULL;
2912 /***********************************************************************
2913 * XFONT_GetFontStruct
2915 XFontStruct* XFONT_GetFontStruct( X_PHYSFONT pFont )
2917 if( CHECK_PFONT(pFont) ) return __PFONT(pFont)->fs;
2918 return NULL;
2921 /***********************************************************************
2922 * XFONT_GetFontInfo
2924 LPIFONTINFO16 XFONT_GetFontInfo( X_PHYSFONT pFont )
2926 if( CHECK_PFONT(pFont) ) return &(__PFONT(pFont)->fi->df);
2927 return NULL;
2932 /* X11DRV Interface ****************************************************
2934 * Exposed via the dc->funcs dispatch table. *
2936 ***********************************************************************/
2937 /***********************************************************************
2938 * X11DRV_FONT_SelectObject
2940 HFONT X11DRV_FONT_SelectObject( DC* dc, HFONT hfont, FONTOBJ* font )
2942 HFONT hPrevFont = 0;
2943 LOGFONT16 lf;
2944 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
2946 EnterCriticalSection( &crtsc_fonts_X11 );
2948 if( CHECK_PFONT(physDev->font) )
2949 XFONT_ReleaseCacheEntry( __PFONT(physDev->font) );
2951 lf = font->logfont;
2953 /* Make sure we don't change the sign when converting to device coords */
2954 /* FIXME - check that the other drivers do this correctly */
2955 if (lf.lfWidth)
2957 int vpt = abs(dc->vportExtX);
2958 int wnd = abs(dc->wndExtX);
2959 lf.lfWidth = (abs(lf.lfWidth) * vpt + (wnd>>1))/wnd;
2960 if (lf.lfWidth == 0)
2961 lf.lfWidth = 1; /* Minimum width */
2963 if (lf.lfHeight)
2965 int vpt = abs(dc->vportExtY);
2966 int wnd = abs(dc->wndExtY);
2967 if (lf.lfHeight > 0)
2968 lf.lfHeight = (lf.lfHeight * vpt + (wnd>>1))/wnd;
2969 else
2970 lf.lfHeight = (lf.lfHeight * vpt - (wnd>>1))/wnd;
2972 if (lf.lfHeight == 0)
2973 lf.lfHeight = MIN_FONT_SIZE;
2975 else
2976 lf.lfHeight = -(DEF_POINT_SIZE * dc->w.devCaps->logPixelsY + (72>>1)) / 72;
2979 /* Fixup aliases before passing to RealizeFont */
2980 /* alias = Windows name in the alias table */
2981 LPCSTR alias = XFONT_UnAlias( lf.lfFaceName );
2982 LPCSTR faceMatched;
2984 TRACE("hfont=%04x\n", hfont); /* to connect with the trace from RealizeFont */
2985 physDev->font = XFONT_RealizeFont( &lf, &faceMatched );
2987 /* set face to the requested facename if it matched
2988 * so that GetTextFace can get the correct face name
2990 if (alias && !strcmp(faceMatched, lf.lfFaceName))
2991 strcpy( font->logfont.lfFaceName, alias );
2992 else
2993 strcpy( font->logfont.lfFaceName, faceMatched );
2996 hPrevFont = dc->w.hFont;
2997 dc->w.hFont = hfont;
2999 LeaveCriticalSection( &crtsc_fonts_X11 );
3001 return hPrevFont;
3005 /***********************************************************************
3007 * X11DRV_EnumDeviceFonts
3009 BOOL X11DRV_EnumDeviceFonts( DC* dc, LPLOGFONT16 plf,
3010 DEVICEFONTENUMPROC proc, LPARAM lp )
3012 ENUMLOGFONTEX16 lf;
3013 NEWTEXTMETRIC16 tm;
3014 fontResource* pfr = fontList;
3015 BOOL b, bRet = 0;
3017 if( plf->lfFaceName[0] )
3019 /* enum all entries in this resource */
3020 pfr = XFONT_FindFIList( pfr, plf->lfFaceName );
3021 if( pfr )
3023 fontInfo* pfi;
3024 for( pfi = pfr->fi; pfi; pfi = pfi->next )
3026 /* Note: XFONT_GetFontMetric() will have to
3027 release the crit section, font list will
3028 have to be retraversed on return */
3030 if( (b = (*proc)( &lf, &tm,
3031 XFONT_GetFontMetric( pfi, &lf, &tm ), lp )) )
3032 bRet = b;
3033 else break;
3037 else /* enum first entry in each resource */
3038 for( ; pfr ; pfr = pfr->next )
3040 if(pfr->fi)
3042 if( (b = (*proc)( &lf, &tm,
3043 XFONT_GetFontMetric( pfr->fi, &lf, &tm ), lp )) )
3044 bRet = b;
3045 else break;
3048 return bRet;
3052 /***********************************************************************
3053 * X11DRV_GetTextMetrics
3055 BOOL X11DRV_GetTextMetrics(DC *dc, TEXTMETRICA *metrics)
3057 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
3059 if( CHECK_PFONT(physDev->font) )
3061 fontObject* pfo = __PFONT(physDev->font);
3062 XFONT_GetTextMetrics( pfo, metrics );
3064 return TRUE;
3066 return FALSE;
3070 /***********************************************************************
3071 * X11DRV_GetCharWidth
3073 BOOL X11DRV_GetCharWidth( DC *dc, UINT firstChar, UINT lastChar,
3074 LPINT buffer )
3076 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
3077 fontObject* pfo = XFONT_GetFontObject( physDev->font );
3079 if( pfo )
3081 int i;
3083 if (pfo->fs->per_char == NULL)
3084 for (i = firstChar; i <= lastChar; i++)
3085 if(pfo->lpX11Trans)
3086 *buffer++ = pfo->fs->min_bounds.attributes *
3087 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3088 else
3089 *buffer++ = pfo->fs->min_bounds.width * pfo->rescale;
3090 else
3092 XCharStruct *cs, *def;
3093 static XCharStruct __null_char = { 0, 0, 0, 0, 0, 0 };
3095 CI_GET_CHAR_INFO(pfo->fs, pfo->fs->default_char, &__null_char,
3096 def);
3098 for (i = firstChar; i <= lastChar; i++)
3100 if (i >= pfo->fs->min_char_or_byte2 &&
3101 i <= pfo->fs->max_char_or_byte2)
3103 cs = &pfo->fs->per_char[(i - pfo->fs->min_char_or_byte2)];
3104 if (CI_NONEXISTCHAR(cs)) cs = def;
3105 } else cs = def;
3106 if(pfo->lpX11Trans)
3107 *buffer++ = max(cs->attributes, 0) *
3108 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3109 else
3110 *buffer++ = max(cs->width, 0 ) * pfo->rescale;
3114 return TRUE;
3116 return FALSE;