Fix size return from InternetGetCookieW.
[wine/multimedia.git] / dlls / wineps / download.c
blobc7f1d718c2f037f130a07cb5bc647c89925a738a
1 /*
2 * PostScript driver downloadable font functions
4 * Copyright 2002 Huw D M Davies for CodeWeavers
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.
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.
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
20 #include <string.h>
21 #include <stdlib.h>
22 #include <assert.h>
23 #include <stdarg.h>
24 #include <stdio.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "wingdi.h"
30 #include "winspool.h"
32 #include "psdrv.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
37 #define MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
38 ( ( (DWORD)_x4 << 24 ) | \
39 ( (DWORD)_x3 << 16 ) | \
40 ( (DWORD)_x2 << 8 ) | \
41 (DWORD)_x1 )
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]) ))
47 /****************************************************************************
48 * get_download_name
50 static void get_download_name(PSDRV_PDEVICE *physDev, LPOUTLINETEXTMETRICA
51 potm, char **str)
53 int len;
54 char *p;
55 len = strlen((char*)potm + (ptrdiff_t)potm->otmpFullName) + 1;
56 *str = HeapAlloc(GetProcessHeap(),0,len);
57 strcpy(*str, (char*)potm + (ptrdiff_t)potm->otmpFullName);
59 p = *str;
60 while((p = strchr(p, ' ')))
61 *p = '_';
63 return;
66 /****************************************************************************
67 * is_font_downloaded
69 static DOWNLOAD *is_font_downloaded(PSDRV_PDEVICE *physDev, char *ps_name)
71 DOWNLOAD *pdl;
73 for(pdl = physDev->downloaded_fonts; pdl; pdl = pdl->next)
74 if(!strcmp(pdl->ps_name, ps_name))
75 break;
76 return pdl;
79 /****************************************************************************
80 * is_room_for_font
82 static BOOL is_room_for_font(PSDRV_PDEVICE *physDev)
84 DOWNLOAD *pdl;
85 int count = 0;
87 /* FIXME: should consider vm usage of each font and available printer memory.
88 For now we allow upto two fonts to be downloaded at a time */
89 for(pdl = physDev->downloaded_fonts; pdl; pdl = pdl->next)
90 count++;
92 if(count > 1)
93 return FALSE;
94 return TRUE;
97 /****************************************************************************
98 * get_bbox
100 * This retrieves the bounding box of the font in font units as well as
101 * the size of the emsquare. To avoid having to worry about mapping mode and
102 * the font size we'll get the data directly from the TrueType HEAD table rather
103 * than using GetOutlineTextMetrics.
105 static BOOL get_bbox(PSDRV_PDEVICE *physDev, RECT *rc, UINT *emsize)
107 BYTE head[54]; /* the head table is 54 bytes long */
109 if(GetFontData(physDev->hdc, MS_MAKE_TAG('h','e','a','d'), 0, head,
110 sizeof(head)) == GDI_ERROR) {
111 ERR("Can't retrieve head table\n");
112 return FALSE;
114 *emsize = GET_BE_WORD(head + 18); /* unitsPerEm */
115 rc->left = (signed short)GET_BE_WORD(head + 36); /* xMin */
116 rc->bottom = (signed short)GET_BE_WORD(head + 38); /* yMin */
117 rc->right = (signed short)GET_BE_WORD(head + 40); /* xMax */
118 rc->top = (signed short)GET_BE_WORD(head + 42); /* yMax */
119 return TRUE;
122 /****************************************************************************
123 * PSDRV_SelectDownloadFont
125 * Set up physDev->font for a downloadable font
128 BOOL PSDRV_SelectDownloadFont(PSDRV_PDEVICE *physDev)
130 char *ps_name;
131 LPOUTLINETEXTMETRICA potm;
132 DWORD len = GetOutlineTextMetricsA(physDev->hdc, 0, NULL);
134 potm = HeapAlloc(GetProcessHeap(), 0, len);
135 GetOutlineTextMetricsA(physDev->hdc, len, potm);
136 get_download_name(physDev, potm, &ps_name);
138 physDev->font.fontloc = Download;
139 physDev->font.fontinfo.Download = is_font_downloaded(physDev, ps_name);
141 physDev->font.size = abs(PSDRV_YWStoDS(physDev, /* ppem */
142 potm->otmTextMetrics.tmAscent +
143 potm->otmTextMetrics.tmDescent -
144 potm->otmTextMetrics.tmInternalLeading));
145 physDev->font.underlineThickness = potm->otmsUnderscoreSize;
146 physDev->font.underlinePosition = potm->otmsUnderscorePosition;
147 physDev->font.strikeoutThickness = potm->otmsStrikeoutSize;
148 physDev->font.strikeoutPosition = potm->otmsStrikeoutPosition;
150 HeapFree(GetProcessHeap(), 0, ps_name);
151 HeapFree(GetProcessHeap(), 0, potm);
152 return TRUE;
155 /****************************************************************************
156 * PSDRV_WriteSetDownloadFont
158 * Write setfont for download font.
161 BOOL PSDRV_WriteSetDownloadFont(PSDRV_PDEVICE *physDev)
163 char *ps_name;
164 LPOUTLINETEXTMETRICA potm;
165 DWORD len = GetOutlineTextMetricsA(physDev->hdc, 0, NULL);
166 DOWNLOAD *pdl;
168 assert(physDev->font.fontloc == Download);
170 potm = HeapAlloc(GetProcessHeap(), 0, len);
171 GetOutlineTextMetricsA(physDev->hdc, len, potm);
173 get_download_name(physDev, potm, &ps_name);
175 if(physDev->font.fontinfo.Download == NULL) {
176 RECT bbox;
177 UINT emsize;
179 pdl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pdl));
180 pdl->ps_name = HeapAlloc(GetProcessHeap(), 0, strlen(ps_name)+1);
181 strcpy(pdl->ps_name, ps_name);
182 pdl->next = NULL;
184 get_bbox(physDev, &bbox, &emsize);
185 if(!is_room_for_font(physDev))
186 PSDRV_EmptyDownloadList(physDev, TRUE);
188 if(physDev->pi->ppd->TTRasterizer == RO_Type42) {
189 pdl->typeinfo.Type42 = T42_download_header(physDev, ps_name, &bbox, emsize);
190 pdl->type = Type42;
192 if(pdl->typeinfo.Type42 == NULL) {
193 pdl->typeinfo.Type1 = T1_download_header(physDev, ps_name, &bbox, emsize);
194 pdl->type = Type1;
196 pdl->next = physDev->downloaded_fonts;
197 physDev->downloaded_fonts = pdl;
198 physDev->font.fontinfo.Download = pdl;
200 if(pdl->type == Type42) {
201 char g_name[MAX_G_NAME + 1];
202 get_glyph_name(physDev->hdc, 0, g_name);
203 T42_download_glyph(physDev, pdl, 0, g_name);
208 PSDRV_WriteSetFont(physDev, ps_name, physDev->font.size,
209 physDev->font.escapement);
211 HeapFree(GetProcessHeap(), 0, ps_name);
212 HeapFree(GetProcessHeap(), 0, potm);
213 return TRUE;
216 void get_glyph_name(HDC hdc, WORD index, char *name)
218 /* FIXME */
219 sprintf(name, "g%04x", index);
220 return;
223 /****************************************************************************
224 * PSDRV_WriteDownloadGlyphShow
226 * Download and write out a number of glyphs
229 BOOL PSDRV_WriteDownloadGlyphShow(PSDRV_PDEVICE *physDev, WORD *glyphs,
230 UINT count)
232 UINT i;
233 char g_name[MAX_G_NAME + 1];
234 assert(physDev->font.fontloc == Download);
236 switch(physDev->font.fontinfo.Download->type) {
237 case Type42:
238 for(i = 0; i < count; i++) {
239 get_glyph_name(physDev->hdc, glyphs[i], g_name);
240 T42_download_glyph(physDev, physDev->font.fontinfo.Download,
241 glyphs[i], g_name);
242 PSDRV_WriteGlyphShow(physDev, g_name);
244 break;
246 case Type1:
247 for(i = 0; i < count; i++) {
248 get_glyph_name(physDev->hdc, glyphs[i], g_name);
249 T1_download_glyph(physDev, physDev->font.fontinfo.Download,
250 glyphs[i], g_name);
251 PSDRV_WriteGlyphShow(physDev, g_name);
253 break;
255 default:
256 ERR("Type = %d\n", physDev->font.fontinfo.Download->type);
257 assert(0);
259 return TRUE;
262 /****************************************************************************
263 * PSDRV_EmptyDownloadList
265 * Clear the list of downloaded fonts
268 BOOL PSDRV_EmptyDownloadList(PSDRV_PDEVICE *physDev, BOOL write_undef)
270 DOWNLOAD *pdl, *old;
271 static const char undef[] = "/%s findfont 40 scalefont setfont /%s undefinefont\n";
272 char buf[sizeof(undef) + 200];
273 char *default_font = physDev->pi->ppd->DefaultFont ?
274 physDev->pi->ppd->DefaultFont : "Courier";
276 if(physDev->font.fontloc == Download) {
277 physDev->font.set = FALSE;
278 physDev->font.fontinfo.Download = NULL;
281 pdl = physDev->downloaded_fonts;
282 physDev->downloaded_fonts = NULL;
283 while(pdl) {
284 if(write_undef) {
285 sprintf(buf, undef, default_font, pdl->ps_name);
286 PSDRV_WriteSpool(physDev, buf, strlen(buf));
289 switch(pdl->type) {
290 case Type42:
291 T42_free(pdl->typeinfo.Type42);
292 break;
294 case Type1:
295 T1_free(pdl->typeinfo.Type1);
296 break;
298 default:
299 ERR("Type = %d\n", pdl->type);
300 assert(0);
303 HeapFree(GetProcessHeap(), 0, pdl->ps_name);
304 old = pdl;
305 pdl = pdl->next;
306 HeapFree(GetProcessHeap(), 0, old);
308 return TRUE;