wined3d: Implement support for projective textures in ps 2.0 and later.
[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 "winerror.h"
29 #include "wingdi.h"
30 #include "winspool.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(PSDRV_PDEVICE *physDev, LPOUTLINETEXTMETRICA
51                               potm, char **str)
52 {
53     int len;
54     char *p;
55     DWORD size;
56
57     size = GetFontData(physDev->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(physDev->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                     return;
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                     return;
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->otmpFullName) + 1;
123     *str = HeapAlloc(GetProcessHeap(),0,len);
124     strcpy(*str, (char*)potm + (ptrdiff_t)potm->otmpFullName);
125
126     p = *str;
127     while((p = strchr(p, ' ')))
128         *p = '_';
129
130     return;
131 }
132
133 /****************************************************************************
134  *  is_font_downloaded
135  */
136 static DOWNLOAD *is_font_downloaded(PSDRV_PDEVICE *physDev, char *ps_name)
137 {
138     DOWNLOAD *pdl;
139
140     for(pdl = physDev->downloaded_fonts; pdl; pdl = pdl->next)
141         if(!strcmp(pdl->ps_name, ps_name))
142             break;
143     return pdl;
144 }
145
146 /****************************************************************************
147  *  is_room_for_font
148  */
149 static BOOL is_room_for_font(PSDRV_PDEVICE *physDev)
150 {
151     DOWNLOAD *pdl;
152     int count = 0;
153
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++;
158
159     if(count > 1)
160         return FALSE;
161     return TRUE;
162 }
163
164 /****************************************************************************
165  *  get_bbox
166  *
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.
171  */
172 static BOOL get_bbox(PSDRV_PDEVICE *physDev, RECT *rc, UINT *emsize)
173 {
174     BYTE head[54]; /* the head table is 54 bytes long */
175
176     if(GetFontData(physDev->hdc, MS_MAKE_TAG('h','e','a','d'), 0, head,
177                    sizeof(head)) == GDI_ERROR) {
178         ERR("Can't retrieve head table\n");
179         return FALSE;
180     }
181     *emsize = GET_BE_WORD(head + 18); /* unitsPerEm */
182     rc->left = (signed short)GET_BE_WORD(head + 36); /* xMin */
183     rc->bottom = (signed short)GET_BE_WORD(head + 38); /* yMin */
184     rc->right = (signed short)GET_BE_WORD(head + 40); /* xMax */
185     rc->top = (signed short)GET_BE_WORD(head + 42); /* yMax */
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(PSDRV_PDEVICE *physDev)
196 {
197     char *ps_name;
198     LPOUTLINETEXTMETRICA potm;
199     DWORD len = GetOutlineTextMetricsA(physDev->hdc, 0, NULL);
200
201     if(!len) return FALSE;
202     potm = HeapAlloc(GetProcessHeap(), 0, len);
203     GetOutlineTextMetricsA(physDev->hdc, len, potm);
204     get_download_name(physDev, potm, &ps_name);
205
206     physDev->font.fontloc = Download;
207     physDev->font.fontinfo.Download = is_font_downloaded(physDev, ps_name);
208
209     physDev->font.size = abs(PSDRV_YWStoDS(physDev, /* ppem */
210                                        potm->otmTextMetrics.tmAscent +
211                                        potm->otmTextMetrics.tmDescent -
212                                        potm->otmTextMetrics.tmInternalLeading));
213     physDev->font.underlineThickness = potm->otmsUnderscoreSize;
214     physDev->font.underlinePosition = potm->otmsUnderscorePosition;
215     physDev->font.strikeoutThickness = potm->otmsStrikeoutSize;
216     physDev->font.strikeoutPosition = potm->otmsStrikeoutPosition;
217
218     HeapFree(GetProcessHeap(), 0, ps_name);
219     HeapFree(GetProcessHeap(), 0, potm);
220     return TRUE;
221 }
222
223 /****************************************************************************
224  *  PSDRV_WriteSetDownloadFont
225  *
226  *  Write setfont for download font.
227  *
228  */
229 BOOL PSDRV_WriteSetDownloadFont(PSDRV_PDEVICE *physDev)
230 {
231     char *ps_name;
232     LPOUTLINETEXTMETRICA potm;
233     DWORD len = GetOutlineTextMetricsA(physDev->hdc, 0, NULL);
234     DOWNLOAD *pdl;
235
236     assert(physDev->font.fontloc == Download);
237
238     potm = HeapAlloc(GetProcessHeap(), 0, len);
239     GetOutlineTextMetricsA(physDev->hdc, len, potm);
240
241     get_download_name(physDev, potm, &ps_name);
242
243     if(physDev->font.fontinfo.Download == NULL) {
244         RECT bbox;
245         UINT emsize;
246
247         pdl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pdl));
248         pdl->ps_name = HeapAlloc(GetProcessHeap(), 0, strlen(ps_name)+1);
249         strcpy(pdl->ps_name, ps_name);
250         pdl->next = NULL;
251
252         if (!get_bbox(physDev, &bbox, &emsize))
253                 return FALSE;
254         if(!is_room_for_font(physDev))
255             PSDRV_EmptyDownloadList(physDev, TRUE);
256
257         if(physDev->pi->ppd->TTRasterizer == RO_Type42) {
258             pdl->typeinfo.Type42 = T42_download_header(physDev, ps_name, &bbox, emsize);
259             pdl->type = Type42;
260         }
261         if(pdl->typeinfo.Type42 == NULL) {
262             pdl->typeinfo.Type1 = T1_download_header(physDev, ps_name, &bbox, emsize);
263             pdl->type = Type1;
264         }
265         pdl->next = physDev->downloaded_fonts;
266         physDev->downloaded_fonts = pdl;
267         physDev->font.fontinfo.Download = pdl;
268
269         if(pdl->type == Type42) {
270             char g_name[MAX_G_NAME + 1];
271             get_glyph_name(physDev->hdc, 0, g_name);
272             T42_download_glyph(physDev, pdl, 0, g_name);
273         }
274     }
275
276
277     PSDRV_WriteSetFont(physDev, ps_name, physDev->font.size,
278                        physDev->font.escapement);
279
280     HeapFree(GetProcessHeap(), 0, ps_name);
281     HeapFree(GetProcessHeap(), 0, potm);
282     return TRUE;
283 }
284
285 void get_glyph_name(HDC hdc, WORD index, char *name)
286 {
287   /* FIXME */
288     sprintf(name, "g%04x", index);
289     return;
290 }
291
292 /****************************************************************************
293  *  PSDRV_WriteDownloadGlyphShow
294  *
295  *  Download and write out a number of glyphs
296  *
297  */
298 BOOL PSDRV_WriteDownloadGlyphShow(PSDRV_PDEVICE *physDev, WORD *glyphs,
299                                   UINT count)
300 {
301     UINT i;
302     char g_name[MAX_G_NAME + 1];
303     assert(physDev->font.fontloc == Download);
304
305     switch(physDev->font.fontinfo.Download->type) {
306     case Type42:
307     for(i = 0; i < count; i++) {
308         get_glyph_name(physDev->hdc, glyphs[i], g_name);
309         T42_download_glyph(physDev, physDev->font.fontinfo.Download,
310                            glyphs[i], g_name);
311         PSDRV_WriteGlyphShow(physDev, g_name);
312     }
313     break;
314
315     case Type1:
316     for(i = 0; i < count; i++) {
317         get_glyph_name(physDev->hdc, glyphs[i], g_name);
318         T1_download_glyph(physDev, physDev->font.fontinfo.Download,
319                           glyphs[i], g_name);
320         PSDRV_WriteGlyphShow(physDev, g_name);
321     }
322     break;
323
324     default:
325         ERR("Type = %d\n", physDev->font.fontinfo.Download->type);
326         assert(0);
327     }
328     return TRUE;
329 }
330
331 /****************************************************************************
332  *  PSDRV_EmptyDownloadList
333  *
334  *  Clear the list of downloaded fonts
335  *
336  */
337 BOOL PSDRV_EmptyDownloadList(PSDRV_PDEVICE *physDev, BOOL write_undef)
338 {
339     DOWNLOAD *pdl, *old;
340     static const char undef[] = "/%s findfont 40 scalefont setfont /%s undefinefont\n";
341     char buf[sizeof(undef) + 200];
342     const char *default_font = physDev->pi->ppd->DefaultFont ?
343         physDev->pi->ppd->DefaultFont : "Courier";
344
345     if(physDev->font.fontloc == Download) {
346         physDev->font.set = FALSE;
347         physDev->font.fontinfo.Download = NULL;
348     }
349
350     pdl = physDev->downloaded_fonts;
351     physDev->downloaded_fonts = NULL;
352     while(pdl) {
353         if(write_undef) {
354             sprintf(buf, undef, default_font, pdl->ps_name);
355             PSDRV_WriteSpool(physDev, buf, strlen(buf));
356         }
357
358         switch(pdl->type) {
359         case Type42:
360             T42_free(pdl->typeinfo.Type42);
361             break;
362
363         case Type1:
364             T1_free(pdl->typeinfo.Type1);
365             break;
366
367         default:
368             ERR("Type = %d\n", pdl->type);
369             assert(0);
370         }
371
372         HeapFree(GetProcessHeap(), 0, pdl->ps_name);
373         old = pdl;
374         pdl = pdl->next;
375         HeapFree(GetProcessHeap(), 0, old);
376     }
377     return TRUE;
378 }