wined3d: Pass a resource to wined3d_resource_free_sysmem().
[wine.git] / dlls / wineps.drv / download.c
blobf47684319e862e3fce47adf66b31ad0c4f54270a
1 /*
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
20 #include <string.h>
21 #include <stdlib.h>
22 #include <assert.h>
23 #include <math.h>
24 #include <stdarg.h>
25 #include <stdio.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winnls.h"
32 #include "psdrv.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
37 #define MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
38 ( ( (DWORD)_x4 << 24 ) | \
39 ( (DWORD)_x3 << 16 ) | \
40 ( (DWORD)_x2 << 8 ) | \
41 (DWORD)_x1 )
43 #define GET_BE_WORD(ptr) MAKEWORD( ((BYTE *)(ptr))[1], ((BYTE *)(ptr))[0] )
44 #define GET_BE_DWORD(ptr) ((DWORD)MAKELONG( GET_BE_WORD(&((WORD *)(ptr))[1]), \
45 GET_BE_WORD(&((WORD *)(ptr))[0]) ))
47 /****************************************************************************
48 * get_download_name
50 static void get_download_name(PHYSDEV dev, LPOUTLINETEXTMETRICA potm, char **str, BOOL vertical)
52 static const char reserved_chars[] = " %/(){}[]<>\n\r\t\b\f";
53 static const char vertical_suffix[] = "_vert";
54 int len;
55 char *p;
56 DWORD size;
58 size = GetFontData(dev->hdc, MS_MAKE_TAG('n','a','m','e'), 0, NULL, 0);
59 if(size != 0 && size != GDI_ERROR)
61 BYTE *name = HeapAlloc(GetProcessHeap(), 0, size);
62 if(name)
64 USHORT count, i;
65 BYTE *strings;
66 struct
68 USHORT platform_id;
69 USHORT encoding_id;
70 USHORT language_id;
71 USHORT name_id;
72 USHORT length;
73 USHORT offset;
74 } *name_record;
76 GetFontData(dev->hdc, MS_MAKE_TAG('n','a','m','e'), 0, name, size);
77 count = GET_BE_WORD(name + 2);
78 strings = name + GET_BE_WORD(name + 4);
79 name_record = (typeof(name_record))(name + 6);
80 for(i = 0; i < count; i++, name_record++)
82 name_record->platform_id = GET_BE_WORD(&name_record->platform_id);
83 name_record->encoding_id = GET_BE_WORD(&name_record->encoding_id);
84 name_record->language_id = GET_BE_WORD(&name_record->language_id);
85 name_record->name_id = GET_BE_WORD(&name_record->name_id);
86 name_record->length = GET_BE_WORD(&name_record->length);
87 name_record->offset = GET_BE_WORD(&name_record->offset);
89 if(name_record->platform_id == 1 && name_record->encoding_id == 0 &&
90 name_record->language_id == 0 && name_record->name_id == 6)
92 TRACE("Got Mac PS name %s\n", debugstr_an((char*)strings + name_record->offset, name_record->length));
93 *str = HeapAlloc(GetProcessHeap(), 0, name_record->length + sizeof(vertical_suffix));
94 memcpy(*str, strings + name_record->offset, name_record->length);
95 *(*str + name_record->length) = '\0';
96 HeapFree(GetProcessHeap(), 0, name);
97 goto done;
99 if(name_record->platform_id == 3 && name_record->encoding_id == 1 &&
100 name_record->language_id == 0x409 && name_record->name_id == 6)
102 WCHAR *unicode = HeapAlloc(GetProcessHeap(), 0, name_record->length + 2);
103 DWORD len;
104 int c;
106 for(c = 0; c < name_record->length / 2; c++)
107 unicode[c] = GET_BE_WORD(strings + name_record->offset + c * 2);
108 unicode[c] = 0;
109 TRACE("Got Windows PS name %s\n", debugstr_w(unicode));
110 len = WideCharToMultiByte(1252, 0, unicode, -1, NULL, 0, NULL, NULL);
111 *str = HeapAlloc(GetProcessHeap(), 0, len + sizeof(vertical_suffix) - 1);
112 WideCharToMultiByte(1252, 0, unicode, -1, *str, len, NULL, NULL);
113 HeapFree(GetProcessHeap(), 0, unicode);
114 HeapFree(GetProcessHeap(), 0, name);
115 goto done;
118 TRACE("Unable to find PostScript name\n");
119 HeapFree(GetProcessHeap(), 0, name);
123 len = strlen((char*)potm + (ptrdiff_t)potm->otmpFaceName) + 1;
124 *str = HeapAlloc(GetProcessHeap(),0,len + sizeof(vertical_suffix) - 1);
125 strcpy(*str, (char*)potm + (ptrdiff_t)potm->otmpFaceName);
127 done:
128 for (p = *str; *p; p++) if (strchr( reserved_chars, *p )) *p = '_';
129 if (vertical)
130 strcat(*str,vertical_suffix);
133 /****************************************************************************
134 * is_font_downloaded
136 static DOWNLOAD *is_font_downloaded(PSDRV_PDEVICE *physDev, char *ps_name)
138 DOWNLOAD *pdl;
140 for(pdl = physDev->downloaded_fonts; pdl; pdl = pdl->next)
141 if(!strcmp(pdl->ps_name, ps_name))
142 break;
143 return pdl;
146 /****************************************************************************
147 * is_room_for_font
149 static BOOL is_room_for_font(PSDRV_PDEVICE *physDev)
151 DOWNLOAD *pdl;
152 int count = 0;
154 /* FIXME: should consider vm usage of each font and available printer memory.
155 For now we allow up to two fonts to be downloaded at a time */
156 for(pdl = physDev->downloaded_fonts; pdl; pdl = pdl->next)
157 count++;
159 if(count > 1)
160 return FALSE;
161 return TRUE;
164 /****************************************************************************
165 * get_bbox
167 * This retrieves the bounding box of the font in font units as well as
168 * the size of the emsquare. To avoid having to worry about mapping mode and
169 * the font size we'll get the data directly from the TrueType HEAD table rather
170 * than using GetOutlineTextMetrics.
172 static BOOL get_bbox(HDC hdc, RECT *rc, UINT *emsize)
174 BYTE head[54]; /* the head table is 54 bytes long */
176 if(GetFontData(hdc, MS_MAKE_TAG('h','e','a','d'), 0, head, sizeof(head)) == GDI_ERROR)
178 ERR("Can't retrieve head table\n");
179 return FALSE;
181 *emsize = GET_BE_WORD(head + 18); /* unitsPerEm */
182 if(rc)
184 rc->left = (signed short)GET_BE_WORD(head + 36); /* xMin */
185 rc->bottom = (signed short)GET_BE_WORD(head + 38); /* yMin */
186 rc->right = (signed short)GET_BE_WORD(head + 40); /* xMax */
187 rc->top = (signed short)GET_BE_WORD(head + 42); /* yMax */
189 return TRUE;
192 /****************************************************************************
193 * PSDRV_SelectDownloadFont
195 * Set up physDev->font for a downloadable font
198 BOOL PSDRV_SelectDownloadFont(PHYSDEV dev)
200 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
202 physDev->font.fontloc = Download;
203 physDev->font.fontinfo.Download = NULL;
205 return TRUE;
208 static UINT calc_ppem_for_height(HDC hdc, LONG height)
210 BYTE os2[78]; /* size of version 0 table */
211 BYTE hhea[8]; /* just enough to get the ascender and descender */
212 LONG ascent = 0, descent = 0;
213 UINT emsize;
215 if(height < 0) return -height;
217 if(GetFontData(hdc, MS_MAKE_TAG('O','S','/','2'), 0, os2, sizeof(os2)) == sizeof(os2))
219 ascent = GET_BE_WORD(os2 + 74); /* usWinAscent */
220 descent = GET_BE_WORD(os2 + 76); /* usWinDescent */
223 if(ascent + descent == 0)
225 if(GetFontData(hdc, MS_MAKE_TAG('h','h','e','a'), 0, hhea, sizeof(hhea)) == sizeof(hhea))
227 ascent = (signed short)GET_BE_WORD(hhea + 4); /* Ascender */
228 descent = -(signed short)GET_BE_WORD(hhea + 6); /* Descender */
232 if(ascent + descent == 0) return height;
234 get_bbox(hdc, NULL, &emsize);
236 return MulDiv(emsize, height, ascent + descent);
239 static inline float ps_round(float f)
241 return (f > 0) ? (f + 0.5) : (f - 0.5);
244 static BOOL is_fake_italic( HDC hdc )
246 TEXTMETRICW tm;
247 BYTE head[54]; /* the head table is 54 bytes long */
248 WORD mac_style;
250 GetTextMetricsW( hdc, &tm );
251 if (!tm.tmItalic) return FALSE;
253 if (GetFontData( hdc, MS_MAKE_TAG('h','e','a','d'), 0, head, sizeof(head) ) == GDI_ERROR)
254 return FALSE;
256 mac_style = GET_BE_WORD( head + 44 );
257 TRACE( "mac style %04x\n", mac_style );
258 return !(mac_style & 2);
261 /****************************************************************************
262 * PSDRV_WriteSetDownloadFont
264 * Write setfont for download font.
267 BOOL PSDRV_WriteSetDownloadFont(PHYSDEV dev, BOOL vertical)
269 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
270 char *ps_name;
271 LPOUTLINETEXTMETRICA potm;
272 DWORD len = GetOutlineTextMetricsA(dev->hdc, 0, NULL);
273 DOWNLOAD *pdl;
274 LOGFONTW lf;
275 UINT ppem;
276 XFORM xform;
277 INT escapement;
279 assert(physDev->font.fontloc == Download);
281 if (!GetObjectW( GetCurrentObject(dev->hdc, OBJ_FONT), sizeof(lf), &lf ))
282 return FALSE;
284 potm = HeapAlloc(GetProcessHeap(), 0, len);
285 if (!potm)
286 return FALSE;
288 GetOutlineTextMetricsA(dev->hdc, len, potm);
290 get_download_name(dev, potm, &ps_name, vertical);
291 physDev->font.fontinfo.Download = is_font_downloaded(physDev, ps_name);
293 ppem = calc_ppem_for_height(dev->hdc, lf.lfHeight);
295 /* Retrieve the world -> device transform */
296 GetTransform(dev->hdc, 0x204, &xform);
298 if(GetGraphicsMode(dev->hdc) == GM_COMPATIBLE)
300 if (xform.eM22 < 0) physDev->font.escapement = -physDev->font.escapement;
301 xform.eM11 = xform.eM22 = fabs(xform.eM22);
302 xform.eM21 = xform.eM12 = 0;
305 physDev->font.size.xx = ps_round(ppem * xform.eM11);
306 physDev->font.size.xy = ps_round(ppem * xform.eM12);
307 physDev->font.size.yx = -ps_round(ppem * xform.eM21);
308 physDev->font.size.yy = -ps_round(ppem * xform.eM22);
310 physDev->font.underlineThickness = potm->otmsUnderscoreSize;
311 physDev->font.underlinePosition = potm->otmsUnderscorePosition;
312 physDev->font.strikeoutThickness = potm->otmsStrikeoutSize;
313 physDev->font.strikeoutPosition = potm->otmsStrikeoutPosition;
315 if(physDev->font.fontinfo.Download == NULL) {
316 RECT bbox;
317 UINT emsize;
319 if (!get_bbox(dev->hdc, &bbox, &emsize)) {
320 HeapFree(GetProcessHeap(), 0, ps_name);
321 HeapFree(GetProcessHeap(), 0, potm);
322 return FALSE;
324 if(!is_room_for_font(physDev))
325 PSDRV_EmptyDownloadList(dev, TRUE);
327 pdl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pdl));
328 pdl->ps_name = HeapAlloc(GetProcessHeap(), 0, strlen(ps_name)+1);
329 strcpy(pdl->ps_name, ps_name);
330 pdl->next = NULL;
332 if(physDev->pi->ppd->TTRasterizer == RO_Type42) {
333 pdl->typeinfo.Type42 = T42_download_header(dev, ps_name, &bbox, emsize);
334 pdl->type = Type42;
336 if(pdl->typeinfo.Type42 == NULL) {
337 pdl->typeinfo.Type1 = T1_download_header(dev, ps_name, &bbox, emsize);
338 pdl->type = Type1;
340 pdl->next = physDev->downloaded_fonts;
341 physDev->downloaded_fonts = pdl;
342 physDev->font.fontinfo.Download = pdl;
344 if(pdl->type == Type42) {
345 char g_name[MAX_G_NAME + 1];
346 get_glyph_name(dev->hdc, 0, g_name);
347 T42_download_glyph(dev, pdl, 0, g_name);
351 escapement = physDev->font.escapement;
352 if (vertical)
353 escapement += 900;
355 PSDRV_WriteSetFont(dev, ps_name, physDev->font.size, escapement,
356 is_fake_italic( dev->hdc ));
358 HeapFree(GetProcessHeap(), 0, ps_name);
359 HeapFree(GetProcessHeap(), 0, potm);
360 return TRUE;
363 void get_glyph_name(HDC hdc, WORD index, char *name)
365 /* FIXME */
366 sprintf(name, "g%04x", index);
367 return;
370 /****************************************************************************
371 * PSDRV_WriteDownloadGlyphShow
373 * Download and write out a number of glyphs
376 BOOL PSDRV_WriteDownloadGlyphShow(PHYSDEV dev, const WORD *glyphs,
377 UINT count)
379 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
380 UINT i;
381 char g_name[MAX_G_NAME + 1];
382 assert(physDev->font.fontloc == Download);
384 switch(physDev->font.fontinfo.Download->type) {
385 case Type42:
386 for(i = 0; i < count; i++) {
387 get_glyph_name(dev->hdc, glyphs[i], g_name);
388 T42_download_glyph(dev, physDev->font.fontinfo.Download, glyphs[i], g_name);
389 PSDRV_WriteGlyphShow(dev, g_name);
391 break;
393 case Type1:
394 for(i = 0; i < count; i++) {
395 get_glyph_name(dev->hdc, glyphs[i], g_name);
396 T1_download_glyph(dev, physDev->font.fontinfo.Download, glyphs[i], g_name);
397 PSDRV_WriteGlyphShow(dev, g_name);
399 break;
401 default:
402 ERR("Type = %d\n", physDev->font.fontinfo.Download->type);
403 assert(0);
405 return TRUE;
408 /****************************************************************************
409 * PSDRV_EmptyDownloadList
411 * Clear the list of downloaded fonts
414 BOOL PSDRV_EmptyDownloadList(PHYSDEV dev, BOOL write_undef)
416 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
417 DOWNLOAD *pdl, *old;
418 static const char undef[] = "/%s findfont 40 scalefont setfont /%s undefinefont\n";
419 char buf[sizeof(undef) + 200];
420 const char *default_font = physDev->pi->ppd->DefaultFont ?
421 physDev->pi->ppd->DefaultFont : "Courier";
423 if(physDev->font.fontloc == Download) {
424 physDev->font.set = FALSE;
425 physDev->font.fontinfo.Download = NULL;
428 pdl = physDev->downloaded_fonts;
429 physDev->downloaded_fonts = NULL;
430 while(pdl) {
431 if(write_undef) {
432 sprintf(buf, undef, default_font, pdl->ps_name);
433 PSDRV_WriteSpool(dev, buf, strlen(buf));
436 switch(pdl->type) {
437 case Type42:
438 T42_free(pdl->typeinfo.Type42);
439 break;
441 case Type1:
442 T1_free(pdl->typeinfo.Type1);
443 break;
445 default:
446 ERR("Type = %d\n", pdl->type);
447 assert(0);
450 HeapFree(GetProcessHeap(), 0, pdl->ps_name);
451 old = pdl;
452 pdl = pdl->next;
453 HeapFree(GetProcessHeap(), 0, old);
455 return TRUE;