Better function parameter checking.
[wine] / dlls / wineps / type42.c
1 /*
2  *      PostScript driver Type42 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
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <assert.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "wingdi.h"
34 #include "winspool.h"
35
36 #include "psdrv.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
40
41
42 #define GET_BE_WORD(ptr)  MAKEWORD( ((BYTE *)(ptr))[1], ((BYTE *)(ptr))[0] )
43 #define GET_BE_DWORD(ptr) ((DWORD)MAKELONG( GET_BE_WORD(&((WORD *)(ptr))[1]), \
44                                             GET_BE_WORD(&((WORD *)(ptr))[0]) ))
45
46 #define MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
47           ( ( (DWORD)_x4 << 24 ) |     \
48             ( (DWORD)_x3 << 16 ) |     \
49             ( (DWORD)_x2 <<  8 ) |     \
50               (DWORD)_x1         )
51
52 typedef struct {
53   DWORD MS_tag;
54   DWORD len, check;
55   BYTE *data;
56   BOOL write;
57 } OTTable;
58
59 const OTTable tables_templ[] = {
60       { MS_MAKE_TAG('c','v','t',' '), 0, 0, NULL, TRUE },
61       { MS_MAKE_TAG('f','p','g','m'), 0, 0, NULL, TRUE },
62       { MS_MAKE_TAG('g','d','i','r'), 0, 0, NULL, TRUE },
63       { MS_MAKE_TAG('g','l','y','f'), 0, 0, NULL, FALSE },
64       { MS_MAKE_TAG('h','e','a','d'), 0, 0, NULL, TRUE },
65       { MS_MAKE_TAG('h','h','e','a'), 0, 0, NULL, TRUE },
66       { MS_MAKE_TAG('h','m','t','x'), 0, 0, NULL, TRUE },
67       { MS_MAKE_TAG('l','o','c','a'), 0, 0, NULL, TRUE },
68       { MS_MAKE_TAG('m','a','x','p'), 0, 0, NULL, TRUE },
69       { MS_MAKE_TAG('p','r','e','p'), 0, 0, NULL, TRUE },
70       { 0, 0, 0, NULL, 0 }
71 };
72
73 struct tagTYPE42 {
74     OTTable tables[sizeof(tables_templ)/sizeof(tables_templ[0])];
75     int glyf_tab, loca_tab, head_tab; /* indices of glyf, loca and head tables */
76     int hmtx_tab, maxp_tab;
77     int num_of_written_tables;
78     DWORD glyph_sent_size;
79     BOOL *glyph_sent;
80     DWORD emsize;
81     DWORD *glyf_blocks;
82 };
83
84 #define GLYPH_SENT_INC 128
85
86 #define FLIP_ORDER(x) \
87  ( ( ((x) & 0xff) << 24) | \
88    ( ((x) & 0xff00) << 8) | \
89    ( ((x) & 0xff0000) >> 8) | \
90    ( ((x) & 0xff000000) >> 24) )
91
92
93 /* Some flags for composite glyphs.  See glyf table in OT spec */
94 #define ARG_1_AND_2_ARE_WORDS    (1L << 0)
95 #define WE_HAVE_A_SCALE          (1L << 3)
96 #define MORE_COMPONENTS          (1L << 5)
97 #define WE_HAVE_AN_X_AND_Y_SCALE (1L << 6)
98 #define WE_HAVE_A_TWO_BY_TWO     (1L << 7)
99
100
101 static BOOL LoadTable(HDC hdc, OTTable *table)
102 {
103     int i;
104
105     if(table->MS_tag == MS_MAKE_TAG('g','d','i','r')) return TRUE;
106     table->len = GetFontData(hdc, table->MS_tag, 0, NULL, 0);
107     table->data = HeapAlloc(GetProcessHeap(), 0, (table->len + 3) & ~3 );
108     memset(table->data + ((table->len - 1) & ~3), 0, sizeof(DWORD));
109     GetFontData(hdc, table->MS_tag, 0, table->data, table->len);
110     table->check = 0;
111     for(i = 0; i < (table->len + 3) / 4; i++)
112         table->check += FLIP_ORDER(*((DWORD*)(table->data) + i));
113     return TRUE;
114 }
115
116 static BOOL get_glyf_pos(TYPE42 *t42, DWORD index, DWORD *start, DWORD *end)
117 {
118     WORD loca_format = GET_BE_WORD(t42->tables[t42->head_tab].data + 50);
119     TRACE("loca_format = %d\n", loca_format);
120     switch(loca_format) {
121     case 0:
122         *start = GET_BE_WORD(((WORD*)t42->tables[t42->loca_tab].data) + index);
123         *start <<= 1;
124         *end = GET_BE_WORD(((WORD*)t42->tables[t42->loca_tab].data) + index + 1);
125         *end <<= 1;
126         break;
127     case 1:
128         *start = GET_BE_DWORD(((DWORD*)t42->tables[t42->loca_tab].data) + index);
129         *end = GET_BE_DWORD(((DWORD*)t42->tables[t42->loca_tab].data) + index + 1);
130         break;
131     default:
132         ERR("Unknown loca_format %d\n", loca_format);
133         return FALSE;
134     }
135     return TRUE;
136 }
137
138 TYPE42 *T42_download_header(PSDRV_PDEVICE *physDev, char *ps_name,
139                             RECT *bbox, UINT emsize)
140 {
141     DWORD i, j, tablepos, nb_blocks, glyf_off = 0, loca_off = 0, cur_off;
142     WORD num_of_tables = sizeof(tables_templ) / sizeof(tables_templ[0]) - 1;
143     char *buf;
144     TYPE42 *t42;
145     const char start[] = /* name, fontbbox */
146             "25 dict begin\n"
147             " /FontName /%s def\n"
148             " /Encoding 256 array 0 1 255{1 index exch /.notdef put} for\n"
149             " def\n"
150             " /PaintType 0 def\n"
151             " /FontMatrix [1 0 0 1 0 0] def\n"
152             " /FontBBox [%f %f %f %f] def\n"
153             " /FontType 42 def\n"
154             " /CharStrings 256 dict begin\n"
155             "  /.notdef 0 def\n"
156             " currentdict end def\n"
157             " /sfnts [\n";
158     const char TT_offset_table[] = "<00010000%04x%04x%04x%04x\n";
159     const char TT_table_dir_entry[] = "%08lx%08lx%08lx%08lx\n";
160     const char storage[] ="]\nhavetype42gdir{pop}{{string} forall}ifelse\n";
161     const char end[] = "] def\n"
162       "havetype42gdir{/GlyphDirectory 256 dict def\n"
163       " sfnts 0 get dup %ld (locx) putinterval %ld (glfx) putinterval}if\n"
164       "currentdict end dup /FontName get exch definefont pop\n";
165
166
167     t42 = HeapAlloc(GetProcessHeap(), 0, sizeof(*t42));
168     memcpy(t42->tables, tables_templ, sizeof(tables_templ));
169     t42->loca_tab = t42->glyf_tab = t42->head_tab = t42->hmtx_tab = -1;
170     t42->emsize = emsize;
171     t42->num_of_written_tables = 0;
172
173     for(i = 0; i < num_of_tables; i++) {
174         LoadTable(physDev->hdc, t42->tables + i);
175         if(t42->tables[i].len > 0xffff && t42->tables[i].write) break;
176         if(t42->tables[i].write) t42->num_of_written_tables++;
177         if(t42->tables[i].MS_tag == MS_MAKE_TAG('l','o','c','a'))
178             t42->loca_tab = i;
179         else if(t42->tables[i].MS_tag == MS_MAKE_TAG('g','l','y','f'))
180             t42->glyf_tab = i;
181         else if(t42->tables[i].MS_tag == MS_MAKE_TAG('h','e','a','d'))
182             t42->head_tab = i;
183         else if(t42->tables[i].MS_tag == MS_MAKE_TAG('h','m','t','x'))
184             t42->hmtx_tab = i;
185         else if(t42->tables[i].MS_tag == MS_MAKE_TAG('m','a','x','p'))
186             t42->maxp_tab = i;
187     }
188     if(i < num_of_tables) {
189         TRACE("Table %ld has length %ld.  Will use Type 1 font instead.\n", i, t42->tables[i].len);
190         T42_free(t42);
191         return NULL;
192     }
193
194     t42->glyph_sent_size = GLYPH_SENT_INC;
195     t42->glyph_sent = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
196                                 t42->glyph_sent_size *
197                                 sizeof(*(t42->glyph_sent)));
198
199     buf = HeapAlloc(GetProcessHeap(), 0, sizeof(start) + strlen(ps_name) +
200                     100);
201
202     sprintf(buf, start, ps_name,
203             (float)bbox->left / emsize, (float)bbox->bottom / emsize,
204             (float)bbox->right / emsize, (float)bbox->top / emsize);
205
206     PSDRV_WriteSpool(physDev, buf, strlen(buf));
207
208     t42->num_of_written_tables++; /* explicitly add glyf */
209     sprintf(buf, TT_offset_table, t42->num_of_written_tables,
210             t42->num_of_written_tables, t42->num_of_written_tables, t42->num_of_written_tables);
211
212     PSDRV_WriteSpool(physDev, buf, strlen(buf));
213
214     tablepos = 12 + t42->num_of_written_tables * 16;
215     cur_off = 12;
216     for(i = 0; i < num_of_tables; i++) {
217         if(!t42->tables[i].write) continue;
218         sprintf(buf, TT_table_dir_entry, FLIP_ORDER(t42->tables[i].MS_tag),
219                 t42->tables[i].check, t42->tables[i].len ? tablepos : 0,
220                 t42->tables[i].len);
221         PSDRV_WriteSpool(physDev, buf, strlen(buf));
222         tablepos += ((t42->tables[i].len + 3) & ~3);
223         if(t42->tables[i].MS_tag == MS_MAKE_TAG('l','o','c','a'))
224             loca_off = cur_off;
225         cur_off += 16;
226     }
227     sprintf(buf, TT_table_dir_entry, FLIP_ORDER(t42->tables[t42->glyf_tab].MS_tag),
228             t42->tables[t42->glyf_tab].check, tablepos, t42->tables[t42->glyf_tab].len);
229     PSDRV_WriteSpool(physDev, buf, strlen(buf));
230     PSDRV_WriteSpool(physDev, "00>\n", 4); /* add an extra byte for old PostScript rips */
231     glyf_off = cur_off;
232
233     for(i = 0; i < num_of_tables; i++) {
234         if(t42->tables[i].len == 0 || !t42->tables[i].write) continue;
235         PSDRV_WriteSpool(physDev, "<", 1);
236         for(j = 0; j < ((t42->tables[i].len + 3) & ~3); j++) {
237             sprintf(buf, "%02x", t42->tables[i].data[j]);
238             PSDRV_WriteSpool(physDev, buf, strlen(buf));
239             if(j % 16 == 15) PSDRV_WriteSpool(physDev, "\n", 1);
240         }
241         PSDRV_WriteSpool(physDev, "00>\n", 4); /* add an extra byte for old PostScript rips */
242     }
243     
244     /* glyf_blocks is a 0 terminated list, holding the start offset of each block.  For simplicity
245        glyf_blocks[0] is 0 */
246     nb_blocks = 2;
247     t42->glyf_blocks = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nb_blocks + 1) * sizeof(DWORD));
248     for(i = 0; i < GET_BE_WORD(t42->tables[t42->maxp_tab].data + 4); i++) {
249         DWORD start, end, size;
250         get_glyf_pos(t42, i, &start, &end);
251         size = end - t42->glyf_blocks[nb_blocks-2];
252         if(size > 0x2000 && t42->glyf_blocks[nb_blocks-1] % 4 == 0) {
253             nb_blocks++;
254             t42->glyf_blocks = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
255                                            t42->glyf_blocks, (nb_blocks + 1) * sizeof(DWORD));
256         }
257         t42->glyf_blocks[nb_blocks-1] = end;
258     }
259
260     PSDRV_WriteSpool(physDev, "[ ", 2);
261     for(i = 1; t42->glyf_blocks[i]; i++) {
262         sprintf(buf,"%ld ", t42->glyf_blocks[i] - t42->glyf_blocks[i-1] + 1);
263         /* again add one byte for old PostScript rips */
264         PSDRV_WriteSpool(physDev, buf, strlen(buf));
265         if(i % 8 == 0)
266             PSDRV_WriteSpool(physDev, "\n", 1);
267     }
268     PSDRV_WriteSpool(physDev, storage, sizeof(storage) - 1);
269     sprintf(buf, end, loca_off, glyf_off);
270     PSDRV_WriteSpool(physDev, buf, strlen(buf));
271     HeapFree(GetProcessHeap(), 0, buf);
272     return t42;
273 }
274
275
276
277
278 BOOL T42_download_glyph(PSDRV_PDEVICE *physDev, DOWNLOAD *pdl, DWORD index,
279                         char *glyph_name)
280 {
281     DWORD start, end, i;
282     char *buf;
283     TYPE42 *t42;
284     WORD awidth;
285     short lsb;
286
287     const char glyph_def[] = 
288       "/%s findfont exch 1 index\n"
289       "havetype42gdir\n"
290       "{/GlyphDirectory get begin %ld exch def end}\n"
291       "{/sfnts get 4 index get 3 index 2 index putinterval pop}\n"
292       "ifelse\n"
293       "/CharStrings get\n"
294       "begin\n"
295       " /%s %ld def\n"
296       "end\n"
297       "pop pop\n";
298
299     TRACE("%ld %s\n", index, glyph_name);
300     assert(pdl->type == Type42);
301     t42 = pdl->typeinfo.Type42;
302
303     if(index < t42->glyph_sent_size) {
304         if(t42->glyph_sent[index])
305             return TRUE;
306     } else {
307         t42->glyph_sent_size = (index / GLYPH_SENT_INC + 1) * GLYPH_SENT_INC;
308         t42->glyph_sent = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
309                                       t42->glyph_sent,
310                                       t42->glyph_sent_size * sizeof(*(t42->glyph_sent)));
311     }
312
313     buf = HeapAlloc(GetProcessHeap(), 0, sizeof(glyph_def) +
314                     strlen(pdl->ps_name) + 100);
315
316     if(!get_glyf_pos(t42, index, &start, &end)) return FALSE;
317     TRACE("start = %lx end = %lx\n", start, end);
318
319     awidth = GET_BE_WORD(t42->tables[t42->hmtx_tab].data + index * 4);
320     lsb = GET_BE_WORD(t42->tables[t42->hmtx_tab].data + index * 4 + 2);
321
322     if(GET_BE_WORD(t42->tables[t42->glyf_tab].data + start) == 0xffff) {
323       /* Composite glyph */
324         char *sg_start = t42->tables[t42->glyf_tab].data + start + 10;
325         DWORD sg_flags, sg_index;
326         char sg_name[MAX_G_NAME + 1];
327
328         do {
329             sg_flags = GET_BE_WORD(sg_start);
330             sg_index = GET_BE_WORD(sg_start + 2);
331
332             TRACE("Sending subglyph %04lx for glyph %04lx\n", sg_index, index);
333             get_glyph_name(physDev->hdc, sg_index, sg_name);
334             T42_download_glyph(physDev, pdl, sg_index, sg_name);
335             sg_start += 4;
336             if(sg_flags & ARG_1_AND_2_ARE_WORDS)
337                 sg_start += 4;
338             else
339                 sg_start += 2;
340             if(sg_flags & WE_HAVE_A_SCALE)
341                 sg_start += 2;
342             else if(sg_flags & WE_HAVE_AN_X_AND_Y_SCALE)
343                 sg_start += 4;
344             else if(sg_flags & WE_HAVE_A_TWO_BY_TWO)
345                 sg_start += 8;
346         } while(sg_flags & MORE_COMPONENTS);
347     }
348
349     for(i = 1; t42->glyf_blocks[i]; i++)
350         if(start < t42->glyf_blocks[i]) break;
351     /* we don't have a string for the gdir and glyf tables, but we do have a 
352        string for the TT header.  So the offset we need is tables - 2 */
353     sprintf(buf, "%ld %ld\n", t42->num_of_written_tables - 2 + i, start - t42->glyf_blocks[i-1]);
354     PSDRV_WriteSpool(physDev, buf, strlen(buf));
355
356     PSDRV_WriteSpool(physDev, "<", 1);
357     for(i = start; i < end; i++) {
358         sprintf(buf, "%02x", *(t42->tables[t42->glyf_tab].data + i));
359         PSDRV_WriteSpool(physDev, buf, strlen(buf));
360         if((i - start) % 16 == 15)
361             PSDRV_WriteSpool(physDev, "\n", 1);
362     }
363     PSDRV_WriteSpool(physDev, ">\n", 2);
364     sprintf(buf, glyph_def, pdl->ps_name, index, glyph_name, index);
365     PSDRV_WriteSpool(physDev, buf, strlen(buf));
366
367     t42->glyph_sent[index] = TRUE;
368     HeapFree(GetProcessHeap(), 0, buf);
369     return TRUE;
370 }
371
372 void T42_free(TYPE42 *t42)
373 {
374     OTTable *table;
375     for(table = t42->tables; table->MS_tag; table++)
376         if(table->data) HeapFree(GetProcessHeap(), 0, table->data);
377     if(t42->glyph_sent) HeapFree(GetProcessHeap(), 0, t42->glyph_sent);
378     if(t42->glyf_blocks) HeapFree(GetProcessHeap(), 0, t42->glyf_blocks);
379     HeapFree(GetProcessHeap(), 0, t42);
380     return;
381 }