_read(): In _O_TEXT mode make Readfile calls in chunks as big as
[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
39 /****************************************************************************
40  *  get_download_name
41  */
42 static void get_download_name(PSDRV_PDEVICE *physDev, LPOUTLINETEXTMETRICA
43                               potm, char **str)
44 {
45     int len;
46     char *p;
47     len = strlen((char*)potm + (ptrdiff_t)potm->otmpFullName) + 1;
48     *str = HeapAlloc(GetProcessHeap(),0,len);
49     strcpy(*str, (char*)potm + (ptrdiff_t)potm->otmpFullName);
50
51     p = *str;
52     while((p = strchr(p, ' ')))
53         *p = '_';
54
55     return;
56 }
57
58 /****************************************************************************
59  *  is_font_downloaded
60  */
61 static DOWNLOAD *is_font_downloaded(PSDRV_PDEVICE *physDev, char *ps_name)
62 {
63     DOWNLOAD *pdl;
64
65     for(pdl = physDev->downloaded_fonts; pdl; pdl = pdl->next)
66         if(!strcmp(pdl->ps_name, ps_name))
67             break;
68     return pdl;
69 }
70
71 /****************************************************************************
72  *  is_room_for_font
73  */
74 static BOOL is_room_for_font(PSDRV_PDEVICE *physDev)
75 {
76     DOWNLOAD *pdl;
77     int count = 0;
78
79     /* FIXME: should consider vm usage of each font and available printer memory.
80        For now we allow upto two fonts to be downloaded at a time */
81     for(pdl = physDev->downloaded_fonts; pdl; pdl = pdl->next)
82         count++;
83
84     if(count > 1)
85         return FALSE;
86     return TRUE;
87 }
88
89 /****************************************************************************
90  *  PSDRV_SelectDownloadFont
91  *
92  *  Set up physDev->font for a downloadable font
93  *
94  */
95 BOOL PSDRV_SelectDownloadFont(PSDRV_PDEVICE *physDev)
96 {
97     char *ps_name;
98     LPOUTLINETEXTMETRICA potm;
99     DWORD len = GetOutlineTextMetricsA(physDev->hdc, 0, NULL);
100
101     potm = HeapAlloc(GetProcessHeap(), 0, len);
102     GetOutlineTextMetricsA(physDev->hdc, len, potm);
103     get_download_name(physDev, potm, &ps_name);
104
105     physDev->font.fontloc = Download;
106     physDev->font.fontinfo.Download = is_font_downloaded(physDev, ps_name);
107
108     physDev->font.size = INTERNAL_YWSTODS(physDev->dc, /* ppem */
109                                           potm->otmTextMetrics.tmAscent +
110                                           potm->otmTextMetrics.tmDescent -
111                                           potm->otmTextMetrics.tmInternalLeading);
112     physDev->font.underlineThickness = potm->otmsUnderscoreSize;
113     physDev->font.underlinePosition = potm->otmsUnderscorePosition;
114     physDev->font.strikeoutThickness = potm->otmsStrikeoutSize;
115     physDev->font.strikeoutPosition = potm->otmsStrikeoutPosition;
116
117     HeapFree(GetProcessHeap(), 0, ps_name);
118     HeapFree(GetProcessHeap(), 0, potm);
119     return TRUE;
120 }
121
122 /****************************************************************************
123  *  PSDRV_WriteSetDownloadFont
124  *
125  *  Write setfont for download font.
126  *
127  */
128 BOOL PSDRV_WriteSetDownloadFont(PSDRV_PDEVICE *physDev)
129 {
130     char *ps_name;
131     LPOUTLINETEXTMETRICA potm;
132     DWORD len = GetOutlineTextMetricsA(physDev->hdc, 0, NULL);
133     DOWNLOAD *pdl;
134
135     assert(physDev->font.fontloc == Download);
136
137     potm = HeapAlloc(GetProcessHeap(), 0, len);
138     GetOutlineTextMetricsA(physDev->hdc, len, potm);
139
140     get_download_name(physDev, potm, &ps_name);
141
142     if(physDev->font.fontinfo.Download == NULL) {
143         pdl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pdl));
144         pdl->ps_name = HeapAlloc(GetProcessHeap(), 0, strlen(ps_name)+1);
145         strcpy(pdl->ps_name, ps_name);
146         pdl->next = NULL;
147
148         if(!is_room_for_font(physDev))
149             PSDRV_EmptyDownloadList(physDev, TRUE);
150
151         if(physDev->pi->ppd->TTRasterizer == RO_Type42) {
152             pdl->typeinfo.Type42 = T42_download_header(physDev, potm,
153                                                        ps_name);
154             pdl->type = Type42;
155         }
156         if(pdl->typeinfo.Type42 == NULL) {
157             pdl->typeinfo.Type1 = T1_download_header(physDev, potm, ps_name);
158             pdl->type = Type1;
159         }
160         pdl->next = physDev->downloaded_fonts;
161         physDev->downloaded_fonts = pdl;
162         physDev->font.fontinfo.Download = pdl;
163
164         if(pdl->type == Type42) {
165             char g_name[MAX_G_NAME + 1];
166             get_glyph_name(physDev->hdc, 0, g_name);
167             T42_download_glyph(physDev, pdl, 0, g_name);
168         }
169     }
170
171
172     PSDRV_WriteSetFont(physDev, ps_name, physDev->font.size,
173                        physDev->font.escapement);
174
175     HeapFree(GetProcessHeap(), 0, ps_name);
176     HeapFree(GetProcessHeap(), 0, potm);
177     return TRUE;
178 }
179
180 void get_glyph_name(HDC hdc, WORD index, char *name)
181 {
182   /* FIXME */
183     sprintf(name, "g%04x", index);
184     return;
185 }
186
187 /****************************************************************************
188  *  PSDRV_WriteDownloadGlyphShow
189  *
190  *  Download and write out a number of glyphs
191  *
192  */
193 BOOL PSDRV_WriteDownloadGlyphShow(PSDRV_PDEVICE *physDev, WORD *glyphs,
194                                   UINT count)
195 {
196     UINT i;
197     char g_name[MAX_G_NAME + 1];
198     assert(physDev->font.fontloc == Download);
199
200     switch(physDev->font.fontinfo.Download->type) {
201     case Type42:
202     for(i = 0; i < count; i++) {
203         get_glyph_name(physDev->hdc, glyphs[i], g_name);
204         T42_download_glyph(physDev, physDev->font.fontinfo.Download,
205                            glyphs[i], g_name);
206         PSDRV_WriteGlyphShow(physDev, g_name);
207     }
208     break;
209
210     case Type1:
211     for(i = 0; i < count; i++) {
212         get_glyph_name(physDev->hdc, glyphs[i], g_name);
213         T1_download_glyph(physDev, physDev->font.fontinfo.Download,
214                           glyphs[i], g_name);
215         PSDRV_WriteGlyphShow(physDev, g_name);
216     }
217     break;
218
219     default:
220         ERR("Type = %d\n", physDev->font.fontinfo.Download->type);
221         assert(0);
222     }
223     return TRUE;
224 }
225
226 /****************************************************************************
227  *  PSDRV_EmptyDownloadList
228  *
229  *  Clear the list of downloaded fonts
230  *
231  */
232 BOOL PSDRV_EmptyDownloadList(PSDRV_PDEVICE *physDev, BOOL write_undef)
233 {
234     DOWNLOAD *pdl, *old;
235     char undef[] = "/%s findfont 40 scalefont setfont /%s undefinefont\n";
236     char buf[sizeof(undef) + 200];
237     char *default_font = physDev->pi->ppd->DefaultFont ?
238         physDev->pi->ppd->DefaultFont : "Courier";
239
240     if(physDev->font.fontloc == Download) {
241         physDev->font.set = FALSE;
242         physDev->font.fontinfo.Download = NULL;
243     }
244
245     pdl = physDev->downloaded_fonts;
246     physDev->downloaded_fonts = NULL;
247     while(pdl) {
248         if(write_undef) {
249             sprintf(buf, undef, default_font, pdl->ps_name);
250             PSDRV_WriteSpool(physDev, buf, strlen(buf));
251         }
252
253         switch(pdl->type) {
254         case Type42:
255             T42_free(pdl->typeinfo.Type42);
256             break;
257
258         case Type1:
259             T1_free(pdl->typeinfo.Type1);
260             break;
261
262         default:
263             ERR("Type = %d\n", pdl->type);
264             assert(0);
265         }
266
267         HeapFree(GetProcessHeap(), 0, pdl->ps_name);
268         old = pdl;
269         pdl = pdl->next;
270         HeapFree(GetProcessHeap(), 0, old);
271     }
272     return TRUE;
273 }