blob: 048e5a2ddb0b1f6f5bcf3769d182bb4939ce1591 [file] [log] [blame]
Tom Kacvinskycd92b112001-01-03 00:15:00 +00001/***************************************************************************/
2/* */
3/* cffgload.c */
4/* */
5/* OpenType Glyph Loader (body). */
6/* */
7/* Copyright 1996-2000 by */
8/* David Turner, Robert Wilhelm, and Werner Lemberg. */
9/* */
10/* This file is part of the FreeType project, and may only be used, */
11/* modified, and distributed under the terms of the FreeType project */
12/* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13/* this file you indicate that you have read the license and */
14/* understand and accept it fully. */
15/* */
16/***************************************************************************/
17
18
19#include <ft2build.h>
20#include FT_INTERNAL_DEBUG_H
21#include FT_INTERNAL_CALC_H
22#include FT_INTERNAL_STREAM_H
23#include FT_INTERNAL_SFNT_H
24#include FT_OUTLINE_H
25#include FT_TRUETYPE_TAGS_H
26
David Turner8d3a4012001-03-20 11:14:24 +000027#include "cffload.h"
28#include "cffgload.h"
Tom Kacvinskycd92b112001-01-03 00:15:00 +000029
Werner Lemberg1f7f0e82001-06-06 17:30:41 +000030#include "cfferrs.h"
Tom Kacvinskycd92b112001-01-03 00:15:00 +000031
32
33 /*************************************************************************/
34 /* */
35 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
36 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
37 /* messages during execution. */
38 /* */
39#undef FT_COMPONENT
40#define FT_COMPONENT trace_cffgload
41
42
43 typedef enum CFF_Operator_
44 {
45 cff_op_unknown = 0,
46
47 cff_op_rmoveto,
48 cff_op_hmoveto,
49 cff_op_vmoveto,
50
51 cff_op_rlineto,
52 cff_op_hlineto,
53 cff_op_vlineto,
54
55 cff_op_rrcurveto,
56 cff_op_hhcurveto,
57 cff_op_hvcurveto,
58 cff_op_rcurveline,
59 cff_op_rlinecurve,
60 cff_op_vhcurveto,
61 cff_op_vvcurveto,
62
63 cff_op_flex,
64 cff_op_hflex,
65 cff_op_hflex1,
66 cff_op_flex1,
67
68 cff_op_endchar,
69
70 cff_op_hstem,
71 cff_op_vstem,
72 cff_op_hstemhm,
73 cff_op_vstemhm,
74
75 cff_op_hintmask,
76 cff_op_cntrmask,
David Turner8d3a4012001-03-20 11:14:24 +000077 cff_op_dotsection, /* deprecated, acts as no-op */
Tom Kacvinskycd92b112001-01-03 00:15:00 +000078
79 cff_op_abs,
80 cff_op_add,
81 cff_op_sub,
82 cff_op_div,
83 cff_op_neg,
84 cff_op_random,
85 cff_op_mul,
86 cff_op_sqrt,
87
88 cff_op_blend,
89
90 cff_op_drop,
91 cff_op_exch,
92 cff_op_index,
93 cff_op_roll,
94 cff_op_dup,
95
96 cff_op_put,
97 cff_op_get,
98 cff_op_store,
99 cff_op_load,
100
101 cff_op_and,
102 cff_op_or,
103 cff_op_not,
104 cff_op_eq,
105 cff_op_ifelse,
106
107 cff_op_callsubr,
108 cff_op_callgsubr,
109 cff_op_return,
110
111 /* do not remove */
112 cff_op_max
113
114 } CFF_Operator;
115
116
117#define CFF_COUNT_CHECK_WIDTH 0x80
118#define CFF_COUNT_EXACT 0x40
119#define CFF_COUNT_CLEAR_STACK 0x20
120
121
122 static const FT_Byte cff_argument_counts[] =
123 {
124 0, /* unknown */
125
126 2 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT, /* rmoveto */
127 1 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT,
128 1 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT,
129
130 0 | CFF_COUNT_CLEAR_STACK, /* rlineto */
131 0 | CFF_COUNT_CLEAR_STACK,
132 0 | CFF_COUNT_CLEAR_STACK,
133
134 0 | CFF_COUNT_CLEAR_STACK, /* rrcurveto */
135 0 | CFF_COUNT_CLEAR_STACK,
136 0 | CFF_COUNT_CLEAR_STACK,
137 0 | CFF_COUNT_CLEAR_STACK,
138 0 | CFF_COUNT_CLEAR_STACK,
139 0 | CFF_COUNT_CLEAR_STACK,
140 0 | CFF_COUNT_CLEAR_STACK,
141
142 13, /* flex */
143 7,
144 9,
145 11,
146
147 0 | CFF_COUNT_CHECK_WIDTH, /* endchar */
148
149 2 | CFF_COUNT_CHECK_WIDTH, /* hstem */
150 2 | CFF_COUNT_CHECK_WIDTH,
151 2 | CFF_COUNT_CHECK_WIDTH,
152 2 | CFF_COUNT_CHECK_WIDTH,
153
154 0, /* hintmask */
155 0, /* cntrmask */
Tom Kacvinsky8316bc52001-03-16 13:35:56 +0000156 0, /* dotsection */
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000157
158 1, /* abs */
159 2,
160 2,
161 2,
162 1,
163 0,
164 2,
165 1,
166
167 1, /* blend */
168
169 1, /* drop */
170 2,
171 1,
172 2,
173 1,
174
175 2, /* put */
176 1,
177 4,
178 3,
179
180 2, /* and */
181 2,
182 1,
183 2,
184 4,
185
186 1, /* callsubr */
187 1,
188 0
189 };
190
191
192 /*************************************************************************/
193 /*************************************************************************/
194 /*************************************************************************/
195 /********** *********/
196 /********** *********/
197 /********** GENERIC CHARSTRING PARSING *********/
198 /********** *********/
199 /********** *********/
200 /*************************************************************************/
201 /*************************************************************************/
202 /*************************************************************************/
203
204
205 /*************************************************************************/
206 /* */
207 /* <Function> */
Werner Lembergd573c7e2001-01-03 07:14:12 +0000208 /* CFF_Init_Builder */
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000209 /* */
210 /* <Description> */
211 /* Initializes a given glyph builder. */
212 /* */
213 /* <InOut> */
214 /* builder :: A pointer to the glyph builder to initialize. */
215 /* */
216 /* <Input> */
217 /* face :: The current face object. */
218 /* */
219 /* size :: The current size object. */
220 /* */
221 /* glyph :: The current glyph object. */
222 /* */
223 static
224 void CFF_Init_Builder( CFF_Builder* builder,
Werner Lembergd573c7e2001-01-03 07:14:12 +0000225 TT_Face face,
226 CFF_Size size,
227 CFF_GlyphSlot glyph )
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000228 {
229 builder->path_begun = 0;
230 builder->load_points = 1;
231
232 builder->face = face;
233 builder->glyph = glyph;
234 builder->memory = face->root.memory;
235
236 if ( glyph )
237 {
238 FT_GlyphLoader* loader = glyph->root.internal->loader;
239
240
241 builder->loader = loader;
242 builder->base = &loader->base.outline;
243 builder->current = &loader->current.outline;
244 FT_GlyphLoader_Rewind( loader );
245 }
246
247 if ( size )
248 {
249 builder->scale_x = size->metrics.x_scale;
250 builder->scale_y = size->metrics.y_scale;
251 }
252
253 builder->pos_x = 0;
254 builder->pos_y = 0;
255
256 builder->left_bearing.x = 0;
257 builder->left_bearing.y = 0;
258 builder->advance.x = 0;
259 builder->advance.y = 0;
260 }
261
262
263 /*************************************************************************/
264 /* */
265 /* <Function> */
Werner Lembergd573c7e2001-01-03 07:14:12 +0000266 /* CFF_Done_Builder */
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000267 /* */
268 /* <Description> */
269 /* Finalizes a given glyph builder. Its contents can still be used */
270 /* after the call, but the function saves important information */
271 /* within the corresponding glyph slot. */
272 /* */
273 /* <Input> */
274 /* builder :: A pointer to the glyph builder to finalize. */
275 /* */
276 static
277 void CFF_Done_Builder( CFF_Builder* builder )
278 {
279 CFF_GlyphSlot glyph = builder->glyph;
280
281
282 if ( glyph )
283 glyph->root.outline = *builder->base;
284 }
285
286
287 /*************************************************************************/
288 /* */
289 /* <Function> */
Werner Lembergd573c7e2001-01-03 07:14:12 +0000290 /* cff_compute_bias */
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000291 /* */
292 /* <Description> */
293 /* Computes the bias value in dependence of the number of glyph */
294 /* subroutines. */
295 /* */
296 /* <Input> */
297 /* num_subrs :: The number of glyph subroutines. */
298 /* */
299 /* <Return> */
300 /* The bias value. */
301 static
302 FT_Int cff_compute_bias( FT_UInt num_subrs )
303 {
304 FT_Int result;
305
306
307 if ( num_subrs < 1240 )
308 result = 107;
Werner Lembergcf24d512001-06-18 14:23:45 +0000309 else if ( num_subrs < 33900U )
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000310 result = 1131;
311 else
Werner Lembergcf24d512001-06-18 14:23:45 +0000312 result = 32768U;
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000313
314 return result;
315 }
316
317
318 /*************************************************************************/
319 /* */
320 /* <Function> */
Werner Lembergd573c7e2001-01-03 07:14:12 +0000321 /* CFF_Init_Decoder */
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000322 /* */
323 /* <Description> */
324 /* Initializes a given glyph decoder. */
325 /* */
326 /* <InOut> */
327 /* decoder :: A pointer to the glyph builder to initialize. */
328 /* */
329 /* <Input> */
330 /* face :: The current face object. */
331 /* */
332 /* size :: The current size object. */
333 /* */
334 /* slot :: The current glyph object. */
335 /* */
336 FT_LOCAL_DEF
Werner Lembergd573c7e2001-01-03 07:14:12 +0000337 void CFF_Init_Decoder( CFF_Decoder* decoder,
338 TT_Face face,
339 CFF_Size size,
340 CFF_GlyphSlot slot )
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000341 {
342 CFF_Font* cff = (CFF_Font*)face->extra.data;
343
344
345 /* clear everything */
346 MEM_Set( decoder, 0, sizeof ( *decoder ) );
347
348 /* initialize builder */
349 CFF_Init_Builder( &decoder->builder, face, size, slot );
350
351 /* initialize Type2 decoder */
352 decoder->num_globals = cff->num_global_subrs;
353 decoder->globals = cff->global_subrs;
354 decoder->globals_bias = cff_compute_bias( decoder->num_globals );
355 }
356
357
358 /* this function is used to select the locals subrs array */
359 FT_LOCAL_DEF
360 void CFF_Prepare_Decoder( CFF_Decoder* decoder,
Werner Lembergd573c7e2001-01-03 07:14:12 +0000361 FT_UInt glyph_index )
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000362 {
363 CFF_Font* cff = (CFF_Font*)decoder->builder.face->extra.data;
364 CFF_SubFont* sub = &cff->top_font;
365
366
367 /* manage CID fonts */
368 if ( cff->num_subfonts >= 1 )
369 {
370 FT_Byte fd_index = CFF_Get_FD( &cff->fd_select, glyph_index );
371
372
373 sub = cff->subfonts[fd_index];
374 }
375
376 decoder->num_locals = sub->num_local_subrs;
377 decoder->locals = sub->local_subrs;
378 decoder->locals_bias = cff_compute_bias( decoder->num_locals );
379
380 decoder->glyph_width = sub->private_dict.default_width;
381 decoder->nominal_width = sub->private_dict.nominal_width;
382 }
383
384
385 /* check that there is enough room for `count' more points */
386 static
387 FT_Error check_points( CFF_Builder* builder,
Werner Lembergd573c7e2001-01-03 07:14:12 +0000388 FT_Int count )
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000389 {
390 return FT_GlyphLoader_Check_Points( builder->loader, count, 0 );
391 }
392
393
394 /* add a new point, do not check space */
395 static
396 void add_point( CFF_Builder* builder,
Werner Lembergd573c7e2001-01-03 07:14:12 +0000397 FT_Pos x,
398 FT_Pos y,
399 FT_Byte flag )
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000400 {
401 FT_Outline* outline = builder->current;
402
403
404 if ( builder->load_points )
405 {
406 FT_Vector* point = outline->points + outline->n_points;
407 FT_Byte* control = (FT_Byte*)outline->tags + outline->n_points;
408
409
410 point->x = x >> 16;
411 point->y = y >> 16;
David Turner8edbcab2001-06-19 08:28:24 +0000412 *control = (FT_Byte)(flag ? FT_Curve_Tag_On : FT_Curve_Tag_Cubic);
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000413
414 builder->last = *point;
415 }
416 outline->n_points++;
417 }
418
419
420 /* check space for a new on-curve point, then add it */
421 static
422 FT_Error add_point1( CFF_Builder* builder,
Werner Lembergd573c7e2001-01-03 07:14:12 +0000423 FT_Pos x,
424 FT_Pos y )
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000425 {
426 FT_Error error;
427
428
429 error = check_points( builder, 1 );
430 if ( !error )
431 add_point( builder, x, y, 1 );
432
433 return error;
434 }
435
436
437 /* check room for a new contour, then add it */
438 static
439 FT_Error add_contour( CFF_Builder* builder )
440 {
441 FT_Outline* outline = builder->current;
442 FT_Error error;
443
444
445 if ( !builder->load_points )
446 {
447 outline->n_contours++;
448 return CFF_Err_Ok;
449 }
450
451 error = FT_GlyphLoader_Check_Points( builder->loader, 0, 1 );
452 if ( !error )
453 {
454 if ( outline->n_contours > 0 )
David Turner8edbcab2001-06-19 08:28:24 +0000455 outline->contours[outline->n_contours - 1] = (short)(outline->n_points - 1);
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000456
457 outline->n_contours++;
458 }
459
460 return error;
461 }
462
463
464 /* if a path was begun, add its first on-curve point */
465 static
466 FT_Error start_point( CFF_Builder* builder,
Werner Lembergd573c7e2001-01-03 07:14:12 +0000467 FT_Pos x,
468 FT_Pos y )
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000469 {
470 FT_Error error = 0;
471
472
473 /* test whether we are building a new contour */
474 if ( !builder->path_begun )
475 {
476 builder->path_begun = 1;
477 error = add_contour( builder );
478 if ( !error )
479 error = add_point1( builder, x, y );
480 }
481 return error;
482 }
483
484
485 /* close the current contour */
486 static
487 void close_contour( CFF_Builder* builder )
488 {
489 FT_Outline* outline = builder->current;
490
491 /* XXXX: We must not include the last point in the path if it */
492 /* is located on the first point. */
493 if ( outline->n_points > 1 )
494 {
495 FT_Int first = 0;
496 FT_Vector* p1 = outline->points + first;
497 FT_Vector* p2 = outline->points + outline->n_points - 1;
498 FT_Byte* control = (FT_Byte*)outline->tags + outline->n_points - 1;
499
500
501 if ( outline->n_contours > 1 )
502 {
503 first = outline->contours[outline->n_contours - 2] + 1;
504 p1 = outline->points + first;
505 }
506
507 /* `delete' last point only if it coincides with the first */
508 /* point and it is not a control point (which can happen). */
509 if ( p1->x == p2->x && p1->y == p2->y )
510 if ( *control == FT_Curve_Tag_On )
511 outline->n_points--;
512 }
513
514 if ( outline->n_contours > 0 )
David Turner8edbcab2001-06-19 08:28:24 +0000515 outline->contours[outline->n_contours - 1] = (short)(outline->n_points - 1);
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000516 }
517
518
519 static
520 FT_Int cff_lookup_glyph_by_stdcharcode( CFF_Font* cff,
Werner Lembergd573c7e2001-01-03 07:14:12 +0000521 FT_Int charcode )
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000522 {
523 FT_UInt n;
524 FT_UShort glyph_sid;
525
526
527 /* check range of standard char code */
528 if ( charcode < 0 || charcode > 255 )
529 return -1;
530
531
532 /* Get code to SID mapping from `cff_standard_encoding'. */
533 glyph_sid = cff_standard_encoding[charcode];
534
535 for ( n = 0; n < cff->num_glyphs; n++ )
536 {
537 if ( cff->charset.sids[n] == glyph_sid )
538 return n;
539 }
540
541 return -1;
542 }
543
544
545 static
546 FT_Error cff_operator_seac( CFF_Decoder* decoder,
Werner Lembergd573c7e2001-01-03 07:14:12 +0000547 FT_Pos adx,
548 FT_Pos ady,
549 FT_Int bchar,
550 FT_Int achar )
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000551 {
552 FT_Error error;
553 FT_Int bchar_index, achar_index, n_base_points;
554 FT_Outline* base = decoder->builder.base;
555 TT_Face face = decoder->builder.face;
556 CFF_Font* cff = (CFF_Font*)(face->extra.data);
557 FT_Vector left_bearing, advance;
558 FT_Byte* charstring;
559 FT_ULong charstring_len;
560
561
562 bchar_index = cff_lookup_glyph_by_stdcharcode( cff, bchar );
563 achar_index = cff_lookup_glyph_by_stdcharcode( cff, achar );
564
565 if ( bchar_index < 0 || achar_index < 0 )
566 {
567 FT_ERROR(( "cff_operator_seac:" ));
568 FT_ERROR(( " invalid seac character code arguments\n" ));
569 return CFF_Err_Syntax_Error;
570 }
571
572 /* If we are trying to load a composite glyph, do not load the */
573 /* accent character and return the array of subglyphs. */
574 if ( decoder->builder.no_recurse )
575 {
576 FT_GlyphSlot glyph = (FT_GlyphSlot)decoder->builder.glyph;
577 FT_GlyphLoader* loader = glyph->internal->loader;
578 FT_SubGlyph* subg;
579
580
581 /* reallocate subglyph array if necessary */
582 error = FT_GlyphLoader_Check_Subglyphs( loader, 2 );
583 if ( error )
584 goto Exit;
585
586 subg = loader->current.subglyphs;
587
588 /* subglyph 0 = base character */
589 subg->index = bchar_index;
590 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES |
591 FT_SUBGLYPH_FLAG_USE_MY_METRICS;
592 subg->arg1 = 0;
593 subg->arg2 = 0;
594 subg++;
595
596 /* subglyph 1 = accent character */
597 subg->index = achar_index;
598 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES;
599 subg->arg1 = adx;
600 subg->arg2 = ady;
601
602 /* set up remaining glyph fields */
603 glyph->num_subglyphs = 2;
604 glyph->subglyphs = loader->base.subglyphs;
605 glyph->format = ft_glyph_format_composite;
606
607 loader->current.num_subglyphs = 2;
608 }
609
610 /* First load `bchar' in builder */
611 error = CFF_Access_Element( &cff->charstrings_index, bchar_index,
612 &charstring, &charstring_len );
613 if ( !error )
614 {
615 error = CFF_Parse_CharStrings( decoder, charstring, charstring_len );
616
617 if ( error )
618 goto Exit;
619
620 CFF_Forget_Element( &cff->charstrings_index, &charstring );
621 }
622
623 n_base_points = base->n_points;
624
625 /* Save the left bearing and width of the base character */
626 /* as they will be erased by the next load. */
627
628 left_bearing = decoder->builder.left_bearing;
629 advance = decoder->builder.advance;
630
631 decoder->builder.left_bearing.x = 0;
632 decoder->builder.left_bearing.y = 0;
633
634 /* Now load `achar' on top of the base outline. */
635 error = CFF_Access_Element( &cff->charstrings_index, achar_index,
636 &charstring, &charstring_len );
637 if ( !error )
638 {
639 error = CFF_Parse_CharStrings( decoder, charstring, charstring_len );
640
641 if ( error )
642 goto Exit;
643
644 CFF_Forget_Element( &cff->charstrings_index, &charstring );
645 }
646
647 /* Restore the left side bearing and advance width */
648 /* of the base character. */
649 decoder->builder.left_bearing = left_bearing;
650 decoder->builder.advance = advance;
651
652 /* Finally, move the accent. */
653 if ( decoder->builder.load_points )
654 {
655 FT_Outline dummy;
656
657
David Turner8edbcab2001-06-19 08:28:24 +0000658 dummy.n_points = (short)(base->n_points - n_base_points);
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000659 dummy.points = base->points + n_base_points;
660
661 FT_Outline_Translate( &dummy, adx, ady );
662 }
663
664 Exit:
665 return error;
666 }
667
668
669 /*************************************************************************/
670 /* */
671 /* <Function> */
Werner Lembergd573c7e2001-01-03 07:14:12 +0000672 /* CFF_Parse_CharStrings */
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000673 /* */
674 /* <Description> */
675 /* Parses a given Type 2 charstrings program. */
676 /* */
677 /* <InOut> */
678 /* decoder :: The current Type 1 decoder. */
679 /* */
680 /* <Input> */
681 /* charstring_base :: The base of the charstring stream. */
682 /* */
683 /* charstring_len :: The length in bytes of the charstring stream. */
684 /* */
685 /* <Return> */
686 /* FreeType error code. 0 means success. */
687 /* */
688 FT_LOCAL_DEF
689 FT_Error CFF_Parse_CharStrings( CFF_Decoder* decoder,
Werner Lembergd573c7e2001-01-03 07:14:12 +0000690 FT_Byte* charstring_base,
691 FT_Int charstring_len )
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000692 {
Werner Lembergd573c7e2001-01-03 07:14:12 +0000693 FT_Error error;
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000694 CFF_Decoder_Zone* zone;
Werner Lembergd573c7e2001-01-03 07:14:12 +0000695 FT_Byte* ip;
696 FT_Byte* limit;
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000697 CFF_Builder* builder = &decoder->builder;
Werner Lembergd573c7e2001-01-03 07:14:12 +0000698 FT_Pos x, y;
699 FT_Fixed seed;
700 FT_Fixed* stack;
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000701
702
703 /* set default width */
704 decoder->num_hints = 0;
705 decoder->read_width = 1;
706
707 /* compute random seed from stack address of parameter */
708 seed = (FT_Fixed)(char*)&seed ^
709 (FT_Fixed)(char*)&decoder ^
710 (FT_Fixed)(char*)&charstring_base;
711 seed = ( seed ^ ( seed >> 10 ) ^ ( seed >> 20 ) ) & 0xFFFF;
712 if ( seed == 0 )
713 seed = 0x7384;
714
715 /* initialize the decoder */
716 decoder->top = decoder->stack;
717 decoder->zone = decoder->zones;
718 zone = decoder->zones;
719 stack = decoder->top;
720
721 builder->path_begun = 0;
722
723 zone->base = charstring_base;
724 limit = zone->limit = charstring_base + charstring_len;
725 ip = zone->cursor = zone->base;
726
Werner Lembergcf24d512001-06-18 14:23:45 +0000727 error = CFF_Err_Ok;
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000728
729 x = builder->pos_x;
730 y = builder->pos_y;
731
732 /* now, execute loop */
733 while ( ip < limit )
734 {
735 CFF_Operator op;
Werner Lembergd573c7e2001-01-03 07:14:12 +0000736 FT_Byte v;
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000737
738
739 /********************************************************************/
740 /* */
741 /* Decode operator or operand */
742 /* */
743 v = *ip++;
744 if ( v >= 32 || v == 28 )
745 {
746 FT_Int shift = 16;
747 FT_Int32 val;
748
749
750 /* this is an operand, push it on the stack */
751 if ( v == 28 )
752 {
753 if ( ip + 1 >= limit )
754 goto Syntax_Error;
755 val = (FT_Short)( ( (FT_Short)ip[0] << 8 ) | ip[1] );
756 ip += 2;
757 }
758 else if ( v < 247 )
759 val = (FT_Long)v - 139;
760 else if ( v < 251 )
761 {
762 if ( ip >= limit )
763 goto Syntax_Error;
764 val = ( (FT_Long)v - 247 ) * 256 + *ip++ + 108;
765 }
766 else if ( v < 255 )
767 {
768 if ( ip >= limit )
769 goto Syntax_Error;
770 val = -( (FT_Long)v - 251 ) * 256 - *ip++ - 108;
771 }
772 else
773 {
774 if ( ip + 3 >= limit )
775 goto Syntax_Error;
776 val = ( (FT_Int32)ip[0] << 24 ) |
777 ( (FT_Int32)ip[1] << 16 ) |
778 ( (FT_Int32)ip[2] << 8 ) |
779 ip[3];
780 ip += 4;
781 shift = 0;
782 }
783 if ( decoder->top - stack >= CFF_MAX_OPERANDS )
784 goto Stack_Overflow;
785
786 val <<= shift;
787 *decoder->top++ = val;
788
789#ifdef FT_DEBUG_LEVEL_TRACE
790 if ( !( val & 0xFFFF ) )
791 FT_TRACE4(( " %d", (FT_Int32)( val >> 16 ) ));
792 else
Tom Kacvinsky8316bc52001-03-16 13:35:56 +0000793 FT_TRACE4(( " %.2f", val / 65536.0 ));
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000794#endif
795
796 }
797 else
798 {
799 FT_Fixed* args = decoder->top;
800 FT_Int num_args = args - decoder->stack;
801 FT_Int req_args;
802
803
804 /* find operator */
805 op = cff_op_unknown;
806
807 switch ( v )
808 {
809 case 1:
810 op = cff_op_hstem;
811 break;
812 case 3:
813 op = cff_op_vstem;
814 break;
815 case 4:
816 op = cff_op_vmoveto;
817 break;
818 case 5:
819 op = cff_op_rlineto;
820 break;
821 case 6:
822 op = cff_op_hlineto;
823 break;
824 case 7:
825 op = cff_op_vlineto;
826 break;
827 case 8:
828 op = cff_op_rrcurveto;
829 break;
830 case 10:
831 op = cff_op_callsubr;
832 break;
833 case 11:
834 op = cff_op_return;
835 break;
836 case 12:
837 {
838 if ( ip >= limit )
839 goto Syntax_Error;
840 v = *ip++;
841
842 switch ( v )
843 {
Tom Kacvinsky8316bc52001-03-16 13:35:56 +0000844 case 0:
845 op = cff_op_dotsection;
846 break;
Tom Kacvinskycd92b112001-01-03 00:15:00 +0000847 case 3:
848 op = cff_op_and;
849 break;
850 case 4:
851 op = cff_op_or;
852 break;
853 case 5:
854 op = cff_op_not;
855 break;
856 case 8:
857 op = cff_op_store;
858 break;
859 case 9:
860 op = cff_op_abs;
861 break;
862 case 10:
863 op = cff_op_add;
864 break;
865 case 11:
866 op = cff_op_sub;
867 break;
868 case 12:
869 op = cff_op_div;
870 break;
871 case 13:
872 op = cff_op_load;
873 break;
874 case 14:
875 op = cff_op_neg;
876 break;
877 case 15:
878 op = cff_op_eq;
879 break;
880 case 18:
881 op = cff_op_drop;
882 break;
883 case 20:
884 op = cff_op_put;
885 break;
886 case 21:
887 op = cff_op_get;
888 break;
889 case 22:
890 op = cff_op_ifelse;
891 break;
892 case 23:
893 op = cff_op_random;
894 break;
895 case 24:
896 op = cff_op_mul;
897 break;
898 case 26:
899 op = cff_op_sqrt;
900 break;
901 case 27:
902 op = cff_op_dup;
903 break;
904 case 28:
905 op = cff_op_exch;
906 break;
907 case 29:
908 op = cff_op_index;
909 break;
910 case 30:
911 op = cff_op_roll;
912 break;
913 case 34:
914 op = cff_op_hflex;
915 break;
916 case 35:
917 op = cff_op_flex;
918 break;
919 case 36:
920 op = cff_op_hflex1;
921 break;
922 case 37:
923 op = cff_op_flex1;
924 break;
925 default:
926 /* decrement ip for syntax error message */
927 ip--;
928 }
929 }
930 break;
931 case 14:
932 op = cff_op_endchar;
933 break;
934 case 16:
935 op = cff_op_blend;
936 break;
937 case 18:
938 op = cff_op_hstemhm;
939 break;
940 case 19:
941 op = cff_op_hintmask;
942 break;
943 case 20:
944 op = cff_op_cntrmask;
945 break;
946 case 21:
947 op = cff_op_rmoveto;
948 break;
949 case 22:
950 op = cff_op_hmoveto;
951 break;
952 case 23:
953 op = cff_op_vstemhm;
954 break;
955 case 24:
956 op = cff_op_rcurveline;
957 break;
958 case 25:
959 op = cff_op_rlinecurve;
960 break;
961 case 26:
962 op = cff_op_vvcurveto;
963 break;
964 case 27:
965 op = cff_op_hhcurveto;
966 break;
967 case 29:
968 op = cff_op_callgsubr;
969 break;
970 case 30:
971 op = cff_op_vhcurveto;
972 break;
973 case 31:
974 op = cff_op_hvcurveto;
975 break;
976 default:
977 ;
978 }
979 if ( op == cff_op_unknown )
980 goto Syntax_Error;
981
982 /* check arguments */
983 req_args = cff_argument_counts[op];
984 if ( req_args & CFF_COUNT_CHECK_WIDTH )
985 {
986 args = stack;
987
988 if ( num_args > 0 && decoder->read_width )
989 {
990 /* If `nominal_width' is non-zero, the number is really a */
991 /* difference against `nominal_width'. Else, the number here */
992 /* is truly a width, not a difference against `nominal_width'. */
993 /* If the font does not set `nominal_width', then */
994 /* `nominal_width' defaults to zero, and so we can set */
995 /* `glyph_width' to `nominal_width' plus number on the stack */
996 /* -- for either case. */
997
998 FT_Int set_width_ok;
999
1000
1001 switch ( op )
1002 {
1003 case cff_op_hmoveto:
1004 case cff_op_vmoveto:
1005 set_width_ok = num_args & 2;
1006 break;
1007
1008 case cff_op_hstem:
1009 case cff_op_vstem:
1010 case cff_op_hstemhm:
1011 case cff_op_vstemhm:
1012 case cff_op_rmoveto:
1013 set_width_ok = num_args & 1;
1014 break;
1015
1016 case cff_op_endchar:
1017 /* If there is a width specified for endchar, we either have */
1018 /* 1 argument or 5 arguments. We like to argue. */
1019 set_width_ok = ( ( num_args == 5 ) || ( num_args == 1 ) );
1020 break;
1021
1022 default:
1023 set_width_ok = 0;
1024 break;
1025 }
1026
1027 if ( set_width_ok )
1028 {
1029 decoder->glyph_width = decoder->nominal_width +
1030 ( stack[0] >> 16 );
1031
1032 /* Consumed an argument. */
1033 num_args--;
1034 args++;
1035 }
1036 }
1037
1038 decoder->read_width = 0;
1039 req_args = 0;
1040 }
1041
1042 req_args &= 15;
1043 if ( num_args < req_args )
1044 goto Stack_Underflow;
1045 args -= req_args;
1046 num_args -= req_args;
1047
1048 switch ( op )
1049 {
1050 case cff_op_hstem:
1051 case cff_op_vstem:
1052 case cff_op_hstemhm:
1053 case cff_op_vstemhm:
1054 /* if the number of arguments is not even, the first one */
1055 /* is simply the glyph width, encoded as the difference */
1056 /* to nominalWidthX */
1057 FT_TRACE4(( op == cff_op_hstem ? " hstem" :
1058 op == cff_op_vstem ? " vstem" :
1059 op == cff_op_hstemhm ? " hstemhm" :
Werner Lembergd573c7e2001-01-03 07:14:12 +00001060 " vstemhm" ));
Tom Kacvinskycd92b112001-01-03 00:15:00 +00001061 decoder->num_hints += num_args / 2;
1062 args = stack;
1063 break;
1064
1065 case cff_op_hintmask:
1066 case cff_op_cntrmask:
1067 FT_TRACE4(( op == cff_op_hintmask ? " hintmask"
Werner Lembergd573c7e2001-01-03 07:14:12 +00001068 : " cntrmask" ));
Tom Kacvinskycd92b112001-01-03 00:15:00 +00001069
1070 decoder->num_hints += num_args / 2;
Tom Kacvinsky8316bc52001-03-16 13:35:56 +00001071
1072#ifdef FT_DEBUG_LEVEL_TRACE
1073 {
1074 FT_UInt maskbyte;
1075
1076 FT_TRACE4(( " " ));
1077
Werner Lemberg521a2d72001-03-20 22:58:56 +00001078 for ( maskbyte = 0;
David Turner6d4fbec2001-04-20 08:50:36 +00001079 maskbyte < (FT_UInt)(( decoder->num_hints + 7 ) >> 3);
Werner Lemberg521a2d72001-03-20 22:58:56 +00001080 maskbyte++, ip++ )
Tom Kacvinsky8316bc52001-03-16 13:35:56 +00001081 {
1082 FT_TRACE4(( "%02X", *ip ));
1083 }
1084
1085 }
1086#else
Tom Kacvinskycd92b112001-01-03 00:15:00 +00001087 ip += ( decoder->num_hints + 7 ) >> 3;
Tom Kacvinsky8316bc52001-03-16 13:35:56 +00001088#endif
Tom Kacvinskycd92b112001-01-03 00:15:00 +00001089 if ( ip >= limit )
1090 goto Syntax_Error;
1091 args = stack;
1092 break;
1093
1094 case cff_op_rmoveto:
1095 FT_TRACE4(( " rmoveto" ));
1096
1097 close_contour( builder );
1098 builder->path_begun = 0;
1099 x += args[0];
1100 y += args[1];
1101 args = stack;
1102 break;
1103
1104 case cff_op_vmoveto:
1105 FT_TRACE4(( " vmoveto" ));
1106
1107 close_contour( builder );
1108 builder->path_begun = 0;
1109 y += args[0];
1110 args = stack;
1111 break;
1112
1113 case cff_op_hmoveto:
1114 FT_TRACE4(( " hmoveto" ));
1115
1116 close_contour( builder );
1117 builder->path_begun = 0;
1118 x += args[0];
1119 args = stack;
1120 break;
1121
1122 case cff_op_rlineto:
1123 FT_TRACE4(( " rlineto" ));
1124
1125 if ( start_point ( builder, x, y ) ||
1126 check_points( builder, num_args / 2 ) )
1127 goto Memory_Error;
1128
1129 if ( num_args < 2 || num_args & 1 )
1130 goto Stack_Underflow;
1131
1132 args = stack;
1133 while ( args < decoder->top )
1134 {
1135 x += args[0];
1136 y += args[1];
1137 add_point( builder, x, y, 1 );
1138 args += 2;
1139 }
1140 args = stack;
1141 break;
1142
1143 case cff_op_hlineto:
1144 case cff_op_vlineto:
1145 {
1146 FT_Int phase = ( op == cff_op_hlineto );
1147
1148
1149 FT_TRACE4(( op == cff_op_hlineto ? " hlineto"
Werner Lembergd573c7e2001-01-03 07:14:12 +00001150 : " vlineto" ));
Tom Kacvinskycd92b112001-01-03 00:15:00 +00001151
1152 if ( start_point ( builder, x, y ) ||
1153 check_points( builder, num_args ) )
1154 goto Memory_Error;
1155
1156 args = stack;
1157 while (args < decoder->top )
1158 {
1159 if ( phase )
1160 x += args[0];
1161 else
1162 y += args[0];
1163
1164 if ( add_point1( builder, x, y ) )
1165 goto Memory_Error;
1166
1167 args++;
1168 phase ^= 1;
1169 }
1170 args = stack;
1171 }
1172 break;
1173
1174 case cff_op_rrcurveto:
1175 FT_TRACE4(( " rrcurveto" ));
1176
1177 /* check number of arguments; must be a multiple of 6 */
1178 if ( num_args % 6 != 0 )
1179 goto Stack_Underflow;
1180
1181 if ( start_point ( builder, x, y ) ||
1182 check_points( builder, num_args / 2 ) )
1183 goto Memory_Error;
1184
1185 args = stack;
1186 while ( args < decoder->top )
1187 {
1188 x += args[0];
1189 y += args[1];
1190 add_point( builder, x, y, 0 );
1191 x += args[2];
1192 y += args[3];
1193 add_point( builder, x, y, 0 );
1194 x += args[4];
1195 y += args[5];
1196 add_point( builder, x, y, 1 );
1197 args += 6;
1198 }
1199 args = stack;
1200 break;
1201
1202 case cff_op_vvcurveto:
1203 FT_TRACE4(( " vvcurveto" ));
1204
1205 if ( start_point ( builder, x, y ) )
1206 goto Memory_Error;
1207
1208 args = stack;
1209 if ( num_args & 1 )
1210 {
1211 x += args[0];
1212 args++;
1213 num_args--;
1214 }
1215
1216 if ( num_args % 4 != 0 )
1217 goto Stack_Underflow;
1218
1219 if ( check_points( builder, 3 * ( num_args / 4 ) ) )
1220 goto Memory_Error;
1221
1222 while ( args < decoder->top )
1223 {
1224 y += args[0];
1225 add_point( builder, x, y, 0 );
1226 x += args[1];
1227 y += args[2];
1228 add_point( builder, x, y, 0 );
1229 y += args[3];
1230 add_point( builder, x, y, 1 );
1231 args += 4;
1232 }
1233 args = stack;
1234 break;
1235
1236 case cff_op_hhcurveto:
1237 FT_TRACE4(( " hhcurveto" ));
1238
1239 if ( start_point ( builder, x, y ) )
1240 goto Memory_Error;
1241
1242 args = stack;
1243 if ( num_args & 1 )
1244 {
1245 y += args[0];
1246 args++;
1247 num_args--;
1248 }
1249
1250 if ( num_args % 4 != 0 )
1251 goto Stack_Underflow;
1252
1253 if ( check_points( builder, 3 * ( num_args / 4 ) ) )
1254 goto Memory_Error;
1255
1256 while ( args < decoder->top )
1257 {
1258 x += args[0];
1259 add_point( builder, x, y, 0 );
1260 x += args[1];
1261 y += args[2];
1262 add_point( builder, x, y, 0 );
1263 x += args[3];
1264 add_point( builder, x, y, 1 );
1265 args += 4;
1266 }
1267 args = stack;
1268 break;
1269
1270 case cff_op_vhcurveto:
1271 case cff_op_hvcurveto:
1272 {
1273 FT_Int phase;
1274
1275
1276 FT_TRACE4(( op == cff_op_vhcurveto ? " vhcurveto"
Werner Lembergd573c7e2001-01-03 07:14:12 +00001277 : " hvcurveto" ));
Tom Kacvinskycd92b112001-01-03 00:15:00 +00001278
1279 if ( start_point ( builder, x, y ) )
1280 goto Memory_Error;
1281
1282 args = stack;
1283 if (num_args < 4 || ( num_args % 4 ) > 1 )
1284 goto Stack_Underflow;
1285
1286 if ( check_points( builder, ( num_args / 4 ) * 3 ) )
1287 goto Stack_Underflow;
1288
1289 phase = ( op == cff_op_hvcurveto );
1290
1291 while ( num_args >= 4 )
1292 {
1293 num_args -= 4;
1294 if ( phase )
1295 {
1296 x += args[0];
1297 add_point( builder, x, y, 0 );
1298 x += args[1];
1299 y += args[2];
1300 add_point( builder, x, y, 0 );
1301 y += args[3];
1302 if ( num_args == 1 )
1303 x += args[4];
1304 add_point( builder, x, y, 1 );
1305 }
1306 else
1307 {
1308 y += args[0];
1309 add_point( builder, x, y, 0 );
1310 x += args[1];
1311 y += args[2];
1312 add_point( builder, x, y, 0 );
1313 x += args[3];
1314 if ( num_args == 1 )
1315 y += args[4];
1316 add_point( builder, x, y, 1 );
1317 }
1318 args += 4;
1319 phase ^= 1;
1320 }
1321 args = stack;
1322 }
1323 break;
1324
1325 case cff_op_rlinecurve:
1326 {
1327 FT_Int num_lines = ( num_args - 6 ) / 2;
1328
1329
1330 FT_TRACE4(( " rlinecurve" ));
1331
1332 if ( num_args < 8 || ( num_args - 6 ) & 1 )
1333 goto Stack_Underflow;
1334
1335 if ( start_point( builder, x, y ) ||
1336 check_points( builder, num_lines + 3 ) )
1337 goto Memory_Error;
1338
1339 args = stack;
1340
1341 /* first, add the line segments */
1342 while ( num_lines > 0 )
1343 {
1344 x += args[0];
1345 y += args[1];
1346 add_point( builder, x, y, 1 );
1347 args += 2;
1348 num_lines--;
1349 }
1350
1351 /* then the curve */
1352 x += args[0];
1353 y += args[1];
1354 add_point( builder, x, y, 0 );
1355 x += args[2];
1356 y += args[3];
1357 add_point( builder, x, y, 0 );
1358 x += args[4];
1359 y += args[5];
1360 add_point( builder, x, y, 1 );
1361 args = stack;
1362 }
1363 break;
1364
1365 case cff_op_rcurveline:
1366 {
1367 FT_Int num_curves = ( num_args - 2 ) / 6;
1368
1369
1370 FT_TRACE4(( " rcurveline" ));
1371
1372 if ( num_args < 8 || ( num_args - 2 ) % 6 )
1373 goto Stack_Underflow;
1374
1375 if ( start_point ( builder, x, y ) ||
1376 check_points( builder, num_curves*3 + 2 ) )
1377 goto Memory_Error;
1378
1379 args = stack;
1380
1381 /* first, add the curves */
1382 while ( num_curves > 0 )
1383 {
1384 x += args[0];
1385 y += args[1];
1386 add_point( builder, x, y, 0 );
1387 x += args[2];
1388 y += args[3];
1389 add_point( builder, x, y, 0 );
1390 x += args[4];
1391 y += args[5];
1392 add_point( builder, x, y, 1 );
1393 args += 6;
1394 num_curves--;
1395 }
1396
1397 /* then the final line */
1398 x += args[0];
1399 y += args[1];
1400 add_point( builder, x, y, 1 );
1401 args = stack;
1402 }
1403 break;
1404
1405 case cff_op_hflex1:
1406 {
1407 FT_Pos start_y;
1408
1409
1410 FT_TRACE4(( " hflex1" ));
1411
1412 args = stack;
1413
1414 /* adding five more points; 4 control points, 1 on-curve point */
1415 /* make sure we have enough space for the start point if it */
1416 /* needs to be added.. */
1417 if ( start_point( builder, x, y ) ||
1418 check_points( builder, 6 ) )
1419 goto Memory_Error;
1420
1421 /* Record the starting point's y postion for later use */
1422 start_y = y;
1423
1424 /* first control point */
1425 x += args[0];
1426 y += args[1];
1427 add_point( builder, x, y, 0 );
1428
1429 /* second control point */
1430 x += args[2];
1431 y += args[3];
1432 add_point( builder, x, y, 0 );
1433
1434 /* join point; on curve, with y-value the same as the last */
1435 /* control point's y-value */
1436 x += args[4];
1437 add_point( builder, x, y, 1 );
1438
1439 /* third control point, with y-value the same as the join */
1440 /* point's y-value */
1441 x += args[5];
1442 add_point( builder, x, y, 0 );
1443
1444 /* fourth control point */
1445 x += args[6];
1446 y += args[7];
1447 add_point( builder, x, y, 0 );
1448
1449 /* ending point, with y-value the same as the start */
1450 x += args[8];
1451 y = start_y;
1452 add_point( builder, x, y, 1 );
1453
1454 args = stack;
1455 break;
1456 }
1457
1458 case cff_op_hflex:
1459 {
1460 FT_Pos start_y;
1461
1462
1463 FT_TRACE4(( " hflex" ));
1464
1465 args = stack;
1466
1467 /* adding six more points; 4 control points, 2 on-curve points */
1468 if ( start_point( builder, x, y ) ||
1469 check_points ( builder, 6 ) )
1470 goto Memory_Error;
1471
1472 /* record the starting point's y-position for later use */
1473 start_y = y;
1474
1475 /* first control point */
1476 x += args[0];
1477 add_point( builder, x, y, 0 );
1478
1479 /* second control point */
1480 x += args[1];
1481 y += args[2];
1482 add_point( builder, x, y, 0 );
1483
1484 /* join point; on curve, with y-value the same as the last */
1485 /* control point's y-value */
1486 x += args[3];
1487 add_point( builder, x, y, 1 );
1488
1489 /* third control point, with y-value the same as the join */
1490 /* point's y-value */
1491 x += args[4];
1492 add_point( builder, x, y, 0 );
1493
1494 /* fourth control point */
1495 x += args[5];
1496 y = start_y;
1497 add_point( builder, x, y, 0 );
1498
1499 /* ending point, with y-value the same as the start point's */
1500 /* y-value -- we don't add this point, though */
1501 x += args[6];
1502 add_point( builder, x, y, 1 );
1503
1504 args = stack;
1505 break;
1506 }
1507
1508 case cff_op_flex1:
1509 {
1510 FT_Pos start_x, start_y; /* record start x, y values for alter */
1511 /* use */
1512 FT_Int dx = 0, dy = 0; /* used in horizontal/vertical */
1513 /* algorithm below */
1514 FT_Int horizontal, count;
1515
1516
1517 FT_TRACE4(( " flex1" ));
1518
1519 /* adding six more points; 4 control points, 2 on-curve points */
1520 if ( start_point( builder, x, y ) ||
1521 check_points( builder, 6 ) )
1522 goto Memory_Error;
1523
1524 /* record the starting point's x, y postion for later use */
1525 start_x = x;
1526 start_y = y;
1527
1528 /* XXX: figure out whether this is supposed to be a horizontal */
1529 /* or vertical flex; the Type 2 specification is vague... */
1530
1531 args = stack;
1532
1533 /* grab up to the last argument */
1534 for ( count = 5; count > 0; count-- )
1535 {
1536 dx += args[0];
1537 dy += args[1];
1538 args += 2;
1539 }
1540
1541 /* rewind */
1542 args = stack;
1543
1544 if ( dx < 0 ) dx = -dx;
1545 if ( dy < 0 ) dy = -dy;
1546
1547 /* strange test, but here it is... */
1548 horizontal = ( dx > dy );
1549
1550 for ( count = 5; count > 0; count-- )
1551 {
1552 x += args[0];
1553 y += args[1];
1554 add_point( builder, x, y, (FT_Bool)( count == 3 ) );
1555 args += 2;
1556 }
1557
1558 /* is last operand an x- or y-delta? */
1559 if ( horizontal )
1560 {
1561 x += args[0];
1562 y = start_y;
1563 }
1564 else
1565 {
1566 x = start_x;
1567 y += args[0];
1568 }
1569
1570 add_point( builder, x, y, 1 );
1571
1572 args = stack;
1573 break;
1574 }
1575
1576 case cff_op_flex:
1577 {
1578 FT_UInt count;
1579
1580
1581 FT_TRACE4(( " flex" ));
1582
1583 if ( start_point( builder, x, y ) ||
1584 check_points( builder, 6 ) )
1585 goto Memory_Error;
1586
1587 args = stack;
1588 for ( count = 6; count > 0; count-- )
1589 {
1590 x += args[0];
1591 y += args[1];
1592 add_point( builder, x, y,
1593 (FT_Bool)( count == 3 || count == 0 ) );
1594 args += 2;
1595 }
1596
1597 args = stack;
1598 }
1599 break;
1600
1601 case cff_op_endchar:
1602 FT_TRACE4(( " endchar" ));
1603
1604 /* We are going to emulate the seac operator. */
1605 if ( num_args == 4 )
1606 {
Werner Lembergd573c7e2001-01-03 07:14:12 +00001607 error = cff_operator_seac( decoder,
1608 args[0] >> 16, args[1] >> 16,
1609 args[2] >> 16, args[3] >> 16 );
Tom Kacvinskycd92b112001-01-03 00:15:00 +00001610 args += 4;
1611 }
1612
1613 if ( !error )
1614 error = CFF_Err_Ok;
1615
1616 close_contour( builder );
1617
1618 /* add current outline to the glyph slot */
1619 FT_GlyphLoader_Add( builder->loader );
1620
1621 /* return now! */
1622 FT_TRACE4(( "\n\n" ));
1623 return error;
1624
1625 case cff_op_abs:
1626 FT_TRACE4(( " abs" ));
1627
1628 if ( args[0] < 0 )
1629 args[0] = -args[0];
1630 args++;
1631 break;
1632
1633 case cff_op_add:
1634 FT_TRACE4(( " add" ));
1635
1636 args[0] += args[1];
1637 args++;
1638 break;
1639
1640 case cff_op_sub:
1641 FT_TRACE4(( " sub" ));
1642
1643 args[0] -= args[1];
1644 args++;
1645 break;
1646
1647 case cff_op_div:
1648 FT_TRACE4(( " div" ));
1649
1650 args[0] = FT_DivFix( args[0], args[1] );
1651 args++;
1652 break;
1653
1654 case cff_op_neg:
1655 FT_TRACE4(( " neg" ));
1656
1657 args[0] = -args[0];
1658 args++;
1659 break;
1660
1661 case cff_op_random:
1662 {
1663 FT_Fixed rand;
1664
1665
1666 FT_TRACE4(( " rand" ));
1667
1668 rand = seed;
1669 if ( rand >= 0x8000 )
1670 rand++;
1671
1672 args[0] = rand;
1673 seed = FT_MulFix( seed, 0x10000L - seed );
1674 if ( seed == 0 )
1675 seed += 0x2873;
1676 args++;
1677 }
1678 break;
1679
1680 case cff_op_mul:
1681 FT_TRACE4(( " mul" ));
1682
1683 args[0] = FT_MulFix( args[0], args[1] );
1684 args++;
1685 break;
1686
1687 case cff_op_sqrt:
1688 FT_TRACE4(( " sqrt" ));
1689
1690 if ( args[0] > 0 )
1691 {
1692 FT_Int count = 9;
1693 FT_Fixed root = args[0];
1694 FT_Fixed new_root;
1695
1696
1697 for (;;)
1698 {
Tom Kacvinsky8316bc52001-03-16 13:35:56 +00001699 new_root = ( root + FT_DivFix( args[0], root ) + 1 ) >> 1;
Tom Kacvinskycd92b112001-01-03 00:15:00 +00001700 if ( new_root == root || count <= 0 )
1701 break;
1702 root = new_root;
1703 }
1704 args[0] = new_root;
1705 }
1706 else
1707 args[0] = 0;
1708 args++;
1709 break;
1710
1711 case cff_op_drop:
1712 /* nothing */
1713 FT_TRACE4(( " drop" ));
1714
1715 break;
1716
1717 case cff_op_exch:
1718 {
1719 FT_Fixed tmp;
1720
1721
1722 FT_TRACE4(( " exch" ));
1723
1724 tmp = args[0];
1725 args[0] = args[1];
1726 args[1] = tmp;
1727 args += 2;
1728 }
1729 break;
1730
1731 case cff_op_index:
1732 {
1733 FT_Int index = args[0] >> 16;
1734
1735
1736 FT_TRACE4(( " index" ));
1737
1738 if ( index < 0 )
1739 index = 0;
1740 else if ( index > num_args - 2 )
1741 index = num_args - 2;
1742 args[0] = args[-( index + 1 )];
1743 args++;
1744 }
1745 break;
1746
1747 case cff_op_roll:
1748 {
1749 FT_Int count = (FT_Int)( args[0] >> 16 );
1750 FT_Int index = (FT_Int)( args[1] >> 16 );
1751
1752
1753 FT_TRACE4(( " roll" ));
1754
1755 if ( count <= 0 )
1756 count = 1;
1757
1758 args -= count;
1759 if ( args < stack )
1760 goto Stack_Underflow;
1761
1762 if ( index >= 0 )
1763 {
1764 while ( index > 0 )
1765 {
1766 FT_Fixed tmp = args[count - 1];
1767 FT_Int i;
1768
1769
1770 for ( i = count - 2; i >= 0; i-- )
1771 args[i + 1] = args[i];
1772 args[0] = tmp;
1773 index--;
1774 }
1775 }
1776 else
1777 {
1778 while ( index < 0 )
1779 {
1780 FT_Fixed tmp = args[0];
1781 FT_Int i;
1782
1783
1784 for ( i = 0; i < count - 1; i++ )
1785 args[i] = args[i + 1];
1786 args[count - 1] = tmp;
1787 index++;
1788 }
1789 }
1790 args += count;
1791 }
1792 break;
1793
1794 case cff_op_dup:
1795 FT_TRACE4(( " dup" ));
1796
1797 args[1] = args[0];
1798 args++;
1799 break;
1800
1801 case cff_op_put:
1802 {
1803 FT_Fixed val = args[0];
1804 FT_Int index = (FT_Int)( args[1] >> 16 );
1805
1806
1807 FT_TRACE4(( " put" ));
1808
1809 if ( index >= 0 && index < decoder->len_buildchar )
1810 decoder->buildchar[index] = val;
1811 }
1812 break;
1813
1814 case cff_op_get:
1815 {
1816 FT_Int index = (FT_Int)( args[0] >> 16 );
1817 FT_Fixed val = 0;
1818
1819
1820 FT_TRACE4(( " get" ));
1821
1822 if ( index >= 0 && index < decoder->len_buildchar )
1823 val = decoder->buildchar[index];
1824
1825 args[0] = val;
1826 args++;
1827 }
1828 break;
1829
1830 case cff_op_store:
1831 FT_TRACE4(( " store "));
1832
1833 goto Unimplemented;
1834
1835 case cff_op_load:
1836 FT_TRACE4(( " load" ));
1837
1838 goto Unimplemented;
1839
David Turner8d3a4012001-03-20 11:14:24 +00001840 case cff_op_dotsection:
Werner Lemberg521a2d72001-03-20 22:58:56 +00001841 /* this operator is deprecated and ignored by the parser */
1842 FT_TRACE4(( " dotsection" ));
David Turner8d3a4012001-03-20 11:14:24 +00001843 break;
1844
Tom Kacvinskycd92b112001-01-03 00:15:00 +00001845 case cff_op_and:
1846 {
1847 FT_Fixed cond = args[0] && args[1];
1848
1849
1850 FT_TRACE4(( " and" ));
1851
1852 args[0] = cond ? 0x10000L : 0;
1853 args++;
1854 }
1855 break;
1856
1857 case cff_op_or:
1858 {
1859 FT_Fixed cond = args[0] || args[1];
1860
1861
1862 FT_TRACE4(( " or" ));
1863
1864 args[0] = cond ? 0x10000L : 0;
1865 args++;
1866 }
1867 break;
1868
1869 case cff_op_eq:
1870 {
1871 FT_Fixed cond = !args[0];
1872
1873
1874 FT_TRACE4(( " eq" ));
1875
1876 args[0] = cond ? 0x10000L : 0;
1877 args++;
1878 }
1879 break;
1880
1881 case cff_op_ifelse:
1882 {
1883 FT_Fixed cond = (args[2] <= args[3]);
1884
1885
1886 FT_TRACE4(( " ifelse" ));
1887
1888 if ( !cond )
1889 args[0] = args[1];
1890 args++;
1891 }
1892 break;
1893
1894 case cff_op_callsubr:
1895 {
1896 FT_UInt index = (FT_UInt)( ( args[0] >> 16 ) +
1897 decoder->locals_bias );
1898
1899
1900 FT_TRACE4(( " callsubr(%d)", index ));
1901
1902 if ( index >= decoder->num_locals )
1903 {
1904 FT_ERROR(( "CFF_Parse_CharStrings:" ));
1905 FT_ERROR(( " invalid local subr index\n" ));
1906 goto Syntax_Error;
1907 }
1908
1909 if ( zone - decoder->zones >= CFF_MAX_SUBRS_CALLS )
1910 {
1911 FT_ERROR(( "CFF_Parse_CharStrings: too many nested subrs\n" ));
1912 goto Syntax_Error;
1913 }
1914
1915 zone->cursor = ip; /* save current instruction pointer */
1916
1917 zone++;
1918 zone->base = decoder->locals[index];
Tom Kacvinsky8316bc52001-03-16 13:35:56 +00001919 zone->limit = decoder->locals[index + 1];
Tom Kacvinskycd92b112001-01-03 00:15:00 +00001920 zone->cursor = zone->base;
1921
1922 if ( !zone->base )
1923 {
1924 FT_ERROR(( "CFF_Parse_CharStrings: invoking empty subrs!\n" ));
1925 goto Syntax_Error;
1926 }
1927
1928 decoder->zone = zone;
1929 ip = zone->base;
1930 limit = zone->limit;
1931 }
1932 break;
1933
1934 case cff_op_callgsubr:
1935 {
1936 FT_UInt index = (FT_UInt)( ( args[0] >> 16 ) +
1937 decoder->globals_bias );
1938
1939
1940 FT_TRACE4(( " callgsubr(%d)", index ));
1941
1942 if ( index >= decoder->num_globals )
1943 {
1944 FT_ERROR(( "CFF_Parse_CharStrings:" ));
1945 FT_ERROR(( " invalid global subr index\n" ));
1946 goto Syntax_Error;
1947 }
1948
1949 if ( zone - decoder->zones >= CFF_MAX_SUBRS_CALLS )
1950 {
1951 FT_ERROR(( "CFF_Parse_CharStrings: too many nested subrs\n" ));
1952 goto Syntax_Error;
1953 }
1954
1955 zone->cursor = ip; /* save current instruction pointer */
1956
1957 zone++;
1958 zone->base = decoder->globals[index];
1959 zone->limit = decoder->globals[index+1];
1960 zone->cursor = zone->base;
1961
1962 if ( !zone->base )
1963 {
1964 FT_ERROR(( "CFF_Parse_CharStrings: invoking empty subrs!\n" ));
1965 goto Syntax_Error;
1966 }
1967
1968 decoder->zone = zone;
1969 ip = zone->base;
1970 limit = zone->limit;
1971 }
1972 break;
1973
1974 case cff_op_return:
1975 FT_TRACE4(( " return" ));
1976
1977 if ( decoder->zone <= decoder->zones )
1978 {
1979 FT_ERROR(( "CFF_Parse_CharStrings: unexpected return\n" ));
1980 goto Syntax_Error;
1981 }
1982
1983 decoder->zone--;
1984 zone = decoder->zone;
1985 ip = zone->cursor;
1986 limit = zone->limit;
1987 break;
1988
1989 default:
1990 Unimplemented:
1991 FT_ERROR(( "Unimplemented opcode: %d", ip[-1] ));
1992
1993 if ( ip[-1] == 12 )
1994 FT_ERROR(( " %d", ip[0] ));
1995 FT_ERROR(( "\n" ));
1996
1997 return CFF_Err_Unimplemented_Feature;
1998 }
1999
2000 decoder->top = args;
2001
2002 } /* general operator processing */
2003
2004 } /* while ip < limit */
2005
2006 FT_TRACE4(( "..end..\n\n" ));
2007
2008 return error;
2009
2010 Syntax_Error:
2011 FT_TRACE4(( "CFF_Parse_CharStrings: syntax error!" ));
2012 return CFF_Err_Invalid_File_Format;
2013
2014 Stack_Underflow:
2015 FT_TRACE4(( "CFF_Parse_CharStrings: stack underflow!" ));
2016 return CFF_Err_Too_Few_Arguments;
2017
2018 Stack_Overflow:
2019 FT_TRACE4(( "CFF_Parse_CharStrings: stack overflow!" ));
2020 return CFF_Err_Stack_Overflow;
2021
2022 Memory_Error:
2023 return builder->error;
2024 }
2025
2026
2027 /*************************************************************************/
2028 /*************************************************************************/
2029 /*************************************************************************/
2030 /********** *********/
2031 /********** *********/
2032 /********** COMPUTE THE MAXIMUM ADVANCE WIDTH *********/
2033 /********** *********/
2034 /********** The following code is in charge of computing *********/
2035 /********** the maximum advance width of the font. It *********/
2036 /********** quickly processes each glyph charstring to *********/
2037 /********** extract the value from either a `sbw' or `seac' *********/
2038 /********** operator. *********/
2039 /********** *********/
2040 /*************************************************************************/
2041 /*************************************************************************/
2042 /*************************************************************************/
2043
2044
2045#if 0 /* unused until we support pure CFF fonts */
2046
2047
2048 FT_LOCAL_DEF
2049 FT_Error CFF_Compute_Max_Advance( TT_Face face,
Werner Lembergd573c7e2001-01-03 07:14:12 +00002050 FT_Int* max_advance )
Tom Kacvinskycd92b112001-01-03 00:15:00 +00002051 {
Werner Lembergd573c7e2001-01-03 07:14:12 +00002052 FT_Error error = 0;
Tom Kacvinskycd92b112001-01-03 00:15:00 +00002053 CFF_Decoder decoder;
Werner Lembergd573c7e2001-01-03 07:14:12 +00002054 FT_Int glyph_index;
2055 CFF_Font* cff = (CFF_Font*)face->other;
Tom Kacvinskycd92b112001-01-03 00:15:00 +00002056
2057
2058 *max_advance = 0;
2059
2060 /* Initialize load decoder */
2061 CFF_Init_Decoder( &decoder, face, 0, 0 );
2062
2063 decoder.builder.metrics_only = 1;
2064 decoder.builder.load_points = 0;
2065
2066 /* For each glyph, parse the glyph charstring and extract */
2067 /* the advance width. */
2068 for ( glyph_index = 0; glyph_index < face->root.num_glyphs;
2069 glyph_index++ )
2070 {
2071 FT_Byte* charstring;
2072 FT_ULong charstring_len;
2073
2074
2075 /* now get load the unscaled outline */
2076 error = CFF_Access_Element( &cff->charstrings_index, glyph_index,
Werner Lembergd573c7e2001-01-03 07:14:12 +00002077 &charstring, &charstring_len );
Tom Kacvinskycd92b112001-01-03 00:15:00 +00002078 if ( !error )
2079 {
2080 CFF_Prepare_Decoder( &decoder, glyph_index );
2081 error = CFF_Parse_CharStrings( &decoder, charstring, charstring_len );
2082
2083 CFF_Forget_Element( &cff->charstrings_index, &charstring );
2084 }
2085
2086 /* ignore the error if one has occurred -- skip to next glyph */
2087 error = 0;
2088 }
2089
2090 *max_advance = decoder.builder.advance.x;
2091
2092 return CFF_Err_Ok;
2093 }
2094
2095
2096#endif /* 0 */
2097
2098
2099 /*************************************************************************/
2100 /*************************************************************************/
2101 /*************************************************************************/
2102 /********** *********/
2103 /********** *********/
2104 /********** UNHINTED GLYPH LOADER *********/
2105 /********** *********/
2106 /********** The following code is in charge of loading a *********/
2107 /********** single outline. It completely ignores hinting *********/
2108 /********** and is used when FT_LOAD_NO_HINTING is set. *********/
2109 /********** *********/
2110 /*************************************************************************/
2111 /*************************************************************************/
2112 /*************************************************************************/
2113
2114
2115 FT_LOCAL_DEF
2116 FT_Error CFF_Load_Glyph( CFF_GlyphSlot glyph,
Werner Lembergd573c7e2001-01-03 07:14:12 +00002117 CFF_Size size,
2118 FT_Int glyph_index,
2119 FT_Int load_flags )
Tom Kacvinskycd92b112001-01-03 00:15:00 +00002120 {
Werner Lembergd573c7e2001-01-03 07:14:12 +00002121 FT_Error error;
Tom Kacvinskycd92b112001-01-03 00:15:00 +00002122 CFF_Decoder decoder;
Werner Lembergd573c7e2001-01-03 07:14:12 +00002123 TT_Face face = (TT_Face)glyph->root.face;
2124 FT_Bool hinting;
2125 CFF_Font* cff = (CFF_Font*)face->extra.data;
Tom Kacvinskycd92b112001-01-03 00:15:00 +00002126
Werner Lembergd573c7e2001-01-03 07:14:12 +00002127 FT_Matrix font_matrix;
2128 FT_Vector font_offset;
Tom Kacvinskycd92b112001-01-03 00:15:00 +00002129
2130
2131 if ( load_flags & FT_LOAD_NO_RECURSE )
2132 load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
2133
2134 glyph->x_scale = 0x10000L;
2135 glyph->y_scale = 0x10000L;
2136 if ( size )
2137 {
2138 glyph->x_scale = size->metrics.x_scale;
2139 glyph->y_scale = size->metrics.y_scale;
2140 }
2141
2142 glyph->root.outline.n_points = 0;
2143 glyph->root.outline.n_contours = 0;
2144
David Turner8edbcab2001-06-19 08:28:24 +00002145 hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 &&
2146 ( load_flags & FT_LOAD_NO_HINTING ) == 0 );
Tom Kacvinskycd92b112001-01-03 00:15:00 +00002147
2148 glyph->root.format = ft_glyph_format_outline; /* by default */
2149
2150 {
2151 FT_Byte* charstring;
2152 FT_ULong charstring_len;
2153
2154
2155 CFF_Init_Decoder( &decoder, face, size, glyph );
2156
2157 decoder.builder.no_recurse =
2158 (FT_Bool)( ( load_flags & FT_LOAD_NO_RECURSE ) != 0 );
2159
2160 /* now load the unscaled outline */
2161 error = CFF_Access_Element( &cff->charstrings_index, glyph_index,
2162 &charstring, &charstring_len );
2163 if ( !error )
2164 {
Tom Kacvinskye20035a2001-03-05 16:22:25 +00002165 CFF_Index csindex = cff->charstrings_index;
2166
Werner Lemberg2e35c9b2001-03-06 13:56:42 +00002167
Tom Kacvinskycd92b112001-01-03 00:15:00 +00002168 CFF_Prepare_Decoder( &decoder, glyph_index );
2169 error = CFF_Parse_CharStrings( &decoder, charstring, charstring_len );
2170
2171 CFF_Forget_Element( &cff->charstrings_index, &charstring );
Tom Kacvinskye20035a2001-03-05 16:22:25 +00002172
2173 /* We set control_data and control_len if charstrings is loaded. */
2174 /* See how charstring loads at CFF_Access_Element() in cffload.c. */
2175
Werner Lemberg2e35c9b2001-03-06 13:56:42 +00002176 glyph->root.control_data =
2177 csindex.bytes + csindex.offsets[glyph_index] - 1;
2178 glyph->root.control_len =
2179 charstring_len;
Tom Kacvinskycd92b112001-01-03 00:15:00 +00002180 }
2181
2182 /* save new glyph tables */
2183 CFF_Done_Builder( &decoder.builder );
2184 }
2185
2186 font_matrix = cff->top_font.font_dict.font_matrix;
2187 font_offset = cff->top_font.font_dict.font_offset;
2188
2189 /* Now, set the metrics -- this is rather simple, as */
2190 /* the left side bearing is the xMin, and the top side */
2191 /* bearing the yMax. */
2192 if ( !error )
2193 {
2194 /* For composite glyphs, return only left side bearing and */
2195 /* advance width. */
2196 if ( load_flags & FT_LOAD_NO_RECURSE )
2197 {
2198 FT_Slot_Internal internal = glyph->root.internal;
2199
2200
2201 glyph->root.metrics.horiBearingX = decoder.builder.left_bearing.x;
2202 glyph->root.metrics.horiAdvance = decoder.glyph_width;
2203 internal->glyph_matrix = font_matrix;
2204 internal->glyph_delta = font_offset;
2205 internal->glyph_transformed = 1;
2206 }
2207 else
2208 {
2209 FT_BBox cbox;
2210 FT_Glyph_Metrics* metrics = &glyph->root.metrics;
2211
2212
2213 /* copy the _unscaled_ advance width */
2214 metrics->horiAdvance = decoder.glyph_width;
2215 glyph->root.linearHoriAdvance = decoder.glyph_width;
2216 glyph->root.internal->glyph_transformed = 0;
2217
2218 /* make up vertical metrics */
2219 metrics->vertBearingX = 0;
2220 metrics->vertBearingY = 0;
2221 metrics->vertAdvance = 0;
2222
2223 glyph->root.linearVertAdvance = 0;
2224
2225 glyph->root.format = ft_glyph_format_outline;
2226
2227 glyph->root.outline.flags = 0;
2228 if ( size && size->metrics.y_ppem < 24 )
2229 glyph->root.outline.flags |= ft_outline_high_precision;
2230
2231 glyph->root.outline.flags |= ft_outline_reverse_fill;
2232
2233 /* apply the font matrix */
2234 FT_Outline_Transform( &glyph->root.outline, &font_matrix );
2235
2236 FT_Outline_Translate( &glyph->root.outline,
2237 font_offset.x,
2238 font_offset.y );
2239
2240 if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 )
2241 {
2242 /* scale the outline and the metrics */
2243 FT_Int n;
2244 FT_Outline* cur = &glyph->root.outline;
2245 FT_Vector* vec = cur->points;
2246 FT_Fixed x_scale = glyph->x_scale;
2247 FT_Fixed y_scale = glyph->y_scale;
2248
2249
2250 /* First of all, scale the points */
2251 for ( n = cur->n_points; n > 0; n--, vec++ )
2252 {
2253 vec->x = FT_MulFix( vec->x, x_scale );
2254 vec->y = FT_MulFix( vec->y, y_scale );
2255 }
2256
2257 FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
2258
2259 /* Then scale the metrics */
2260 metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale );
2261 metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale );
2262
2263 metrics->vertBearingX = FT_MulFix( metrics->vertBearingX, x_scale );
2264 metrics->vertBearingY = FT_MulFix( metrics->vertBearingY, y_scale );
2265 }
2266
2267 /* compute the other metrics */
2268 FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
2269
2270 /* grid fit the bounding box if necessary */
2271 if ( hinting )
2272 {
2273 cbox.xMin &= -64;
2274 cbox.yMin &= -64;
2275 cbox.xMax = ( cbox.xMax + 63 ) & -64;
2276 cbox.yMax = ( cbox.yMax + 63 ) & -64;
2277 }
2278
2279 metrics->width = cbox.xMax - cbox.xMin;
2280 metrics->height = cbox.yMax - cbox.yMin;
2281
2282 metrics->horiBearingX = cbox.xMin;
2283 metrics->horiBearingY = cbox.yMax;
2284 }
2285 }
2286
2287 return error;
2288 }
2289
2290
2291/* END */