Moved GetTextMetrics to unicode.
[wine] / dlls / wineps / font.c
1 /*
2  *      PostScript driver font functions
3  *
4  *      Copyright 1998  Huw D M Davies
5  *
6  */
7 #include <string.h>
8 #include "winspool.h"
9 #include "psdrv.h"
10 #include "debugtools.h"
11 #include "gdi.h"
12 #include "winerror.h"
13
14 DEFAULT_DEBUG_CHANNEL(psdrv);
15
16
17 /***********************************************************************
18  *           PSDRV_FONT_SelectObject
19  */
20 HFONT16 PSDRV_FONT_SelectObject( DC * dc, HFONT16 hfont,
21                                  FONTOBJ *font )
22 {
23     HFONT16 prevfont = dc->hFont;
24     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
25     LOGFONTW *lf = &(font->logfont);
26     BOOL bd = FALSE, it = FALSE;
27     AFMLISTENTRY *afmle;
28     AFM *afm;
29     FONTFAMILY *family;
30     char FaceName[LF_FACESIZE];
31
32
33     TRACE("FaceName = '%s' Height = %ld Italic = %d Weight = %ld\n",
34           debugstr_w(lf->lfFaceName), lf->lfHeight, lf->lfItalic,
35           lf->lfWeight);
36
37     dc->hFont = hfont;
38
39     if(lf->lfItalic)
40         it = TRUE;
41     if(lf->lfWeight > 550)
42         bd = TRUE;
43     WideCharToMultiByte(CP_ACP, 0, lf->lfFaceName, -1,
44                         FaceName, sizeof(FaceName), NULL, NULL);
45     
46     if(FaceName[0] == '\0') {
47         switch(lf->lfPitchAndFamily & 0xf0) {
48         case FF_DONTCARE:
49             break;
50         case FF_ROMAN:
51         case FF_SCRIPT:
52             strcpy(FaceName, "Times");
53             break;
54         case FF_SWISS:
55             strcpy(FaceName, "Helvetica");
56             break;
57         case FF_MODERN:
58             strcpy(FaceName, "Courier");
59             break;
60         case FF_DECORATIVE:
61             strcpy(FaceName, "Symbol");
62             break;
63         }
64     }
65
66     if(FaceName[0] == '\0') {
67         switch(lf->lfPitchAndFamily & 0x0f) {
68         case VARIABLE_PITCH:
69             strcpy(FaceName, "Times");
70             break;
71         default:
72             strcpy(FaceName, "Courier");
73             break;
74         }
75     }
76
77     TRACE("Trying to find facename '%s'\n", FaceName);
78
79     /* Look for a matching font family */
80     for(family = physDev->pi->Fonts; family; family = family->next) {
81         if(!strcasecmp(FaceName, family->FamilyName))
82             break;
83     }
84     if(!family) {
85         /* Fallback for Window's font families to common PostScript families */
86         if(!strcmp(FaceName, "Arial"))
87             strcpy(FaceName, "Helvetica");
88         else if(!strcmp(FaceName, "System"))
89             strcpy(FaceName, "Helvetica");
90         else if(!strcmp(FaceName, "Times New Roman"))
91             strcpy(FaceName, "Times");
92         else if(!strcmp(FaceName, "Courier New"))
93             strcpy(FaceName, "Courier");
94
95         for(family = physDev->pi->Fonts; family; family = family->next) {
96             if(!strcmp(FaceName, family->FamilyName))
97                 break;
98         }
99     }
100     /* If all else fails, use the first font defined for the printer */
101     if(!family)
102         family = physDev->pi->Fonts;
103
104     TRACE("Got family '%s'\n", family->FamilyName);
105
106     for(afmle = family->afmlist; afmle; afmle = afmle->next) {
107         if( (bd == (afmle->afm->Weight == FW_BOLD)) && 
108             (it == (afmle->afm->ItalicAngle != 0.0)) )
109                 break;
110     }
111     if(!afmle)
112         afmle = family->afmlist; /* not ideal */
113     
114     afm = afmle->afm;
115
116     physDev->font.afm = afm;
117     physDev->font.tm.tmHeight = INTERNAL_YWSTODS(dc, lf->lfHeight);
118     if(physDev->font.tm.tmHeight < 0) {
119         physDev->font.tm.tmHeight *= - (afm->FullAscender - afm->Descender) /
120                                        (afm->Ascender - afm->Descender);
121         TRACE("Fixed -ve height to %ld\n", physDev->font.tm.tmHeight);
122     }
123     physDev->font.size = physDev->font.tm.tmHeight * 1000.0 /
124                                 (afm->FullAscender - afm->Descender);
125     physDev->font.scale = physDev->font.size / 1000.0;
126     physDev->font.escapement = lf->lfEscapement;
127     physDev->font.tm.tmAscent = afm->FullAscender * physDev->font.scale;
128     physDev->font.tm.tmDescent = -afm->Descender * physDev->font.scale;
129     physDev->font.tm.tmInternalLeading = (afm->FullAscender - afm->Ascender)
130                                                 * physDev->font.scale;
131     physDev->font.tm.tmExternalLeading = (1000.0 - afm->FullAscender)
132                                                 * physDev->font.scale; /* ?? */
133     physDev->font.tm.tmAveCharWidth = afm->CharWidths[120] * /* x */
134                                                    physDev->font.scale;
135     physDev->font.tm.tmMaxCharWidth = afm->CharWidths[77] * /* M */
136                                            physDev->font.scale;
137     physDev->font.tm.tmWeight = afm->Weight;
138     physDev->font.tm.tmItalic = afm->ItalicAngle != 0.0;
139     physDev->font.tm.tmUnderlined = lf->lfUnderline;
140     physDev->font.tm.tmStruckOut = lf->lfStrikeOut;
141     physDev->font.tm.tmFirstChar = 32;
142     physDev->font.tm.tmLastChar = 251;
143     physDev->font.tm.tmDefaultChar = 128;
144     physDev->font.tm.tmBreakChar = 32;
145     physDev->font.tm.tmPitchAndFamily = afm->IsFixedPitch ? 0 :
146                                           TMPF_FIXED_PITCH;
147     physDev->font.tm.tmPitchAndFamily |= TMPF_DEVICE;
148     physDev->font.tm.tmCharSet = ANSI_CHARSET;
149     physDev->font.tm.tmOverhang = 0;
150     physDev->font.tm.tmDigitizedAspectX = dc->devCaps->logPixelsY;
151     physDev->font.tm.tmDigitizedAspectY = dc->devCaps->logPixelsX;
152
153     physDev->font.set = FALSE;
154
155     TRACE("Selected PS font '%s' size %d weight %ld.\n", 
156           physDev->font.afm->FontName, physDev->font.size,
157           physDev->font.tm.tmWeight );
158     TRACE("H = %ld As = %ld Des = %ld IL = %ld EL = %ld\n",
159           physDev->font.tm.tmHeight, physDev->font.tm.tmAscent,
160           physDev->font.tm.tmDescent, physDev->font.tm.tmInternalLeading,
161           physDev->font.tm.tmExternalLeading);
162
163     return prevfont;
164 }
165
166 /***********************************************************************
167  *           PSDRV_GetTextMetrics
168  */
169 BOOL PSDRV_GetTextMetrics(DC *dc, TEXTMETRICW *metrics)
170 {
171     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
172
173     memcpy(metrics, &(physDev->font.tm), sizeof(physDev->font.tm));
174     return TRUE;
175 }
176
177 /***********************************************************************
178  *           PSDRV_UnicodeToANSI
179  */
180 char PSDRV_UnicodeToANSI(int u)
181 {
182     if((u & 0xff) == u)
183         return u;
184     switch(u) {
185     case 0x2013: /* endash */
186         return 0x96;
187     case 0x2014: /* emdash */
188         return 0x97;
189     case 0x2018: /* quoteleft */
190         return 0x91;
191     case 0x2019: /* quoteright */
192         return 0x92;
193     case 0x201c: /* quotedblleft */
194        return 0x93;
195     case 0x201d: /* quotedblright */
196         return 0x94;
197     case 0x2022: /* bullet */
198         return 0x95;
199     default:
200         WARN("Umapped unicode char U%04x\n", u);
201         return 0xff;
202     }
203 }
204 /***********************************************************************
205  *           PSDRV_GetTextExtentPoint
206  */
207 BOOL PSDRV_GetTextExtentPoint( DC *dc, LPCWSTR str, INT count,
208                                   LPSIZE size )
209 {
210     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
211     INT i;
212     float width;
213
214     width = 0.0;
215
216     for(i = 0; i < count && str[i]; i++) {
217         char c = PSDRV_UnicodeToANSI(str[i]);
218         width += physDev->font.afm->CharWidths[(int)(unsigned char)c];
219 /*      TRACE(psdrv, "Width after %dth char '%c' = %f\n", i, str[i], width);*/
220     }
221     width *= physDev->font.scale;
222     TRACE("Width after scale (%f) is %f\n", physDev->font.scale, width);
223
224     size->cx = GDI_ROUND((FLOAT)width * dc->xformVport2World.eM11);
225     size->cy = GDI_ROUND((FLOAT)physDev->font.tm.tmHeight  * dc->xformVport2World.eM22);
226
227     return TRUE;
228 }
229
230
231 /***********************************************************************
232  *           PSDRV_GetCharWidth
233  */
234 BOOL PSDRV_GetCharWidth( DC *dc, UINT firstChar, UINT lastChar,
235                            LPINT buffer )
236 {
237     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
238     UINT i;
239
240     TRACE("first = %d last = %d\n", firstChar, lastChar);
241
242     if(lastChar > 0xff) return FALSE;
243     for( i = firstChar; i <= lastChar; i++ )
244         *buffer++ = physDev->font.afm->CharWidths[i] * physDev->font.scale;
245
246     return TRUE;
247 }
248
249     
250 /***********************************************************************
251  *           PSDRV_SetFont
252  */
253 BOOL PSDRV_SetFont( DC *dc )
254 {
255     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
256     BOOL ReEncode = FALSE;
257
258     PSDRV_WriteSetColor(dc, &physDev->font.color);
259     if(physDev->font.set) return TRUE;
260
261     if(physDev->font.afm->EncodingScheme && 
262        !strcmp(physDev->font.afm->EncodingScheme, "AdobeStandardEncoding"))
263         ReEncode = TRUE;
264     if(ReEncode)
265         PSDRV_WriteReencodeFont(dc);
266     PSDRV_WriteSetFont(dc, ReEncode);
267     physDev->font.set = TRUE;
268     return TRUE;
269 }
270
271
272 /***********************************************************************
273  *           PSDRV_GetFontMetric
274  */
275 static UINT PSDRV_GetFontMetric(HDC hdc, AFM *pafm, NEWTEXTMETRICEXW *pTM, 
276                                 ENUMLOGFONTEXW *pLF, INT16 size)
277
278 {
279     DC *dc = DC_GetDCPtr( hdc );
280     float scale = size / (pafm->FullAscender - pafm->Descender);
281
282     if (!dc) return FALSE;
283
284     memset( pLF, 0, sizeof(*pLF) );
285     memset( pTM, 0, sizeof(*pTM) );
286
287 #define plf ((LPLOGFONTW)pLF)
288 #define ptm ((LPNEWTEXTMETRICW)pTM)
289     plf->lfHeight    = ptm->tmHeight       = size;
290     plf->lfWidth     = ptm->tmAveCharWidth = pafm->CharWidths[120] * scale;
291     plf->lfWeight    = ptm->tmWeight       = pafm->Weight;
292     plf->lfItalic    = ptm->tmItalic       = pafm->ItalicAngle != 0.0;
293     plf->lfUnderline = ptm->tmUnderlined   = 0;
294     plf->lfStrikeOut = ptm->tmStruckOut    = 0;
295     plf->lfCharSet   = ptm->tmCharSet      = ANSI_CHARSET;
296
297     /* convert pitch values */
298
299     ptm->tmPitchAndFamily = pafm->IsFixedPitch ? 0 : TMPF_FIXED_PITCH;
300     ptm->tmPitchAndFamily |= TMPF_DEVICE;
301     plf->lfPitchAndFamily = 0;
302
303     MultiByteToWideChar(CP_ACP, 0, pafm->FamilyName, -1,
304                         plf->lfFaceName, LF_FACESIZE);
305 #undef plf
306
307     ptm->tmAscent = pafm->FullAscender * scale;
308     ptm->tmDescent = -pafm->Descender * scale;
309     ptm->tmInternalLeading = (pafm->FullAscender - pafm->Ascender) * scale;
310     ptm->tmMaxCharWidth = pafm->CharWidths[77] * scale;
311     ptm->tmDigitizedAspectX = dc->devCaps->logPixelsY;
312     ptm->tmDigitizedAspectY = dc->devCaps->logPixelsX;
313
314     *(INT*)&ptm->tmFirstChar = 32;
315
316     GDI_ReleaseObj( hdc );
317     /* return font type */
318
319     return DEVICE_FONTTYPE;
320 #undef ptm
321 }
322
323 /***********************************************************************
324  *           PSDRV_EnumDeviceFonts
325  */
326 BOOL PSDRV_EnumDeviceFonts( HDC hdc, LPLOGFONTW plf, 
327                             DEVICEFONTENUMPROC proc, LPARAM lp )
328 {
329     ENUMLOGFONTEXW      lf;
330     NEWTEXTMETRICEXW    tm;
331     BOOL                b, bRet = 0;
332     AFMLISTENTRY        *afmle;
333     FONTFAMILY          *family;
334     PSDRV_PDEVICE       *physDev;
335     char                FaceName[LF_FACESIZE];
336     DC *dc = DC_GetDCPtr( hdc );
337     if (!dc) return FALSE;
338
339     physDev = (PSDRV_PDEVICE *)dc->physDev;
340     /* FIXME!! should reevaluate dc->physDev after every callback */
341     GDI_ReleaseObj( hdc );
342
343     if( plf->lfFaceName[0] ) {
344         WideCharToMultiByte(CP_ACP, 0, plf->lfFaceName, -1,
345                           FaceName, sizeof(FaceName), NULL, NULL);
346         TRACE("lfFaceName = '%s'\n", FaceName);
347         for(family = physDev->pi->Fonts; family; family = family->next) {
348             if(!strncmp(FaceName, family->FamilyName, 
349                         strlen(family->FamilyName)))
350                 break;
351         }
352         if(family) {
353             for(afmle = family->afmlist; afmle; afmle = afmle->next) {
354                 TRACE("Got '%s'\n", afmle->afm->FontName);
355                 if( (b = (*proc)( &lf, &tm, 
356                         PSDRV_GetFontMetric( hdc, afmle->afm, &tm, &lf, 200 ),
357                                   lp )) )
358                      bRet = b;
359                 else break;
360             }
361         }
362     } else {
363
364         TRACE("lfFaceName = NULL\n");
365         for(family = physDev->pi->Fonts; family; family = family->next) {
366             afmle = family->afmlist;
367             TRACE("Got '%s'\n", afmle->afm->FontName);
368             if( (b = (*proc)( &lf, &tm, 
369                    PSDRV_GetFontMetric( hdc, afmle->afm, &tm, &lf, 200 ), 
370                               lp )) )
371                 bRet = b;
372             else break;
373         }
374     }
375     return bRet;
376 }