Use a case-insensitive comparison to match PostScript font names.
[wine.git] / dlls / wineps / font.c
blob7ca351082ca22ac5b3dbdf9353243b8aac44dae0
1 /*
2 * PostScript driver font functions
4 * Copyright 1998 Huw D M Davies
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"
14 DEFAULT_DEBUG_CHANNEL(psdrv);
17 /***********************************************************************
18 * PSDRV_FONT_SelectObject
20 HFONT16 PSDRV_FONT_SelectObject( DC * dc, HFONT16 hfont,
21 FONTOBJ *font )
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];
33 TRACE("FaceName = '%s' Height = %d Italic = %d Weight = %d\n",
34 lf->lfFaceName, lf->lfHeight, lf->lfItalic, lf->lfWeight);
36 dc->hFont = hfont;
38 if(lf->lfItalic)
39 it = TRUE;
40 if(lf->lfWeight > 550)
41 bd = TRUE;
42 strcpy(FaceName, lf->lfFaceName);
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;
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;
75 TRACE("Trying to find facename '%s'\n", FaceName);
77 /* Look for a matching font family */
78 for(family = physDev->pi->Fonts; family; family = family->next) {
79 if(!strcasecmp(FaceName, family->FamilyName))
80 break;
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;
95 /* If all else fails, use the first font defined for the printer */
96 if(!family)
97 family = physDev->pi->Fonts;
99 TRACE("Got family '%s'\n", family->FamilyName);
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;
106 if(!afmle)
107 afmle = family->afmlist; /* not ideal */
109 afm = afmle->afm;
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);
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;
148 physDev->font.set = FALSE;
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);
158 return prevfont;
161 /***********************************************************************
162 * PSDRV_GetTextMetrics
164 BOOL PSDRV_GetTextMetrics(DC *dc, TEXTMETRICA *metrics)
166 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
168 memcpy(metrics, &(physDev->font.tm), sizeof(physDev->font.tm));
169 return TRUE;
172 /***********************************************************************
173 * PSDRV_UnicodeToANSI
175 char PSDRV_UnicodeToANSI(int u)
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;
199 /***********************************************************************
200 * PSDRV_GetTextExtentPoint
202 BOOL PSDRV_GetTextExtentPoint( DC *dc, LPCWSTR str, INT count,
203 LPSIZE size )
205 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
206 INT i;
207 float width;
209 size->cy = YDSTOLS(dc, physDev->font.tm.tmHeight);
210 width = 0.0;
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);*/
217 width *= physDev->font.scale;
218 TRACE("Width after scale (%f) is %f\n", physDev->font.scale, width);
219 size->cx = XDSTOLS(dc, width);
221 return TRUE;
225 /***********************************************************************
226 * PSDRV_GetCharWidth
228 BOOL PSDRV_GetCharWidth( DC *dc, UINT firstChar, UINT lastChar,
229 LPINT buffer )
231 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
232 UINT i;
234 TRACE("first = %d last = %d\n", firstChar, lastChar);
236 if(lastChar > 0xff) return FALSE;
237 for( i = firstChar; i <= lastChar; i++ )
238 *buffer++ = physDev->font.afm->CharWidths[i] * physDev->font.scale;
240 return TRUE;
244 /***********************************************************************
245 * PSDRV_SetFont
247 BOOL PSDRV_SetFont( DC *dc )
249 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
250 BOOL ReEncode = FALSE;
252 PSDRV_WriteSetColor(dc, &physDev->font.color);
253 if(physDev->font.set) return TRUE;
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;
266 /***********************************************************************
267 * PSDRV_GetFontMetric
269 static UINT PSDRV_GetFontMetric(HDC hdc, AFM *pafm, NEWTEXTMETRIC16 *pTM,
270 ENUMLOGFONTEX16 *pLF, INT16 size)
273 DC *dc = DC_GetDCPtr( hdc );
274 float scale = size / (pafm->FullAscender - pafm->Descender);
276 if (!dc) return FALSE;
278 memset( pLF, 0, sizeof(*pLF) );
279 memset( pTM, 0, sizeof(*pTM) );
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;
290 /* convert pitch values */
292 pTM->tmPitchAndFamily = pafm->IsFixedPitch ? 0 : TMPF_FIXED_PITCH;
293 pTM->tmPitchAndFamily |= TMPF_DEVICE;
294 plf->lfPitchAndFamily = 0;
296 lstrcpynA( plf->lfFaceName, pafm->FamilyName, LF_FACESIZE );
297 #undef plf
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;
306 *(INT*)&pTM->tmFirstChar = 32;
308 GDI_ReleaseObj( hdc );
309 /* return font type */
310 return DEVICE_FONTTYPE;
314 /***********************************************************************
315 * PSDRV_EnumDeviceFonts
317 BOOL PSDRV_EnumDeviceFonts( HDC hdc, LPLOGFONT16 plf,
318 DEVICEFONTENUMPROC proc, LPARAM lp )
320 ENUMLOGFONTEX16 lf;
321 NEWTEXTMETRIC16 tm;
322 BOOL b, bRet = 0;
323 AFMLISTENTRY *afmle;
324 FONTFAMILY *family;
325 PSDRV_PDEVICE *physDev;
327 DC *dc = DC_GetDCPtr( hdc );
328 if (!dc) return FALSE;
330 physDev = (PSDRV_PDEVICE *)dc->physDev;
331 /* FIXME!! should reevaluate dc->physDev after every callback */
332 GDI_ReleaseObj( hdc );
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;
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;
351 } else {
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;
364 return bRet;