2 * PostScript driver font functions
4 * Copyright 1998 Huw D M Davies
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.
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.
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
21 #include <stdlib.h> /* for bsearch() */
24 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
29 /***********************************************************************
32 inline static BOOL is_stock_font( HFONT font )
35 for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
37 if (i != DEFAULT_PALETTE && font == GetStockObject(i)) return TRUE;
43 /*******************************************************************************
46 * Scale font to requested lfHeight
49 inline static float round(float f)
51 return (f > 0) ? (f + 0.5) : (f - 0.5);
54 static VOID ScaleFont(const AFM *afm, LONG lfHeight, PSFONT *font,
57 const WINMETRICS *wm = &(afm->WinMetrics);
58 USHORT usUnitsPerEm, usWinAscent, usWinDescent;
59 SHORT sAscender, sDescender, sLineGap, sTypoAscender;
60 SHORT sTypoDescender, sTypoLineGap, sAvgCharWidth;
62 TRACE("'%s' %li\n", afm->FontName, lfHeight);
64 if (lfHeight < 0) /* match em height */
66 font->scale = - ((float)lfHeight / (float)(wm->usUnitsPerEm));
68 else /* match cell height */
70 font->scale = (float)lfHeight /
71 (float)(wm->usWinAscent + wm->usWinDescent);
74 font->size = (INT)round(font->scale * (float)wm->usUnitsPerEm);
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);
88 tm->tmAscent = (LONG)usWinAscent;
89 tm->tmDescent = (LONG)usWinDescent;
90 tm->tmHeight = tm->tmAscent + tm->tmDescent;
92 tm->tmInternalLeading = tm->tmHeight - (LONG)usUnitsPerEm;
93 if (tm->tmInternalLeading < 0)
94 tm->tmInternalLeading = 0;
96 tm->tmExternalLeading =
97 (LONG)(sAscender - sDescender + sLineGap) - tm->tmHeight;
98 if (tm->tmExternalLeading < 0)
99 tm->tmExternalLeading = 0;
101 tm->tmAveCharWidth = (LONG)sAvgCharWidth;
103 tm->tmWeight = afm->Weight;
104 tm->tmItalic = (afm->ItalicAngle != 0.0);
105 tm->tmUnderlined = 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' */
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;
118 tm->tmCharSet = ANSI_CHARSET; /* FIXME */
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..
128 font->scale *= (float)wm->usUnitsPerEm / 1000.0;
130 tm->tmMaxCharWidth = (LONG)round(
131 (afm->FontBBox.urx - afm->FontBBox.llx) * font->scale);
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);
140 /***********************************************************************
141 * PSDRV_FONT_SelectObject
143 HFONT PSDRV_FONT_SelectObject( DC * dc, HFONT hfont )
146 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
147 BOOL bd = FALSE, it = FALSE;
150 char FaceName[LF_FACESIZE];
152 if (!GetObjectW( hfont, sizeof(lf), &lf )) return 0;
154 TRACE("FaceName = %s Height = %ld Italic = %d Weight = %ld\n",
155 debugstr_w(lf.lfFaceName), lf.lfHeight, lf.lfItalic,
160 if(lf.lfWeight > 550)
162 WideCharToMultiByte(CP_ACP, 0, lf.lfFaceName, -1,
163 FaceName, sizeof(FaceName), NULL, NULL);
165 if(FaceName[0] == '\0') {
166 switch(lf.lfPitchAndFamily & 0xf0) {
171 strcpy(FaceName, "Times");
174 strcpy(FaceName, "Helvetica");
177 strcpy(FaceName, "Courier");
180 strcpy(FaceName, "Symbol");
185 if(FaceName[0] == '\0') {
186 switch(lf.lfPitchAndFamily & 0x0f) {
188 strcpy(FaceName, "Times");
191 strcpy(FaceName, "Courier");
196 if (physDev->pi->FontSubTableSize != 0)
200 for (i = 0; i < physDev->pi->FontSubTableSize; ++i)
202 if (!strcasecmp (FaceName,
203 physDev->pi->FontSubTable[i].pValueName))
205 TRACE ("substituting facename '%s' for '%s'\n",
206 (LPSTR) physDev->pi->FontSubTable[i].pData, FaceName);
207 if (strlen ((LPSTR) physDev->pi->FontSubTable[i].pData) <
210 (LPSTR) physDev->pi->FontSubTable[i].pData);
212 WARN ("Facename '%s' is too long; ignoring substitution\n",
213 (LPSTR) physDev->pi->FontSubTable[i].pData);
219 TRACE("Trying to find facename '%s'\n", FaceName);
221 /* Look for a matching font family */
222 for(family = physDev->pi->Fonts; family; family = family->next) {
223 if(!strcasecmp(FaceName, family->FamilyName))
227 /* Fallback for Window's font families to common PostScript families */
228 if(!strcmp(FaceName, "Arial"))
229 strcpy(FaceName, "Helvetica");
230 else if(!strcmp(FaceName, "System"))
231 strcpy(FaceName, "Helvetica");
232 else if(!strcmp(FaceName, "Times New Roman"))
233 strcpy(FaceName, "Times");
234 else if(!strcmp(FaceName, "Courier New"))
235 strcpy(FaceName, "Courier");
237 for(family = physDev->pi->Fonts; family; family = family->next) {
238 if(!strcmp(FaceName, family->FamilyName))
242 /* If all else fails, use the first font defined for the printer */
244 family = physDev->pi->Fonts;
246 TRACE("Got family '%s'\n", family->FamilyName);
248 for(afmle = family->afmlist; afmle; afmle = afmle->next) {
249 if( (bd == (afmle->afm->Weight == FW_BOLD)) &&
250 (it == (afmle->afm->ItalicAngle != 0.0)) )
254 afmle = family->afmlist; /* not ideal */
256 TRACE("Got font '%s'\n", afmle->afm->FontName);
258 physDev->font.afm = afmle->afm;
259 /* stock fonts ignore the mapping mode */
260 if (!is_stock_font( hfont )) lf.lfHeight = INTERNAL_YWSTODS(dc, lf.lfHeight);
261 ScaleFont(physDev->font.afm, lf.lfHeight,
262 &(physDev->font), &(physDev->font.tm));
264 physDev->font.escapement = lf.lfEscapement;
266 /* Does anyone know if these are supposed to be reversed like this? */
268 physDev->font.tm.tmDigitizedAspectX = physDev->logPixelsY;
269 physDev->font.tm.tmDigitizedAspectY = physDev->logPixelsX;
271 return TRUE; /* We'll use a device font for now */
274 /***********************************************************************
275 * PSDRV_GetTextMetrics
277 BOOL PSDRV_GetTextMetrics(DC *dc, TEXTMETRICW *metrics)
279 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
281 memcpy(metrics, &(physDev->font.tm), sizeof(physDev->font.tm));
285 /******************************************************************************
288 * Find the AFMMETRICS for a given UV. Returns first glyph in the font
289 * (space?) if the font does not have a glyph for the given UV.
291 static int MetricsByUV(const void *a, const void *b)
293 return (int)(((const AFMMETRICS *)a)->UV - ((const AFMMETRICS *)b)->UV);
296 const AFMMETRICS *PSDRV_UVMetrics(LONG UV, const AFM *afm)
299 const AFMMETRICS *needle;
302 * Ugly work-around for symbol fonts. Wine is sending characters which
303 * belong in the Unicode private use range (U+F020 - U+F0FF) as ASCII
304 * characters (U+0020 - U+00FF).
307 if ((afm->Metrics->UV & 0xff00) == 0xf000 && UV < 0x100)
312 needle = bsearch(&key, afm->Metrics, afm->NumofMetrics, sizeof(AFMMETRICS),
317 WARN("No glyph for U+%.4lX in %s\n", UV, afm->FontName);
318 needle = afm->Metrics;
324 /***********************************************************************
325 * PSDRV_GetTextExtentPoint
327 BOOL PSDRV_GetTextExtentPoint(DC *dc, LPCWSTR str, INT count, LPSIZE size)
329 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
333 TRACE("%s %i\n", debugstr_wn(str, count), count);
335 for (i = 0; i < count && str[i] != '\0'; ++i)
336 width += PSDRV_UVMetrics(str[i], physDev->font.afm)->WX;
338 width *= physDev->font.scale;
340 size->cx = GDI_ROUND((FLOAT)width * dc->xformVport2World.eM11);
341 size->cy = GDI_ROUND((FLOAT)physDev->font.tm.tmHeight *
342 dc->xformVport2World.eM22);
344 TRACE("cx=%li cy=%li\n", size->cx, size->cy);
349 /***********************************************************************
352 BOOL PSDRV_GetCharWidth(DC *dc, UINT firstChar, UINT lastChar, LPINT buffer)
354 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
357 TRACE("U+%.4X U+%.4X\n", firstChar, lastChar);
359 if (lastChar > 0xffff || firstChar > lastChar)
361 SetLastError(ERROR_INVALID_PARAMETER);
365 for (i = firstChar; i <= lastChar; ++i)
367 *buffer = GDI_ROUND(PSDRV_UVMetrics(i, physDev->font.afm)->WX
368 * physDev->font.scale);
369 TRACE("U+%.4X: %i\n", i, *buffer);
376 /***********************************************************************
379 BOOL PSDRV_SetFont( DC *dc )
381 PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
383 PSDRV_WriteSetColor(dc, &physDev->font.color);
384 if(physDev->font.set) return TRUE;
386 PSDRV_WriteSetFont(dc);
387 physDev->font.set = TRUE;
392 /***********************************************************************
393 * PSDRV_GetFontMetric
395 static UINT PSDRV_GetFontMetric(HDC hdc, const AFM *afm,
396 NEWTEXTMETRICEXW *ntmx, ENUMLOGFONTEXW *elfx)
398 /* ntmx->ntmTm is NEWTEXTMETRICW; compatible w/ TEXTMETRICW per Win32 doc */
400 TEXTMETRICW *tm = (TEXTMETRICW *)&(ntmx->ntmTm);
401 LOGFONTW *lf = &(elfx->elfLogFont);
404 memset(ntmx, 0, sizeof(*ntmx));
405 memset(elfx, 0, sizeof(*elfx));
407 ScaleFont(afm, -(LONG)(afm->WinMetrics.usUnitsPerEm), &font, tm);
409 lf->lfHeight = tm->tmHeight;
410 lf->lfWidth = tm->tmAveCharWidth;
411 lf->lfWeight = tm->tmWeight;
412 lf->lfItalic = tm->tmItalic;
413 lf->lfCharSet = tm->tmCharSet;
415 lf->lfPitchAndFamily = (afm->IsFixedPitch) ? FIXED_PITCH : VARIABLE_PITCH;
417 MultiByteToWideChar(CP_ACP, 0, afm->FamilyName, -1, lf->lfFaceName,
420 return DEVICE_FONTTYPE;
423 /***********************************************************************
424 * PSDRV_EnumDeviceFonts
426 BOOL PSDRV_EnumDeviceFonts( HDC hdc, LPLOGFONTW plf,
427 DEVICEFONTENUMPROC proc, LPARAM lp )
434 PSDRV_PDEVICE *physDev;
435 char FaceName[LF_FACESIZE];
436 DC *dc = DC_GetDCPtr( hdc );
437 if (!dc) return FALSE;
439 physDev = (PSDRV_PDEVICE *)dc->physDev;
440 /* FIXME!! should reevaluate dc->physDev after every callback */
441 GDI_ReleaseObj( hdc );
443 if( plf->lfFaceName[0] ) {
444 WideCharToMultiByte(CP_ACP, 0, plf->lfFaceName, -1,
445 FaceName, sizeof(FaceName), NULL, NULL);
446 TRACE("lfFaceName = '%s'\n", FaceName);
447 for(family = physDev->pi->Fonts; family; family = family->next) {
448 if(!strncmp(FaceName, family->FamilyName,
449 strlen(family->FamilyName)))
453 for(afmle = family->afmlist; afmle; afmle = afmle->next) {
454 TRACE("Got '%s'\n", afmle->afm->FontName);
455 if( (b = (*proc)( &lf, &tm,
456 PSDRV_GetFontMetric( hdc, afmle->afm, &tm, &lf ),
464 TRACE("lfFaceName = NULL\n");
465 for(family = physDev->pi->Fonts; family; family = family->next) {
466 afmle = family->afmlist;
467 TRACE("Got '%s'\n", afmle->afm->FontName);
468 if( (b = (*proc)( &lf, &tm,
469 PSDRV_GetFontMetric( hdc, afmle->afm, &tm, &lf ),