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