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