Enhanced synchronization between playback thread and stop/close commands
[wine.git] / graphics / x11drv / xfont.c
blob1b32196bfdc9cf0aa1b4cde3e1f0f38d80220048
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 #ifndef X_DISPLAY_MISSING
13 #include <X11/Xatom.h>
14 #include "ts_xlib.h"
15 #include "x11font.h"
16 #endif /* !defined(X_DISPLAY_MISSING) */
18 #include <ctype.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <math.h>
27 #include <assert.h>
28 #include "winuser.h"
29 #include "heap.h"
30 #include "options.h"
31 #include "font.h"
32 #include "debugtools.h"
33 #include "ldt.h"
34 #include "tweak.h"
36 DEFAULT_DEBUG_CHANNEL(font)
38 #ifndef X_DISPLAY_MISSING
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* INIWinePrefix = "/.wine";
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 LPSTR psuffix;
106 BYTE charset;
107 } SuffixCharset;
109 static SuffixCharset sufch_ansi[] = {
110 { "0", ANSI_CHARSET },
111 { NULL, ANSI_CHARSET }};
113 static SuffixCharset sufch_iso646[] = {
114 { "irv", ANSI_CHARSET },
115 { NULL, SYMBOL_CHARSET }};
117 static SuffixCharset sufch_iso8859[] = {
118 { "1", ANSI_CHARSET },
119 { "2", EE_CHARSET },
120 { "3", ISO3_CHARSET },
121 { "4", ISO4_CHARSET },
122 { "5", RUSSIAN_CHARSET },
123 { "6", ARABIC_CHARSET },
124 { "7", GREEK_CHARSET },
125 { "8", HEBREW_CHARSET },
126 { "9", TURKISH_CHARSET },
127 { "10", BALTIC_CHARSET },
128 { "11", THAI_CHARSET },
129 { "12", SYMBOL_CHARSET },
130 { "13", SYMBOL_CHARSET },
131 { "14", SYMBOL_CHARSET },
132 { "15", ANSI_CHARSET },
133 { NULL, SYMBOL_CHARSET }};
135 static SuffixCharset sufch_microsoft[] = {
136 { "cp1250", EE_CHARSET },
137 { "cp1251", RUSSIAN_CHARSET },
138 { "cp1252", ANSI_CHARSET },
139 { "cp1253", GREEK_CHARSET },
140 { "cp1254", TURKISH_CHARSET },
141 { "cp1255", HEBREW_CHARSET },
142 { "cp1256", ARABIC_CHARSET },
143 { "cp1257", BALTIC_CHARSET },
144 { "fontspecific", SYMBOL_CHARSET },
145 { "symbol", SYMBOL_CHARSET },
146 { NULL, SYMBOL_CHARSET }};
148 static SuffixCharset sufch_tcvn[] = {
149 { "0", TCVN_CHARSET },
150 { NULL, TCVN_CHARSET }};
152 static SuffixCharset sufch_tis620[] = {
153 { "0", THAI_CHARSET },
154 { NULL, THAI_CHARSET }};
156 static SuffixCharset sufch_viscii[] = {
157 { "1", VISCII_CHARSET },
158 { NULL, VISCII_CHARSET }};
160 static SuffixCharset sufch_windows[] = {
161 { "1250", EE_CHARSET },
162 { "1251", RUSSIAN_CHARSET },
163 { "1252", ANSI_CHARSET },
164 { "1253", GREEK_CHARSET },
165 { "1254", TURKISH_CHARSET },
166 { "1255", HEBREW_CHARSET },
167 { "1256", ARABIC_CHARSET },
168 { "1257", BALTIC_CHARSET },
169 { NULL, BALTIC_CHARSET }}; /* CHECK/FIXME is BALTIC really the default ? */
171 /* Each of these must be matched explicitly */
172 static SuffixCharset sufch_any[] = {
173 { "fontspecific", SYMBOL_CHARSET },
174 { NULL, 0 }};
177 typedef struct __fet
179 LPSTR prefix;
180 SuffixCharset* sufch;
181 struct __fet* next;
182 } fontEncodingTemplate;
184 /* Note: we can attach additional encoding mappings to the end
185 * of this table at runtime.
187 static fontEncodingTemplate __fETTable[] = {
188 { "ansi", sufch_ansi, &__fETTable[1] },
189 { "ascii", sufch_ansi, &__fETTable[2] },
190 { "iso646.1991", sufch_iso646, &__fETTable[3] },
191 { "iso8859", sufch_iso8859, &__fETTable[4] },
192 { "microsoft", sufch_microsoft, &__fETTable[5] },
193 { "tcvn", sufch_tcvn, &__fETTable[6] },
194 { "tis620.2533", sufch_tis620, &__fETTable[7] },
195 { "viscii1.1", sufch_viscii, &__fETTable[8] },
196 { "windows", sufch_windows, &__fETTable[9] },
197 /* NULL prefix matches anything so put it last */
198 { NULL, sufch_any, NULL },
200 static fontEncodingTemplate* fETTable = __fETTable;
202 static int DefResolution = 0;
204 static CRITICAL_SECTION crtsc_fonts_X11;
206 static fontResource* fontList = NULL;
207 static fontObject* fontCache = NULL; /* array */
208 static int fontCacheSize = FONTCACHE;
209 static int fontLF = -1, fontMRU = -1; /* last free, most recently used */
211 #define __PFONT(pFont) ( fontCache + ((UINT)(pFont) & 0x0000FFFF) )
212 #define CHECK_PFONT(pFont) ( (((UINT)(pFont) & 0xFFFF0000) == X_PFONT_MAGIC) &&\
213 (((UINT)(pFont) & 0x0000FFFF) < fontCacheSize) )
215 static Atom RAW_ASCENT;
216 static Atom RAW_DESCENT;
218 /***********************************************************************
219 * Helper macros from X distribution
222 #define CI_NONEXISTCHAR(cs) (((cs)->width == 0) && \
223 (((cs)->rbearing|(cs)->lbearing| \
224 (cs)->ascent|(cs)->descent) == 0))
226 #define CI_GET_CHAR_INFO(fs,col,def,cs) \
228 cs = def; \
229 if (col >= fs->min_char_or_byte2 && col <= fs->max_char_or_byte2) { \
230 if (fs->per_char == NULL) { \
231 cs = &fs->min_bounds; \
232 } else { \
233 cs = &fs->per_char[(col - fs->min_char_or_byte2)]; \
234 if (CI_NONEXISTCHAR(cs)) cs = def; \
239 #define CI_GET_DEFAULT_INFO(fs,cs) \
240 CI_GET_CHAR_INFO(fs, fs->default_char, NULL, cs)
242 /***********************************************************************
243 * Checksums
245 static UINT16 __lfCheckSum( LPLOGFONT16 plf )
247 CHAR font[LF_FACESIZE];
248 UINT16 checksum = 0;
249 UINT16 i;
251 #define ptr ((UINT16*)plf)
252 for( i = 0; i < 9; i++ ) checksum ^= *ptr++;
253 #undef ptr
254 i = 0;
255 #define ptr ((CHAR*)plf)
256 do { font[i++] = tolower(*ptr++); } while (( i < LF_FACESIZE) && (*ptr) && (*ptr!=' '));
257 for( ptr = font, i >>= 1; i > 0; i-- )
258 #undef ptr
259 #define ptr ((UINT16*)plf)
260 checksum ^= *ptr++;
261 #undef ptr
262 return checksum;
265 static UINT16 __genericCheckSum( const void *ptr, int size )
267 unsigned int checksum = 0;
268 const char *p = (const char *)ptr;
269 while (size-- > 0)
270 checksum ^= (checksum << 3) + (checksum >> 29) + *p++;
272 return checksum & 0xffff;
275 /*************************************************************************
276 * LFD parse/compose routines
278 * NB. LFD_Parse will use lpFont for its own ends, so if you want to keep it
279 * make a copy first
281 * These functions also do TILDE to HYPHEN conversion
283 static LFD* LFD_Parse(LPSTR lpFont)
285 LFD* lfd;
286 char *lpch = lpFont, *lfd_fld[LFD_FIELDS], *field_start;
287 int i;
288 if (*lpch != HYPHEN)
290 /* WARN("font '%s' doesn't begin with '%c'\n", lpFont, HYPHEN); */
291 return NULL;
294 field_start = ++lpch;
295 for( i = 0; i < LFD_FIELDS; )
297 if (*lpch == HYPHEN)
299 *lpch = '\0';
300 lfd_fld[i] = field_start;
301 i++;
302 field_start = ++lpch;
304 else if (!*lpch)
306 lfd_fld[i] = field_start;
307 i++;
308 break;
310 else if (*lpch == TILDE)
312 *lpch = HYPHEN;
313 ++lpch;
315 else
316 ++lpch;
318 /* Fill in the empty fields with NULLS */
319 for (; i< LFD_FIELDS; i++)
320 lfd_fld[i] = NULL;
321 if (*lpch)
322 WARN("Extra ignored in font '%s'\n", lpFont);
324 lfd = HeapAlloc( SystemHeap, 0, sizeof(LFD) );
325 if (lfd)
327 lfd->foundry = lfd_fld[0];
328 lfd->family = lfd_fld[1];
329 lfd->weight = lfd_fld[2];
330 lfd->slant = lfd_fld[3];
331 lfd->set_width = lfd_fld[4];
332 lfd->add_style = lfd_fld[5];
333 lfd->pixel_size = lfd_fld[6];
334 lfd->point_size = lfd_fld[7];
335 lfd->resolution_x = lfd_fld[8];
336 lfd->resolution_y = lfd_fld[9];
337 lfd->spacing = lfd_fld[10];
338 lfd->average_width = lfd_fld[11];
339 lfd->charset_registry = lfd_fld[12];
340 lfd->charset_encoding = lfd_fld[13];
342 return lfd;
346 static void LFD_UnParse(LPSTR dp, UINT buf_size, LFD* lfd)
348 char* lfd_fld[LFD_FIELDS];
349 int i;
351 if (!buf_size)
352 return; /* Dont be silly */
354 lfd_fld[0] = lfd->foundry;
355 lfd_fld[1] = lfd->family;
356 lfd_fld[2] = lfd->weight ;
357 lfd_fld[3] = lfd->slant ;
358 lfd_fld[4] = lfd->set_width ;
359 lfd_fld[5] = lfd->add_style;
360 lfd_fld[6] = lfd->pixel_size;
361 lfd_fld[7] = lfd->point_size;
362 lfd_fld[8] = lfd->resolution_x;
363 lfd_fld[9] = lfd->resolution_y ;
364 lfd_fld[10] = lfd->spacing ;
365 lfd_fld[11] = lfd->average_width ;
366 lfd_fld[12] = lfd->charset_registry ;
367 lfd_fld[13] = lfd->charset_encoding ;
369 buf_size--; /* Room for the terminator */
371 for (i = 0; i < LFD_FIELDS; i++)
373 char* sp = lfd_fld[i];
374 if (!sp || !buf_size)
375 break;
377 *dp++ = HYPHEN;
378 buf_size--;
379 while (buf_size > 0 && *sp)
381 *dp = (*sp == HYPHEN) ? TILDE : *sp;
382 buf_size--;
383 dp++; sp++;
386 *dp = '\0';
390 static void LFD_GetWeight( fontInfo* fi, LPCSTR lpStr)
392 int j = lstrlenA(lpStr);
393 if( j == 1 && *lpStr == '0')
394 fi->fi_flags |= FI_POLYWEIGHT;
395 else if( j == 4 )
397 if( !strcasecmp( "bold", lpStr) )
398 fi->df.dfWeight = FW_BOLD;
399 else if( !strcasecmp( "demi", lpStr) )
401 fi->fi_flags |= FI_FW_DEMI;
402 fi->df.dfWeight = FW_DEMIBOLD;
404 else if( !strcasecmp( "book", lpStr) )
406 fi->fi_flags |= FI_FW_BOOK;
407 fi->df.dfWeight = FW_REGULAR;
410 else if( j == 5 )
412 if( !strcasecmp( "light", lpStr) )
413 fi->df.dfWeight = FW_LIGHT;
414 else if( !strcasecmp( "black", lpStr) )
415 fi->df.dfWeight = FW_BLACK;
417 else if( j == 6 && !strcasecmp( "medium", lpStr) )
418 fi->df.dfWeight = FW_REGULAR;
419 else if( j == 8 && !strcasecmp( "demibold", lpStr) )
420 fi->df.dfWeight = FW_DEMIBOLD;
421 else
422 fi->df.dfWeight = FW_DONTCARE; /* FIXME: try to get something
423 * from the weight property */
426 static BOOL LFD_GetSlant( fontInfo* fi, LPCSTR lpStr)
428 int l = lstrlenA(lpStr);
429 if( l == 1 )
431 switch( tolower( *lpStr ) )
433 case '0': fi->fi_flags |= FI_POLYSLANT; /* haven't seen this one yet */
434 default:
435 case 'r': fi->df.dfItalic = 0;
436 break;
437 case 'o':
438 fi->fi_flags |= FI_OBLIQUE;
439 case 'i': fi->df.dfItalic = 1;
440 break;
442 return FALSE;
444 return TRUE;
447 static void LFD_GetStyle( fontInfo* fi, LPCSTR lpstr, int dec_style_check)
449 int j = lstrlenA(lpstr);
450 if( j > 3 ) /* find out is there "sans" or "script" */
452 j = 0;
454 if( strstr(lpstr, "sans") )
456 fi->df.dfPitchAndFamily |= FF_SWISS;
457 j = 1;
459 if( strstr(lpstr, "script") )
461 fi->df.dfPitchAndFamily |= FF_SCRIPT;
462 j = 1;
464 if( !j && dec_style_check )
465 fi->df.dfPitchAndFamily |= FF_DECORATIVE;
469 /*************************************************************************
470 * LFD_InitFontInfo
472 * INIT ONLY
474 * Fill in some fields in the fontInfo struct.
476 static int LFD_InitFontInfo( fontInfo* fi, const LFD* lfd, LPCSTR fullname )
478 int i, j, dec_style_check, scalability;
479 fontEncodingTemplate* boba;
480 const char* ridiculous = "font '%s' has ridiculous %s\n";
481 char* lpstr;
483 if (!lfd->charset_registry)
485 WARN("font '%s' does not have enough fields\n", fullname);
486 return FALSE;
489 memset(fi, 0, sizeof(fontInfo) );
491 /* weight name - */
492 LFD_GetWeight( fi, lfd->weight);
494 /* slant - */
495 dec_style_check = LFD_GetSlant( fi, lfd->slant);
497 /* width name - */
498 lpstr = lfd->set_width;
499 if( strcasecmp( "normal", lpstr) ) /* XXX 'narrow', 'condensed', etc... */
500 dec_style_check = TRUE;
501 else
502 fi->fi_flags |= FI_NORMAL;
504 /* style - */
505 LFD_GetStyle(fi, lfd->add_style, dec_style_check);
507 /* pixel & decipoint height, and res_x & y */
509 scalability = 0;
511 j = strlen(lfd->pixel_size);
512 if( j == 0 || j > 3 )
514 WARN(ridiculous, fullname, "pixel_size");
515 return FALSE;
517 if( !(fi->lfd_height = atoi(lfd->pixel_size)) )
518 scalability++;
520 j = strlen(lfd->point_size);
521 if( j == 0 || j > 3 )
523 WARN(ridiculous, fullname, "point_size");
524 return FALSE;
526 if( !(atoi(lfd->point_size)) )
527 scalability++;
529 j = strlen(lfd->resolution_x);
530 if( j == 0 || j > 3 )
532 WARN(ridiculous, fullname, "resolution_x");
533 return FALSE;
535 if( !(fi->lfd_resolution = atoi(lfd->resolution_x)) )
536 scalability++;
538 j = strlen(lfd->resolution_y);
539 if( j == 0 || j > 3 )
541 WARN(ridiculous, fullname, "resolution_y");
542 return FALSE;
544 if( !(atoi(lfd->resolution_y)) )
545 scalability++;
547 /* Check scalability */
548 switch (scalability)
550 case 0: /* Bitmap */
551 break;
552 case 4: /* Scalable */
553 fi->fi_flags |= FI_SCALABLE;
554 break;
555 case 2:
556 /* #$%^!!! X11R6 mutant garbage (scalable bitmap) */
557 TRACE("Skipping scalable bitmap '%s'\n", fullname);
558 return FALSE;
559 default:
560 WARN("Font '%s' has weird scalability\n", fullname);
561 return FALSE;
564 /* spacing - */
565 lpstr = lfd->spacing;
566 switch( *lpstr )
568 case '\0':
569 WARN("font '%s' has no spacing\n", fullname);
570 return FALSE;
572 case 'p': fi->fi_flags |= FI_VARIABLEPITCH;
573 break;
574 case 'c': fi->df.dfPitchAndFamily |= FF_MODERN;
575 fi->fi_flags |= FI_FIXEDEX;
576 /* fall through */
577 case 'm': fi->fi_flags |= FI_FIXEDPITCH;
578 break;
579 default:
580 /* Of course this line does nothing */
581 fi->df.dfPitchAndFamily |= DEFAULT_PITCH | FF_DONTCARE;
584 /* average width - */
585 lpstr = lfd->average_width;
586 j = strlen(lpstr);
587 if( j == 0 || j > 3 )
589 WARN(ridiculous, fullname, "average_width");
590 return FALSE;
593 if( !(atoi(lpstr)) && !scalability )
595 WARN("font '%s' has average_width 0 but is not scalable!!\n", fullname);
596 return FALSE;
599 /* charset registry, charset encoding - */
600 lpstr = lfd->charset_registry;
601 if( strstr(lpstr, "jisx") ||
602 strstr(lpstr, "ksc") ||
603 strstr(lpstr, "gb2312") ||
604 strstr(lpstr, "big5") ||
605 strstr(lpstr, "unicode") )
607 TRACE(" 2-byte fonts like '%s' are not supported\n", fullname);
608 return FALSE;
611 fi->df.dfCharSet = ANSI_CHARSET;
613 for( i = 0, boba = fETTable; boba; boba = boba->next, i++ )
615 if (!boba->prefix || !strcasecmp(lpstr, boba->prefix))
617 if (lfd->charset_encoding)
619 for( j = 0; boba->sufch[j].psuffix; j++ )
621 if( !strcasecmp(lfd->charset_encoding, boba->sufch[j].psuffix ))
623 fi->df.dfCharSet = boba->sufch[j].charset;
624 goto done;
627 if (boba->prefix)
629 WARN("font '%s' has unknown character encoding '%s'\n",
630 fullname, lfd->charset_encoding);
631 fi->df.dfCharSet = boba->sufch[j].charset;
632 j = 254;
633 goto done;
636 else if (boba->prefix)
638 for( j = 0; boba->sufch[j].psuffix; j++ )
640 fi->df.dfCharSet = boba->sufch[j].charset;
641 j = 255;
642 goto done;
646 /* WARN("font '%s' has unknown character set '%s'\n", fullname, lpstr); */
647 return FALSE;
649 done:
650 /* i - index into fETTable
651 * j - index into suffix array for fETTable[i]
652 * except:
653 * 254 - unknown suffix
654 * 255 - no suffix at all.
656 fi->fi_encoding = 256 * (UINT16)i + (UINT16)j;
658 return TRUE;
662 /*************************************************************************
663 * LFD_AngleMatrix
665 * make a matrix suitable for LFD based on theta radians
667 static void LFD_AngleMatrix( char* buffer, int h, double theta)
669 double matrix[4];
670 matrix[0] = h*cos(theta);
671 matrix[1] = h*sin(theta);
672 matrix[2] = -h*sin(theta);
673 matrix[3] = h*cos(theta);
674 sprintf(buffer, "[%+f%+f%+f%+f]", matrix[0], matrix[1], matrix[2], matrix[3]);
677 /*************************************************************************
678 * LFD_ComposeLFD
680 * Note: uRelax is a treatment not a cure. Font mapping algorithm
681 * should be bulletproof enough to allow us to avoid hacks like
682 * this despite LFD being so braindead.
684 static BOOL LFD_ComposeLFD( const fontObject* fo,
685 INT height, LPSTR lpLFD, UINT uRelax )
687 int i, h;
688 char *any = "*";
689 char h_string[64], resx_string[64], resy_string[64];
690 LFD aLFD;
692 /* Get the worst case over with first */
694 /* RealizeFont() will call us repeatedly with increasing uRelax
695 * until XLoadFont() succeeds.
696 * to avoid an infinite loop; these will always match
698 if (uRelax >= 5)
700 if (uRelax == 5)
701 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-iso8859-1" );
702 else
703 sprintf( lpLFD, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" );
704 return TRUE;
707 /* read foundry + family from fo */
708 aLFD.foundry = fo->fr->resource->foundry;
709 aLFD.family = fo->fr->resource->family;
711 /* add weight */
712 switch( fo->fi->df.dfWeight )
714 case FW_BOLD:
715 aLFD.weight = "bold"; break;
716 case FW_REGULAR:
717 if( fo->fi->fi_flags & FI_FW_BOOK )
718 aLFD.weight = "book";
719 else
720 aLFD.weight = "medium";
721 break;
722 case FW_DEMIBOLD:
723 aLFD.weight = "demi";
724 if( !( fo->fi->fi_flags & FI_FW_DEMI) )
725 aLFD.weight = "bold";
726 break;
727 case FW_BLACK:
728 aLFD.weight = "black"; break;
729 case FW_LIGHT:
730 aLFD.weight = "light"; break;
731 default:
732 aLFD.weight = any;
735 /* add slant */
736 if( fo->fi->df.dfItalic )
737 if( fo->fi->fi_flags & FI_OBLIQUE )
738 aLFD.slant = "o";
739 else
740 aLFD.slant = "i";
741 else
742 aLFD.slant = (uRelax < 1) ? "r" : any;
744 /* add width */
745 if( fo->fi->fi_flags & FI_NORMAL )
746 aLFD.set_width = "normal";
747 else
748 aLFD.set_width = any;
750 /* ignore style */
751 aLFD.add_style = any;
753 /* add pixelheight
755 * FIXME: fill in lpXForm and lpPixmap for rotated fonts
757 if( fo->fo_flags & FO_SYNTH_HEIGHT )
758 h = fo->fi->lfd_height;
759 else
761 h = (fo->fi->lfd_height * height + (fo->fi->df.dfPixHeight>>1));
762 h /= fo->fi->df.dfPixHeight;
764 if (h < MIN_FONT_SIZE) /* Resist rounding down to 0 */
765 h = MIN_FONT_SIZE;
766 else if (h > MAX_FONT_SIZE) /* Sanity check as huge fonts can lock up the font server */
768 WARN("Huge font size %d pixels has been reduced to %d\n", h, MAX_FONT_SIZE);
769 h = MAX_FONT_SIZE;
772 if (uRelax <= 2)
773 /* handle rotated fonts */
774 if (fo->lf.lfEscapement) {
775 /* escapement is in tenths of degrees, theta is in radians */
776 double theta = M_PI*fo->lf.lfEscapement/1800.;
777 LFD_AngleMatrix(h_string, h, theta);
779 else
781 sprintf(h_string, "%d", h);
783 else
784 sprintf(h_string, "%d", fo->fi->lfd_height);
786 aLFD.pixel_size = h_string;
787 aLFD.point_size = any;
789 /* resolution_x,y, average width */
790 /* FOX ME - Why do some font servers ignore average width ?
791 * so that you have to mess around with res_y
793 aLFD.average_width = any;
794 if (uRelax <= 3)
796 sprintf(resx_string, "%d", fo->fi->lfd_resolution);
797 aLFD.resolution_x = resx_string;
799 strcpy(resy_string, resx_string);
800 if( uRelax == 0 && XTextCaps & TC_SF_X_YINDEP )
802 if( fo->lf.lfWidth && !(fo->fo_flags & FO_SYNTH_WIDTH))
804 int resy = 0.5 + fo->fi->lfd_resolution *
805 (fo->fi->df.dfAvgWidth * height) /
806 (fo->lf.lfWidth * fo->fi->df.dfPixHeight) ;
807 sprintf(resy_string, "%d", resy);
809 else
810 ; /* FIXME - synth width */
812 aLFD.resolution_y = resy_string;
814 else
816 aLFD.resolution_x = aLFD.resolution_y = any;
819 /* spacing */
821 char* w;
823 if( fo->fi->fi_flags & FI_FIXEDPITCH )
824 w = ( fo->fi->fi_flags & FI_FIXEDEX ) ? "c" : "m";
825 else
826 w = ( fo->fi->fi_flags & FI_VARIABLEPITCH ) ? "p" : any;
828 aLFD.spacing = (uRelax <= 1) ? w : any;
831 /* encoding */
833 if (uRelax <= 4)
835 fontEncodingTemplate* boba;
837 i = fo->fi->fi_encoding >> 8;
838 for( boba = fETTable; i; i--, boba = boba->next );
840 aLFD.charset_registry = boba->prefix ? boba->prefix : any;
842 i = fo->fi->fi_encoding & 255;
843 switch( i )
845 default:
846 aLFD.charset_encoding = boba->sufch[i].psuffix;
847 break;
849 case 254:
850 aLFD.charset_encoding = any;
851 break;
853 case 255: /* no suffix - it ends eg "-ascii" */
854 aLFD.charset_encoding = NULL;
855 break;
858 else
860 aLFD.charset_registry = any;
861 aLFD.charset_encoding = any;
864 LFD_UnParse(lpLFD, MAX_LFD_LENGTH, &aLFD);
866 TRACE("\tLFD(uRelax=%d): %s\n", uRelax, lpLFD );
867 return TRUE;
871 /***********************************************************************
872 * X Font Resources
874 * font info - http://www.microsoft.com/kb/articles/q65/1/23.htm
875 * Windows font metrics - http://www.microsoft.com/kb/articles/q32/6/67.htm
877 static void XFONT_GetLeading( const LPIFONTINFO16 pFI, const XFontStruct* x_fs,
878 INT16* pIL, INT16* pEL, const XFONTTRANS *XFT )
880 unsigned long height;
881 unsigned min = (unsigned char)pFI->dfFirstChar;
882 BOOL bIsLatin = IS_LATIN_CHARSET(pFI->dfCharSet);
884 if( pEL ) *pEL = 0;
886 if(XFT) {
887 Atom RAW_CAP_HEIGHT = TSXInternAtom(display, "RAW_CAP_HEIGHT", TRUE);
888 if(TSXGetFontProperty((XFontStruct*)x_fs, RAW_CAP_HEIGHT, &height))
889 *pIL = XFT->ascent -
890 (INT)(XFT->pixelsize / 1000.0 * height);
891 else
892 *pIL = 0;
893 return;
896 if( TSXGetFontProperty((XFontStruct*)x_fs, XA_CAP_HEIGHT, &height) == FALSE )
898 if( x_fs->per_char )
899 if( bIsLatin )
900 height = x_fs->per_char['X' - min].ascent;
901 else
902 if (x_fs->ascent >= x_fs->max_bounds.ascent)
903 height = x_fs->max_bounds.ascent;
904 else
906 height = x_fs->ascent;
907 if( pEL )
908 *pEL = x_fs->max_bounds.ascent - height;
910 else
911 height = x_fs->min_bounds.ascent;
914 *pIL = x_fs->ascent - height;
917 /***********************************************************************
918 * XFONT_CharWidth
920 static int XFONT_CharWidth(const XFontStruct* x_fs,
921 const XFONTTRANS *XFT, int ch)
923 if(!XFT)
924 return x_fs->per_char[ch].width;
925 else
926 return x_fs->per_char[ch].attributes * XFT->pixelsize / 1000.0;
929 /***********************************************************************
930 * XFONT_GetAvgCharWidth
932 static INT XFONT_GetAvgCharWidth( LPIFONTINFO16 pFI, const XFontStruct* x_fs,
933 const XFONTTRANS *XFT)
935 unsigned min = (unsigned char)pFI->dfFirstChar;
936 unsigned max = (unsigned char)pFI->dfLastChar;
938 INT avg;
940 if( x_fs->per_char )
942 int width = 0, chars = 0, j;
943 if( IS_LATIN_CHARSET(pFI->dfCharSet))
945 /* FIXME - should use a weighted average */
946 for( j = 0; j < 26; j++ )
947 width += XFONT_CharWidth(x_fs, XFT, 'a' + j - min) +
948 XFONT_CharWidth(x_fs, XFT, 'A' + j - min);
949 chars = 52;
951 else /* unweighted average of everything */
953 for( j = 0, max -= min; j <= max; j++ )
954 if( !CI_NONEXISTCHAR(x_fs->per_char + j) )
956 width += XFONT_CharWidth(x_fs, XFT, j);
957 chars++;
960 avg = (width + (chars>>1))/ chars;
962 else /* uniform width */
963 avg = x_fs->min_bounds.width;
965 return avg;
968 /***********************************************************************
969 * XFONT_GetMaxCharWidth
971 static INT XFONT_GetMaxCharWidth(const XFontStruct* xfs, const XFONTTRANS *XFT)
973 unsigned min = (unsigned char)xfs->min_char_or_byte2;
974 unsigned max = (unsigned char)xfs->max_char_or_byte2;
975 int maxwidth, j;
977 if(!XFT || !xfs->per_char)
978 return abs(xfs->max_bounds.width);
980 for( j = 0, maxwidth = 0, max -= min; j <= max; j++ )
981 if( !CI_NONEXISTCHAR(xfs->per_char + j) )
982 if(maxwidth < xfs->per_char[j].attributes)
983 maxwidth = xfs->per_char[j].attributes;
985 maxwidth *= XFT->pixelsize / 1000.0;
986 return maxwidth;
989 /***********************************************************************
990 * XFONT_SetFontMetric
992 * INIT ONLY
994 * Initializes IFONTINFO16.
996 static void XFONT_SetFontMetric(fontInfo* fi, const fontResource* fr, XFontStruct* xfs)
998 unsigned min, max;
999 fi->df.dfFirstChar = (BYTE)(min = xfs->min_char_or_byte2);
1000 fi->df.dfLastChar = (BYTE)(max = xfs->max_char_or_byte2);
1002 fi->df.dfDefaultChar = (BYTE)xfs->default_char;
1003 fi->df.dfBreakChar = (BYTE)(( ' ' < min || ' ' > max) ? xfs->default_char: ' ');
1005 fi->df.dfPixHeight = (INT16)((fi->df.dfAscent = (INT16)xfs->ascent) + xfs->descent);
1006 fi->df.dfPixWidth = (xfs->per_char) ? 0 : xfs->min_bounds.width;
1008 XFONT_GetLeading( &fi->df, xfs, &fi->df.dfInternalLeading, &fi->df.dfExternalLeading, NULL );
1009 fi->df.dfAvgWidth = (INT16)XFONT_GetAvgCharWidth(&fi->df, xfs, NULL );
1010 fi->df.dfMaxWidth = (INT16)XFONT_GetMaxCharWidth(xfs, NULL);
1012 if( xfs->min_bounds.width != xfs->max_bounds.width )
1013 fi->df.dfPitchAndFamily |= TMPF_FIXED_PITCH; /* au contraire! */
1014 if( fi->fi_flags & FI_SCALABLE )
1016 fi->df.dfType = DEVICE_FONTTYPE;
1017 fi->df.dfPitchAndFamily |= TMPF_DEVICE;
1019 else if( fi->fi_flags & FI_TRUETYPE )
1020 fi->df.dfType = TRUETYPE_FONTTYPE;
1021 else
1022 fi->df.dfType = RASTER_FONTTYPE;
1024 fi->df.dfFace = fr->lfFaceName;
1027 /***********************************************************************
1028 * XFONT_GetTextMetrics
1030 * GetTextMetrics() back end.
1032 static void XFONT_GetTextMetrics( const fontObject* pfo, const LPTEXTMETRICA pTM )
1034 LPIFONTINFO16 pdf = &pfo->fi->df;
1036 if( ! pfo->lpX11Trans ) {
1037 pTM->tmAscent = pfo->fs->ascent;
1038 pTM->tmDescent = pfo->fs->descent;
1039 } else {
1040 pTM->tmAscent = pfo->lpX11Trans->ascent;
1041 pTM->tmDescent = pfo->lpX11Trans->descent;
1044 pTM->tmAscent *= pfo->rescale;
1045 pTM->tmDescent *= pfo->rescale;
1047 pTM->tmHeight = pTM->tmAscent + pTM->tmDescent;
1049 pTM->tmAveCharWidth = pfo->foAvgCharWidth * pfo->rescale;
1050 pTM->tmMaxCharWidth = pfo->foMaxCharWidth * pfo->rescale;
1052 pTM->tmInternalLeading = pfo->foInternalLeading * pfo->rescale;
1053 pTM->tmExternalLeading = pdf->dfExternalLeading * pfo->rescale;
1055 pTM->tmStruckOut = (pfo->fo_flags & FO_SYNTH_STRIKEOUT )
1056 ? 1 : pdf->dfStrikeOut;
1057 pTM->tmUnderlined = (pfo->fo_flags & FO_SYNTH_UNDERLINE )
1058 ? 1 : pdf->dfUnderline;
1060 pTM->tmOverhang = 0;
1061 if( pfo->fo_flags & FO_SYNTH_ITALIC )
1063 pTM->tmOverhang += pTM->tmHeight/3;
1064 pTM->tmItalic = 1;
1065 } else
1066 pTM->tmItalic = pdf->dfItalic;
1068 pTM->tmWeight = pdf->dfWeight;
1069 if( pfo->fo_flags & FO_SYNTH_BOLD )
1071 pTM->tmOverhang++;
1072 pTM->tmWeight += 100;
1075 pTM->tmFirstChar = pdf->dfFirstChar;
1076 pTM->tmLastChar = pdf->dfLastChar;
1077 pTM->tmDefaultChar = pdf->dfDefaultChar;
1078 pTM->tmBreakChar = pdf->dfBreakChar;
1080 pTM->tmCharSet = pdf->dfCharSet;
1081 pTM->tmPitchAndFamily = pdf->dfPitchAndFamily;
1083 pTM->tmDigitizedAspectX = pdf->dfHorizRes;
1084 pTM->tmDigitizedAspectY = pdf->dfVertRes;
1087 /***********************************************************************
1088 * XFONT_GetFontMetric
1090 * Retrieve font metric info (enumeration).
1092 static UINT XFONT_GetFontMetric( const fontInfo* pfi, const LPENUMLOGFONTEX16 pLF,
1093 const LPNEWTEXTMETRIC16 pTM )
1095 memset( pLF, 0, sizeof(*pLF) );
1096 memset( pTM, 0, sizeof(*pTM) );
1098 #define plf ((LPLOGFONT16)pLF)
1099 plf->lfHeight = pTM->tmHeight = pfi->df.dfPixHeight;
1100 plf->lfWidth = pTM->tmAveCharWidth = pfi->df.dfAvgWidth;
1101 plf->lfWeight = pTM->tmWeight = pfi->df.dfWeight;
1102 plf->lfItalic = pTM->tmItalic = pfi->df.dfItalic;
1103 plf->lfUnderline = pTM->tmUnderlined = pfi->df.dfUnderline;
1104 plf->lfStrikeOut = pTM->tmStruckOut = pfi->df.dfStrikeOut;
1105 plf->lfCharSet = pTM->tmCharSet = pfi->df.dfCharSet;
1107 /* convert pitch values */
1109 pTM->tmPitchAndFamily = pfi->df.dfPitchAndFamily;
1110 plf->lfPitchAndFamily = (pfi->df.dfPitchAndFamily & 0xF1) + 1;
1112 lstrcpynA( plf->lfFaceName, pfi->df.dfFace, LF_FACESIZE );
1113 #undef plf
1115 /* FIXME: fill in rest of plF values
1116 lstrcpynA(plF->elfFullName, , LF_FULLFACESIZE);
1117 lstrcpynA(plF->elfStyle, , LF_FACESIZE);
1118 lstrcpynA(plF->elfScript, , LF_FACESIZE);
1121 pTM->tmAscent = pfi->df.dfAscent;
1122 pTM->tmDescent = pTM->tmHeight - pTM->tmAscent;
1123 pTM->tmInternalLeading = pfi->df.dfInternalLeading;
1124 pTM->tmMaxCharWidth = pfi->df.dfMaxWidth;
1125 pTM->tmDigitizedAspectX = pfi->df.dfHorizRes;
1126 pTM->tmDigitizedAspectY = pfi->df.dfVertRes;
1128 pTM->tmFirstChar = pfi->df.dfFirstChar;
1129 pTM->tmLastChar = pfi->df.dfLastChar;
1130 pTM->tmDefaultChar = pfi->df.dfDefaultChar;
1131 pTM->tmBreakChar = pfi->df.dfBreakChar;
1133 /* return font type */
1135 return pfi->df.dfType;
1139 /***********************************************************************
1140 * XFONT_FixupFlags
1142 * INIT ONLY
1144 * dfPitchAndFamily flags for some common typefaces.
1146 static BYTE XFONT_FixupFlags( LPCSTR lfFaceName )
1148 switch( lfFaceName[0] )
1150 case 'a':
1151 case 'A': if(!strncasecmp(lfFaceName, "Arial", 5) )
1152 return FF_SWISS;
1153 break;
1154 case 'h':
1155 case 'H': if(!strcasecmp(lfFaceName, "Helvetica") )
1156 return FF_SWISS;
1157 break;
1158 case 'c':
1159 case 'C': if(!strncasecmp(lfFaceName, "Courier", 7))
1160 return FF_MODERN;
1162 if (!strcasecmp(lfFaceName, "Charter") )
1163 return FF_ROMAN;
1164 break;
1165 case 'p':
1166 case 'P': if( !strcasecmp(lfFaceName,"Palatino") )
1167 return FF_ROMAN;
1168 break;
1169 case 't':
1170 case 'T': if(!strncasecmp(lfFaceName, "Times", 5) )
1171 return FF_ROMAN;
1172 break;
1173 case 'u':
1174 case 'U': if(!strcasecmp(lfFaceName, "Utopia") )
1175 return FF_ROMAN;
1176 break;
1177 case 'z':
1178 case 'Z': if(!strcasecmp(lfFaceName, "Zapf Dingbats") )
1179 return FF_DECORATIVE;
1181 return 0;
1184 /***********************************************************************
1185 * XFONT_SameFoundryAndFamily
1187 * INIT ONLY
1189 static BOOL XFONT_SameFoundryAndFamily( const LFD* lfd1, const LFD* lfd2 )
1191 return ( !strcasecmp( lfd1->foundry, lfd2->foundry ) &&
1192 !strcasecmp( lfd1->family, lfd2->family ) );
1195 /***********************************************************************
1196 * XFONT_InitialCapitals
1198 * INIT ONLY
1200 * Upper case first letters of words & remove multiple spaces
1202 static void XFONT_InitialCapitals(LPSTR lpch)
1204 int i;
1205 BOOL up;
1206 char* lpstr = lpch;
1208 for( i = 0, up = TRUE; *lpch; lpch++, i++ )
1210 if( isspace(*lpch) )
1212 if (!up) /* Not already got one */
1214 *lpstr++ = ' ';
1215 up = TRUE;
1218 else if( isalpha(*lpch) && up )
1220 *lpstr++ = toupper(*lpch);
1221 up = FALSE;
1223 else
1225 *lpstr++ = *lpch;
1226 up = FALSE;
1230 /* Remove possible trailing space */
1231 if (up && i > 0)
1232 --lpstr;
1233 *lpstr = '\0';
1237 /***********************************************************************
1238 * XFONT_WindowsNames
1240 * INIT ONLY
1242 * Build generic Windows aliases for X font names.
1244 * -misc-fixed- -> "Fixed"
1245 * -sony-fixed- -> "Sony Fixed", etc...
1247 static void XFONT_WindowsNames(void)
1249 fontResource* fr;
1251 for( fr = fontList; fr ; fr = fr->next )
1253 fontResource* pfr;
1254 char* lpch;
1256 if( fr->fr_flags & FR_NAMESET ) continue; /* skip already assigned */
1258 for( pfr = fontList; pfr != fr ; pfr = pfr->next )
1259 if( pfr->fr_flags & FR_NAMESET )
1261 if (!strcasecmp( pfr->resource->family, fr->resource->family))
1262 break;
1265 lpch = fr->lfFaceName;
1266 wsnprintfA( fr->lfFaceName, sizeof(fr->lfFaceName), "%s %s",
1267 /* prepend vendor name */
1268 (pfr==fr) ? "" : fr->resource->foundry,
1269 fr->resource->family);
1270 XFONT_InitialCapitals(fr->lfFaceName);
1272 BYTE bFamilyStyle = XFONT_FixupFlags( fr->lfFaceName );
1273 if( bFamilyStyle)
1275 fontInfo* fi;
1276 for( fi = fr->fi ; fi ; fi = fi->next )
1277 fi->df.dfPitchAndFamily |= bFamilyStyle;
1281 TRACE("typeface '%s'\n", fr->lfFaceName);
1283 fr->fr_flags |= FR_NAMESET;
1287 /***********************************************************************
1288 * XFONT_LoadDefaultLFD
1290 * Move lfd to the head of fontList to make it more likely to be matched
1292 static void XFONT_LoadDefaultLFD(LFD* lfd, LPCSTR fonttype)
1295 fontResource *fr, *pfr;
1296 for( fr = NULL, pfr = fontList; pfr; pfr = pfr->next )
1298 if( XFONT_SameFoundryAndFamily(pfr->resource, lfd) )
1300 if( fr )
1302 fr->next = pfr->next;
1303 pfr->next = fontList;
1304 fontList = pfr;
1306 break;
1308 fr = pfr;
1310 if (!pfr)
1311 WARN("Default %sfont '-%s-%s-' not available\n", fonttype,
1312 lfd->foundry, lfd->family);
1316 /***********************************************************************
1317 * XFONT_LoadDefault
1319 static void XFONT_LoadDefault(LPCSTR ini, LPCSTR fonttype)
1321 char buffer[MAX_LFD_LENGTH];
1323 if( PROFILE_GetWineIniString( INIFontSection, ini, "", buffer, sizeof buffer ) )
1325 if (*buffer)
1327 LFD* lfd;
1328 char* pch = buffer;
1329 while( *pch && isspace(*pch) ) pch++;
1331 TRACE("Using '%s' as default %sfont\n", pch, fonttype);
1332 lfd = LFD_Parse(pch);
1333 if (lfd && lfd->foundry && lfd->family)
1334 XFONT_LoadDefaultLFD(lfd, fonttype);
1335 else
1336 WARN("Ini section [%s]%s is malformed\n", INIFontSection, ini);
1337 HeapFree(SystemHeap, 0, lfd);
1342 /***********************************************************************
1343 * XFONT_LoadDefaults
1345 static void XFONT_LoadDefaults(void)
1347 XFONT_LoadDefault(INIDefaultFixed, "fixed ");
1348 XFONT_LoadDefault(INIDefault, "");
1351 /***********************************************************************
1352 * XFONT_CreateAlias
1354 static fontAlias* XFONT_CreateAlias( LPCSTR lpTypeFace, LPCSTR lpAlias )
1356 int j;
1357 fontAlias *pfa, *prev = NULL;
1359 for(pfa = aliasTable; pfa; pfa = pfa->next)
1361 /* check if we already got one */
1362 if( !strcasecmp( pfa->faTypeFace, lpAlias ) )
1364 TRACE("redundant alias '%s' -> '%s'\n",
1365 lpAlias, lpTypeFace );
1366 return NULL;
1368 prev = pfa;
1371 j = lstrlenA(lpTypeFace) + 1;
1372 pfa = HeapAlloc( SystemHeap, 0, sizeof(fontAlias) +
1373 j + lstrlenA(lpAlias) + 1 );
1374 if (pfa)
1376 if (!prev)
1377 aliasTable = pfa;
1378 else
1379 prev->next = pfa;
1381 pfa->next = NULL;
1382 pfa->faTypeFace = (LPSTR)(pfa + 1);
1383 lstrcpyA( pfa->faTypeFace, lpTypeFace );
1384 pfa->faAlias = pfa->faTypeFace + j;
1385 lstrcpyA( pfa->faAlias, lpAlias );
1387 TRACE("added alias '%s' for %s\n", lpAlias, lpTypeFace );
1389 return pfa;
1391 return NULL;
1395 /***********************************************************************
1396 * XFONT_LoadAlias
1398 static void XFONT_LoadAlias(const LFD* lfd, LPCSTR lpAlias, BOOL bSubst)
1400 fontResource *fr, *frMatch = NULL;
1401 if (!lfd->foundry || ! lfd->family)
1403 WARN("Malformed font resource for alias '%s'\n", lpAlias);
1404 return;
1406 for (fr = fontList; fr ; fr = fr->next)
1408 if(!strcasecmp(fr->resource->family, lpAlias))
1410 /* alias is not needed since the real font is present */
1411 TRACE("Ignoring font alias '%s' as it is already available as a real font\n", lpAlias);
1412 return;
1414 if( XFONT_SameFoundryAndFamily( fr->resource, lfd ) )
1416 frMatch = fr;
1417 break;
1421 if( frMatch )
1423 if( bSubst )
1425 fontAlias *pfa, *prev = NULL;
1427 for(pfa = aliasTable; pfa; pfa = pfa->next)
1429 /* Remove lpAlias from aliasTable - we should free the old entry */
1430 if(!strcmp(lpAlias, pfa->faAlias))
1432 if(prev)
1433 prev->next = pfa->next;
1434 else
1435 aliasTable = pfa->next;
1438 /* Update any references to the substituted font in aliasTable */
1439 if(!strcmp(frMatch->lfFaceName, pfa->faTypeFace))
1440 pfa->faTypeFace = HEAP_strdupA( SystemHeap, 0, lpAlias );
1441 prev = pfa;
1444 TRACE("\tsubstituted '%s' with %s\n", frMatch->lfFaceName, lpAlias );
1446 lstrcpynA( frMatch->lfFaceName, lpAlias, LF_FACESIZE );
1447 frMatch->fr_flags |= FR_NAMESET;
1449 else
1451 /* create new entry in the alias table */
1452 XFONT_CreateAlias( frMatch->lfFaceName, lpAlias );
1455 else
1457 WARN("Font alias '-%s-%s-' is not available\n", lfd->foundry, lfd->family);
1461 /***********************************************************************
1462 * XFONT_LoadAliases
1464 * INIT ONLY
1466 * Create font aliases for some standard windows fonts using users
1467 * default choice of (sans-)serif fonts
1469 * Read user-defined aliases from wine.conf. Format is as follows
1471 * Alias# = [Windows font name],[LFD font name], <substitute original name>
1473 * Example:
1474 * Alias0 = Arial, -adobe-helvetica-
1475 * Alias1 = Times New Roman, -bitstream-courier-, 1
1476 * ...
1478 * Note that from 081797 and on we have built-in alias templates that take
1479 * care of the necessary Windows typefaces.
1481 typedef struct
1483 LPSTR fatResource;
1484 LPSTR fatAlias;
1485 } aliasTemplate;
1487 static void XFONT_LoadAliases(void)
1489 char *lpResource;
1490 char buffer[MAX_LFD_LENGTH];
1491 int i = 0;
1492 LFD* lfd;
1494 /* built-ins first */
1495 PROFILE_GetWineIniString( INIFontSection, INIDefaultSerif,
1496 "-bitstream-charter-", buffer, sizeof buffer );
1497 TRACE("Using '%s' as default serif font\n", buffer);
1498 lfd = LFD_Parse(buffer);
1499 /* NB XFONT_InitialCapitals should not change these standard aliases */
1500 if (lfd)
1502 XFONT_LoadAlias( lfd, "Tms Roman", FALSE);
1503 XFONT_LoadAlias( lfd, "MS Serif", FALSE);
1504 XFONT_LoadAlias( lfd, "Times New Roman", FALSE);
1506 XFONT_LoadDefaultLFD( lfd, "serif ");
1507 HeapFree(SystemHeap, 0, lfd);
1510 PROFILE_GetWineIniString( INIFontSection, INIDefaultSansSerif,
1511 "-adobe-helvetica-", buffer, sizeof buffer);
1512 TRACE("Using '%s' as default sans serif font\n", buffer);
1513 lfd = LFD_Parse(buffer);
1514 if (lfd)
1516 XFONT_LoadAlias( lfd, "Helv", FALSE);
1517 XFONT_LoadAlias( lfd, "MS Sans Serif", FALSE);
1518 XFONT_LoadAlias( lfd, "Arial", FALSE);
1520 XFONT_LoadDefaultLFD( lfd, "sans serif ");
1521 HeapFree(SystemHeap, 0, lfd);
1524 /* then user specified aliases */
1527 BOOL bHaveAlias, bSubst;
1528 char subsection[32];
1529 wsnprintfA( subsection, sizeof subsection, "%s%i", INIAliasSection, i++ );
1531 bHaveAlias = PROFILE_GetWineIniString( INIFontSection,
1532 subsection, "", buffer, sizeof buffer);
1533 if (!bHaveAlias)
1534 break;
1536 XFONT_InitialCapitals(buffer);
1537 lpResource = PROFILE_GetStringItem( buffer );
1538 bSubst = (PROFILE_GetStringItem( lpResource )) ? TRUE : FALSE;
1539 if( lpResource && *lpResource )
1541 lfd = LFD_Parse(lpResource);
1542 if (lfd)
1544 XFONT_LoadAlias(lfd, buffer, bSubst);
1545 HeapFree(SystemHeap, 0, lfd);
1548 else
1549 WARN("malformed font alias '%s'\n", buffer );
1551 while(TRUE);
1554 /***********************************************************************
1555 * XFONT_UnAlias
1557 * Convert an (potential) alias into a real windows name
1560 static LPCSTR XFONT_UnAlias(char* font)
1562 if (font[0])
1564 fontAlias* fa;
1565 XFONT_InitialCapitals(font); /* to remove extra white space */
1567 for( fa = aliasTable; fa; fa = fa->next )
1568 /* use case insensitive matching to handle eg "MS Sans Serif" */
1569 if( !strcasecmp( fa->faAlias, font ) )
1571 TRACE("found alias '%s'->%s'\n", font, fa->faTypeFace );
1572 strcpy(font, fa->faTypeFace);
1573 return fa->faAlias;
1574 break;
1577 return NULL;
1580 /***********************************************************************
1581 * XFONT_RemoveFontResource
1583 * Caller should check if the font resource is in use. If it is it should
1584 * set FR_REMOVED flag to delay removal until the resource is not in use
1585 * anymore.
1587 void XFONT_RemoveFontResource( fontResource** ppfr )
1589 fontResource* pfr = *ppfr;
1590 #if 0
1591 fontInfo* pfi;
1593 /* FIXME - if fonts were read from a cache, these HeapFrees will fail */
1594 while( pfr->fi )
1596 pfi = pfr->fi->next;
1597 HeapFree( SystemHeap, 0, pfr->fi );
1598 pfr->fi = pfi;
1600 HeapFree( SystemHeap, 0, pfr );
1601 #endif
1602 *ppfr = pfr->next;
1605 /***********************************************************************
1606 * XFONT_LoadIgnores
1608 * INIT ONLY
1610 * Removes specified fonts from the font table to prevent Wine from
1611 * using it.
1613 * Ignore# = [LFD font name]
1615 * Example:
1616 * Ignore0 = -misc-nil-
1617 * Ignore1 = -sun-open look glyph-
1618 * ...
1621 static void XFONT_LoadIgnore(char* lfdname)
1623 fontResource** ppfr;
1625 LFD* lfd = LFD_Parse(lfdname);
1626 if (lfd && lfd->foundry && lfd->family)
1628 for( ppfr = &fontList; *ppfr ; ppfr = &((*ppfr)->next))
1630 if( XFONT_SameFoundryAndFamily( (*ppfr)->resource, lfd) )
1632 TRACE("Ignoring '-%s-%s-'\n",
1633 (*ppfr)->resource->foundry, (*ppfr)->resource->family );
1635 XFONT_RemoveFontResource( ppfr );
1636 break;
1640 else
1641 WARN("Malformed font resource\n");
1643 HeapFree(SystemHeap, 0, lfd);
1646 static void XFONT_LoadIgnores(void)
1648 int i = 0;
1649 char subsection[32];
1650 char buffer[MAX_LFD_LENGTH];
1652 /* Standard one that noone wants */
1653 strcpy(buffer, "-misc-nil-");
1654 XFONT_LoadIgnore(buffer);
1656 /* Others from INI file */
1659 wsprintfA( subsection, "%s%i", INIIgnoreSection, i++ );
1661 if( PROFILE_GetWineIniString( INIFontSection,
1662 subsection, "", buffer, sizeof buffer) )
1664 char* pch = buffer;
1665 while( *pch && isspace(*pch) ) pch++;
1666 XFONT_LoadIgnore(pch);
1668 else
1669 break;
1670 } while(TRUE);
1674 /***********************************************************************
1675 * XFONT_UserMetricsCache
1677 * Returns expanded name for the cachedmetrics file.
1678 * Now it also appends the current value of the $DISPLAY varaible.
1680 static char* XFONT_UserMetricsCache( char* buffer, int* buf_size )
1682 char* pchDisplay, *home;
1684 pchDisplay = getenv( "DISPLAY" );
1685 if( !pchDisplay ) pchDisplay = "0";
1687 if ((home = getenv( "HOME" )) != NULL)
1689 int i = strlen( home ) + strlen( INIWinePrefix ) +
1690 strlen( INIFontMetrics ) + strlen( pchDisplay ) + 2;
1691 if( i > *buf_size )
1692 buffer = (char*) HeapReAlloc( SystemHeap, 0, buffer, *buf_size = i );
1693 strcpy( buffer, home );
1694 strcat( buffer, INIWinePrefix );
1695 strcat( buffer, INIFontMetrics );
1696 strcat( buffer, pchDisplay );
1697 } else buffer[0] = '\0';
1698 return buffer;
1702 /***********************************************************************
1703 * X Font Matching
1705 * Compare two fonts (only parameters set by the XFONT_InitFontInfo()).
1707 static INT XFONT_IsSubset(const fontInfo* match, const fontInfo* fi)
1709 INT m;
1711 /* 0 - keep both, 1 - keep match, -1 - keep fi */
1713 /* Compare dfItalic, Underline, Strikeout, Weight, Charset */
1714 m = (BYTE*)&fi->df.dfPixWidth - (BYTE*)&fi->df.dfItalic;
1715 if( memcmp(&match->df.dfItalic, &fi->df.dfItalic, m )) return 0;
1717 if( (!((fi->fi_flags & FI_SCALABLE) + (match->fi_flags & FI_SCALABLE))
1718 && fi->lfd_height != match->lfd_height) ||
1719 (!((fi->fi_flags & FI_POLYWEIGHT) + (match->fi_flags & FI_POLYWEIGHT))
1720 && fi->df.dfWeight != match->df.dfWeight) ) return 0;
1722 m = (int)(match->fi_flags & (FI_POLYWEIGHT | FI_SCALABLE)) -
1723 (int)(fi->fi_flags & (FI_SCALABLE | FI_POLYWEIGHT));
1725 if( m == (FI_POLYWEIGHT - FI_SCALABLE) ||
1726 m == (FI_SCALABLE - FI_POLYWEIGHT) ) return 0; /* keep both */
1727 else if( m >= 0 ) return 1; /* 'match' is better */
1729 return -1; /* 'fi' is better */
1732 /***********************************************************************
1733 * XFONT_CheckFIList
1735 * REMOVE_SUBSETS - attach new fi and purge subsets
1736 * UNMARK_SUBSETS - remove subset flags from all fi entries
1738 static void XFONT_CheckFIList( fontResource* fr, fontInfo* fi, int action)
1740 int i = 0;
1741 fontInfo* pfi, *prev;
1743 for( prev = NULL, pfi = fr->fi; pfi; )
1745 if( action == REMOVE_SUBSETS )
1747 if( pfi->fi_flags & FI_SUBSET )
1749 fontInfo* subset = pfi;
1751 i++;
1752 fr->fi_count--;
1753 if( prev ) prev->next = pfi = pfi->next;
1754 else fr->fi = pfi = pfi->next;
1755 HeapFree( SystemHeap, 0, subset );
1756 continue;
1759 else pfi->fi_flags &= ~FI_SUBSET;
1761 prev = pfi;
1762 pfi = pfi->next;
1765 if( action == REMOVE_SUBSETS ) /* also add the superset */
1767 if( fi->fi_flags & FI_SCALABLE )
1769 fi->next = fr->fi;
1770 fr->fi = fi;
1772 else if( prev ) prev->next = fi; else fr->fi = fi;
1773 fr->fi_count++;
1776 if( i ) TRACE("\t purged %i subsets [%i]\n", i , fr->fi_count);
1779 /***********************************************************************
1780 * XFONT_FindFIList
1782 static fontResource* XFONT_FindFIList( fontResource* pfr, const char* pTypeFace )
1784 while( pfr )
1786 if( !strcasecmp( pfr->lfFaceName, pTypeFace ) ) break;
1787 pfr = pfr->next;
1789 /* Give the app back the font name it asked for. Encarta checks this. */
1790 if (pfr) strcpy(pfr->lfFaceName,pTypeFace);
1791 return pfr;
1794 /***********************************************************************
1795 * XFONT_FixupPointSize
1797 static void XFONT_FixupPointSize(fontInfo* fi)
1799 #define df (fi->df)
1800 df.dfHorizRes = df.dfVertRes = fi->lfd_resolution;
1801 df.dfPoints = (INT16)
1802 (((INT)(df.dfPixHeight - df.dfInternalLeading) * 72 + (df.dfVertRes >> 1)) /
1803 df.dfVertRes );
1804 #undef df
1807 /***********************************************************************
1808 * XFONT_BuildMetrics
1810 * Build font metrics from X font
1812 static int XFONT_BuildMetrics(char** x_pattern, int res, unsigned x_checksum, int x_count)
1814 int i;
1815 fontInfo* fi = NULL;
1816 int n_ff = 0;
1818 MESSAGE("Building font metrics. This may take some time...\n");
1819 for( i = 0; i < x_count; i++ )
1821 char* typeface;
1822 LFD* lfd;
1823 fontResource* fr, *pfr;
1824 int j;
1825 char buffer[MAX_LFD_LENGTH];
1826 char* lpstr;
1827 XFontStruct* x_fs;
1828 fontInfo* pfi;
1830 typeface = HEAP_strdupA(SystemHeap, 0, x_pattern[i]);
1831 if (!typeface)
1832 break;
1834 lfd = LFD_Parse(typeface);
1835 if (!lfd)
1837 HeapFree(SystemHeap, 0, typeface);
1838 continue;
1841 /* find a family to insert into */
1843 for( pfr = NULL, fr = fontList; fr; fr = fr->next )
1845 if( XFONT_SameFoundryAndFamily(fr->resource, lfd))
1846 break;
1847 pfr = fr;
1850 if( !fi ) fi = (fontInfo*) HeapAlloc(SystemHeap, 0, sizeof(fontInfo));
1852 if( !LFD_InitFontInfo( fi, lfd, x_pattern[i]) )
1853 goto nextfont;
1855 if( !fr ) /* add new family */
1857 n_ff++;
1858 fr = (fontResource*) HeapAlloc(SystemHeap, 0, sizeof(fontResource));
1859 if (fr)
1861 memset(fr, 0, sizeof(fontResource));
1863 fr->resource = (LFD*) HeapAlloc(SystemHeap, 0, sizeof(LFD));
1864 memset(fr->resource, 0, sizeof(LFD));
1866 TRACE("family: -%s-%s-\n", lfd->foundry, lfd->family );
1867 fr->resource->foundry = HEAP_strdupA(SystemHeap, 0, lfd->foundry);
1868 fr->resource->family = HEAP_strdupA(SystemHeap, 0, lfd->family);
1869 fr->resource->weight = "";
1871 if( pfr ) pfr->next = fr;
1872 else fontList = fr;
1874 else
1875 WARN("Not enough memory for a new font family\n");
1878 /* check if we already have something better than "fi" */
1880 for( pfi = fr->fi, j = 0; pfi && j <= 0; pfi = pfi->next )
1881 if( (j = XFONT_IsSubset( pfi, fi )) < 0 )
1882 pfi->fi_flags |= FI_SUBSET; /* superseded by "fi" */
1883 if( j > 0 ) goto nextfont;
1885 /* add new font instance "fi" to the "fr" font resource */
1887 if( fi->fi_flags & FI_SCALABLE )
1889 LFD lfd1;
1890 char pxl_string[4], res_string[4];
1891 /* set scalable font height to get an basis for extrapolation */
1893 fi->lfd_height = DEF_SCALABLE_HEIGHT;
1894 fi->lfd_resolution = res;
1896 sprintf(pxl_string, "%d", fi->lfd_height);
1897 sprintf(res_string, "%d", fi->lfd_resolution);
1899 lfd1 = *lfd;
1900 lfd1.pixel_size = pxl_string;
1901 lfd1.point_size = "*";
1902 lfd1.resolution_x = res_string;
1903 lfd1.resolution_y = res_string;
1905 LFD_UnParse(buffer, sizeof buffer, &lfd1);
1907 lpstr = buffer;
1909 else lpstr = x_pattern[i];
1911 if( (x_fs = TSXLoadQueryFont(display, lpstr)) )
1913 XFONT_SetFontMetric( fi, fr, x_fs );
1914 TSXFreeFont( display, x_fs );
1916 XFONT_FixupPointSize(fi);
1918 TRACE("\t[% 2ipt] '%s'\n", fi->df.dfPoints, x_pattern[i] );
1920 XFONT_CheckFIList( fr, fi, REMOVE_SUBSETS );
1921 fi = NULL; /* preventing reuse */
1923 else
1925 ERR("failed to load %s\n", lpstr );
1927 XFONT_CheckFIList( fr, fi, UNMARK_SUBSETS );
1929 nextfont:
1930 HeapFree(SystemHeap, 0, lfd);
1931 HeapFree(SystemHeap, 0, typeface);
1933 if( fi ) HeapFree(SystemHeap, 0, fi);
1935 return n_ff;
1938 /***********************************************************************
1939 * XFONT_ReadCachedMetrics
1941 * INIT ONLY
1943 static BOOL XFONT_ReadCachedMetrics( int fd, int res, unsigned x_checksum, int x_count )
1945 if( fd >= 0 )
1947 unsigned u;
1948 int i, j;
1950 /* read checksums */
1951 read( fd, &u, sizeof(unsigned) );
1952 read( fd, &i, sizeof(int) );
1954 if( u == x_checksum && i == x_count )
1956 off_t length, offset = 3 * sizeof(int);
1958 /* read total size */
1959 read( fd, &i, sizeof(int) );
1960 length = lseek( fd, 0, SEEK_END );
1962 if( length == (i + offset) )
1964 lseek( fd, offset, SEEK_SET );
1965 fontList = (fontResource*)HeapAlloc( SystemHeap, 0, i);
1966 if( fontList )
1968 fontResource* pfr = fontList;
1969 fontInfo* pfi = NULL;
1971 TRACE("Reading cached font metrics:\n");
1973 read( fd, fontList, i); /* read all metrics at once */
1974 while( offset < length )
1976 offset += sizeof(fontResource) + sizeof(fontInfo);
1977 pfr->fi = pfi = (fontInfo*)(pfr + 1);
1978 j = 1;
1979 while( TRUE )
1981 if( offset > length ||
1982 (int)(pfi->next) != j++ ) goto fail;
1984 pfi->df.dfFace = pfr->lfFaceName;
1985 if( pfi->fi_flags & FI_SCALABLE )
1987 /* we can pretend we got this font for any resolution */
1988 pfi->lfd_resolution = res;
1989 XFONT_FixupPointSize(pfi);
1991 pfi->next = pfi + 1;
1993 if( j > pfr->fi_count ) break;
1995 pfi = pfi->next;
1996 offset += sizeof(fontInfo);
1998 pfi->next = NULL;
1999 if( pfr->next )
2001 pfr->next = (fontResource*)(pfi + 1);
2002 pfr = pfr->next;
2004 else break;
2006 if( pfr->next == NULL &&
2007 *(int*)(pfi + 1) == X_FMC_MAGIC )
2009 /* read LFD stubs */
2010 char* lpch = (char*)((int*)(pfi + 1) + 1);
2011 offset += sizeof(int);
2012 for( pfr = fontList; pfr; pfr = pfr->next )
2014 size_t len = strlen(lpch) + 1;
2015 TRACE("\t%s, %i instances\n", lpch, pfr->fi_count );
2016 pfr->resource = LFD_Parse(lpch);
2017 lpch += len;
2018 offset += len;
2019 if (offset > length)
2020 goto fail;
2022 close( fd );
2023 return TRUE;
2028 fail:
2029 if( fontList ) HeapFree( SystemHeap, 0, fontList );
2030 fontList = NULL;
2031 close( fd );
2033 return FALSE;
2036 /***********************************************************************
2037 * XFONT_WriteCachedMetrics
2039 * INIT ONLY
2041 static BOOL XFONT_WriteCachedMetrics( int fd, unsigned x_checksum, int x_count, int n_ff )
2043 fontResource* pfr;
2044 fontInfo* pfi;
2046 if( fd >= 0 )
2048 int i, j, k;
2049 char buffer[MAX_LFD_LENGTH];
2051 /* font metrics file:
2053 * +0000 x_checksum
2054 * +0004 x_count
2055 * +0008 total size to load
2056 * +000C prepackaged font metrics
2057 * ...
2058 * +...x X_FMC_MAGIC
2059 * +...x + 4 LFD stubs
2062 write( fd, &x_checksum, sizeof(unsigned) );
2063 write( fd, &x_count, sizeof(int) );
2065 for( j = i = 0, pfr = fontList; pfr; pfr = pfr->next )
2067 LFD_UnParse(buffer, sizeof buffer, pfr->resource);
2068 i += strlen( buffer) + 1;
2069 j += pfr->fi_count;
2071 i += n_ff * sizeof(fontResource) + j * sizeof(fontInfo) + sizeof(int);
2072 write( fd, &i, sizeof(int) );
2074 TRACE("Writing font cache:\n");
2076 for( pfr = fontList; pfr; pfr = pfr->next )
2078 fontInfo fi;
2080 TRACE("\t-%s-%s-, %i instances\n", pfr->resource->foundry, pfr->resource->family, pfr->fi_count );
2082 i = write( fd, pfr, sizeof(fontResource) );
2083 if( i == sizeof(fontResource) )
2085 for( k = 1, pfi = pfr->fi; pfi; pfi = pfi->next )
2087 fi = *pfi;
2089 fi.df.dfFace = NULL;
2090 fi.next = (fontInfo*)k; /* loader checks this */
2092 j = write( fd, &fi, sizeof(fi) );
2093 k++;
2095 if( j == sizeof(fontInfo) ) continue;
2097 break;
2099 if( i == sizeof(fontResource) && j == sizeof(fontInfo) )
2101 i = j = X_FMC_MAGIC;
2102 write( fd, &i, sizeof(int) );
2103 for( pfr = fontList; pfr && i == j; pfr = pfr->next )
2105 LFD_UnParse(buffer, sizeof buffer, pfr->resource);
2106 i = strlen( buffer ) + 1;
2107 j = write( fd, buffer, i );
2110 close( fd );
2111 return ( i == j );
2113 return FALSE;
2116 /***********************************************************************
2117 * XFONT_CheckIniSection
2119 * INIT ONLY
2121 * Examines wine.conf for old/invalid font entries and recommend changes to
2122 * the user.
2124 * Revision history
2125 * 05-Jul-1997 Dave Cuthbert (dacut@ece.cmu.edu)
2126 * Original implementation.
2128 static void XFONT_CheckIniCallback(char const *, char const *, void *);
2130 static char const *fontmsgprologue =
2131 "Wine warning:\n"
2132 " The following entries in the [fonts] section of the wine.conf file are\n"
2133 " obsolete or invalid:\n";
2135 static char const *fontmsgepilogue =
2136 " These entries should be eliminated or updated.\n"
2137 " See the documentation/fonts file for more information.\n";
2139 static int XFONT_CheckIniSection(void)
2141 int found = 0;
2143 PROFILE_EnumerateWineIniSection("Fonts", &XFONT_CheckIniCallback,
2144 (void *)&found);
2145 if(found)
2146 MESSAGE(fontmsgepilogue);
2148 return 1;
2151 static void XFONT_CheckIniCallback(
2152 char const *key,
2153 char const *value,
2154 void *found)
2156 /* Ignore any keys that start with potential comment characters "'", '#',
2157 or ';'. */
2158 if(key[0] == '\'' || key[0] == '#' || key[0] == ';' || key[0] == '\0')
2159 return;
2161 /* Make sure this is a valid key */
2162 if((strncasecmp(key, INIAliasSection, 5) == 0) ||
2163 (strncasecmp(key, INIIgnoreSection, 6) == 0) ||
2164 (strcasecmp( key, INIDefault) == 0) ||
2165 (strcasecmp( key, INIDefaultFixed) == 0) ||
2166 (strcasecmp( key, INIGlobalMetrics) == 0) ||
2167 (strcasecmp( key, INIResolution) == 0) ||
2168 (strcasecmp( key, INIDefaultSerif) == 0) ||
2169 (strcasecmp( key, INIDefaultSansSerif) ==0) )
2171 /* Valid key; make sure the value doesn't contain a wildcard */
2172 if(strchr(value, '*')) {
2173 if(*(int *)found == 0) {
2174 MESSAGE(fontmsgprologue);
2175 ++*(int *)found;
2177 MESSAGE(" %s=%s [no wildcards allowed]\n", key, value);
2180 else {
2181 /* Not a valid key */
2182 if(*(int *)found == 0) {
2183 MESSAGE(fontmsgprologue);
2184 ++*(int *)found;
2187 MESSAGE(" %s=%s [obsolete]\n", key, value);
2190 return;
2193 /***********************************************************************
2194 * XFONT_GetPointResolution()
2196 * INIT ONLY
2198 * Here we initialize DefResolution which is used in the
2199 * XFONT_Match() penalty function. We also load the point
2200 * resolution value (higher values result in larger fonts).
2202 static int XFONT_GetPointResolution( DeviceCaps* pDevCaps )
2204 int i, j, point_resolution, num = 3;
2205 int allowed_xfont_resolutions[3] = { 72, 75, 100 };
2206 int best = 0, best_diff = 65536;
2208 point_resolution = PROFILE_GetWineIniInt( INIFontSection, INIResolution, 0 );
2209 if( !point_resolution )
2210 point_resolution = pDevCaps->logPixelsY;
2211 else
2212 pDevCaps->logPixelsX = pDevCaps->logPixelsY = point_resolution;
2215 /* FIXME We can only really guess at a best DefResolution
2216 * - this should be configurable
2218 for( i = best = 0; i < num; i++ )
2220 j = abs( point_resolution - allowed_xfont_resolutions[i] );
2221 if( j < best_diff )
2223 best = i;
2224 best_diff = j;
2227 DefResolution = allowed_xfont_resolutions[best];
2229 /* FIXME - do win95,nt40,... do this as well ? */
2230 if (TWEAK_WineLook == WIN98_LOOK)
2232 /* Lie about the screen size, so that eg MM_LOMETRIC becomes MM_logical_LOMETRIC */
2233 int denom;
2234 denom = pDevCaps->logPixelsX * 10;
2235 pDevCaps->horzSize = (pDevCaps->horzRes * 254 + (denom>>1)) / denom;
2236 denom = pDevCaps->logPixelsY * 10;
2237 pDevCaps->vertSize = (pDevCaps->vertRes * 254 + (denom>>1)) / denom;
2240 return point_resolution;
2244 /***********************************************************************
2245 * XFONT_Match
2247 * Compute the matching score between the logical font and the device font.
2249 * contributions from highest to lowest:
2250 * charset
2251 * fixed pitch
2252 * height
2253 * family flags (only when the facename is not present)
2254 * width
2255 * weight, italics, underlines, strikeouts
2257 * NOTE: you can experiment with different penalty weights to see what happens.
2258 * http://premium.microsoft.com/msdn/library/techart/f365/f36b/f37b/d38b/sa8bf.htm
2260 static UINT XFONT_Match( fontMatch* pfm )
2262 fontInfo* pfi = pfm->pfi; /* device font to match */
2263 LPLOGFONT16 plf = pfm->plf; /* wanted logical font */
2264 UINT penalty = 0;
2265 BOOL bR6 = pfm->flags & FO_MATCH_XYINDEP; /* from TextCaps */
2266 BOOL bScale = pfi->fi_flags & FI_SCALABLE;
2267 int d = 0, height;
2269 TRACE("\t[ %-2ipt h=%-3i w=%-3i %s%s]\n", pfi->df.dfPoints,
2270 pfi->df.dfPixHeight, pfi->df.dfAvgWidth,
2271 (pfi->df.dfWeight > FW_NORMAL) ? "Bold " : "Normal ",
2272 (pfi->df.dfItalic) ? "Italic" : "" );
2274 pfm->flags &= FO_MATCH_MASK;
2276 /* Charset */
2277 if( plf->lfCharSet == DEFAULT_CHARSET )
2279 if( (pfi->df.dfCharSet != ANSI_CHARSET) && (pfi->df.dfCharSet != DEFAULT_CHARSET) )
2280 penalty += 0x200;
2282 else if (plf->lfCharSet != pfi->df.dfCharSet) penalty += 0x200;
2284 /* Height */
2285 height = -1;
2287 if( plf->lfHeight > 0 )
2289 int h = pfi->df.dfPixHeight;
2290 d = h - plf->lfHeight;
2291 height = plf->lfHeight;
2293 else
2295 int h = pfi->df.dfPixHeight - pfi->df.dfInternalLeading;
2296 if (h)
2298 d = h + plf->lfHeight;
2299 height = (-plf->lfHeight * pfi->df.dfPixHeight) / h;
2301 else
2303 ERR("PixHeight == InternalLeading\n");
2304 penalty += 0x1000; /* dont want this */
2309 if( height == 0 )
2310 pfm->height = 1; /* Very small */
2311 else if( d )
2313 if( bScale )
2314 pfm->height = height;
2315 else if( (plf->lfQuality != PROOF_QUALITY) && bR6 )
2317 if( d > 0 ) /* do not shrink raster fonts */
2319 pfm->height = pfi->df.dfPixHeight;
2320 penalty += (pfi->df.dfPixHeight - height) * 0x4;
2322 else /* expand only in integer multiples */
2324 pfm->height = height - height%pfi->df.dfPixHeight;
2325 penalty += (height - pfm->height + 1) * height / pfi->df.dfPixHeight;
2328 else /* can't be scaled at all */
2330 if( plf->lfQuality != PROOF_QUALITY) pfm->flags |= FO_SYNTH_HEIGHT;
2331 pfm->height = pfi->df.dfPixHeight;
2332 penalty += (d > 0)? d * 0x8 : -d * 0x10;
2335 else
2336 pfm->height = pfi->df.dfPixHeight;
2338 /* Pitch and Family */
2339 if( pfm->flags & FO_MATCH_PAF ) {
2340 int family = plf->lfPitchAndFamily & FF_FAMILY;
2342 /* TMPF_FIXED_PITCH means exactly the opposite */
2343 if( plf->lfPitchAndFamily & FIXED_PITCH ) {
2344 if( pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH ) penalty += 0x100;
2345 } else /* Variable is the default */
2346 if( !(pfi->df.dfPitchAndFamily & TMPF_FIXED_PITCH) ) penalty += 0x2;
2348 if (family != FF_DONTCARE && family != (pfi->df.dfPitchAndFamily & FF_FAMILY) )
2349 penalty += 0x10;
2352 /* Width */
2353 if( plf->lfWidth )
2355 int h;
2356 if( bR6 || bScale ) h = 0;
2357 else
2359 /* FIXME: not complete */
2361 pfm->flags |= FO_SYNTH_WIDTH;
2362 h = abs(plf->lfWidth - (pfm->height * pfi->df.dfAvgWidth)/pfi->df.dfPixHeight);
2364 penalty += h * ( d ) ? 0x2 : 0x1 ;
2366 else if( !(pfi->fi_flags & FI_NORMAL) ) penalty++;
2368 /* Weight */
2369 if( plf->lfWeight != FW_DONTCARE )
2371 penalty += abs(plf->lfWeight - pfi->df.dfWeight) / 40;
2372 if( plf->lfWeight > pfi->df.dfWeight ) pfm->flags |= FO_SYNTH_BOLD;
2373 } else if( pfi->df.dfWeight >= FW_BOLD ) penalty++; /* choose normal by default */
2375 /* Italic */
2376 if( plf->lfItalic != pfi->df.dfItalic )
2378 penalty += 0x4;
2379 pfm->flags |= FO_SYNTH_ITALIC;
2381 /* Underline */
2382 if( plf->lfUnderline ) pfm->flags |= FO_SYNTH_UNDERLINE;
2384 /* Strikeout */
2385 if( plf->lfStrikeOut ) pfm->flags |= FO_SYNTH_STRIKEOUT;
2388 if( penalty && !bScale && pfi->lfd_resolution != DefResolution )
2389 penalty++;
2391 TRACE(" returning %i\n", penalty );
2393 return penalty;
2396 /***********************************************************************
2397 * XFONT_MatchFIList
2399 * Scan a particular font resource for the best match.
2401 static UINT XFONT_MatchFIList( fontMatch* pfm )
2403 BOOL skipRaster = (pfm->flags & FO_MATCH_NORASTER);
2404 UINT current_score, score = (UINT)(-1);
2405 fontMatch fm = *pfm;
2407 for( fm.pfi = pfm->pfr->fi; fm.pfi && score; fm.pfi = fm.pfi->next)
2409 if( skipRaster && !(fm.pfi->fi_flags & FI_SCALABLE) )
2410 continue;
2412 current_score = XFONT_Match( &fm );
2413 if( score > current_score )
2415 *pfm = fm;
2416 score = current_score;
2419 return score;
2422 /***********************************************************************
2423 * XFONT_MatchDeviceFont
2425 * Scan font resource tree.
2428 static void XFONT_MatchDeviceFont( fontResource* start, fontMatch* pfm)
2430 fontMatch fm = *pfm;
2431 UINT current_score, score = (UINT)(-1);
2432 fontResource** ppfr;
2434 TRACE("(%u) '%s' h=%i weight=%i %s\n",
2435 pfm->plf->lfCharSet, pfm->plf->lfFaceName, pfm->plf->lfHeight,
2436 pfm->plf->lfWeight, (pfm->plf->lfItalic) ? "Italic" : "" );
2438 pfm->pfi = NULL;
2439 if( fm.plf->lfFaceName[0] )
2441 fm.pfr = XFONT_FindFIList( start, fm.plf->lfFaceName);
2442 if( fm.pfr ) /* match family */
2444 TRACE("found facename '%s'\n", fm.pfr->lfFaceName );
2446 if( fm.pfr->fr_flags & FR_REMOVED )
2447 fm.pfr = 0;
2448 else
2450 XFONT_MatchFIList( &fm );
2451 *pfm = fm;
2452 if (pfm->pfi)
2453 return;
2458 /* match all available fonts */
2460 fm.flags |= FO_MATCH_PAF;
2461 for( ppfr = &fontList; *ppfr && score; ppfr = &(*ppfr)->next )
2463 if( (*ppfr)->fr_flags & FR_REMOVED )
2465 if( (*ppfr)->fo_count == 0 )
2466 XFONT_RemoveFontResource( ppfr );
2467 continue;
2470 fm.pfr = *ppfr;
2472 TRACE("%s\n", fm.pfr->lfFaceName );
2474 current_score = XFONT_MatchFIList( &fm );
2475 if( current_score < score )
2477 score = current_score;
2478 *pfm = fm;
2484 /***********************************************************************
2485 * X Font Cache
2487 static void XFONT_GrowFreeList(int start, int end)
2489 /* add all entries from 'start' up to and including 'end' */
2491 memset( fontCache + start, 0, (end - start + 1) * sizeof(fontObject) );
2493 fontCache[end].lru = fontLF;
2494 fontCache[end].count = -1;
2495 fontLF = start;
2496 while( start < end )
2498 fontCache[start].count = -1;
2499 fontCache[start].lru = start + 1;
2500 start++;
2504 static fontObject* XFONT_LookupCachedFont( const LPLOGFONT16 plf, UINT16* checksum )
2506 UINT16 cs = __lfCheckSum( plf );
2507 int i = fontMRU, prev = -1;
2509 *checksum = cs;
2510 while( i >= 0 )
2512 if( fontCache[i].lfchecksum == cs &&
2513 !(fontCache[i].fo_flags & FO_REMOVED) )
2515 /* FIXME: something more intelligent here ? */
2517 if( !memcmp( plf, &fontCache[i].lf,
2518 sizeof(LOGFONT16) - LF_FACESIZE ) &&
2519 !strcmp( plf->lfFaceName, fontCache[i].lf.lfFaceName) )
2521 /* remove temporarily from the lru list */
2522 if( prev >= 0 )
2523 fontCache[prev].lru = fontCache[i].lru;
2524 else
2525 fontMRU = (INT16)fontCache[i].lru;
2526 return (fontCache + i);
2529 prev = i;
2530 i = (INT16)fontCache[i].lru;
2532 return NULL;
2535 static fontObject* XFONT_GetCacheEntry(void)
2537 int i;
2539 if( fontLF == -1 )
2541 int prev_i, prev_j, j;
2543 TRACE("font cache is full\n");
2545 /* lookup the least recently used font */
2547 for( prev_i = prev_j = j = -1, i = fontMRU; i >= 0; i = (INT16)fontCache[i].lru )
2549 if( fontCache[i].count <= 0 &&
2550 !(fontCache[i].fo_flags & FO_SYSTEM) )
2552 prev_j = prev_i;
2553 j = i;
2555 prev_i = i;
2558 if( j >= 0 ) /* unload font */
2560 /* detach from the lru list */
2562 TRACE("\tfreeing entry %i\n", j );
2564 fontCache[j].fr->fo_count--;
2566 if( prev_j >= 0 )
2567 fontCache[prev_j].lru = fontCache[j].lru;
2568 else fontMRU = (INT16)fontCache[j].lru;
2570 /* FIXME: lpXForm, lpPixmap */
2571 if(fontCache[j].lpX11Trans)
2572 HeapFree( SystemHeap, 0, fontCache[j].lpX11Trans );
2574 TSXFreeFont( display, fontCache[j].fs );
2576 memset( fontCache + j, 0, sizeof(fontObject) );
2577 return (fontCache + j);
2579 else /* expand cache */
2581 fontObject* newCache;
2583 prev_i = fontCacheSize + FONTCACHE;
2585 TRACE("\tgrowing font cache from %i to %i\n", fontCacheSize, prev_i );
2587 if( (newCache = (fontObject*)HeapReAlloc(SystemHeap, 0,
2588 fontCache, prev_i)) )
2590 i = fontCacheSize;
2591 fontCacheSize = prev_i;
2592 fontCache = newCache;
2593 XFONT_GrowFreeList( i, fontCacheSize - 1);
2595 else return NULL;
2599 /* detach from the free list */
2601 i = fontLF;
2602 fontLF = (INT16)fontCache[i].lru;
2603 fontCache[i].count = 0;
2604 return (fontCache + i);
2607 static int XFONT_ReleaseCacheEntry(const fontObject* pfo)
2609 UINT u = (UINT)(pfo - fontCache);
2611 if( u < fontCacheSize ) return (--fontCache[u].count);
2612 return -1;
2615 /***********************************************************************
2616 * X11DRV_FONT_Init
2618 * Initialize font resource list and allocate font cache.
2620 BOOL X11DRV_FONT_Init( DeviceCaps* pDevCaps )
2622 char** x_pattern;
2623 unsigned x_checksum;
2624 int i,res, x_count, fd, buf_size;
2625 char *buffer;
2627 XFONT_CheckIniSection();
2629 res = XFONT_GetPointResolution( pDevCaps );
2631 x_pattern = TSXListFonts(display, "*", MAX_FONTS, &x_count );
2633 TRACE("Font Mapper: initializing %i fonts [logical dpi=%i, default dpi=%i]\n",
2634 x_count, res, DefResolution);
2635 if (x_count == MAX_FONTS)
2636 MESSAGE("There may be more fonts available - try increasing the value of MAX_FONTS\n");
2638 for( i = x_checksum = 0; i < x_count; i++ )
2640 int j;
2641 #if 0
2642 printf("%i\t: %s\n", i, x_pattern[i] );
2643 #endif
2645 j = strlen( x_pattern[i] );
2646 if( j ) x_checksum ^= __genericCheckSum( x_pattern[i], j );
2648 x_checksum |= X_PFONT_MAGIC;
2649 buf_size = 128;
2650 buffer = HeapAlloc( SystemHeap, 0, buf_size );
2652 /* deal with systemwide font metrics cache */
2654 if( PROFILE_GetWineIniString( INIFontSection, INIGlobalMetrics, "", buffer, buf_size ) )
2656 fd = open( buffer, O_RDONLY );
2657 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2659 if (fontList == NULL)
2661 /* try per-user */
2662 buffer = XFONT_UserMetricsCache( buffer, &buf_size );
2663 if( buffer[0] )
2665 fd = open( buffer, O_RDONLY );
2666 XFONT_ReadCachedMetrics(fd, DefResolution, x_checksum, x_count);
2670 if( fontList == NULL ) /* build metrics from scratch */
2672 int n_ff = XFONT_BuildMetrics(x_pattern, DefResolution, x_checksum, x_count);
2673 if( buffer[0] ) /* update cached metrics */
2675 fd = open( buffer, O_CREAT | O_TRUNC | O_RDWR, 0644 ); /* -rw-r--r-- */
2676 if( XFONT_WriteCachedMetrics( fd, x_checksum, x_count, n_ff ) == FALSE )
2678 WARN("Unable to write to fontcache '%s'\n", buffer);
2679 if( fd >= 0) remove( buffer ); /* couldn't write entire file */
2684 TSXFreeFontNames(x_pattern);
2686 /* check if we're dealing with X11 R6 server */
2688 XFontStruct* x_fs;
2689 strcpy(buffer, "-*-*-*-*-normal-*-[12 0 0 12]-*-72-*-*-*-iso8859-1");
2690 if( (x_fs = TSXLoadQueryFont(display, buffer)) )
2692 XTextCaps |= TC_SF_X_YINDEP;
2693 TSXFreeFont(display, x_fs);
2696 HeapFree(SystemHeap, 0, buffer);
2698 XFONT_WindowsNames();
2699 XFONT_LoadAliases();
2700 XFONT_LoadDefaults();
2701 XFONT_LoadIgnores();
2703 InitializeCriticalSection( &crtsc_fonts_X11 );
2704 MakeCriticalSectionGlobal( &crtsc_fonts_X11 );
2706 /* fontList initialization is over, allocate X font cache */
2708 fontCache = (fontObject*) HeapAlloc(SystemHeap, 0, fontCacheSize * sizeof(fontObject));
2709 XFONT_GrowFreeList(0, fontCacheSize - 1);
2711 TRACE("done!\n");
2713 /* update text caps parameter */
2715 pDevCaps->textCaps = XTextCaps;
2717 RAW_ASCENT = TSXInternAtom(display, "RAW_ASCENT", TRUE);
2718 RAW_DESCENT = TSXInternAtom(display, "RAW_DESCENT", TRUE);
2720 return TRUE;
2723 /**********************************************************************
2724 * XFONT_SetX11Trans
2726 static BOOL XFONT_SetX11Trans( fontObject *pfo )
2728 char *fontName;
2729 Atom nameAtom;
2730 LFD* lfd;
2732 TSXGetFontProperty( pfo->fs, XA_FONT, &nameAtom );
2733 fontName = TSXGetAtomName( display, nameAtom );
2734 lfd = LFD_Parse(fontName);
2735 if (!lfd)
2737 TSXFree(fontName);
2738 return FALSE;
2741 if (lfd->pixel_size[0] != '[') {
2742 HeapFree(SystemHeap, 0, lfd);
2743 TSXFree(fontName);
2744 return FALSE;
2747 #define PX pfo->lpX11Trans
2749 sscanf(lfd->pixel_size, "[%f%f%f%f]", &PX->a, &PX->b, &PX->c, &PX->d);
2750 TSXFree(fontName);
2751 HeapFree(SystemHeap, 0, lfd);
2753 TSXGetFontProperty( pfo->fs, RAW_ASCENT, &PX->RAW_ASCENT );
2754 TSXGetFontProperty( pfo->fs, RAW_DESCENT, &PX->RAW_DESCENT );
2756 PX->pixelsize = hypot(PX->a, PX->b);
2757 PX->ascent = PX->pixelsize / 1000.0 * PX->RAW_ASCENT;
2758 PX->descent = PX->pixelsize / 1000.0 * PX->RAW_DESCENT;
2760 TRACE("[%f %f %f %f] RA = %ld RD = %ld\n",
2761 PX->a, PX->b, PX->c, PX->d,
2762 PX->RAW_ASCENT, PX->RAW_DESCENT);
2764 #undef PX
2765 return TRUE;
2768 /***********************************************************************
2769 * X Device Font Objects
2771 static X_PHYSFONT XFONT_RealizeFont( const LPLOGFONT16 plf, LPCSTR* faceMatched)
2773 UINT16 checksum;
2774 INT index = 0;
2775 fontObject* pfo;
2777 pfo = XFONT_LookupCachedFont( plf, &checksum );
2778 if( !pfo )
2780 fontMatch fm;
2781 INT i;
2783 fm.pfr = NULL;
2784 fm.pfi = NULL;
2785 fm.height = 0;
2786 fm.flags = 0;
2787 fm.plf = plf;
2789 if( XTextCaps & TC_SF_X_YINDEP ) fm.flags = FO_MATCH_XYINDEP;
2791 /* allocate new font cache entry */
2793 if( (pfo = XFONT_GetCacheEntry()) )
2795 /* initialize entry and load font */
2796 char lpLFD[MAX_LFD_LENGTH];
2797 UINT uRelaxLevel = 0;
2799 if(abs(plf->lfHeight) > MAX_FONT_SIZE) {
2800 ERR(
2801 "plf->lfHeight = %d, Creating a 100 pixel font and rescaling metrics \n",
2802 plf->lfHeight);
2803 pfo->rescale = fabs(plf->lfHeight / 100.0);
2804 if(plf->lfHeight > 0) plf->lfHeight = 100;
2805 else plf->lfHeight = -100;
2806 } else
2807 pfo->rescale = 1.0;
2809 XFONT_MatchDeviceFont( fontList, &fm );
2810 pfo->fr = fm.pfr;
2811 pfo->fi = fm.pfi;
2812 pfo->fr->fo_count++;
2813 pfo->fo_flags = fm.flags & ~FO_MATCH_MASK;
2815 pfo->lf = *plf;
2816 pfo->lfchecksum = checksum;
2820 LFD_ComposeLFD( pfo, fm.height, lpLFD, uRelaxLevel++ );
2821 if( (pfo->fs = TSXLoadQueryFont( display, lpLFD )) ) break;
2822 } while( uRelaxLevel );
2825 if(pfo->lf.lfEscapement != 0) {
2826 pfo->lpX11Trans = HeapAlloc(SystemHeap, 0, sizeof(XFONTTRANS));
2827 if(!XFONT_SetX11Trans( pfo )) {
2828 HeapFree(SystemHeap, 0, pfo->lpX11Trans);
2829 pfo->lpX11Trans = NULL;
2832 XFONT_GetLeading( &pfo->fi->df, pfo->fs,
2833 &pfo->foInternalLeading, NULL, pfo->lpX11Trans );
2834 pfo->foAvgCharWidth = (INT16)XFONT_GetAvgCharWidth(&pfo->fi->df, pfo->fs, pfo->lpX11Trans );
2835 pfo->foMaxCharWidth = (INT16)XFONT_GetMaxCharWidth(pfo->fs, pfo->lpX11Trans);
2837 /* FIXME: If we've got a soft font or
2838 * there are FO_SYNTH_... flags for the
2839 * non PROOF_QUALITY request, the engine
2840 * should rasterize characters into mono
2841 * pixmaps and store them in the pfo->lpPixmap
2842 * array (pfo->fs should be updated as well).
2843 * array (pfo->fs should be updated as well).
2844 * X11DRV_ExtTextOut() must be heavily modified
2845 * to support pixmap blitting and FO_SYNTH_...
2846 * styles.
2849 pfo->lpPixmap = NULL;
2852 if( !pfo ) /* couldn't get a new entry, get one of the cached fonts */
2854 UINT current_score, score = (UINT)(-1);
2856 i = index = fontMRU;
2857 fm.flags |= FO_MATCH_PAF;
2860 pfo = fontCache + i;
2861 fm.pfr = pfo->fr; fm.pfi = pfo->fi;
2863 current_score = XFONT_Match( &fm );
2864 if( current_score < score ) index = i;
2866 i = pfo->lru;
2867 } while( i >= 0 );
2868 pfo = fontCache + index;
2869 goto END;
2873 /* attach at the head of the lru list */
2874 pfo->lru = fontMRU;
2875 index = fontMRU = (pfo - fontCache);
2877 END:
2878 pfo->count++;
2880 TRACE("physfont %i\n", index);
2881 *faceMatched = pfo->fi->df.dfFace;
2883 return (X_PHYSFONT)(X_PFONT_MAGIC | index);
2886 /***********************************************************************
2887 * XFONT_GetFontObject
2889 fontObject* XFONT_GetFontObject( X_PHYSFONT pFont )
2891 if( CHECK_PFONT(pFont) ) return __PFONT(pFont);
2892 return NULL;
2895 /***********************************************************************
2896 * XFONT_GetFontStruct
2898 XFontStruct* XFONT_GetFontStruct( X_PHYSFONT pFont )
2900 if( CHECK_PFONT(pFont) ) return __PFONT(pFont)->fs;
2901 return NULL;
2904 /***********************************************************************
2905 * XFONT_GetFontInfo
2907 LPIFONTINFO16 XFONT_GetFontInfo( X_PHYSFONT pFont )
2909 if( CHECK_PFONT(pFont) ) return &(__PFONT(pFont)->fi->df);
2910 return NULL;
2915 /* X11DRV Interface ****************************************************
2917 * Exposed via the dc->funcs dispatch table. *
2919 ***********************************************************************/
2920 /***********************************************************************
2921 * X11DRV_FONT_SelectObject
2923 HFONT X11DRV_FONT_SelectObject( DC* dc, HFONT hfont, FONTOBJ* font )
2925 HFONT hPrevFont = 0;
2926 LOGFONT16 lf;
2927 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
2929 EnterCriticalSection( &crtsc_fonts_X11 );
2931 if( CHECK_PFONT(physDev->font) )
2932 XFONT_ReleaseCacheEntry( __PFONT(physDev->font) );
2934 lf = font->logfont;
2936 /* Make sure we don't change the sign when converting to device coords */
2937 /* FIXME - check that the other drivers do this correctly */
2938 if (lf.lfWidth)
2940 int vpt = abs(dc->vportExtX);
2941 int wnd = abs(dc->wndExtX);
2942 lf.lfWidth = (abs(lf.lfWidth) * vpt + (wnd>>1))/wnd;
2943 if (lf.lfWidth == 0)
2944 lf.lfWidth = 1; /* Minimum width */
2946 if (lf.lfHeight)
2948 int vpt = abs(dc->vportExtY);
2949 int wnd = abs(dc->wndExtY);
2950 if (lf.lfHeight > 0)
2951 lf.lfHeight = (lf.lfHeight * vpt + (wnd>>1))/wnd;
2952 else
2953 lf.lfHeight = (lf.lfHeight * vpt - (wnd>>1))/wnd;
2955 if (lf.lfHeight == 0)
2956 lf.lfHeight = MIN_FONT_SIZE;
2958 else
2959 lf.lfHeight = -(DEF_POINT_SIZE * dc->w.devCaps->logPixelsY + (72>>1)) / 72;
2962 /* Fixup aliases before passing to RealizeFont */
2963 /* alias = Windows name in the alias table */
2964 LPCSTR alias = XFONT_UnAlias( lf.lfFaceName );
2965 LPCSTR faceMatched;
2967 TRACE("hfont=%04x\n", hfont); /* to connect with the trace from RealizeFont */
2968 physDev->font = XFONT_RealizeFont( &lf, &faceMatched );
2970 /* set face to the requested facename if it matched
2971 * so that GetTextFace can get the correct face name
2973 if (alias && !strcmp(faceMatched, lf.lfFaceName))
2974 strcpy( font->logfont.lfFaceName, alias );
2975 else
2976 strcpy( font->logfont.lfFaceName, faceMatched );
2979 hPrevFont = dc->w.hFont;
2980 dc->w.hFont = hfont;
2982 LeaveCriticalSection( &crtsc_fonts_X11 );
2984 return hPrevFont;
2988 /***********************************************************************
2990 * X11DRV_EnumDeviceFonts
2992 BOOL X11DRV_EnumDeviceFonts( DC* dc, LPLOGFONT16 plf,
2993 DEVICEFONTENUMPROC proc, LPARAM lp )
2995 ENUMLOGFONTEX16 lf;
2996 NEWTEXTMETRIC16 tm;
2997 fontResource* pfr = fontList;
2998 BOOL b, bRet = 0;
3000 if( plf->lfFaceName[0] )
3002 /* enum all entries in this resource */
3003 pfr = XFONT_FindFIList( pfr, plf->lfFaceName );
3004 if( pfr )
3006 fontInfo* pfi;
3007 for( pfi = pfr->fi; pfi; pfi = pfi->next )
3009 /* Note: XFONT_GetFontMetric() will have to
3010 release the crit section, font list will
3011 have to be retraversed on return */
3013 if( (b = (*proc)( &lf, &tm,
3014 XFONT_GetFontMetric( pfi, &lf, &tm ), lp )) )
3015 bRet = b;
3016 else break;
3020 else /* enum first entry in each resource */
3021 for( ; pfr ; pfr = pfr->next )
3023 if(pfr->fi)
3025 if( (b = (*proc)( &lf, &tm,
3026 XFONT_GetFontMetric( pfr->fi, &lf, &tm ), lp )) )
3027 bRet = b;
3028 else break;
3031 return bRet;
3035 /***********************************************************************
3036 * X11DRV_GetTextExtentPoint
3038 BOOL X11DRV_GetTextExtentPoint( DC *dc, LPCSTR str, INT count,
3039 LPSIZE size )
3041 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
3042 fontObject* pfo = XFONT_GetFontObject( physDev->font );
3043 if( pfo ) {
3044 if( !pfo->lpX11Trans ) {
3045 int dir, ascent, descent;
3046 XCharStruct info;
3048 TSXTextExtents( pfo->fs, str, count, &dir, &ascent, &descent, &info );
3049 size->cx = abs((info.width + dc->w.breakRem + count *
3050 dc->w.charExtra) * dc->wndExtX / dc->vportExtX);
3051 size->cy = abs((pfo->fs->ascent + pfo->fs->descent) *
3052 dc->wndExtY / dc->vportExtY);
3053 } else {
3055 INT i;
3056 float x = 0.0, y = 0.0;
3057 for(i = 0; i < count; i++) {
3058 x += pfo->fs->per_char ?
3059 pfo->fs->per_char[str[i] - pfo->fs->min_char_or_byte2].attributes :
3060 pfo->fs->min_bounds.attributes;
3062 y = pfo->lpX11Trans->RAW_ASCENT + pfo->lpX11Trans->RAW_DESCENT;
3063 TRACE("x = %f y = %f\n", x, y);
3064 x *= pfo->lpX11Trans->pixelsize / 1000.0;
3065 y *= pfo->lpX11Trans->pixelsize / 1000.0;
3066 size->cx = fabs((x + dc->w.breakRem + count * dc->w.charExtra) *
3067 dc->wndExtX / dc->vportExtX);
3068 size->cy = fabs(y * dc->wndExtY / dc->vportExtY);
3070 size->cx *= pfo->rescale;
3071 size->cy *= pfo->rescale;
3072 return TRUE;
3074 return FALSE;
3078 /***********************************************************************
3079 * X11DRV_GetTextMetrics
3081 BOOL X11DRV_GetTextMetrics(DC *dc, TEXTMETRICA *metrics)
3083 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
3085 if( CHECK_PFONT(physDev->font) )
3087 fontObject* pfo = __PFONT(physDev->font);
3088 XFONT_GetTextMetrics( pfo, metrics );
3090 return TRUE;
3092 return FALSE;
3096 /***********************************************************************
3097 * X11DRV_GetCharWidth
3099 BOOL X11DRV_GetCharWidth( DC *dc, UINT firstChar, UINT lastChar,
3100 LPINT buffer )
3102 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
3103 fontObject* pfo = XFONT_GetFontObject( physDev->font );
3105 if( pfo )
3107 int i;
3109 if (pfo->fs->per_char == NULL)
3110 for (i = firstChar; i <= lastChar; i++)
3111 if(pfo->lpX11Trans)
3112 *buffer++ = pfo->fs->min_bounds.attributes *
3113 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3114 else
3115 *buffer++ = pfo->fs->min_bounds.width * pfo->rescale;
3116 else
3118 XCharStruct *cs, *def;
3119 static XCharStruct __null_char = { 0, 0, 0, 0, 0, 0 };
3121 CI_GET_CHAR_INFO(pfo->fs, pfo->fs->default_char, &__null_char,
3122 def);
3124 for (i = firstChar; i <= lastChar; i++)
3126 if (i >= pfo->fs->min_char_or_byte2 &&
3127 i <= pfo->fs->max_char_or_byte2)
3129 cs = &pfo->fs->per_char[(i - pfo->fs->min_char_or_byte2)];
3130 if (CI_NONEXISTCHAR(cs)) cs = def;
3131 } else cs = def;
3132 if(pfo->lpX11Trans)
3133 *buffer++ = MAX(cs->attributes, 0) *
3134 pfo->lpX11Trans->pixelsize / 1000.0 * pfo->rescale;
3135 else
3136 *buffer++ = MAX(cs->width, 0 ) * pfo->rescale;
3140 return TRUE;
3142 return FALSE;
3145 #endif /* !defined(X_DISPLAY_MISSING) */
3147 /***********************************************************************
3149 * Font Resource API *
3151 ***********************************************************************/
3152 /***********************************************************************
3153 * AddFontResource16 (GDI.119)
3155 * Can be either .FON, or .FNT, or .TTF, or .FOT font file.
3157 * FIXME: Load header and find the best-matching font in the fontList;
3158 * fixup dfPoints if all metrics are identical, otherwise create
3159 * new fontAlias. When soft font support is ready this will
3160 * simply create a new fontResource ('filename' will go into
3161 * the pfr->resource field) with FR_SOFTFONT/FR_SOFTRESOURCE
3162 * flag set.
3164 INT16 WINAPI AddFontResource16( LPCSTR filename )
3166 return AddFontResourceA( filename );
3170 /***********************************************************************
3171 * AddFontResource32A (GDI32.2)
3173 INT WINAPI AddFontResourceA( LPCSTR str )
3175 FIXME("(%s): stub\n", debugres_a(str));
3176 return 1;
3180 /***********************************************************************
3181 * AddFontResource32W (GDI32.4)
3183 INT WINAPI AddFontResourceW( LPCWSTR str )
3185 FIXME("(%s): stub\n", debugres_w(str) );
3186 return 1;
3189 /***********************************************************************
3190 * RemoveFontResource16 (GDI.136)
3192 BOOL16 WINAPI RemoveFontResource16( SEGPTR str )
3194 FIXME("(%s): stub\n", debugres_a(PTR_SEG_TO_LIN(str)));
3195 return TRUE;
3199 /***********************************************************************
3200 * RemoveFontResource32A (GDI32.284)
3202 BOOL WINAPI RemoveFontResourceA( LPCSTR str )
3204 /* This is how it should look like */
3206 fontResource** ppfr;
3207 BOOL32 retVal = FALSE;
3209 EnterCriticalSection( &crtsc_fonts_X11 );
3210 for( ppfr = &fontList; *ppfr; ppfr = &(*ppfr)->next )
3211 if( !strcasecmp( (*ppfr)->lfFaceName, str ) )
3213 if(((*ppfr)->fr_flags & (FR_SOFTFONT | FR_SOFTRESOURCE)) &&
3214 (*ppfr)->hOwnerProcess == GetCurrentProcess() )
3216 if( (*ppfr)->fo_count )
3217 (*ppfr)->fr_flags |= FR_REMOVED;
3218 else
3219 XFONT_RemoveFontResource( ppfr );
3221 retVal = TRUE;
3223 LeaveCriticalSection( &crtsc_fonts_X11 );
3224 return retVal;
3226 FIXME("(%s): stub\n", debugres_a(str));
3227 return TRUE;
3231 /***********************************************************************
3232 * RemoveFontResource32W (GDI32.286)
3234 BOOL WINAPI RemoveFontResourceW( LPCWSTR str )
3236 FIXME("(%s): stub\n", debugres_w(str) );
3237 return TRUE;