Avoid local WINAPI function pointers in _invoke.
[wine] / dlls / wineps / type1.c
1 /*
2  *      PostScript driver Type1 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 <stdio.h>
23 #include <assert.h>
24
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "wingdi.h"
28 #include "winspool.h"
29
30 #include "psdrv.h"
31 #include "wine/debug.h"
32 #include "config.h"
33 #include "wine/port.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
36
37 struct tagTYPE1 {
38     DWORD glyph_sent_size;
39     BOOL *glyph_sent;
40     DWORD emsize;
41 };
42
43 #define GLYPH_SENT_INC 128
44
45 /* Type 1 font commands */
46 enum t1_cmds {
47     rlineto = 5,
48     rrcurveto = 8,
49     closepath = 9,
50     hsbw = 13,
51     endchar = 14,
52     rmoveto = 21
53 };
54
55
56 TYPE1 *T1_download_header(PSDRV_PDEVICE *physDev, LPOUTLINETEXTMETRICA potm,
57                           char *ps_name)
58 {
59     char *buf;
60     TYPE1 *t1;
61
62     char dict[] = /* name, emsquare, fontbbox */
63       "25 dict begin\n"
64       " /FontName /%s def\n"
65       " /Encoding 256 array 0 1 255{1 index exch /.notdef put} for def\n"
66       " /PaintType 0 def\n"
67       " /FontMatrix [1 %d div 0 0 1 %d div 0 0] def\n"
68       " /FontBBox [%d %d %d %d] def\n"
69       " /FontType 1 def\n"
70       " /Private 7 dict begin\n"
71       "  /RD {string currentfile exch readhexstring pop} def\n"
72       "  /ND {def} def\n"
73       "  /NP {put} def\n"
74       "  /MinFeature {16 16} def\n"
75       "  /BlueValues [] def\n"
76       "  /password 5839 def\n"
77       "  /lenIV -1 def\n"
78       " currentdict end def\n"
79       " currentdict dup /Private get begin\n"
80       "  /CharStrings 256 dict begin\n"
81       "   /.notdef 4 RD 8b8b0d0e ND\n"
82       "  currentdict end put\n"
83       " end\n"
84       "currentdict end dup /FontName get exch definefont pop\n";
85
86     t1 = HeapAlloc(GetProcessHeap(), 0, sizeof(*t1));
87     t1->emsize = potm->otmEMSquare;
88
89     t1->glyph_sent_size = GLYPH_SENT_INC;
90     t1->glyph_sent = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
91                                t1->glyph_sent_size *
92                                sizeof(*(t1->glyph_sent)));
93
94     buf = HeapAlloc(GetProcessHeap(), 0, sizeof(dict) + strlen(ps_name) +
95                     100);
96
97     sprintf(buf, dict, ps_name, t1->emsize, t1->emsize,
98             potm->otmrcFontBox.left, potm->otmrcFontBox.bottom,
99             potm->otmrcFontBox.right, potm->otmrcFontBox.top);
100
101     PSDRV_WriteSpool(physDev, buf, strlen(buf));
102
103     HeapFree(GetProcessHeap(), 0, buf);
104     return t1;
105 }
106
107
108 typedef struct {
109   BYTE *str;
110   int len, max_len;
111 } STR;
112
113 static STR *str_init(int sz)
114 {
115   STR *str = HeapAlloc(GetProcessHeap(), 0, sizeof(*str));
116   str->max_len = sz;
117   str->str = HeapAlloc(GetProcessHeap(), 0, str->max_len);
118   str->len = 0;
119   return str;
120 }
121
122 static void str_free(STR *str)
123 {
124   HeapFree(GetProcessHeap(), 0, str->str);
125   HeapFree(GetProcessHeap(), 0, str);
126 }
127
128 static void str_add_byte(STR *str, BYTE b)
129 {
130     if(str->len == str->max_len) {
131         str->max_len *= 2;
132         str->str = HeapReAlloc(GetProcessHeap(), 0, str->str, str->max_len);
133     }
134     str->str[str->len++] = b;
135 }
136
137 static void str_add_num(STR *str, int num)
138 {
139     if(num <= 107 && num >= -107)
140         str_add_byte(str, num + 139);
141     else if(num >= 108 && num <= 1131) {
142         str_add_byte(str, ((num - 108) >> 8) + 247);
143         str_add_byte(str, (num - 108) & 0xff);
144     } else if(num <= -108 && num >= -1131) {
145         num = -num;
146         str_add_byte(str, ((num - 108) >> 8) + 251);
147         str_add_byte(str, (num - 108) & 0xff);
148     } else {
149         str_add_byte(str, 0xff);
150         str_add_byte(str, (num >> 24) & 0xff);
151         str_add_byte(str, (num >> 16) & 0xff);
152         str_add_byte(str, (num >> 8) & 0xff);
153         str_add_byte(str, (num & 0xff));
154     }
155 }
156
157 static void str_add_point(STR *str, POINTFX *pt, POINT *curpos)
158 {
159     POINT newpos;
160     newpos.x = pt->x.value + ((pt->x.fract >> 15) & 0x1);
161     newpos.y = pt->y.value + ((pt->y.fract >> 15) & 0x1);
162
163     str_add_num(str, newpos.x - curpos->x);
164     str_add_num(str, newpos.y - curpos->y);
165     *curpos = newpos;
166 }
167
168 static void str_add_cmd(STR *str, enum t1_cmds cmd)
169 {
170     str_add_byte(str, (BYTE)cmd);
171 }
172
173 static int str_get_bytes(STR *str, BYTE **b)
174 {
175   *b = str->str;
176   return str->len;
177 }
178
179 BOOL T1_download_glyph(PSDRV_PDEVICE *physDev, DOWNLOAD *pdl, DWORD index,
180                        char *glyph_name)
181 {
182     DWORD len, i;
183     char *buf;
184     TYPE1 *t1;
185     STR *charstring;
186     BYTE *bytes;
187     HFONT old_font, unscaled_font;
188     GLYPHMETRICS gm;
189     char *glyph_buf;
190     POINT curpos;
191     TTPOLYGONHEADER *pph;
192     TTPOLYCURVE *ppc;
193     LOGFONTW lf;
194     RECT rc;
195
196     char glyph_def_begin[] =
197       "/%s findfont dup\n"
198       "/Private get begin\n"
199       "/CharStrings get begin\n"
200       "/%s %d RD\n";
201     char glyph_def_end[] =
202       "ND\n"
203       "end end\n";
204
205     TRACE("%ld %s\n", index, glyph_name);
206     assert(pdl->type == Type1);
207     t1 = pdl->typeinfo.Type1;
208
209     if(index < t1->glyph_sent_size) {
210         if(t1->glyph_sent[index])
211             return TRUE;
212     } else {
213         t1->glyph_sent_size = (index / GLYPH_SENT_INC + 1) * GLYPH_SENT_INC;
214         t1->glyph_sent = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
215                                       t1->glyph_sent,
216                                       t1->glyph_sent_size * sizeof(*(t1->glyph_sent)));
217     }
218
219     GetObjectW(GetCurrentObject(physDev->hdc, OBJ_FONT), sizeof(lf), &lf);
220     rc.left = rc.right = rc.bottom = 0;
221     rc.top = t1->emsize;
222     DPtoLP(physDev->hdc, (POINT*)&rc, 2);
223     lf.lfHeight = -abs(rc.top - rc.bottom);
224     unscaled_font = CreateFontIndirectW(&lf);
225     old_font = SelectObject(physDev->hdc, unscaled_font);
226     len = GetGlyphOutlineW(physDev->hdc, index, GGO_GLYPH_INDEX | GGO_BEZIER,
227                            &gm, 0, NULL, NULL);
228     if(len == GDI_ERROR) return FALSE;
229     glyph_buf = HeapAlloc(GetProcessHeap(), 0, len);
230     GetGlyphOutlineW(physDev->hdc, index, GGO_GLYPH_INDEX | GGO_BEZIER,
231                      &gm, len, glyph_buf, NULL);
232
233     SelectObject(physDev->hdc, old_font);
234     DeleteObject(unscaled_font);
235
236     charstring = str_init(100);
237
238     curpos.x = gm.gmptGlyphOrigin.x;
239     curpos.y = 0;
240
241     str_add_num(charstring, curpos.x);
242     str_add_num(charstring, gm.gmCellIncX);
243     str_add_cmd(charstring, hsbw);
244
245     pph = (TTPOLYGONHEADER*)glyph_buf;
246     while((char*)pph < glyph_buf + len) {
247         TRACE("contour len %ld\n", pph->cb);
248         ppc = (TTPOLYCURVE*)((char*)pph + sizeof(*pph));
249
250         str_add_point(charstring, &pph->pfxStart, &curpos);
251         str_add_cmd(charstring, rmoveto);
252
253         while((char*)ppc < (char*)pph + pph->cb) {
254             TRACE("line type %d cpfx = %d\n", ppc->wType, ppc->cpfx);
255             switch(ppc->wType) {
256             case TT_PRIM_LINE:
257                 for(i = 0; i < ppc->cpfx; i++) {
258                     str_add_point(charstring, ppc->apfx + i, &curpos);
259                     str_add_cmd(charstring, rlineto);
260                 }
261                 break;
262             case TT_PRIM_CSPLINE:
263                 for(i = 0; i < ppc->cpfx/3; i++) {
264                     str_add_point(charstring, ppc->apfx + 3 * i, &curpos);
265                     str_add_point(charstring, ppc->apfx + 3 * i + 1, &curpos);
266                     str_add_point(charstring, ppc->apfx + 3 * i + 2, &curpos);
267                     str_add_cmd(charstring, rrcurveto);
268                 }
269                 break;
270             default:
271                 ERR("curve type = %d\n", ppc->wType);
272                 return FALSE;
273             }
274             ppc = (TTPOLYCURVE*)((char*)ppc + sizeof(*ppc) +
275                                  (ppc->cpfx - 1) * sizeof(POINTFX));
276         }
277         str_add_cmd(charstring, closepath);
278         pph = (TTPOLYGONHEADER*)((char*)pph + pph->cb);
279     }
280     str_add_cmd(charstring, endchar);
281
282     buf = HeapAlloc(GetProcessHeap(), 0, sizeof(glyph_def_begin) +
283                     strlen(pdl->ps_name) + strlen(glyph_name) + 100);
284
285     sprintf(buf, "%%%%glyph %04lx\n", index);
286     PSDRV_WriteSpool(physDev, buf, strlen(buf));
287
288     len = str_get_bytes(charstring, &bytes);
289     sprintf(buf, glyph_def_begin, pdl->ps_name, glyph_name, len);
290     PSDRV_WriteSpool(physDev, buf, strlen(buf));
291     PSDRV_WriteBytes(physDev, bytes, len);
292     sprintf(buf, glyph_def_end);
293     PSDRV_WriteSpool(physDev, buf, strlen(buf));
294     str_free(charstring);
295
296     t1->glyph_sent[index] = TRUE;
297     HeapFree(GetProcessHeap(), 0, glyph_buf);
298     HeapFree(GetProcessHeap(), 0, buf);
299     return TRUE;
300 }
301
302 void T1_free(TYPE1 *t1)
303 {
304     HeapFree(GetProcessHeap(), 0, t1->glyph_sent);
305     HeapFree(GetProcessHeap(), 0, t1);
306     return;
307 }