Disabling auto-refresh of game list by default, as it is causing bugs sometimes
[open-ps2-loader.git] / thirdparty / freetype-2.3.12 / src / cff / cffgload.c
blob9330c058823b1b829cf8c5fccd08e9fc0ef2e2e3
1 /***************************************************************************/
2 /* */
3 /* cffgload.c */
4 /* */
5 /* OpenType Glyph Loader (body). */
6 /* */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, */
8 /* 2010 by */
9 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* */
11 /* This file is part of the FreeType project, and may only be used, */
12 /* modified, and distributed under the terms of the FreeType project */
13 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
14 /* this file you indicate that you have read the license and */
15 /* understand and accept it fully. */
16 /* */
17 /***************************************************************************/
20 #include <ft2build.h>
21 #include FT_INTERNAL_DEBUG_H
22 #include FT_INTERNAL_STREAM_H
23 #include FT_INTERNAL_SFNT_H
24 #include FT_OUTLINE_H
25 #include FT_INTERNAL_POSTSCRIPT_HINTS_H
27 #include "cffobjs.h"
28 #include "cffload.h"
29 #include "cffgload.h"
31 #include "cfferrs.h"
34 /*************************************************************************/
35 /* */
36 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
37 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
38 /* messages during execution. */
39 /* */
40 #undef FT_COMPONENT
41 #define FT_COMPONENT trace_cffgload
44 typedef enum CFF_Operator_
46 cff_op_unknown = 0,
48 cff_op_rmoveto,
49 cff_op_hmoveto,
50 cff_op_vmoveto,
52 cff_op_rlineto,
53 cff_op_hlineto,
54 cff_op_vlineto,
56 cff_op_rrcurveto,
57 cff_op_hhcurveto,
58 cff_op_hvcurveto,
59 cff_op_rcurveline,
60 cff_op_rlinecurve,
61 cff_op_vhcurveto,
62 cff_op_vvcurveto,
64 cff_op_flex,
65 cff_op_hflex,
66 cff_op_hflex1,
67 cff_op_flex1,
69 cff_op_endchar,
71 cff_op_hstem,
72 cff_op_vstem,
73 cff_op_hstemhm,
74 cff_op_vstemhm,
76 cff_op_hintmask,
77 cff_op_cntrmask,
78 cff_op_dotsection, /* deprecated, acts as no-op */
80 cff_op_abs,
81 cff_op_add,
82 cff_op_sub,
83 cff_op_div,
84 cff_op_neg,
85 cff_op_random,
86 cff_op_mul,
87 cff_op_sqrt,
89 cff_op_blend,
91 cff_op_drop,
92 cff_op_exch,
93 cff_op_index,
94 cff_op_roll,
95 cff_op_dup,
97 cff_op_put,
98 cff_op_get,
99 cff_op_store,
100 cff_op_load,
102 cff_op_and,
103 cff_op_or,
104 cff_op_not,
105 cff_op_eq,
106 cff_op_ifelse,
108 cff_op_callsubr,
109 cff_op_callgsubr,
110 cff_op_return,
112 /* Type 1 opcodes: invalid but seen in real life */
113 cff_op_hsbw,
114 cff_op_closepath,
115 cff_op_callothersubr,
116 cff_op_pop,
117 cff_op_seac,
118 cff_op_sbw,
119 cff_op_setcurrentpoint,
121 /* do not remove */
122 cff_op_max
124 } CFF_Operator;
127 #define CFF_COUNT_CHECK_WIDTH 0x80
128 #define CFF_COUNT_EXACT 0x40
129 #define CFF_COUNT_CLEAR_STACK 0x20
131 /* count values which have the `CFF_COUNT_CHECK_WIDTH' flag set are */
132 /* used for checking the width and requested numbers of arguments */
133 /* only; they are set to zero afterwards */
135 /* the other two flags are informative only and unused currently */
137 static const FT_Byte cff_argument_counts[] =
139 0, /* unknown */
141 2 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT, /* rmoveto */
142 1 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT,
143 1 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT,
145 0 | CFF_COUNT_CLEAR_STACK, /* rlineto */
146 0 | CFF_COUNT_CLEAR_STACK,
147 0 | CFF_COUNT_CLEAR_STACK,
149 0 | CFF_COUNT_CLEAR_STACK, /* rrcurveto */
150 0 | CFF_COUNT_CLEAR_STACK,
151 0 | CFF_COUNT_CLEAR_STACK,
152 0 | CFF_COUNT_CLEAR_STACK,
153 0 | CFF_COUNT_CLEAR_STACK,
154 0 | CFF_COUNT_CLEAR_STACK,
155 0 | CFF_COUNT_CLEAR_STACK,
157 13, /* flex */
162 0 | CFF_COUNT_CHECK_WIDTH, /* endchar */
164 2 | CFF_COUNT_CHECK_WIDTH, /* hstem */
165 2 | CFF_COUNT_CHECK_WIDTH,
166 2 | CFF_COUNT_CHECK_WIDTH,
167 2 | CFF_COUNT_CHECK_WIDTH,
169 0 | CFF_COUNT_CHECK_WIDTH, /* hintmask */
170 0 | CFF_COUNT_CHECK_WIDTH, /* cntrmask */
171 0, /* dotsection */
173 1, /* abs */
182 1, /* blend */
184 1, /* drop */
190 2, /* put */
195 2, /* and */
201 1, /* callsubr */
205 2, /* hsbw */
209 5, /* seac */
210 4, /* sbw */
211 2 /* setcurrentpoint */
215 /*************************************************************************/
216 /*************************************************************************/
217 /*************************************************************************/
218 /********** *********/
219 /********** *********/
220 /********** GENERIC CHARSTRING PARSING *********/
221 /********** *********/
222 /********** *********/
223 /*************************************************************************/
224 /*************************************************************************/
225 /*************************************************************************/
228 /*************************************************************************/
229 /* */
230 /* <Function> */
231 /* cff_builder_init */
232 /* */
233 /* <Description> */
234 /* Initializes a given glyph builder. */
235 /* */
236 /* <InOut> */
237 /* builder :: A pointer to the glyph builder to initialize. */
238 /* */
239 /* <Input> */
240 /* face :: The current face object. */
241 /* */
242 /* size :: The current size object. */
243 /* */
244 /* glyph :: The current glyph object. */
245 /* */
246 /* hinting :: Whether hinting is active. */
247 /* */
248 static void
249 cff_builder_init( CFF_Builder* builder,
250 TT_Face face,
251 CFF_Size size,
252 CFF_GlyphSlot glyph,
253 FT_Bool hinting )
255 builder->path_begun = 0;
256 builder->load_points = 1;
258 builder->face = face;
259 builder->glyph = glyph;
260 builder->memory = face->root.memory;
262 if ( glyph )
264 FT_GlyphLoader loader = glyph->root.internal->loader;
267 builder->loader = loader;
268 builder->base = &loader->base.outline;
269 builder->current = &loader->current.outline;
270 FT_GlyphLoader_Rewind( loader );
272 builder->hints_globals = 0;
273 builder->hints_funcs = 0;
275 if ( hinting && size )
277 CFF_Internal internal = (CFF_Internal)size->root.internal;
280 builder->hints_globals = (void *)internal->topfont;
281 builder->hints_funcs = glyph->root.internal->glyph_hints;
285 builder->pos_x = 0;
286 builder->pos_y = 0;
288 builder->left_bearing.x = 0;
289 builder->left_bearing.y = 0;
290 builder->advance.x = 0;
291 builder->advance.y = 0;
295 /*************************************************************************/
296 /* */
297 /* <Function> */
298 /* cff_builder_done */
299 /* */
300 /* <Description> */
301 /* Finalizes a given glyph builder. Its contents can still be used */
302 /* after the call, but the function saves important information */
303 /* within the corresponding glyph slot. */
304 /* */
305 /* <Input> */
306 /* builder :: A pointer to the glyph builder to finalize. */
307 /* */
308 static void
309 cff_builder_done( CFF_Builder* builder )
311 CFF_GlyphSlot glyph = builder->glyph;
314 if ( glyph )
315 glyph->root.outline = *builder->base;
319 /*************************************************************************/
320 /* */
321 /* <Function> */
322 /* cff_compute_bias */
323 /* */
324 /* <Description> */
325 /* Computes the bias value in dependence of the number of glyph */
326 /* subroutines. */
327 /* */
328 /* <Input> */
329 /* in_charstring_type :: The `CharstringType' value of the top DICT */
330 /* dictionary. */
331 /* */
332 /* num_subrs :: The number of glyph subroutines. */
333 /* */
334 /* <Return> */
335 /* The bias value. */
336 static FT_Int
337 cff_compute_bias( FT_Int in_charstring_type,
338 FT_UInt num_subrs )
340 FT_Int result;
343 if ( in_charstring_type == 1 )
344 result = 0;
345 else if ( num_subrs < 1240 )
346 result = 107;
347 else if ( num_subrs < 33900U )
348 result = 1131;
349 else
350 result = 32768U;
352 return result;
356 /*************************************************************************/
357 /* */
358 /* <Function> */
359 /* cff_decoder_init */
360 /* */
361 /* <Description> */
362 /* Initializes a given glyph decoder. */
363 /* */
364 /* <InOut> */
365 /* decoder :: A pointer to the glyph builder to initialize. */
366 /* */
367 /* <Input> */
368 /* face :: The current face object. */
369 /* */
370 /* size :: The current size object. */
371 /* */
372 /* slot :: The current glyph object. */
373 /* */
374 /* hinting :: Whether hinting is active. */
375 /* */
376 /* hint_mode :: The hinting mode. */
377 /* */
378 FT_LOCAL_DEF( void )
379 cff_decoder_init( CFF_Decoder* decoder,
380 TT_Face face,
381 CFF_Size size,
382 CFF_GlyphSlot slot,
383 FT_Bool hinting,
384 FT_Render_Mode hint_mode )
386 CFF_Font cff = (CFF_Font)face->extra.data;
389 /* clear everything */
390 FT_MEM_ZERO( decoder, sizeof ( *decoder ) );
392 /* initialize builder */
393 cff_builder_init( &decoder->builder, face, size, slot, hinting );
395 /* initialize Type2 decoder */
396 decoder->cff = cff;
397 decoder->num_globals = cff->num_global_subrs;
398 decoder->globals = cff->global_subrs;
399 decoder->globals_bias = cff_compute_bias(
400 cff->top_font.font_dict.charstring_type,
401 decoder->num_globals );
403 decoder->hint_mode = hint_mode;
407 /* this function is used to select the subfont */
408 /* and the locals subrs array */
409 FT_LOCAL_DEF( FT_Error )
410 cff_decoder_prepare( CFF_Decoder* decoder,
411 CFF_Size size,
412 FT_UInt glyph_index )
414 CFF_Builder *builder = &decoder->builder;
415 CFF_Font cff = (CFF_Font)builder->face->extra.data;
416 CFF_SubFont sub = &cff->top_font;
417 FT_Error error = CFF_Err_Ok;
420 /* manage CID fonts */
421 if ( cff->num_subfonts )
423 FT_Byte fd_index = cff_fd_select_get( &cff->fd_select, glyph_index );
426 if ( fd_index >= cff->num_subfonts )
428 FT_TRACE4(( "cff_decoder_prepare: invalid CID subfont index\n" ));
429 error = CFF_Err_Invalid_File_Format;
430 goto Exit;
433 FT_TRACE4(( "glyph index %d (subfont %d):\n", glyph_index, fd_index ));
435 sub = cff->subfonts[fd_index];
437 if ( builder->hints_funcs && size )
439 CFF_Internal internal = (CFF_Internal)size->root.internal;
442 /* for CFFs without subfonts, this value has already been set */
443 builder->hints_globals = (void *)internal->subfonts[fd_index];
446 #ifdef FT_DEBUG_LEVEL_TRACE
447 else
448 FT_TRACE4(( "glyph index %d:\n", glyph_index ));
449 #endif
451 decoder->num_locals = sub->num_local_subrs;
452 decoder->locals = sub->local_subrs;
453 decoder->locals_bias = cff_compute_bias(
454 decoder->cff->top_font.font_dict.charstring_type,
455 decoder->num_locals );
457 decoder->glyph_width = sub->private_dict.default_width;
458 decoder->nominal_width = sub->private_dict.nominal_width;
460 Exit:
461 return error;
465 /* check that there is enough space for `count' more points */
466 static FT_Error
467 check_points( CFF_Builder* builder,
468 FT_Int count )
470 return FT_GLYPHLOADER_CHECK_POINTS( builder->loader, count, 0 );
474 /* add a new point, do not check space */
475 static void
476 cff_builder_add_point( CFF_Builder* builder,
477 FT_Pos x,
478 FT_Pos y,
479 FT_Byte flag )
481 FT_Outline* outline = builder->current;
484 if ( builder->load_points )
486 FT_Vector* point = outline->points + outline->n_points;
487 FT_Byte* control = (FT_Byte*)outline->tags + outline->n_points;
490 point->x = x >> 16;
491 point->y = y >> 16;
492 *control = (FT_Byte)( flag ? FT_CURVE_TAG_ON : FT_CURVE_TAG_CUBIC );
495 outline->n_points++;
499 /* check space for a new on-curve point, then add it */
500 static FT_Error
501 cff_builder_add_point1( CFF_Builder* builder,
502 FT_Pos x,
503 FT_Pos y )
505 FT_Error error;
508 error = check_points( builder, 1 );
509 if ( !error )
510 cff_builder_add_point( builder, x, y, 1 );
512 return error;
516 /* check space for a new contour, then add it */
517 static FT_Error
518 cff_builder_add_contour( CFF_Builder* builder )
520 FT_Outline* outline = builder->current;
521 FT_Error error;
524 if ( !builder->load_points )
526 outline->n_contours++;
527 return CFF_Err_Ok;
530 error = FT_GLYPHLOADER_CHECK_POINTS( builder->loader, 0, 1 );
531 if ( !error )
533 if ( outline->n_contours > 0 )
534 outline->contours[outline->n_contours - 1] =
535 (short)( outline->n_points - 1 );
537 outline->n_contours++;
540 return error;
544 /* if a path was begun, add its first on-curve point */
545 static FT_Error
546 cff_builder_start_point( CFF_Builder* builder,
547 FT_Pos x,
548 FT_Pos y )
550 FT_Error error = CFF_Err_Ok;
553 /* test whether we are building a new contour */
554 if ( !builder->path_begun )
556 builder->path_begun = 1;
557 error = cff_builder_add_contour( builder );
558 if ( !error )
559 error = cff_builder_add_point1( builder, x, y );
562 return error;
566 /* close the current contour */
567 static void
568 cff_builder_close_contour( CFF_Builder* builder )
570 FT_Outline* outline = builder->current;
571 FT_Int first;
574 if ( !outline )
575 return;
577 first = outline->n_contours <= 1
578 ? 0 : outline->contours[outline->n_contours - 2] + 1;
580 /* We must not include the last point in the path if it */
581 /* is located on the first point. */
582 if ( outline->n_points > 1 )
584 FT_Vector* p1 = outline->points + first;
585 FT_Vector* p2 = outline->points + outline->n_points - 1;
586 FT_Byte* control = (FT_Byte*)outline->tags + outline->n_points - 1;
589 /* `delete' last point only if it coincides with the first */
590 /* point and if it is not a control point (which can happen). */
591 if ( p1->x == p2->x && p1->y == p2->y )
592 if ( *control == FT_CURVE_TAG_ON )
593 outline->n_points--;
596 if ( outline->n_contours > 0 )
598 /* Don't add contours only consisting of one point, i.e., */
599 /* check whether begin point and last point are the same. */
600 if ( first == outline->n_points - 1 )
602 outline->n_contours--;
603 outline->n_points--;
605 else
606 outline->contours[outline->n_contours - 1] =
607 (short)( outline->n_points - 1 );
612 static FT_Int
613 cff_lookup_glyph_by_stdcharcode( CFF_Font cff,
614 FT_Int charcode )
616 FT_UInt n;
617 FT_UShort glyph_sid;
620 /* CID-keyed fonts don't have glyph names */
621 if ( !cff->charset.sids )
622 return -1;
624 /* check range of standard char code */
625 if ( charcode < 0 || charcode > 255 )
626 return -1;
628 /* Get code to SID mapping from `cff_standard_encoding'. */
629 glyph_sid = cff_get_standard_encoding( (FT_UInt)charcode );
631 for ( n = 0; n < cff->num_glyphs; n++ )
633 if ( cff->charset.sids[n] == glyph_sid )
634 return n;
637 return -1;
641 static FT_Error
642 cff_get_glyph_data( TT_Face face,
643 FT_UInt glyph_index,
644 FT_Byte** pointer,
645 FT_ULong* length )
647 #ifdef FT_CONFIG_OPTION_INCREMENTAL
648 /* For incremental fonts get the character data using the */
649 /* callback function. */
650 if ( face->root.internal->incremental_interface )
652 FT_Data data;
653 FT_Error error =
654 face->root.internal->incremental_interface->funcs->get_glyph_data(
655 face->root.internal->incremental_interface->object,
656 glyph_index, &data );
659 *pointer = (FT_Byte*)data.pointer;
660 *length = data.length;
662 return error;
664 else
665 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
668 CFF_Font cff = (CFF_Font)(face->extra.data);
671 return cff_index_access_element( &cff->charstrings_index, glyph_index,
672 pointer, length );
677 static void
678 cff_free_glyph_data( TT_Face face,
679 FT_Byte** pointer,
680 FT_ULong length )
682 #ifndef FT_CONFIG_OPTION_INCREMENTAL
683 FT_UNUSED( length );
684 #endif
686 #ifdef FT_CONFIG_OPTION_INCREMENTAL
687 /* For incremental fonts get the character data using the */
688 /* callback function. */
689 if ( face->root.internal->incremental_interface )
691 FT_Data data;
694 data.pointer = *pointer;
695 data.length = length;
697 face->root.internal->incremental_interface->funcs->free_glyph_data(
698 face->root.internal->incremental_interface->object, &data );
700 else
701 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
704 CFF_Font cff = (CFF_Font)(face->extra.data);
707 cff_index_forget_element( &cff->charstrings_index, pointer );
712 static FT_Error
713 cff_operator_seac( CFF_Decoder* decoder,
714 FT_Pos asb,
715 FT_Pos adx,
716 FT_Pos ady,
717 FT_Int bchar,
718 FT_Int achar )
720 FT_Error error;
721 CFF_Builder* builder = &decoder->builder;
722 FT_Int bchar_index, achar_index;
723 TT_Face face = decoder->builder.face;
724 FT_Vector left_bearing, advance;
725 FT_Byte* charstring;
726 FT_ULong charstring_len;
727 FT_Pos glyph_width;
730 if ( decoder->seac )
732 FT_ERROR(( "cff_operator_seac: invalid nested seac\n" ));
733 return CFF_Err_Syntax_Error;
736 adx += decoder->builder.left_bearing.x;
737 ady += decoder->builder.left_bearing.y;
739 #ifdef FT_CONFIG_OPTION_INCREMENTAL
740 /* Incremental fonts don't necessarily have valid charsets. */
741 /* They use the character code, not the glyph index, in this case. */
742 if ( face->root.internal->incremental_interface )
744 bchar_index = bchar;
745 achar_index = achar;
747 else
748 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
750 CFF_Font cff = (CFF_Font)(face->extra.data);
753 bchar_index = cff_lookup_glyph_by_stdcharcode( cff, bchar );
754 achar_index = cff_lookup_glyph_by_stdcharcode( cff, achar );
757 if ( bchar_index < 0 || achar_index < 0 )
759 FT_ERROR(( "cff_operator_seac:"
760 " invalid seac character code arguments\n" ));
761 return CFF_Err_Syntax_Error;
764 /* If we are trying to load a composite glyph, do not load the */
765 /* accent character and return the array of subglyphs. */
766 if ( builder->no_recurse )
768 FT_GlyphSlot glyph = (FT_GlyphSlot)builder->glyph;
769 FT_GlyphLoader loader = glyph->internal->loader;
770 FT_SubGlyph subg;
773 /* reallocate subglyph array if necessary */
774 error = FT_GlyphLoader_CheckSubGlyphs( loader, 2 );
775 if ( error )
776 goto Exit;
778 subg = loader->current.subglyphs;
780 /* subglyph 0 = base character */
781 subg->index = bchar_index;
782 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES |
783 FT_SUBGLYPH_FLAG_USE_MY_METRICS;
784 subg->arg1 = 0;
785 subg->arg2 = 0;
786 subg++;
788 /* subglyph 1 = accent character */
789 subg->index = achar_index;
790 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES;
791 subg->arg1 = (FT_Int)( adx >> 16 );
792 subg->arg2 = (FT_Int)( ady >> 16 );
794 /* set up remaining glyph fields */
795 glyph->num_subglyphs = 2;
796 glyph->subglyphs = loader->base.subglyphs;
797 glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
799 loader->current.num_subglyphs = 2;
802 FT_GlyphLoader_Prepare( builder->loader );
804 /* First load `bchar' in builder */
805 error = cff_get_glyph_data( face, bchar_index,
806 &charstring, &charstring_len );
807 if ( !error )
809 /* the seac operator must not be nested */
810 decoder->seac = TRUE;
811 error = cff_decoder_parse_charstrings( decoder, charstring,
812 charstring_len );
813 decoder->seac = FALSE;
815 if ( error )
816 goto Exit;
818 cff_free_glyph_data( face, &charstring, charstring_len );
821 /* Save the left bearing, advance and glyph width of the base */
822 /* character as they will be erased by the next load. */
824 left_bearing = builder->left_bearing;
825 advance = builder->advance;
826 glyph_width = decoder->glyph_width;
828 builder->left_bearing.x = 0;
829 builder->left_bearing.y = 0;
831 builder->pos_x = adx - asb;
832 builder->pos_y = ady;
834 /* Now load `achar' on top of the base outline. */
835 error = cff_get_glyph_data( face, achar_index,
836 &charstring, &charstring_len );
837 if ( !error )
839 /* the seac operator must not be nested */
840 decoder->seac = TRUE;
841 error = cff_decoder_parse_charstrings( decoder, charstring,
842 charstring_len );
843 decoder->seac = FALSE;
845 if ( error )
846 goto Exit;
848 cff_free_glyph_data( face, &charstring, charstring_len );
851 /* Restore the left side bearing, advance and glyph width */
852 /* of the base character. */
853 builder->left_bearing = left_bearing;
854 builder->advance = advance;
855 decoder->glyph_width = glyph_width;
857 builder->pos_x = 0;
858 builder->pos_y = 0;
860 Exit:
861 return error;
865 /*************************************************************************/
866 /* */
867 /* <Function> */
868 /* cff_decoder_parse_charstrings */
869 /* */
870 /* <Description> */
871 /* Parses a given Type 2 charstrings program. */
872 /* */
873 /* <InOut> */
874 /* decoder :: The current Type 1 decoder. */
875 /* */
876 /* <Input> */
877 /* charstring_base :: The base of the charstring stream. */
878 /* */
879 /* charstring_len :: The length in bytes of the charstring stream. */
880 /* */
881 /* <Return> */
882 /* FreeType error code. 0 means success. */
883 /* */
884 FT_LOCAL_DEF( FT_Error )
885 cff_decoder_parse_charstrings( CFF_Decoder* decoder,
886 FT_Byte* charstring_base,
887 FT_ULong charstring_len )
889 FT_Error error;
890 CFF_Decoder_Zone* zone;
891 FT_Byte* ip;
892 FT_Byte* limit;
893 CFF_Builder* builder = &decoder->builder;
894 FT_Pos x, y;
895 FT_Fixed seed;
896 FT_Fixed* stack;
897 FT_Int charstring_type =
898 decoder->cff->top_font.font_dict.charstring_type;
900 T2_Hints_Funcs hinter;
903 /* set default width */
904 decoder->num_hints = 0;
905 decoder->read_width = 1;
907 /* compute random seed from stack address of parameter */
908 seed = (FT_Fixed)( ( (FT_PtrDist)(char*)&seed ^
909 (FT_PtrDist)(char*)&decoder ^
910 (FT_PtrDist)(char*)&charstring_base ) &
911 FT_ULONG_MAX ) ;
912 seed = ( seed ^ ( seed >> 10 ) ^ ( seed >> 20 ) ) & 0xFFFFL;
913 if ( seed == 0 )
914 seed = 0x7384;
916 /* initialize the decoder */
917 decoder->top = decoder->stack;
918 decoder->zone = decoder->zones;
919 zone = decoder->zones;
920 stack = decoder->top;
922 hinter = (T2_Hints_Funcs)builder->hints_funcs;
924 builder->path_begun = 0;
926 zone->base = charstring_base;
927 limit = zone->limit = charstring_base + charstring_len;
928 ip = zone->cursor = zone->base;
930 error = CFF_Err_Ok;
932 x = builder->pos_x;
933 y = builder->pos_y;
935 /* begin hints recording session, if any */
936 if ( hinter )
937 hinter->open( hinter->hints );
939 /* now execute loop */
940 while ( ip < limit )
942 CFF_Operator op;
943 FT_Byte v;
946 /********************************************************************/
947 /* */
948 /* Decode operator or operand */
949 /* */
950 v = *ip++;
951 if ( v >= 32 || v == 28 )
953 FT_Int shift = 16;
954 FT_Int32 val;
957 /* this is an operand, push it on the stack */
958 if ( v == 28 )
960 if ( ip + 1 >= limit )
961 goto Syntax_Error;
962 val = (FT_Short)( ( (FT_Short)ip[0] << 8 ) | ip[1] );
963 ip += 2;
965 else if ( v < 247 )
966 val = (FT_Int32)v - 139;
967 else if ( v < 251 )
969 if ( ip >= limit )
970 goto Syntax_Error;
971 val = ( (FT_Int32)v - 247 ) * 256 + *ip++ + 108;
973 else if ( v < 255 )
975 if ( ip >= limit )
976 goto Syntax_Error;
977 val = -( (FT_Int32)v - 251 ) * 256 - *ip++ - 108;
979 else
981 if ( ip + 3 >= limit )
982 goto Syntax_Error;
983 val = ( (FT_Int32)ip[0] << 24 ) |
984 ( (FT_Int32)ip[1] << 16 ) |
985 ( (FT_Int32)ip[2] << 8 ) |
986 ip[3];
987 ip += 4;
988 if ( charstring_type == 2 )
989 shift = 0;
991 if ( decoder->top - stack >= CFF_MAX_OPERANDS )
992 goto Stack_Overflow;
994 val <<= shift;
995 *decoder->top++ = val;
997 #ifdef FT_DEBUG_LEVEL_TRACE
998 if ( !( val & 0xFFFFL ) )
999 FT_TRACE4(( " %ld", (FT_Int32)( val >> 16 ) ));
1000 else
1001 FT_TRACE4(( " %.2f", val / 65536.0 ));
1002 #endif
1005 else
1007 /* The specification says that normally arguments are to be taken */
1008 /* from the bottom of the stack. However, this seems not to be */
1009 /* correct, at least for Acroread 7.0.8 on GNU/Linux: It pops the */
1010 /* arguments similar to a PS interpreter. */
1012 FT_Fixed* args = decoder->top;
1013 FT_Int num_args = (FT_Int)( args - decoder->stack );
1014 FT_Int req_args;
1017 /* find operator */
1018 op = cff_op_unknown;
1020 switch ( v )
1022 case 1:
1023 op = cff_op_hstem;
1024 break;
1025 case 3:
1026 op = cff_op_vstem;
1027 break;
1028 case 4:
1029 op = cff_op_vmoveto;
1030 break;
1031 case 5:
1032 op = cff_op_rlineto;
1033 break;
1034 case 6:
1035 op = cff_op_hlineto;
1036 break;
1037 case 7:
1038 op = cff_op_vlineto;
1039 break;
1040 case 8:
1041 op = cff_op_rrcurveto;
1042 break;
1043 case 9:
1044 op = cff_op_closepath;
1045 break;
1046 case 10:
1047 op = cff_op_callsubr;
1048 break;
1049 case 11:
1050 op = cff_op_return;
1051 break;
1052 case 12:
1054 if ( ip >= limit )
1055 goto Syntax_Error;
1056 v = *ip++;
1058 switch ( v )
1060 case 0:
1061 op = cff_op_dotsection;
1062 break;
1063 case 1: /* this is actually the Type1 vstem3 operator */
1064 op = cff_op_vstem;
1065 break;
1066 case 2: /* this is actually the Type1 hstem3 operator */
1067 op = cff_op_hstem;
1068 break;
1069 case 3:
1070 op = cff_op_and;
1071 break;
1072 case 4:
1073 op = cff_op_or;
1074 break;
1075 case 5:
1076 op = cff_op_not;
1077 break;
1078 case 6:
1079 op = cff_op_seac;
1080 break;
1081 case 7:
1082 op = cff_op_sbw;
1083 break;
1084 case 8:
1085 op = cff_op_store;
1086 break;
1087 case 9:
1088 op = cff_op_abs;
1089 break;
1090 case 10:
1091 op = cff_op_add;
1092 break;
1093 case 11:
1094 op = cff_op_sub;
1095 break;
1096 case 12:
1097 op = cff_op_div;
1098 break;
1099 case 13:
1100 op = cff_op_load;
1101 break;
1102 case 14:
1103 op = cff_op_neg;
1104 break;
1105 case 15:
1106 op = cff_op_eq;
1107 break;
1108 case 16:
1109 op = cff_op_callothersubr;
1110 break;
1111 case 17:
1112 op = cff_op_pop;
1113 break;
1114 case 18:
1115 op = cff_op_drop;
1116 break;
1117 case 20:
1118 op = cff_op_put;
1119 break;
1120 case 21:
1121 op = cff_op_get;
1122 break;
1123 case 22:
1124 op = cff_op_ifelse;
1125 break;
1126 case 23:
1127 op = cff_op_random;
1128 break;
1129 case 24:
1130 op = cff_op_mul;
1131 break;
1132 case 26:
1133 op = cff_op_sqrt;
1134 break;
1135 case 27:
1136 op = cff_op_dup;
1137 break;
1138 case 28:
1139 op = cff_op_exch;
1140 break;
1141 case 29:
1142 op = cff_op_index;
1143 break;
1144 case 30:
1145 op = cff_op_roll;
1146 break;
1147 case 33:
1148 op = cff_op_setcurrentpoint;
1149 break;
1150 case 34:
1151 op = cff_op_hflex;
1152 break;
1153 case 35:
1154 op = cff_op_flex;
1155 break;
1156 case 36:
1157 op = cff_op_hflex1;
1158 break;
1159 case 37:
1160 op = cff_op_flex1;
1161 break;
1162 default:
1163 /* decrement ip for syntax error message */
1164 ip--;
1167 break;
1168 case 13:
1169 op = cff_op_hsbw;
1170 break;
1171 case 14:
1172 op = cff_op_endchar;
1173 break;
1174 case 16:
1175 op = cff_op_blend;
1176 break;
1177 case 18:
1178 op = cff_op_hstemhm;
1179 break;
1180 case 19:
1181 op = cff_op_hintmask;
1182 break;
1183 case 20:
1184 op = cff_op_cntrmask;
1185 break;
1186 case 21:
1187 op = cff_op_rmoveto;
1188 break;
1189 case 22:
1190 op = cff_op_hmoveto;
1191 break;
1192 case 23:
1193 op = cff_op_vstemhm;
1194 break;
1195 case 24:
1196 op = cff_op_rcurveline;
1197 break;
1198 case 25:
1199 op = cff_op_rlinecurve;
1200 break;
1201 case 26:
1202 op = cff_op_vvcurveto;
1203 break;
1204 case 27:
1205 op = cff_op_hhcurveto;
1206 break;
1207 case 29:
1208 op = cff_op_callgsubr;
1209 break;
1210 case 30:
1211 op = cff_op_vhcurveto;
1212 break;
1213 case 31:
1214 op = cff_op_hvcurveto;
1215 break;
1216 default:
1217 break;
1220 if ( op == cff_op_unknown )
1221 goto Syntax_Error;
1223 /* check arguments */
1224 req_args = cff_argument_counts[op];
1225 if ( req_args & CFF_COUNT_CHECK_WIDTH )
1227 if ( num_args > 0 && decoder->read_width )
1229 /* If `nominal_width' is non-zero, the number is really a */
1230 /* difference against `nominal_width'. Else, the number here */
1231 /* is truly a width, not a difference against `nominal_width'. */
1232 /* If the font does not set `nominal_width', then */
1233 /* `nominal_width' defaults to zero, and so we can set */
1234 /* `glyph_width' to `nominal_width' plus number on the stack */
1235 /* -- for either case. */
1237 FT_Int set_width_ok;
1240 switch ( op )
1242 case cff_op_hmoveto:
1243 case cff_op_vmoveto:
1244 set_width_ok = num_args & 2;
1245 break;
1247 case cff_op_hstem:
1248 case cff_op_vstem:
1249 case cff_op_hstemhm:
1250 case cff_op_vstemhm:
1251 case cff_op_rmoveto:
1252 case cff_op_hintmask:
1253 case cff_op_cntrmask:
1254 set_width_ok = num_args & 1;
1255 break;
1257 case cff_op_endchar:
1258 /* If there is a width specified for endchar, we either have */
1259 /* 1 argument or 5 arguments. We like to argue. */
1260 set_width_ok = ( num_args == 5 ) || ( num_args == 1 );
1261 break;
1263 default:
1264 set_width_ok = 0;
1265 break;
1268 if ( set_width_ok )
1270 decoder->glyph_width = decoder->nominal_width +
1271 ( stack[0] >> 16 );
1273 if ( decoder->width_only )
1275 /* we only want the advance width; stop here */
1276 break;
1279 /* Consumed an argument. */
1280 num_args--;
1284 decoder->read_width = 0;
1285 req_args = 0;
1288 req_args &= 0x000F;
1289 if ( num_args < req_args )
1290 goto Stack_Underflow;
1291 args -= req_args;
1292 num_args -= req_args;
1294 /* At this point, `args' points to the first argument of the */
1295 /* operand in case `req_args' isn't zero. Otherwise, we have */
1296 /* to adjust `args' manually. */
1298 /* Note that we only pop arguments from the stack which we */
1299 /* really need and can digest so that we can continue in case */
1300 /* of superfluous stack elements. */
1302 switch ( op )
1304 case cff_op_hstem:
1305 case cff_op_vstem:
1306 case cff_op_hstemhm:
1307 case cff_op_vstemhm:
1308 /* the number of arguments is always even here */
1309 FT_TRACE4((
1310 op == cff_op_hstem ? " hstem\n" :
1311 ( op == cff_op_vstem ? " vstem\n" :
1312 ( op == cff_op_hstemhm ? " hstemhm\n" : " vstemhm\n" ) ) ));
1314 if ( hinter )
1315 hinter->stems( hinter->hints,
1316 ( op == cff_op_hstem || op == cff_op_hstemhm ),
1317 num_args / 2,
1318 args - ( num_args & ~1 ) );
1320 decoder->num_hints += num_args / 2;
1321 args = stack;
1322 break;
1324 case cff_op_hintmask:
1325 case cff_op_cntrmask:
1326 FT_TRACE4(( op == cff_op_hintmask ? " hintmask" : " cntrmask" ));
1328 /* implement vstem when needed -- */
1329 /* the specification doesn't say it, but this also works */
1330 /* with the 'cntrmask' operator */
1331 /* */
1332 if ( num_args > 0 )
1334 if ( hinter )
1335 hinter->stems( hinter->hints,
1337 num_args / 2,
1338 args - ( num_args & ~1 ) );
1340 decoder->num_hints += num_args / 2;
1343 if ( hinter )
1345 if ( op == cff_op_hintmask )
1346 hinter->hintmask( hinter->hints,
1347 builder->current->n_points,
1348 decoder->num_hints,
1349 ip );
1350 else
1351 hinter->counter( hinter->hints,
1352 decoder->num_hints,
1353 ip );
1356 #ifdef FT_DEBUG_LEVEL_TRACE
1358 FT_UInt maskbyte;
1361 FT_TRACE4(( " (maskbytes: " ));
1363 for ( maskbyte = 0;
1364 maskbyte < (FT_UInt)(( decoder->num_hints + 7 ) >> 3);
1365 maskbyte++, ip++ )
1366 FT_TRACE4(( "0x%02X", *ip ));
1368 FT_TRACE4(( ")\n" ));
1370 #else
1371 ip += ( decoder->num_hints + 7 ) >> 3;
1372 #endif
1373 if ( ip >= limit )
1374 goto Syntax_Error;
1375 args = stack;
1376 break;
1378 case cff_op_rmoveto:
1379 FT_TRACE4(( " rmoveto\n" ));
1381 cff_builder_close_contour( builder );
1382 builder->path_begun = 0;
1383 x += args[-2];
1384 y += args[-1];
1385 args = stack;
1386 break;
1388 case cff_op_vmoveto:
1389 FT_TRACE4(( " vmoveto\n" ));
1391 cff_builder_close_contour( builder );
1392 builder->path_begun = 0;
1393 y += args[-1];
1394 args = stack;
1395 break;
1397 case cff_op_hmoveto:
1398 FT_TRACE4(( " hmoveto\n" ));
1400 cff_builder_close_contour( builder );
1401 builder->path_begun = 0;
1402 x += args[-1];
1403 args = stack;
1404 break;
1406 case cff_op_rlineto:
1407 FT_TRACE4(( " rlineto\n" ));
1409 if ( cff_builder_start_point ( builder, x, y ) ||
1410 check_points( builder, num_args / 2 ) )
1411 goto Fail;
1413 if ( num_args < 2 )
1414 goto Stack_Underflow;
1416 args -= num_args & ~1;
1417 while ( args < decoder->top )
1419 x += args[0];
1420 y += args[1];
1421 cff_builder_add_point( builder, x, y, 1 );
1422 args += 2;
1424 args = stack;
1425 break;
1427 case cff_op_hlineto:
1428 case cff_op_vlineto:
1430 FT_Int phase = ( op == cff_op_hlineto );
1433 FT_TRACE4(( op == cff_op_hlineto ? " hlineto\n"
1434 : " vlineto\n" ));
1436 if ( num_args < 1 )
1437 goto Stack_Underflow;
1439 if ( cff_builder_start_point ( builder, x, y ) ||
1440 check_points( builder, num_args ) )
1441 goto Fail;
1443 args = stack;
1444 while ( args < decoder->top )
1446 if ( phase )
1447 x += args[0];
1448 else
1449 y += args[0];
1451 if ( cff_builder_add_point1( builder, x, y ) )
1452 goto Fail;
1454 args++;
1455 phase ^= 1;
1457 args = stack;
1459 break;
1461 case cff_op_rrcurveto:
1463 FT_Int nargs;
1466 FT_TRACE4(( " rrcurveto\n" ));
1468 if ( num_args < 6 )
1469 goto Stack_Underflow;
1471 nargs = num_args - num_args % 6;
1473 if ( cff_builder_start_point ( builder, x, y ) ||
1474 check_points( builder, nargs / 2 ) )
1475 goto Fail;
1477 args -= nargs;
1478 while ( args < decoder->top )
1480 x += args[0];
1481 y += args[1];
1482 cff_builder_add_point( builder, x, y, 0 );
1483 x += args[2];
1484 y += args[3];
1485 cff_builder_add_point( builder, x, y, 0 );
1486 x += args[4];
1487 y += args[5];
1488 cff_builder_add_point( builder, x, y, 1 );
1489 args += 6;
1491 args = stack;
1493 break;
1495 case cff_op_vvcurveto:
1497 FT_Int nargs;
1500 FT_TRACE4(( " vvcurveto\n" ));
1502 if ( num_args < 4 )
1503 goto Stack_Underflow;
1505 /* if num_args isn't of the form 4n or 4n+1, */
1506 /* we reduce it to 4n+1 */
1508 nargs = num_args - num_args % 4;
1509 if ( num_args - nargs > 0 )
1510 nargs += 1;
1512 if ( cff_builder_start_point( builder, x, y ) )
1513 goto Fail;
1515 args -= nargs;
1517 if ( nargs & 1 )
1519 x += args[0];
1520 args++;
1521 nargs--;
1524 if ( check_points( builder, 3 * ( nargs / 4 ) ) )
1525 goto Fail;
1527 while ( args < decoder->top )
1529 y += args[0];
1530 cff_builder_add_point( builder, x, y, 0 );
1531 x += args[1];
1532 y += args[2];
1533 cff_builder_add_point( builder, x, y, 0 );
1534 y += args[3];
1535 cff_builder_add_point( builder, x, y, 1 );
1536 args += 4;
1538 args = stack;
1540 break;
1542 case cff_op_hhcurveto:
1544 FT_Int nargs;
1547 FT_TRACE4(( " hhcurveto\n" ));
1549 if ( num_args < 4 )
1550 goto Stack_Underflow;
1552 /* if num_args isn't of the form 4n or 4n+1, */
1553 /* we reduce it to 4n+1 */
1555 nargs = num_args - num_args % 4;
1556 if ( num_args - nargs > 0 )
1557 nargs += 1;
1559 if ( cff_builder_start_point( builder, x, y ) )
1560 goto Fail;
1562 args -= nargs;
1563 if ( nargs & 1 )
1565 y += args[0];
1566 args++;
1567 nargs--;
1570 if ( check_points( builder, 3 * ( nargs / 4 ) ) )
1571 goto Fail;
1573 while ( args < decoder->top )
1575 x += args[0];
1576 cff_builder_add_point( builder, x, y, 0 );
1577 x += args[1];
1578 y += args[2];
1579 cff_builder_add_point( builder, x, y, 0 );
1580 x += args[3];
1581 cff_builder_add_point( builder, x, y, 1 );
1582 args += 4;
1584 args = stack;
1586 break;
1588 case cff_op_vhcurveto:
1589 case cff_op_hvcurveto:
1591 FT_Int phase;
1592 FT_Int nargs;
1595 FT_TRACE4(( op == cff_op_vhcurveto ? " vhcurveto\n"
1596 : " hvcurveto\n" ));
1598 if ( cff_builder_start_point( builder, x, y ) )
1599 goto Fail;
1601 if ( num_args < 4 )
1602 goto Stack_Underflow;
1604 /* if num_args isn't of the form 8n, 8n+1, 8n+4, or 8n+5, */
1605 /* we reduce it to the largest one which fits */
1607 nargs = num_args - num_args % 4;
1608 if ( num_args - nargs > 0 )
1609 nargs += 1;
1611 args -= nargs;
1612 if ( check_points( builder, ( nargs / 4 ) * 3 ) )
1613 goto Stack_Underflow;
1615 phase = ( op == cff_op_hvcurveto );
1617 while ( nargs >= 4 )
1619 nargs -= 4;
1620 if ( phase )
1622 x += args[0];
1623 cff_builder_add_point( builder, x, y, 0 );
1624 x += args[1];
1625 y += args[2];
1626 cff_builder_add_point( builder, x, y, 0 );
1627 y += args[3];
1628 if ( nargs == 1 )
1629 x += args[4];
1630 cff_builder_add_point( builder, x, y, 1 );
1632 else
1634 y += args[0];
1635 cff_builder_add_point( builder, x, y, 0 );
1636 x += args[1];
1637 y += args[2];
1638 cff_builder_add_point( builder, x, y, 0 );
1639 x += args[3];
1640 if ( nargs == 1 )
1641 y += args[4];
1642 cff_builder_add_point( builder, x, y, 1 );
1644 args += 4;
1645 phase ^= 1;
1647 args = stack;
1649 break;
1651 case cff_op_rlinecurve:
1653 FT_Int num_lines;
1654 FT_Int nargs;
1657 FT_TRACE4(( " rlinecurve\n" ));
1659 if ( num_args < 8 )
1660 goto Stack_Underflow;
1662 nargs = num_args & ~1;
1663 num_lines = ( nargs - 6 ) / 2;
1665 if ( cff_builder_start_point( builder, x, y ) ||
1666 check_points( builder, num_lines + 3 ) )
1667 goto Fail;
1669 args -= nargs;
1671 /* first, add the line segments */
1672 while ( num_lines > 0 )
1674 x += args[0];
1675 y += args[1];
1676 cff_builder_add_point( builder, x, y, 1 );
1677 args += 2;
1678 num_lines--;
1681 /* then the curve */
1682 x += args[0];
1683 y += args[1];
1684 cff_builder_add_point( builder, x, y, 0 );
1685 x += args[2];
1686 y += args[3];
1687 cff_builder_add_point( builder, x, y, 0 );
1688 x += args[4];
1689 y += args[5];
1690 cff_builder_add_point( builder, x, y, 1 );
1691 args = stack;
1693 break;
1695 case cff_op_rcurveline:
1697 FT_Int num_curves;
1698 FT_Int nargs;
1701 FT_TRACE4(( " rcurveline\n" ));
1703 if ( num_args < 8 )
1704 goto Stack_Underflow;
1706 nargs = num_args - 2;
1707 nargs = nargs - nargs % 6 + 2;
1708 num_curves = ( nargs - 2 ) / 6;
1710 if ( cff_builder_start_point ( builder, x, y ) ||
1711 check_points( builder, num_curves * 3 + 2 ) )
1712 goto Fail;
1714 args -= nargs;
1716 /* first, add the curves */
1717 while ( num_curves > 0 )
1719 x += args[0];
1720 y += args[1];
1721 cff_builder_add_point( builder, x, y, 0 );
1722 x += args[2];
1723 y += args[3];
1724 cff_builder_add_point( builder, x, y, 0 );
1725 x += args[4];
1726 y += args[5];
1727 cff_builder_add_point( builder, x, y, 1 );
1728 args += 6;
1729 num_curves--;
1732 /* then the final line */
1733 x += args[0];
1734 y += args[1];
1735 cff_builder_add_point( builder, x, y, 1 );
1736 args = stack;
1738 break;
1740 case cff_op_hflex1:
1742 FT_Pos start_y;
1745 FT_TRACE4(( " hflex1\n" ));
1747 /* adding five more points: 4 control points, 1 on-curve point */
1748 /* -- make sure we have enough space for the start point if it */
1749 /* needs to be added */
1750 if ( cff_builder_start_point( builder, x, y ) ||
1751 check_points( builder, 6 ) )
1752 goto Fail;
1754 /* record the starting point's y position for later use */
1755 start_y = y;
1757 /* first control point */
1758 x += args[0];
1759 y += args[1];
1760 cff_builder_add_point( builder, x, y, 0 );
1762 /* second control point */
1763 x += args[2];
1764 y += args[3];
1765 cff_builder_add_point( builder, x, y, 0 );
1767 /* join point; on curve, with y-value the same as the last */
1768 /* control point's y-value */
1769 x += args[4];
1770 cff_builder_add_point( builder, x, y, 1 );
1772 /* third control point, with y-value the same as the join */
1773 /* point's y-value */
1774 x += args[5];
1775 cff_builder_add_point( builder, x, y, 0 );
1777 /* fourth control point */
1778 x += args[6];
1779 y += args[7];
1780 cff_builder_add_point( builder, x, y, 0 );
1782 /* ending point, with y-value the same as the start */
1783 x += args[8];
1784 y = start_y;
1785 cff_builder_add_point( builder, x, y, 1 );
1787 args = stack;
1788 break;
1791 case cff_op_hflex:
1793 FT_Pos start_y;
1796 FT_TRACE4(( " hflex\n" ));
1798 /* adding six more points; 4 control points, 2 on-curve points */
1799 if ( cff_builder_start_point( builder, x, y ) ||
1800 check_points( builder, 6 ) )
1801 goto Fail;
1803 /* record the starting point's y-position for later use */
1804 start_y = y;
1806 /* first control point */
1807 x += args[0];
1808 cff_builder_add_point( builder, x, y, 0 );
1810 /* second control point */
1811 x += args[1];
1812 y += args[2];
1813 cff_builder_add_point( builder, x, y, 0 );
1815 /* join point; on curve, with y-value the same as the last */
1816 /* control point's y-value */
1817 x += args[3];
1818 cff_builder_add_point( builder, x, y, 1 );
1820 /* third control point, with y-value the same as the join */
1821 /* point's y-value */
1822 x += args[4];
1823 cff_builder_add_point( builder, x, y, 0 );
1825 /* fourth control point */
1826 x += args[5];
1827 y = start_y;
1828 cff_builder_add_point( builder, x, y, 0 );
1830 /* ending point, with y-value the same as the start point's */
1831 /* y-value -- we don't add this point, though */
1832 x += args[6];
1833 cff_builder_add_point( builder, x, y, 1 );
1835 args = stack;
1836 break;
1839 case cff_op_flex1:
1841 FT_Pos start_x, start_y; /* record start x, y values for */
1842 /* alter use */
1843 FT_Fixed dx = 0, dy = 0; /* used in horizontal/vertical */
1844 /* algorithm below */
1845 FT_Int horizontal, count;
1846 FT_Fixed* temp;
1849 FT_TRACE4(( " flex1\n" ));
1851 /* adding six more points; 4 control points, 2 on-curve points */
1852 if ( cff_builder_start_point( builder, x, y ) ||
1853 check_points( builder, 6 ) )
1854 goto Fail;
1856 /* record the starting point's x, y position for later use */
1857 start_x = x;
1858 start_y = y;
1860 /* XXX: figure out whether this is supposed to be a horizontal */
1861 /* or vertical flex; the Type 2 specification is vague... */
1863 temp = args;
1865 /* grab up to the last argument */
1866 for ( count = 5; count > 0; count-- )
1868 dx += temp[0];
1869 dy += temp[1];
1870 temp += 2;
1873 if ( dx < 0 )
1874 dx = -dx;
1875 if ( dy < 0 )
1876 dy = -dy;
1878 /* strange test, but here it is... */
1879 horizontal = ( dx > dy );
1881 for ( count = 5; count > 0; count-- )
1883 x += args[0];
1884 y += args[1];
1885 cff_builder_add_point( builder, x, y,
1886 (FT_Bool)( count == 3 ) );
1887 args += 2;
1890 /* is last operand an x- or y-delta? */
1891 if ( horizontal )
1893 x += args[0];
1894 y = start_y;
1896 else
1898 x = start_x;
1899 y += args[0];
1902 cff_builder_add_point( builder, x, y, 1 );
1904 args = stack;
1905 break;
1908 case cff_op_flex:
1910 FT_UInt count;
1913 FT_TRACE4(( " flex\n" ));
1915 if ( cff_builder_start_point( builder, x, y ) ||
1916 check_points( builder, 6 ) )
1917 goto Fail;
1919 for ( count = 6; count > 0; count-- )
1921 x += args[0];
1922 y += args[1];
1923 cff_builder_add_point( builder, x, y,
1924 (FT_Bool)( count == 4 || count == 1 ) );
1925 args += 2;
1928 args = stack;
1930 break;
1932 case cff_op_seac:
1933 FT_TRACE4(( " seac\n" ));
1935 error = cff_operator_seac( decoder,
1936 args[0], args[1], args[2],
1937 (FT_Int)( args[3] >> 16 ),
1938 (FT_Int)( args[4] >> 16 ) );
1940 /* add current outline to the glyph slot */
1941 FT_GlyphLoader_Add( builder->loader );
1943 /* return now! */
1944 FT_TRACE4(( "\n" ));
1945 return error;
1947 case cff_op_endchar:
1948 FT_TRACE4(( " endchar\n" ));
1950 /* We are going to emulate the seac operator. */
1951 if ( num_args >= 4 )
1953 /* Save glyph width so that the subglyphs don't overwrite it. */
1954 FT_Pos glyph_width = decoder->glyph_width;
1956 error = cff_operator_seac( decoder,
1957 0L, args[-4], args[-3],
1958 (FT_Int)( args[-2] >> 16 ),
1959 (FT_Int)( args[-1] >> 16 ) );
1961 decoder->glyph_width = glyph_width;
1963 else
1965 if ( !error )
1966 error = CFF_Err_Ok;
1968 cff_builder_close_contour( builder );
1970 /* close hints recording session */
1971 if ( hinter )
1973 if ( hinter->close( hinter->hints,
1974 builder->current->n_points ) )
1975 goto Syntax_Error;
1977 /* apply hints to the loaded glyph outline now */
1978 hinter->apply( hinter->hints,
1979 builder->current,
1980 (PSH_Globals)builder->hints_globals,
1981 decoder->hint_mode );
1984 /* add current outline to the glyph slot */
1985 FT_GlyphLoader_Add( builder->loader );
1988 /* return now! */
1989 FT_TRACE4(( "\n" ));
1990 return error;
1992 case cff_op_abs:
1993 FT_TRACE4(( " abs\n" ));
1995 if ( args[0] < 0 )
1996 args[0] = -args[0];
1997 args++;
1998 break;
2000 case cff_op_add:
2001 FT_TRACE4(( " add\n" ));
2003 args[0] += args[1];
2004 args++;
2005 break;
2007 case cff_op_sub:
2008 FT_TRACE4(( " sub\n" ));
2010 args[0] -= args[1];
2011 args++;
2012 break;
2014 case cff_op_div:
2015 FT_TRACE4(( " div\n" ));
2017 args[0] = FT_DivFix( args[0], args[1] );
2018 args++;
2019 break;
2021 case cff_op_neg:
2022 FT_TRACE4(( " neg\n" ));
2024 args[0] = -args[0];
2025 args++;
2026 break;
2028 case cff_op_random:
2030 FT_Fixed Rand;
2033 FT_TRACE4(( " rand\n" ));
2035 Rand = seed;
2036 if ( Rand >= 0x8000L )
2037 Rand++;
2039 args[0] = Rand;
2040 seed = FT_MulFix( seed, 0x10000L - seed );
2041 if ( seed == 0 )
2042 seed += 0x2873;
2043 args++;
2045 break;
2047 case cff_op_mul:
2048 FT_TRACE4(( " mul\n" ));
2050 args[0] = FT_MulFix( args[0], args[1] );
2051 args++;
2052 break;
2054 case cff_op_sqrt:
2055 FT_TRACE4(( " sqrt\n" ));
2057 if ( args[0] > 0 )
2059 FT_Int count = 9;
2060 FT_Fixed root = args[0];
2061 FT_Fixed new_root;
2064 for (;;)
2066 new_root = ( root + FT_DivFix( args[0], root ) + 1 ) >> 1;
2067 if ( new_root == root || count <= 0 )
2068 break;
2069 root = new_root;
2071 args[0] = new_root;
2073 else
2074 args[0] = 0;
2075 args++;
2076 break;
2078 case cff_op_drop:
2079 /* nothing */
2080 FT_TRACE4(( " drop\n" ));
2082 break;
2084 case cff_op_exch:
2086 FT_Fixed tmp;
2089 FT_TRACE4(( " exch\n" ));
2091 tmp = args[0];
2092 args[0] = args[1];
2093 args[1] = tmp;
2094 args += 2;
2096 break;
2098 case cff_op_index:
2100 FT_Int idx = (FT_Int)( args[0] >> 16 );
2103 FT_TRACE4(( " index\n" ));
2105 if ( idx < 0 )
2106 idx = 0;
2107 else if ( idx > num_args - 2 )
2108 idx = num_args - 2;
2109 args[0] = args[-( idx + 1 )];
2110 args++;
2112 break;
2114 case cff_op_roll:
2116 FT_Int count = (FT_Int)( args[0] >> 16 );
2117 FT_Int idx = (FT_Int)( args[1] >> 16 );
2120 FT_TRACE4(( " roll\n" ));
2122 if ( count <= 0 )
2123 count = 1;
2125 args -= count;
2126 if ( args < stack )
2127 goto Stack_Underflow;
2129 if ( idx >= 0 )
2131 while ( idx > 0 )
2133 FT_Fixed tmp = args[count - 1];
2134 FT_Int i;
2137 for ( i = count - 2; i >= 0; i-- )
2138 args[i + 1] = args[i];
2139 args[0] = tmp;
2140 idx--;
2143 else
2145 while ( idx < 0 )
2147 FT_Fixed tmp = args[0];
2148 FT_Int i;
2151 for ( i = 0; i < count - 1; i++ )
2152 args[i] = args[i + 1];
2153 args[count - 1] = tmp;
2154 idx++;
2157 args += count;
2159 break;
2161 case cff_op_dup:
2162 FT_TRACE4(( " dup\n" ));
2164 args[1] = args[0];
2165 args += 2;
2166 break;
2168 case cff_op_put:
2170 FT_Fixed val = args[0];
2171 FT_Int idx = (FT_Int)( args[1] >> 16 );
2174 FT_TRACE4(( " put\n" ));
2176 if ( idx >= 0 && idx < CFF_MAX_TRANS_ELEMENTS )
2177 decoder->buildchar[idx] = val;
2179 break;
2181 case cff_op_get:
2183 FT_Int idx = (FT_Int)( args[0] >> 16 );
2184 FT_Fixed val = 0;
2187 FT_TRACE4(( " get\n" ));
2189 if ( idx >= 0 && idx < CFF_MAX_TRANS_ELEMENTS )
2190 val = decoder->buildchar[idx];
2192 args[0] = val;
2193 args++;
2195 break;
2197 case cff_op_store:
2198 FT_TRACE4(( " store\n"));
2200 goto Unimplemented;
2202 case cff_op_load:
2203 FT_TRACE4(( " load\n" ));
2205 goto Unimplemented;
2207 case cff_op_dotsection:
2208 /* this operator is deprecated and ignored by the parser */
2209 FT_TRACE4(( " dotsection\n" ));
2210 break;
2212 case cff_op_closepath:
2213 /* this is an invalid Type 2 operator; however, there */
2214 /* exist fonts which are incorrectly converted from probably */
2215 /* Type 1 to CFF, and some parsers seem to accept it */
2217 FT_TRACE4(( " closepath (invalid op)\n" ));
2219 args = stack;
2220 break;
2222 case cff_op_hsbw:
2223 /* this is an invalid Type 2 operator; however, there */
2224 /* exist fonts which are incorrectly converted from probably */
2225 /* Type 1 to CFF, and some parsers seem to accept it */
2227 FT_TRACE4(( " hsbw (invalid op)\n" ));
2229 decoder->glyph_width = decoder->nominal_width + ( args[1] >> 16 );
2231 decoder->builder.left_bearing.x = args[0];
2232 decoder->builder.left_bearing.y = 0;
2234 x = decoder->builder.pos_x + args[0];
2235 y = decoder->builder.pos_y;
2236 args = stack;
2237 break;
2239 case cff_op_sbw:
2240 /* this is an invalid Type 2 operator; however, there */
2241 /* exist fonts which are incorrectly converted from probably */
2242 /* Type 1 to CFF, and some parsers seem to accept it */
2244 FT_TRACE4(( " sbw (invalid op)\n" ));
2246 decoder->glyph_width = decoder->nominal_width + ( args[2] >> 16 );
2248 decoder->builder.left_bearing.x = args[0];
2249 decoder->builder.left_bearing.y = args[1];
2251 x = decoder->builder.pos_x + args[0];
2252 y = decoder->builder.pos_y + args[1];
2253 args = stack;
2254 break;
2256 case cff_op_setcurrentpoint:
2257 /* this is an invalid Type 2 operator; however, there */
2258 /* exist fonts which are incorrectly converted from probably */
2259 /* Type 1 to CFF, and some parsers seem to accept it */
2261 FT_TRACE4(( " setcurrentpoint (invalid op)\n" ));
2263 x = decoder->builder.pos_x + args[0];
2264 y = decoder->builder.pos_y + args[1];
2265 args = stack;
2266 break;
2268 case cff_op_callothersubr:
2269 /* this is an invalid Type 2 operator; however, there */
2270 /* exist fonts which are incorrectly converted from probably */
2271 /* Type 1 to CFF, and some parsers seem to accept it */
2273 FT_TRACE4(( " callothersubr (invalid op)\n" ));
2275 /* subsequent `pop' operands should add the arguments, */
2276 /* this is the implementation described for `unknown' other */
2277 /* subroutines in the Type1 spec. */
2278 args -= 2 + ( args[-2] >> 16 );
2279 break;
2281 case cff_op_pop:
2282 /* this is an invalid Type 2 operator; however, there */
2283 /* exist fonts which are incorrectly converted from probably */
2284 /* Type 1 to CFF, and some parsers seem to accept it */
2286 FT_TRACE4(( " pop (invalid op)\n" ));
2288 args++;
2289 break;
2291 case cff_op_and:
2293 FT_Fixed cond = args[0] && args[1];
2296 FT_TRACE4(( " and\n" ));
2298 args[0] = cond ? 0x10000L : 0;
2299 args++;
2301 break;
2303 case cff_op_or:
2305 FT_Fixed cond = args[0] || args[1];
2308 FT_TRACE4(( " or\n" ));
2310 args[0] = cond ? 0x10000L : 0;
2311 args++;
2313 break;
2315 case cff_op_eq:
2317 FT_Fixed cond = !args[0];
2320 FT_TRACE4(( " eq\n" ));
2322 args[0] = cond ? 0x10000L : 0;
2323 args++;
2325 break;
2327 case cff_op_ifelse:
2329 FT_Fixed cond = ( args[2] <= args[3] );
2332 FT_TRACE4(( " ifelse\n" ));
2334 if ( !cond )
2335 args[0] = args[1];
2336 args++;
2338 break;
2340 case cff_op_callsubr:
2342 FT_UInt idx = (FT_UInt)( ( args[0] >> 16 ) +
2343 decoder->locals_bias );
2346 FT_TRACE4(( " callsubr(%d)\n", idx ));
2348 if ( idx >= decoder->num_locals )
2350 FT_ERROR(( "cff_decoder_parse_charstrings:"
2351 " invalid local subr index\n" ));
2352 goto Syntax_Error;
2355 if ( zone - decoder->zones >= CFF_MAX_SUBRS_CALLS )
2357 FT_ERROR(( "cff_decoder_parse_charstrings:"
2358 " too many nested subrs\n" ));
2359 goto Syntax_Error;
2362 zone->cursor = ip; /* save current instruction pointer */
2364 zone++;
2365 zone->base = decoder->locals[idx];
2366 zone->limit = decoder->locals[idx + 1];
2367 zone->cursor = zone->base;
2369 if ( !zone->base || zone->limit == zone->base )
2371 FT_ERROR(( "cff_decoder_parse_charstrings:"
2372 " invoking empty subrs\n" ));
2373 goto Syntax_Error;
2376 decoder->zone = zone;
2377 ip = zone->base;
2378 limit = zone->limit;
2380 break;
2382 case cff_op_callgsubr:
2384 FT_UInt idx = (FT_UInt)( ( args[0] >> 16 ) +
2385 decoder->globals_bias );
2388 FT_TRACE4(( " callgsubr(%d)\n", idx ));
2390 if ( idx >= decoder->num_globals )
2392 FT_ERROR(( "cff_decoder_parse_charstrings:"
2393 " invalid global subr index\n" ));
2394 goto Syntax_Error;
2397 if ( zone - decoder->zones >= CFF_MAX_SUBRS_CALLS )
2399 FT_ERROR(( "cff_decoder_parse_charstrings:"
2400 " too many nested subrs\n" ));
2401 goto Syntax_Error;
2404 zone->cursor = ip; /* save current instruction pointer */
2406 zone++;
2407 zone->base = decoder->globals[idx];
2408 zone->limit = decoder->globals[idx + 1];
2409 zone->cursor = zone->base;
2411 if ( !zone->base || zone->limit == zone->base )
2413 FT_ERROR(( "cff_decoder_parse_charstrings:"
2414 " invoking empty subrs\n" ));
2415 goto Syntax_Error;
2418 decoder->zone = zone;
2419 ip = zone->base;
2420 limit = zone->limit;
2422 break;
2424 case cff_op_return:
2425 FT_TRACE4(( " return\n" ));
2427 if ( decoder->zone <= decoder->zones )
2429 FT_ERROR(( "cff_decoder_parse_charstrings:"
2430 " unexpected return\n" ));
2431 goto Syntax_Error;
2434 decoder->zone--;
2435 zone = decoder->zone;
2436 ip = zone->cursor;
2437 limit = zone->limit;
2438 break;
2440 default:
2441 Unimplemented:
2442 FT_ERROR(( "Unimplemented opcode: %d", ip[-1] ));
2444 if ( ip[-1] == 12 )
2445 FT_ERROR(( " %d", ip[0] ));
2446 FT_ERROR(( "\n" ));
2448 return CFF_Err_Unimplemented_Feature;
2451 decoder->top = args;
2453 } /* general operator processing */
2455 } /* while ip < limit */
2457 FT_TRACE4(( "..end..\n\n" ));
2459 Fail:
2460 return error;
2462 Syntax_Error:
2463 FT_TRACE4(( "cff_decoder_parse_charstrings: syntax error\n" ));
2464 return CFF_Err_Invalid_File_Format;
2466 Stack_Underflow:
2467 FT_TRACE4(( "cff_decoder_parse_charstrings: stack underflow\n" ));
2468 return CFF_Err_Too_Few_Arguments;
2470 Stack_Overflow:
2471 FT_TRACE4(( "cff_decoder_parse_charstrings: stack overflow\n" ));
2472 return CFF_Err_Stack_Overflow;
2476 /*************************************************************************/
2477 /*************************************************************************/
2478 /*************************************************************************/
2479 /********** *********/
2480 /********** *********/
2481 /********** COMPUTE THE MAXIMUM ADVANCE WIDTH *********/
2482 /********** *********/
2483 /********** The following code is in charge of computing *********/
2484 /********** the maximum advance width of the font. It *********/
2485 /********** quickly processes each glyph charstring to *********/
2486 /********** extract the value from either a `sbw' or `seac' *********/
2487 /********** operator. *********/
2488 /********** *********/
2489 /*************************************************************************/
2490 /*************************************************************************/
2491 /*************************************************************************/
2494 #if 0 /* unused until we support pure CFF fonts */
2497 FT_LOCAL_DEF( FT_Error )
2498 cff_compute_max_advance( TT_Face face,
2499 FT_Int* max_advance )
2501 FT_Error error = CFF_Err_Ok;
2502 CFF_Decoder decoder;
2503 FT_Int glyph_index;
2504 CFF_Font cff = (CFF_Font)face->other;
2507 *max_advance = 0;
2509 /* Initialize load decoder */
2510 cff_decoder_init( &decoder, face, 0, 0, 0, 0 );
2512 decoder.builder.metrics_only = 1;
2513 decoder.builder.load_points = 0;
2515 /* For each glyph, parse the glyph charstring and extract */
2516 /* the advance width. */
2517 for ( glyph_index = 0; glyph_index < face->root.num_glyphs;
2518 glyph_index++ )
2520 FT_Byte* charstring;
2521 FT_ULong charstring_len;
2524 /* now get load the unscaled outline */
2525 error = cff_get_glyph_data( face, glyph_index,
2526 &charstring, &charstring_len );
2527 if ( !error )
2529 error = cff_decoder_prepare( &decoder, size, glyph_index );
2530 if ( !error )
2531 error = cff_decoder_parse_charstrings( &decoder,
2532 charstring,
2533 charstring_len );
2535 cff_free_glyph_data( face, &charstring, &charstring_len );
2538 /* ignore the error if one has occurred -- skip to next glyph */
2539 error = CFF_Err_Ok;
2542 *max_advance = decoder.builder.advance.x;
2544 return CFF_Err_Ok;
2548 #endif /* 0 */
2551 FT_LOCAL_DEF( FT_Error )
2552 cff_slot_load( CFF_GlyphSlot glyph,
2553 CFF_Size size,
2554 FT_UInt glyph_index,
2555 FT_Int32 load_flags )
2557 FT_Error error;
2558 CFF_Decoder decoder;
2559 TT_Face face = (TT_Face)glyph->root.face;
2560 FT_Bool hinting, force_scaling;
2561 CFF_Font cff = (CFF_Font)face->extra.data;
2563 FT_Matrix font_matrix;
2564 FT_Vector font_offset;
2567 force_scaling = FALSE;
2569 /* in a CID-keyed font, consider `glyph_index' as a CID and map */
2570 /* it immediately to the real glyph_index -- if it isn't a */
2571 /* subsetted font, glyph_indices and CIDs are identical, though */
2572 if ( cff->top_font.font_dict.cid_registry != 0xFFFFU &&
2573 cff->charset.cids )
2575 /* don't handle CID 0 (.notdef) which is directly mapped to GID 0 */
2576 if ( glyph_index != 0 )
2578 glyph_index = cff_charset_cid_to_gindex( &cff->charset,
2579 glyph_index );
2580 if ( glyph_index == 0 )
2581 return CFF_Err_Invalid_Argument;
2584 else if ( glyph_index >= cff->num_glyphs )
2585 return CFF_Err_Invalid_Argument;
2587 if ( load_flags & FT_LOAD_NO_RECURSE )
2588 load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
2590 glyph->x_scale = 0x10000L;
2591 glyph->y_scale = 0x10000L;
2592 if ( size )
2594 glyph->x_scale = size->root.metrics.x_scale;
2595 glyph->y_scale = size->root.metrics.y_scale;
2598 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
2600 /* try to load embedded bitmap if any */
2601 /* */
2602 /* XXX: The convention should be emphasized in */
2603 /* the documents because it can be confusing. */
2604 if ( size )
2606 CFF_Face cff_face = (CFF_Face)size->root.face;
2607 SFNT_Service sfnt = (SFNT_Service)cff_face->sfnt;
2608 FT_Stream stream = cff_face->root.stream;
2611 if ( size->strike_index != 0xFFFFFFFFUL &&
2612 sfnt->load_eblc &&
2613 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
2615 TT_SBit_MetricsRec metrics;
2618 error = sfnt->load_sbit_image( face,
2619 size->strike_index,
2620 glyph_index,
2621 (FT_Int)load_flags,
2622 stream,
2623 &glyph->root.bitmap,
2624 &metrics );
2626 if ( !error )
2628 glyph->root.outline.n_points = 0;
2629 glyph->root.outline.n_contours = 0;
2631 glyph->root.metrics.width = (FT_Pos)metrics.width << 6;
2632 glyph->root.metrics.height = (FT_Pos)metrics.height << 6;
2634 glyph->root.metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6;
2635 glyph->root.metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6;
2636 glyph->root.metrics.horiAdvance = (FT_Pos)metrics.horiAdvance << 6;
2638 glyph->root.metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6;
2639 glyph->root.metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6;
2640 glyph->root.metrics.vertAdvance = (FT_Pos)metrics.vertAdvance << 6;
2642 glyph->root.format = FT_GLYPH_FORMAT_BITMAP;
2644 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
2646 glyph->root.bitmap_left = metrics.vertBearingX;
2647 glyph->root.bitmap_top = metrics.vertBearingY;
2649 else
2651 glyph->root.bitmap_left = metrics.horiBearingX;
2652 glyph->root.bitmap_top = metrics.horiBearingY;
2654 return error;
2659 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
2661 /* return immediately if we only want the embedded bitmaps */
2662 if ( load_flags & FT_LOAD_SBITS_ONLY )
2663 return CFF_Err_Invalid_Argument;
2665 /* if we have a CID subfont, use its matrix (which has already */
2666 /* been multiplied with the root matrix) */
2668 /* this scaling is only relevant if the PS hinter isn't active */
2669 if ( cff->num_subfonts )
2671 FT_Byte fd_index = cff_fd_select_get( &cff->fd_select,
2672 glyph_index );
2674 FT_ULong top_upm = cff->top_font.font_dict.units_per_em;
2675 FT_ULong sub_upm = cff->subfonts[fd_index]->font_dict.units_per_em;
2678 font_matrix = cff->subfonts[fd_index]->font_dict.font_matrix;
2679 font_offset = cff->subfonts[fd_index]->font_dict.font_offset;
2681 if ( top_upm != sub_upm )
2683 glyph->x_scale = FT_MulDiv( glyph->x_scale, top_upm, sub_upm );
2684 glyph->y_scale = FT_MulDiv( glyph->y_scale, top_upm, sub_upm );
2686 force_scaling = TRUE;
2689 else
2691 font_matrix = cff->top_font.font_dict.font_matrix;
2692 font_offset = cff->top_font.font_dict.font_offset;
2695 glyph->root.outline.n_points = 0;
2696 glyph->root.outline.n_contours = 0;
2698 hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 &&
2699 ( load_flags & FT_LOAD_NO_HINTING ) == 0 );
2701 glyph->root.format = FT_GLYPH_FORMAT_OUTLINE; /* by default */
2704 FT_Byte* charstring;
2705 FT_ULong charstring_len;
2708 cff_decoder_init( &decoder, face, size, glyph, hinting,
2709 FT_LOAD_TARGET_MODE( load_flags ) );
2711 if ( load_flags & FT_LOAD_ADVANCE_ONLY )
2712 decoder.width_only = TRUE;
2714 decoder.builder.no_recurse =
2715 (FT_Bool)( load_flags & FT_LOAD_NO_RECURSE );
2717 /* now load the unscaled outline */
2718 error = cff_get_glyph_data( face, glyph_index,
2719 &charstring, &charstring_len );
2720 if ( !error )
2722 error = cff_decoder_prepare( &decoder, size, glyph_index );
2723 if ( !error )
2725 error = cff_decoder_parse_charstrings( &decoder,
2726 charstring,
2727 charstring_len );
2729 cff_free_glyph_data( face, &charstring, charstring_len );
2732 #ifdef FT_CONFIG_OPTION_INCREMENTAL
2733 /* Control data and length may not be available for incremental */
2734 /* fonts. */
2735 if ( face->root.internal->incremental_interface )
2737 glyph->root.control_data = 0;
2738 glyph->root.control_len = 0;
2740 else
2741 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
2743 /* We set control_data and control_len if charstrings is loaded. */
2744 /* See how charstring loads at cff_index_access_element() in */
2745 /* cffload.c. */
2747 CFF_Index csindex = &cff->charstrings_index;
2750 if ( csindex->offsets )
2752 glyph->root.control_data = csindex->bytes +
2753 csindex->offsets[glyph_index] - 1;
2754 glyph->root.control_len = charstring_len;
2760 /* save new glyph tables */
2761 cff_builder_done( &decoder.builder );
2764 #ifdef FT_CONFIG_OPTION_INCREMENTAL
2766 /* Incremental fonts can optionally override the metrics. */
2767 if ( !error &&
2768 face->root.internal->incremental_interface &&
2769 face->root.internal->incremental_interface->funcs->get_glyph_metrics )
2771 FT_Incremental_MetricsRec metrics;
2774 metrics.bearing_x = decoder.builder.left_bearing.x;
2775 metrics.bearing_y = 0;
2776 metrics.advance = decoder.builder.advance.x;
2777 metrics.advance_v = decoder.builder.advance.y;
2779 error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
2780 face->root.internal->incremental_interface->object,
2781 glyph_index, FALSE, &metrics );
2783 decoder.builder.left_bearing.x = metrics.bearing_x;
2784 decoder.builder.advance.x = metrics.advance;
2785 decoder.builder.advance.y = metrics.advance_v;
2788 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
2790 if ( !error )
2792 /* Now, set the metrics -- this is rather simple, as */
2793 /* the left side bearing is the xMin, and the top side */
2794 /* bearing the yMax. */
2796 /* For composite glyphs, return only left side bearing and */
2797 /* advance width. */
2798 if ( load_flags & FT_LOAD_NO_RECURSE )
2800 FT_Slot_Internal internal = glyph->root.internal;
2803 glyph->root.metrics.horiBearingX = decoder.builder.left_bearing.x;
2804 glyph->root.metrics.horiAdvance = decoder.glyph_width;
2805 internal->glyph_matrix = font_matrix;
2806 internal->glyph_delta = font_offset;
2807 internal->glyph_transformed = 1;
2809 else
2811 FT_BBox cbox;
2812 FT_Glyph_Metrics* metrics = &glyph->root.metrics;
2813 FT_Vector advance;
2814 FT_Bool has_vertical_info;
2817 /* copy the _unscaled_ advance width */
2818 metrics->horiAdvance = decoder.glyph_width;
2819 glyph->root.linearHoriAdvance = decoder.glyph_width;
2820 glyph->root.internal->glyph_transformed = 0;
2822 #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
2823 has_vertical_info = FT_BOOL( face->vertical_info &&
2824 face->vertical.number_Of_VMetrics > 0 &&
2825 face->vertical.long_metrics );
2826 #else
2827 has_vertical_info = FT_BOOL( face->vertical_info &&
2828 face->vertical.number_Of_VMetrics > 0 );
2829 #endif
2831 /* get the vertical metrics from the vtmx table if we have one */
2832 if ( has_vertical_info )
2834 FT_Short vertBearingY = 0;
2835 FT_UShort vertAdvance = 0;
2838 ( (SFNT_Service)face->sfnt )->get_metrics( face, 1,
2839 glyph_index,
2840 &vertBearingY,
2841 &vertAdvance );
2842 metrics->vertBearingY = vertBearingY;
2843 metrics->vertAdvance = vertAdvance;
2845 else
2847 /* make up vertical ones */
2848 if ( face->os2.version != 0xFFFFU )
2849 metrics->vertAdvance = (FT_Pos)( face->os2.sTypoAscender -
2850 face->os2.sTypoDescender );
2851 else
2852 metrics->vertAdvance = (FT_Pos)( face->horizontal.Ascender -
2853 face->horizontal.Descender );
2856 glyph->root.linearVertAdvance = metrics->vertAdvance;
2858 glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;
2860 glyph->root.outline.flags = 0;
2861 if ( size && size->root.metrics.y_ppem < 24 )
2862 glyph->root.outline.flags |= FT_OUTLINE_HIGH_PRECISION;
2864 glyph->root.outline.flags |= FT_OUTLINE_REVERSE_FILL;
2866 if ( !( font_matrix.xx == 0x10000L &&
2867 font_matrix.yy == 0x10000L &&
2868 font_matrix.xy == 0 &&
2869 font_matrix.yx == 0 ) )
2870 FT_Outline_Transform( &glyph->root.outline, &font_matrix );
2872 if ( !( font_offset.x == 0 &&
2873 font_offset.y == 0 ) )
2874 FT_Outline_Translate( &glyph->root.outline,
2875 font_offset.x, font_offset.y );
2877 advance.x = metrics->horiAdvance;
2878 advance.y = 0;
2879 FT_Vector_Transform( &advance, &font_matrix );
2880 metrics->horiAdvance = advance.x + font_offset.x;
2882 advance.x = 0;
2883 advance.y = metrics->vertAdvance;
2884 FT_Vector_Transform( &advance, &font_matrix );
2885 metrics->vertAdvance = advance.y + font_offset.y;
2887 if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 || force_scaling )
2889 /* scale the outline and the metrics */
2890 FT_Int n;
2891 FT_Outline* cur = &glyph->root.outline;
2892 FT_Vector* vec = cur->points;
2893 FT_Fixed x_scale = glyph->x_scale;
2894 FT_Fixed y_scale = glyph->y_scale;
2897 /* First of all, scale the points */
2898 if ( !hinting || !decoder.builder.hints_funcs )
2899 for ( n = cur->n_points; n > 0; n--, vec++ )
2901 vec->x = FT_MulFix( vec->x, x_scale );
2902 vec->y = FT_MulFix( vec->y, y_scale );
2905 /* Then scale the metrics */
2906 metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale );
2907 metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale );
2910 /* compute the other metrics */
2911 FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
2913 metrics->width = cbox.xMax - cbox.xMin;
2914 metrics->height = cbox.yMax - cbox.yMin;
2916 metrics->horiBearingX = cbox.xMin;
2917 metrics->horiBearingY = cbox.yMax;
2919 if ( has_vertical_info )
2920 metrics->vertBearingX = metrics->horiBearingX -
2921 metrics->horiAdvance / 2;
2922 else
2924 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
2925 ft_synthesize_vertical_metrics( metrics,
2926 metrics->vertAdvance );
2931 return error;
2935 /* END */