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
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(psdrv
);
38 /***********************************************************************
41 inline static BOOL
is_stock_font( HFONT font
)
44 for (i
= OEM_FIXED_FONT
; i
<= DEFAULT_GUI_FONT
; i
++)
46 if (i
!= DEFAULT_PALETTE
&& font
== GetStockObject(i
)) return TRUE
;
52 /*******************************************************************************
55 * Scale builtin font to requested lfHeight
58 inline static 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
,
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
= (INT
)Round(font
->fontinfo
.Builtin
.scale
* (float)wm
->usUnitsPerEm
);
84 usUnitsPerEm
= (USHORT
)Round((float)(wm
->usUnitsPerEm
) * font
->fontinfo
.Builtin
.scale
);
85 sAscender
= (SHORT
)Round((float)(wm
->sAscender
) * font
->fontinfo
.Builtin
.scale
);
86 sDescender
= (SHORT
)Round((float)(wm
->sDescender
) * font
->fontinfo
.Builtin
.scale
);
87 sLineGap
= (SHORT
)Round((float)(wm
->sLineGap
) * font
->fontinfo
.Builtin
.scale
);
88 usWinAscent
= (USHORT
)Round((float)(wm
->usWinAscent
) * font
->fontinfo
.Builtin
.scale
);
89 usWinDescent
= (USHORT
)Round((float)(wm
->usWinDescent
) * font
->fontinfo
.Builtin
.scale
);
90 sAvgCharWidth
= (SHORT
)Round((float)(wm
->sAvgCharWidth
) * font
->fontinfo
.Builtin
.scale
);
92 tm
->tmAscent
= (LONG
)usWinAscent
;
93 tm
->tmDescent
= (LONG
)usWinDescent
;
94 tm
->tmHeight
= tm
->tmAscent
+ tm
->tmDescent
;
96 tm
->tmInternalLeading
= tm
->tmHeight
- (LONG
)usUnitsPerEm
;
97 if (tm
->tmInternalLeading
< 0)
98 tm
->tmInternalLeading
= 0;
100 tm
->tmExternalLeading
=
101 (LONG
)(sAscender
- sDescender
+ sLineGap
) - tm
->tmHeight
;
102 if (tm
->tmExternalLeading
< 0)
103 tm
->tmExternalLeading
= 0;
105 tm
->tmAveCharWidth
= (LONG
)sAvgCharWidth
;
107 tm
->tmWeight
= afm
->Weight
;
108 tm
->tmItalic
= (afm
->ItalicAngle
!= 0.0);
109 tm
->tmUnderlined
= 0;
111 tm
->tmFirstChar
= (WCHAR
)(afm
->Metrics
[0].UV
);
112 tm
->tmLastChar
= (WCHAR
)(afm
->Metrics
[afm
->NumofMetrics
- 1].UV
);
113 tm
->tmDefaultChar
= 0x001f; /* Win2K does this - FIXME? */
114 tm
->tmBreakChar
= tm
->tmFirstChar
; /* should be 'space' */
116 tm
->tmPitchAndFamily
= TMPF_DEVICE
| TMPF_VECTOR
;
117 if (!afm
->IsFixedPitch
)
118 tm
->tmPitchAndFamily
|= TMPF_FIXED_PITCH
; /* yes, it's backwards */
119 if (wm
->usUnitsPerEm
!= 1000)
120 tm
->tmPitchAndFamily
|= TMPF_TRUETYPE
;
122 tm
->tmCharSet
= ANSI_CHARSET
; /* FIXME */
126 * This is kludgy. font->scale is used in several places in the driver
127 * to adjust PostScript-style metrics. Since these metrics have been
128 * "normalized" to an em-square size of 1000, font->scale needs to be
129 * similarly adjusted..
132 font
->fontinfo
.Builtin
.scale
*= (float)wm
->usUnitsPerEm
/ 1000.0;
134 tm
->tmMaxCharWidth
= (LONG
)Round(
135 (afm
->FontBBox
.urx
- afm
->FontBBox
.llx
) * font
->fontinfo
.Builtin
.scale
);
137 font
->underlinePosition
= afm
->UnderlinePosition
* font
->fontinfo
.Builtin
.scale
;
138 font
->underlineThickness
= afm
->UnderlineThickness
* font
->fontinfo
.Builtin
.scale
;
139 font
->strikeoutPosition
= tm
->tmAscent
/ 2;
140 font
->strikeoutThickness
= font
->underlineThickness
;
142 TRACE("Selected PS font '%s' size %d weight %d.\n", afm
->FontName
,
143 font
->size
, tm
->tmWeight
);
144 TRACE("H = %d As = %d Des = %d IL = %d EL = %d\n", tm
->tmHeight
,
145 tm
->tmAscent
, tm
->tmDescent
, tm
->tmInternalLeading
,
146 tm
->tmExternalLeading
);
150 /****************************************************************************
151 * PSDRV_SelectBuiltinFont
153 * Set up physDev->font for a builtin font
156 BOOL
PSDRV_SelectBuiltinFont(PSDRV_PDEVICE
*physDev
, HFONT hfont
,
157 LOGFONTW
*plf
, LPSTR FaceName
)
161 BOOL bd
= FALSE
, it
= FALSE
;
164 TRACE("Trying to find facename '%s'\n", FaceName
);
166 /* Look for a matching font family */
167 for(family
= physDev
->pi
->Fonts
; family
; family
= family
->next
) {
168 if(!strcasecmp(FaceName
, family
->FamilyName
))
173 /* Fallback for Window's font families to common PostScript families */
174 if(!strcmp(FaceName
, "Arial"))
175 strcpy(FaceName
, "Helvetica");
176 else if(!strcmp(FaceName
, "System"))
177 strcpy(FaceName
, "Helvetica");
178 else if(!strcmp(FaceName
, "Times New Roman"))
179 strcpy(FaceName
, "Times");
180 else if(!strcmp(FaceName
, "Courier New"))
181 strcpy(FaceName
, "Courier");
183 for(family
= physDev
->pi
->Fonts
; family
; family
= family
->next
) {
184 if(!strcmp(FaceName
, family
->FamilyName
))
188 /* If all else fails, use the first font defined for the printer */
190 family
= physDev
->pi
->Fonts
;
192 TRACE("Got family '%s'\n", family
->FamilyName
);
196 if(plf
->lfWeight
> 550)
199 for(afmle
= family
->afmlist
; afmle
; afmle
= afmle
->next
) {
200 if( (bd
== (afmle
->afm
->Weight
== FW_BOLD
)) &&
201 (it
== (afmle
->afm
->ItalicAngle
!= 0.0)) )
205 afmle
= family
->afmlist
; /* not ideal */
207 TRACE("Got font '%s'\n", afmle
->afm
->FontName
);
209 physDev
->font
.fontloc
= Builtin
;
210 physDev
->font
.fontinfo
.Builtin
.afm
= afmle
->afm
;
212 height
= plf
->lfHeight
;
213 /* stock fonts ignore the mapping mode */
214 if (!is_stock_font( hfont
)) {
216 pts
[0].x
= pts
[0].y
= pts
[1].x
= 0;
218 LPtoDP(physDev
->hdc
, pts
, 2);
219 height
= pts
[1].y
- pts
[0].y
;
221 ScaleFont(physDev
->font
.fontinfo
.Builtin
.afm
, height
,
222 &(physDev
->font
), &(physDev
->font
.fontinfo
.Builtin
.tm
));
225 /* Does anyone know if these are supposed to be reversed like this? */
227 physDev
->font
.fontinfo
.Builtin
.tm
.tmDigitizedAspectX
= physDev
->logPixelsY
;
228 physDev
->font
.fontinfo
.Builtin
.tm
.tmDigitizedAspectY
= physDev
->logPixelsX
;
233 BOOL
PSDRV_WriteSetBuiltinFont(PSDRV_PDEVICE
*physDev
)
235 return PSDRV_WriteSetFont(physDev
,
236 physDev
->font
.fontinfo
.Builtin
.afm
->FontName
,
237 physDev
->font
.size
, physDev
->font
.escapement
);
240 BOOL
PSDRV_WriteBuiltinGlyphShow(PSDRV_PDEVICE
*physDev
, LPCWSTR str
, INT count
)
245 for (i
= 0; i
< count
; ++i
)
247 name
= PSDRV_UVMetrics(str
[i
], physDev
->font
.fontinfo
.Builtin
.afm
)->N
->sz
;
249 PSDRV_WriteGlyphShow(physDev
, name
);
255 /***********************************************************************
256 * PSDRV_GetTextMetrics
258 BOOL
PSDRV_GetTextMetrics(PSDRV_PDEVICE
*physDev
, TEXTMETRICW
*metrics
)
260 assert(physDev
->font
.fontloc
== Builtin
);
262 memcpy(metrics
, &(physDev
->font
.fontinfo
.Builtin
.tm
),
263 sizeof(physDev
->font
.fontinfo
.Builtin
.tm
));
267 /******************************************************************************
270 * Find the AFMMETRICS for a given UV. Returns first glyph in the font
271 * (space?) if the font does not have a glyph for the given UV.
273 static int MetricsByUV(const void *a
, const void *b
)
275 return (int)(((const AFMMETRICS
*)a
)->UV
- ((const AFMMETRICS
*)b
)->UV
);
278 const AFMMETRICS
*PSDRV_UVMetrics(LONG UV
, const AFM
*afm
)
281 const AFMMETRICS
*needle
;
284 * Ugly work-around for symbol fonts. Wine is sending characters which
285 * belong in the Unicode private use range (U+F020 - U+F0FF) as ASCII
286 * characters (U+0020 - U+00FF).
289 if ((afm
->Metrics
->UV
& 0xff00) == 0xf000 && UV
< 0x100)
294 needle
= bsearch(&key
, afm
->Metrics
, afm
->NumofMetrics
, sizeof(AFMMETRICS
),
299 WARN("No glyph for U+%.4X in %s\n", UV
, afm
->FontName
);
300 needle
= afm
->Metrics
;
306 /***********************************************************************
307 * PSDRV_GetTextExtentExPoint
309 BOOL
PSDRV_GetTextExtentExPoint(PSDRV_PDEVICE
*physDev
, LPCWSTR str
, INT count
,
310 INT maxExt
, LPINT lpnFit
, LPINT alpDx
, LPSIZE size
)
317 assert(physDev
->font
.fontloc
== Builtin
);
319 TRACE("%s %i\n", debugstr_wn(str
, count
), count
);
321 scale
= physDev
->font
.fontinfo
.Builtin
.scale
;
322 for (i
= 0; i
< count
&& str
[i
] != '\0'; ++i
)
325 width
+= PSDRV_UVMetrics(str
[i
], physDev
->font
.fontinfo
.Builtin
.afm
)->WX
;
326 scaled_width
= width
* scale
;
328 alpDx
[i
] = scaled_width
;
329 if (scaled_width
<= maxExt
)
333 size
->cx
= width
* physDev
->font
.fontinfo
.Builtin
.scale
;
334 size
->cy
= physDev
->font
.fontinfo
.Builtin
.tm
.tmHeight
;
339 TRACE("cx=%i cy=%i\n", size
->cx
, size
->cy
);
344 /***********************************************************************
347 BOOL
PSDRV_GetCharWidth(PSDRV_PDEVICE
*physDev
, UINT firstChar
, UINT lastChar
, LPINT buffer
)
351 assert(physDev
->font
.fontloc
== Builtin
);
353 TRACE("U+%.4X U+%.4X\n", firstChar
, lastChar
);
355 if (lastChar
> 0xffff || firstChar
> lastChar
)
357 SetLastError(ERROR_INVALID_PARAMETER
);
361 for (i
= firstChar
; i
<= lastChar
; ++i
)
363 *buffer
= floor( PSDRV_UVMetrics(i
, physDev
->font
.fontinfo
.Builtin
.afm
)->WX
364 * physDev
->font
.fontinfo
.Builtin
.scale
+ 0.5 );
365 TRACE("U+%.4X: %i\n", i
, *buffer
);
373 /***********************************************************************
374 * PSDRV_GetFontMetric
376 static UINT
PSDRV_GetFontMetric(HDC hdc
, const AFM
*afm
,
377 NEWTEXTMETRICEXW
*ntmx
, ENUMLOGFONTEXW
*elfx
)
379 /* ntmx->ntmTm is NEWTEXTMETRICW; compatible w/ TEXTMETRICW per Win32 doc */
381 TEXTMETRICW
*tm
= (TEXTMETRICW
*)&(ntmx
->ntmTm
);
382 LOGFONTW
*lf
= &(elfx
->elfLogFont
);
385 memset(ntmx
, 0, sizeof(*ntmx
));
386 memset(elfx
, 0, sizeof(*elfx
));
388 ScaleFont(afm
, -(LONG
)(afm
->WinMetrics
.usUnitsPerEm
), &font
, tm
);
390 lf
->lfHeight
= tm
->tmHeight
;
391 lf
->lfWidth
= tm
->tmAveCharWidth
;
392 lf
->lfWeight
= tm
->tmWeight
;
393 lf
->lfItalic
= tm
->tmItalic
;
394 lf
->lfCharSet
= tm
->tmCharSet
;
396 lf
->lfPitchAndFamily
= (afm
->IsFixedPitch
) ? FIXED_PITCH
: VARIABLE_PITCH
;
398 MultiByteToWideChar(CP_ACP
, 0, afm
->FamilyName
, -1, lf
->lfFaceName
,
401 return DEVICE_FONTTYPE
;
404 /***********************************************************************
405 * PSDRV_EnumDeviceFonts
407 BOOL
PSDRV_EnumDeviceFonts( PSDRV_PDEVICE
*physDev
, LPLOGFONTW plf
,
408 FONTENUMPROCW proc
, LPARAM lp
)
415 char FaceName
[LF_FACESIZE
];
417 if( plf
->lfFaceName
[0] ) {
418 WideCharToMultiByte(CP_ACP
, 0, plf
->lfFaceName
, -1,
419 FaceName
, sizeof(FaceName
), NULL
, NULL
);
420 TRACE("lfFaceName = '%s'\n", FaceName
);
421 for(family
= physDev
->pi
->Fonts
; family
; family
= family
->next
) {
422 if(!strncmp(FaceName
, family
->FamilyName
,
423 strlen(family
->FamilyName
)))
427 for(afmle
= family
->afmlist
; afmle
; afmle
= afmle
->next
) {
428 TRACE("Got '%s'\n", afmle
->afm
->FontName
);
429 if( (b
= (*proc
)( &lf
.elfLogFont
, (TEXTMETRICW
*)&tm
,
430 PSDRV_GetFontMetric( physDev
->hdc
, afmle
->afm
, &tm
, &lf
),
438 TRACE("lfFaceName = NULL\n");
439 for(family
= physDev
->pi
->Fonts
; family
; family
= family
->next
) {
440 afmle
= family
->afmlist
;
441 TRACE("Got '%s'\n", afmle
->afm
->FontName
);
442 if( (b
= (*proc
)( &lf
.elfLogFont
, (TEXTMETRICW
*)&tm
,
443 PSDRV_GetFontMetric( physDev
->hdc
, afmle
->afm
, &tm
, &lf
),