2 * PostScript driver font functions
4 * Copyright 1998 Huw D M Davies
10 #include "debugtools.h"
14 DEFAULT_DEBUG_CHANNEL(psdrv
);
17 /***********************************************************************
18 * PSDRV_FONT_SelectObject
20 HFONT16
PSDRV_FONT_SelectObject( DC
* dc
, HFONT16 hfont
,
23 HFONT16 prevfont
= dc
->hFont
;
24 PSDRV_PDEVICE
*physDev
= (PSDRV_PDEVICE
*)dc
->physDev
;
25 LOGFONT16
*lf
= &(font
->logfont
);
26 BOOL bd
= FALSE
, it
= FALSE
;
30 char FaceName
[LF_FACESIZE
];
33 TRACE("FaceName = '%s' Height = %d Italic = %d Weight = %d\n",
34 lf
->lfFaceName
, lf
->lfHeight
, lf
->lfItalic
, lf
->lfWeight
);
40 if(lf
->lfWeight
> 550)
42 strcpy(FaceName
, lf
->lfFaceName
);
44 if(FaceName
[0] == '\0') {
45 switch(lf
->lfPitchAndFamily
& 0xf0) {
50 strcpy(FaceName
, "Times");
53 strcpy(FaceName
, "Helvetica");
56 strcpy(FaceName
, "Courier");
59 strcpy(FaceName
, "Symbol");
64 if(FaceName
[0] == '\0') {
65 switch(lf
->lfPitchAndFamily
& 0x0f) {
67 strcpy(FaceName
, "Times");
70 strcpy(FaceName
, "Courier");
75 TRACE("Trying to find facename '%s'\n", FaceName
);
77 /* Look for a matching font family */
78 for(family
= physDev
->pi
->Fonts
; family
; family
= family
->next
) {
79 if(!strcasecmp(FaceName
, family
->FamilyName
))
83 /* Fallback for Window's font families to common PostScript families */
84 if(!strcmp(FaceName
, "Arial"))
85 strcpy(FaceName
, "Helvetica");
86 else if(!strcmp(FaceName
, "System"))
87 strcpy(FaceName
, "Helvetica");
88 else if(!strcmp(FaceName
, "Times New Roman"))
89 strcpy(FaceName
, "Times");
90 for(family
= physDev
->pi
->Fonts
; family
; family
= family
->next
) {
91 if(!strcmp(FaceName
, family
->FamilyName
))
95 /* If all else fails, use the first font defined for the printer */
97 family
= physDev
->pi
->Fonts
;
99 TRACE("Got family '%s'\n", family
->FamilyName
);
101 for(afmle
= family
->afmlist
; afmle
; afmle
= afmle
->next
) {
102 if( (bd
== (afmle
->afm
->Weight
== FW_BOLD
)) &&
103 (it
== (afmle
->afm
->ItalicAngle
!= 0.0)) )
107 afmle
= family
->afmlist
; /* not ideal */
111 physDev
->font
.afm
= afm
;
112 physDev
->font
.tm
.tmHeight
= YLSTODS(dc
, lf
->lfHeight
);
113 if(physDev
->font
.tm
.tmHeight
< 0) {
114 physDev
->font
.tm
.tmHeight
*= - (afm
->FullAscender
- afm
->Descender
) /
115 (afm
->Ascender
- afm
->Descender
);
116 TRACE("Fixed -ve height to %ld\n", physDev
->font
.tm
.tmHeight
);
118 physDev
->font
.size
= physDev
->font
.tm
.tmHeight
* 1000.0 /
119 (afm
->FullAscender
- afm
->Descender
);
120 physDev
->font
.scale
= physDev
->font
.size
/ 1000.0;
121 physDev
->font
.escapement
= lf
->lfEscapement
;
122 physDev
->font
.tm
.tmAscent
= afm
->FullAscender
* physDev
->font
.scale
;
123 physDev
->font
.tm
.tmDescent
= -afm
->Descender
* physDev
->font
.scale
;
124 physDev
->font
.tm
.tmInternalLeading
= (afm
->FullAscender
- afm
->Ascender
)
125 * physDev
->font
.scale
;
126 physDev
->font
.tm
.tmExternalLeading
= (1000.0 - afm
->FullAscender
)
127 * physDev
->font
.scale
; /* ?? */
128 physDev
->font
.tm
.tmAveCharWidth
= afm
->CharWidths
[120] * /* x */
130 physDev
->font
.tm
.tmMaxCharWidth
= afm
->CharWidths
[77] * /* M */
132 physDev
->font
.tm
.tmWeight
= afm
->Weight
;
133 physDev
->font
.tm
.tmItalic
= afm
->ItalicAngle
!= 0.0;
134 physDev
->font
.tm
.tmUnderlined
= lf
->lfUnderline
;
135 physDev
->font
.tm
.tmStruckOut
= lf
->lfStrikeOut
;
136 physDev
->font
.tm
.tmFirstChar
= 32;
137 physDev
->font
.tm
.tmLastChar
= 251;
138 physDev
->font
.tm
.tmDefaultChar
= 128;
139 physDev
->font
.tm
.tmBreakChar
= 32;
140 physDev
->font
.tm
.tmPitchAndFamily
= afm
->IsFixedPitch
? 0 :
142 physDev
->font
.tm
.tmPitchAndFamily
|= TMPF_DEVICE
;
143 physDev
->font
.tm
.tmCharSet
= ANSI_CHARSET
;
144 physDev
->font
.tm
.tmOverhang
= 0;
145 physDev
->font
.tm
.tmDigitizedAspectX
= dc
->devCaps
->logPixelsY
;
146 physDev
->font
.tm
.tmDigitizedAspectY
= dc
->devCaps
->logPixelsX
;
148 physDev
->font
.set
= FALSE
;
150 TRACE("Selected PS font '%s' size %d weight %ld.\n",
151 physDev
->font
.afm
->FontName
, physDev
->font
.size
,
152 physDev
->font
.tm
.tmWeight
);
153 TRACE("H = %ld As = %ld Des = %ld IL = %ld EL = %ld\n",
154 physDev
->font
.tm
.tmHeight
, physDev
->font
.tm
.tmAscent
,
155 physDev
->font
.tm
.tmDescent
, physDev
->font
.tm
.tmInternalLeading
,
156 physDev
->font
.tm
.tmExternalLeading
);
161 /***********************************************************************
162 * PSDRV_GetTextMetrics
164 BOOL
PSDRV_GetTextMetrics(DC
*dc
, TEXTMETRICA
*metrics
)
166 PSDRV_PDEVICE
*physDev
= (PSDRV_PDEVICE
*)dc
->physDev
;
168 memcpy(metrics
, &(physDev
->font
.tm
), sizeof(physDev
->font
.tm
));
172 /***********************************************************************
173 * PSDRV_UnicodeToANSI
175 char PSDRV_UnicodeToANSI(int u
)
180 case 0x2013: /* endash */
182 case 0x2014: /* emdash */
184 case 0x2018: /* quoteleft */
186 case 0x2019: /* quoteright */
188 case 0x201c: /* quotedblleft */
190 case 0x201d: /* quotedblright */
192 case 0x2022: /* bullet */
195 WARN("Umapped unicode char U%04x\n", u
);
199 /***********************************************************************
200 * PSDRV_GetTextExtentPoint
202 BOOL
PSDRV_GetTextExtentPoint( DC
*dc
, LPCWSTR str
, INT count
,
205 PSDRV_PDEVICE
*physDev
= (PSDRV_PDEVICE
*)dc
->physDev
;
209 size
->cy
= YDSTOLS(dc
, physDev
->font
.tm
.tmHeight
);
212 for(i
= 0; i
< count
&& str
[i
]; i
++) {
213 char c
= PSDRV_UnicodeToANSI(str
[i
]);
214 width
+= physDev
->font
.afm
->CharWidths
[(int)(unsigned char)c
];
215 /* TRACE(psdrv, "Width after %dth char '%c' = %f\n", i, str[i], width);*/
217 width
*= physDev
->font
.scale
;
218 TRACE("Width after scale (%f) is %f\n", physDev
->font
.scale
, width
);
219 size
->cx
= XDSTOLS(dc
, width
);
225 /***********************************************************************
228 BOOL
PSDRV_GetCharWidth( DC
*dc
, UINT firstChar
, UINT lastChar
,
231 PSDRV_PDEVICE
*physDev
= (PSDRV_PDEVICE
*)dc
->physDev
;
234 TRACE("first = %d last = %d\n", firstChar
, lastChar
);
236 if(lastChar
> 0xff) return FALSE
;
237 for( i
= firstChar
; i
<= lastChar
; i
++ )
238 *buffer
++ = physDev
->font
.afm
->CharWidths
[i
] * physDev
->font
.scale
;
244 /***********************************************************************
247 BOOL
PSDRV_SetFont( DC
*dc
)
249 PSDRV_PDEVICE
*physDev
= (PSDRV_PDEVICE
*)dc
->physDev
;
250 BOOL ReEncode
= FALSE
;
252 PSDRV_WriteSetColor(dc
, &physDev
->font
.color
);
253 if(physDev
->font
.set
) return TRUE
;
255 if(physDev
->font
.afm
->EncodingScheme
&&
256 !strcmp(physDev
->font
.afm
->EncodingScheme
, "AdobeStandardEncoding"))
259 PSDRV_WriteReencodeFont(dc
);
260 PSDRV_WriteSetFont(dc
, ReEncode
);
261 physDev
->font
.set
= TRUE
;
266 /***********************************************************************
267 * PSDRV_GetFontMetric
269 static UINT
PSDRV_GetFontMetric(HDC hdc
, AFM
*pafm
, NEWTEXTMETRIC16
*pTM
,
270 ENUMLOGFONTEX16
*pLF
, INT16 size
)
273 DC
*dc
= DC_GetDCPtr( hdc
);
274 float scale
= size
/ (pafm
->FullAscender
- pafm
->Descender
);
276 if (!dc
) return FALSE
;
278 memset( pLF
, 0, sizeof(*pLF
) );
279 memset( pTM
, 0, sizeof(*pTM
) );
281 #define plf ((LPLOGFONT16)pLF)
282 plf
->lfHeight
= pTM
->tmHeight
= size
;
283 plf
->lfWidth
= pTM
->tmAveCharWidth
= pafm
->CharWidths
[120] * scale
;
284 plf
->lfWeight
= pTM
->tmWeight
= pafm
->Weight
;
285 plf
->lfItalic
= pTM
->tmItalic
= pafm
->ItalicAngle
!= 0.0;
286 plf
->lfUnderline
= pTM
->tmUnderlined
= 0;
287 plf
->lfStrikeOut
= pTM
->tmStruckOut
= 0;
288 plf
->lfCharSet
= pTM
->tmCharSet
= ANSI_CHARSET
;
290 /* convert pitch values */
292 pTM
->tmPitchAndFamily
= pafm
->IsFixedPitch
? 0 : TMPF_FIXED_PITCH
;
293 pTM
->tmPitchAndFamily
|= TMPF_DEVICE
;
294 plf
->lfPitchAndFamily
= 0;
296 lstrcpynA( plf
->lfFaceName
, pafm
->FamilyName
, LF_FACESIZE
);
299 pTM
->tmAscent
= pafm
->FullAscender
* scale
;
300 pTM
->tmDescent
= -pafm
->Descender
* scale
;
301 pTM
->tmInternalLeading
= (pafm
->FullAscender
- pafm
->Ascender
) * scale
;
302 pTM
->tmMaxCharWidth
= pafm
->CharWidths
[77] * scale
;
303 pTM
->tmDigitizedAspectX
= dc
->devCaps
->logPixelsY
;
304 pTM
->tmDigitizedAspectY
= dc
->devCaps
->logPixelsX
;
306 *(INT
*)&pTM
->tmFirstChar
= 32;
308 GDI_ReleaseObj( hdc
);
309 /* return font type */
310 return DEVICE_FONTTYPE
;
314 /***********************************************************************
315 * PSDRV_EnumDeviceFonts
317 BOOL
PSDRV_EnumDeviceFonts( HDC hdc
, LPLOGFONT16 plf
,
318 DEVICEFONTENUMPROC proc
, LPARAM lp
)
325 PSDRV_PDEVICE
*physDev
;
327 DC
*dc
= DC_GetDCPtr( hdc
);
328 if (!dc
) return FALSE
;
330 physDev
= (PSDRV_PDEVICE
*)dc
->physDev
;
331 /* FIXME!! should reevaluate dc->physDev after every callback */
332 GDI_ReleaseObj( hdc
);
334 if( plf
->lfFaceName
[0] ) {
335 TRACE("lfFaceName = '%s'\n", plf
->lfFaceName
);
336 for(family
= physDev
->pi
->Fonts
; family
; family
= family
->next
) {
337 if(!strncmp(plf
->lfFaceName
, family
->FamilyName
,
338 strlen(family
->FamilyName
)))
342 for(afmle
= family
->afmlist
; afmle
; afmle
= afmle
->next
) {
343 TRACE("Got '%s'\n", afmle
->afm
->FontName
);
344 if( (b
= (*proc
)( &lf
, &tm
,
345 PSDRV_GetFontMetric( hdc
, afmle
->afm
, &tm
, &lf
, 200 ),
353 TRACE("lfFaceName = NULL\n");
354 for(family
= physDev
->pi
->Fonts
; family
; family
= family
->next
) {
355 afmle
= family
->afmlist
;
356 TRACE("Got '%s'\n", afmle
->afm
->FontName
);
357 if( (b
= (*proc
)( &lf
, &tm
,
358 PSDRV_GetFontMetric( hdc
, afmle
->afm
, &tm
, &lf
, 200 ),