xactengine3_7: Map IXACT3Wave interfaces.
[wine.git] / dlls / wineps.drv / type1.c
blobf31c6dd3beea366f312bdac6f2ae0aaa1d492b0e
1 /*
2 * PostScript driver Type1 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <string.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <assert.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winternl.h"
32 #include "psdrv.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
37 struct tagTYPE1 {
38 DWORD glyph_sent_size;
39 BOOL *glyph_sent;
40 DWORD emsize;
43 #define GLYPH_SENT_INC 128
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
56 #define MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
57 ( ( (DWORD)_x4 << 24 ) | \
58 ( (DWORD)_x3 << 16 ) | \
59 ( (DWORD)_x2 << 8 ) | \
60 (DWORD)_x1 )
62 #ifdef WORDS_BIGENDIAN
63 static inline WORD get_be_word(const void *p) { return *(const WORD*)p; }
64 static inline DWORD get_be_dword(const void *p) { return *(const DWORD*)p; }
65 #else
66 static inline WORD get_be_word(const void *p) { return RtlUshortByteSwap(*(const WORD*)p); }
67 static inline DWORD get_be_dword(const void *p) { return RtlUlongByteSwap(*(const DWORD*)p); }
68 #endif
70 TYPE1 *T1_download_header(PHYSDEV dev, char *ps_name, RECT *bbox, UINT emsize)
72 char *buf;
73 TYPE1 *t1;
75 static const char dict[] = /* name, emsquare, fontbbox */
76 "25 dict begin\n"
77 " /FontName /%s def\n"
78 " /Encoding 256 array 0 1 255{1 index exch /.notdef put} for def\n"
79 " /PaintType 0 def\n"
80 " /FontMatrix [1 %ld div 0 0 1 %ld div 0 0] def\n"
81 " /FontBBox [%ld %ld %ld %ld] def\n"
82 " /FontType 1 def\n"
83 " /Private 7 dict begin\n"
84 " /RD {string currentfile exch readhexstring pop} def\n"
85 " /ND {def} def\n"
86 " /NP {put} def\n"
87 " /MinFeature {16 16} def\n"
88 " /BlueValues [] def\n"
89 " /password 5839 def\n"
90 " /lenIV -1 def\n"
91 " currentdict end def\n"
92 " currentdict dup /Private get begin\n"
93 " /CharStrings 256 dict begin\n"
94 " /.notdef 4 RD 8b8b0d0e ND\n"
95 " currentdict end put\n"
96 " end\n"
97 "currentdict end dup /FontName get exch definefont pop\n";
99 t1 = HeapAlloc(GetProcessHeap(), 0, sizeof(*t1));
100 t1->emsize = emsize;
102 t1->glyph_sent_size = GLYPH_SENT_INC;
103 t1->glyph_sent = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
104 t1->glyph_sent_size *
105 sizeof(*(t1->glyph_sent)));
107 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(dict) + strlen(ps_name) +
108 100);
110 sprintf(buf, dict, ps_name, t1->emsize, t1->emsize,
111 bbox->left, bbox->bottom, bbox->right, bbox->top);
113 PSDRV_WriteSpool(dev, buf, strlen(buf));
115 HeapFree(GetProcessHeap(), 0, buf);
116 return t1;
120 typedef struct {
121 BYTE *str;
122 int len, max_len;
123 } STR;
125 static STR *str_init(int sz)
127 STR *str = HeapAlloc(GetProcessHeap(), 0, sizeof(*str));
128 str->max_len = sz;
129 str->str = HeapAlloc(GetProcessHeap(), 0, str->max_len);
130 str->len = 0;
131 return str;
134 static void str_free(STR *str)
136 HeapFree(GetProcessHeap(), 0, str->str);
137 HeapFree(GetProcessHeap(), 0, str);
140 static void str_add_byte(STR *str, BYTE b)
142 if(str->len == str->max_len) {
143 str->max_len *= 2;
144 str->str = HeapReAlloc(GetProcessHeap(), 0, str->str, str->max_len);
146 str->str[str->len++] = b;
149 static void str_add_num(STR *str, int num)
151 if(num <= 107 && num >= -107)
152 str_add_byte(str, num + 139);
153 else if(num >= 108 && num <= 1131) {
154 str_add_byte(str, ((num - 108) >> 8) + 247);
155 str_add_byte(str, (num - 108) & 0xff);
156 } else if(num <= -108 && num >= -1131) {
157 num = -num;
158 str_add_byte(str, ((num - 108) >> 8) + 251);
159 str_add_byte(str, (num - 108) & 0xff);
160 } else {
161 str_add_byte(str, 0xff);
162 str_add_byte(str, (num >> 24) & 0xff);
163 str_add_byte(str, (num >> 16) & 0xff);
164 str_add_byte(str, (num >> 8) & 0xff);
165 str_add_byte(str, (num & 0xff));
169 static void str_add_point(STR *str, POINT pt, POINT *curpos)
171 str_add_num(str, pt.x - curpos->x);
172 str_add_num(str, pt.y - curpos->y);
173 *curpos = pt;
176 static void str_add_cmd(STR *str, enum t1_cmds cmd)
178 str_add_byte(str, (BYTE)cmd);
181 static int str_get_bytes(STR *str, BYTE **b)
183 *b = str->str;
184 return str->len;
187 static BOOL get_hmetrics(HDC hdc, DWORD index, short *lsb, WORD *advance)
189 BYTE hhea[36];
190 WORD num_of_long;
191 BYTE buf[4];
193 *lsb = 0;
194 *advance = 0;
196 GetFontData(hdc, MS_MAKE_TAG('h','h','e','a'), 0, hhea, sizeof(hhea));
197 num_of_long = get_be_word(hhea + 34);
199 if(index < num_of_long)
201 if(GetFontData(hdc, MS_MAKE_TAG('h','m','t','x'), index * 4, buf, 4) != 4) return FALSE;
202 *advance = get_be_word(buf);
203 *lsb = (signed short)get_be_word(buf + 2);
205 else
207 if(GetFontData(hdc, MS_MAKE_TAG('h','m','t','x'), (num_of_long - 1) * 4, buf, 2) != 2) return FALSE;
208 *advance = get_be_word(buf);
209 if(GetFontData(hdc, MS_MAKE_TAG('h','m','t','x'), num_of_long * 4 + (index - num_of_long) * 2, buf, 2) != 2) return FALSE;
210 *lsb = (signed short)get_be_word(buf);
213 return TRUE;
216 static BOOL get_glyf_pos(HDC hdc, DWORD index, DWORD *start, DWORD *end)
218 DWORD len;
219 BYTE *head, *loca;
220 WORD loca_format;
221 BOOL ret = FALSE;
223 *start = *end = 0;
225 len = GetFontData(hdc, MS_MAKE_TAG('h','e','a','d'), 0, NULL, 0);
226 if (len == GDI_ERROR) return FALSE;
227 head = HeapAlloc(GetProcessHeap(), 0, len);
228 GetFontData(hdc, MS_MAKE_TAG('h','e','a','d'), 0, head, len);
229 loca_format = get_be_word(head + 50);
231 len = GetFontData(hdc, MS_MAKE_TAG('l','o','c','a'), 0, NULL, 0);
232 if (len == GDI_ERROR)
234 len = GetFontData(hdc, MS_MAKE_TAG('C','F','F',' '), 0, NULL, 0);
235 if (len != GDI_ERROR) FIXME( "CFF tables not supported yet\n" );
236 else ERR( "loca table not found\n" );
237 HeapFree(GetProcessHeap(), 0, head);
238 return FALSE;
240 loca = HeapAlloc(GetProcessHeap(), 0, len);
241 GetFontData(hdc, MS_MAKE_TAG('l','o','c','a'), 0, loca, len);
243 switch(loca_format) {
244 case 0:
245 *start = get_be_word(((WORD*)loca) + index);
246 *start <<= 1;
247 *end = get_be_word(((WORD*)loca) + index + 1);
248 *end <<= 1;
249 ret = TRUE;
250 break;
251 case 1:
252 *start = get_be_dword(((DWORD*)loca) + index);
253 *end = get_be_dword(((DWORD*)loca) + index + 1);
254 ret = TRUE;
255 break;
256 default:
257 ERR("Unknown loca_format %d\n", loca_format);
260 HeapFree(GetProcessHeap(), 0, loca);
261 HeapFree(GetProcessHeap(), 0, head);
262 return ret;
266 static BYTE *get_glyph_data(HDC hdc, DWORD index)
268 DWORD start, end, len;
269 BYTE *data;
271 if(!get_glyf_pos(hdc, index, &start, &end))
272 return NULL;
274 len = end - start;
275 if(!len) return NULL;
277 data = HeapAlloc(GetProcessHeap(), 0, len);
278 if(!data) return NULL;
280 if(GetFontData(hdc, MS_MAKE_TAG('g','l','y','f'), start, data, len) != len)
282 HeapFree(GetProcessHeap(), 0, data);
283 return NULL;
285 return data;
288 typedef struct
290 WORD num_conts;
291 WORD *end_pts; /* size_is(num_conts) */
292 BYTE *flags; /* size_is(end_pts[num_conts - 1] + 1) */
293 POINT *pts; /* size_is(end_pts[num_conts - 1] + 1) */
294 short lsb;
295 WORD advance;
296 } glyph_outline;
298 static inline WORD pts_in_outline(glyph_outline *outline)
300 WORD num_pts = 0;
302 if(outline->num_conts)
303 num_pts = outline->end_pts[outline->num_conts - 1] + 1;
305 return num_pts;
308 static BOOL append_glyph_outline(HDC hdc, DWORD index, glyph_outline *outline);
310 static BOOL append_simple_glyph(BYTE *data, glyph_outline *outline)
312 USHORT num_pts, start_pt = 0, ins_len;
313 WORD *end_pts;
314 int num_conts, start_cont, i;
315 BYTE *ptr;
316 int x, y;
318 start_cont = outline->num_conts;
319 start_pt = pts_in_outline(outline);
321 num_conts = get_be_word(data);
323 end_pts = (WORD*)(data + 10);
324 num_pts = get_be_word(end_pts + num_conts - 1) + 1;
326 ins_len = get_be_word(end_pts + num_conts);
328 ptr = (BYTE*)(end_pts + num_conts) + 2 + ins_len;
330 if(outline->num_conts)
332 outline->end_pts = HeapReAlloc(GetProcessHeap(), 0, outline->end_pts, (start_cont + num_conts) * sizeof(*outline->end_pts));
333 outline->flags = HeapReAlloc(GetProcessHeap(), 0, outline->flags, start_pt + num_pts);
334 outline->pts = HeapReAlloc(GetProcessHeap(), 0, outline->pts, (start_pt + num_pts) * sizeof(*outline->pts));
336 else
338 outline->end_pts = HeapAlloc(GetProcessHeap(), 0, num_conts * sizeof(*outline->end_pts));
339 outline->flags = HeapAlloc(GetProcessHeap(), 0, num_pts);
340 outline->pts = HeapAlloc(GetProcessHeap(), 0, num_pts * sizeof(*outline->pts));
343 outline->num_conts += num_conts;
345 for(i = 0; i < num_conts; i++)
346 outline->end_pts[start_cont + i] = start_pt + get_be_word(end_pts + i);
348 for(i = 0; i < num_pts; i++)
350 outline->flags[start_pt + i] = *ptr;
351 if(*ptr & 8)
353 BYTE count = *++ptr;
354 while(count)
356 i++;
357 outline->flags[start_pt + i] = *(ptr - 1);
358 count--;
361 ptr++;
364 x = 0;
366 for(i = 0; i < num_pts; i++)
368 INT delta;
370 delta = 0;
371 if(outline->flags[start_pt + i] & 2)
373 delta = *ptr++;
374 if((outline->flags[start_pt + i] & 0x10) == 0)
375 delta = -delta;
377 else if((outline->flags[start_pt + i] & 0x10) == 0)
379 delta = (signed short)get_be_word(ptr);
380 ptr += 2;
382 x += delta;
383 outline->pts[start_pt + i].x = x;
386 y = 0;
387 for(i = 0; i < num_pts; i++)
389 INT delta;
391 delta = 0;
392 if(outline->flags[start_pt + i] & 4)
394 delta = *ptr++;
395 if((outline->flags[start_pt + i] & 0x20) == 0)
396 delta = -delta;
398 else if((outline->flags[start_pt + i] & 0x20) == 0)
400 delta = (signed short)get_be_word(ptr);
401 ptr += 2;
403 y += delta;
404 outline->pts[start_pt + i].y = y;
406 return TRUE;
409 /* Some flags for composite glyphs. See glyf table in OT spec */
410 #define ARG_1_AND_2_ARE_WORDS (1L << 0)
411 #define ARGS_ARE_XY_VALUES (1L << 1)
412 #define WE_HAVE_A_SCALE (1L << 3)
413 #define MORE_COMPONENTS (1L << 5)
414 #define WE_HAVE_AN_X_AND_Y_SCALE (1L << 6)
415 #define WE_HAVE_A_TWO_BY_TWO (1L << 7)
416 #define USE_MY_METRICS (1L << 9)
418 static BOOL append_complex_glyph(HDC hdc, const BYTE *data, glyph_outline *outline)
420 const BYTE *ptr = data;
421 WORD flags, index;
422 short arg1, arg2;
423 FLOAT scale_xx = 1, scale_xy = 0, scale_yx = 0, scale_yy = 1;
424 WORD start_pt, end_pt;
426 ptr += 10;
429 flags = get_be_word(ptr);
430 ptr += 2;
431 index = get_be_word(ptr);
432 ptr += 2;
433 if(flags & ARG_1_AND_2_ARE_WORDS)
435 arg1 = (short)get_be_word(ptr);
436 ptr += 2;
437 arg2 = (short)get_be_word(ptr);
438 ptr += 2;
440 else
442 arg1 = *(const char*)ptr++;
443 arg2 = *(const char*)ptr++;
445 if(flags & WE_HAVE_A_SCALE)
447 scale_xx = scale_yy = (FLOAT)(short)get_be_word(ptr) / 0x4000;
448 ptr += 2;
450 else if(flags & WE_HAVE_AN_X_AND_Y_SCALE)
452 scale_xx = (FLOAT)(short)get_be_word(ptr) / 0x4000;
453 ptr += 2;
454 scale_yy = (FLOAT)(short)get_be_word(ptr) / 0x4000;
455 ptr += 2;
457 else if(flags & WE_HAVE_A_TWO_BY_TWO)
459 scale_xx = (FLOAT)(short)get_be_word(ptr) / 0x4000;
460 ptr += 2;
461 scale_xy = (FLOAT)(short)get_be_word(ptr) / 0x4000;
462 ptr += 2;
463 scale_yx = (FLOAT)(short)get_be_word(ptr) / 0x4000;
464 ptr += 2;
465 scale_yy = (FLOAT)(short)get_be_word(ptr) / 0x4000;
466 ptr += 2;
469 start_pt = pts_in_outline(outline);
470 append_glyph_outline(hdc, index, outline);
471 end_pt = pts_in_outline(outline);
473 if (flags & (WE_HAVE_A_SCALE | WE_HAVE_AN_X_AND_Y_SCALE | WE_HAVE_A_TWO_BY_TWO))
475 WORD i;
476 TRACE("transform %f,%f,%f,%f of glyph %x\n", scale_xx, scale_xy, scale_yx, scale_yy, index);
477 for (i = start_pt; i < end_pt; i++)
479 LONG x = outline->pts[i].x, y = outline->pts[i].y;
480 outline->pts[i].x = x * scale_xx + y * scale_yx;
481 outline->pts[i].y = x * scale_xy + y * scale_yy;
485 if((flags & ARGS_ARE_XY_VALUES) == 0)
487 WORD orig_pt = arg1, new_pt = arg2;
488 new_pt += start_pt;
490 if(orig_pt >= start_pt || new_pt >= end_pt) return FALSE;
492 arg1 = outline->pts[orig_pt].x - outline->pts[new_pt].x;
493 arg2 = outline->pts[orig_pt].y - outline->pts[new_pt].y;
495 while(start_pt < end_pt)
497 outline->pts[start_pt].x += arg1;
498 outline->pts[start_pt].y += arg2;
499 start_pt++;
502 if(flags & USE_MY_METRICS)
503 get_hmetrics(hdc, index, &outline->lsb, &outline->advance);
505 } while(flags & MORE_COMPONENTS);
506 return TRUE;
509 static BOOL append_glyph_outline(HDC hdc, DWORD index, glyph_outline *outline)
511 BYTE *glyph_data;
512 SHORT num_conts;
514 glyph_data = get_glyph_data(hdc, index);
515 if(!glyph_data) return TRUE;
517 num_conts = get_be_word(glyph_data);
519 if(num_conts == -1)
520 append_complex_glyph(hdc, glyph_data, outline);
521 else if(num_conts > 0)
522 append_simple_glyph(glyph_data, outline);
524 HeapFree(GetProcessHeap(), 0, glyph_data);
525 return TRUE;
528 static inline BOOL on_point(const glyph_outline *outline, WORD pt)
530 return outline->flags[pt] & 1;
533 BOOL T1_download_glyph(PHYSDEV dev, DOWNLOAD *pdl, DWORD index, char *glyph_name)
535 DWORD len;
536 WORD cur_pt, cont;
537 char *buf;
538 TYPE1 *t1;
539 STR *charstring;
540 BYTE *bytes;
541 POINT curpos;
542 glyph_outline outline;
544 static const char glyph_def_begin[] =
545 "/%s findfont dup\n"
546 "/Private get begin\n"
547 "/CharStrings get begin\n"
548 "/%s %ld RD\n";
549 static const char glyph_def_end[] =
550 "ND\n"
551 "end end\n";
553 TRACE("%ld %s\n", index, glyph_name);
554 assert(pdl->type == Type1);
555 t1 = pdl->typeinfo.Type1;
557 if(index < t1->glyph_sent_size) {
558 if(t1->glyph_sent[index])
559 return TRUE;
560 } else {
561 t1->glyph_sent_size = (index / GLYPH_SENT_INC + 1) * GLYPH_SENT_INC;
562 t1->glyph_sent = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
563 t1->glyph_sent,
564 t1->glyph_sent_size * sizeof(*(t1->glyph_sent)));
567 outline.num_conts = 0;
568 outline.flags = NULL;
569 outline.end_pts = NULL;
570 outline.pts = NULL;
571 get_hmetrics(dev->hdc, index, &outline.lsb, &outline.advance);
573 if(!append_glyph_outline(dev->hdc, index, &outline)) return FALSE;
575 charstring = str_init(100);
576 curpos.x = outline.lsb;
577 curpos.y = 0;
579 str_add_num(charstring, curpos.x);
580 str_add_num(charstring, outline.advance);
581 str_add_cmd(charstring, hsbw);
583 for(cur_pt = 0, cont = 0; cont < outline.num_conts; cont++)
585 POINT start_pos = outline.pts[cur_pt++];
586 WORD end_pt = outline.end_pts[cont];
587 POINT curve_pts[3] = {{0,0},{0,0},{0,0}};
589 str_add_point(charstring, start_pos, &curpos);
590 str_add_cmd(charstring, rmoveto);
592 for(; cur_pt <= end_pt; cur_pt++)
594 if(on_point(&outline, cur_pt))
596 str_add_point(charstring, outline.pts[cur_pt], &curpos);
597 str_add_cmd(charstring, rlineto);
599 else
601 BOOL added_next = FALSE;
603 /* temporarily store the start pt in curve_pts[0] */
604 if(on_point(&outline, cur_pt - 1))
605 curve_pts[0] = outline.pts[cur_pt - 1];
606 else /* last pt was off curve too, so the previous curve's
607 end pt was constructed */
608 curve_pts[0] = curve_pts[2];
610 if(cur_pt != end_pt)
612 if(on_point(&outline, cur_pt + 1))
614 curve_pts[2] = outline.pts[cur_pt + 1];
615 added_next = TRUE;
617 else /* next pt is off curve too, so construct the end pt from the
618 average of the two */
620 curve_pts[2].x = (outline.pts[cur_pt].x + outline.pts[cur_pt + 1].x + 1) / 2;
621 curve_pts[2].y = (outline.pts[cur_pt].y + outline.pts[cur_pt + 1].y + 1) / 2;
624 else /* last pt of the contour is off curve, so the end pt is the
625 start pt of the contour */
626 curve_pts[2] = start_pos;
628 curve_pts[0].x = (curve_pts[0].x + 2 * outline.pts[cur_pt].x + 1) / 3;
629 curve_pts[0].y = (curve_pts[0].y + 2 * outline.pts[cur_pt].y + 1) / 3;
631 curve_pts[1].x = (curve_pts[2].x + 2 * outline.pts[cur_pt].x + 1) / 3;
632 curve_pts[1].y = (curve_pts[2].y + 2 * outline.pts[cur_pt].y + 1) / 3;
634 str_add_point(charstring, curve_pts[0], &curpos);
635 str_add_point(charstring, curve_pts[1], &curpos);
636 str_add_point(charstring, curve_pts[2], &curpos);
637 str_add_cmd(charstring, rrcurveto);
638 if(added_next) cur_pt++;
641 str_add_cmd(charstring, closepath);
643 str_add_cmd(charstring, endchar);
645 HeapFree(GetProcessHeap(), 0, outline.pts);
646 HeapFree(GetProcessHeap(), 0, outline.end_pts);
647 HeapFree(GetProcessHeap(), 0, outline.flags);
649 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(glyph_def_begin) +
650 strlen(pdl->ps_name) + strlen(glyph_name) + 100);
652 sprintf(buf, "%%%%glyph %04lx\n", index);
653 PSDRV_WriteSpool(dev, buf, strlen(buf));
655 len = str_get_bytes(charstring, &bytes);
656 sprintf(buf, glyph_def_begin, pdl->ps_name, glyph_name, len);
657 PSDRV_WriteSpool(dev, buf, strlen(buf));
658 PSDRV_WriteBytes(dev, bytes, len);
659 sprintf(buf, glyph_def_end);
660 PSDRV_WriteSpool(dev, buf, strlen(buf));
661 str_free(charstring);
663 t1->glyph_sent[index] = TRUE;
664 HeapFree(GetProcessHeap(), 0, buf);
665 return TRUE;
668 void T1_free(TYPE1 *t1)
670 HeapFree(GetProcessHeap(), 0, t1->glyph_sent);
671 HeapFree(GetProcessHeap(), 0, t1);
672 return;