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
22 #include "wine/port.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(psdrv
);
41 DWORD glyph_sent_size
;
46 #define GLYPH_SENT_INC 128
48 /* Type 1 font commands */
59 #define MS_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
60 ( ( (DWORD)_x4 << 24 ) | \
61 ( (DWORD)_x3 << 16 ) | \
62 ( (DWORD)_x2 << 8 ) | \
65 #ifdef WORDS_BIGENDIAN
66 static inline WORD
get_be_word(const void *p
) { return *(WORD
*)p
; }
67 static inline DWORD
get_be_dword(const void *p
) { return *(DWORD
*)p
; }
69 static inline WORD
get_be_word(const void *p
) { return RtlUshortByteSwap(*(WORD
*)p
); }
70 static inline DWORD
get_be_dword(const void *p
) { return RtlUlongByteSwap(*(DWORD
*)p
); }
73 TYPE1
*T1_download_header(PSDRV_PDEVICE
*physDev
, char *ps_name
, RECT
*bbox
, UINT emsize
)
78 char dict
[] = /* name, emsquare, fontbbox */
80 " /FontName /%s def\n"
81 " /Encoding 256 array 0 1 255{1 index exch /.notdef put} for def\n"
83 " /FontMatrix [1 %d div 0 0 1 %d div 0 0] def\n"
84 " /FontBBox [%d %d %d %d] def\n"
86 " /Private 7 dict begin\n"
87 " /RD {string currentfile exch readhexstring pop} def\n"
90 " /MinFeature {16 16} def\n"
91 " /BlueValues [] def\n"
92 " /password 5839 def\n"
94 " currentdict end def\n"
95 " currentdict dup /Private get begin\n"
96 " /CharStrings 256 dict begin\n"
97 " /.notdef 4 RD 8b8b0d0e ND\n"
98 " currentdict end put\n"
100 "currentdict end dup /FontName get exch definefont pop\n";
102 t1
= HeapAlloc(GetProcessHeap(), 0, sizeof(*t1
));
105 t1
->glyph_sent_size
= GLYPH_SENT_INC
;
106 t1
->glyph_sent
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
107 t1
->glyph_sent_size
*
108 sizeof(*(t1
->glyph_sent
)));
110 buf
= HeapAlloc(GetProcessHeap(), 0, sizeof(dict
) + strlen(ps_name
) +
113 sprintf(buf
, dict
, ps_name
, t1
->emsize
, t1
->emsize
,
114 bbox
->left
, bbox
->bottom
, bbox
->right
, bbox
->top
);
116 PSDRV_WriteSpool(physDev
, buf
, strlen(buf
));
118 HeapFree(GetProcessHeap(), 0, buf
);
128 static STR
*str_init(int sz
)
130 STR
*str
= HeapAlloc(GetProcessHeap(), 0, sizeof(*str
));
132 str
->str
= HeapAlloc(GetProcessHeap(), 0, str
->max_len
);
137 static void str_free(STR
*str
)
139 HeapFree(GetProcessHeap(), 0, str
->str
);
140 HeapFree(GetProcessHeap(), 0, str
);
143 static void str_add_byte(STR
*str
, BYTE b
)
145 if(str
->len
== str
->max_len
) {
147 str
->str
= HeapReAlloc(GetProcessHeap(), 0, str
->str
, str
->max_len
);
149 str
->str
[str
->len
++] = b
;
152 static void str_add_num(STR
*str
, int num
)
154 if(num
<= 107 && num
>= -107)
155 str_add_byte(str
, num
+ 139);
156 else if(num
>= 108 && num
<= 1131) {
157 str_add_byte(str
, ((num
- 108) >> 8) + 247);
158 str_add_byte(str
, (num
- 108) & 0xff);
159 } else if(num
<= -108 && num
>= -1131) {
161 str_add_byte(str
, ((num
- 108) >> 8) + 251);
162 str_add_byte(str
, (num
- 108) & 0xff);
164 str_add_byte(str
, 0xff);
165 str_add_byte(str
, (num
>> 24) & 0xff);
166 str_add_byte(str
, (num
>> 16) & 0xff);
167 str_add_byte(str
, (num
>> 8) & 0xff);
168 str_add_byte(str
, (num
& 0xff));
172 static void str_add_point(STR
*str
, POINT pt
, POINT
*curpos
)
174 str_add_num(str
, pt
.x
- curpos
->x
);
175 str_add_num(str
, pt
.y
- curpos
->y
);
179 static void str_add_cmd(STR
*str
, enum t1_cmds cmd
)
181 str_add_byte(str
, (BYTE
)cmd
);
184 static int str_get_bytes(STR
*str
, BYTE
**b
)
190 static BOOL
get_hmetrics(HDC hdc
, DWORD index
, short *lsb
, WORD
*advance
)
199 GetFontData(hdc
, MS_MAKE_TAG('h','h','e','a'), 0, hhea
, sizeof(hhea
));
200 num_of_long
= get_be_word(hhea
+ 34);
202 if(index
< num_of_long
)
204 if(GetFontData(hdc
, MS_MAKE_TAG('h','m','t','x'), index
* 4, buf
, 4) != 4) return FALSE
;
205 *advance
= get_be_word(buf
);
206 *lsb
= (signed short)get_be_word(buf
+ 2);
210 if(GetFontData(hdc
, MS_MAKE_TAG('h','m','t','x'), (num_of_long
- 1) * 4, buf
, 2) != 2) return FALSE
;
211 *advance
= get_be_word(buf
);
212 if(GetFontData(hdc
, MS_MAKE_TAG('h','m','t','x'), num_of_long
* 4 + (index
- num_of_long
) * 2, buf
, 2) != 2) return FALSE
;
213 *lsb
= (signed short)get_be_word(buf
);
219 static BOOL
get_glyf_pos(HDC hdc
, DWORD index
, DWORD
*start
, DWORD
*end
)
228 len
= GetFontData(hdc
, MS_MAKE_TAG('h','e','a','d'), 0, NULL
, 0);
229 head
= HeapAlloc(GetProcessHeap(), 0, len
);
230 GetFontData(hdc
, MS_MAKE_TAG('h','e','a','d'), 0, head
, len
);
231 loca_format
= get_be_word(head
+ 50);
233 len
= GetFontData(hdc
, MS_MAKE_TAG('l','o','c','a'), 0, NULL
, 0);
234 loca
= HeapAlloc(GetProcessHeap(), 0, len
);
235 GetFontData(hdc
, MS_MAKE_TAG('l','o','c','a'), 0, loca
, len
);
237 switch(loca_format
) {
239 *start
= get_be_word(((WORD
*)loca
) + index
);
241 *end
= get_be_word(((WORD
*)loca
) + index
+ 1);
246 *start
= get_be_dword(((DWORD
*)loca
) + index
);
247 *end
= get_be_dword(((DWORD
*)loca
) + index
+ 1);
251 ERR("Unknown loca_format %d\n", loca_format
);
254 HeapFree(GetProcessHeap(), 0, loca
);
255 HeapFree(GetProcessHeap(), 0, head
);
260 static BYTE
*get_glyph_data(HDC hdc
, DWORD index
)
262 DWORD start
, end
, len
;
265 if(!get_glyf_pos(hdc
, index
, &start
, &end
))
269 if(!len
) return NULL
;
271 data
= HeapAlloc(GetProcessHeap(), 0, len
);
272 if(!data
) return NULL
;
274 if(GetFontData(hdc
, MS_MAKE_TAG('g','l','y','f'), start
, data
, len
) != len
)
276 HeapFree(GetProcessHeap(), 0, data
);
285 WORD
*end_pts
; /* size_is(num_conts) */
286 BYTE
*flags
; /* size_is(end_pts[num_conts - 1] + 1) */
287 POINT
*pts
; /* size_is(end_pts[num_conts - 1] + 1) */
292 static inline WORD
pts_in_outline(glyph_outline
*outline
)
296 if(outline
->num_conts
)
297 num_pts
= outline
->end_pts
[outline
->num_conts
- 1] + 1;
302 static BOOL
append_glyph_outline(HDC hdc
, DWORD index
, glyph_outline
*outline
);
304 static BOOL
append_simple_glyph(BYTE
*data
, glyph_outline
*outline
)
306 USHORT num_pts
, start_pt
= 0, ins_len
;
308 int num_conts
, start_cont
, i
;
312 start_cont
= outline
->num_conts
;
313 start_pt
= pts_in_outline(outline
);
315 num_conts
= get_be_word(data
);
317 end_pts
= (WORD
*)(data
+ 10);
318 num_pts
= get_be_word(end_pts
+ num_conts
- 1) + 1;
320 ins_len
= get_be_word(end_pts
+ num_conts
);
322 ptr
= (BYTE
*)(end_pts
+ num_conts
) + 2 + ins_len
;
324 if(outline
->num_conts
)
326 outline
->end_pts
= HeapReAlloc(GetProcessHeap(), 0, outline
->end_pts
, (start_cont
+ num_conts
) * sizeof(*outline
->end_pts
));
327 outline
->flags
= HeapReAlloc(GetProcessHeap(), 0, outline
->flags
, start_pt
+ num_pts
);
328 outline
->pts
= HeapReAlloc(GetProcessHeap(), 0, outline
->pts
, (start_pt
+ num_pts
) * sizeof(*outline
->pts
));
332 outline
->end_pts
= HeapAlloc(GetProcessHeap(), 0, num_conts
* sizeof(*outline
->end_pts
));
333 outline
->flags
= HeapAlloc(GetProcessHeap(), 0, num_pts
);
334 outline
->pts
= HeapAlloc(GetProcessHeap(), 0, num_pts
* sizeof(*outline
->pts
));
337 outline
->num_conts
+= num_conts
;
339 for(i
= 0; i
< num_conts
; i
++)
340 outline
->end_pts
[start_cont
+ i
] = start_pt
+ get_be_word(end_pts
+ i
);
342 for(i
= 0; i
< num_pts
; i
++)
344 outline
->flags
[start_pt
+ i
] = *ptr
;
351 outline
->flags
[start_pt
+ i
] = *(ptr
- 1);
360 for(i
= 0; i
< num_pts
; i
++)
365 if(outline
->flags
[start_pt
+ i
] & 2)
368 if((outline
->flags
[start_pt
+ i
] & 0x10) == 0)
371 else if((outline
->flags
[start_pt
+ i
] & 0x10) == 0)
373 delta
= (signed short)get_be_word(ptr
);
377 outline
->pts
[start_pt
+ i
].x
= x
;
381 for(i
= 0; i
< num_pts
; i
++)
386 if(outline
->flags
[start_pt
+ i
] & 4)
389 if((outline
->flags
[start_pt
+ i
] & 0x20) == 0)
392 else if((outline
->flags
[start_pt
+ i
] & 0x20) == 0)
394 delta
= (signed short)get_be_word(ptr
);
398 outline
->pts
[start_pt
+ i
].y
= y
;
403 /* Some flags for composite glyphs. See glyf table in OT spec */
404 #define ARG_1_AND_2_ARE_WORDS (1L << 0)
405 #define ARGS_ARE_XY_VALUES (1L << 1)
406 #define WE_HAVE_A_SCALE (1L << 3)
407 #define MORE_COMPONENTS (1L << 5)
408 #define WE_HAVE_AN_X_AND_Y_SCALE (1L << 6)
409 #define WE_HAVE_A_TWO_BY_TWO (1L << 7)
410 #define USE_MY_METRICS (1L << 9)
412 static BOOL
append_complex_glyph(HDC hdc
, const BYTE
*data
, glyph_outline
*outline
)
414 const BYTE
*ptr
= data
;
417 WORD scale_xx
= 1, scale_xy
= 0, scale_yx
= 0, scale_yy
= 1;
418 WORD start_pt
, end_pt
;
423 flags
= get_be_word(ptr
);
425 index
= get_be_word(ptr
);
427 if(flags
& ARG_1_AND_2_ARE_WORDS
)
429 arg1
= (short)get_be_word(ptr
);
431 arg2
= (short)get_be_word(ptr
);
436 arg1
= *(char*)ptr
++;
437 arg2
= *(char*)ptr
++;
439 if(flags
& WE_HAVE_A_SCALE
)
441 scale_xx
= scale_yy
= get_be_word(ptr
);
444 else if(flags
& WE_HAVE_AN_X_AND_Y_SCALE
)
446 scale_xx
= get_be_word(ptr
);
448 scale_yy
= get_be_word(ptr
);
451 else if(flags
& WE_HAVE_A_TWO_BY_TWO
)
453 scale_xx
= get_be_word(ptr
);
455 scale_xy
= get_be_word(ptr
);
457 scale_yx
= get_be_word(ptr
);
459 scale_yy
= get_be_word(ptr
);
463 start_pt
= pts_in_outline(outline
);
464 append_glyph_outline(hdc
, index
, outline
);
465 end_pt
= pts_in_outline(outline
);
467 if((flags
& ARGS_ARE_XY_VALUES
) == 0)
469 WORD orig_pt
= arg1
, new_pt
= arg2
;
472 if(orig_pt
>= start_pt
|| new_pt
>= end_pt
) return FALSE
;
474 arg1
= outline
->pts
[orig_pt
].x
- outline
->pts
[new_pt
].x
;
475 arg2
= outline
->pts
[orig_pt
].y
- outline
->pts
[new_pt
].y
;
477 while(start_pt
< end_pt
)
479 outline
->pts
[start_pt
].x
+= arg1
;
480 outline
->pts
[start_pt
].y
+= arg2
;
484 if(flags
& USE_MY_METRICS
)
485 get_hmetrics(hdc
, index
, &outline
->lsb
, &outline
->advance
);
487 } while(flags
& MORE_COMPONENTS
);
491 static BOOL
append_glyph_outline(HDC hdc
, DWORD index
, glyph_outline
*outline
)
496 glyph_data
= get_glyph_data(hdc
, index
);
497 if(!glyph_data
) return TRUE
;
499 num_conts
= get_be_word(glyph_data
);
502 append_complex_glyph(hdc
, glyph_data
, outline
);
503 else if(num_conts
> 0)
504 append_simple_glyph(glyph_data
, outline
);
506 HeapFree(GetProcessHeap(), 0, glyph_data
);
510 static inline BOOL
on_point(const glyph_outline
*outline
, WORD pt
)
512 return outline
->flags
[pt
] & 1;
515 BOOL
T1_download_glyph(PSDRV_PDEVICE
*physDev
, DOWNLOAD
*pdl
, DWORD index
,
525 glyph_outline outline
;
527 static const char glyph_def_begin
[] =
529 "/Private get begin\n"
530 "/CharStrings get begin\n"
532 static const char glyph_def_end
[] =
536 TRACE("%d %s\n", index
, glyph_name
);
537 assert(pdl
->type
== Type1
);
538 t1
= pdl
->typeinfo
.Type1
;
540 if(index
< t1
->glyph_sent_size
) {
541 if(t1
->glyph_sent
[index
])
544 t1
->glyph_sent_size
= (index
/ GLYPH_SENT_INC
+ 1) * GLYPH_SENT_INC
;
545 t1
->glyph_sent
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
547 t1
->glyph_sent_size
* sizeof(*(t1
->glyph_sent
)));
550 outline
.num_conts
= 0;
551 outline
.flags
= NULL
;
552 outline
.end_pts
= NULL
;
554 get_hmetrics(physDev
->hdc
, index
, &outline
.lsb
, &outline
.advance
);
556 if(!append_glyph_outline(physDev
->hdc
, index
, &outline
)) return FALSE
;
558 charstring
= str_init(100);
559 curpos
.x
= outline
.lsb
;
562 str_add_num(charstring
, curpos
.x
);
563 str_add_num(charstring
, outline
.advance
);
564 str_add_cmd(charstring
, hsbw
);
566 for(cur_pt
= 0, cont
= 0; cont
< outline
.num_conts
; cont
++)
568 POINT start_pos
= outline
.pts
[cur_pt
++];
569 WORD end_pt
= outline
.end_pts
[cont
];
570 POINT curve_pts
[3] = {{0,0},{0,0},{0,0}};
572 str_add_point(charstring
, start_pos
, &curpos
);
573 str_add_cmd(charstring
, rmoveto
);
575 for(; cur_pt
<= end_pt
; cur_pt
++)
577 if(on_point(&outline
, cur_pt
))
579 str_add_point(charstring
, outline
.pts
[cur_pt
], &curpos
);
580 str_add_cmd(charstring
, rlineto
);
584 BOOL added_next
= FALSE
;
586 /* temporarily store the start pt in curve_pts[0] */
587 if(on_point(&outline
, cur_pt
- 1))
588 curve_pts
[0] = outline
.pts
[cur_pt
- 1];
589 else /* last pt was off curve too, so the previous curve's
590 end pt was constructed */
591 curve_pts
[0] = curve_pts
[2];
595 if(on_point(&outline
, cur_pt
+ 1))
597 curve_pts
[2] = outline
.pts
[cur_pt
+ 1];
600 else /* next pt is off curve too, so construct the end pt from the
601 average of the two */
603 curve_pts
[2].x
= (outline
.pts
[cur_pt
].x
+ outline
.pts
[cur_pt
+ 1].x
+ 1) / 2;
604 curve_pts
[2].y
= (outline
.pts
[cur_pt
].y
+ outline
.pts
[cur_pt
+ 1].y
+ 1) / 2;
607 else /* last pt of the contour is off curve, so the end pt is the
608 start pt of the contour */
609 curve_pts
[2] = start_pos
;
611 curve_pts
[0].x
= (curve_pts
[0].x
+ 2 * outline
.pts
[cur_pt
].x
+ 1) / 3;
612 curve_pts
[0].y
= (curve_pts
[0].y
+ 2 * outline
.pts
[cur_pt
].y
+ 1) / 3;
614 curve_pts
[1].x
= (curve_pts
[2].x
+ 2 * outline
.pts
[cur_pt
].x
+ 1) / 3;
615 curve_pts
[1].y
= (curve_pts
[2].y
+ 2 * outline
.pts
[cur_pt
].y
+ 1) / 3;
617 str_add_point(charstring
, curve_pts
[0], &curpos
);
618 str_add_point(charstring
, curve_pts
[1], &curpos
);
619 str_add_point(charstring
, curve_pts
[2], &curpos
);
620 str_add_cmd(charstring
, rrcurveto
);
621 if(added_next
) cur_pt
++;
624 str_add_cmd(charstring
, closepath
);
626 str_add_cmd(charstring
, endchar
);
628 HeapFree(GetProcessHeap(), 0, outline
.pts
);
629 HeapFree(GetProcessHeap(), 0, outline
.end_pts
);
630 HeapFree(GetProcessHeap(), 0, outline
.flags
);
632 buf
= HeapAlloc(GetProcessHeap(), 0, sizeof(glyph_def_begin
) +
633 strlen(pdl
->ps_name
) + strlen(glyph_name
) + 100);
635 sprintf(buf
, "%%%%glyph %04x\n", index
);
636 PSDRV_WriteSpool(physDev
, buf
, strlen(buf
));
638 len
= str_get_bytes(charstring
, &bytes
);
639 sprintf(buf
, glyph_def_begin
, pdl
->ps_name
, glyph_name
, len
);
640 PSDRV_WriteSpool(physDev
, buf
, strlen(buf
));
641 PSDRV_WriteBytes(physDev
, bytes
, len
);
642 sprintf(buf
, glyph_def_end
);
643 PSDRV_WriteSpool(physDev
, buf
, strlen(buf
));
644 str_free(charstring
);
646 t1
->glyph_sent
[index
] = TRUE
;
647 HeapFree(GetProcessHeap(), 0, buf
);
651 void T1_free(TYPE1
*t1
)
653 HeapFree(GetProcessHeap(), 0, t1
->glyph_sent
);
654 HeapFree(GetProcessHeap(), 0, t1
);