d3dx9/tests: Add tests for ID3DXFont::PreloadText.
[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 <math.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winnls.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(PHYSDEV dev, LPOUTLINETEXTMETRICA potm, char **str)
51 {
52     int len;
53     char *p;
54     DWORD size;
55
56     size = GetFontData(dev->hdc, MS_MAKE_TAG('n','a','m','e'), 0, NULL, 0);
57     if(size != 0 && size != GDI_ERROR)
58     {
59         BYTE *name = HeapAlloc(GetProcessHeap(), 0, size);
60         if(name)
61         {
62             USHORT count, i;
63             BYTE *strings;
64             struct
65             {
66                 USHORT platform_id;
67                 USHORT encoding_id;
68                 USHORT language_id;
69                 USHORT name_id;
70                 USHORT length;
71                 USHORT offset;
72             } *name_record;
73
74             GetFontData(dev->hdc, MS_MAKE_TAG('n','a','m','e'), 0, name, size);
75             count = GET_BE_WORD(name + 2);
76             strings = name + GET_BE_WORD(name + 4);
77             name_record = (typeof(name_record))(name + 6);
78             for(i = 0; i < count; i++, name_record++)
79             {
80                 name_record->platform_id = GET_BE_WORD(&name_record->platform_id);
81                 name_record->encoding_id = GET_BE_WORD(&name_record->encoding_id);
82                 name_record->language_id = GET_BE_WORD(&name_record->language_id);
83                 name_record->name_id     = GET_BE_WORD(&name_record->name_id);
84                 name_record->length      = GET_BE_WORD(&name_record->length);
85                 name_record->offset      = GET_BE_WORD(&name_record->offset);
86
87                 if(name_record->platform_id == 1 && name_record->encoding_id == 0 &&
88                    name_record->language_id == 0 && name_record->name_id == 6)
89                 {
90                     TRACE("Got Mac PS name %s\n", debugstr_an((char*)strings + name_record->offset, name_record->length));
91                     *str = HeapAlloc(GetProcessHeap(), 0, name_record->length + 1);
92                     memcpy(*str, strings + name_record->offset, name_record->length);
93                     *(*str + name_record->length) = '\0';
94                     HeapFree(GetProcessHeap(), 0, name);
95                     return;
96                 }
97                 if(name_record->platform_id == 3 && name_record->encoding_id == 1 &&
98                    name_record->language_id == 0x409 && name_record->name_id == 6)
99                 {
100                     WCHAR *unicode = HeapAlloc(GetProcessHeap(), 0, name_record->length + 2);
101                     DWORD len;
102                     int c;
103
104                     for(c = 0; c < name_record->length / 2; c++)
105                         unicode[c] = GET_BE_WORD(strings + name_record->offset + c * 2);
106                     unicode[c] = 0;
107                     TRACE("Got Windows PS name %s\n", debugstr_w(unicode));
108                     len = WideCharToMultiByte(1252, 0, unicode, -1, NULL, 0, NULL, NULL);
109                     *str = HeapAlloc(GetProcessHeap(), 0, len);
110                     WideCharToMultiByte(1252, 0, unicode, -1, *str, len, NULL, NULL);
111                     HeapFree(GetProcessHeap(), 0, unicode);
112                     HeapFree(GetProcessHeap(), 0, name);
113                     return;
114                 }
115             }
116             TRACE("Unable to find PostScript name\n");
117             HeapFree(GetProcessHeap(), 0, name);
118         }
119     }
120
121     len = strlen((char*)potm + (ptrdiff_t)potm->otmpFullName) + 1;
122     *str = HeapAlloc(GetProcessHeap(),0,len);
123     strcpy(*str, (char*)potm + (ptrdiff_t)potm->otmpFullName);
124
125     p = *str;
126     while((p = strchr(p, ' ')))
127         *p = '_';
128
129     return;
130 }
131
132 /****************************************************************************
133  *  is_font_downloaded
134  */
135 static DOWNLOAD *is_font_downloaded(PSDRV_PDEVICE *physDev, char *ps_name)
136 {
137     DOWNLOAD *pdl;
138
139     for(pdl = physDev->downloaded_fonts; pdl; pdl = pdl->next)
140         if(!strcmp(pdl->ps_name, ps_name))
141             break;
142     return pdl;
143 }
144
145 /****************************************************************************
146  *  is_room_for_font
147  */
148 static BOOL is_room_for_font(PSDRV_PDEVICE *physDev)
149 {
150     DOWNLOAD *pdl;
151     int count = 0;
152
153     /* FIXME: should consider vm usage of each font and available printer memory.
154        For now we allow up to two fonts to be downloaded at a time */
155     for(pdl = physDev->downloaded_fonts; pdl; pdl = pdl->next)
156         count++;
157
158     if(count > 1)
159         return FALSE;
160     return TRUE;
161 }
162
163 /****************************************************************************
164  *  get_bbox
165  *
166  * This retrieves the bounding box of the font in font units as well as
167  * the size of the emsquare.  To avoid having to worry about mapping mode and
168  * the font size we'll get the data directly from the TrueType HEAD table rather
169  * than using GetOutlineTextMetrics.
170  */
171 static BOOL get_bbox(HDC hdc, RECT *rc, UINT *emsize)
172 {
173     BYTE head[54]; /* the head table is 54 bytes long */
174
175     if(GetFontData(hdc, MS_MAKE_TAG('h','e','a','d'), 0, head, sizeof(head)) == GDI_ERROR)
176     {
177         ERR("Can't retrieve head table\n");
178         return FALSE;
179     }
180     *emsize = GET_BE_WORD(head + 18); /* unitsPerEm */
181     if(rc)
182     {
183         rc->left   = (signed short)GET_BE_WORD(head + 36); /* xMin */
184         rc->bottom = (signed short)GET_BE_WORD(head + 38); /* yMin */
185         rc->right  = (signed short)GET_BE_WORD(head + 40); /* xMax */
186         rc->top    = (signed short)GET_BE_WORD(head + 42); /* yMax */
187     }
188     return TRUE;
189 }
190
191 /****************************************************************************
192  *  PSDRV_SelectDownloadFont
193  *
194  *  Set up physDev->font for a downloadable font
195  *
196  */
197 BOOL PSDRV_SelectDownloadFont(PHYSDEV dev)
198 {
199     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
200
201     physDev->font.fontloc = Download;
202     physDev->font.fontinfo.Download = NULL;
203
204     return TRUE;
205 }
206
207 static UINT calc_ppem_for_height(HDC hdc, LONG height)
208 {
209     BYTE os2[78]; /* size of version 0 table */
210     BYTE hhea[8]; /* just enough to get the ascender and descender */
211     LONG ascent = 0, descent = 0;
212     UINT emsize;
213
214     if(height < 0) return -height;
215
216     if(GetFontData(hdc, MS_MAKE_TAG('O','S','/','2'), 0, os2, sizeof(os2)) == sizeof(os2))
217     {
218         ascent  = GET_BE_WORD(os2 + 74); /* usWinAscent */
219         descent = GET_BE_WORD(os2 + 76); /* usWinDescent */
220     }
221
222     if(ascent + descent == 0)
223     {
224         if(GetFontData(hdc, MS_MAKE_TAG('h','h','e','a'), 0, hhea, sizeof(hhea)) == sizeof(hhea))
225         {
226             ascent  =  (signed short)GET_BE_WORD(hhea + 4); /* Ascender */
227             descent = -(signed short)GET_BE_WORD(hhea + 6); /* Descender */
228         }
229     }
230
231     if(ascent + descent == 0) return height;
232
233     get_bbox(hdc, NULL, &emsize);
234
235     return MulDiv(emsize, height, ascent + descent);
236 }
237
238 static inline float ps_round(float f)
239 {
240     return (f > 0) ? (f + 0.5) : (f - 0.5);
241 }
242 /****************************************************************************
243  *  PSDRV_WriteSetDownloadFont
244  *
245  *  Write setfont for download font.
246  *
247  */
248 BOOL PSDRV_WriteSetDownloadFont(PHYSDEV dev)
249 {
250     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
251     char *ps_name;
252     LPOUTLINETEXTMETRICA potm;
253     DWORD len = GetOutlineTextMetricsA(dev->hdc, 0, NULL);
254     DOWNLOAD *pdl;
255     LOGFONTW lf;
256     UINT ppem;
257     XFORM xform;
258
259     assert(physDev->font.fontloc == Download);
260
261     if (!GetObjectW( GetCurrentObject(dev->hdc, OBJ_FONT), sizeof(lf), &lf ))
262         return FALSE;
263
264     potm = HeapAlloc(GetProcessHeap(), 0, len);
265     if (!potm)
266         return FALSE;
267
268     GetOutlineTextMetricsA(dev->hdc, len, potm);
269
270     get_download_name(dev, potm, &ps_name);
271     physDev->font.fontinfo.Download = is_font_downloaded(physDev, ps_name);
272
273     ppem = calc_ppem_for_height(dev->hdc, lf.lfHeight);
274
275     /* Retrieve the world -> device transform */
276     GetTransform(dev->hdc, 0x204, &xform);
277
278     if(GetGraphicsMode(dev->hdc) == GM_COMPATIBLE)
279     {
280         if (xform.eM22 < 0) physDev->font.escapement = -physDev->font.escapement;
281         xform.eM11 = xform.eM22 = fabs(xform.eM22);
282         xform.eM21 = xform.eM12 = 0;
283     }
284
285     physDev->font.size.xx = ps_round(ppem * xform.eM11);
286     physDev->font.size.xy = ps_round(ppem * xform.eM12);
287     physDev->font.size.yx = -ps_round(ppem * xform.eM21);
288     physDev->font.size.yy = -ps_round(ppem * xform.eM22);
289
290     physDev->font.underlineThickness = potm->otmsUnderscoreSize;
291     physDev->font.underlinePosition = potm->otmsUnderscorePosition;
292     physDev->font.strikeoutThickness = potm->otmsStrikeoutSize;
293     physDev->font.strikeoutPosition = potm->otmsStrikeoutPosition;
294
295     if(physDev->font.fontinfo.Download == NULL) {
296         RECT bbox;
297         UINT emsize;
298
299         if (!get_bbox(dev->hdc, &bbox, &emsize)) {
300             HeapFree(GetProcessHeap(), 0, potm);
301             return FALSE;
302         }
303         if(!is_room_for_font(physDev))
304             PSDRV_EmptyDownloadList(dev, TRUE);
305
306         pdl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pdl));
307         pdl->ps_name = HeapAlloc(GetProcessHeap(), 0, strlen(ps_name)+1);
308         strcpy(pdl->ps_name, ps_name);
309         pdl->next = NULL;
310
311         if(physDev->pi->ppd->TTRasterizer == RO_Type42) {
312             pdl->typeinfo.Type42 = T42_download_header(dev, ps_name, &bbox, emsize);
313             pdl->type = Type42;
314         }
315         if(pdl->typeinfo.Type42 == NULL) {
316             pdl->typeinfo.Type1 = T1_download_header(dev, ps_name, &bbox, emsize);
317             pdl->type = Type1;
318         }
319         pdl->next = physDev->downloaded_fonts;
320         physDev->downloaded_fonts = pdl;
321         physDev->font.fontinfo.Download = pdl;
322
323         if(pdl->type == Type42) {
324             char g_name[MAX_G_NAME + 1];
325             get_glyph_name(dev->hdc, 0, g_name);
326             T42_download_glyph(dev, pdl, 0, g_name);
327         }
328     }
329
330
331     PSDRV_WriteSetFont(dev, ps_name, physDev->font.size, physDev->font.escapement);
332
333     HeapFree(GetProcessHeap(), 0, ps_name);
334     HeapFree(GetProcessHeap(), 0, potm);
335     return TRUE;
336 }
337
338 void get_glyph_name(HDC hdc, WORD index, char *name)
339 {
340   /* FIXME */
341     sprintf(name, "g%04x", index);
342     return;
343 }
344
345 /****************************************************************************
346  *  PSDRV_WriteDownloadGlyphShow
347  *
348  *  Download and write out a number of glyphs
349  *
350  */
351 BOOL PSDRV_WriteDownloadGlyphShow(PHYSDEV dev, WORD *glyphs,
352                                   UINT count)
353 {
354     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
355     UINT i;
356     char g_name[MAX_G_NAME + 1];
357     assert(physDev->font.fontloc == Download);
358
359     switch(physDev->font.fontinfo.Download->type) {
360     case Type42:
361     for(i = 0; i < count; i++) {
362         get_glyph_name(dev->hdc, glyphs[i], g_name);
363         T42_download_glyph(dev, physDev->font.fontinfo.Download, glyphs[i], g_name);
364         PSDRV_WriteGlyphShow(dev, g_name);
365     }
366     break;
367
368     case Type1:
369     for(i = 0; i < count; i++) {
370         get_glyph_name(dev->hdc, glyphs[i], g_name);
371         T1_download_glyph(dev, physDev->font.fontinfo.Download, glyphs[i], g_name);
372         PSDRV_WriteGlyphShow(dev, g_name);
373     }
374     break;
375
376     default:
377         ERR("Type = %d\n", physDev->font.fontinfo.Download->type);
378         assert(0);
379     }
380     return TRUE;
381 }
382
383 /****************************************************************************
384  *  PSDRV_EmptyDownloadList
385  *
386  *  Clear the list of downloaded fonts
387  *
388  */
389 BOOL PSDRV_EmptyDownloadList(PHYSDEV dev, BOOL write_undef)
390 {
391     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
392     DOWNLOAD *pdl, *old;
393     static const char undef[] = "/%s findfont 40 scalefont setfont /%s undefinefont\n";
394     char buf[sizeof(undef) + 200];
395     const char *default_font = physDev->pi->ppd->DefaultFont ?
396         physDev->pi->ppd->DefaultFont : "Courier";
397
398     if(physDev->font.fontloc == Download) {
399         physDev->font.set = FALSE;
400         physDev->font.fontinfo.Download = NULL;
401     }
402
403     pdl = physDev->downloaded_fonts;
404     physDev->downloaded_fonts = NULL;
405     while(pdl) {
406         if(write_undef) {
407             sprintf(buf, undef, default_font, pdl->ps_name);
408             PSDRV_WriteSpool(dev, buf, strlen(buf));
409         }
410
411         switch(pdl->type) {
412         case Type42:
413             T42_free(pdl->typeinfo.Type42);
414             break;
415
416         case Type1:
417             T1_free(pdl->typeinfo.Type1);
418             break;
419
420         default:
421             ERR("Type = %d\n", pdl->type);
422             assert(0);
423         }
424
425         HeapFree(GetProcessHeap(), 0, pdl->ps_name);
426         old = pdl;
427         pdl = pdl->next;
428         HeapFree(GetProcessHeap(), 0, old);
429     }
430     return TRUE;
431 }