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