1 /***************************************************************************/
5 /* OpenType Glyph Loader (body). */
7 /* Copyright 1996-2013 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
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. */
16 /***************************************************************************/
19 #include "../../include/ft2build.h"
20 #include "../../include/freetype/internal/ftdebug.h"
21 #include "../../include/freetype/internal/ftstream.h"
22 #include "../../include/freetype/internal/sfnt.h"
23 #include "../../include/freetype/ftoutln.h"
24 #include "../../include/freetype/ftcffdrv.h"
29 #include "cf2ft.h" /* for cf2_decoder_parse_charstrings */
34 /*************************************************************************/
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. */
41 #define FT_COMPONENT trace_cffgload
44 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
46 typedef enum CFF_Operator_
80 cff_op_dotsection, /* deprecated, acts as no-op */
114 /* Type 1 opcodes: invalid but seen in real life */
117 cff_op_callothersubr,
121 cff_op_setcurrentpoint,
129 #define CFF_COUNT_CHECK_WIDTH 0x80
130 #define CFF_COUNT_EXACT 0x40
131 #define CFF_COUNT_CLEAR_STACK 0x20
133 /* count values which have the `CFF_COUNT_CHECK_WIDTH' flag set are */
134 /* used for checking the width and requested numbers of arguments */
135 /* only; they are set to zero afterwards */
137 /* the other two flags are informative only and unused currently */
139 static const FT_Byte cff_argument_counts[] =
143 2 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT, /* rmoveto */
144 1 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT,
145 1 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT,
147 0 | CFF_COUNT_CLEAR_STACK, /* rlineto */
148 0 | CFF_COUNT_CLEAR_STACK,
149 0 | CFF_COUNT_CLEAR_STACK,
151 0 | CFF_COUNT_CLEAR_STACK, /* rrcurveto */
152 0 | CFF_COUNT_CLEAR_STACK,
153 0 | CFF_COUNT_CLEAR_STACK,
154 0 | CFF_COUNT_CLEAR_STACK,
155 0 | CFF_COUNT_CLEAR_STACK,
156 0 | CFF_COUNT_CLEAR_STACK,
157 0 | CFF_COUNT_CLEAR_STACK,
164 0 | CFF_COUNT_CHECK_WIDTH, /* endchar */
166 2 | CFF_COUNT_CHECK_WIDTH, /* hstem */
167 2 | CFF_COUNT_CHECK_WIDTH,
168 2 | CFF_COUNT_CHECK_WIDTH,
169 2 | CFF_COUNT_CHECK_WIDTH,
171 0 | CFF_COUNT_CHECK_WIDTH, /* hintmask */
172 0 | CFF_COUNT_CHECK_WIDTH, /* cntrmask */
213 2 /* setcurrentpoint */
216 #endif /* CFF_CONFIG_OPTION_OLD_ENGINE */
219 /*************************************************************************/
220 /*************************************************************************/
221 /*************************************************************************/
222 /********** *********/
223 /********** *********/
224 /********** GENERIC CHARSTRING PARSING *********/
225 /********** *********/
226 /********** *********/
227 /*************************************************************************/
228 /*************************************************************************/
229 /*************************************************************************/
232 /*************************************************************************/
235 /* cff_builder_init */
238 /* Initializes a given glyph builder. */
241 /* builder :: A pointer to the glyph builder to initialize. */
244 /* face :: The current face object. */
246 /* size :: The current size object. */
248 /* glyph :: The current glyph object. */
250 /* hinting :: Whether hinting is active. */
253 cff_builder_init( CFF_Builder* builder,
259 builder->path_begun = 0;
260 builder->load_points = 1;
262 builder->face = face;
263 builder->glyph = glyph;
264 builder->memory = face->root.memory;
268 FT_GlyphLoader loader = glyph->root.internal->loader;
271 builder->loader = loader;
272 builder->base = &loader->base.outline;
273 builder->current = &loader->current.outline;
274 FT_GlyphLoader_Rewind( loader );
276 builder->hints_globals = 0;
277 builder->hints_funcs = 0;
279 if ( hinting && size )
281 CFF_Internal internal = (CFF_Internal)size->root.internal;
284 builder->hints_globals = (void *)internal->topfont;
285 builder->hints_funcs = glyph->root.internal->glyph_hints;
292 builder->left_bearing.x = 0;
293 builder->left_bearing.y = 0;
294 builder->advance.x = 0;
295 builder->advance.y = 0;
299 /*************************************************************************/
302 /* cff_builder_done */
305 /* Finalizes a given glyph builder. Its contents can still be used */
306 /* after the call, but the function saves important information */
307 /* within the corresponding glyph slot. */
310 /* builder :: A pointer to the glyph builder to finalize. */
313 cff_builder_done( CFF_Builder* builder )
315 CFF_GlyphSlot glyph = builder->glyph;
319 glyph->root.outline = *builder->base;
323 /*************************************************************************/
326 /* cff_compute_bias */
329 /* Computes the bias value in dependence of the number of glyph */
333 /* in_charstring_type :: The `CharstringType' value of the top DICT */
336 /* num_subrs :: The number of glyph subroutines. */
339 /* The bias value. */
341 cff_compute_bias( FT_Int in_charstring_type,
347 if ( in_charstring_type == 1 )
349 else if ( num_subrs < 1240 )
351 else if ( num_subrs < 33900U )
360 /*************************************************************************/
363 /* cff_decoder_init */
366 /* Initializes a given glyph decoder. */
369 /* decoder :: A pointer to the glyph builder to initialize. */
372 /* face :: The current face object. */
374 /* size :: The current size object. */
376 /* slot :: The current glyph object. */
378 /* hinting :: Whether hinting is active. */
380 /* hint_mode :: The hinting mode. */
383 cff_decoder_init( CFF_Decoder* decoder,
388 FT_Render_Mode hint_mode )
390 CFF_Font cff = (CFF_Font)face->extra.data;
393 /* clear everything */
394 FT_MEM_ZERO( decoder, sizeof ( *decoder ) );
396 /* initialize builder */
397 cff_builder_init( &decoder->builder, face, size, slot, hinting );
399 /* initialize Type2 decoder */
401 decoder->num_globals = cff->global_subrs_index.count;
402 decoder->globals = cff->global_subrs;
403 decoder->globals_bias = cff_compute_bias(
404 cff->top_font.font_dict.charstring_type,
405 decoder->num_globals );
407 decoder->hint_mode = hint_mode;
411 /* this function is used to select the subfont */
412 /* and the locals subrs array */
413 FT_LOCAL_DEF( FT_Error )
414 cff_decoder_prepare( CFF_Decoder* decoder,
416 FT_UInt glyph_index )
418 CFF_Builder *builder = &decoder->builder;
419 CFF_Font cff = (CFF_Font)builder->face->extra.data;
420 CFF_SubFont sub = &cff->top_font;
421 FT_Error error = FT_Err_Ok;
424 /* manage CID fonts */
425 if ( cff->num_subfonts )
427 FT_Byte fd_index = cff_fd_select_get( &cff->fd_select, glyph_index );
430 if ( fd_index >= cff->num_subfonts )
432 FT_TRACE4(( "cff_decoder_prepare: invalid CID subfont index\n" ));
433 error = FT_THROW( Invalid_File_Format );
437 FT_TRACE3(( "glyph index %d (subfont %d):\n", glyph_index, fd_index ));
439 sub = cff->subfonts[fd_index];
441 if ( builder->hints_funcs && size )
443 CFF_Internal internal = (CFF_Internal)size->root.internal;
446 /* for CFFs without subfonts, this value has already been set */
447 builder->hints_globals = (void *)internal->subfonts[fd_index];
450 #ifdef FT_DEBUG_LEVEL_TRACE
452 FT_TRACE3(( "glyph index %d:\n", glyph_index ));
455 decoder->num_locals = sub->local_subrs_index.count;
456 decoder->locals = sub->local_subrs;
457 decoder->locals_bias = cff_compute_bias(
458 decoder->cff->top_font.font_dict.charstring_type,
459 decoder->num_locals );
461 decoder->glyph_width = sub->private_dict.default_width;
462 decoder->nominal_width = sub->private_dict.nominal_width;
464 decoder->current_subfont = sub; /* for Adobe's CFF handler */
471 /* check that there is enough space for `count' more points */
472 FT_LOCAL_DEF( FT_Error )
473 cff_check_points( CFF_Builder* builder,
476 return FT_GLYPHLOADER_CHECK_POINTS( builder->loader, count, 0 );
480 /* add a new point, do not check space */
482 cff_builder_add_point( CFF_Builder* builder,
487 FT_Outline* outline = builder->current;
490 if ( builder->load_points )
492 FT_Vector* point = outline->points + outline->n_points;
493 FT_Byte* control = (FT_Byte*)outline->tags + outline->n_points;
495 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
496 CFF_Driver driver = (CFF_Driver)FT_FACE_DRIVER( builder->face );
499 if ( driver->hinting_engine == FT_CFF_HINTING_FREETYPE )
507 /* cf2_decoder_parse_charstrings uses 16.16 coordinates */
511 *control = (FT_Byte)( flag ? FT_CURVE_TAG_ON : FT_CURVE_TAG_CUBIC );
518 /* check space for a new on-curve point, then add it */
519 FT_LOCAL_DEF( FT_Error )
520 cff_builder_add_point1( CFF_Builder* builder,
527 error = cff_check_points( builder, 1 );
529 cff_builder_add_point( builder, x, y, 1 );
535 /* check space for a new contour, then add it */
537 cff_builder_add_contour( CFF_Builder* builder )
539 FT_Outline* outline = builder->current;
543 if ( !builder->load_points )
545 outline->n_contours++;
549 error = FT_GLYPHLOADER_CHECK_POINTS( builder->loader, 0, 1 );
552 if ( outline->n_contours > 0 )
553 outline->contours[outline->n_contours - 1] =
554 (short)( outline->n_points - 1 );
556 outline->n_contours++;
563 /* if a path was begun, add its first on-curve point */
564 FT_LOCAL_DEF( FT_Error )
565 cff_builder_start_point( CFF_Builder* builder,
569 FT_Error error = FT_Err_Ok;
572 /* test whether we are building a new contour */
573 if ( !builder->path_begun )
575 builder->path_begun = 1;
576 error = cff_builder_add_contour( builder );
578 error = cff_builder_add_point1( builder, x, y );
585 /* close the current contour */
587 cff_builder_close_contour( CFF_Builder* builder )
589 FT_Outline* outline = builder->current;
596 first = outline->n_contours <= 1
597 ? 0 : outline->contours[outline->n_contours - 2] + 1;
599 /* We must not include the last point in the path if it */
600 /* is located on the first point. */
601 if ( outline->n_points > 1 )
603 FT_Vector* p1 = outline->points + first;
604 FT_Vector* p2 = outline->points + outline->n_points - 1;
605 FT_Byte* control = (FT_Byte*)outline->tags + outline->n_points - 1;
608 /* `delete' last point only if it coincides with the first */
609 /* point and if it is not a control point (which can happen). */
610 if ( p1->x == p2->x && p1->y == p2->y )
611 if ( *control == FT_CURVE_TAG_ON )
615 if ( outline->n_contours > 0 )
617 /* Don't add contours only consisting of one point, i.e., */
618 /* check whether begin point and last point are the same. */
619 if ( first == outline->n_points - 1 )
621 outline->n_contours--;
625 outline->contours[outline->n_contours - 1] =
626 (short)( outline->n_points - 1 );
631 FT_LOCAL_DEF( FT_Int )
632 cff_lookup_glyph_by_stdcharcode( CFF_Font cff,
639 /* CID-keyed fonts don't have glyph names */
640 if ( !cff->charset.sids )
643 /* check range of standard char code */
644 if ( charcode < 0 || charcode > 255 )
647 /* Get code to SID mapping from `cff_standard_encoding'. */
648 glyph_sid = cff_get_standard_encoding( (FT_UInt)charcode );
650 for ( n = 0; n < cff->num_glyphs; n++ )
652 if ( cff->charset.sids[n] == glyph_sid )
660 FT_LOCAL_DEF( FT_Error )
661 cff_get_glyph_data( TT_Face face,
666 #ifdef FT_CONFIG_OPTION_INCREMENTAL
667 /* For incremental fonts get the character data using the */
668 /* callback function. */
669 if ( face->root.internal->incremental_interface )
673 face->root.internal->incremental_interface->funcs->get_glyph_data(
674 face->root.internal->incremental_interface->object,
675 glyph_index, &data );
678 *pointer = (FT_Byte*)data.pointer;
679 *length = data.length;
684 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
687 CFF_Font cff = (CFF_Font)(face->extra.data);
690 return cff_index_access_element( &cff->charstrings_index, glyph_index,
697 cff_free_glyph_data( TT_Face face,
701 #ifndef FT_CONFIG_OPTION_INCREMENTAL
705 #ifdef FT_CONFIG_OPTION_INCREMENTAL
706 /* For incremental fonts get the character data using the */
707 /* callback function. */
708 if ( face->root.internal->incremental_interface )
713 data.pointer = *pointer;
714 data.length = length;
716 face->root.internal->incremental_interface->funcs->free_glyph_data(
717 face->root.internal->incremental_interface->object, &data );
720 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
723 CFF_Font cff = (CFF_Font)(face->extra.data);
726 cff_index_forget_element( &cff->charstrings_index, pointer );
731 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
734 cff_operator_seac( CFF_Decoder* decoder,
742 CFF_Builder* builder = &decoder->builder;
743 FT_Int bchar_index, achar_index;
744 TT_Face face = decoder->builder.face;
745 FT_Vector left_bearing, advance;
747 FT_ULong charstring_len;
753 FT_ERROR(( "cff_operator_seac: invalid nested seac\n" ));
754 return FT_THROW( Syntax_Error );
757 adx += decoder->builder.left_bearing.x;
758 ady += decoder->builder.left_bearing.y;
760 #ifdef FT_CONFIG_OPTION_INCREMENTAL
761 /* Incremental fonts don't necessarily have valid charsets. */
762 /* They use the character code, not the glyph index, in this case. */
763 if ( face->root.internal->incremental_interface )
769 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
771 CFF_Font cff = (CFF_Font)(face->extra.data);
774 bchar_index = cff_lookup_glyph_by_stdcharcode( cff, bchar );
775 achar_index = cff_lookup_glyph_by_stdcharcode( cff, achar );
778 if ( bchar_index < 0 || achar_index < 0 )
780 FT_ERROR(( "cff_operator_seac:"
781 " invalid seac character code arguments\n" ));
782 return FT_THROW( Syntax_Error );
785 /* If we are trying to load a composite glyph, do not load the */
786 /* accent character and return the array of subglyphs. */
787 if ( builder->no_recurse )
789 FT_GlyphSlot glyph = (FT_GlyphSlot)builder->glyph;
790 FT_GlyphLoader loader = glyph->internal->loader;
794 /* reallocate subglyph array if necessary */
795 error = FT_GlyphLoader_CheckSubGlyphs( loader, 2 );
799 subg = loader->current.subglyphs;
801 /* subglyph 0 = base character */
802 subg->index = bchar_index;
803 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES |
804 FT_SUBGLYPH_FLAG_USE_MY_METRICS;
809 /* subglyph 1 = accent character */
810 subg->index = achar_index;
811 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES;
812 subg->arg1 = (FT_Int)( adx >> 16 );
813 subg->arg2 = (FT_Int)( ady >> 16 );
815 /* set up remaining glyph fields */
816 glyph->num_subglyphs = 2;
817 glyph->subglyphs = loader->base.subglyphs;
818 glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
820 loader->current.num_subglyphs = 2;
823 FT_GlyphLoader_Prepare( builder->loader );
825 /* First load `bchar' in builder */
826 error = cff_get_glyph_data( face, bchar_index,
827 &charstring, &charstring_len );
830 /* the seac operator must not be nested */
831 decoder->seac = TRUE;
832 error = cff_decoder_parse_charstrings( decoder, charstring,
834 decoder->seac = FALSE;
836 cff_free_glyph_data( face, &charstring, charstring_len );
842 /* Save the left bearing, advance and glyph width of the base */
843 /* character as they will be erased by the next load. */
845 left_bearing = builder->left_bearing;
846 advance = builder->advance;
847 glyph_width = decoder->glyph_width;
849 builder->left_bearing.x = 0;
850 builder->left_bearing.y = 0;
852 builder->pos_x = adx - asb;
853 builder->pos_y = ady;
855 /* Now load `achar' on top of the base outline. */
856 error = cff_get_glyph_data( face, achar_index,
857 &charstring, &charstring_len );
860 /* the seac operator must not be nested */
861 decoder->seac = TRUE;
862 error = cff_decoder_parse_charstrings( decoder, charstring,
864 decoder->seac = FALSE;
866 cff_free_glyph_data( face, &charstring, charstring_len );
872 /* Restore the left side bearing, advance and glyph width */
873 /* of the base character. */
874 builder->left_bearing = left_bearing;
875 builder->advance = advance;
876 decoder->glyph_width = glyph_width;
886 /*************************************************************************/
889 /* cff_decoder_parse_charstrings */
892 /* Parses a given Type 2 charstrings program. */
895 /* decoder :: The current Type 1 decoder. */
898 /* charstring_base :: The base of the charstring stream. */
900 /* charstring_len :: The length in bytes of the charstring stream. */
903 /* FreeType error code. 0 means success. */
905 FT_LOCAL_DEF( FT_Error )
906 cff_decoder_parse_charstrings( CFF_Decoder* decoder,
907 FT_Byte* charstring_base,
908 FT_ULong charstring_len )
911 CFF_Decoder_Zone* zone;
914 CFF_Builder* builder = &decoder->builder;
918 FT_Int charstring_type =
919 decoder->cff->top_font.font_dict.charstring_type;
921 T2_Hints_Funcs hinter;
924 /* set default width */
925 decoder->num_hints = 0;
926 decoder->read_width = 1;
928 /* compute random seed from stack address of parameter */
929 seed = (FT_Fixed)( ( (FT_PtrDist)(char*)&seed ^
930 (FT_PtrDist)(char*)&decoder ^
931 (FT_PtrDist)(char*)&charstring_base ) &
933 seed = ( seed ^ ( seed >> 10 ) ^ ( seed >> 20 ) ) & 0xFFFFL;
937 /* initialize the decoder */
938 decoder->top = decoder->stack;
939 decoder->zone = decoder->zones;
940 zone = decoder->zones;
941 stack = decoder->top;
943 hinter = (T2_Hints_Funcs)builder->hints_funcs;
945 builder->path_begun = 0;
947 zone->base = charstring_base;
948 limit = zone->limit = charstring_base + charstring_len;
949 ip = zone->cursor = zone->base;
956 /* begin hints recording session, if any */
958 hinter->open( hinter->hints );
960 /* now execute loop */
967 /********************************************************************/
969 /* Decode operator or operand */
972 if ( v >= 32 || v == 28 )
978 /* this is an operand, push it on the stack */
980 /* if we use shifts, all computations are done with unsigned */
981 /* values; the conversion to a signed value is the last step */
984 if ( ip + 1 >= limit )
986 val = (FT_Short)( ( (FT_UShort)ip[0] << 8 ) | ip[1] );
990 val = (FT_Int32)v - 139;
995 val = ( (FT_Int32)v - 247 ) * 256 + *ip++ + 108;
1001 val = -( (FT_Int32)v - 251 ) * 256 - *ip++ - 108;
1005 if ( ip + 3 >= limit )
1007 val = (FT_Int32)( ( (FT_UInt32)ip[0] << 24 ) |
1008 ( (FT_UInt32)ip[1] << 16 ) |
1009 ( (FT_UInt32)ip[2] << 8 ) |
1012 if ( charstring_type == 2 )
1015 if ( decoder->top - stack >= CFF_MAX_OPERANDS )
1016 goto Stack_Overflow;
1018 val = (FT_Int32)( (FT_UInt32)val << shift );
1019 *decoder->top++ = val;
1021 #ifdef FT_DEBUG_LEVEL_TRACE
1022 if ( !( val & 0xFFFFL ) )
1023 FT_TRACE4(( " %hd", (FT_Short)( (FT_UInt32)val >> 16 ) ));
1025 FT_TRACE4(( " %.2f", val / 65536.0 ));
1031 /* The specification says that normally arguments are to be taken */
1032 /* from the bottom of the stack. However, this seems not to be */
1033 /* correct, at least for Acroread 7.0.8 on GNU/Linux: It pops the */
1034 /* arguments similar to a PS interpreter. */
1036 FT_Fixed* args = decoder->top;
1037 FT_Int num_args = (FT_Int)( args - decoder->stack );
1042 op = cff_op_unknown;
1053 op = cff_op_vmoveto;
1056 op = cff_op_rlineto;
1059 op = cff_op_hlineto;
1062 op = cff_op_vlineto;
1065 op = cff_op_rrcurveto;
1068 op = cff_op_closepath;
1071 op = cff_op_callsubr;
1085 op = cff_op_dotsection;
1087 case 1: /* this is actually the Type1 vstem3 operator */
1090 case 2: /* this is actually the Type1 hstem3 operator */
1133 op = cff_op_callothersubr;
1172 op = cff_op_setcurrentpoint;
1186 /*default: */ /* XYQ 2007-9-6: we can't just quit if we see some reserved op */
1187 /* decrement ip for syntax error message */
1196 op = cff_op_endchar;
1202 op = cff_op_hstemhm;
1205 op = cff_op_hintmask;
1208 op = cff_op_cntrmask;
1211 op = cff_op_rmoveto;
1214 op = cff_op_hmoveto;
1217 op = cff_op_vstemhm;
1220 op = cff_op_rcurveline;
1223 op = cff_op_rlinecurve;
1226 op = cff_op_vvcurveto;
1229 op = cff_op_hhcurveto;
1232 op = cff_op_callgsubr;
1235 op = cff_op_vhcurveto;
1238 op = cff_op_hvcurveto;
1241 FT_TRACE4(( " unknown op (%d)\n", v ));
1245 if ( op == cff_op_unknown )
1248 /* check arguments */
1249 req_args = cff_argument_counts[op];
1250 if ( req_args & CFF_COUNT_CHECK_WIDTH )
1252 if ( num_args > 0 && decoder->read_width )
1254 /* If `nominal_width' is non-zero, the number is really a */
1255 /* difference against `nominal_width'. Else, the number here */
1256 /* is truly a width, not a difference against `nominal_width'. */
1257 /* If the font does not set `nominal_width', then */
1258 /* `nominal_width' defaults to zero, and so we can set */
1259 /* `glyph_width' to `nominal_width' plus number on the stack */
1260 /* -- for either case. */
1262 FT_Int set_width_ok;
1267 case cff_op_hmoveto:
1268 case cff_op_vmoveto:
1269 set_width_ok = num_args & 2;
1274 case cff_op_hstemhm:
1275 case cff_op_vstemhm:
1276 case cff_op_rmoveto:
1277 case cff_op_hintmask:
1278 case cff_op_cntrmask:
1279 set_width_ok = num_args & 1;
1282 case cff_op_endchar:
1283 /* If there is a width specified for endchar, we either have */
1284 /* 1 argument or 5 arguments. We like to argue. */
1285 set_width_ok = ( num_args == 5 ) || ( num_args == 1 );
1295 decoder->glyph_width = decoder->nominal_width +
1298 if ( decoder->width_only )
1300 /* we only want the advance width; stop here */
1304 /* Consumed an argument. */
1309 decoder->read_width = 0;
1314 if ( num_args < req_args )
1315 goto Stack_Underflow;
1317 num_args -= req_args;
1319 /* Sunliang.Liu sync 221's revison. */
1320 if (args > decoder->stack + CFF_MAX_OPERANDS)
1321 goto Stack_Overflow;
1323 /* At this point, `args' points to the first argument of the */
1324 /* operand in case `req_args' isn't zero. Otherwise, we have */
1325 /* to adjust `args' manually. */
1327 /* Note that we only pop arguments from the stack which we */
1328 /* really need and can digest so that we can continue in case */
1329 /* of superfluous stack elements. */
1335 case cff_op_hstemhm:
1336 case cff_op_vstemhm:
1337 /* the number of arguments is always even here */
1339 op == cff_op_hstem ? " hstem\n" :
1340 ( op == cff_op_vstem ? " vstem\n" :
1341 ( op == cff_op_hstemhm ? " hstemhm\n" : " vstemhm\n" ) ) ));
1344 hinter->stems( hinter->hints,
1345 ( op == cff_op_hstem || op == cff_op_hstemhm ),
1347 args - ( num_args & ~1 ) );
1349 decoder->num_hints += num_args / 2;
1353 case cff_op_hintmask:
1354 case cff_op_cntrmask:
1355 FT_TRACE4(( op == cff_op_hintmask ? " hintmask" : " cntrmask" ));
1357 /* implement vstem when needed -- */
1358 /* the specification doesn't say it, but this also works */
1359 /* with the 'cntrmask' operator */
1364 hinter->stems( hinter->hints,
1367 args - ( num_args & ~1 ) );
1369 decoder->num_hints += num_args / 2;
1372 /* In a valid charstring there must be at least one byte */
1373 /* after `hintmask' or `cntrmask' (e.g., for a `return' */
1374 /* instruction). Additionally, there must be space for */
1375 /* `num_hints' bits. */
1377 if ( ( ip + ( ( decoder->num_hints + 7 ) >> 3 ) ) >= limit )
1382 if ( op == cff_op_hintmask )
1383 hinter->hintmask( hinter->hints,
1384 builder->current->n_points,
1388 hinter->counter( hinter->hints,
1393 #ifdef FT_DEBUG_LEVEL_TRACE
1398 FT_TRACE4(( " (maskbytes:" ));
1401 maskbyte < (FT_UInt)( ( decoder->num_hints + 7 ) >> 3 );
1403 FT_TRACE4(( " 0x%02X", *ip ));
1405 FT_TRACE4(( ")\n" ));
1408 ip += ( decoder->num_hints + 7 ) >> 3;
1413 case cff_op_rmoveto:
1414 FT_TRACE4(( " rmoveto\n" ));
1416 cff_builder_close_contour( builder );
1417 builder->path_begun = 0;
1423 case cff_op_vmoveto:
1424 FT_TRACE4(( " vmoveto\n" ));
1426 cff_builder_close_contour( builder );
1427 builder->path_begun = 0;
1432 case cff_op_hmoveto:
1433 FT_TRACE4(( " hmoveto\n" ));
1435 cff_builder_close_contour( builder );
1436 builder->path_begun = 0;
1441 case cff_op_rlineto:
1442 FT_TRACE4(( " rlineto\n" ));
1444 if ( cff_builder_start_point( builder, x, y ) ||
1445 cff_check_points( builder, num_args / 2 ) )
1449 goto Stack_Underflow;
1451 args -= num_args & ~1;
1452 while ( args < decoder->top )
1456 cff_builder_add_point( builder, x, y, 1 );
1462 case cff_op_hlineto:
1463 case cff_op_vlineto:
1465 FT_Int phase = ( op == cff_op_hlineto );
1468 FT_TRACE4(( op == cff_op_hlineto ? " hlineto\n"
1472 goto Stack_Underflow;
1474 /* there exist subsetted fonts (found in PDFs) */
1475 /* which call `hlineto' without arguments */
1476 if ( num_args == 0 )
1479 if ( cff_builder_start_point( builder, x, y ) ||
1480 cff_check_points( builder, num_args ) )
1484 while ( args < decoder->top )
1491 if ( cff_builder_add_point1( builder, x, y ) )
1501 case cff_op_rrcurveto:
1506 FT_TRACE4(( " rrcurveto\n" ));
1509 goto Stack_Underflow;
1511 nargs = num_args - num_args % 6;
1513 if ( cff_builder_start_point( builder, x, y ) ||
1514 cff_check_points( builder, nargs / 2 ) )
1518 while ( args < decoder->top )
1522 cff_builder_add_point( builder, x, y, 0 );
1525 cff_builder_add_point( builder, x, y, 0 );
1528 cff_builder_add_point( builder, x, y, 1 );
1535 case cff_op_vvcurveto:
1540 FT_TRACE4(( " vvcurveto\n" ));
1543 goto Stack_Underflow;
1545 /* if num_args isn't of the form 4n or 4n+1, */
1546 /* we enforce it by clearing the second bit */
1548 nargs = num_args & ~2;
1550 if ( cff_builder_start_point( builder, x, y ) )
1562 if ( cff_check_points( builder, 3 * ( nargs / 4 ) ) )
1565 while ( args < decoder->top )
1568 cff_builder_add_point( builder, x, y, 0 );
1571 cff_builder_add_point( builder, x, y, 0 );
1573 cff_builder_add_point( builder, x, y, 1 );
1580 case cff_op_hhcurveto:
1585 FT_TRACE4(( " hhcurveto\n" ));
1588 goto Stack_Underflow;
1590 /* if num_args isn't of the form 4n or 4n+1, */
1591 /* we enforce it by clearing the second bit */
1593 nargs = num_args & ~2;
1595 if ( cff_builder_start_point( builder, x, y ) )
1606 if ( cff_check_points( builder, 3 * ( nargs / 4 ) ) )
1609 while ( args < decoder->top )
1612 cff_builder_add_point( builder, x, y, 0 );
1615 cff_builder_add_point( builder, x, y, 0 );
1617 cff_builder_add_point( builder, x, y, 1 );
1624 case cff_op_vhcurveto:
1625 case cff_op_hvcurveto:
1631 FT_TRACE4(( op == cff_op_vhcurveto ? " vhcurveto\n"
1632 : " hvcurveto\n" ));
1634 if ( cff_builder_start_point( builder, x, y ) )
1638 goto Stack_Underflow;
1640 /* if num_args isn't of the form 8n, 8n+1, 8n+4, or 8n+5, */
1641 /* we enforce it by clearing the second bit */
1643 nargs = num_args & ~2;
1646 if ( cff_check_points( builder, ( nargs / 4 ) * 3 ) )
1647 goto Stack_Underflow;
1649 phase = ( op == cff_op_hvcurveto );
1651 while ( nargs >= 4 )
1657 cff_builder_add_point( builder, x, y, 0 );
1660 cff_builder_add_point( builder, x, y, 0 );
1664 cff_builder_add_point( builder, x, y, 1 );
1669 cff_builder_add_point( builder, x, y, 0 );
1672 cff_builder_add_point( builder, x, y, 0 );
1676 cff_builder_add_point( builder, x, y, 1 );
1685 case cff_op_rlinecurve:
1691 FT_TRACE4(( " rlinecurve\n" ));
1694 goto Stack_Underflow;
1696 nargs = num_args & ~1;
1697 num_lines = ( nargs - 6 ) / 2;
1699 if ( cff_builder_start_point( builder, x, y ) ||
1700 cff_check_points( builder, num_lines + 3 ) )
1705 /* first, add the line segments */
1706 while ( num_lines > 0 )
1710 cff_builder_add_point( builder, x, y, 1 );
1715 /* then the curve */
1718 cff_builder_add_point( builder, x, y, 0 );
1721 cff_builder_add_point( builder, x, y, 0 );
1724 cff_builder_add_point( builder, x, y, 1 );
1729 case cff_op_rcurveline:
1735 FT_TRACE4(( " rcurveline\n" ));
1738 goto Stack_Underflow;
1740 nargs = num_args - 2;
1741 nargs = nargs - nargs % 6 + 2;
1742 num_curves = ( nargs - 2 ) / 6;
1744 if ( cff_builder_start_point( builder, x, y ) ||
1745 cff_check_points( builder, num_curves * 3 + 2 ) )
1750 /* first, add the curves */
1751 while ( num_curves > 0 )
1755 cff_builder_add_point( builder, x, y, 0 );
1758 cff_builder_add_point( builder, x, y, 0 );
1761 cff_builder_add_point( builder, x, y, 1 );
1766 /* then the final line */
1769 cff_builder_add_point( builder, x, y, 1 );
1779 FT_TRACE4(( " hflex1\n" ));
1781 /* adding five more points: 4 control points, 1 on-curve point */
1782 /* -- make sure we have enough space for the start point if it */
1783 /* needs to be added */
1784 if ( cff_builder_start_point( builder, x, y ) ||
1785 cff_check_points( builder, 6 ) )
1788 /* record the starting point's y position for later use */
1791 /* first control point */
1794 cff_builder_add_point( builder, x, y, 0 );
1796 /* second control point */
1799 cff_builder_add_point( builder, x, y, 0 );
1801 /* join point; on curve, with y-value the same as the last */
1802 /* control point's y-value */
1804 cff_builder_add_point( builder, x, y, 1 );
1806 /* third control point, with y-value the same as the join */
1807 /* point's y-value */
1809 cff_builder_add_point( builder, x, y, 0 );
1811 /* fourth control point */
1814 cff_builder_add_point( builder, x, y, 0 );
1816 /* ending point, with y-value the same as the start */
1819 cff_builder_add_point( builder, x, y, 1 );
1830 FT_TRACE4(( " hflex\n" ));
1832 /* adding six more points; 4 control points, 2 on-curve points */
1833 if ( cff_builder_start_point( builder, x, y ) ||
1834 cff_check_points( builder, 6 ) )
1837 /* record the starting point's y-position for later use */
1840 /* first control point */
1842 cff_builder_add_point( builder, x, y, 0 );
1844 /* second control point */
1847 cff_builder_add_point( builder, x, y, 0 );
1849 /* join point; on curve, with y-value the same as the last */
1850 /* control point's y-value */
1852 cff_builder_add_point( builder, x, y, 1 );
1854 /* third control point, with y-value the same as the join */
1855 /* point's y-value */
1857 cff_builder_add_point( builder, x, y, 0 );
1859 /* fourth control point */
1862 cff_builder_add_point( builder, x, y, 0 );
1864 /* ending point, with y-value the same as the start point's */
1865 /* y-value -- we don't add this point, though */
1867 cff_builder_add_point( builder, x, y, 1 );
1875 FT_Pos start_x, start_y; /* record start x, y values for */
1877 FT_Fixed dx = 0, dy = 0; /* used in horizontal/vertical */
1878 /* algorithm below */
1879 FT_Int horizontal, count;
1883 FT_TRACE4(( " flex1\n" ));
1885 /* adding six more points; 4 control points, 2 on-curve points */
1886 if ( cff_builder_start_point( builder, x, y ) ||
1887 cff_check_points( builder, 6 ) )
1890 /* record the starting point's x, y position for later use */
1894 /* XXX: figure out whether this is supposed to be a horizontal */
1895 /* or vertical flex; the Type 2 specification is vague... */
1899 /* grab up to the last argument */
1900 for ( count = 5; count > 0; count-- )
1912 /* strange test, but here it is... */
1913 horizontal = ( dx > dy );
1915 for ( count = 5; count > 0; count-- )
1919 cff_builder_add_point( builder, x, y,
1920 (FT_Bool)( count == 3 ) );
1924 /* is last operand an x- or y-delta? */
1936 cff_builder_add_point( builder, x, y, 1 );
1947 FT_TRACE4(( " flex\n" ));
1949 if ( cff_builder_start_point( builder, x, y ) ||
1950 cff_check_points( builder, 6 ) )
1953 for ( count = 6; count > 0; count-- )
1957 cff_builder_add_point( builder, x, y,
1958 (FT_Bool)( count == 4 || count == 1 ) );
1967 FT_TRACE4(( " seac\n" ));
1969 error = cff_operator_seac( decoder,
1970 args[0], args[1], args[2],
1971 (FT_Int)( args[3] >> 16 ),
1972 (FT_Int)( args[4] >> 16 ) );
1974 /* add current outline to the glyph slot */
1975 FT_GlyphLoader_Add( builder->loader );
1978 FT_TRACE4(( "\n" ));
1981 case cff_op_endchar:
1982 FT_TRACE4(( " endchar\n" ));
1984 /* We are going to emulate the seac operator. */
1985 if ( num_args >= 4 )
1987 /* Save glyph width so that the subglyphs don't overwrite it. */
1988 FT_Pos glyph_width = decoder->glyph_width;
1991 error = cff_operator_seac( decoder,
1992 0L, args[-4], args[-3],
1993 (FT_Int)( args[-2] >> 16 ),
1994 (FT_Int)( args[-1] >> 16 ) );
1996 decoder->glyph_width = glyph_width;
2003 cff_builder_close_contour( builder );
2005 /* close hints recording session */
2008 if ( hinter->close( hinter->hints,
2009 builder->current->n_points ) )
2012 /* apply hints to the loaded glyph outline now */
2013 hinter->apply( hinter->hints,
2015 (PSH_Globals)builder->hints_globals,
2016 decoder->hint_mode );
2019 /* add current outline to the glyph slot */
2020 FT_GlyphLoader_Add( builder->loader );
2024 FT_TRACE4(( "\n" ));
2028 FT_TRACE4(( " abs\n" ));
2036 FT_TRACE4(( " add\n" ));
2043 FT_TRACE4(( " sub\n" ));
2050 FT_TRACE4(( " div\n" ));
2052 args[0] = FT_DivFix( args[0], args[1] );
2057 FT_TRACE4(( " neg\n" ));
2068 FT_TRACE4(( " rand\n" ));
2071 if ( Rand >= 0x8000L )
2075 seed = FT_MulFix( seed, 0x10000L - seed );
2083 FT_TRACE4(( " mul\n" ));
2085 args[0] = FT_MulFix( args[0], args[1] );
2090 FT_TRACE4(( " sqrt\n" ));
2095 FT_Fixed root = args[0];
2101 new_root = ( root + FT_DivFix( args[0], root ) + 1 ) >> 1;
2102 if ( new_root == root || count <= 0 )
2115 FT_TRACE4(( " drop\n" ));
2124 FT_TRACE4(( " exch\n" ));
2135 FT_Int idx = (FT_Int)( args[0] >> 16 );
2138 FT_TRACE4(( " index\n" ));
2142 else if ( idx > num_args - 2 )
2144 args[0] = args[-( idx + 1 )];
2151 FT_Int count = (FT_Int)( args[0] >> 16 );
2152 FT_Int idx = (FT_Int)( args[1] >> 16 );
2155 FT_TRACE4(( " roll\n" ));
2162 goto Stack_Underflow;
2168 FT_Fixed tmp = args[count - 1];
2172 for ( i = count - 2; i >= 0; i-- )
2173 args[i + 1] = args[i];
2182 FT_Fixed tmp = args[0];
2186 for ( i = 0; i < count - 1; i++ )
2187 args[i] = args[i + 1];
2188 args[count - 1] = tmp;
2197 FT_TRACE4(( " dup\n" ));
2205 FT_Fixed val = args[0];
2206 FT_Int idx = (FT_Int)( args[1] >> 16 );
2209 FT_TRACE4(( " put\n" ));
2211 if ( idx >= 0 && idx < CFF_MAX_TRANS_ELEMENTS )
2212 decoder->buildchar[idx] = val;
2218 FT_Int idx = (FT_Int)( args[0] >> 16 );
2222 FT_TRACE4(( " get\n" ));
2224 if ( idx >= 0 && idx < CFF_MAX_TRANS_ELEMENTS )
2225 val = decoder->buildchar[idx];
2233 FT_TRACE4(( " store\n"));
2238 FT_TRACE4(( " load\n" ));
2242 case cff_op_dotsection:
2243 /* this operator is deprecated and ignored by the parser */
2244 FT_TRACE4(( " dotsection\n" ));
2247 case cff_op_closepath:
2248 /* this is an invalid Type 2 operator; however, there */
2249 /* exist fonts which are incorrectly converted from probably */
2250 /* Type 1 to CFF, and some parsers seem to accept it */
2252 FT_TRACE4(( " closepath (invalid op)\n" ));
2258 /* this is an invalid Type 2 operator; however, there */
2259 /* exist fonts which are incorrectly converted from probably */
2260 /* Type 1 to CFF, and some parsers seem to accept it */
2262 FT_TRACE4(( " hsbw (invalid op)\n" ));
2264 decoder->glyph_width = decoder->nominal_width + ( args[1] >> 16 );
2266 decoder->builder.left_bearing.x = args[0];
2267 decoder->builder.left_bearing.y = 0;
2269 x = decoder->builder.pos_x + args[0];
2270 y = decoder->builder.pos_y;
2275 /* this is an invalid Type 2 operator; however, there */
2276 /* exist fonts which are incorrectly converted from probably */
2277 /* Type 1 to CFF, and some parsers seem to accept it */
2279 FT_TRACE4(( " sbw (invalid op)\n" ));
2281 decoder->glyph_width = decoder->nominal_width + ( args[2] >> 16 );
2283 decoder->builder.left_bearing.x = args[0];
2284 decoder->builder.left_bearing.y = args[1];
2286 x = decoder->builder.pos_x + args[0];
2287 y = decoder->builder.pos_y + args[1];
2291 case cff_op_setcurrentpoint:
2292 /* this is an invalid Type 2 operator; however, there */
2293 /* exist fonts which are incorrectly converted from probably */
2294 /* Type 1 to CFF, and some parsers seem to accept it */
2296 FT_TRACE4(( " setcurrentpoint (invalid op)\n" ));
2298 x = decoder->builder.pos_x + args[0];
2299 y = decoder->builder.pos_y + args[1];
2303 case cff_op_callothersubr:
2304 /* this is an invalid Type 2 operator; however, there */
2305 /* exist fonts which are incorrectly converted from probably */
2306 /* Type 1 to CFF, and some parsers seem to accept it */
2308 FT_TRACE4(( " callothersubr (invalid op)\n" ));
2310 /* subsequent `pop' operands should add the arguments, */
2311 /* this is the implementation described for `unknown' other */
2312 /* subroutines in the Type1 spec. */
2314 /* XXX Fix return arguments (see discussion below). */
2315 args -= 2 + ( args[-2] >> 16 );
2317 goto Stack_Underflow;
2321 /* this is an invalid Type 2 operator; however, there */
2322 /* exist fonts which are incorrectly converted from probably */
2323 /* Type 1 to CFF, and some parsers seem to accept it */
2325 FT_TRACE4(( " pop (invalid op)\n" ));
2327 /* XXX Increasing `args' is wrong: After a certain number of */
2328 /* `pop's we get a stack overflow. Reason for doing it is */
2329 /* code like this (actually found in a CFF font): */
2331 /* 17 1 3 callothersubr */
2335 /* Since we handle `callothersubr' as a no-op, and */
2336 /* `callsubr' needs at least one argument, `pop' can't be a */
2337 /* no-op too as it basically should be. */
2339 /* The right solution would be to provide real support for */
2340 /* `callothersubr' as done in `t1decode.c', however, given */
2341 /* the fact that CFF fonts with `pop' are invalid, it is */
2342 /* questionable whether it is worth the time. */
2348 FT_Fixed cond = args[0] && args[1];
2351 FT_TRACE4(( " and\n" ));
2353 args[0] = cond ? 0x10000L : 0;
2360 FT_Fixed cond = args[0] || args[1];
2363 FT_TRACE4(( " or\n" ));
2365 args[0] = cond ? 0x10000L : 0;
2372 FT_Fixed cond = !args[0];
2375 FT_TRACE4(( " eq\n" ));
2377 args[0] = cond ? 0x10000L : 0;
2384 FT_Fixed cond = ( args[2] <= args[3] );
2387 FT_TRACE4(( " ifelse\n" ));
2395 case cff_op_callsubr:
2397 FT_UInt idx = (FT_UInt)( ( args[0] >> 16 ) +
2398 decoder->locals_bias );
2401 FT_TRACE4(( " callsubr(%d)\n", idx ));
2403 if ( idx >= decoder->num_locals )
2405 FT_ERROR(( "cff_decoder_parse_charstrings:"
2406 " invalid local subr index\n" ));
2410 if ( zone - decoder->zones >= CFF_MAX_SUBRS_CALLS )
2412 FT_ERROR(( "cff_decoder_parse_charstrings:"
2413 " too many nested subrs\n" ));
2417 zone->cursor = ip; /* save current instruction pointer */
2420 zone->base = decoder->locals[idx];
2421 zone->limit = decoder->locals[idx + 1];
2422 zone->cursor = zone->base;
2424 if ( !zone->base || zone->limit == zone->base )
2426 FT_ERROR(( "cff_decoder_parse_charstrings:"
2427 " invoking empty subrs\n" ));
2431 decoder->zone = zone;
2433 limit = zone->limit;
2437 case cff_op_callgsubr:
2439 FT_UInt idx = (FT_UInt)( ( args[0] >> 16 ) +
2440 decoder->globals_bias );
2443 FT_TRACE4(( " callgsubr(%d)\n", idx ));
2445 if ( idx >= decoder->num_globals )
2447 FT_ERROR(( "cff_decoder_parse_charstrings:"
2448 " invalid global subr index\n" ));
2452 if ( zone - decoder->zones >= CFF_MAX_SUBRS_CALLS )
2454 FT_ERROR(( "cff_decoder_parse_charstrings:"
2455 " too many nested subrs\n" ));
2459 zone->cursor = ip; /* save current instruction pointer */
2462 zone->base = decoder->globals[idx];
2463 zone->limit = decoder->globals[idx + 1];
2464 zone->cursor = zone->base;
2466 if ( !zone->base || zone->limit == zone->base )
2468 FT_ERROR(( "cff_decoder_parse_charstrings:"
2469 " invoking empty subrs\n" ));
2473 decoder->zone = zone;
2475 limit = zone->limit;
2480 FT_TRACE4(( " return\n" ));
2482 if ( decoder->zone <= decoder->zones )
2484 FT_ERROR(( "cff_decoder_parse_charstrings:"
2485 " unexpected return\n" ));
2490 zone = decoder->zone;
2492 limit = zone->limit;
2497 FT_ERROR(( "Unimplemented opcode: %d", ip[-1] ));
2500 FT_ERROR(( " %d", ip[0] ));
2503 return FT_THROW( Unimplemented_Feature );
2506 decoder->top = args;
2508 if ( decoder->top - stack >= CFF_MAX_OPERANDS )
2509 goto Stack_Overflow;
2511 } /* general operator processing */
2513 } /* while ip < limit */
2515 FT_TRACE4(( "..end..\n\n" ));
2521 FT_TRACE4(( "cff_decoder_parse_charstrings: syntax error\n" ));
2522 return FT_THROW( Invalid_File_Format );
2525 FT_TRACE4(( "cff_decoder_parse_charstrings: stack underflow\n" ));
2526 return FT_THROW( Too_Few_Arguments );
2529 FT_TRACE4(( "cff_decoder_parse_charstrings: stack overflow\n" ));
2530 return FT_THROW( Stack_Overflow );
2533 #endif /* CFF_CONFIG_OPTION_OLD_ENGINE */
2536 /*************************************************************************/
2537 /*************************************************************************/
2538 /*************************************************************************/
2539 /********** *********/
2540 /********** *********/
2541 /********** COMPUTE THE MAXIMUM ADVANCE WIDTH *********/
2542 /********** *********/
2543 /********** The following code is in charge of computing *********/
2544 /********** the maximum advance width of the font. It *********/
2545 /********** quickly processes each glyph charstring to *********/
2546 /********** extract the value from either a `sbw' or `seac' *********/
2547 /********** operator. *********/
2548 /********** *********/
2549 /*************************************************************************/
2550 /*************************************************************************/
2551 /*************************************************************************/
2554 #if 0 /* unused until we support pure CFF fonts */
2557 FT_LOCAL_DEF( FT_Error )
2558 cff_compute_max_advance( TT_Face face,
2559 FT_Int* max_advance )
2561 FT_Error error = FT_Err_Ok;
2562 CFF_Decoder decoder;
2564 CFF_Font cff = (CFF_Font)face->other;
2569 /* Initialize load decoder */
2570 cff_decoder_init( &decoder, face, 0, 0, 0, 0 );
2572 decoder.builder.metrics_only = 1;
2573 decoder.builder.load_points = 0;
2575 /* For each glyph, parse the glyph charstring and extract */
2576 /* the advance width. */
2577 for ( glyph_index = 0; glyph_index < face->root.num_glyphs;
2580 FT_Byte* charstring;
2581 FT_ULong charstring_len;
2584 /* now get load the unscaled outline */
2585 error = cff_get_glyph_data( face, glyph_index,
2586 &charstring, &charstring_len );
2589 error = cff_decoder_prepare( &decoder, size, glyph_index );
2591 error = cff_decoder_parse_charstrings( &decoder,
2595 cff_free_glyph_data( face, &charstring, &charstring_len );
2598 /* ignore the error if one has occurred -- skip to next glyph */
2602 *max_advance = decoder.builder.advance.x;
2611 FT_LOCAL_DEF( FT_Error )
2612 cff_slot_load( CFF_GlyphSlot glyph,
2614 FT_UInt glyph_index,
2615 FT_Int32 load_flags )
2618 CFF_Decoder decoder;
2619 TT_Face face = (TT_Face)glyph->root.face;
2620 FT_Bool hinting, scaled, force_scaling;
2621 CFF_Font cff = (CFF_Font)face->extra.data;
2623 FT_Matrix font_matrix;
2624 FT_Vector font_offset;
2627 force_scaling = FALSE;
2629 /* in a CID-keyed font, consider `glyph_index' as a CID and map */
2630 /* it immediately to the real glyph_index -- if it isn't a */
2631 /* subsetted font, glyph_indices and CIDs are identical, though */
2632 if ( cff->top_font.font_dict.cid_registry != 0xFFFFU &&
2635 /* don't handle CID 0 (.notdef) which is directly mapped to GID 0 */
2636 if ( glyph_index != 0 )
2638 glyph_index = cff_charset_cid_to_gindex( &cff->charset,
2640 if ( glyph_index == 0 )
2641 return FT_THROW( Invalid_Argument );
2644 else if ( glyph_index >= cff->num_glyphs )
2645 return FT_THROW( Invalid_Argument );
2647 if ( load_flags & FT_LOAD_NO_RECURSE )
2648 load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
2650 glyph->x_scale = 0x10000L;
2651 glyph->y_scale = 0x10000L;
2654 glyph->x_scale = size->root.metrics.x_scale;
2655 glyph->y_scale = size->root.metrics.y_scale;
2658 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
2660 /* try to load embedded bitmap if any */
2662 /* XXX: The convention should be emphasized in */
2663 /* the documents because it can be confusing. */
2666 CFF_Face cff_face = (CFF_Face)size->root.face;
2667 SFNT_Service sfnt = (SFNT_Service)cff_face->sfnt;
2668 FT_Stream stream = cff_face->root.stream;
2671 if ( size->strike_index != 0xFFFFFFFFUL &&
2673 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
2675 TT_SBit_MetricsRec metrics;
2678 error = sfnt->load_sbit_image( face,
2683 &glyph->root.bitmap,
2688 FT_Bool has_vertical_info;
2693 glyph->root.outline.n_points = 0;
2694 glyph->root.outline.n_contours = 0;
2696 glyph->root.metrics.width = (FT_Pos)metrics.width << 6;
2697 glyph->root.metrics.height = (FT_Pos)metrics.height << 6;
2699 glyph->root.metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6;
2700 glyph->root.metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6;
2701 glyph->root.metrics.horiAdvance = (FT_Pos)metrics.horiAdvance << 6;
2703 glyph->root.metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6;
2704 glyph->root.metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6;
2705 glyph->root.metrics.vertAdvance = (FT_Pos)metrics.vertAdvance << 6;
2707 glyph->root.format = FT_GLYPH_FORMAT_BITMAP;
2709 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
2711 glyph->root.bitmap_left = metrics.vertBearingX;
2712 glyph->root.bitmap_top = metrics.vertBearingY;
2716 glyph->root.bitmap_left = metrics.horiBearingX;
2717 glyph->root.bitmap_top = metrics.horiBearingY;
2720 /* compute linear advance widths */
2722 ( (SFNT_Service)face->sfnt )->get_metrics( face, 0,
2726 glyph->root.linearHoriAdvance = advance;
2728 has_vertical_info = FT_BOOL(
2729 face->vertical_info &&
2730 face->vertical.number_Of_VMetrics > 0 );
2732 /* get the vertical metrics from the vtmx table if we have one */
2733 if ( has_vertical_info )
2735 ( (SFNT_Service)face->sfnt )->get_metrics( face, 1,
2739 glyph->root.linearVertAdvance = advance;
2743 /* make up vertical ones */
2744 if ( face->os2.version != 0xFFFFU )
2745 glyph->root.linearVertAdvance = (FT_Pos)
2746 ( face->os2.sTypoAscender - face->os2.sTypoDescender );
2748 glyph->root.linearVertAdvance = (FT_Pos)
2749 ( face->horizontal.Ascender - face->horizontal.Descender );
2757 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
2759 /* return immediately if we only want the embedded bitmaps */
2760 if ( load_flags & FT_LOAD_SBITS_ONLY )
2761 return FT_THROW( Invalid_Argument );
2763 /* if we have a CID subfont, use its matrix (which has already */
2764 /* been multiplied with the root matrix) */
2766 /* this scaling is only relevant if the PS hinter isn't active */
2767 if ( cff->num_subfonts )
2769 FT_ULong top_upm, sub_upm;
2770 FT_Byte fd_index = cff_fd_select_get( &cff->fd_select,
2774 if ( fd_index >= cff->num_subfonts )
2775 fd_index = (FT_Byte)( cff->num_subfonts - 1 );
2777 top_upm = cff->top_font.font_dict.units_per_em;
2778 sub_upm = cff->subfonts[fd_index]->font_dict.units_per_em;
2781 font_matrix = cff->subfonts[fd_index]->font_dict.font_matrix;
2782 font_offset = cff->subfonts[fd_index]->font_dict.font_offset;
2784 if ( top_upm != sub_upm )
2786 glyph->x_scale = FT_MulDiv( glyph->x_scale, top_upm, sub_upm );
2787 glyph->y_scale = FT_MulDiv( glyph->y_scale, top_upm, sub_upm );
2789 force_scaling = TRUE;
2794 font_matrix = cff->top_font.font_dict.font_matrix;
2795 font_offset = cff->top_font.font_dict.font_offset;
2798 glyph->root.outline.n_points = 0;
2799 glyph->root.outline.n_contours = 0;
2801 /* top-level code ensures that FT_LOAD_NO_HINTING is set */
2802 /* if FT_LOAD_NO_SCALE is active */
2803 hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_HINTING ) == 0 );
2804 scaled = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 );
2806 glyph->hint = hinting;
2807 glyph->scaled = scaled;
2808 glyph->root.format = FT_GLYPH_FORMAT_OUTLINE; /* by default */
2811 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
2812 CFF_Driver driver = (CFF_Driver)FT_FACE_DRIVER( face );
2816 FT_Byte* charstring;
2817 FT_ULong charstring_len;
2820 cff_decoder_init( &decoder, face, size, glyph, hinting,
2821 FT_LOAD_TARGET_MODE( load_flags ) );
2823 if ( load_flags & FT_LOAD_ADVANCE_ONLY )
2824 decoder.width_only = TRUE;
2826 decoder.builder.no_recurse =
2827 (FT_Bool)( load_flags & FT_LOAD_NO_RECURSE );
2829 /* now load the unscaled outline */
2830 error = cff_get_glyph_data( face, glyph_index,
2831 &charstring, &charstring_len );
2833 goto Glyph_Build_Finished;
2835 error = cff_decoder_prepare( &decoder, size, glyph_index );
2837 goto Glyph_Build_Finished;
2839 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
2840 /* choose which CFF renderer to use */
2841 if ( driver->hinting_engine == FT_CFF_HINTING_FREETYPE )
2842 error = cff_decoder_parse_charstrings( &decoder,
2848 error = cf2_decoder_parse_charstrings( &decoder,
2852 /* Adobe's engine uses 16.16 numbers everywhere; */
2853 /* as a consequence, glyphs larger than 2000ppem get rejected */
2854 if ( FT_ERR_EQ( error, Glyph_Too_Big ) )
2856 /* this time, we retry unhinted and scale up the glyph later on */
2857 /* (the engine uses and sets the hardcoded value 0x10000 / 64 = */
2858 /* 0x400 for both `x_scale' and `y_scale' in this case) */
2860 force_scaling = TRUE;
2861 glyph->hint = hinting;
2863 error = cf2_decoder_parse_charstrings( &decoder,
2869 cff_free_glyph_data( face, &charstring, charstring_len );
2872 goto Glyph_Build_Finished;
2874 #ifdef FT_CONFIG_OPTION_INCREMENTAL
2875 /* Control data and length may not be available for incremental */
2877 if ( face->root.internal->incremental_interface )
2879 glyph->root.control_data = 0;
2880 glyph->root.control_len = 0;
2883 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
2885 /* We set control_data and control_len if charstrings is loaded. */
2886 /* See how charstring loads at cff_index_access_element() in */
2889 CFF_Index csindex = &cff->charstrings_index;
2892 if ( csindex->offsets )
2894 glyph->root.control_data = csindex->bytes +
2895 csindex->offsets[glyph_index] - 1;
2896 glyph->root.control_len = charstring_len;
2900 Glyph_Build_Finished:
2901 /* save new glyph tables, if no error */
2903 cff_builder_done( &decoder.builder );
2904 /* XXX: anything to do for broken glyph entry? */
2907 #ifdef FT_CONFIG_OPTION_INCREMENTAL
2909 /* Incremental fonts can optionally override the metrics. */
2911 face->root.internal->incremental_interface &&
2912 face->root.internal->incremental_interface->funcs->get_glyph_metrics )
2914 FT_Incremental_MetricsRec metrics;
2917 metrics.bearing_x = decoder.builder.left_bearing.x;
2918 metrics.bearing_y = 0;
2919 metrics.advance = decoder.builder.advance.x;
2920 metrics.advance_v = decoder.builder.advance.y;
2922 error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
2923 face->root.internal->incremental_interface->object,
2924 glyph_index, FALSE, &metrics );
2926 decoder.builder.left_bearing.x = metrics.bearing_x;
2927 decoder.builder.advance.x = metrics.advance;
2928 decoder.builder.advance.y = metrics.advance_v;
2931 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
2935 /* Now, set the metrics -- this is rather simple, as */
2936 /* the left side bearing is the xMin, and the top side */
2937 /* bearing the yMax. */
2939 /* For composite glyphs, return only left side bearing and */
2940 /* advance width. */
2941 if ( load_flags & FT_LOAD_NO_RECURSE )
2943 FT_Slot_Internal internal = glyph->root.internal;
2946 glyph->root.metrics.horiBearingX = decoder.builder.left_bearing.x;
2947 glyph->root.metrics.horiAdvance = decoder.glyph_width;
2948 internal->glyph_matrix = font_matrix;
2949 internal->glyph_delta = font_offset;
2950 internal->glyph_transformed = 1;
2955 FT_Glyph_Metrics* metrics = &glyph->root.metrics;
2957 FT_Bool has_vertical_info;
2960 /* copy the _unscaled_ advance width */
2961 metrics->horiAdvance = decoder.glyph_width;
2962 glyph->root.linearHoriAdvance = decoder.glyph_width;
2963 glyph->root.internal->glyph_transformed = 0;
2965 has_vertical_info = FT_BOOL( face->vertical_info &&
2966 face->vertical.number_Of_VMetrics > 0 );
2968 /* get the vertical metrics from the vtmx table if we have one */
2969 if ( has_vertical_info )
2971 FT_Short vertBearingY = 0;
2972 FT_UShort vertAdvance = 0;
2975 ( (SFNT_Service)face->sfnt )->get_metrics( face, 1,
2979 metrics->vertBearingY = vertBearingY;
2980 metrics->vertAdvance = vertAdvance;
2984 /* make up vertical ones */
2985 if ( face->os2.version != 0xFFFFU )
2986 metrics->vertAdvance = (FT_Pos)( face->os2.sTypoAscender -
2987 face->os2.sTypoDescender );
2989 metrics->vertAdvance = (FT_Pos)( face->horizontal.Ascender -
2990 face->horizontal.Descender );
2993 glyph->root.linearVertAdvance = metrics->vertAdvance;
2995 glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;
2997 glyph->root.outline.flags = 0;
2998 if ( size && size->root.metrics.y_ppem < 24 )
2999 glyph->root.outline.flags |= FT_OUTLINE_HIGH_PRECISION;
3001 glyph->root.outline.flags |= FT_OUTLINE_REVERSE_FILL;
3003 if ( !( font_matrix.xx == 0x10000L &&
3004 font_matrix.yy == 0x10000L &&
3005 font_matrix.xy == 0 &&
3006 font_matrix.yx == 0 ) )
3007 FT_Outline_Transform( &glyph->root.outline, &font_matrix );
3009 if ( !( font_offset.x == 0 &&
3010 font_offset.y == 0 ) )
3011 FT_Outline_Translate( &glyph->root.outline,
3012 font_offset.x, font_offset.y );
3014 advance.x = metrics->horiAdvance;
3016 FT_Vector_Transform( &advance, &font_matrix );
3017 metrics->horiAdvance = advance.x + font_offset.x;
3020 advance.y = metrics->vertAdvance;
3021 FT_Vector_Transform( &advance, &font_matrix );
3022 metrics->vertAdvance = advance.y + font_offset.y;
3024 if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 || force_scaling )
3026 /* scale the outline and the metrics */
3028 FT_Outline* cur = &glyph->root.outline;
3029 FT_Vector* vec = cur->points;
3030 FT_Fixed x_scale = glyph->x_scale;
3031 FT_Fixed y_scale = glyph->y_scale;
3034 /* First of all, scale the points */
3035 if ( !hinting || !decoder.builder.hints_funcs )
3036 for ( n = cur->n_points; n > 0; n--, vec++ )
3038 vec->x = FT_MulFix( vec->x, x_scale );
3039 vec->y = FT_MulFix( vec->y, y_scale );
3042 /* Then scale the metrics */
3043 metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale );
3044 metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale );
3047 /* compute the other metrics */
3048 FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
3050 metrics->width = cbox.xMax - cbox.xMin;
3051 metrics->height = cbox.yMax - cbox.yMin;
3053 metrics->horiBearingX = cbox.xMin;
3054 metrics->horiBearingY = cbox.yMax;
3056 if ( has_vertical_info )
3057 metrics->vertBearingX = metrics->horiBearingX -
3058 metrics->horiAdvance / 2;
3061 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
3062 ft_synthesize_vertical_metrics( metrics,
3063 metrics->vertAdvance );