sync with trunk r46493
[reactos.git] / lib / 3rdparty / freetype / src / cff / cffgload.c
1 /***************************************************************************/
2 /* */
3 /* cffgload.c */
4 /* */
5 /* OpenType Glyph Loader (body). */
6 /* */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007 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 #include FT_INTERNAL_POSTSCRIPT_HINTS_H
27
28 #include "cffobjs.h"
29 #include "cffload.h"
30 #include "cffgload.h"
31
32 #include "cfferrs.h"
33
34
35 /*************************************************************************/
36 /* */
37 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
38 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
39 /* messages during execution. */
40 /* */
41 #undef FT_COMPONENT
42 #define FT_COMPONENT trace_cffgload
43
44
45 typedef enum CFF_Operator_
46 {
47 cff_op_unknown = 0,
48
49 cff_op_rmoveto,
50 cff_op_hmoveto,
51 cff_op_vmoveto,
52
53 cff_op_rlineto,
54 cff_op_hlineto,
55 cff_op_vlineto,
56
57 cff_op_rrcurveto,
58 cff_op_hhcurveto,
59 cff_op_hvcurveto,
60 cff_op_rcurveline,
61 cff_op_rlinecurve,
62 cff_op_vhcurveto,
63 cff_op_vvcurveto,
64
65 cff_op_flex,
66 cff_op_hflex,
67 cff_op_hflex1,
68 cff_op_flex1,
69
70 cff_op_endchar,
71
72 cff_op_hstem,
73 cff_op_vstem,
74 cff_op_hstemhm,
75 cff_op_vstemhm,
76
77 cff_op_hintmask,
78 cff_op_cntrmask,
79 cff_op_dotsection, /* deprecated, acts as no-op */
80
81 cff_op_abs,
82 cff_op_add,
83 cff_op_sub,
84 cff_op_div,
85 cff_op_neg,
86 cff_op_random,
87 cff_op_mul,
88 cff_op_sqrt,
89
90 cff_op_blend,
91
92 cff_op_drop,
93 cff_op_exch,
94 cff_op_index,
95 cff_op_roll,
96 cff_op_dup,
97
98 cff_op_put,
99 cff_op_get,
100 cff_op_store,
101 cff_op_load,
102
103 cff_op_and,
104 cff_op_or,
105 cff_op_not,
106 cff_op_eq,
107 cff_op_ifelse,
108
109 cff_op_callsubr,
110 cff_op_callgsubr,
111 cff_op_return,
112
113 /* do not remove */
114 cff_op_max
115
116 } CFF_Operator;
117
118
119 #define CFF_COUNT_CHECK_WIDTH 0x80
120 #define CFF_COUNT_EXACT 0x40
121 #define CFF_COUNT_CLEAR_STACK 0x20
122
123
124 static const FT_Byte cff_argument_counts[] =
125 {
126 0, /* unknown */
127
128 2 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT, /* rmoveto */
129 1 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT,
130 1 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT,
131
132 0 | CFF_COUNT_CLEAR_STACK, /* rlineto */
133 0 | CFF_COUNT_CLEAR_STACK,
134 0 | CFF_COUNT_CLEAR_STACK,
135
136 0 | CFF_COUNT_CLEAR_STACK, /* rrcurveto */
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 0 | CFF_COUNT_CLEAR_STACK,
142 0 | CFF_COUNT_CLEAR_STACK,
143
144 13, /* flex */
145 7,
146 9,
147 11,
148
149 0 | CFF_COUNT_CHECK_WIDTH, /* endchar */
150
151 2 | CFF_COUNT_CHECK_WIDTH, /* hstem */
152 2 | CFF_COUNT_CHECK_WIDTH,
153 2 | CFF_COUNT_CHECK_WIDTH,
154 2 | CFF_COUNT_CHECK_WIDTH,
155
156 0 | CFF_COUNT_CHECK_WIDTH, /* hintmask */
157 0 | CFF_COUNT_CHECK_WIDTH, /* cntrmask */
158 0, /* dotsection */
159
160 1, /* abs */
161 2,
162 2,
163 2,
164 1,
165 0,
166 2,
167 1,
168
169 1, /* blend */
170
171 1, /* drop */
172 2,
173 1,
174 2,
175 1,
176
177 2, /* put */
178 1,
179 4,
180 3,
181
182 2, /* and */
183 2,
184 1,
185 2,
186 4,
187
188 1, /* callsubr */
189 1,
190 0
191 };
192
193
194 /*************************************************************************/
195 /*************************************************************************/
196 /*************************************************************************/
197 /********** *********/
198 /********** *********/
199 /********** GENERIC CHARSTRING PARSING *********/
200 /********** *********/
201 /********** *********/
202 /*************************************************************************/
203 /*************************************************************************/
204 /*************************************************************************/
205
206
207 /*************************************************************************/
208 /* */
209 /* <Function> */
210 /* cff_builder_init */
211 /* */
212 /* <Description> */
213 /* Initializes a given glyph builder. */
214 /* */
215 /* <InOut> */
216 /* builder :: A pointer to the glyph builder to initialize. */
217 /* */
218 /* <Input> */
219 /* face :: The current face object. */
220 /* */
221 /* size :: The current size object. */
222 /* */
223 /* glyph :: The current glyph object. */
224 /* */
225 static void
226 cff_builder_init( CFF_Builder* builder,
227 TT_Face face,
228 CFF_Size size,
229 CFF_GlyphSlot glyph,
230 FT_Bool hinting )
231 {
232 builder->path_begun = 0;
233 builder->load_points = 1;
234
235 builder->face = face;
236 builder->glyph = glyph;
237 builder->memory = face->root.memory;
238
239 if ( glyph )
240 {
241 FT_GlyphLoader loader = glyph->root.internal->loader;
242
243
244 builder->loader = loader;
245 builder->base = &loader->base.outline;
246 builder->current = &loader->current.outline;
247 FT_GlyphLoader_Rewind( loader );
248
249 builder->hints_globals = 0;
250 builder->hints_funcs = 0;
251
252 if ( hinting && size )
253 {
254 builder->hints_globals = size->root.internal;
255 builder->hints_funcs = glyph->root.internal->glyph_hints;
256 }
257 }
258
259 if ( size )
260 {
261 builder->scale_x = size->root.metrics.x_scale;
262 builder->scale_y = size->root.metrics.y_scale;
263 }
264
265 builder->pos_x = 0;
266 builder->pos_y = 0;
267
268 builder->left_bearing.x = 0;
269 builder->left_bearing.y = 0;
270 builder->advance.x = 0;
271 builder->advance.y = 0;
272 }
273
274
275 /*************************************************************************/
276 /* */
277 /* <Function> */
278 /* cff_builder_done */
279 /* */
280 /* <Description> */
281 /* Finalizes a given glyph builder. Its contents can still be used */
282 /* after the call, but the function saves important information */
283 /* within the corresponding glyph slot. */
284 /* */
285 /* <Input> */
286 /* builder :: A pointer to the glyph builder to finalize. */
287 /* */
288 static void
289 cff_builder_done( CFF_Builder* builder )
290 {
291 CFF_GlyphSlot glyph = builder->glyph;
292
293
294 if ( glyph )
295 glyph->root.outline = *builder->base;
296 }
297
298
299 /*************************************************************************/
300 /* */
301 /* <Function> */
302 /* cff_compute_bias */
303 /* */
304 /* <Description> */
305 /* Computes the bias value in dependence of the number of glyph */
306 /* subroutines. */
307 /* */
308 /* <Input> */
309 /* num_subrs :: The number of glyph subroutines. */
310 /* */
311 /* <Return> */
312 /* The bias value. */
313 static FT_Int
314 cff_compute_bias( FT_UInt num_subrs )
315 {
316 FT_Int result;
317
318
319 if ( num_subrs < 1240 )
320 result = 107;
321 else if ( num_subrs < 33900U )
322 result = 1131;
323 else
324 result = 32768U;
325
326 return result;
327 }
328
329
330 /*************************************************************************/
331 /* */
332 /* <Function> */
333 /* cff_decoder_init */
334 /* */
335 /* <Description> */
336 /* Initializes a given glyph decoder. */
337 /* */
338 /* <InOut> */
339 /* decoder :: A pointer to the glyph builder to initialize. */
340 /* */
341 /* <Input> */
342 /* face :: The current face object. */
343 /* */
344 /* size :: The current size object. */
345 /* */
346 /* slot :: The current glyph object. */
347 /* */
348 FT_LOCAL_DEF( void )
349 cff_decoder_init( CFF_Decoder* decoder,
350 TT_Face face,
351 CFF_Size size,
352 CFF_GlyphSlot slot,
353 FT_Bool hinting,
354 FT_Render_Mode hint_mode )
355 {
356 CFF_Font cff = (CFF_Font)face->extra.data;
357
358
359 /* clear everything */
360 FT_MEM_ZERO( decoder, sizeof ( *decoder ) );
361
362 /* initialize builder */
363 cff_builder_init( &decoder->builder, face, size, slot, hinting );
364
365 /* initialize Type2 decoder */
366 decoder->num_globals = cff->num_global_subrs;
367 decoder->globals = cff->global_subrs;
368 decoder->globals_bias = cff_compute_bias( decoder->num_globals );
369
370 decoder->hint_mode = hint_mode;
371 }
372
373
374 /* this function is used to select the locals subrs array */
375 FT_LOCAL_DEF( FT_Error )
376 cff_decoder_prepare( CFF_Decoder* decoder,
377 FT_UInt glyph_index )
378 {
379 CFF_Font cff = (CFF_Font)decoder->builder.face->extra.data;
380 CFF_SubFont sub = &cff->top_font;
381 FT_Error error = CFF_Err_Ok;
382
383
384 /* manage CID fonts */
385 if ( cff->num_subfonts >= 1 )
386 {
387 FT_Byte fd_index = cff_fd_select_get( &cff->fd_select, glyph_index );
388
389
390 if ( fd_index >= cff->num_subfonts )
391 {
392 FT_TRACE4(( "cff_decoder_prepare: invalid CID subfont index\n" ));
393 error = CFF_Err_Invalid_File_Format;
394 goto Exit;
395 }
396
397 sub = cff->subfonts[fd_index];
398 }
399
400 decoder->num_locals = sub->num_local_subrs;
401 decoder->locals = sub->local_subrs;
402 decoder->locals_bias = cff_compute_bias( decoder->num_locals );
403
404 decoder->glyph_width = sub->private_dict.default_width;
405 decoder->nominal_width = sub->private_dict.nominal_width;
406
407 Exit:
408 return error;
409 }
410
411
412 /* check that there is enough space for `count' more points */
413 static FT_Error
414 check_points( CFF_Builder* builder,
415 FT_Int count )
416 {
417 return FT_GLYPHLOADER_CHECK_POINTS( builder->loader, count, 0 );
418 }
419
420
421 /* add a new point, do not check space */
422 static void
423 cff_builder_add_point( CFF_Builder* builder,
424 FT_Pos x,
425 FT_Pos y,
426 FT_Byte flag )
427 {
428 FT_Outline* outline = builder->current;
429
430
431 if ( builder->load_points )
432 {
433 FT_Vector* point = outline->points + outline->n_points;
434 FT_Byte* control = (FT_Byte*)outline->tags + outline->n_points;
435
436
437 point->x = x >> 16;
438 point->y = y >> 16;
439 *control = (FT_Byte)( flag ? FT_CURVE_TAG_ON : FT_CURVE_TAG_CUBIC );
440
441 builder->last = *point;
442 }
443
444 outline->n_points++;
445 }
446
447
448 /* check space for a new on-curve point, then add it */
449 static FT_Error
450 cff_builder_add_point1( CFF_Builder* builder,
451 FT_Pos x,
452 FT_Pos y )
453 {
454 FT_Error error;
455
456
457 error = check_points( builder, 1 );
458 if ( !error )
459 cff_builder_add_point( builder, x, y, 1 );
460
461 return error;
462 }
463
464
465 /* check space for a new contour, then add it */
466 static FT_Error
467 cff_builder_add_contour( CFF_Builder* builder )
468 {
469 FT_Outline* outline = builder->current;
470 FT_Error error;
471
472
473 if ( !builder->load_points )
474 {
475 outline->n_contours++;
476 return CFF_Err_Ok;
477 }
478
479 error = FT_GLYPHLOADER_CHECK_POINTS( builder->loader, 0, 1 );
480 if ( !error )
481 {
482 if ( outline->n_contours > 0 )
483 outline->contours[outline->n_contours - 1] =
484 (short)( outline->n_points - 1 );
485
486 outline->n_contours++;
487 }
488
489 return error;
490 }
491
492
493 /* if a path was begun, add its first on-curve point */
494 static FT_Error
495 cff_builder_start_point( CFF_Builder* builder,
496 FT_Pos x,
497 FT_Pos y )
498 {
499 FT_Error error = CFF_Err_Ok;
500
501
502 /* test whether we are building a new contour */
503 if ( !builder->path_begun )
504 {
505 builder->path_begun = 1;
506 error = cff_builder_add_contour( builder );
507 if ( !error )
508 error = cff_builder_add_point1( builder, x, y );
509 }
510
511 return error;
512 }
513
514
515 /* close the current contour */
516 static void
517 cff_builder_close_contour( CFF_Builder* builder )
518 {
519 FT_Outline* outline = builder->current;
520
521
522 if ( !outline )
523 return;
524
525 /* XXXX: We must not include the last point in the path if it */
526 /* is located on the first point. */
527 if ( outline->n_points > 1 )
528 {
529 FT_Int first = 0;
530 FT_Vector* p1 = outline->points + first;
531 FT_Vector* p2 = outline->points + outline->n_points - 1;
532 FT_Byte* control = (FT_Byte*)outline->tags + outline->n_points - 1;
533
534
535 if ( outline->n_contours > 1 )
536 {
537 first = outline->contours[outline->n_contours - 2] + 1;
538 p1 = outline->points + first;
539 }
540
541 /* `delete' last point only if it coincides with the first */
542 /* point and if it is not a control point (which can happen). */
543 if ( p1->x == p2->x && p1->y == p2->y )
544 if ( *control == FT_CURVE_TAG_ON )
545 outline->n_points--;
546 }
547
548 if ( outline->n_contours > 0 )
549 outline->contours[outline->n_contours - 1] =
550 (short)( outline->n_points - 1 );
551 }
552
553
554 static FT_Int
555 cff_lookup_glyph_by_stdcharcode( CFF_Font cff,
556 FT_Int charcode )
557 {
558 FT_UInt n;
559 FT_UShort glyph_sid;
560
561
562 /* CID-keyed fonts don't have glyph names */
563 if ( !cff->charset.sids )
564 return -1;
565
566 /* check range of standard char code */
567 if ( charcode < 0 || charcode > 255 )
568 return -1;
569
570 /* Get code to SID mapping from `cff_standard_encoding'. */
571 glyph_sid = cff_get_standard_encoding( (FT_UInt)charcode );
572
573 for ( n = 0; n < cff->num_glyphs; n++ )
574 {
575 if ( cff->charset.sids[n] == glyph_sid )
576 return n;
577 }
578
579 return -1;
580 }
581
582
583 static FT_Error
584 cff_get_glyph_data( TT_Face face,
585 FT_UInt glyph_index,
586 FT_Byte** pointer,
587 FT_ULong* length )
588 {
589 #ifdef FT_CONFIG_OPTION_INCREMENTAL
590 /* For incremental fonts get the character data using the */
591 /* callback function. */
592 if ( face->root.internal->incremental_interface )
593 {
594 FT_Data data;
595 FT_Error error =
596 face->root.internal->incremental_interface->funcs->get_glyph_data(
597 face->root.internal->incremental_interface->object,
598 glyph_index, &data );
599
600
601 *pointer = (FT_Byte*)data.pointer;
602 *length = data.length;
603
604 return error;
605 }
606 else
607 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
608
609 {
610 CFF_Font cff = (CFF_Font)(face->extra.data);
611
612
613 return cff_index_access_element( &cff->charstrings_index, glyph_index,
614 pointer, length );
615 }
616 }
617
618
619 static void
620 cff_free_glyph_data( TT_Face face,
621 FT_Byte** pointer,
622 FT_ULong length )
623 {
624 #ifndef FT_CONFIG_OPTION_INCREMENTAL
625 FT_UNUSED( length );
626 #endif
627
628 #ifdef FT_CONFIG_OPTION_INCREMENTAL
629 /* For incremental fonts get the character data using the */
630 /* callback function. */
631 if ( face->root.internal->incremental_interface )
632 {
633 FT_Data data;
634
635
636 data.pointer = *pointer;
637 data.length = length;
638
639 face->root.internal->incremental_interface->funcs->free_glyph_data(
640 face->root.internal->incremental_interface->object,&data );
641 }
642 else
643 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
644
645 {
646 CFF_Font cff = (CFF_Font)(face->extra.data);
647
648
649 cff_index_forget_element( &cff->charstrings_index, pointer );
650 }
651 }
652
653
654 static FT_Error
655 cff_operator_seac( CFF_Decoder* decoder,
656 FT_Pos adx,
657 FT_Pos ady,
658 FT_Int bchar,
659 FT_Int achar )
660 {
661 FT_Error error;
662 CFF_Builder* builder = &decoder->builder;
663 FT_Int bchar_index, achar_index;
664 TT_Face face = decoder->builder.face;
665 FT_Vector left_bearing, advance;
666 FT_Byte* charstring;
667 FT_ULong charstring_len;
668
669
670 #ifdef FT_CONFIG_OPTION_INCREMENTAL
671 /* Incremental fonts don't necessarily have valid charsets. */
672 /* They use the character code, not the glyph index, in this case. */
673 if ( face->root.internal->incremental_interface )
674 {
675 bchar_index = bchar;
676 achar_index = achar;
677 }
678 else
679 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
680 {
681 CFF_Font cff = (CFF_Font)(face->extra.data);
682
683
684 bchar_index = cff_lookup_glyph_by_stdcharcode( cff, bchar );
685 achar_index = cff_lookup_glyph_by_stdcharcode( cff, achar );
686 }
687
688 if ( bchar_index < 0 || achar_index < 0 )
689 {
690 FT_ERROR(( "cff_operator_seac:" ));
691 FT_ERROR(( " invalid seac character code arguments\n" ));
692 return CFF_Err_Syntax_Error;
693 }
694
695 /* If we are trying to load a composite glyph, do not load the */
696 /* accent character and return the array of subglyphs. */
697 if ( builder->no_recurse )
698 {
699 FT_GlyphSlot glyph = (FT_GlyphSlot)builder->glyph;
700 FT_GlyphLoader loader = glyph->internal->loader;
701 FT_SubGlyph subg;
702
703
704 /* reallocate subglyph array if necessary */
705 error = FT_GlyphLoader_CheckSubGlyphs( loader, 2 );
706 if ( error )
707 goto Exit;
708
709 subg = loader->current.subglyphs;
710
711 /* subglyph 0 = base character */
712 subg->index = bchar_index;
713 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES |
714 FT_SUBGLYPH_FLAG_USE_MY_METRICS;
715 subg->arg1 = 0;
716 subg->arg2 = 0;
717 subg++;
718
719 /* subglyph 1 = accent character */
720 subg->index = achar_index;
721 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES;
722 subg->arg1 = (FT_Int)( adx >> 16 );
723 subg->arg2 = (FT_Int)( ady >> 16 );
724
725 /* set up remaining glyph fields */
726 glyph->num_subglyphs = 2;
727 glyph->subglyphs = loader->base.subglyphs;
728 glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
729
730 loader->current.num_subglyphs = 2;
731 }
732
733 FT_GlyphLoader_Prepare( builder->loader );
734
735 /* First load `bchar' in builder */
736 error = cff_get_glyph_data( face, bchar_index,
737 &charstring, &charstring_len );
738 if ( !error )
739 {
740 error = cff_decoder_parse_charstrings( decoder, charstring,
741 charstring_len );
742
743 if ( error )
744 goto Exit;
745
746 cff_free_glyph_data( face, &charstring, charstring_len );
747 }
748
749 /* Save the left bearing and width of the base character */
750 /* as they will be erased by the next load. */
751
752 left_bearing = builder->left_bearing;
753 advance = builder->advance;
754
755 builder->left_bearing.x = 0;
756 builder->left_bearing.y = 0;
757
758 builder->pos_x = adx;
759 builder->pos_y = ady;
760
761 /* Now load `achar' on top of the base outline. */
762 error = cff_get_glyph_data( face, achar_index,
763 &charstring, &charstring_len );
764 if ( !error )
765 {
766 error = cff_decoder_parse_charstrings( decoder, charstring,
767 charstring_len );
768
769 if ( error )
770 goto Exit;
771
772 cff_free_glyph_data( face, &charstring, charstring_len );
773 }
774
775 /* Restore the left side bearing and advance width */
776 /* of the base character. */
777 builder->left_bearing = left_bearing;
778 builder->advance = advance;
779
780 builder->pos_x = 0;
781 builder->pos_y = 0;
782
783 Exit:
784 return error;
785 }
786
787
788 /*************************************************************************/
789 /* */
790 /* <Function> */
791 /* cff_decoder_parse_charstrings */
792 /* */
793 /* <Description> */
794 /* Parses a given Type 2 charstrings program. */
795 /* */
796 /* <InOut> */
797 /* decoder :: The current Type 1 decoder. */
798 /* */
799 /* <Input> */
800 /* charstring_base :: The base of the charstring stream. */
801 /* */
802 /* charstring_len :: The length in bytes of the charstring stream. */
803 /* */
804 /* <Return> */
805 /* FreeType error code. 0 means success. */
806 /* */
807 FT_LOCAL_DEF( FT_Error )
808 cff_decoder_parse_charstrings( CFF_Decoder* decoder,
809 FT_Byte* charstring_base,
810 FT_ULong charstring_len )
811 {
812 FT_Error error;
813 CFF_Decoder_Zone* zone;
814 FT_Byte* ip;
815 FT_Byte* limit;
816 CFF_Builder* builder = &decoder->builder;
817 FT_Pos x, y;
818 FT_Fixed seed;
819 FT_Fixed* stack;
820
821 T2_Hints_Funcs hinter;
822
823
824 /* set default width */
825 decoder->num_hints = 0;
826 decoder->read_width = 1;
827
828 /* compute random seed from stack address of parameter */
829 seed = (FT_Fixed)(char*)&seed ^
830 (FT_Fixed)(char*)&decoder ^
831 (FT_Fixed)(char*)&charstring_base;
832 seed = ( seed ^ ( seed >> 10 ) ^ ( seed >> 20 ) ) & 0xFFFFL;
833 if ( seed == 0 )
834 seed = 0x7384;
835
836 /* initialize the decoder */
837 decoder->top = decoder->stack;
838 decoder->zone = decoder->zones;
839 zone = decoder->zones;
840 stack = decoder->top;
841
842 hinter = (T2_Hints_Funcs)builder->hints_funcs;
843
844 builder->path_begun = 0;
845
846 zone->base = charstring_base;
847 limit = zone->limit = charstring_base + charstring_len;
848 ip = zone->cursor = zone->base;
849
850 error = CFF_Err_Ok;
851
852 x = builder->pos_x;
853 y = builder->pos_y;
854
855 /* begin hints recording session, if any */
856 if ( hinter )
857 hinter->open( hinter->hints );
858
859 /* now execute loop */
860 while ( ip < limit )
861 {
862 CFF_Operator op;
863 FT_Byte v;
864
865
866 /********************************************************************/
867 /* */
868 /* Decode operator or operand */
869 /* */
870 v = *ip++;
871 if ( v >= 32 || v == 28 )
872 {
873 FT_Int shift = 16;
874 FT_Int32 val;
875
876
877 /* this is an operand, push it on the stack */
878 if ( v == 28 )
879 {
880 if ( ip + 1 >= limit )
881 goto Syntax_Error;
882 val = (FT_Short)( ( (FT_Short)ip[0] << 8 ) | ip[1] );
883 ip += 2;
884 }
885 else if ( v < 247 )
886 val = (FT_Long)v - 139;
887 else if ( v < 251 )
888 {
889 if ( ip >= limit )
890 goto Syntax_Error;
891 val = ( (FT_Long)v - 247 ) * 256 + *ip++ + 108;
892 }
893 else if ( v < 255 )
894 {
895 if ( ip >= limit )
896 goto Syntax_Error;
897 val = -( (FT_Long)v - 251 ) * 256 - *ip++ - 108;
898 }
899 else
900 {
901 if ( ip + 3 >= limit )
902 goto Syntax_Error;
903 val = ( (FT_Int32)ip[0] << 24 ) |
904 ( (FT_Int32)ip[1] << 16 ) |
905 ( (FT_Int32)ip[2] << 8 ) |
906 ip[3];
907 ip += 4;
908 shift = 0;
909 }
910 if ( decoder->top - stack >= CFF_MAX_OPERANDS )
911 goto Stack_Overflow;
912
913 val <<= shift;
914 *decoder->top++ = val;
915
916 #ifdef FT_DEBUG_LEVEL_TRACE
917 if ( !( val & 0xFFFFL ) )
918 FT_TRACE4(( " %ld", (FT_Int32)( val >> 16 ) ));
919 else
920 FT_TRACE4(( " %.2f", val / 65536.0 ));
921 #endif
922
923 }
924 else
925 {
926 FT_Fixed* args = decoder->top;
927 FT_Int num_args = (FT_Int)( args - decoder->stack );
928 FT_Int req_args;
929
930
931 /* find operator */
932 op = cff_op_unknown;
933
934 switch ( v )
935 {
936 case 1:
937 op = cff_op_hstem;
938 break;
939 case 3:
940 op = cff_op_vstem;
941 break;
942 case 4:
943 op = cff_op_vmoveto;
944 break;
945 case 5:
946 op = cff_op_rlineto;
947 break;
948 case 6:
949 op = cff_op_hlineto;
950 break;
951 case 7:
952 op = cff_op_vlineto;
953 break;
954 case 8:
955 op = cff_op_rrcurveto;
956 break;
957 case 10:
958 op = cff_op_callsubr;
959 break;
960 case 11:
961 op = cff_op_return;
962 break;
963 case 12:
964 {
965 if ( ip >= limit )
966 goto Syntax_Error;
967 v = *ip++;
968
969 switch ( v )
970 {
971 case 0:
972 op = cff_op_dotsection;
973 break;
974 case 3:
975 op = cff_op_and;
976 break;
977 case 4:
978 op = cff_op_or;
979 break;
980 case 5:
981 op = cff_op_not;
982 break;
983 case 8:
984 op = cff_op_store;
985 break;
986 case 9:
987 op = cff_op_abs;
988 break;
989 case 10:
990 op = cff_op_add;
991 break;
992 case 11:
993 op = cff_op_sub;
994 break;
995 case 12:
996 op = cff_op_div;
997 break;
998 case 13:
999 op = cff_op_load;
1000 break;
1001 case 14:
1002 op = cff_op_neg;
1003 break;
1004 case 15:
1005 op = cff_op_eq;
1006 break;
1007 case 18:
1008 op = cff_op_drop;
1009 break;
1010 case 20:
1011 op = cff_op_put;
1012 break;
1013 case 21:
1014 op = cff_op_get;
1015 break;
1016 case 22:
1017 op = cff_op_ifelse;
1018 break;
1019 case 23:
1020 op = cff_op_random;
1021 break;
1022 case 24:
1023 op = cff_op_mul;
1024 break;
1025 case 26:
1026 op = cff_op_sqrt;
1027 break;
1028 case 27:
1029 op = cff_op_dup;
1030 break;
1031 case 28:
1032 op = cff_op_exch;
1033 break;
1034 case 29:
1035 op = cff_op_index;
1036 break;
1037 case 30:
1038 op = cff_op_roll;
1039 break;
1040 case 34:
1041 op = cff_op_hflex;
1042 break;
1043 case 35:
1044 op = cff_op_flex;
1045 break;
1046 case 36:
1047 op = cff_op_hflex1;
1048 break;
1049 case 37:
1050 op = cff_op_flex1;
1051 break;
1052 default:
1053 /* decrement ip for syntax error message */
1054 ip--;
1055 }
1056 }
1057 break;
1058 case 14:
1059 op = cff_op_endchar;
1060 break;
1061 case 16:
1062 op = cff_op_blend;
1063 break;
1064 case 18:
1065 op = cff_op_hstemhm;
1066 break;
1067 case 19:
1068 op = cff_op_hintmask;
1069 break;
1070 case 20:
1071 op = cff_op_cntrmask;
1072 break;
1073 case 21:
1074 op = cff_op_rmoveto;
1075 break;
1076 case 22:
1077 op = cff_op_hmoveto;
1078 break;
1079 case 23:
1080 op = cff_op_vstemhm;
1081 break;
1082 case 24:
1083 op = cff_op_rcurveline;
1084 break;
1085 case 25:
1086 op = cff_op_rlinecurve;
1087 break;
1088 case 26:
1089 op = cff_op_vvcurveto;
1090 break;
1091 case 27:
1092 op = cff_op_hhcurveto;
1093 break;
1094 case 29:
1095 op = cff_op_callgsubr;
1096 break;
1097 case 30:
1098 op = cff_op_vhcurveto;
1099 break;
1100 case 31:
1101 op = cff_op_hvcurveto;
1102 break;
1103 default:
1104 ;
1105 }
1106 if ( op == cff_op_unknown )
1107 goto Syntax_Error;
1108
1109 /* check arguments */
1110 req_args = cff_argument_counts[op];
1111 if ( req_args & CFF_COUNT_CHECK_WIDTH )
1112 {
1113 args = stack;
1114
1115 if ( num_args > 0 && decoder->read_width )
1116 {
1117 /* If `nominal_width' is non-zero, the number is really a */
1118 /* difference against `nominal_width'. Else, the number here */
1119 /* is truly a width, not a difference against `nominal_width'. */
1120 /* If the font does not set `nominal_width', then */
1121 /* `nominal_width' defaults to zero, and so we can set */
1122 /* `glyph_width' to `nominal_width' plus number on the stack */
1123 /* -- for either case. */
1124
1125 FT_Int set_width_ok;
1126
1127
1128 switch ( op )
1129 {
1130 case cff_op_hmoveto:
1131 case cff_op_vmoveto:
1132 set_width_ok = num_args & 2;
1133 break;
1134
1135 case cff_op_hstem:
1136 case cff_op_vstem:
1137 case cff_op_hstemhm:
1138 case cff_op_vstemhm:
1139 case cff_op_rmoveto:
1140 case cff_op_hintmask:
1141 case cff_op_cntrmask:
1142 set_width_ok = num_args & 1;
1143 break;
1144
1145 case cff_op_endchar:
1146 /* If there is a width specified for endchar, we either have */
1147 /* 1 argument or 5 arguments. We like to argue. */
1148 set_width_ok = ( ( num_args == 5 ) || ( num_args == 1 ) );
1149 break;
1150
1151 default:
1152 set_width_ok = 0;
1153 break;
1154 }
1155
1156 if ( set_width_ok )
1157 {
1158 decoder->glyph_width = decoder->nominal_width +
1159 ( stack[0] >> 16 );
1160
1161 /* Consumed an argument. */
1162 num_args--;
1163 args++;
1164 }
1165 }
1166
1167 decoder->read_width = 0;
1168 req_args = 0;
1169 }
1170
1171 req_args &= 15;
1172 if ( num_args < req_args )
1173 goto Stack_Underflow;
1174 args -= req_args;
1175 num_args -= req_args;
1176
1177 switch ( op )
1178 {
1179 case cff_op_hstem:
1180 case cff_op_vstem:
1181 case cff_op_hstemhm:
1182 case cff_op_vstemhm:
1183 /* the number of arguments is always even here */
1184 FT_TRACE4(( op == cff_op_hstem ? " hstem" :
1185 ( op == cff_op_vstem ? " vstem" :
1186 ( op == cff_op_hstemhm ? " hstemhm" : " vstemhm" ) ) ));
1187
1188 if ( hinter )
1189 hinter->stems( hinter->hints,
1190 ( op == cff_op_hstem || op == cff_op_hstemhm ),
1191 num_args / 2,
1192 args );
1193
1194 decoder->num_hints += num_args / 2;
1195 args = stack;
1196 break;
1197
1198 case cff_op_hintmask:
1199 case cff_op_cntrmask:
1200 FT_TRACE4(( op == cff_op_hintmask ? " hintmask" : " cntrmask" ));
1201
1202 /* implement vstem when needed -- */
1203 /* the specification doesn't say it, but this also works */
1204 /* with the 'cntrmask' operator */
1205 /* */
1206 if ( num_args > 0 )
1207 {
1208 if ( hinter )
1209 hinter->stems( hinter->hints,
1210 0,
1211 num_args / 2,
1212 args );
1213
1214 decoder->num_hints += num_args / 2;
1215 }
1216
1217 if ( hinter )
1218 {
1219 if ( op == cff_op_hintmask )
1220 hinter->hintmask( hinter->hints,
1221 builder->current->n_points,
1222 decoder->num_hints,
1223 ip );
1224 else
1225 hinter->counter( hinter->hints,
1226 decoder->num_hints,
1227 ip );
1228 }
1229
1230 #ifdef FT_DEBUG_LEVEL_TRACE
1231 {
1232 FT_UInt maskbyte;
1233
1234
1235 FT_TRACE4(( " " ));
1236
1237 for ( maskbyte = 0;
1238 maskbyte < (FT_UInt)(( decoder->num_hints + 7 ) >> 3);
1239 maskbyte++, ip++ )
1240 FT_TRACE4(( "0x%02X", *ip ));
1241 }
1242 #else
1243 ip += ( decoder->num_hints + 7 ) >> 3;
1244 #endif
1245 if ( ip >= limit )
1246 goto Syntax_Error;
1247 args = stack;
1248 break;
1249
1250 case cff_op_rmoveto:
1251 FT_TRACE4(( " rmoveto" ));
1252
1253 cff_builder_close_contour( builder );
1254 builder->path_begun = 0;
1255 x += args[0];
1256 y += args[1];
1257 args = stack;
1258 break;
1259
1260 case cff_op_vmoveto:
1261 FT_TRACE4(( " vmoveto" ));
1262
1263 cff_builder_close_contour( builder );
1264 builder->path_begun = 0;
1265 y += args[0];
1266 args = stack;
1267 break;
1268
1269 case cff_op_hmoveto:
1270 FT_TRACE4(( " hmoveto" ));
1271
1272 cff_builder_close_contour( builder );
1273 builder->path_begun = 0;
1274 x += args[0];
1275 args = stack;
1276 break;
1277
1278 case cff_op_rlineto:
1279 FT_TRACE4(( " rlineto" ));
1280
1281 if ( cff_builder_start_point ( builder, x, y ) ||
1282 check_points( builder, num_args / 2 ) )
1283 goto Fail;
1284
1285 if ( num_args < 2 || num_args & 1 )
1286 goto Stack_Underflow;
1287
1288 args = stack;
1289 while ( args < decoder->top )
1290 {
1291 x += args[0];
1292 y += args[1];
1293 cff_builder_add_point( builder, x, y, 1 );
1294 args += 2;
1295 }
1296 args = stack;
1297 break;
1298
1299 case cff_op_hlineto:
1300 case cff_op_vlineto:
1301 {
1302 FT_Int phase = ( op == cff_op_hlineto );
1303
1304
1305 FT_TRACE4(( op == cff_op_hlineto ? " hlineto"
1306 : " vlineto" ));
1307
1308 if ( cff_builder_start_point ( builder, x, y ) ||
1309 check_points( builder, num_args ) )
1310 goto Fail;
1311
1312 args = stack;
1313 while ( args < decoder->top )
1314 {
1315 if ( phase )
1316 x += args[0];
1317 else
1318 y += args[0];
1319
1320 if ( cff_builder_add_point1( builder, x, y ) )
1321 goto Fail;
1322
1323 args++;
1324 phase ^= 1;
1325 }
1326 args = stack;
1327 }
1328 break;
1329
1330 case cff_op_rrcurveto:
1331 FT_TRACE4(( " rrcurveto" ));
1332
1333 /* check number of arguments; must be a multiple of 6 */
1334 if ( num_args % 6 != 0 )
1335 goto Stack_Underflow;
1336
1337 if ( cff_builder_start_point ( builder, x, y ) ||
1338 check_points( builder, num_args / 2 ) )
1339 goto Fail;
1340
1341 args = stack;
1342 while ( args < decoder->top )
1343 {
1344 x += args[0];
1345 y += args[1];
1346 cff_builder_add_point( builder, x, y, 0 );
1347 x += args[2];
1348 y += args[3];
1349 cff_builder_add_point( builder, x, y, 0 );
1350 x += args[4];
1351 y += args[5];
1352 cff_builder_add_point( builder, x, y, 1 );
1353 args += 6;
1354 }
1355 args = stack;
1356 break;
1357
1358 case cff_op_vvcurveto:
1359 FT_TRACE4(( " vvcurveto" ));
1360
1361 if ( cff_builder_start_point( builder, x, y ) )
1362 goto Fail;
1363
1364 args = stack;
1365 if ( num_args & 1 )
1366 {
1367 x += args[0];
1368 args++;
1369 num_args--;
1370 }
1371
1372 if ( num_args % 4 != 0 )
1373 goto Stack_Underflow;
1374
1375 if ( check_points( builder, 3 * ( num_args / 4 ) ) )
1376 goto Fail;
1377
1378 while ( args < decoder->top )
1379 {
1380 y += args[0];
1381 cff_builder_add_point( builder, x, y, 0 );
1382 x += args[1];
1383 y += args[2];
1384 cff_builder_add_point( builder, x, y, 0 );
1385 y += args[3];
1386 cff_builder_add_point( builder, x, y, 1 );
1387 args += 4;
1388 }
1389 args = stack;
1390 break;
1391
1392 case cff_op_hhcurveto:
1393 FT_TRACE4(( " hhcurveto" ));
1394
1395 if ( cff_builder_start_point( builder, x, y ) )
1396 goto Fail;
1397
1398 args = stack;
1399 if ( num_args & 1 )
1400 {
1401 y += args[0];
1402 args++;
1403 num_args--;
1404 }
1405
1406 if ( num_args % 4 != 0 )
1407 goto Stack_Underflow;
1408
1409 if ( check_points( builder, 3 * ( num_args / 4 ) ) )
1410 goto Fail;
1411
1412 while ( args < decoder->top )
1413 {
1414 x += args[0];
1415 cff_builder_add_point( builder, x, y, 0 );
1416 x += args[1];
1417 y += args[2];
1418 cff_builder_add_point( builder, x, y, 0 );
1419 x += args[3];
1420 cff_builder_add_point( builder, x, y, 1 );
1421 args += 4;
1422 }
1423 args = stack;
1424 break;
1425
1426 case cff_op_vhcurveto:
1427 case cff_op_hvcurveto:
1428 {
1429 FT_Int phase;
1430
1431
1432 FT_TRACE4(( op == cff_op_vhcurveto ? " vhcurveto"
1433 : " hvcurveto" ));
1434
1435 if ( cff_builder_start_point( builder, x, y ) )
1436 goto Fail;
1437
1438 args = stack;
1439 if ( num_args < 4 || ( num_args % 4 ) > 1 )
1440 goto Stack_Underflow;
1441
1442 if ( check_points( builder, ( num_args / 4 ) * 3 ) )
1443 goto Stack_Underflow;
1444
1445 phase = ( op == cff_op_hvcurveto );
1446
1447 while ( num_args >= 4 )
1448 {
1449 num_args -= 4;
1450 if ( phase )
1451 {
1452 x += args[0];
1453 cff_builder_add_point( builder, x, y, 0 );
1454 x += args[1];
1455 y += args[2];
1456 cff_builder_add_point( builder, x, y, 0 );
1457 y += args[3];
1458 if ( num_args == 1 )
1459 x += args[4];
1460 cff_builder_add_point( builder, x, y, 1 );
1461 }
1462 else
1463 {
1464 y += args[0];
1465 cff_builder_add_point( builder, x, y, 0 );
1466 x += args[1];
1467 y += args[2];
1468 cff_builder_add_point( builder, x, y, 0 );
1469 x += args[3];
1470 if ( num_args == 1 )
1471 y += args[4];
1472 cff_builder_add_point( builder, x, y, 1 );
1473 }
1474 args += 4;
1475 phase ^= 1;
1476 }
1477 args = stack;
1478 }
1479 break;
1480
1481 case cff_op_rlinecurve:
1482 {
1483 FT_Int num_lines = ( num_args - 6 ) / 2;
1484
1485
1486 FT_TRACE4(( " rlinecurve" ));
1487
1488 if ( num_args < 8 || ( num_args - 6 ) & 1 )
1489 goto Stack_Underflow;
1490
1491 if ( cff_builder_start_point( builder, x, y ) ||
1492 check_points( builder, num_lines + 3 ) )
1493 goto Fail;
1494
1495 args = stack;
1496
1497 /* first, add the line segments */
1498 while ( num_lines > 0 )
1499 {
1500 x += args[0];
1501 y += args[1];
1502 cff_builder_add_point( builder, x, y, 1 );
1503 args += 2;
1504 num_lines--;
1505 }
1506
1507 /* then the curve */
1508 x += args[0];
1509 y += args[1];
1510 cff_builder_add_point( builder, x, y, 0 );
1511 x += args[2];
1512 y += args[3];
1513 cff_builder_add_point( builder, x, y, 0 );
1514 x += args[4];
1515 y += args[5];
1516 cff_builder_add_point( builder, x, y, 1 );
1517 args = stack;
1518 }
1519 break;
1520
1521 case cff_op_rcurveline:
1522 {
1523 FT_Int num_curves = ( num_args - 2 ) / 6;
1524
1525
1526 FT_TRACE4(( " rcurveline" ));
1527
1528 if ( num_args < 8 || ( num_args - 2 ) % 6 )
1529 goto Stack_Underflow;
1530
1531 if ( cff_builder_start_point ( builder, x, y ) ||
1532 check_points( builder, num_curves*3 + 2 ) )
1533 goto Fail;
1534
1535 args = stack;
1536
1537 /* first, add the curves */
1538 while ( num_curves > 0 )
1539 {
1540 x += args[0];
1541 y += args[1];
1542 cff_builder_add_point( builder, x, y, 0 );
1543 x += args[2];
1544 y += args[3];
1545 cff_builder_add_point( builder, x, y, 0 );
1546 x += args[4];
1547 y += args[5];
1548 cff_builder_add_point( builder, x, y, 1 );
1549 args += 6;
1550 num_curves--;
1551 }
1552
1553 /* then the final line */
1554 x += args[0];
1555 y += args[1];
1556 cff_builder_add_point( builder, x, y, 1 );
1557 args = stack;
1558 }
1559 break;
1560
1561 case cff_op_hflex1:
1562 {
1563 FT_Pos start_y;
1564
1565
1566 FT_TRACE4(( " hflex1" ));
1567
1568 args = stack;
1569
1570 /* adding five more points; 4 control points, 1 on-curve point */
1571 /* make sure we have enough space for the start point if it */
1572 /* needs to be added */
1573 if ( cff_builder_start_point( builder, x, y ) ||
1574 check_points( builder, 6 ) )
1575 goto Fail;
1576
1577 /* Record the starting point's y position for later use */
1578 start_y = y;
1579
1580 /* first control point */
1581 x += args[0];
1582 y += args[1];
1583 cff_builder_add_point( builder, x, y, 0 );
1584
1585 /* second control point */
1586 x += args[2];
1587 y += args[3];
1588 cff_builder_add_point( builder, x, y, 0 );
1589
1590 /* join point; on curve, with y-value the same as the last */
1591 /* control point's y-value */
1592 x += args[4];
1593 cff_builder_add_point( builder, x, y, 1 );
1594
1595 /* third control point, with y-value the same as the join */
1596 /* point's y-value */
1597 x += args[5];
1598 cff_builder_add_point( builder, x, y, 0 );
1599
1600 /* fourth control point */
1601 x += args[6];
1602 y += args[7];
1603 cff_builder_add_point( builder, x, y, 0 );
1604
1605 /* ending point, with y-value the same as the start */
1606 x += args[8];
1607 y = start_y;
1608 cff_builder_add_point( builder, x, y, 1 );
1609
1610 args = stack;
1611 break;
1612 }
1613
1614 case cff_op_hflex:
1615 {
1616 FT_Pos start_y;
1617
1618
1619 FT_TRACE4(( " hflex" ));
1620
1621 args = stack;
1622
1623 /* adding six more points; 4 control points, 2 on-curve points */
1624 if ( cff_builder_start_point( builder, x, y ) ||
1625 check_points( builder, 6 ) )
1626 goto Fail;
1627
1628 /* record the starting point's y-position for later use */
1629 start_y = y;
1630
1631 /* first control point */
1632 x += args[0];
1633 cff_builder_add_point( builder, x, y, 0 );
1634
1635 /* second control point */
1636 x += args[1];
1637 y += args[2];
1638 cff_builder_add_point( builder, x, y, 0 );
1639
1640 /* join point; on curve, with y-value the same as the last */
1641 /* control point's y-value */
1642 x += args[3];
1643 cff_builder_add_point( builder, x, y, 1 );
1644
1645 /* third control point, with y-value the same as the join */
1646 /* point's y-value */
1647 x += args[4];
1648 cff_builder_add_point( builder, x, y, 0 );
1649
1650 /* fourth control point */
1651 x += args[5];
1652 y = start_y;
1653 cff_builder_add_point( builder, x, y, 0 );
1654
1655 /* ending point, with y-value the same as the start point's */
1656 /* y-value -- we don't add this point, though */
1657 x += args[6];
1658 cff_builder_add_point( builder, x, y, 1 );
1659
1660 args = stack;
1661 break;
1662 }
1663
1664 case cff_op_flex1:
1665 {
1666 FT_Pos start_x, start_y; /* record start x, y values for */
1667 /* alter use */
1668 FT_Fixed dx = 0, dy = 0; /* used in horizontal/vertical */
1669 /* algorithm below */
1670 FT_Int horizontal, count;
1671
1672
1673 FT_TRACE4(( " flex1" ));
1674
1675 /* adding six more points; 4 control points, 2 on-curve points */
1676 if ( cff_builder_start_point( builder, x, y ) ||
1677 check_points( builder, 6 ) )
1678 goto Fail;
1679
1680 /* record the starting point's x, y position for later use */
1681 start_x = x;
1682 start_y = y;
1683
1684 /* XXX: figure out whether this is supposed to be a horizontal */
1685 /* or vertical flex; the Type 2 specification is vague... */
1686
1687 args = stack;
1688
1689 /* grab up to the last argument */
1690 for ( count = 5; count > 0; count-- )
1691 {
1692 dx += args[0];
1693 dy += args[1];
1694 args += 2;
1695 }
1696
1697 /* rewind */
1698 args = stack;
1699
1700 if ( dx < 0 ) dx = -dx;
1701 if ( dy < 0 ) dy = -dy;
1702
1703 /* strange test, but here it is... */
1704 horizontal = ( dx > dy );
1705
1706 for ( count = 5; count > 0; count-- )
1707 {
1708 x += args[0];
1709 y += args[1];
1710 cff_builder_add_point( builder, x, y, (FT_Bool)( count == 3 ) );
1711 args += 2;
1712 }
1713
1714 /* is last operand an x- or y-delta? */
1715 if ( horizontal )
1716 {
1717 x += args[0];
1718 y = start_y;
1719 }
1720 else
1721 {
1722 x = start_x;
1723 y += args[0];
1724 }
1725
1726 cff_builder_add_point( builder, x, y, 1 );
1727
1728 args = stack;
1729 break;
1730 }
1731
1732 case cff_op_flex:
1733 {
1734 FT_UInt count;
1735
1736
1737 FT_TRACE4(( " flex" ));
1738
1739 if ( cff_builder_start_point( builder, x, y ) ||
1740 check_points( builder, 6 ) )
1741 goto Fail;
1742
1743 args = stack;
1744 for ( count = 6; count > 0; count-- )
1745 {
1746 x += args[0];
1747 y += args[1];
1748 cff_builder_add_point( builder, x, y,
1749 (FT_Bool)( count == 4 || count == 1 ) );
1750 args += 2;
1751 }
1752
1753 args = stack;
1754 }
1755 break;
1756
1757 case cff_op_endchar:
1758 FT_TRACE4(( " endchar" ));
1759
1760 /* We are going to emulate the seac operator. */
1761 if ( num_args == 4 )
1762 {
1763 /* Save glyph width so that the subglyphs don't overwrite it. */
1764 FT_Pos glyph_width = decoder->glyph_width;
1765
1766
1767 error = cff_operator_seac( decoder,
1768 args[0],
1769 args[1],
1770 (FT_Int)( args[2] >> 16 ),
1771 (FT_Int)( args[3] >> 16 ) );
1772 args += 4;
1773
1774 decoder->glyph_width = glyph_width;
1775 }
1776 else
1777 {
1778 if ( !error )
1779 error = CFF_Err_Ok;
1780
1781 cff_builder_close_contour( builder );
1782
1783 /* close hints recording session */
1784 if ( hinter )
1785 {
1786 if ( hinter->close( hinter->hints,
1787 builder->current->n_points ) )
1788 goto Syntax_Error;
1789
1790 /* apply hints to the loaded glyph outline now */
1791 hinter->apply( hinter->hints,
1792 builder->current,
1793 (PSH_Globals)builder->hints_globals,
1794 decoder->hint_mode );
1795 }
1796
1797 /* add current outline to the glyph slot */
1798 FT_GlyphLoader_Add( builder->loader );
1799 }
1800
1801 /* return now! */
1802 FT_TRACE4(( "\n\n" ));
1803 return error;
1804
1805 case cff_op_abs:
1806 FT_TRACE4(( " abs" ));
1807
1808 if ( args[0] < 0 )
1809 args[0] = -args[0];
1810 args++;
1811 break;
1812
1813 case cff_op_add:
1814 FT_TRACE4(( " add" ));
1815
1816 args[0] += args[1];
1817 args++;
1818 break;
1819
1820 case cff_op_sub:
1821 FT_TRACE4(( " sub" ));
1822
1823 args[0] -= args[1];
1824 args++;
1825 break;
1826
1827 case cff_op_div:
1828 FT_TRACE4(( " div" ));
1829
1830 args[0] = FT_DivFix( args[0], args[1] );
1831 args++;
1832 break;
1833
1834 case cff_op_neg:
1835 FT_TRACE4(( " neg" ));
1836
1837 args[0] = -args[0];
1838 args++;
1839 break;
1840
1841 case cff_op_random:
1842 {
1843 FT_Fixed Rand;
1844
1845
1846 FT_TRACE4(( " rand" ));
1847
1848 Rand = seed;
1849 if ( Rand >= 0x8000L )
1850 Rand++;
1851
1852 args[0] = Rand;
1853 seed = FT_MulFix( seed, 0x10000L - seed );
1854 if ( seed == 0 )
1855 seed += 0x2873;
1856 args++;
1857 }
1858 break;
1859
1860 case cff_op_mul:
1861 FT_TRACE4(( " mul" ));
1862
1863 args[0] = FT_MulFix( args[0], args[1] );
1864 args++;
1865 break;
1866
1867 case cff_op_sqrt:
1868 FT_TRACE4(( " sqrt" ));
1869
1870 if ( args[0] > 0 )
1871 {
1872 FT_Int count = 9;
1873 FT_Fixed root = args[0];
1874 FT_Fixed new_root;
1875
1876
1877 for (;;)
1878 {
1879 new_root = ( root + FT_DivFix( args[0], root ) + 1 ) >> 1;
1880 if ( new_root == root || count <= 0 )
1881 break;
1882 root = new_root;
1883 }
1884 args[0] = new_root;
1885 }
1886 else
1887 args[0] = 0;
1888 args++;
1889 break;
1890
1891 case cff_op_drop:
1892 /* nothing */
1893 FT_TRACE4(( " drop" ));
1894
1895 break;
1896
1897 case cff_op_exch:
1898 {
1899 FT_Fixed tmp;
1900
1901
1902 FT_TRACE4(( " exch" ));
1903
1904 tmp = args[0];
1905 args[0] = args[1];
1906 args[1] = tmp;
1907 args += 2;
1908 }
1909 break;
1910
1911 case cff_op_index:
1912 {
1913 FT_Int idx = (FT_Int)( args[0] >> 16 );
1914
1915
1916 FT_TRACE4(( " index" ));
1917
1918 if ( idx < 0 )
1919 idx = 0;
1920 else if ( idx > num_args - 2 )
1921 idx = num_args - 2;
1922 args[0] = args[-( idx + 1 )];
1923 args++;
1924 }
1925 break;
1926
1927 case cff_op_roll:
1928 {
1929 FT_Int count = (FT_Int)( args[0] >> 16 );
1930 FT_Int idx = (FT_Int)( args[1] >> 16 );
1931
1932
1933 FT_TRACE4(( " roll" ));
1934
1935 if ( count <= 0 )
1936 count = 1;
1937
1938 args -= count;
1939 if ( args < stack )
1940 goto Stack_Underflow;
1941
1942 if ( idx >= 0 )
1943 {
1944 while ( idx > 0 )
1945 {
1946 FT_Fixed tmp = args[count - 1];
1947 FT_Int i;
1948
1949
1950 for ( i = count - 2; i >= 0; i-- )
1951 args[i + 1] = args[i];
1952 args[0] = tmp;
1953 idx--;
1954 }
1955 }
1956 else
1957 {
1958 while ( idx < 0 )
1959 {
1960 FT_Fixed tmp = args[0];
1961 FT_Int i;
1962
1963
1964 for ( i = 0; i < count - 1; i++ )
1965 args[i] = args[i + 1];
1966 args[count - 1] = tmp;
1967 idx++;
1968 }
1969 }
1970 args += count;
1971 }
1972 break;
1973
1974 case cff_op_dup:
1975 FT_TRACE4(( " dup" ));
1976
1977 args[1] = args[0];
1978 args++;
1979 break;
1980
1981 case cff_op_put:
1982 {
1983 FT_Fixed val = args[0];
1984 FT_Int idx = (FT_Int)( args[1] >> 16 );
1985
1986
1987 FT_TRACE4(( " put" ));
1988
1989 if ( idx >= 0 && idx < decoder->len_buildchar )
1990 decoder->buildchar[idx] = val;
1991 }
1992 break;
1993
1994 case cff_op_get:
1995 {
1996 FT_Int idx = (FT_Int)( args[0] >> 16 );
1997 FT_Fixed val = 0;
1998
1999
2000 FT_TRACE4(( " get" ));
2001
2002 if ( idx >= 0 && idx < decoder->len_buildchar )
2003 val = decoder->buildchar[idx];
2004
2005 args[0] = val;
2006 args++;
2007 }
2008 break;
2009
2010 case cff_op_store:
2011 FT_TRACE4(( " store "));
2012
2013 goto Unimplemented;
2014
2015 case cff_op_load:
2016 FT_TRACE4(( " load" ));
2017
2018 goto Unimplemented;
2019
2020 case cff_op_dotsection:
2021 /* this operator is deprecated and ignored by the parser */
2022 FT_TRACE4(( " dotsection" ));
2023 break;
2024
2025 case cff_op_and:
2026 {
2027 FT_Fixed cond = args[0] && args[1];
2028
2029
2030 FT_TRACE4(( " and" ));
2031
2032 args[0] = cond ? 0x10000L : 0;
2033 args++;
2034 }
2035 break;
2036
2037 case cff_op_or:
2038 {
2039 FT_Fixed cond = args[0] || args[1];
2040
2041
2042 FT_TRACE4(( " or" ));
2043
2044 args[0] = cond ? 0x10000L : 0;
2045 args++;
2046 }
2047 break;
2048
2049 case cff_op_eq:
2050 {
2051 FT_Fixed cond = !args[0];
2052
2053
2054 FT_TRACE4(( " eq" ));
2055
2056 args[0] = cond ? 0x10000L : 0;
2057 args++;
2058 }
2059 break;
2060
2061 case cff_op_ifelse:
2062 {
2063 FT_Fixed cond = ( args[2] <= args[3] );
2064
2065
2066 FT_TRACE4(( " ifelse" ));
2067
2068 if ( !cond )
2069 args[0] = args[1];
2070 args++;
2071 }
2072 break;
2073
2074 case cff_op_callsubr:
2075 {
2076 FT_UInt idx = (FT_UInt)( ( args[0] >> 16 ) +
2077 decoder->locals_bias );
2078
2079
2080 FT_TRACE4(( " callsubr(%d)", idx ));
2081
2082 if ( idx >= decoder->num_locals )
2083 {
2084 FT_ERROR(( "cff_decoder_parse_charstrings:" ));
2085 FT_ERROR(( " invalid local subr index\n" ));
2086 goto Syntax_Error;
2087 }
2088
2089 if ( zone - decoder->zones >= CFF_MAX_SUBRS_CALLS )
2090 {
2091 FT_ERROR(( "cff_decoder_parse_charstrings:"
2092 " too many nested subrs\n" ));
2093 goto Syntax_Error;
2094 }
2095
2096 zone->cursor = ip; /* save current instruction pointer */
2097
2098 zone++;
2099 zone->base = decoder->locals[idx];
2100 zone->limit = decoder->locals[idx + 1];
2101 zone->cursor = zone->base;
2102
2103 if ( !zone->base || zone->limit == zone->base )
2104 {
2105 FT_ERROR(( "cff_decoder_parse_charstrings:"
2106 " invoking empty subrs!\n" ));
2107 goto Syntax_Error;
2108 }
2109
2110 decoder->zone = zone;
2111 ip = zone->base;
2112 limit = zone->limit;
2113 }
2114 break;
2115
2116 case cff_op_callgsubr:
2117 {
2118 FT_UInt idx = (FT_UInt)( ( args[0] >> 16 ) +
2119 decoder->globals_bias );
2120
2121
2122 FT_TRACE4(( " callgsubr(%d)", idx ));
2123
2124 if ( idx >= decoder->num_globals )
2125 {
2126 FT_ERROR(( "cff_decoder_parse_charstrings:" ));
2127 FT_ERROR(( " invalid global subr index\n" ));
2128 goto Syntax_Error;
2129 }
2130
2131 if ( zone - decoder->zones >= CFF_MAX_SUBRS_CALLS )
2132 {
2133 FT_ERROR(( "cff_decoder_parse_charstrings:"
2134 " too many nested subrs\n" ));
2135 goto Syntax_Error;
2136 }
2137
2138 zone->cursor = ip; /* save current instruction pointer */
2139
2140 zone++;
2141 zone->base = decoder->globals[idx];
2142 zone->limit = decoder->globals[idx + 1];
2143 zone->cursor = zone->base;
2144
2145 if ( !zone->base || zone->limit == zone->base )
2146 {
2147 FT_ERROR(( "cff_decoder_parse_charstrings:"
2148 " invoking empty subrs!\n" ));
2149 goto Syntax_Error;
2150 }
2151
2152 decoder->zone = zone;
2153 ip = zone->base;
2154 limit = zone->limit;
2155 }
2156 break;
2157
2158 case cff_op_return:
2159 FT_TRACE4(( " return" ));
2160
2161 if ( decoder->zone <= decoder->zones )
2162 {
2163 FT_ERROR(( "cff_decoder_parse_charstrings:"
2164 " unexpected return\n" ));
2165 goto Syntax_Error;
2166 }
2167
2168 decoder->zone--;
2169 zone = decoder->zone;
2170 ip = zone->cursor;
2171 limit = zone->limit;
2172 break;
2173
2174 default:
2175 Unimplemented:
2176 FT_ERROR(( "Unimplemented opcode: %d", ip[-1] ));
2177
2178 if ( ip[-1] == 12 )
2179 FT_ERROR(( " %d", ip[0] ));
2180 FT_ERROR(( "\n" ));
2181
2182 return CFF_Err_Unimplemented_Feature;
2183 }
2184
2185 decoder->top = args;
2186
2187 } /* general operator processing */
2188
2189 } /* while ip < limit */
2190
2191 FT_TRACE4(( "..end..\n\n" ));
2192
2193 Fail:
2194 return error;
2195
2196 Syntax_Error:
2197 FT_TRACE4(( "cff_decoder_parse_charstrings: syntax error!" ));
2198 return CFF_Err_Invalid_File_Format;
2199
2200 Stack_Underflow:
2201 FT_TRACE4(( "cff_decoder_parse_charstrings: stack underflow!" ));
2202 return CFF_Err_Too_Few_Arguments;
2203
2204 Stack_Overflow:
2205 FT_TRACE4(( "cff_decoder_parse_charstrings: stack overflow!" ));
2206 return CFF_Err_Stack_Overflow;
2207 }
2208
2209
2210 /*************************************************************************/
2211 /*************************************************************************/
2212 /*************************************************************************/
2213 /********** *********/
2214 /********** *********/
2215 /********** COMPUTE THE MAXIMUM ADVANCE WIDTH *********/
2216 /********** *********/
2217 /********** The following code is in charge of computing *********/
2218 /********** the maximum advance width of the font. It *********/
2219 /********** quickly processes each glyph charstring to *********/
2220 /********** extract the value from either a `sbw' or `seac' *********/
2221 /********** operator. *********/
2222 /********** *********/
2223 /*************************************************************************/
2224 /*************************************************************************/
2225 /*************************************************************************/
2226
2227
2228 #if 0 /* unused until we support pure CFF fonts */
2229
2230
2231 FT_LOCAL_DEF( FT_Error )
2232 cff_compute_max_advance( TT_Face face,
2233 FT_Int* max_advance )
2234 {
2235 FT_Error error = CFF_Err_Ok;
2236 CFF_Decoder decoder;
2237 FT_Int glyph_index;
2238 CFF_Font cff = (CFF_Font)face->other;
2239
2240
2241 *max_advance = 0;
2242
2243 /* Initialize load decoder */
2244 cff_decoder_init( &decoder, face, 0, 0, 0, 0 );
2245
2246 decoder.builder.metrics_only = 1;
2247 decoder.builder.load_points = 0;
2248
2249 /* For each glyph, parse the glyph charstring and extract */
2250 /* the advance width. */
2251 for ( glyph_index = 0; glyph_index < face->root.num_glyphs;
2252 glyph_index++ )
2253 {
2254 FT_Byte* charstring;
2255 FT_ULong charstring_len;
2256
2257
2258 /* now get load the unscaled outline */
2259 error = cff_get_glyph_data( face, glyph_index,
2260 &charstring, &charstring_len );
2261 if ( !error )
2262 {
2263 error = cff_decoder_prepare( &decoder, glyph_index );
2264 if ( !error )
2265 error = cff_decoder_parse_charstrings( &decoder,
2266 charstring,
2267 charstring_len );
2268
2269 cff_free_glyph_data( face, &charstring, &charstring_len );
2270 }
2271
2272 /* ignore the error if one has occurred -- skip to next glyph */
2273 error = CFF_Err_Ok;
2274 }
2275
2276 *max_advance = decoder.builder.advance.x;
2277
2278 return CFF_Err_Ok;
2279 }
2280
2281
2282 #endif /* 0 */
2283
2284
2285 FT_LOCAL_DEF( FT_Error )
2286 cff_slot_load( CFF_GlyphSlot glyph,
2287 CFF_Size size,
2288 FT_UInt glyph_index,
2289 FT_Int32 load_flags )
2290 {
2291 FT_Error error;
2292 CFF_Decoder decoder;
2293 TT_Face face = (TT_Face)glyph->root.face;
2294 FT_Bool hinting;
2295 CFF_Font cff = (CFF_Font)face->extra.data;
2296
2297 FT_Matrix font_matrix;
2298 FT_Vector font_offset;
2299
2300
2301 /* in a CID-keyed font, consider `glyph_index' as a CID and map */
2302 /* it immediately to the real glyph_index -- if it isn't a */
2303 /* subsetted font, glyph_indices and CIDs are identical, though */
2304 if ( cff->top_font.font_dict.cid_registry != 0xFFFFU &&
2305 cff->charset.cids )
2306 {
2307 glyph_index = cff_charset_cid_to_gindex( &cff->charset, glyph_index );
2308 if ( glyph_index == 0 )
2309 return CFF_Err_Invalid_Argument;
2310 }
2311 else if ( glyph_index >= cff->num_glyphs )
2312 return CFF_Err_Invalid_Argument;
2313
2314 if ( load_flags & FT_LOAD_NO_RECURSE )
2315 load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
2316
2317 glyph->x_scale = 0x10000L;
2318 glyph->y_scale = 0x10000L;
2319 if ( size )
2320 {
2321 glyph->x_scale = size->root.metrics.x_scale;
2322 glyph->y_scale = size->root.metrics.y_scale;
2323 }
2324
2325 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
2326
2327 /* try to load embedded bitmap if any */
2328 /* */
2329 /* XXX: The convention should be emphasized in */
2330 /* the documents because it can be confusing. */
2331 if ( size )
2332 {
2333 CFF_Face cff_face = (CFF_Face)size->root.face;
2334 SFNT_Service sfnt = (SFNT_Service)cff_face->sfnt;
2335 FT_Stream stream = cff_face->root.stream;
2336
2337
2338 if ( size->strike_index != 0xFFFFFFFFUL &&
2339 sfnt->load_eblc &&
2340 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
2341 {
2342 TT_SBit_MetricsRec metrics;
2343
2344
2345 error = sfnt->load_sbit_image( face,
2346 size->strike_index,
2347 glyph_index,
2348 (FT_Int)load_flags,
2349 stream,
2350 &glyph->root.bitmap,
2351 &metrics );
2352
2353 if ( !error )
2354 {
2355 glyph->root.outline.n_points = 0;
2356 glyph->root.outline.n_contours = 0;
2357
2358 glyph->root.metrics.width = (FT_Pos)metrics.width << 6;
2359 glyph->root.metrics.height = (FT_Pos)metrics.height << 6;
2360
2361 glyph->root.metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6;
2362 glyph->root.metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6;
2363 glyph->root.metrics.horiAdvance = (FT_Pos)metrics.horiAdvance << 6;
2364
2365 glyph->root.metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6;
2366 glyph->root.metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6;
2367 glyph->root.metrics.vertAdvance = (FT_Pos)metrics.vertAdvance << 6;
2368
2369 glyph->root.format = FT_GLYPH_FORMAT_BITMAP;
2370
2371 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
2372 {
2373 glyph->root.bitmap_left = metrics.vertBearingX;
2374 glyph->root.bitmap_top = metrics.vertBearingY;
2375 }
2376 else
2377 {
2378 glyph->root.bitmap_left = metrics.horiBearingX;
2379 glyph->root.bitmap_top = metrics.horiBearingY;
2380 }
2381 return error;
2382 }
2383 }
2384 }
2385
2386 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
2387
2388 /* return immediately if we only want the embedded bitmaps */
2389 if ( load_flags & FT_LOAD_SBITS_ONLY )
2390 return CFF_Err_Invalid_Argument;
2391
2392 glyph->root.outline.n_points = 0;
2393 glyph->root.outline.n_contours = 0;
2394
2395 hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 &&
2396 ( load_flags & FT_LOAD_NO_HINTING ) == 0 );
2397
2398 glyph->root.format = FT_GLYPH_FORMAT_OUTLINE; /* by default */
2399
2400 {
2401 FT_Byte* charstring;
2402 FT_ULong charstring_len;
2403
2404
2405 cff_decoder_init( &decoder, face, size, glyph, hinting,
2406 FT_LOAD_TARGET_MODE( load_flags ) );
2407
2408 decoder.builder.no_recurse =
2409 (FT_Bool)( ( load_flags & FT_LOAD_NO_RECURSE ) != 0 );
2410
2411 /* now load the unscaled outline */
2412 error = cff_get_glyph_data( face, glyph_index,
2413 &charstring, &charstring_len );
2414 if ( !error )
2415 {
2416 error = cff_decoder_prepare( &decoder, glyph_index );
2417 if ( !error )
2418 {
2419 error = cff_decoder_parse_charstrings( &decoder,
2420 charstring,
2421 charstring_len );
2422
2423 cff_free_glyph_data( face, &charstring, charstring_len );
2424
2425
2426 #ifdef FT_CONFIG_OPTION_INCREMENTAL
2427 /* Control data and length may not be available for incremental */
2428 /* fonts. */
2429 if ( face->root.internal->incremental_interface )
2430 {
2431 glyph->root.control_data = 0;
2432 glyph->root.control_len = 0;
2433 }
2434 else
2435 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
2436
2437 /* We set control_data and control_len if charstrings is loaded. */
2438 /* See how charstring loads at cff_index_access_element() in */
2439 /* cffload.c. */
2440 {
2441 CFF_Index csindex = &cff->charstrings_index;
2442
2443
2444 if ( csindex->offsets )
2445 {
2446 glyph->root.control_data = csindex->bytes +
2447 csindex->offsets[glyph_index] - 1;
2448 glyph->root.control_len = charstring_len;
2449 }
2450 }
2451 }
2452 }
2453
2454 /* save new glyph tables */
2455 cff_builder_done( &decoder.builder );
2456 }
2457
2458 #ifdef FT_CONFIG_OPTION_INCREMENTAL
2459
2460 /* Incremental fonts can optionally override the metrics. */
2461 if ( !error &&
2462 face->root.internal->incremental_interface &&
2463 face->root.internal->incremental_interface->funcs->get_glyph_metrics )
2464 {
2465 FT_Incremental_MetricsRec metrics;
2466
2467
2468 metrics.bearing_x = decoder.builder.left_bearing.x;
2469 metrics.bearing_y = decoder.builder.left_bearing.y;
2470 metrics.advance = decoder.builder.advance.x;
2471 error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
2472 face->root.internal->incremental_interface->object,
2473 glyph_index, FALSE, &metrics );
2474 decoder.builder.left_bearing.x = metrics.bearing_x;
2475 decoder.builder.left_bearing.y = metrics.bearing_y;
2476 decoder.builder.advance.x = metrics.advance;
2477 decoder.builder.advance.y = 0;
2478 }
2479
2480 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
2481
2482 if ( !error )
2483 {
2484 if ( cff->num_subfonts >= 1 )
2485 {
2486 FT_Byte fd_index = cff_fd_select_get( &cff->fd_select,
2487 glyph_index );
2488
2489
2490 font_matrix = cff->subfonts[fd_index]->font_dict.font_matrix;
2491 font_offset = cff->subfonts[fd_index]->font_dict.font_offset;
2492 }
2493 else
2494 {
2495 font_matrix = cff->top_font.font_dict.font_matrix;
2496 font_offset = cff->top_font.font_dict.font_offset;
2497 }
2498
2499 /* Now, set the metrics -- this is rather simple, as */
2500 /* the left side bearing is the xMin, and the top side */
2501 /* bearing the yMax. */
2502
2503 /* For composite glyphs, return only left side bearing and */
2504 /* advance width. */
2505 if ( load_flags & FT_LOAD_NO_RECURSE )
2506 {
2507 FT_Slot_Internal internal = glyph->root.internal;
2508
2509
2510 glyph->root.metrics.horiBearingX = decoder.builder.left_bearing.x;
2511 glyph->root.metrics.horiAdvance = decoder.glyph_width;
2512 internal->glyph_matrix = font_matrix;
2513 internal->glyph_delta = font_offset;
2514 internal->glyph_transformed = 1;
2515 }
2516 else
2517 {
2518 FT_BBox cbox;
2519 FT_Glyph_Metrics* metrics = &glyph->root.metrics;
2520 FT_Vector advance;
2521 FT_Bool has_vertical_info;
2522
2523
2524 /* copy the _unscaled_ advance width */
2525 metrics->horiAdvance = decoder.glyph_width;
2526 glyph->root.linearHoriAdvance = decoder.glyph_width;
2527 glyph->root.internal->glyph_transformed = 0;
2528
2529 has_vertical_info = FT_BOOL( face->vertical_info &&
2530 face->vertical.number_Of_VMetrics > 0 &&
2531 face->vertical.long_metrics != 0 );
2532
2533 /* get the vertical metrics from the vtmx table if we have one */
2534 if ( has_vertical_info )
2535 {
2536 FT_Short vertBearingY = 0;
2537 FT_UShort vertAdvance = 0;
2538
2539
2540 ( (SFNT_Service)face->sfnt )->get_metrics( face, 1,
2541 glyph_index,
2542 &vertBearingY,
2543 &vertAdvance );
2544 metrics->vertBearingY = vertBearingY;
2545 metrics->vertAdvance = vertAdvance;
2546 }
2547 else
2548 {
2549 /* make up vertical ones */
2550 if ( face->os2.version != 0xFFFFU )
2551 metrics->vertAdvance = (FT_Pos)( face->os2.sTypoAscender -
2552 face->os2.sTypoDescender );
2553 else
2554 metrics->vertAdvance = (FT_Pos)( face->horizontal.Ascender -
2555 face->horizontal.Descender );
2556 }
2557
2558 glyph->root.linearVertAdvance = metrics->vertAdvance;
2559
2560 glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;
2561
2562 glyph->root.outline.flags = 0;
2563 if ( size && size->root.metrics.y_ppem < 24 )
2564 glyph->root.outline.flags |= FT_OUTLINE_HIGH_PRECISION;
2565
2566 glyph->root.outline.flags |= FT_OUTLINE_REVERSE_FILL;
2567
2568 /* apply the font matrix */
2569 if ( !( font_matrix.xx == 0x10000L &&
2570 font_matrix.yy == 0x10000L &&
2571 font_matrix.xy == 0 &&
2572 font_matrix.yx == 0 ) )
2573 FT_Outline_Transform( &glyph->root.outline, &font_matrix );
2574
2575 if ( !( font_offset.x == 0 &&
2576 font_offset.y == 0 ) )
2577 FT_Outline_Translate( &glyph->root.outline,
2578 font_offset.x, font_offset.y );
2579
2580 advance.x = metrics->horiAdvance;
2581 advance.y = 0;
2582 FT_Vector_Transform( &advance, &font_matrix );
2583 metrics->horiAdvance = advance.x + font_offset.x;
2584
2585 advance.x = 0;
2586 advance.y = metrics->vertAdvance;
2587 FT_Vector_Transform( &advance, &font_matrix );
2588 metrics->vertAdvance = advance.y + font_offset.y;
2589
2590 if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 )
2591 {
2592 /* scale the outline and the metrics */
2593 FT_Int n;
2594 FT_Outline* cur = &glyph->root.outline;
2595 FT_Vector* vec = cur->points;
2596 FT_Fixed x_scale = glyph->x_scale;
2597 FT_Fixed y_scale = glyph->y_scale;
2598
2599
2600 /* First of all, scale the points */
2601 if ( !hinting || !decoder.builder.hints_funcs )
2602 for ( n = cur->n_points; n > 0; n--, vec++ )
2603 {
2604 vec->x = FT_MulFix( vec->x, x_scale );
2605 vec->y = FT_MulFix( vec->y, y_scale );
2606 }
2607
2608 /* Then scale the metrics */
2609 metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale );
2610 metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale );
2611 }
2612
2613 /* compute the other metrics */
2614 FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
2615
2616 metrics->width = cbox.xMax - cbox.xMin;
2617 metrics->height = cbox.yMax - cbox.yMin;
2618
2619 metrics->horiBearingX = cbox.xMin;
2620 metrics->horiBearingY = cbox.yMax;
2621
2622 if ( has_vertical_info )
2623 metrics->vertBearingX = -metrics->width / 2;
2624 else
2625 ft_synthesize_vertical_metrics( metrics,
2626 metrics->vertAdvance );
2627 }
2628 }
2629
2630 return error;
2631 }
2632
2633
2634 /* END */