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