Faster serial speed cases for non Linux systems.
[wine] / dlls / wineps / font.c
1 /*
2  *      PostScript driver font functions
3  *
4  *      Copyright 1998  Huw D M Davies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include <string.h>
21 #include <stdlib.h>         /* for bsearch() */
22 #include "winspool.h"
23 #include "psdrv.h"
24 #include "wine/debug.h"
25 #include "winerror.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
28
29 /***********************************************************************
30  *           is_stock_font
31  */
32 inline static BOOL is_stock_font( HFONT font )
33 {
34     int i;
35     for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
36     {
37         if (i != DEFAULT_PALETTE && font == GetStockObject(i)) return TRUE;
38     }
39     return FALSE;
40 }
41
42
43 /*******************************************************************************
44  *  ScaleFont
45  *
46  *  Scale font to requested lfHeight
47  *
48  */
49 inline static float round(float f)
50 {
51     return (f > 0) ? (f + 0.5) : (f - 0.5);
52 }
53
54 static VOID ScaleFont(const AFM *afm, LONG lfHeight, PSFONT *font,
55         TEXTMETRICW *tm)
56 {
57     const WINMETRICS    *wm = &(afm->WinMetrics);
58     USHORT              usUnitsPerEm, usWinAscent, usWinDescent;
59     SHORT               sAscender, sDescender, sLineGap, sTypoAscender;
60     SHORT               sTypoDescender, sTypoLineGap, sAvgCharWidth;
61     
62     TRACE("'%s' %li\n", afm->FontName, lfHeight);
63                 
64     if (lfHeight < 0)                                   /* match em height */
65     {
66         font->scale = - ((float)lfHeight / (float)(wm->usUnitsPerEm));
67     }
68     else                                                /* match cell height */
69     {
70         font->scale = (float)lfHeight /
71                 (float)(wm->usWinAscent + wm->usWinDescent);
72     }
73     
74     font->size = (INT)round(font->scale * (float)wm->usUnitsPerEm);
75     font->set = FALSE;
76     
77     usUnitsPerEm = (USHORT)round((float)(wm->usUnitsPerEm) * font->scale);
78     sAscender = (SHORT)round((float)(wm->sAscender) * font->scale);
79     sDescender = (SHORT)round((float)(wm->sDescender) * font->scale);
80     sLineGap = (SHORT)round((float)(wm->sLineGap) * font->scale);
81     sTypoAscender = (SHORT)round((float)(wm->sTypoAscender) * font->scale);
82     sTypoDescender = (SHORT)round((float)(wm->sTypoDescender) * font->scale);
83     sTypoLineGap = (SHORT)round((float)(wm->sTypoLineGap) * font->scale);
84     usWinAscent = (USHORT)round((float)(wm->usWinAscent) * font->scale);
85     usWinDescent = (USHORT)round((float)(wm->usWinDescent) * font->scale);
86     sAvgCharWidth = (SHORT)round((float)(wm->sAvgCharWidth) * font->scale);
87     
88     tm->tmAscent = (LONG)usWinAscent;
89     tm->tmDescent = (LONG)usWinDescent;
90     tm->tmHeight = tm->tmAscent + tm->tmDescent;
91     
92     tm->tmInternalLeading = tm->tmHeight - (LONG)usUnitsPerEm;
93     if (tm->tmInternalLeading < 0)
94         tm->tmInternalLeading = 0;
95         
96     tm->tmExternalLeading =
97             (LONG)(sAscender - sDescender + sLineGap) - tm->tmHeight;
98     if (tm->tmExternalLeading < 0)
99         tm->tmExternalLeading = 0;
100     
101     tm->tmAveCharWidth = (LONG)sAvgCharWidth;
102         
103     tm->tmWeight = afm->Weight;
104     tm->tmItalic = (afm->ItalicAngle != 0.0);
105     tm->tmUnderlined = 0;
106     tm->tmStruckOut = 0;
107     tm->tmFirstChar = (WCHAR)(afm->Metrics[0].UV);
108     tm->tmLastChar = (WCHAR)(afm->Metrics[afm->NumofMetrics - 1].UV);
109     tm->tmDefaultChar = 0x001f;         /* Win2K does this - FIXME? */
110     tm->tmBreakChar = tm->tmFirstChar;          /* should be 'space' */
111     
112     tm->tmPitchAndFamily = TMPF_DEVICE | TMPF_VECTOR;
113     if (!afm->IsFixedPitch)
114         tm->tmPitchAndFamily |= TMPF_FIXED_PITCH;   /* yes, it's backwards */
115     if (wm->usUnitsPerEm != 1000)
116         tm->tmPitchAndFamily |= TMPF_TRUETYPE;
117     
118     tm->tmCharSet = ANSI_CHARSET;       /* FIXME */
119     tm->tmOverhang = 0;
120     
121     /*
122      *  This is kludgy.  font->scale is used in several places in the driver
123      *  to adjust PostScript-style metrics.  Since these metrics have been
124      *  "normalized" to an em-square size of 1000, font->scale needs to be
125      *  similarly adjusted..
126      */
127      
128     font->scale *= (float)wm->usUnitsPerEm / 1000.0;
129      
130     tm->tmMaxCharWidth = (LONG)round(
131             (afm->FontBBox.urx - afm->FontBBox.llx) * font->scale);
132     
133     TRACE("Selected PS font '%s' size %d weight %ld.\n", afm->FontName,
134             font->size, tm->tmWeight );
135     TRACE("H = %ld As = %ld Des = %ld IL = %ld EL = %ld\n", tm->tmHeight,
136             tm->tmAscent, tm->tmDescent, tm->tmInternalLeading,
137             tm->tmExternalLeading);
138 }
139
140 /***********************************************************************
141  *           SelectFont   (WINEPS.@)
142  */
143 HFONT PSDRV_SelectFont( PSDRV_PDEVICE *physDev, HFONT hfont )
144 {
145     LOGFONTW lf;
146     BOOL bd = FALSE, it = FALSE;
147     AFMLISTENTRY *afmle;
148     FONTFAMILY *family;
149     char FaceName[LF_FACESIZE];
150
151     if (!GetObjectW( hfont, sizeof(lf), &lf )) return 0;
152
153     TRACE("FaceName = %s Height = %ld Italic = %d Weight = %ld\n",
154           debugstr_w(lf.lfFaceName), lf.lfHeight, lf.lfItalic,
155           lf.lfWeight);
156
157     if(lf.lfItalic)
158         it = TRUE;
159     if(lf.lfWeight > 550)
160         bd = TRUE;
161     WideCharToMultiByte(CP_ACP, 0, lf.lfFaceName, -1,
162                         FaceName, sizeof(FaceName), NULL, NULL);
163     
164     if(FaceName[0] == '\0') {
165         switch(lf.lfPitchAndFamily & 0xf0) {
166         case FF_DONTCARE:
167             break;
168         case FF_ROMAN:
169         case FF_SCRIPT:
170             strcpy(FaceName, "Times");
171             break;
172         case FF_SWISS:
173             strcpy(FaceName, "Helvetica");
174             break;
175         case FF_MODERN:
176             strcpy(FaceName, "Courier");
177             break;
178         case FF_DECORATIVE:
179             strcpy(FaceName, "Symbol");
180             break;
181         }
182     }
183
184     if(FaceName[0] == '\0') {
185         switch(lf.lfPitchAndFamily & 0x0f) {
186         case VARIABLE_PITCH:
187             strcpy(FaceName, "Times");
188             break;
189         default:
190             strcpy(FaceName, "Courier");
191             break;
192         }
193     }
194
195     if (physDev->pi->FontSubTableSize != 0)
196     {
197         DWORD i;
198
199         for (i = 0; i < physDev->pi->FontSubTableSize; ++i)
200         {
201             if (!strcasecmp (FaceName,
202                     physDev->pi->FontSubTable[i].pValueName))
203             {
204                 TRACE ("substituting facename '%s' for '%s'\n",
205                         (LPSTR) physDev->pi->FontSubTable[i].pData, FaceName);
206                 if (strlen ((LPSTR) physDev->pi->FontSubTable[i].pData) <
207                         LF_FACESIZE)
208                     strcpy (FaceName,
209                             (LPSTR) physDev->pi->FontSubTable[i].pData);
210                 else
211                     WARN ("Facename '%s' is too long; ignoring substitution\n",
212                             (LPSTR) physDev->pi->FontSubTable[i].pData);
213                 break;
214             }
215         }
216     }
217
218     TRACE("Trying to find facename '%s'\n", FaceName);
219
220     /* Look for a matching font family */
221     for(family = physDev->pi->Fonts; family; family = family->next) {
222         if(!strcasecmp(FaceName, family->FamilyName))
223             break;
224     }
225     if(!family) {
226         /* Fallback for Window's font families to common PostScript families */
227         if(!strcmp(FaceName, "Arial"))
228             strcpy(FaceName, "Helvetica");
229         else if(!strcmp(FaceName, "System"))
230             strcpy(FaceName, "Helvetica");
231         else if(!strcmp(FaceName, "Times New Roman"))
232             strcpy(FaceName, "Times");
233         else if(!strcmp(FaceName, "Courier New"))
234             strcpy(FaceName, "Courier");
235
236         for(family = physDev->pi->Fonts; family; family = family->next) {
237             if(!strcmp(FaceName, family->FamilyName))
238                 break;
239         }
240     }
241     /* If all else fails, use the first font defined for the printer */
242     if(!family)
243         family = physDev->pi->Fonts;
244
245     TRACE("Got family '%s'\n", family->FamilyName);
246
247     for(afmle = family->afmlist; afmle; afmle = afmle->next) {
248         if( (bd == (afmle->afm->Weight == FW_BOLD)) && 
249             (it == (afmle->afm->ItalicAngle != 0.0)) )
250                 break;
251     }
252     if(!afmle)
253         afmle = family->afmlist; /* not ideal */
254         
255     TRACE("Got font '%s'\n", afmle->afm->FontName);
256     
257     physDev->font.afm = afmle->afm;
258     /* stock fonts ignore the mapping mode */
259     if (!is_stock_font( hfont )) lf.lfHeight = INTERNAL_YWSTODS(physDev->dc, lf.lfHeight);
260     ScaleFont(physDev->font.afm, lf.lfHeight,
261             &(physDev->font), &(physDev->font.tm));
262     
263     physDev->font.escapement = lf.lfEscapement;
264     
265     /* Does anyone know if these are supposed to be reversed like this? */
266     
267     physDev->font.tm.tmDigitizedAspectX = physDev->logPixelsY;
268     physDev->font.tm.tmDigitizedAspectY = physDev->logPixelsX;
269     
270     return TRUE; /* We'll use a device font for now */
271 }
272
273 /***********************************************************************
274  *           PSDRV_GetTextMetrics
275  */
276 BOOL PSDRV_GetTextMetrics(PSDRV_PDEVICE *physDev, TEXTMETRICW *metrics)
277 {
278     memcpy(metrics, &(physDev->font.tm), sizeof(physDev->font.tm));
279     return TRUE;
280 }
281
282 /******************************************************************************
283  *      PSDRV_UVMetrics
284  *
285  *  Find the AFMMETRICS for a given UV.  Returns first glyph in the font
286  *  (space?) if the font does not have a glyph for the given UV.
287  */
288 static int MetricsByUV(const void *a, const void *b)
289 {
290     return (int)(((const AFMMETRICS *)a)->UV - ((const AFMMETRICS *)b)->UV);
291 }
292  
293 const AFMMETRICS *PSDRV_UVMetrics(LONG UV, const AFM *afm)
294 {
295     AFMMETRICS          key;
296     const AFMMETRICS    *needle;
297     
298     /*
299      *  Ugly work-around for symbol fonts.  Wine is sending characters which
300      *  belong in the Unicode private use range (U+F020 - U+F0FF) as ASCII
301      *  characters (U+0020 - U+00FF).
302      */
303     
304     if ((afm->Metrics->UV & 0xff00) == 0xf000 && UV < 0x100)
305         UV |= 0xf000;
306     
307     key.UV = UV;
308     
309     needle = bsearch(&key, afm->Metrics, afm->NumofMetrics, sizeof(AFMMETRICS),
310             MetricsByUV);
311
312     if (needle == NULL)
313     {
314         WARN("No glyph for U+%.4lX in %s\n", UV, afm->FontName);
315         needle = afm->Metrics;
316     }
317         
318     return needle;
319 }
320
321 /***********************************************************************
322  *           PSDRV_GetTextExtentPoint
323  */
324 BOOL PSDRV_GetTextExtentPoint(PSDRV_PDEVICE *physDev, LPCWSTR str, INT count, LPSIZE size)
325 {
326     DC *dc = physDev->dc;
327     int             i;
328     float           width = 0.0;
329     
330     TRACE("%s %i\n", debugstr_wn(str, count), count);
331     
332     for (i = 0; i < count && str[i] != '\0'; ++i)
333         width += PSDRV_UVMetrics(str[i], physDev->font.afm)->WX;
334         
335     width *= physDev->font.scale;
336     
337     size->cx = GDI_ROUND((FLOAT)width * dc->xformVport2World.eM11);
338     size->cy = GDI_ROUND((FLOAT)physDev->font.tm.tmHeight *
339             dc->xformVport2World.eM22);
340             
341     TRACE("cx=%li cy=%li\n", size->cx, size->cy);
342             
343     return TRUE;
344 }
345
346 /***********************************************************************
347  *           PSDRV_GetCharWidth
348  */
349 BOOL PSDRV_GetCharWidth(PSDRV_PDEVICE *physDev, UINT firstChar, UINT lastChar, LPINT buffer)
350 {
351     UINT            i;
352     
353     TRACE("U+%.4X U+%.4X\n", firstChar, lastChar);
354     
355     if (lastChar > 0xffff || firstChar > lastChar)
356     {
357         SetLastError(ERROR_INVALID_PARAMETER);
358         return FALSE;
359     }
360         
361     for (i = firstChar; i <= lastChar; ++i)
362     {
363         *buffer = GDI_ROUND(PSDRV_UVMetrics(i, physDev->font.afm)->WX
364                 * physDev->font.scale);
365         TRACE("U+%.4X: %i\n", i, *buffer);
366         ++buffer;
367     }
368         
369     return TRUE;
370 }
371     
372 /***********************************************************************
373  *           PSDRV_SetFont
374  */
375 BOOL PSDRV_SetFont( PSDRV_PDEVICE *physDev )
376 {
377     PSDRV_WriteSetColor(physDev, &physDev->font.color);
378     if(physDev->font.set) return TRUE;
379
380     PSDRV_WriteSetFont(physDev);
381     physDev->font.set = TRUE;
382     return TRUE;
383 }
384
385
386 /***********************************************************************
387  *           PSDRV_GetFontMetric
388  */
389 static UINT PSDRV_GetFontMetric( PSDRV_PDEVICE *physDev, const AFM *afm,
390                                  NEWTEXTMETRICEXW *ntmx, ENUMLOGFONTEXW *elfx)
391 {
392     /* ntmx->ntmTm is NEWTEXTMETRICW; compatible w/ TEXTMETRICW per Win32 doc */
393
394     TEXTMETRICW     *tm = (TEXTMETRICW *)&(ntmx->ntmTm);
395     LOGFONTW        *lf = &(elfx->elfLogFont);
396     PSFONT          font;
397     
398     memset(ntmx, 0, sizeof(*ntmx));
399     memset(elfx, 0, sizeof(*elfx));
400     
401     ScaleFont(afm, -(LONG)(afm->WinMetrics.usUnitsPerEm), &font, tm);
402     
403     lf->lfHeight = tm->tmHeight;
404     lf->lfWidth = tm->tmAveCharWidth;
405     lf->lfWeight = tm->tmWeight;
406     lf->lfItalic = tm->tmItalic;
407     lf->lfCharSet = tm->tmCharSet;
408     
409     lf->lfPitchAndFamily = (afm->IsFixedPitch) ? FIXED_PITCH : VARIABLE_PITCH;
410     
411     MultiByteToWideChar(CP_ACP, 0, afm->FamilyName, -1, lf->lfFaceName,
412             LF_FACESIZE);
413             
414     return DEVICE_FONTTYPE;
415 }
416
417 /***********************************************************************
418  *           PSDRV_EnumDeviceFonts
419  */
420 BOOL PSDRV_EnumDeviceFonts( PSDRV_PDEVICE *physDev, LPLOGFONTW plf,
421                             DEVICEFONTENUMPROC proc, LPARAM lp )
422 {
423     ENUMLOGFONTEXW      lf;
424     NEWTEXTMETRICEXW    tm;
425     BOOL                b, bRet = 0;
426     AFMLISTENTRY        *afmle;
427     FONTFAMILY          *family;
428     char                FaceName[LF_FACESIZE];
429
430     if( plf->lfFaceName[0] ) {
431         WideCharToMultiByte(CP_ACP, 0, plf->lfFaceName, -1,
432                           FaceName, sizeof(FaceName), NULL, NULL);
433         TRACE("lfFaceName = '%s'\n", FaceName);
434         for(family = physDev->pi->Fonts; family; family = family->next) {
435             if(!strncmp(FaceName, family->FamilyName, 
436                         strlen(family->FamilyName)))
437                 break;
438         }
439         if(family) {
440             for(afmle = family->afmlist; afmle; afmle = afmle->next) {
441                 TRACE("Got '%s'\n", afmle->afm->FontName);
442                 if( (b = proc( &lf, &tm, PSDRV_GetFontMetric(physDev, afmle->afm, &tm, &lf), lp )) )
443                      bRet = b;
444                 else break;
445             }
446         }
447     } else {
448
449         TRACE("lfFaceName = NULL\n");
450         for(family = physDev->pi->Fonts; family; family = family->next) {
451             afmle = family->afmlist;
452             TRACE("Got '%s'\n", afmle->afm->FontName);
453             if( (b = proc( &lf, &tm, PSDRV_GetFontMetric(physDev, afmle->afm, &tm, &lf), lp )) )
454                 bRet = b;
455             else break;
456         }
457     }
458     return bRet;
459 }