user32: Destroy the unused buttons in a message box instead of only hiding them.
[wine/testsucceed.git] / dlls / wineps.drv / builtin.c
blobb57cfb2b3d952481b56514e62d906d5f6886eb06
1 /*
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <math.h>
24 #include <assert.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "wingdi.h"
30 #include "winnls.h"
32 #include "psdrv.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
38 /***********************************************************************
39 * is_stock_font
41 static inline BOOL is_stock_font( HFONT font )
43 int i;
44 for (i = OEM_FIXED_FONT; i <= DEFAULT_GUI_FONT; i++)
46 if (i != DEFAULT_PALETTE && font == GetStockObject(i)) return TRUE;
48 return FALSE;
52 /*******************************************************************************
53 * ScaleFont
55 * Scale builtin font to requested lfHeight
58 static inline float Round(float f)
60 return (f > 0) ? (f + 0.5) : (f - 0.5);
63 static VOID ScaleFont(const AFM *afm, LONG lfHeight, PSFONT *font,
64 TEXTMETRICW *tm)
66 const WINMETRICS *wm = &(afm->WinMetrics);
67 USHORT usUnitsPerEm, usWinAscent, usWinDescent;
68 SHORT sAscender, sDescender, sLineGap, sAvgCharWidth;
70 TRACE("'%s' %i\n", afm->FontName, lfHeight);
72 if (lfHeight < 0) /* match em height */
74 font->fontinfo.Builtin.scale = - ((float)lfHeight / (float)(wm->usUnitsPerEm));
76 else /* match cell height */
78 font->fontinfo.Builtin.scale = (float)lfHeight /
79 (float)(wm->usWinAscent + wm->usWinDescent);
82 font->size.xx = (INT)Round(font->fontinfo.Builtin.scale * (float)wm->usUnitsPerEm);
83 font->size.xy = font->size.yx = 0;
84 font->size.yy = -(INT)Round(font->fontinfo.Builtin.scale * (float)wm->usUnitsPerEm);
86 usUnitsPerEm = (USHORT)Round((float)(wm->usUnitsPerEm) * font->fontinfo.Builtin.scale);
87 sAscender = (SHORT)Round((float)(wm->sAscender) * font->fontinfo.Builtin.scale);
88 sDescender = (SHORT)Round((float)(wm->sDescender) * font->fontinfo.Builtin.scale);
89 sLineGap = (SHORT)Round((float)(wm->sLineGap) * font->fontinfo.Builtin.scale);
90 usWinAscent = (USHORT)Round((float)(wm->usWinAscent) * font->fontinfo.Builtin.scale);
91 usWinDescent = (USHORT)Round((float)(wm->usWinDescent) * font->fontinfo.Builtin.scale);
92 sAvgCharWidth = (SHORT)Round((float)(wm->sAvgCharWidth) * font->fontinfo.Builtin.scale);
94 tm->tmAscent = (LONG)usWinAscent;
95 tm->tmDescent = (LONG)usWinDescent;
96 tm->tmHeight = tm->tmAscent + tm->tmDescent;
98 tm->tmInternalLeading = tm->tmHeight - (LONG)usUnitsPerEm;
99 if (tm->tmInternalLeading < 0)
100 tm->tmInternalLeading = 0;
102 tm->tmExternalLeading =
103 (LONG)(sAscender - sDescender + sLineGap) - tm->tmHeight;
104 if (tm->tmExternalLeading < 0)
105 tm->tmExternalLeading = 0;
107 tm->tmAveCharWidth = (LONG)sAvgCharWidth;
109 tm->tmWeight = afm->Weight;
110 tm->tmItalic = (afm->ItalicAngle != 0.0);
111 tm->tmUnderlined = 0;
112 tm->tmStruckOut = 0;
113 tm->tmFirstChar = (WCHAR)(afm->Metrics[0].UV);
114 tm->tmLastChar = (WCHAR)(afm->Metrics[afm->NumofMetrics - 1].UV);
115 tm->tmDefaultChar = 0x001f; /* Win2K does this - FIXME? */
116 tm->tmBreakChar = tm->tmFirstChar; /* should be 'space' */
118 tm->tmPitchAndFamily = TMPF_DEVICE | TMPF_VECTOR;
119 if (!afm->IsFixedPitch)
120 tm->tmPitchAndFamily |= TMPF_FIXED_PITCH; /* yes, it's backwards */
121 if (wm->usUnitsPerEm != 1000)
122 tm->tmPitchAndFamily |= TMPF_TRUETYPE;
124 tm->tmCharSet = ANSI_CHARSET; /* FIXME */
125 tm->tmOverhang = 0;
128 * This is kludgy. font->scale is used in several places in the driver
129 * to adjust PostScript-style metrics. Since these metrics have been
130 * "normalized" to an em-square size of 1000, font->scale needs to be
131 * similarly adjusted..
134 font->fontinfo.Builtin.scale *= (float)wm->usUnitsPerEm / 1000.0;
136 tm->tmMaxCharWidth = (LONG)Round(
137 (afm->FontBBox.urx - afm->FontBBox.llx) * font->fontinfo.Builtin.scale);
139 font->underlinePosition = afm->UnderlinePosition * font->fontinfo.Builtin.scale;
140 font->underlineThickness = afm->UnderlineThickness * font->fontinfo.Builtin.scale;
141 font->strikeoutPosition = tm->tmAscent / 2;
142 font->strikeoutThickness = font->underlineThickness;
144 TRACE("Selected PS font '%s' size %d weight %d.\n", afm->FontName,
145 font->size.xx, tm->tmWeight );
146 TRACE("H = %d As = %d Des = %d IL = %d EL = %d\n", tm->tmHeight,
147 tm->tmAscent, tm->tmDescent, tm->tmInternalLeading,
148 tm->tmExternalLeading);
152 /****************************************************************************
153 * PSDRV_SelectBuiltinFont
155 * Set up physDev->font for a builtin font
158 BOOL PSDRV_SelectBuiltinFont(PSDRV_PDEVICE *physDev, HFONT hfont,
159 LOGFONTW *plf, LPSTR FaceName)
161 AFMLISTENTRY *afmle;
162 FONTFAMILY *family;
163 BOOL bd = FALSE, it = FALSE;
164 LONG height;
166 TRACE("Trying to find facename '%s'\n", FaceName);
168 /* Look for a matching font family */
169 for(family = physDev->pi->Fonts; family; family = family->next) {
170 if(!strcasecmp(FaceName, family->FamilyName))
171 break;
174 if(!family) {
175 /* Fallback for Window's font families to common PostScript families */
176 if(!strcmp(FaceName, "Arial"))
177 strcpy(FaceName, "Helvetica");
178 else if(!strcmp(FaceName, "System"))
179 strcpy(FaceName, "Helvetica");
180 else if(!strcmp(FaceName, "Times New Roman"))
181 strcpy(FaceName, "Times");
182 else if(!strcmp(FaceName, "Courier New"))
183 strcpy(FaceName, "Courier");
185 for(family = physDev->pi->Fonts; family; family = family->next) {
186 if(!strcmp(FaceName, family->FamilyName))
187 break;
190 /* If all else fails, use the first font defined for the printer */
191 if(!family)
192 family = physDev->pi->Fonts;
194 TRACE("Got family '%s'\n", family->FamilyName);
196 if(plf->lfItalic)
197 it = TRUE;
198 if(plf->lfWeight > 550)
199 bd = TRUE;
201 for(afmle = family->afmlist; afmle; afmle = afmle->next) {
202 if( (bd == (afmle->afm->Weight == FW_BOLD)) &&
203 (it == (afmle->afm->ItalicAngle != 0.0)) )
204 break;
206 if(!afmle)
207 afmle = family->afmlist; /* not ideal */
209 TRACE("Got font '%s'\n", afmle->afm->FontName);
211 physDev->font.fontloc = Builtin;
212 physDev->font.fontinfo.Builtin.afm = afmle->afm;
214 height = plf->lfHeight;
215 /* stock fonts ignore the mapping mode */
216 if (!is_stock_font( hfont )) {
217 POINT pts[2];
218 pts[0].x = pts[0].y = pts[1].x = 0;
219 pts[1].y = height;
220 LPtoDP(physDev->hdc, pts, 2);
221 height = pts[1].y - pts[0].y;
223 ScaleFont(physDev->font.fontinfo.Builtin.afm, height,
224 &(physDev->font), &(physDev->font.fontinfo.Builtin.tm));
227 /* Does anyone know if these are supposed to be reversed like this? */
229 physDev->font.fontinfo.Builtin.tm.tmDigitizedAspectX = physDev->logPixelsY;
230 physDev->font.fontinfo.Builtin.tm.tmDigitizedAspectY = physDev->logPixelsX;
232 return TRUE;
235 BOOL PSDRV_WriteSetBuiltinFont(PSDRV_PDEVICE *physDev)
237 return PSDRV_WriteSetFont(physDev,
238 physDev->font.fontinfo.Builtin.afm->FontName,
239 physDev->font.size, physDev->font.escapement);
242 BOOL PSDRV_WriteBuiltinGlyphShow(PSDRV_PDEVICE *physDev, LPCWSTR str, INT count)
244 int i;
245 LPCSTR name;
247 for (i = 0; i < count; ++i)
249 name = PSDRV_UVMetrics(str[i], physDev->font.fontinfo.Builtin.afm)->N->sz;
251 PSDRV_WriteGlyphShow(physDev, name);
254 return TRUE;
257 /***********************************************************************
258 * PSDRV_GetTextMetrics
260 BOOL CDECL PSDRV_GetTextMetrics(PSDRV_PDEVICE *physDev, TEXTMETRICW *metrics)
262 assert(physDev->font.fontloc == Builtin);
264 memcpy(metrics, &(physDev->font.fontinfo.Builtin.tm),
265 sizeof(physDev->font.fontinfo.Builtin.tm));
266 return TRUE;
269 /******************************************************************************
270 * PSDRV_UVMetrics
272 * Find the AFMMETRICS for a given UV. Returns first glyph in the font
273 * (space?) if the font does not have a glyph for the given UV.
275 static int MetricsByUV(const void *a, const void *b)
277 return (int)(((const AFMMETRICS *)a)->UV - ((const AFMMETRICS *)b)->UV);
280 const AFMMETRICS *PSDRV_UVMetrics(LONG UV, const AFM *afm)
282 AFMMETRICS key;
283 const AFMMETRICS *needle;
286 * Ugly work-around for symbol fonts. Wine is sending characters which
287 * belong in the Unicode private use range (U+F020 - U+F0FF) as ASCII
288 * characters (U+0020 - U+00FF).
291 if ((afm->Metrics->UV & 0xff00) == 0xf000 && UV < 0x100)
292 UV |= 0xf000;
294 key.UV = UV;
296 needle = bsearch(&key, afm->Metrics, afm->NumofMetrics, sizeof(AFMMETRICS),
297 MetricsByUV);
299 if (needle == NULL)
301 WARN("No glyph for U+%.4X in %s\n", UV, afm->FontName);
302 needle = afm->Metrics;
305 return needle;
308 /***********************************************************************
309 * PSDRV_GetTextExtentExPoint
311 BOOL CDECL PSDRV_GetTextExtentExPoint(PSDRV_PDEVICE *physDev, LPCWSTR str, INT count,
312 INT maxExt, LPINT lpnFit, LPINT alpDx, LPSIZE size)
314 int nfit = 0;
315 int i;
316 float width = 0.0;
317 float scale;
319 assert(physDev->font.fontloc == Builtin);
321 TRACE("%s %i\n", debugstr_wn(str, count), count);
323 scale = physDev->font.fontinfo.Builtin.scale;
324 for (i = 0; i < count && str[i] != '\0'; ++i)
326 float scaled_width;
327 width += PSDRV_UVMetrics(str[i], physDev->font.fontinfo.Builtin.afm)->WX;
328 scaled_width = width * scale;
329 if (alpDx)
330 alpDx[i] = scaled_width;
331 if (scaled_width <= maxExt)
332 ++nfit;
335 size->cx = width * physDev->font.fontinfo.Builtin.scale;
336 size->cy = physDev->font.fontinfo.Builtin.tm.tmHeight;
338 if (lpnFit)
339 *lpnFit = nfit;
341 TRACE("cx=%i cy=%i\n", size->cx, size->cy);
343 return TRUE;
346 /***********************************************************************
347 * PSDRV_GetCharWidth
349 BOOL CDECL PSDRV_GetCharWidth(PSDRV_PDEVICE *physDev, UINT firstChar, UINT lastChar, LPINT buffer)
351 UINT i;
353 assert(physDev->font.fontloc == Builtin);
355 TRACE("U+%.4X U+%.4X\n", firstChar, lastChar);
357 if (lastChar > 0xffff || firstChar > lastChar)
359 SetLastError(ERROR_INVALID_PARAMETER);
360 return FALSE;
363 for (i = firstChar; i <= lastChar; ++i)
365 *buffer = floor( PSDRV_UVMetrics(i, physDev->font.fontinfo.Builtin.afm)->WX
366 * physDev->font.fontinfo.Builtin.scale + 0.5 );
367 TRACE("U+%.4X: %i\n", i, *buffer);
368 ++buffer;
371 return TRUE;
375 /***********************************************************************
376 * PSDRV_GetFontMetric
378 static UINT PSDRV_GetFontMetric(HDC hdc, const AFM *afm,
379 NEWTEXTMETRICEXW *ntmx, ENUMLOGFONTEXW *elfx)
381 /* ntmx->ntmTm is NEWTEXTMETRICW; compatible w/ TEXTMETRICW per Win32 doc */
383 TEXTMETRICW *tm = (TEXTMETRICW *)&(ntmx->ntmTm);
384 LOGFONTW *lf = &(elfx->elfLogFont);
385 PSFONT font;
387 memset(ntmx, 0, sizeof(*ntmx));
388 memset(elfx, 0, sizeof(*elfx));
390 ScaleFont(afm, -(LONG)(afm->WinMetrics.usUnitsPerEm), &font, tm);
392 lf->lfHeight = tm->tmHeight;
393 lf->lfWidth = tm->tmAveCharWidth;
394 lf->lfWeight = tm->tmWeight;
395 lf->lfItalic = tm->tmItalic;
396 lf->lfCharSet = tm->tmCharSet;
398 lf->lfPitchAndFamily = (afm->IsFixedPitch) ? FIXED_PITCH : VARIABLE_PITCH;
400 MultiByteToWideChar(CP_ACP, 0, afm->FamilyName, -1, lf->lfFaceName,
401 LF_FACESIZE);
403 return DEVICE_FONTTYPE;
406 /***********************************************************************
407 * PSDRV_EnumDeviceFonts
409 BOOL CDECL PSDRV_EnumDeviceFonts( PSDRV_PDEVICE *physDev, LPLOGFONTW plf,
410 FONTENUMPROCW proc, LPARAM lp )
412 ENUMLOGFONTEXW lf;
413 NEWTEXTMETRICEXW tm;
414 BOOL b, bRet = 0;
415 AFMLISTENTRY *afmle;
416 FONTFAMILY *family;
417 char FaceName[LF_FACESIZE];
419 if( plf && plf->lfFaceName[0] ) {
420 WideCharToMultiByte(CP_ACP, 0, plf->lfFaceName, -1,
421 FaceName, sizeof(FaceName), NULL, NULL);
422 TRACE("lfFaceName = '%s'\n", FaceName);
423 for(family = physDev->pi->Fonts; family; family = family->next) {
424 if(!strncmp(FaceName, family->FamilyName,
425 strlen(family->FamilyName)))
426 break;
428 if(family) {
429 for(afmle = family->afmlist; afmle; afmle = afmle->next) {
430 UINT fm;
432 TRACE("Got '%s'\n", afmle->afm->FontName);
433 fm = PSDRV_GetFontMetric( physDev->hdc, afmle->afm, &tm, &lf );
434 if( (b = (*proc)( &lf.elfLogFont, (TEXTMETRICW *)&tm, fm, lp )) )
435 bRet = b;
436 else break;
439 } else {
441 TRACE("lfFaceName = NULL\n");
442 for(family = physDev->pi->Fonts; family; family = family->next) {
443 UINT fm;
445 afmle = family->afmlist;
446 TRACE("Got '%s'\n", afmle->afm->FontName);
447 fm = PSDRV_GetFontMetric( physDev->hdc, afmle->afm, &tm, &lf );
448 if( (b = (*proc)( &lf.elfLogFont, (TEXTMETRICW *)&tm, fm, lp )) )
449 bRet = b;
450 else break;
453 return bRet;