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