oleaut32: Improved ICreateTypeInfo2_SetFuncAndParamNames implementation.
[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 <stdarg.h>
24 #include <stdio.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winnls.h"
30
31 #include "psdrv.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
35
36 #define MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
37           ( ( (DWORD)_x4 << 24 ) |     \
38             ( (DWORD)_x3 << 16 ) |     \
39             ( (DWORD)_x2 <<  8 ) |     \
40               (DWORD)_x1         )
41
42 #define GET_BE_WORD(ptr)  MAKEWORD( ((BYTE *)(ptr))[1], ((BYTE *)(ptr))[0] )
43 #define GET_BE_DWORD(ptr) ((DWORD)MAKELONG( GET_BE_WORD(&((WORD *)(ptr))[1]), \
44                                             GET_BE_WORD(&((WORD *)(ptr))[0]) ))
45
46 /****************************************************************************
47  *  get_download_name
48  */
49 static void get_download_name(PSDRV_PDEVICE *physDev, LPOUTLINETEXTMETRICA
50                               potm, char **str)
51 {
52     int len;
53     char *p;
54     DWORD size;
55
56     size = GetFontData(physDev->hdc, MS_MAKE_TAG('n','a','m','e'), 0, NULL, 0);
57     if(size != 0 && size != GDI_ERROR)
58     {
59         BYTE *name = HeapAlloc(GetProcessHeap(), 0, size);
60         if(name)
61         {
62             USHORT count, i;
63             BYTE *strings;
64             struct
65             {
66                 USHORT platform_id;
67                 USHORT encoding_id;
68                 USHORT language_id;
69                 USHORT name_id;
70                 USHORT length;
71                 USHORT offset;
72             } *name_record;
73
74             GetFontData(physDev->hdc, MS_MAKE_TAG('n','a','m','e'), 0, name, size);
75             count = GET_BE_WORD(name + 2);
76             strings = name + GET_BE_WORD(name + 4);
77             name_record = (typeof(name_record))(name + 6);
78             for(i = 0; i < count; i++, name_record++)
79             {
80                 name_record->platform_id = GET_BE_WORD(&name_record->platform_id);
81                 name_record->encoding_id = GET_BE_WORD(&name_record->encoding_id);
82                 name_record->language_id = GET_BE_WORD(&name_record->language_id);
83                 name_record->name_id     = GET_BE_WORD(&name_record->name_id);
84                 name_record->length      = GET_BE_WORD(&name_record->length);
85                 name_record->offset      = GET_BE_WORD(&name_record->offset);
86
87                 if(name_record->platform_id == 1 && name_record->encoding_id == 0 &&
88                    name_record->language_id == 0 && name_record->name_id == 6)
89                 {
90                     TRACE("Got Mac PS name %s\n", debugstr_an((char*)strings + name_record->offset, name_record->length));
91                     *str = HeapAlloc(GetProcessHeap(), 0, name_record->length + 1);
92                     memcpy(*str, strings + name_record->offset, name_record->length);
93                     *(*str + name_record->length) = '\0';
94                     HeapFree(GetProcessHeap(), 0, name);
95                     return;
96                 }
97                 if(name_record->platform_id == 3 && name_record->encoding_id == 1 &&
98                    name_record->language_id == 0x409 && name_record->name_id == 6)
99                 {
100                     WCHAR *unicode = HeapAlloc(GetProcessHeap(), 0, name_record->length + 2);
101                     DWORD len;
102                     int c;
103
104                     for(c = 0; c < name_record->length / 2; c++)
105                         unicode[c] = GET_BE_WORD(strings + name_record->offset + c * 2);
106                     unicode[c] = 0;
107                     TRACE("Got Windows PS name %s\n", debugstr_w(unicode));
108                     len = WideCharToMultiByte(1252, 0, unicode, -1, NULL, 0, NULL, NULL);
109                     *str = HeapAlloc(GetProcessHeap(), 0, len);
110                     WideCharToMultiByte(1252, 0, unicode, -1, *str, len, NULL, NULL);
111                     HeapFree(GetProcessHeap(), 0, unicode);
112                     HeapFree(GetProcessHeap(), 0, name);
113                     return;
114                 }
115             }
116             TRACE("Unable to find PostScript name\n");
117             HeapFree(GetProcessHeap(), 0, name);
118         }
119     }
120
121     len = strlen((char*)potm + (ptrdiff_t)potm->otmpFullName) + 1;
122     *str = HeapAlloc(GetProcessHeap(),0,len);
123     strcpy(*str, (char*)potm + (ptrdiff_t)potm->otmpFullName);
124
125     p = *str;
126     while((p = strchr(p, ' ')))
127         *p = '_';
128
129     return;
130 }
131
132 /****************************************************************************
133  *  is_font_downloaded
134  */
135 static DOWNLOAD *is_font_downloaded(PSDRV_PDEVICE *physDev, char *ps_name)
136 {
137     DOWNLOAD *pdl;
138
139     for(pdl = physDev->downloaded_fonts; pdl; pdl = pdl->next)
140         if(!strcmp(pdl->ps_name, ps_name))
141             break;
142     return pdl;
143 }
144
145 /****************************************************************************
146  *  is_room_for_font
147  */
148 static BOOL is_room_for_font(PSDRV_PDEVICE *physDev)
149 {
150     DOWNLOAD *pdl;
151     int count = 0;
152
153     /* FIXME: should consider vm usage of each font and available printer memory.
154        For now we allow up to two fonts to be downloaded at a time */
155     for(pdl = physDev->downloaded_fonts; pdl; pdl = pdl->next)
156         count++;
157
158     if(count > 1)
159         return FALSE;
160     return TRUE;
161 }
162
163 /****************************************************************************
164  *  get_bbox
165  *
166  * This retrieves the bounding box of the font in font units as well as
167  * the size of the emsquare.  To avoid having to worry about mapping mode and
168  * the font size we'll get the data directly from the TrueType HEAD table rather
169  * than using GetOutlineTextMetrics.
170  */
171 static BOOL get_bbox(PSDRV_PDEVICE *physDev, RECT *rc, UINT *emsize)
172 {
173     BYTE head[54]; /* the head table is 54 bytes long */
174
175     if(GetFontData(physDev->hdc, MS_MAKE_TAG('h','e','a','d'), 0, head,
176                    sizeof(head)) == GDI_ERROR) {
177         ERR("Can't retrieve head table\n");
178         return FALSE;
179     }
180     *emsize = GET_BE_WORD(head + 18); /* unitsPerEm */
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     return TRUE;
186 }
187
188 /****************************************************************************
189  *  PSDRV_SelectDownloadFont
190  *
191  *  Set up physDev->font for a downloadable font
192  *
193  */
194 BOOL PSDRV_SelectDownloadFont(PSDRV_PDEVICE *physDev)
195 {
196     char *ps_name;
197     LPOUTLINETEXTMETRICA potm;
198     DWORD len = GetOutlineTextMetricsA(physDev->hdc, 0, NULL);
199
200     if(!len) return FALSE;
201     potm = HeapAlloc(GetProcessHeap(), 0, len);
202     GetOutlineTextMetricsA(physDev->hdc, len, potm);
203     get_download_name(physDev, potm, &ps_name);
204
205     physDev->font.fontloc = Download;
206     physDev->font.fontinfo.Download = is_font_downloaded(physDev, ps_name);
207
208     physDev->font.size = abs(PSDRV_YWStoDS(physDev, /* ppem */
209                                        potm->otmTextMetrics.tmAscent +
210                                        potm->otmTextMetrics.tmDescent -
211                                        potm->otmTextMetrics.tmInternalLeading));
212     physDev->font.underlineThickness = potm->otmsUnderscoreSize;
213     physDev->font.underlinePosition = potm->otmsUnderscorePosition;
214     physDev->font.strikeoutThickness = potm->otmsStrikeoutSize;
215     physDev->font.strikeoutPosition = potm->otmsStrikeoutPosition;
216
217     HeapFree(GetProcessHeap(), 0, ps_name);
218     HeapFree(GetProcessHeap(), 0, potm);
219     return TRUE;
220 }
221
222 /****************************************************************************
223  *  PSDRV_WriteSetDownloadFont
224  *
225  *  Write setfont for download font.
226  *
227  */
228 BOOL PSDRV_WriteSetDownloadFont(PSDRV_PDEVICE *physDev)
229 {
230     char *ps_name;
231     LPOUTLINETEXTMETRICA potm;
232     DWORD len = GetOutlineTextMetricsA(physDev->hdc, 0, NULL);
233     DOWNLOAD *pdl;
234
235     assert(physDev->font.fontloc == Download);
236
237     potm = HeapAlloc(GetProcessHeap(), 0, len);
238     GetOutlineTextMetricsA(physDev->hdc, len, potm);
239
240     get_download_name(physDev, potm, &ps_name);
241
242     if(physDev->font.fontinfo.Download == NULL) {
243         RECT bbox;
244         UINT emsize;
245
246         if (!get_bbox(physDev, &bbox, &emsize)) {
247             HeapFree(GetProcessHeap(), 0, potm);
248             return FALSE;
249         }
250         if(!is_room_for_font(physDev))
251             PSDRV_EmptyDownloadList(physDev, TRUE);
252
253         pdl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pdl));
254         pdl->ps_name = HeapAlloc(GetProcessHeap(), 0, strlen(ps_name)+1);
255         strcpy(pdl->ps_name, ps_name);
256         pdl->next = NULL;
257
258         if(physDev->pi->ppd->TTRasterizer == RO_Type42) {
259             pdl->typeinfo.Type42 = T42_download_header(physDev, ps_name, &bbox, emsize);
260             pdl->type = Type42;
261         }
262         if(pdl->typeinfo.Type42 == NULL) {
263             pdl->typeinfo.Type1 = T1_download_header(physDev, ps_name, &bbox, emsize);
264             pdl->type = Type1;
265         }
266         pdl->next = physDev->downloaded_fonts;
267         physDev->downloaded_fonts = pdl;
268         physDev->font.fontinfo.Download = pdl;
269
270         if(pdl->type == Type42) {
271             char g_name[MAX_G_NAME + 1];
272             get_glyph_name(physDev->hdc, 0, g_name);
273             T42_download_glyph(physDev, pdl, 0, g_name);
274         }
275     }
276
277
278     PSDRV_WriteSetFont(physDev, ps_name, physDev->font.size,
279                        physDev->font.escapement);
280
281     HeapFree(GetProcessHeap(), 0, ps_name);
282     HeapFree(GetProcessHeap(), 0, potm);
283     return TRUE;
284 }
285
286 void get_glyph_name(HDC hdc, WORD index, char *name)
287 {
288   /* FIXME */
289     sprintf(name, "g%04x", index);
290     return;
291 }
292
293 /****************************************************************************
294  *  PSDRV_WriteDownloadGlyphShow
295  *
296  *  Download and write out a number of glyphs
297  *
298  */
299 BOOL PSDRV_WriteDownloadGlyphShow(PSDRV_PDEVICE *physDev, WORD *glyphs,
300                                   UINT count)
301 {
302     UINT i;
303     char g_name[MAX_G_NAME + 1];
304     assert(physDev->font.fontloc == Download);
305
306     switch(physDev->font.fontinfo.Download->type) {
307     case Type42:
308     for(i = 0; i < count; i++) {
309         get_glyph_name(physDev->hdc, glyphs[i], g_name);
310         T42_download_glyph(physDev, physDev->font.fontinfo.Download,
311                            glyphs[i], g_name);
312         PSDRV_WriteGlyphShow(physDev, g_name);
313     }
314     break;
315
316     case Type1:
317     for(i = 0; i < count; i++) {
318         get_glyph_name(physDev->hdc, glyphs[i], g_name);
319         T1_download_glyph(physDev, physDev->font.fontinfo.Download,
320                           glyphs[i], g_name);
321         PSDRV_WriteGlyphShow(physDev, g_name);
322     }
323     break;
324
325     default:
326         ERR("Type = %d\n", physDev->font.fontinfo.Download->type);
327         assert(0);
328     }
329     return TRUE;
330 }
331
332 /****************************************************************************
333  *  PSDRV_EmptyDownloadList
334  *
335  *  Clear the list of downloaded fonts
336  *
337  */
338 BOOL PSDRV_EmptyDownloadList(PSDRV_PDEVICE *physDev, BOOL write_undef)
339 {
340     DOWNLOAD *pdl, *old;
341     static const char undef[] = "/%s findfont 40 scalefont setfont /%s undefinefont\n";
342     char buf[sizeof(undef) + 200];
343     const char *default_font = physDev->pi->ppd->DefaultFont ?
344         physDev->pi->ppd->DefaultFont : "Courier";
345
346     if(physDev->font.fontloc == Download) {
347         physDev->font.set = FALSE;
348         physDev->font.fontinfo.Download = NULL;
349     }
350
351     pdl = physDev->downloaded_fonts;
352     physDev->downloaded_fonts = NULL;
353     while(pdl) {
354         if(write_undef) {
355             sprintf(buf, undef, default_font, pdl->ps_name);
356             PSDRV_WriteSpool(physDev, buf, strlen(buf));
357         }
358
359         switch(pdl->type) {
360         case Type42:
361             T42_free(pdl->typeinfo.Type42);
362             break;
363
364         case Type1:
365             T1_free(pdl->typeinfo.Type1);
366             break;
367
368         default:
369             ERR("Type = %d\n", pdl->type);
370             assert(0);
371         }
372
373         HeapFree(GetProcessHeap(), 0, pdl->ps_name);
374         old = pdl;
375         pdl = pdl->next;
376         HeapFree(GetProcessHeap(), 0, old);
377     }
378     return TRUE;
379 }