2 * PostScript driver downloadable 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
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(psdrv
);
36 #define MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
37 ( ( (DWORD)_x4 << 24 ) | \
38 ( (DWORD)_x3 << 16 ) | \
39 ( (DWORD)_x2 << 8 ) | \
42 #define GET_BE_WORD(ptr) MAKEWORD( ((BYTE *)(ptr))[1], ((BYTE *)(ptr))[0] )
43 #define GET_BE_DWORD(ptr) ((DWORD)MAKELONG( GET_BE_WORD(&((WORD *)(ptr))[1]), \
44 GET_BE_WORD(&((WORD *)(ptr))[0]) ))
46 /****************************************************************************
49 static void get_download_name(PSDRV_PDEVICE
*physDev
, LPOUTLINETEXTMETRICA
56 size
= GetFontData(physDev
->hdc
, MS_MAKE_TAG('n','a','m','e'), 0, NULL
, 0);
57 if(size
!= 0 && size
!= GDI_ERROR
)
59 BYTE
*name
= HeapAlloc(GetProcessHeap(), 0, size
);
74 GetFontData(physDev
->hdc
, MS_MAKE_TAG('n','a','m','e'), 0, name
, size
);
75 count
= GET_BE_WORD(name
+ 2);
76 strings
= name
+ GET_BE_WORD(name
+ 4);
77 name_record
= (typeof(name_record
))(name
+ 6);
78 for(i
= 0; i
< count
; i
++, name_record
++)
80 name_record
->platform_id
= GET_BE_WORD(&name_record
->platform_id
);
81 name_record
->encoding_id
= GET_BE_WORD(&name_record
->encoding_id
);
82 name_record
->language_id
= GET_BE_WORD(&name_record
->language_id
);
83 name_record
->name_id
= GET_BE_WORD(&name_record
->name_id
);
84 name_record
->length
= GET_BE_WORD(&name_record
->length
);
85 name_record
->offset
= GET_BE_WORD(&name_record
->offset
);
87 if(name_record
->platform_id
== 1 && name_record
->encoding_id
== 0 &&
88 name_record
->language_id
== 0 && name_record
->name_id
== 6)
90 TRACE("Got Mac PS name %s\n", debugstr_an((char*)strings
+ name_record
->offset
, name_record
->length
));
91 *str
= HeapAlloc(GetProcessHeap(), 0, name_record
->length
+ 1);
92 memcpy(*str
, strings
+ name_record
->offset
, name_record
->length
);
93 *(*str
+ name_record
->length
) = '\0';
94 HeapFree(GetProcessHeap(), 0, name
);
97 if(name_record
->platform_id
== 3 && name_record
->encoding_id
== 1 &&
98 name_record
->language_id
== 0x409 && name_record
->name_id
== 6)
100 WCHAR
*unicode
= HeapAlloc(GetProcessHeap(), 0, name_record
->length
+ 2);
104 for(c
= 0; c
< name_record
->length
/ 2; c
++)
105 unicode
[c
] = GET_BE_WORD(strings
+ name_record
->offset
+ c
* 2);
107 TRACE("Got Windows PS name %s\n", debugstr_w(unicode
));
108 len
= WideCharToMultiByte(1252, 0, unicode
, -1, NULL
, 0, NULL
, NULL
);
109 *str
= HeapAlloc(GetProcessHeap(), 0, len
);
110 WideCharToMultiByte(1252, 0, unicode
, -1, *str
, len
, NULL
, NULL
);
111 HeapFree(GetProcessHeap(), 0, unicode
);
112 HeapFree(GetProcessHeap(), 0, name
);
116 TRACE("Unable to find PostScript name\n");
117 HeapFree(GetProcessHeap(), 0, name
);
121 len
= strlen((char*)potm
+ (ptrdiff_t)potm
->otmpFullName
) + 1;
122 *str
= HeapAlloc(GetProcessHeap(),0,len
);
123 strcpy(*str
, (char*)potm
+ (ptrdiff_t)potm
->otmpFullName
);
126 while((p
= strchr(p
, ' ')))
132 /****************************************************************************
135 static DOWNLOAD
*is_font_downloaded(PSDRV_PDEVICE
*physDev
, char *ps_name
)
139 for(pdl
= physDev
->downloaded_fonts
; pdl
; pdl
= pdl
->next
)
140 if(!strcmp(pdl
->ps_name
, ps_name
))
145 /****************************************************************************
148 static BOOL
is_room_for_font(PSDRV_PDEVICE
*physDev
)
153 /* FIXME: should consider vm usage of each font and available printer memory.
154 For now we allow up to two fonts to be downloaded at a time */
155 for(pdl
= physDev
->downloaded_fonts
; pdl
; pdl
= pdl
->next
)
163 /****************************************************************************
166 * This retrieves the bounding box of the font in font units as well as
167 * the size of the emsquare. To avoid having to worry about mapping mode and
168 * the font size we'll get the data directly from the TrueType HEAD table rather
169 * than using GetOutlineTextMetrics.
171 static BOOL
get_bbox(HDC hdc
, RECT
*rc
, UINT
*emsize
)
173 BYTE head
[54]; /* the head table is 54 bytes long */
175 if(GetFontData(hdc
, MS_MAKE_TAG('h','e','a','d'), 0, head
, sizeof(head
)) == GDI_ERROR
)
177 ERR("Can't retrieve head table\n");
180 *emsize
= GET_BE_WORD(head
+ 18); /* unitsPerEm */
183 rc
->left
= (signed short)GET_BE_WORD(head
+ 36); /* xMin */
184 rc
->bottom
= (signed short)GET_BE_WORD(head
+ 38); /* yMin */
185 rc
->right
= (signed short)GET_BE_WORD(head
+ 40); /* xMax */
186 rc
->top
= (signed short)GET_BE_WORD(head
+ 42); /* yMax */
191 /****************************************************************************
192 * PSDRV_SelectDownloadFont
194 * Set up physDev->font for a downloadable font
197 BOOL
PSDRV_SelectDownloadFont(PSDRV_PDEVICE
*physDev
)
199 physDev
->font
.fontloc
= Download
;
200 physDev
->font
.fontinfo
.Download
= NULL
;
205 static UINT
calc_ppem_for_height(HDC hdc
, LONG height
)
207 BYTE os2
[78]; /* size of version 0 table */
208 BYTE hhea
[8]; /* just enough to get the ascender and descender */
209 LONG ascent
= 0, descent
= 0;
212 if(height
< 0) return -height
;
214 if(GetFontData(hdc
, MS_MAKE_TAG('O','S','/','2'), 0, os2
, sizeof(os2
)) == sizeof(os2
))
216 ascent
= GET_BE_WORD(os2
+ 74); /* usWinAscent */
217 descent
= GET_BE_WORD(os2
+ 76); /* usWinDescent */
220 if(ascent
+ descent
== 0)
222 if(GetFontData(hdc
, MS_MAKE_TAG('h','h','e','a'), 0, hhea
, sizeof(hhea
)) == sizeof(hhea
))
224 ascent
= (signed short)GET_BE_WORD(hhea
+ 4); /* Ascender */
225 descent
= -(signed short)GET_BE_WORD(hhea
+ 6); /* Descender */
229 if(ascent
+ descent
== 0) return height
;
231 get_bbox(hdc
, NULL
, &emsize
);
233 return MulDiv(emsize
, height
, ascent
+ descent
);
236 /****************************************************************************
237 * PSDRV_WriteSetDownloadFont
239 * Write setfont for download font.
242 BOOL
PSDRV_WriteSetDownloadFont(PSDRV_PDEVICE
*physDev
)
245 LPOUTLINETEXTMETRICA potm
;
246 DWORD len
= GetOutlineTextMetricsA(physDev
->hdc
, 0, NULL
);
251 assert(physDev
->font
.fontloc
== Download
);
253 potm
= HeapAlloc(GetProcessHeap(), 0, len
);
254 GetOutlineTextMetricsA(physDev
->hdc
, len
, potm
);
256 get_download_name(physDev
, potm
, &ps_name
);
257 physDev
->font
.fontinfo
.Download
= is_font_downloaded(physDev
, ps_name
);
259 if (!GetObjectW( GetCurrentObject(physDev
->hdc
, OBJ_FONT
), sizeof(lf
), &lf
))
262 ppem
= calc_ppem_for_height(physDev
->hdc
, lf
.lfHeight
);
264 physDev
->font
.size
= abs(PSDRV_YWStoDS(physDev
, ppem
));
266 physDev
->font
.underlineThickness
= potm
->otmsUnderscoreSize
;
267 physDev
->font
.underlinePosition
= potm
->otmsUnderscorePosition
;
268 physDev
->font
.strikeoutThickness
= potm
->otmsStrikeoutSize
;
269 physDev
->font
.strikeoutPosition
= potm
->otmsStrikeoutPosition
;
271 if(physDev
->font
.fontinfo
.Download
== NULL
) {
275 if (!get_bbox(physDev
->hdc
, &bbox
, &emsize
)) {
276 HeapFree(GetProcessHeap(), 0, potm
);
279 if(!is_room_for_font(physDev
))
280 PSDRV_EmptyDownloadList(physDev
, TRUE
);
282 pdl
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*pdl
));
283 pdl
->ps_name
= HeapAlloc(GetProcessHeap(), 0, strlen(ps_name
)+1);
284 strcpy(pdl
->ps_name
, ps_name
);
287 if(physDev
->pi
->ppd
->TTRasterizer
== RO_Type42
) {
288 pdl
->typeinfo
.Type42
= T42_download_header(physDev
, ps_name
, &bbox
, emsize
);
291 if(pdl
->typeinfo
.Type42
== NULL
) {
292 pdl
->typeinfo
.Type1
= T1_download_header(physDev
, ps_name
, &bbox
, emsize
);
295 pdl
->next
= physDev
->downloaded_fonts
;
296 physDev
->downloaded_fonts
= pdl
;
297 physDev
->font
.fontinfo
.Download
= pdl
;
299 if(pdl
->type
== Type42
) {
300 char g_name
[MAX_G_NAME
+ 1];
301 get_glyph_name(physDev
->hdc
, 0, g_name
);
302 T42_download_glyph(physDev
, pdl
, 0, g_name
);
307 PSDRV_WriteSetFont(physDev
, ps_name
, physDev
->font
.size
,
308 physDev
->font
.escapement
);
310 HeapFree(GetProcessHeap(), 0, ps_name
);
311 HeapFree(GetProcessHeap(), 0, potm
);
315 void get_glyph_name(HDC hdc
, WORD index
, char *name
)
318 sprintf(name
, "g%04x", index
);
322 /****************************************************************************
323 * PSDRV_WriteDownloadGlyphShow
325 * Download and write out a number of glyphs
328 BOOL
PSDRV_WriteDownloadGlyphShow(PSDRV_PDEVICE
*physDev
, WORD
*glyphs
,
332 char g_name
[MAX_G_NAME
+ 1];
333 assert(physDev
->font
.fontloc
== Download
);
335 switch(physDev
->font
.fontinfo
.Download
->type
) {
337 for(i
= 0; i
< count
; i
++) {
338 get_glyph_name(physDev
->hdc
, glyphs
[i
], g_name
);
339 T42_download_glyph(physDev
, physDev
->font
.fontinfo
.Download
,
341 PSDRV_WriteGlyphShow(physDev
, g_name
);
346 for(i
= 0; i
< count
; i
++) {
347 get_glyph_name(physDev
->hdc
, glyphs
[i
], g_name
);
348 T1_download_glyph(physDev
, physDev
->font
.fontinfo
.Download
,
350 PSDRV_WriteGlyphShow(physDev
, g_name
);
355 ERR("Type = %d\n", physDev
->font
.fontinfo
.Download
->type
);
361 /****************************************************************************
362 * PSDRV_EmptyDownloadList
364 * Clear the list of downloaded fonts
367 BOOL
PSDRV_EmptyDownloadList(PSDRV_PDEVICE
*physDev
, BOOL write_undef
)
370 static const char undef
[] = "/%s findfont 40 scalefont setfont /%s undefinefont\n";
371 char buf
[sizeof(undef
) + 200];
372 const char *default_font
= physDev
->pi
->ppd
->DefaultFont
?
373 physDev
->pi
->ppd
->DefaultFont
: "Courier";
375 if(physDev
->font
.fontloc
== Download
) {
376 physDev
->font
.set
= FALSE
;
377 physDev
->font
.fontinfo
.Download
= NULL
;
380 pdl
= physDev
->downloaded_fonts
;
381 physDev
->downloaded_fonts
= NULL
;
384 sprintf(buf
, undef
, default_font
, pdl
->ps_name
);
385 PSDRV_WriteSpool(physDev
, buf
, strlen(buf
));
390 T42_free(pdl
->typeinfo
.Type42
);
394 T1_free(pdl
->typeinfo
.Type1
);
398 ERR("Type = %d\n", pdl
->type
);
402 HeapFree(GetProcessHeap(), 0, pdl
->ps_name
);
405 HeapFree(GetProcessHeap(), 0, old
);