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