ddraw: Avoid LPD3DSTATE.
[wine] / dlls / wineps.drv / download.c
1 /*
2  *      PostScript driver downloadable font functions
3  *
4  *      Copyright 2002  Huw D M Davies for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20 #include <string.h>
21 #include <stdlib.h>
22 #include <assert.h>
23 #include <math.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winnls.h"
31
32 #include "psdrv.h"
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
36
37 #define MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
38           ( ( (DWORD)_x4 << 24 ) |     \
39             ( (DWORD)_x3 << 16 ) |     \
40             ( (DWORD)_x2 <<  8 ) |     \
41               (DWORD)_x1         )
42
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]) ))
46
47 /****************************************************************************
48  *  get_download_name
49  */
50 static void get_download_name(PHYSDEV dev, LPOUTLINETEXTMETRICA potm, char **str)
51 {
52     static const char reserved_chars[] = " %/(){}[]<>\n\r\t\b\f";
53     int len;
54     char *p;
55     DWORD size;
56
57     size = GetFontData(dev->hdc, MS_MAKE_TAG('n','a','m','e'), 0, NULL, 0);
58     if(size != 0 && size != GDI_ERROR)
59     {
60         BYTE *name = HeapAlloc(GetProcessHeap(), 0, size);
61         if(name)
62         {
63             USHORT count, i;
64             BYTE *strings;
65             struct
66             {
67                 USHORT platform_id;
68                 USHORT encoding_id;
69                 USHORT language_id;
70                 USHORT name_id;
71                 USHORT length;
72                 USHORT offset;
73             } *name_record;
74
75             GetFontData(dev->hdc, MS_MAKE_TAG('n','a','m','e'), 0, name, size);
76             count = GET_BE_WORD(name + 2);
77             strings = name + GET_BE_WORD(name + 4);
78             name_record = (typeof(name_record))(name + 6);
79             for(i = 0; i < count; i++, name_record++)
80             {
81                 name_record->platform_id = GET_BE_WORD(&name_record->platform_id);
82                 name_record->encoding_id = GET_BE_WORD(&name_record->encoding_id);
83                 name_record->language_id = GET_BE_WORD(&name_record->language_id);
84                 name_record->name_id     = GET_BE_WORD(&name_record->name_id);
85                 name_record->length      = GET_BE_WORD(&name_record->length);
86                 name_record->offset      = GET_BE_WORD(&name_record->offset);
87
88                 if(name_record->platform_id == 1 && name_record->encoding_id == 0 &&
89                    name_record->language_id == 0 && name_record->name_id == 6)
90                 {
91                     TRACE("Got Mac PS name %s\n", debugstr_an((char*)strings + name_record->offset, name_record->length));
92                     *str = HeapAlloc(GetProcessHeap(), 0, name_record->length + 1);
93                     memcpy(*str, strings + name_record->offset, name_record->length);
94                     *(*str + name_record->length) = '\0';
95                     HeapFree(GetProcessHeap(), 0, name);
96                     goto done;
97                 }
98                 if(name_record->platform_id == 3 && name_record->encoding_id == 1 &&
99                    name_record->language_id == 0x409 && name_record->name_id == 6)
100                 {
101                     WCHAR *unicode = HeapAlloc(GetProcessHeap(), 0, name_record->length + 2);
102                     DWORD len;
103                     int c;
104
105                     for(c = 0; c < name_record->length / 2; c++)
106                         unicode[c] = GET_BE_WORD(strings + name_record->offset + c * 2);
107                     unicode[c] = 0;
108                     TRACE("Got Windows PS name %s\n", debugstr_w(unicode));
109                     len = WideCharToMultiByte(1252, 0, unicode, -1, NULL, 0, NULL, NULL);
110                     *str = HeapAlloc(GetProcessHeap(), 0, len);
111                     WideCharToMultiByte(1252, 0, unicode, -1, *str, len, NULL, NULL);
112                     HeapFree(GetProcessHeap(), 0, unicode);
113                     HeapFree(GetProcessHeap(), 0, name);
114                     goto done;
115                 }
116             }
117             TRACE("Unable to find PostScript name\n");
118             HeapFree(GetProcessHeap(), 0, name);
119         }
120     }
121
122     len = strlen((char*)potm + (ptrdiff_t)potm->otmpFaceName) + 1;
123     *str = HeapAlloc(GetProcessHeap(),0,len);
124     strcpy(*str, (char*)potm + (ptrdiff_t)potm->otmpFaceName);
125
126 done:
127     for (p = *str; *p; p++) if (strchr( reserved_chars, *p )) *p = '_';
128 }
129
130 /****************************************************************************
131  *  is_font_downloaded
132  */
133 static DOWNLOAD *is_font_downloaded(PSDRV_PDEVICE *physDev, char *ps_name)
134 {
135     DOWNLOAD *pdl;
136
137     for(pdl = physDev->downloaded_fonts; pdl; pdl = pdl->next)
138         if(!strcmp(pdl->ps_name, ps_name))
139             break;
140     return pdl;
141 }
142
143 /****************************************************************************
144  *  is_room_for_font
145  */
146 static BOOL is_room_for_font(PSDRV_PDEVICE *physDev)
147 {
148     DOWNLOAD *pdl;
149     int count = 0;
150
151     /* FIXME: should consider vm usage of each font and available printer memory.
152        For now we allow up to two fonts to be downloaded at a time */
153     for(pdl = physDev->downloaded_fonts; pdl; pdl = pdl->next)
154         count++;
155
156     if(count > 1)
157         return FALSE;
158     return TRUE;
159 }
160
161 /****************************************************************************
162  *  get_bbox
163  *
164  * This retrieves the bounding box of the font in font units as well as
165  * the size of the emsquare.  To avoid having to worry about mapping mode and
166  * the font size we'll get the data directly from the TrueType HEAD table rather
167  * than using GetOutlineTextMetrics.
168  */
169 static BOOL get_bbox(HDC hdc, RECT *rc, UINT *emsize)
170 {
171     BYTE head[54]; /* the head table is 54 bytes long */
172
173     if(GetFontData(hdc, MS_MAKE_TAG('h','e','a','d'), 0, head, sizeof(head)) == GDI_ERROR)
174     {
175         ERR("Can't retrieve head table\n");
176         return FALSE;
177     }
178     *emsize = GET_BE_WORD(head + 18); /* unitsPerEm */
179     if(rc)
180     {
181         rc->left   = (signed short)GET_BE_WORD(head + 36); /* xMin */
182         rc->bottom = (signed short)GET_BE_WORD(head + 38); /* yMin */
183         rc->right  = (signed short)GET_BE_WORD(head + 40); /* xMax */
184         rc->top    = (signed short)GET_BE_WORD(head + 42); /* yMax */
185     }
186     return TRUE;
187 }
188
189 /****************************************************************************
190  *  PSDRV_SelectDownloadFont
191  *
192  *  Set up physDev->font for a downloadable font
193  *
194  */
195 BOOL PSDRV_SelectDownloadFont(PHYSDEV dev)
196 {
197     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
198
199     physDev->font.fontloc = Download;
200     physDev->font.fontinfo.Download = NULL;
201
202     return TRUE;
203 }
204
205 static UINT calc_ppem_for_height(HDC hdc, LONG height)
206 {
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;
210     UINT emsize;
211
212     if(height < 0) return -height;
213
214     if(GetFontData(hdc, MS_MAKE_TAG('O','S','/','2'), 0, os2, sizeof(os2)) == sizeof(os2))
215     {
216         ascent  = GET_BE_WORD(os2 + 74); /* usWinAscent */
217         descent = GET_BE_WORD(os2 + 76); /* usWinDescent */
218     }
219
220     if(ascent + descent == 0)
221     {
222         if(GetFontData(hdc, MS_MAKE_TAG('h','h','e','a'), 0, hhea, sizeof(hhea)) == sizeof(hhea))
223         {
224             ascent  =  (signed short)GET_BE_WORD(hhea + 4); /* Ascender */
225             descent = -(signed short)GET_BE_WORD(hhea + 6); /* Descender */
226         }
227     }
228
229     if(ascent + descent == 0) return height;
230
231     get_bbox(hdc, NULL, &emsize);
232
233     return MulDiv(emsize, height, ascent + descent);
234 }
235
236 static inline float ps_round(float f)
237 {
238     return (f > 0) ? (f + 0.5) : (f - 0.5);
239 }
240
241 static BOOL is_fake_italic( HDC hdc )
242 {
243     TEXTMETRICW tm;
244     BYTE head[54]; /* the head table is 54 bytes long */
245     WORD mac_style;
246
247     GetTextMetricsW( hdc, &tm );
248     if (!tm.tmItalic) return FALSE;
249
250     if (GetFontData( hdc, MS_MAKE_TAG('h','e','a','d'), 0, head, sizeof(head) ) == GDI_ERROR)
251         return FALSE;
252
253     mac_style = GET_BE_WORD( head + 44 );
254     TRACE( "mac style %04x\n", mac_style );
255     return !(mac_style & 2);
256 }
257
258 /****************************************************************************
259  *  PSDRV_WriteSetDownloadFont
260  *
261  *  Write setfont for download font.
262  *
263  */
264 BOOL PSDRV_WriteSetDownloadFont(PHYSDEV dev)
265 {
266     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
267     char *ps_name;
268     LPOUTLINETEXTMETRICA potm;
269     DWORD len = GetOutlineTextMetricsA(dev->hdc, 0, NULL);
270     DOWNLOAD *pdl;
271     LOGFONTW lf;
272     UINT ppem;
273     XFORM xform;
274
275     assert(physDev->font.fontloc == Download);
276
277     if (!GetObjectW( GetCurrentObject(dev->hdc, OBJ_FONT), sizeof(lf), &lf ))
278         return FALSE;
279
280     potm = HeapAlloc(GetProcessHeap(), 0, len);
281     if (!potm)
282         return FALSE;
283
284     GetOutlineTextMetricsA(dev->hdc, len, potm);
285
286     get_download_name(dev, potm, &ps_name);
287     physDev->font.fontinfo.Download = is_font_downloaded(physDev, ps_name);
288
289     ppem = calc_ppem_for_height(dev->hdc, lf.lfHeight);
290
291     /* Retrieve the world -> device transform */
292     GetTransform(dev->hdc, 0x204, &xform);
293
294     if(GetGraphicsMode(dev->hdc) == GM_COMPATIBLE)
295     {
296         if (xform.eM22 < 0) physDev->font.escapement = -physDev->font.escapement;
297         xform.eM11 = xform.eM22 = fabs(xform.eM22);
298         xform.eM21 = xform.eM12 = 0;
299     }
300
301     physDev->font.size.xx = ps_round(ppem * xform.eM11);
302     physDev->font.size.xy = ps_round(ppem * xform.eM12);
303     physDev->font.size.yx = -ps_round(ppem * xform.eM21);
304     physDev->font.size.yy = -ps_round(ppem * xform.eM22);
305
306     physDev->font.underlineThickness = potm->otmsUnderscoreSize;
307     physDev->font.underlinePosition = potm->otmsUnderscorePosition;
308     physDev->font.strikeoutThickness = potm->otmsStrikeoutSize;
309     physDev->font.strikeoutPosition = potm->otmsStrikeoutPosition;
310
311     if(physDev->font.fontinfo.Download == NULL) {
312         RECT bbox;
313         UINT emsize;
314
315         if (!get_bbox(dev->hdc, &bbox, &emsize)) {
316             HeapFree(GetProcessHeap(), 0, ps_name);
317             HeapFree(GetProcessHeap(), 0, potm);
318             return FALSE;
319         }
320         if(!is_room_for_font(physDev))
321             PSDRV_EmptyDownloadList(dev, TRUE);
322
323         pdl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pdl));
324         pdl->ps_name = HeapAlloc(GetProcessHeap(), 0, strlen(ps_name)+1);
325         strcpy(pdl->ps_name, ps_name);
326         pdl->next = NULL;
327
328         if(physDev->pi->ppd->TTRasterizer == RO_Type42) {
329             pdl->typeinfo.Type42 = T42_download_header(dev, ps_name, &bbox, emsize);
330             pdl->type = Type42;
331         }
332         if(pdl->typeinfo.Type42 == NULL) {
333             pdl->typeinfo.Type1 = T1_download_header(dev, ps_name, &bbox, emsize);
334             pdl->type = Type1;
335         }
336         pdl->next = physDev->downloaded_fonts;
337         physDev->downloaded_fonts = pdl;
338         physDev->font.fontinfo.Download = pdl;
339
340         if(pdl->type == Type42) {
341             char g_name[MAX_G_NAME + 1];
342             get_glyph_name(dev->hdc, 0, g_name);
343             T42_download_glyph(dev, pdl, 0, g_name);
344         }
345     }
346
347
348     PSDRV_WriteSetFont(dev, ps_name, physDev->font.size, physDev->font.escapement,
349                        is_fake_italic( dev->hdc ));
350
351     HeapFree(GetProcessHeap(), 0, ps_name);
352     HeapFree(GetProcessHeap(), 0, potm);
353     return TRUE;
354 }
355
356 void get_glyph_name(HDC hdc, WORD index, char *name)
357 {
358   /* FIXME */
359     sprintf(name, "g%04x", index);
360     return;
361 }
362
363 /****************************************************************************
364  *  PSDRV_WriteDownloadGlyphShow
365  *
366  *  Download and write out a number of glyphs
367  *
368  */
369 BOOL PSDRV_WriteDownloadGlyphShow(PHYSDEV dev, const WORD *glyphs,
370                                   UINT count)
371 {
372     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
373     UINT i;
374     char g_name[MAX_G_NAME + 1];
375     assert(physDev->font.fontloc == Download);
376
377     switch(physDev->font.fontinfo.Download->type) {
378     case Type42:
379     for(i = 0; i < count; i++) {
380         get_glyph_name(dev->hdc, glyphs[i], g_name);
381         T42_download_glyph(dev, physDev->font.fontinfo.Download, glyphs[i], g_name);
382         PSDRV_WriteGlyphShow(dev, g_name);
383     }
384     break;
385
386     case Type1:
387     for(i = 0; i < count; i++) {
388         get_glyph_name(dev->hdc, glyphs[i], g_name);
389         T1_download_glyph(dev, physDev->font.fontinfo.Download, glyphs[i], g_name);
390         PSDRV_WriteGlyphShow(dev, g_name);
391     }
392     break;
393
394     default:
395         ERR("Type = %d\n", physDev->font.fontinfo.Download->type);
396         assert(0);
397     }
398     return TRUE;
399 }
400
401 /****************************************************************************
402  *  PSDRV_EmptyDownloadList
403  *
404  *  Clear the list of downloaded fonts
405  *
406  */
407 BOOL PSDRV_EmptyDownloadList(PHYSDEV dev, BOOL write_undef)
408 {
409     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
410     DOWNLOAD *pdl, *old;
411     static const char undef[] = "/%s findfont 40 scalefont setfont /%s undefinefont\n";
412     char buf[sizeof(undef) + 200];
413     const char *default_font = physDev->pi->ppd->DefaultFont ?
414         physDev->pi->ppd->DefaultFont : "Courier";
415
416     if(physDev->font.fontloc == Download) {
417         physDev->font.set = FALSE;
418         physDev->font.fontinfo.Download = NULL;
419     }
420
421     pdl = physDev->downloaded_fonts;
422     physDev->downloaded_fonts = NULL;
423     while(pdl) {
424         if(write_undef) {
425             sprintf(buf, undef, default_font, pdl->ps_name);
426             PSDRV_WriteSpool(dev, buf, strlen(buf));
427         }
428
429         switch(pdl->type) {
430         case Type42:
431             T42_free(pdl->typeinfo.Type42);
432             break;
433
434         case Type1:
435             T1_free(pdl->typeinfo.Type1);
436             break;
437
438         default:
439             ERR("Type = %d\n", pdl->type);
440             assert(0);
441         }
442
443         HeapFree(GetProcessHeap(), 0, pdl->ps_name);
444         old = pdl;
445         pdl = pdl->next;
446         HeapFree(GetProcessHeap(), 0, old);
447     }
448     return TRUE;
449 }