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
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(psdrv
);
39 #define MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
40 ( ( (DWORD)_x4 << 24 ) | \
41 ( (DWORD)_x3 << 16 ) | \
42 ( (DWORD)_x2 << 8 ) | \
45 #define GET_BE_WORD(ptr) MAKEWORD( ((BYTE *)(ptr))[1], ((BYTE *)(ptr))[0] )
46 #define GET_BE_DWORD(ptr) ((DWORD)MAKELONG( GET_BE_WORD(&((WORD *)(ptr))[1]), \
47 GET_BE_WORD(&((WORD *)(ptr))[0]) ))
49 /****************************************************************************
52 static void get_download_name(PHYSDEV dev
, LPOUTLINETEXTMETRICA potm
, char **str
, BOOL vertical
)
54 static const char reserved_chars
[] = " %/(){}[]<>\n\r\t\b\f";
55 static const char vertical_suffix
[] = "_vert";
60 size
= GetFontData(dev
->hdc
, MS_MAKE_TAG('n','a','m','e'), 0, NULL
, 0);
61 if(size
!= 0 && size
!= GDI_ERROR
)
63 BYTE
*name
= HeapAlloc(GetProcessHeap(), 0, size
);
78 GetFontData(dev
->hdc
, MS_MAKE_TAG('n','a','m','e'), 0, name
, size
);
79 count
= GET_BE_WORD(name
+ 2);
80 strings
= name
+ GET_BE_WORD(name
+ 4);
81 name_record
= (typeof(name_record
))(name
+ 6);
82 for(i
= 0; i
< count
; i
++, name_record
++)
84 name_record
->platform_id
= GET_BE_WORD(&name_record
->platform_id
);
85 name_record
->encoding_id
= GET_BE_WORD(&name_record
->encoding_id
);
86 name_record
->language_id
= GET_BE_WORD(&name_record
->language_id
);
87 name_record
->name_id
= GET_BE_WORD(&name_record
->name_id
);
88 name_record
->length
= GET_BE_WORD(&name_record
->length
);
89 name_record
->offset
= GET_BE_WORD(&name_record
->offset
);
91 if(name_record
->platform_id
== 1 && name_record
->encoding_id
== 0 &&
92 name_record
->language_id
== 0 && name_record
->name_id
== 6)
94 TRACE("Got Mac PS name %s\n", debugstr_an((char*)strings
+ name_record
->offset
, name_record
->length
));
95 *str
= HeapAlloc(GetProcessHeap(), 0, name_record
->length
+ sizeof(vertical_suffix
));
96 memcpy(*str
, strings
+ name_record
->offset
, name_record
->length
);
97 *(*str
+ name_record
->length
) = '\0';
98 HeapFree(GetProcessHeap(), 0, name
);
101 if(name_record
->platform_id
== 3 && name_record
->encoding_id
== 1 &&
102 name_record
->language_id
== 0x409 && name_record
->name_id
== 6)
104 WCHAR
*unicode
= HeapAlloc(GetProcessHeap(), 0, name_record
->length
+ 2);
108 for(c
= 0; c
< name_record
->length
/ 2; c
++)
109 unicode
[c
] = GET_BE_WORD(strings
+ name_record
->offset
+ c
* 2);
111 TRACE("Got Windows PS name %s\n", debugstr_w(unicode
));
112 len
= WideCharToMultiByte(1252, 0, unicode
, -1, NULL
, 0, NULL
, NULL
);
113 *str
= HeapAlloc(GetProcessHeap(), 0, len
+ sizeof(vertical_suffix
) - 1);
114 WideCharToMultiByte(1252, 0, unicode
, -1, *str
, len
, NULL
, NULL
);
115 HeapFree(GetProcessHeap(), 0, unicode
);
116 HeapFree(GetProcessHeap(), 0, name
);
120 TRACE("Unable to find PostScript name\n");
121 HeapFree(GetProcessHeap(), 0, name
);
125 len
= strlen((char*)potm
+ (ptrdiff_t)potm
->otmpFaceName
) + 1;
126 *str
= HeapAlloc(GetProcessHeap(),0,len
+ sizeof(vertical_suffix
) - 1);
127 strcpy(*str
, (char*)potm
+ (ptrdiff_t)potm
->otmpFaceName
);
130 for (p
= *str
; *p
; p
++) if (strchr( reserved_chars
, *p
)) *p
= '_';
132 strcat(*str
,vertical_suffix
);
135 /****************************************************************************
138 static DOWNLOAD
*is_font_downloaded(PSDRV_PDEVICE
*physDev
, char *ps_name
)
142 for(pdl
= physDev
->downloaded_fonts
; pdl
; pdl
= pdl
->next
)
143 if(!strcmp(pdl
->ps_name
, ps_name
))
148 /****************************************************************************
151 static BOOL
is_room_for_font(PSDRV_PDEVICE
*physDev
)
156 /* FIXME: should consider vm usage of each font and available printer memory.
157 For now we allow up to two fonts to be downloaded at a time */
158 for(pdl
= physDev
->downloaded_fonts
; pdl
; pdl
= pdl
->next
)
166 /****************************************************************************
169 * This retrieves the bounding box of the font in font units as well as
170 * the size of the emsquare. To avoid having to worry about mapping mode and
171 * the font size we'll get the data directly from the TrueType HEAD table rather
172 * than using GetOutlineTextMetrics.
174 static UINT
get_bbox(HDC hdc
, RECT
*rc
)
176 BYTE head
[54]; /* the head table is 54 bytes long */
178 if(GetFontData(hdc
, MS_MAKE_TAG('h','e','a','d'), 0, head
, sizeof(head
)) == GDI_ERROR
)
180 ERR("Can't retrieve head table\n");
185 rc
->left
= (signed short)GET_BE_WORD(head
+ 36); /* xMin */
186 rc
->bottom
= (signed short)GET_BE_WORD(head
+ 38); /* yMin */
187 rc
->right
= (signed short)GET_BE_WORD(head
+ 40); /* xMax */
188 rc
->top
= (signed short)GET_BE_WORD(head
+ 42); /* yMax */
190 return GET_BE_WORD(head
+ 18); /* unitsPerEm */
193 /****************************************************************************
194 * PSDRV_SelectDownloadFont
196 * Set up physDev->font for a downloadable font
199 BOOL
PSDRV_SelectDownloadFont(PHYSDEV dev
)
201 PSDRV_PDEVICE
*physDev
= get_psdrv_dev( dev
);
203 physDev
->font
.fontloc
= Download
;
204 physDev
->font
.fontinfo
.Download
= NULL
;
209 static UINT
calc_ppem_for_height(HDC hdc
, LONG height
)
211 BYTE os2
[78]; /* size of version 0 table */
212 BYTE hhea
[8]; /* just enough to get the ascender and descender */
213 LONG ascent
= 0, descent
= 0;
216 if(height
< 0) return -height
;
218 if(GetFontData(hdc
, MS_MAKE_TAG('O','S','/','2'), 0, os2
, sizeof(os2
)) == sizeof(os2
))
220 ascent
= GET_BE_WORD(os2
+ 74); /* usWinAscent */
221 descent
= GET_BE_WORD(os2
+ 76); /* usWinDescent */
224 if(ascent
+ descent
== 0)
226 if(GetFontData(hdc
, MS_MAKE_TAG('h','h','e','a'), 0, hhea
, sizeof(hhea
)) == sizeof(hhea
))
228 ascent
= (signed short)GET_BE_WORD(hhea
+ 4); /* Ascender */
229 descent
= -(signed short)GET_BE_WORD(hhea
+ 6); /* Descender */
233 if(ascent
+ descent
== 0) return height
;
235 emsize
= get_bbox(hdc
, NULL
);
237 return MulDiv(emsize
, height
, ascent
+ descent
);
240 static inline float ps_round(float f
)
242 return (f
> 0) ? (f
+ 0.5) : (f
- 0.5);
245 static BOOL
is_fake_italic( HDC hdc
)
248 BYTE head
[54]; /* the head table is 54 bytes long */
251 GetTextMetricsW( hdc
, &tm
);
252 if (!tm
.tmItalic
) return FALSE
;
254 if (GetFontData( hdc
, MS_MAKE_TAG('h','e','a','d'), 0, head
, sizeof(head
) ) == GDI_ERROR
)
257 mac_style
= GET_BE_WORD( head
+ 44 );
258 TRACE( "mac style %04x\n", mac_style
);
259 return !(mac_style
& 2);
262 /****************************************************************************
263 * PSDRV_WriteSetDownloadFont
265 * Write setfont for download font.
268 BOOL
PSDRV_WriteSetDownloadFont(PHYSDEV dev
, BOOL vertical
)
270 PSDRV_PDEVICE
*physDev
= get_psdrv_dev( dev
);
272 LPOUTLINETEXTMETRICA potm
;
273 DWORD len
= GetOutlineTextMetricsA(dev
->hdc
, 0, NULL
);
280 assert(physDev
->font
.fontloc
== Download
);
282 if (!GetObjectW( GetCurrentObject(dev
->hdc
, OBJ_FONT
), sizeof(lf
), &lf
))
285 potm
= HeapAlloc(GetProcessHeap(), 0, len
);
289 GetOutlineTextMetricsA(dev
->hdc
, len
, potm
);
291 get_download_name(dev
, potm
, &ps_name
, vertical
);
292 physDev
->font
.fontinfo
.Download
= is_font_downloaded(physDev
, ps_name
);
294 ppem
= calc_ppem_for_height(dev
->hdc
, lf
.lfHeight
);
296 /* Retrieve the world -> device transform */
297 GetTransform(dev
->hdc
, 0x204, &xform
);
299 if(GetGraphicsMode(dev
->hdc
) == GM_COMPATIBLE
)
301 if (xform
.eM22
< 0) physDev
->font
.escapement
= -physDev
->font
.escapement
;
302 xform
.eM11
= xform
.eM22
= fabs(xform
.eM22
);
303 xform
.eM21
= xform
.eM12
= 0;
306 physDev
->font
.size
.xx
= ps_round(ppem
* xform
.eM11
);
307 physDev
->font
.size
.xy
= ps_round(ppem
* xform
.eM12
);
308 physDev
->font
.size
.yx
= -ps_round(ppem
* xform
.eM21
);
309 physDev
->font
.size
.yy
= -ps_round(ppem
* xform
.eM22
);
311 physDev
->font
.underlineThickness
= potm
->otmsUnderscoreSize
;
312 physDev
->font
.underlinePosition
= potm
->otmsUnderscorePosition
;
313 physDev
->font
.strikeoutThickness
= potm
->otmsStrikeoutSize
;
314 physDev
->font
.strikeoutPosition
= potm
->otmsStrikeoutPosition
;
316 if(physDev
->font
.fontinfo
.Download
== NULL
) {
318 UINT emsize
= get_bbox(dev
->hdc
, &bbox
);
321 HeapFree(GetProcessHeap(), 0, ps_name
);
322 HeapFree(GetProcessHeap(), 0, potm
);
325 if(!is_room_for_font(physDev
))
326 PSDRV_EmptyDownloadList(dev
, TRUE
);
328 pdl
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*pdl
));
329 pdl
->ps_name
= HeapAlloc(GetProcessHeap(), 0, strlen(ps_name
)+1);
330 strcpy(pdl
->ps_name
, ps_name
);
333 if(physDev
->pi
->ppd
->TTRasterizer
== RO_Type42
) {
334 pdl
->typeinfo
.Type42
= T42_download_header(dev
, ps_name
, &bbox
, emsize
);
337 if(pdl
->typeinfo
.Type42
== NULL
) {
338 pdl
->typeinfo
.Type1
= T1_download_header(dev
, ps_name
, &bbox
, emsize
);
341 pdl
->next
= physDev
->downloaded_fonts
;
342 physDev
->downloaded_fonts
= pdl
;
343 physDev
->font
.fontinfo
.Download
= pdl
;
345 if(pdl
->type
== Type42
) {
346 char g_name
[MAX_G_NAME
+ 1];
347 get_glyph_name(dev
->hdc
, 0, g_name
);
348 T42_download_glyph(dev
, pdl
, 0, g_name
);
352 escapement
= physDev
->font
.escapement
;
356 PSDRV_WriteSetFont(dev
, ps_name
, physDev
->font
.size
, escapement
,
357 is_fake_italic( dev
->hdc
));
359 HeapFree(GetProcessHeap(), 0, ps_name
);
360 HeapFree(GetProcessHeap(), 0, potm
);
364 static void get_standard_glyph_name(WORD index
, char *name
)
366 static const GLYPHNAME nonbreakingspace
= { -1, "nonbreakingspace" };
367 static const GLYPHNAME nonmarkingreturn
= { -1, "nonmarkingreturn" };
368 static const GLYPHNAME notdef
= { -1, ".notdef" };
369 static const GLYPHNAME null
= { -1, ".null" };
370 /* These PostScript Format 1 glyph names are stored by glyph index, do not reorder them. */
371 static const GLYPHNAME
*glyph_table
[] = {
631 snprintf(name
, MAX_G_NAME
+ 1, "%s", glyph_table
[index
]->sz
);
634 static int get_post2_name_index(BYTE
*post2header
, DWORD size
, WORD index
)
636 USHORT numberOfGlyphs
= GET_BE_WORD(post2header
);
637 DWORD offset
= (1 + index
) * sizeof(USHORT
);
639 if(offset
+ sizeof(USHORT
) > size
|| index
>= numberOfGlyphs
)
641 FIXME("Index '%d' exceeds PostScript Format 2 table size (%d)\n", index
, numberOfGlyphs
);
644 return GET_BE_WORD(post2header
+ offset
);
647 static void get_post2_custom_glyph_name(BYTE
*post2header
, DWORD size
, WORD index
, char *name
)
649 USHORT numberOfGlyphs
= GET_BE_WORD(post2header
);
650 int i
, name_offset
= (1 + numberOfGlyphs
) * sizeof(USHORT
);
651 BYTE name_length
= 0;
653 for(i
= 0; i
<= index
; i
++)
655 name_offset
+= name_length
;
656 if(name_offset
+ sizeof(BYTE
) > size
)
658 FIXME("Pascal name offset '%d' exceeds PostScript Format 2 table size (%d)\n",
659 name_offset
+ 1, size
);
662 name_length
= (post2header
+ name_offset
)[0];
663 if(name_offset
+ name_length
> size
)
665 FIXME("Pascal name offset '%d' exceeds PostScript Format 2 table size (%d)\n",
666 name_offset
+ name_length
, size
);
669 name_offset
+= sizeof(BYTE
);
671 name_length
= min(name_length
, MAX_G_NAME
);
672 memcpy(name
, post2header
+ name_offset
, name_length
);
673 name
[name_length
] = 0;
676 void get_glyph_name(HDC hdc
, WORD index
, char *name
)
682 SHORT underlinePosition
;
683 SHORT underlineThickness
;
693 /* set a fallback name that is just 'g<index>' */
694 snprintf(name
, MAX_G_NAME
+ 1, "g%04x", index
);
696 /* attempt to obtain the glyph name from the 'post' table */
697 size
= GetFontData(hdc
, MS_MAKE_TAG('p','o','s','t'), 0, NULL
, 0);
698 if(size
< sizeof(*post_header
) || size
== GDI_ERROR
)
700 post
= HeapAlloc(GetProcessHeap(), 0, size
);
703 size
= GetFontData(hdc
, MS_MAKE_TAG('p','o','s','t'), 0, post
, size
);
704 if(size
< sizeof(*post_header
) || size
== GDI_ERROR
)
706 post_header
= (typeof(post_header
))(post
);
707 /* note: only interested in the format for obtaining glyph names */
708 post_header
->format
= GET_BE_DWORD(&post_header
->format
);
710 /* now that we know the format of the 'post' table we can get the glyph name */
711 if(post_header
->format
== MAKELONG(0, 1))
714 get_standard_glyph_name(index
, name
);
716 WARN("Font uses PostScript Format 1, but non-standard glyph (%d) requested.\n", index
);
718 else if(post_header
->format
== MAKELONG(0, 2))
720 BYTE
*post2header
= post
+ sizeof(*post_header
);
723 size
-= sizeof(*post_header
);
724 if(size
< sizeof(USHORT
))
726 FIXME("PostScript Format 2 table is invalid (cannot fit header)\n");
729 glyphNameIndex
= get_post2_name_index(post2header
, size
, index
);
730 if(glyphNameIndex
== -1)
731 goto cleanup
; /* invalid index, use fallback name */
732 else if(glyphNameIndex
< 258)
733 get_standard_glyph_name(glyphNameIndex
, name
);
735 get_post2_custom_glyph_name(post2header
, size
, glyphNameIndex
- 258, name
);
738 FIXME("PostScript Format %d.%d glyph names are currently unsupported.\n",
739 HIWORD(post_header
->format
), LOWORD(post_header
->format
));
742 HeapFree(GetProcessHeap(), 0, post
);
745 /****************************************************************************
746 * PSDRV_WriteDownloadGlyphShow
748 * Download and write out a number of glyphs
751 BOOL
PSDRV_WriteDownloadGlyphShow(PHYSDEV dev
, const WORD
*glyphs
,
754 PSDRV_PDEVICE
*physDev
= get_psdrv_dev( dev
);
756 char g_name
[MAX_G_NAME
+ 1];
757 assert(physDev
->font
.fontloc
== Download
);
759 switch(physDev
->font
.fontinfo
.Download
->type
) {
761 for(i
= 0; i
< count
; i
++) {
762 get_glyph_name(dev
->hdc
, glyphs
[i
], g_name
);
763 T42_download_glyph(dev
, physDev
->font
.fontinfo
.Download
, glyphs
[i
], g_name
);
764 PSDRV_WriteGlyphShow(dev
, g_name
);
769 for(i
= 0; i
< count
; i
++) {
770 get_glyph_name(dev
->hdc
, glyphs
[i
], g_name
);
771 T1_download_glyph(dev
, physDev
->font
.fontinfo
.Download
, glyphs
[i
], g_name
);
772 PSDRV_WriteGlyphShow(dev
, g_name
);
777 ERR("Type = %d\n", physDev
->font
.fontinfo
.Download
->type
);
783 /****************************************************************************
784 * PSDRV_EmptyDownloadList
786 * Clear the list of downloaded fonts
789 BOOL
PSDRV_EmptyDownloadList(PHYSDEV dev
, BOOL write_undef
)
791 PSDRV_PDEVICE
*physDev
= get_psdrv_dev( dev
);
793 static const char undef
[] = "/%s findfont 40 scalefont setfont /%s undefinefont\n";
794 char buf
[sizeof(undef
) + 200];
795 const char *default_font
= physDev
->pi
->ppd
->DefaultFont
?
796 physDev
->pi
->ppd
->DefaultFont
: "Courier";
798 if(physDev
->font
.fontloc
== Download
) {
799 physDev
->font
.set
= FALSE
;
800 physDev
->font
.fontinfo
.Download
= NULL
;
803 pdl
= physDev
->downloaded_fonts
;
804 physDev
->downloaded_fonts
= NULL
;
807 sprintf(buf
, undef
, default_font
, pdl
->ps_name
);
808 PSDRV_WriteSpool(dev
, buf
, strlen(buf
));
813 T42_free(pdl
->typeinfo
.Type42
);
817 T1_free(pdl
->typeinfo
.Type1
);
821 ERR("Type = %d\n", pdl
->type
);
825 HeapFree(GetProcessHeap(), 0, pdl
->ps_name
);
828 HeapFree(GetProcessHeap(), 0, old
);