2 * PostScript driver builtin font functions
4 * Copyright 2002 Huw D M Davies for CodeWeavers
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
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
36 /***********************************************************************
39 inline static BOOL is_stock_font( HFONT font )
42 for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
44 if (i != DEFAULT_PALETTE && font == GetStockObject(i)) return TRUE;
50 /*******************************************************************************
53 * Scale builtin font to requested lfHeight
56 inline static float Round(float f)
58 return (f > 0) ? (f + 0.5) : (f - 0.5);
61 static VOID ScaleFont(const AFM *afm, LONG lfHeight, PSFONT *font,
64 const WINMETRICS *wm = &(afm->WinMetrics);
65 USHORT usUnitsPerEm, usWinAscent, usWinDescent;
66 SHORT sAscender, sDescender, sLineGap, sAvgCharWidth;
68 TRACE("'%s' %li\n", afm->FontName, lfHeight);
70 if (lfHeight < 0) /* match em height */
72 font->fontinfo.Builtin.scale = - ((float)lfHeight / (float)(wm->usUnitsPerEm));
74 else /* match cell height */
76 font->fontinfo.Builtin.scale = (float)lfHeight /
77 (float)(wm->usWinAscent + wm->usWinDescent);
80 font->size = (INT)Round(font->fontinfo.Builtin.scale * (float)wm->usUnitsPerEm);
82 usUnitsPerEm = (USHORT)Round((float)(wm->usUnitsPerEm) * font->fontinfo.Builtin.scale);
83 sAscender = (SHORT)Round((float)(wm->sAscender) * font->fontinfo.Builtin.scale);
84 sDescender = (SHORT)Round((float)(wm->sDescender) * font->fontinfo.Builtin.scale);
85 sLineGap = (SHORT)Round((float)(wm->sLineGap) * font->fontinfo.Builtin.scale);
86 usWinAscent = (USHORT)Round((float)(wm->usWinAscent) * font->fontinfo.Builtin.scale);
87 usWinDescent = (USHORT)Round((float)(wm->usWinDescent) * font->fontinfo.Builtin.scale);
88 sAvgCharWidth = (SHORT)Round((float)(wm->sAvgCharWidth) * font->fontinfo.Builtin.scale);
90 tm->tmAscent = (LONG)usWinAscent;
91 tm->tmDescent = (LONG)usWinDescent;
92 tm->tmHeight = tm->tmAscent + tm->tmDescent;
94 tm->tmInternalLeading = tm->tmHeight - (LONG)usUnitsPerEm;
95 if (tm->tmInternalLeading < 0)
96 tm->tmInternalLeading = 0;
98 tm->tmExternalLeading =
99 (LONG)(sAscender - sDescender + sLineGap) - tm->tmHeight;
100 if (tm->tmExternalLeading < 0)
101 tm->tmExternalLeading = 0;
103 tm->tmAveCharWidth = (LONG)sAvgCharWidth;
105 tm->tmWeight = afm->Weight;
106 tm->tmItalic = (afm->ItalicAngle != 0.0);
107 tm->tmUnderlined = 0;
109 tm->tmFirstChar = (WCHAR)(afm->Metrics[0].UV);
110 tm->tmLastChar = (WCHAR)(afm->Metrics[afm->NumofMetrics - 1].UV);
111 tm->tmDefaultChar = 0x001f; /* Win2K does this - FIXME? */
112 tm->tmBreakChar = tm->tmFirstChar; /* should be 'space' */
114 tm->tmPitchAndFamily = TMPF_DEVICE | TMPF_VECTOR;
115 if (!afm->IsFixedPitch)
116 tm->tmPitchAndFamily |= TMPF_FIXED_PITCH; /* yes, it's backwards */
117 if (wm->usUnitsPerEm != 1000)
118 tm->tmPitchAndFamily |= TMPF_TRUETYPE;
120 tm->tmCharSet = ANSI_CHARSET; /* FIXME */
124 * This is kludgy. font->scale is used in several places in the driver
125 * to adjust PostScript-style metrics. Since these metrics have been
126 * "normalized" to an em-square size of 1000, font->scale needs to be
127 * similarly adjusted..
130 font->fontinfo.Builtin.scale *= (float)wm->usUnitsPerEm / 1000.0;
132 tm->tmMaxCharWidth = (LONG)Round(
133 (afm->FontBBox.urx - afm->FontBBox.llx) * font->fontinfo.Builtin.scale);
135 font->underlinePosition = afm->UnderlinePosition * font->fontinfo.Builtin.scale;
136 font->underlineThickness = afm->UnderlineThickness * font->fontinfo.Builtin.scale;
137 font->strikeoutPosition = tm->tmAscent / 2;
138 font->strikeoutThickness = font->underlineThickness;
140 TRACE("Selected PS font '%s' size %d weight %ld.\n", afm->FontName,
141 font->size, tm->tmWeight );
142 TRACE("H = %ld As = %ld Des = %ld IL = %ld EL = %ld\n", tm->tmHeight,
143 tm->tmAscent, tm->tmDescent, tm->tmInternalLeading,
144 tm->tmExternalLeading);
148 /****************************************************************************
149 * PSDRV_SelectBuiltinFont
151 * Set up physDev->font for a builtin font
154 BOOL PSDRV_SelectBuiltinFont(PSDRV_PDEVICE *physDev, HFONT hfont,
155 LOGFONTW *plf, LPSTR FaceName)
159 BOOL bd = FALSE, it = FALSE;
162 TRACE("Trying to find facename '%s'\n", FaceName);
164 /* Look for a matching font family */
165 for(family = physDev->pi->Fonts; family; family = family->next) {
166 if(!strcasecmp(FaceName, family->FamilyName))
171 /* Fallback for Window's font families to common PostScript families */
172 if(!strcmp(FaceName, "Arial"))
173 strcpy(FaceName, "Helvetica");
174 else if(!strcmp(FaceName, "System"))
175 strcpy(FaceName, "Helvetica");
176 else if(!strcmp(FaceName, "Times New Roman"))
177 strcpy(FaceName, "Times");
178 else if(!strcmp(FaceName, "Courier New"))
179 strcpy(FaceName, "Courier");
181 for(family = physDev->pi->Fonts; family; family = family->next) {
182 if(!strcmp(FaceName, family->FamilyName))
186 /* If all else fails, use the first font defined for the printer */
188 family = physDev->pi->Fonts;
190 TRACE("Got family '%s'\n", family->FamilyName);
194 if(plf->lfWeight > 550)
197 for(afmle = family->afmlist; afmle; afmle = afmle->next) {
198 if( (bd == (afmle->afm->Weight == FW_BOLD)) &&
199 (it == (afmle->afm->ItalicAngle != 0.0)) )
203 afmle = family->afmlist; /* not ideal */
205 TRACE("Got font '%s'\n", afmle->afm->FontName);
207 physDev->font.fontloc = Builtin;
208 physDev->font.fontinfo.Builtin.afm = afmle->afm;
210 height = plf->lfHeight;
211 /* stock fonts ignore the mapping mode */
212 if (!is_stock_font( hfont )) {
214 pts[0].x = pts[0].y = pts[1].x = 0;
216 LPtoDP(physDev->hdc, pts, 2);
217 height = pts[1].y - pts[0].y;
219 ScaleFont(physDev->font.fontinfo.Builtin.afm, height,
220 &(physDev->font), &(physDev->font.fontinfo.Builtin.tm));
223 /* Does anyone know if these are supposed to be reversed like this? */
225 physDev->font.fontinfo.Builtin.tm.tmDigitizedAspectX = physDev->logPixelsY;
226 physDev->font.fontinfo.Builtin.tm.tmDigitizedAspectY = physDev->logPixelsX;
231 BOOL PSDRV_WriteSetBuiltinFont(PSDRV_PDEVICE *physDev)
233 return PSDRV_WriteSetFont(physDev,
234 physDev->font.fontinfo.Builtin.afm->FontName,
235 physDev->font.size, physDev->font.escapement);
238 BOOL PSDRV_WriteBuiltinGlyphShow(PSDRV_PDEVICE *physDev, LPCWSTR str, INT count)
243 for (i = 0; i < count; ++i)
245 name = PSDRV_UVMetrics(str[i], physDev->font.fontinfo.Builtin.afm)->N->sz;
247 PSDRV_WriteGlyphShow(physDev, name);
253 /***********************************************************************
254 * PSDRV_GetTextMetrics
256 BOOL PSDRV_GetTextMetrics(PSDRV_PDEVICE *physDev, TEXTMETRICW *metrics)
258 assert(physDev->dc->gdiFont == 0);
259 assert(physDev->font.fontloc == Builtin);
261 memcpy(metrics, &(physDev->font.fontinfo.Builtin.tm),
262 sizeof(physDev->font.fontinfo.Builtin.tm));
266 /******************************************************************************
269 * Find the AFMMETRICS for a given UV. Returns first glyph in the font
270 * (space?) if the font does not have a glyph for the given UV.
272 static int MetricsByUV(const void *a, const void *b)
274 return (int)(((const AFMMETRICS *)a)->UV - ((const AFMMETRICS *)b)->UV);
277 const AFMMETRICS *PSDRV_UVMetrics(LONG UV, const AFM *afm)
280 const AFMMETRICS *needle;
283 * Ugly work-around for symbol fonts. Wine is sending characters which
284 * belong in the Unicode private use range (U+F020 - U+F0FF) as ASCII
285 * characters (U+0020 - U+00FF).
288 if ((afm->Metrics->UV & 0xff00) == 0xf000 && UV < 0x100)
293 needle = bsearch(&key, afm->Metrics, afm->NumofMetrics, sizeof(AFMMETRICS),
298 WARN("No glyph for U+%.4lX in %s\n", UV, afm->FontName);
299 needle = afm->Metrics;
305 /***********************************************************************
306 * PSDRV_GetTextExtentPoint
308 BOOL PSDRV_GetTextExtentPoint(PSDRV_PDEVICE *physDev, LPCWSTR str, INT count, LPSIZE size)
313 assert(physDev->dc->gdiFont == 0);
314 assert(physDev->font.fontloc == Builtin);
316 TRACE("%s %i\n", debugstr_wn(str, count), count);
318 for (i = 0; i < count && str[i] != '\0'; ++i)
319 width += PSDRV_UVMetrics(str[i], physDev->font.fontinfo.Builtin.afm)->WX;
321 width *= physDev->font.fontinfo.Builtin.scale;
323 size->cx = GDI_ROUND((FLOAT)width * physDev->dc->xformVport2World.eM11);
324 size->cy = GDI_ROUND((FLOAT)physDev->font.fontinfo.Builtin.tm.tmHeight *
325 physDev->dc->xformVport2World.eM22);
327 TRACE("cx=%li cy=%li\n", size->cx, size->cy);
332 /***********************************************************************
335 BOOL PSDRV_GetCharWidth(PSDRV_PDEVICE *physDev, UINT firstChar, UINT lastChar, LPINT buffer)
339 assert(physDev->dc->gdiFont == 0);
340 assert(physDev->font.fontloc == Builtin);
342 TRACE("U+%.4X U+%.4X\n", firstChar, lastChar);
344 if (lastChar > 0xffff || firstChar > lastChar)
346 SetLastError(ERROR_INVALID_PARAMETER);
350 for (i = firstChar; i <= lastChar; ++i)
352 *buffer = GDI_ROUND(PSDRV_UVMetrics(i, physDev->font.fontinfo.Builtin.afm)->WX
353 * physDev->font.fontinfo.Builtin.scale);
354 TRACE("U+%.4X: %i\n", i, *buffer);
362 /***********************************************************************
363 * PSDRV_GetFontMetric
365 static UINT PSDRV_GetFontMetric(HDC hdc, const AFM *afm,
366 NEWTEXTMETRICEXW *ntmx, ENUMLOGFONTEXW *elfx)
368 /* ntmx->ntmTm is NEWTEXTMETRICW; compatible w/ TEXTMETRICW per Win32 doc */
370 TEXTMETRICW *tm = (TEXTMETRICW *)&(ntmx->ntmTm);
371 LOGFONTW *lf = &(elfx->elfLogFont);
374 memset(ntmx, 0, sizeof(*ntmx));
375 memset(elfx, 0, sizeof(*elfx));
377 ScaleFont(afm, -(LONG)(afm->WinMetrics.usUnitsPerEm), &font, tm);
379 lf->lfHeight = tm->tmHeight;
380 lf->lfWidth = tm->tmAveCharWidth;
381 lf->lfWeight = tm->tmWeight;
382 lf->lfItalic = tm->tmItalic;
383 lf->lfCharSet = tm->tmCharSet;
385 lf->lfPitchAndFamily = (afm->IsFixedPitch) ? FIXED_PITCH : VARIABLE_PITCH;
387 MultiByteToWideChar(CP_ACP, 0, afm->FamilyName, -1, lf->lfFaceName,
390 return DEVICE_FONTTYPE;
393 /***********************************************************************
394 * PSDRV_EnumDeviceFonts
396 BOOL PSDRV_EnumDeviceFonts( PSDRV_PDEVICE *physDev, LPLOGFONTW plf,
397 DEVICEFONTENUMPROC proc, LPARAM lp )
404 char FaceName[LF_FACESIZE];
406 if( plf->lfFaceName[0] ) {
407 WideCharToMultiByte(CP_ACP, 0, plf->lfFaceName, -1,
408 FaceName, sizeof(FaceName), NULL, NULL);
409 TRACE("lfFaceName = '%s'\n", FaceName);
410 for(family = physDev->pi->Fonts; family; family = family->next) {
411 if(!strncmp(FaceName, family->FamilyName,
412 strlen(family->FamilyName)))
416 for(afmle = family->afmlist; afmle; afmle = afmle->next) {
417 TRACE("Got '%s'\n", afmle->afm->FontName);
418 if( (b = (*proc)( &lf, &tm,
419 PSDRV_GetFontMetric( physDev->hdc, afmle->afm, &tm, &lf ),
427 TRACE("lfFaceName = NULL\n");
428 for(family = physDev->pi->Fonts; family; family = family->next) {
429 afmle = family->afmlist;
430 TRACE("Got '%s'\n", afmle->afm->FontName);
431 if( (b = (*proc)( &lf, &tm,
432 PSDRV_GetFontMetric( physDev->hdc, afmle->afm, &tm, &lf ),