1 /***************************************************************************/
5 /* OpenType Glyph Loader (body). */
7 /* Copyright 1996-2014 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 /***************************************************************************/
20 #include FT_INTERNAL_DEBUG_H
21 #include FT_INTERNAL_STREAM_H
22 #include FT_INTERNAL_SFNT_H
24 #include FT_CFF_DRIVER_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(( " in subfont %d:\n", 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];
451 decoder->num_locals = sub->local_subrs_index.count;
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 decoder->current_subfont = sub; /* for Adobe's CFF handler */
467 /* check that there is enough space for `count' more points */
468 FT_LOCAL_DEF( FT_Error )
469 cff_check_points( CFF_Builder* builder,
472 return FT_GLYPHLOADER_CHECK_POINTS( builder->loader, count, 0 );
476 /* add a new point, do not check space */
478 cff_builder_add_point( CFF_Builder* builder,
483 FT_Outline* outline = builder->current;
486 if ( builder->load_points )
488 FT_Vector* point = outline->points + outline->n_points;
489 FT_Byte* control = (FT_Byte*)outline->tags + outline->n_points;
491 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
492 CFF_Driver driver = (CFF_Driver)FT_FACE_DRIVER( builder->face );
495 if ( driver->hinting_engine == FT_CFF_HINTING_FREETYPE )
503 /* cf2_decoder_parse_charstrings uses 16.16 coordinates */
507 *control = (FT_Byte)( flag ? FT_CURVE_TAG_ON : FT_CURVE_TAG_CUBIC );
514 /* check space for a new on-curve point, then add it */
515 FT_LOCAL_DEF( FT_Error )
516 cff_builder_add_point1( CFF_Builder* builder,
523 error = cff_check_points( builder, 1 );
525 cff_builder_add_point( builder, x, y, 1 );
531 /* check space for a new contour, then add it */
533 cff_builder_add_contour( CFF_Builder* builder )
535 FT_Outline* outline = builder->current;
539 if ( !builder->load_points )
541 outline->n_contours++;
545 error = FT_GLYPHLOADER_CHECK_POINTS( builder->loader, 0, 1 );
548 if ( outline->n_contours > 0 )
549 outline->contours[outline->n_contours - 1] =
550 (short)( outline->n_points - 1 );
552 outline->n_contours++;
559 /* if a path was begun, add its first on-curve point */
560 FT_LOCAL_DEF( FT_Error )
561 cff_builder_start_point( CFF_Builder* builder,
565 FT_Error error = FT_Err_Ok;
568 /* test whether we are building a new contour */
569 if ( !builder->path_begun )
571 builder->path_begun = 1;
572 error = cff_builder_add_contour( builder );
574 error = cff_builder_add_point1( builder, x, y );
581 /* close the current contour */
583 cff_builder_close_contour( CFF_Builder* builder )
585 FT_Outline* outline = builder->current;
592 first = outline->n_contours <= 1
593 ? 0 : outline->contours[outline->n_contours - 2] + 1;
595 /* We must not include the last point in the path if it */
596 /* is located on the first point. */
597 if ( outline->n_points > 1 )
599 FT_Vector* p1 = outline->points + first;
600 FT_Vector* p2 = outline->points + outline->n_points - 1;
601 FT_Byte* control = (FT_Byte*)outline->tags + outline->n_points - 1;
604 /* `delete' last point only if it coincides with the first */
605 /* point and if it is not a control point (which can happen). */
606 if ( p1->x == p2->x && p1->y == p2->y )
607 if ( *control == FT_CURVE_TAG_ON )
611 if ( outline->n_contours > 0 )
613 /* Don't add contours only consisting of one point, i.e., */
614 /* check whether begin point and last point are the same. */
615 if ( first == outline->n_points - 1 )
617 outline->n_contours--;
621 outline->contours[outline->n_contours - 1] =
622 (short)( outline->n_points - 1 );
627 FT_LOCAL_DEF( FT_Int )
628 cff_lookup_glyph_by_stdcharcode( CFF_Font cff,
635 /* CID-keyed fonts don't have glyph names */
636 if ( !cff->charset.sids )
639 /* check range of standard char code */
640 if ( charcode < 0 || charcode > 255 )
643 /* Get code to SID mapping from `cff_standard_encoding'. */
644 glyph_sid = cff_get_standard_encoding( (FT_UInt)charcode );
646 for ( n = 0; n < cff->num_glyphs; n++ )
648 if ( cff->charset.sids[n] == glyph_sid )
656 FT_LOCAL_DEF( FT_Error )
657 cff_get_glyph_data( TT_Face face,
662 #ifdef FT_CONFIG_OPTION_INCREMENTAL
663 /* For incremental fonts get the character data using the */
664 /* callback function. */
665 if ( face->root.internal->incremental_interface )
669 face->root.internal->incremental_interface->funcs->get_glyph_data(
670 face->root.internal->incremental_interface->object,
671 glyph_index, &data );
674 *pointer = (FT_Byte*)data.pointer;
675 *length = data.length;
680 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
683 CFF_Font cff = (CFF_Font)(face->extra.data);
686 return cff_index_access_element( &cff->charstrings_index, glyph_index,
693 cff_free_glyph_data( TT_Face face,
697 #ifndef FT_CONFIG_OPTION_INCREMENTAL
701 #ifdef FT_CONFIG_OPTION_INCREMENTAL
702 /* For incremental fonts get the character data using the */
703 /* callback function. */
704 if ( face->root.internal->incremental_interface )
709 data.pointer = *pointer;
710 data.length = length;
712 face->root.internal->incremental_interface->funcs->free_glyph_data(
713 face->root.internal->incremental_interface->object, &data );
716 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
719 CFF_Font cff = (CFF_Font)(face->extra.data);
722 cff_index_forget_element( &cff->charstrings_index, pointer );
727 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
730 cff_operator_seac( CFF_Decoder* decoder,
738 CFF_Builder* builder = &decoder->builder;
739 FT_Int bchar_index, achar_index;
740 TT_Face face = decoder->builder.face;
741 FT_Vector left_bearing, advance;
743 FT_ULong charstring_len;
749 FT_ERROR(( "cff_operator_seac: invalid nested seac\n" ));
750 return FT_THROW( Syntax_Error );
753 adx += decoder->builder.left_bearing.x;
754 ady += decoder->builder.left_bearing.y;
756 #ifdef FT_CONFIG_OPTION_INCREMENTAL
757 /* Incremental fonts don't necessarily have valid charsets. */
758 /* They use the character code, not the glyph index, in this case. */
759 if ( face->root.internal->incremental_interface )
765 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
767 CFF_Font cff = (CFF_Font)(face->extra.data);
770 bchar_index = cff_lookup_glyph_by_stdcharcode( cff, bchar );
771 achar_index = cff_lookup_glyph_by_stdcharcode( cff, achar );
774 if ( bchar_index < 0 || achar_index < 0 )
776 FT_ERROR(( "cff_operator_seac:"
777 " invalid seac character code arguments\n" ));
778 return FT_THROW( Syntax_Error );
781 /* If we are trying to load a composite glyph, do not load the */
782 /* accent character and return the array of subglyphs. */
783 if ( builder->no_recurse )
785 FT_GlyphSlot glyph = (FT_GlyphSlot)builder->glyph;
786 FT_GlyphLoader loader = glyph->internal->loader;
790 /* reallocate subglyph array if necessary */
791 error = FT_GlyphLoader_CheckSubGlyphs( loader, 2 );
795 subg = loader->current.subglyphs;
797 /* subglyph 0 = base character */
798 subg->index = bchar_index;
799 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES |
800 FT_SUBGLYPH_FLAG_USE_MY_METRICS;
805 /* subglyph 1 = accent character */
806 subg->index = achar_index;
807 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES;
808 subg->arg1 = (FT_Int)( adx >> 16 );
809 subg->arg2 = (FT_Int)( ady >> 16 );
811 /* set up remaining glyph fields */
812 glyph->num_subglyphs = 2;
813 glyph->subglyphs = loader->base.subglyphs;
814 glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
816 loader->current.num_subglyphs = 2;
819 FT_GlyphLoader_Prepare( builder->loader );
821 /* First load `bchar' in builder */
822 error = cff_get_glyph_data( face, bchar_index,
823 &charstring, &charstring_len );
826 /* the seac operator must not be nested */
827 decoder->seac = TRUE;
828 error = cff_decoder_parse_charstrings( decoder, charstring,
830 decoder->seac = FALSE;
832 cff_free_glyph_data( face, &charstring, charstring_len );
838 /* Save the left bearing, advance and glyph width of the base */
839 /* character as they will be erased by the next load. */
841 left_bearing = builder->left_bearing;
842 advance = builder->advance;
843 glyph_width = decoder->glyph_width;
845 builder->left_bearing.x = 0;
846 builder->left_bearing.y = 0;
848 builder->pos_x = adx - asb;
849 builder->pos_y = ady;
851 /* Now load `achar' on top of the base outline. */
852 error = cff_get_glyph_data( face, achar_index,
853 &charstring, &charstring_len );
856 /* the seac operator must not be nested */
857 decoder->seac = TRUE;
858 error = cff_decoder_parse_charstrings( decoder, charstring,
860 decoder->seac = FALSE;
862 cff_free_glyph_data( face, &charstring, charstring_len );
868 /* Restore the left side bearing, advance and glyph width */
869 /* of the base character. */
870 builder->left_bearing = left_bearing;
871 builder->advance = advance;
872 decoder->glyph_width = glyph_width;
882 /*************************************************************************/
885 /* cff_decoder_parse_charstrings */
888 /* Parses a given Type 2 charstrings program. */
891 /* decoder :: The current Type 1 decoder. */
894 /* charstring_base :: The base of the charstring stream. */
896 /* charstring_len :: The length in bytes of the charstring stream. */
899 /* FreeType error code. 0 means success. */
901 FT_LOCAL_DEF( FT_Error )
902 cff_decoder_parse_charstrings( CFF_Decoder* decoder,
903 FT_Byte* charstring_base,
904 FT_ULong charstring_len )
907 CFF_Decoder_Zone* zone;
910 CFF_Builder* builder = &decoder->builder;
914 FT_Int charstring_type =
915 decoder->cff->top_font.font_dict.charstring_type;
917 T2_Hints_Funcs hinter;
920 /* set default width */
921 decoder->num_hints = 0;
922 decoder->read_width = 1;
924 /* compute random seed from stack address of parameter */
925 seed = (FT_Fixed)( ( (FT_PtrDist)(char*)&seed ^
926 (FT_PtrDist)(char*)&decoder ^
927 (FT_PtrDist)(char*)&charstring_base ) &
929 seed = ( seed ^ ( seed >> 10 ) ^ ( seed >> 20 ) ) & 0xFFFFL;
933 /* initialize the decoder */
934 decoder->top = decoder->stack;
935 decoder->zone = decoder->zones;
936 zone = decoder->zones;
937 stack = decoder->top;
939 hinter = (T2_Hints_Funcs)builder->hints_funcs;
941 builder->path_begun = 0;
943 zone->base = charstring_base;
944 limit = zone->limit = charstring_base + charstring_len;
945 ip = zone->cursor = zone->base;
952 /* begin hints recording session, if any */
954 hinter->open( hinter->hints );
956 /* now execute loop */
963 /********************************************************************/
965 /* Decode operator or operand */
968 if ( v >= 32 || v == 28 )
974 /* this is an operand, push it on the stack */
976 /* if we use shifts, all computations are done with unsigned */
977 /* values; the conversion to a signed value is the last step */
980 if ( ip + 1 >= limit )
982 val = (FT_Short)( ( (FT_UShort)ip[0] << 8 ) | ip[1] );
986 val = (FT_Int32)v - 139;
991 val = ( (FT_Int32)v - 247 ) * 256 + *ip++ + 108;
997 val = -( (FT_Int32)v - 251 ) * 256 - *ip++ - 108;
1001 if ( ip + 3 >= limit )
1003 val = (FT_Int32)( ( (FT_UInt32)ip[0] << 24 ) |
1004 ( (FT_UInt32)ip[1] << 16 ) |
1005 ( (FT_UInt32)ip[2] << 8 ) |
1008 if ( charstring_type == 2 )
1011 if ( decoder->top - stack >= CFF_MAX_OPERANDS )
1012 goto Stack_Overflow;
1014 val = (FT_Int32)( (FT_UInt32)val << shift );
1015 *decoder->top++ = val;
1017 #ifdef FT_DEBUG_LEVEL_TRACE
1018 if ( !( val & 0xFFFFL ) )
1019 FT_TRACE4(( " %hd", (FT_Short)( (FT_UInt32)val >> 16 ) ));
1021 FT_TRACE4(( " %.2f", val / 65536.0 ));
1027 /* The specification says that normally arguments are to be taken */
1028 /* from the bottom of the stack. However, this seems not to be */
1029 /* correct, at least for Acroread 7.0.8 on GNU/Linux: It pops the */
1030 /* arguments similar to a PS interpreter. */
1032 FT_Fixed* args = decoder->top;
1033 FT_Int num_args = (FT_Int)( args - decoder->stack );
1038 op = cff_op_unknown;
1049 op = cff_op_vmoveto;
1052 op = cff_op_rlineto;
1055 op = cff_op_hlineto;
1058 op = cff_op_vlineto;
1061 op = cff_op_rrcurveto;
1064 op = cff_op_closepath;
1067 op = cff_op_callsubr;
1081 op = cff_op_dotsection;
1083 case 1: /* this is actually the Type1 vstem3 operator */
1086 case 2: /* this is actually the Type1 hstem3 operator */
1129 op = cff_op_callothersubr;
1168 op = cff_op_setcurrentpoint;
1183 FT_TRACE4(( " unknown op (12, %d)\n", v ));
1192 op = cff_op_endchar;
1198 op = cff_op_hstemhm;
1201 op = cff_op_hintmask;
1204 op = cff_op_cntrmask;
1207 op = cff_op_rmoveto;
1210 op = cff_op_hmoveto;
1213 op = cff_op_vstemhm;
1216 op = cff_op_rcurveline;
1219 op = cff_op_rlinecurve;
1222 op = cff_op_vvcurveto;
1225 op = cff_op_hhcurveto;
1228 op = cff_op_callgsubr;
1231 op = cff_op_vhcurveto;
1234 op = cff_op_hvcurveto;
1237 FT_TRACE4(( " unknown op (%d)\n", v ));
1241 if ( op == cff_op_unknown )
1244 /* check arguments */
1245 req_args = cff_argument_counts[op];
1246 if ( req_args & CFF_COUNT_CHECK_WIDTH )
1248 if ( num_args > 0 && decoder->read_width )
1250 /* If `nominal_width' is non-zero, the number is really a */
1251 /* difference against `nominal_width'. Else, the number here */
1252 /* is truly a width, not a difference against `nominal_width'. */
1253 /* If the font does not set `nominal_width', then */
1254 /* `nominal_width' defaults to zero, and so we can set */
1255 /* `glyph_width' to `nominal_width' plus number on the stack */
1256 /* -- for either case. */
1258 FT_Int set_width_ok;
1263 case cff_op_hmoveto:
1264 case cff_op_vmoveto:
1265 set_width_ok = num_args & 2;
1270 case cff_op_hstemhm:
1271 case cff_op_vstemhm:
1272 case cff_op_rmoveto:
1273 case cff_op_hintmask:
1274 case cff_op_cntrmask:
1275 set_width_ok = num_args & 1;
1278 case cff_op_endchar:
1279 /* If there is a width specified for endchar, we either have */
1280 /* 1 argument or 5 arguments. We like to argue. */
1281 set_width_ok = ( num_args == 5 ) || ( num_args == 1 );
1291 decoder->glyph_width = decoder->nominal_width +
1294 if ( decoder->width_only )
1296 /* we only want the advance width; stop here */
1300 /* Consumed an argument. */
1305 decoder->read_width = 0;
1310 if ( num_args < req_args )
1311 goto Stack_Underflow;
1313 num_args -= req_args;
1315 /* At this point, `args' points to the first argument of the */
1316 /* operand in case `req_args' isn't zero. Otherwise, we have */
1317 /* to adjust `args' manually. */
1319 /* Note that we only pop arguments from the stack which we */
1320 /* really need and can digest so that we can continue in case */
1321 /* of superfluous stack elements. */
1327 case cff_op_hstemhm:
1328 case cff_op_vstemhm:
1329 /* the number of arguments is always even here */
1331 op == cff_op_hstem ? " hstem\n" :
1332 ( op == cff_op_vstem ? " vstem\n" :
1333 ( op == cff_op_hstemhm ? " hstemhm\n" : " vstemhm\n" ) ) ));
1336 hinter->stems( hinter->hints,
1337 ( op == cff_op_hstem || op == cff_op_hstemhm ),
1339 args - ( num_args & ~1 ) );
1341 decoder->num_hints += num_args / 2;
1345 case cff_op_hintmask:
1346 case cff_op_cntrmask:
1347 FT_TRACE4(( op == cff_op_hintmask ? " hintmask" : " cntrmask" ));
1349 /* implement vstem when needed -- */
1350 /* the specification doesn't say it, but this also works */
1351 /* with the 'cntrmask' operator */
1356 hinter->stems( hinter->hints,
1359 args - ( num_args & ~1 ) );
1361 decoder->num_hints += num_args / 2;
1364 /* In a valid charstring there must be at least one byte */
1365 /* after `hintmask' or `cntrmask' (e.g., for a `return' */
1366 /* instruction). Additionally, there must be space for */
1367 /* `num_hints' bits. */
1369 if ( ( ip + ( ( decoder->num_hints + 7 ) >> 3 ) ) >= limit )
1374 if ( op == cff_op_hintmask )
1375 hinter->hintmask( hinter->hints,
1376 builder->current->n_points,
1380 hinter->counter( hinter->hints,
1385 #ifdef FT_DEBUG_LEVEL_TRACE
1390 FT_TRACE4(( " (maskbytes:" ));
1393 maskbyte < (FT_UInt)( ( decoder->num_hints + 7 ) >> 3 );
1395 FT_TRACE4(( " 0x%02X", *ip ));
1397 FT_TRACE4(( ")\n" ));
1400 ip += ( decoder->num_hints + 7 ) >> 3;
1405 case cff_op_rmoveto:
1406 FT_TRACE4(( " rmoveto\n" ));
1408 cff_builder_close_contour( builder );
1409 builder->path_begun = 0;
1415 case cff_op_vmoveto:
1416 FT_TRACE4(( " vmoveto\n" ));
1418 cff_builder_close_contour( builder );
1419 builder->path_begun = 0;
1424 case cff_op_hmoveto:
1425 FT_TRACE4(( " hmoveto\n" ));
1427 cff_builder_close_contour( builder );
1428 builder->path_begun = 0;
1433 case cff_op_rlineto:
1434 FT_TRACE4(( " rlineto\n" ));
1436 if ( cff_builder_start_point( builder, x, y ) ||
1437 cff_check_points( builder, num_args / 2 ) )
1441 goto Stack_Underflow;
1443 args -= num_args & ~1;
1444 while ( args < decoder->top )
1448 cff_builder_add_point( builder, x, y, 1 );
1454 case cff_op_hlineto:
1455 case cff_op_vlineto:
1457 FT_Int phase = ( op == cff_op_hlineto );
1460 FT_TRACE4(( op == cff_op_hlineto ? " hlineto\n"
1464 goto Stack_Underflow;
1466 /* there exist subsetted fonts (found in PDFs) */
1467 /* which call `hlineto' without arguments */
1468 if ( num_args == 0 )
1471 if ( cff_builder_start_point( builder, x, y ) ||
1472 cff_check_points( builder, num_args ) )
1476 while ( args < decoder->top )
1483 if ( cff_builder_add_point1( builder, x, y ) )
1493 case cff_op_rrcurveto:
1498 FT_TRACE4(( " rrcurveto\n" ));
1501 goto Stack_Underflow;
1503 nargs = num_args - num_args % 6;
1505 if ( cff_builder_start_point( builder, x, y ) ||
1506 cff_check_points( builder, nargs / 2 ) )
1510 while ( args < decoder->top )
1514 cff_builder_add_point( builder, x, y, 0 );
1517 cff_builder_add_point( builder, x, y, 0 );
1520 cff_builder_add_point( builder, x, y, 1 );
1527 case cff_op_vvcurveto:
1532 FT_TRACE4(( " vvcurveto\n" ));
1535 goto Stack_Underflow;
1537 /* if num_args isn't of the form 4n or 4n+1, */
1538 /* we enforce it by clearing the second bit */
1540 nargs = num_args & ~2;
1542 if ( cff_builder_start_point( builder, x, y ) )
1554 if ( cff_check_points( builder, 3 * ( nargs / 4 ) ) )
1557 while ( args < decoder->top )
1560 cff_builder_add_point( builder, x, y, 0 );
1563 cff_builder_add_point( builder, x, y, 0 );
1565 cff_builder_add_point( builder, x, y, 1 );
1572 case cff_op_hhcurveto:
1577 FT_TRACE4(( " hhcurveto\n" ));
1580 goto Stack_Underflow;
1582 /* if num_args isn't of the form 4n or 4n+1, */
1583 /* we enforce it by clearing the second bit */
1585 nargs = num_args & ~2;
1587 if ( cff_builder_start_point( builder, x, y ) )
1598 if ( cff_check_points( builder, 3 * ( nargs / 4 ) ) )
1601 while ( args < decoder->top )
1604 cff_builder_add_point( builder, x, y, 0 );
1607 cff_builder_add_point( builder, x, y, 0 );
1609 cff_builder_add_point( builder, x, y, 1 );
1616 case cff_op_vhcurveto:
1617 case cff_op_hvcurveto:
1623 FT_TRACE4(( op == cff_op_vhcurveto ? " vhcurveto\n"
1624 : " hvcurveto\n" ));
1626 if ( cff_builder_start_point( builder, x, y ) )
1630 goto Stack_Underflow;
1632 /* if num_args isn't of the form 8n, 8n+1, 8n+4, or 8n+5, */
1633 /* we enforce it by clearing the second bit */
1635 nargs = num_args & ~2;
1638 if ( cff_check_points( builder, ( nargs / 4 ) * 3 ) )
1639 goto Stack_Underflow;
1641 phase = ( op == cff_op_hvcurveto );
1643 while ( nargs >= 4 )
1649 cff_builder_add_point( builder, x, y, 0 );
1652 cff_builder_add_point( builder, x, y, 0 );
1656 cff_builder_add_point( builder, x, y, 1 );
1661 cff_builder_add_point( builder, x, y, 0 );
1664 cff_builder_add_point( builder, x, y, 0 );
1668 cff_builder_add_point( builder, x, y, 1 );
1677 case cff_op_rlinecurve:
1683 FT_TRACE4(( " rlinecurve\n" ));
1686 goto Stack_Underflow;
1688 nargs = num_args & ~1;
1689 num_lines = ( nargs - 6 ) / 2;
1691 if ( cff_builder_start_point( builder, x, y ) ||
1692 cff_check_points( builder, num_lines + 3 ) )
1697 /* first, add the line segments */
1698 while ( num_lines > 0 )
1702 cff_builder_add_point( builder, x, y, 1 );
1707 /* then the curve */
1710 cff_builder_add_point( builder, x, y, 0 );
1713 cff_builder_add_point( builder, x, y, 0 );
1716 cff_builder_add_point( builder, x, y, 1 );
1721 case cff_op_rcurveline:
1727 FT_TRACE4(( " rcurveline\n" ));
1730 goto Stack_Underflow;
1732 nargs = num_args - 2;
1733 nargs = nargs - nargs % 6 + 2;
1734 num_curves = ( nargs - 2 ) / 6;
1736 if ( cff_builder_start_point( builder, x, y ) ||
1737 cff_check_points( builder, num_curves * 3 + 2 ) )
1742 /* first, add the curves */
1743 while ( num_curves > 0 )
1747 cff_builder_add_point( builder, x, y, 0 );
1750 cff_builder_add_point( builder, x, y, 0 );
1753 cff_builder_add_point( builder, x, y, 1 );
1758 /* then the final line */
1761 cff_builder_add_point( builder, x, y, 1 );
1771 FT_TRACE4(( " hflex1\n" ));
1773 /* adding five more points: 4 control points, 1 on-curve point */
1774 /* -- make sure we have enough space for the start point if it */
1775 /* needs to be added */
1776 if ( cff_builder_start_point( builder, x, y ) ||
1777 cff_check_points( builder, 6 ) )
1780 /* record the starting point's y position for later use */
1783 /* first control point */
1786 cff_builder_add_point( builder, x, y, 0 );
1788 /* second control point */
1791 cff_builder_add_point( builder, x, y, 0 );
1793 /* join point; on curve, with y-value the same as the last */
1794 /* control point's y-value */
1796 cff_builder_add_point( builder, x, y, 1 );
1798 /* third control point, with y-value the same as the join */
1799 /* point's y-value */
1801 cff_builder_add_point( builder, x, y, 0 );
1803 /* fourth control point */
1806 cff_builder_add_point( builder, x, y, 0 );
1808 /* ending point, with y-value the same as the start */
1811 cff_builder_add_point( builder, x, y, 1 );
1822 FT_TRACE4(( " hflex\n" ));
1824 /* adding six more points; 4 control points, 2 on-curve points */
1825 if ( cff_builder_start_point( builder, x, y ) ||
1826 cff_check_points( builder, 6 ) )
1829 /* record the starting point's y-position for later use */
1832 /* first control point */
1834 cff_builder_add_point( builder, x, y, 0 );
1836 /* second control point */
1839 cff_builder_add_point( builder, x, y, 0 );
1841 /* join point; on curve, with y-value the same as the last */
1842 /* control point's y-value */
1844 cff_builder_add_point( builder, x, y, 1 );
1846 /* third control point, with y-value the same as the join */
1847 /* point's y-value */
1849 cff_builder_add_point( builder, x, y, 0 );
1851 /* fourth control point */
1854 cff_builder_add_point( builder, x, y, 0 );
1856 /* ending point, with y-value the same as the start point's */
1857 /* y-value -- we don't add this point, though */
1859 cff_builder_add_point( builder, x, y, 1 );
1867 FT_Pos start_x, start_y; /* record start x, y values for */
1869 FT_Fixed dx = 0, dy = 0; /* used in horizontal/vertical */
1870 /* algorithm below */
1871 FT_Int horizontal, count;
1875 FT_TRACE4(( " flex1\n" ));
1877 /* adding six more points; 4 control points, 2 on-curve points */
1878 if ( cff_builder_start_point( builder, x, y ) ||
1879 cff_check_points( builder, 6 ) )
1882 /* record the starting point's x, y position for later use */
1886 /* XXX: figure out whether this is supposed to be a horizontal */
1887 /* or vertical flex; the Type 2 specification is vague... */
1891 /* grab up to the last argument */
1892 for ( count = 5; count > 0; count-- )
1904 /* strange test, but here it is... */
1905 horizontal = ( dx > dy );
1907 for ( count = 5; count > 0; count-- )
1911 cff_builder_add_point( builder, x, y,
1912 (FT_Bool)( count == 3 ) );
1916 /* is last operand an x- or y-delta? */
1928 cff_builder_add_point( builder, x, y, 1 );
1939 FT_TRACE4(( " flex\n" ));
1941 if ( cff_builder_start_point( builder, x, y ) ||
1942 cff_check_points( builder, 6 ) )
1945 for ( count = 6; count > 0; count-- )
1949 cff_builder_add_point( builder, x, y,
1950 (FT_Bool)( count == 4 || count == 1 ) );
1959 FT_TRACE4(( " seac\n" ));
1961 error = cff_operator_seac( decoder,
1962 args[0], args[1], args[2],
1963 (FT_Int)( args[3] >> 16 ),
1964 (FT_Int)( args[4] >> 16 ) );
1966 /* add current outline to the glyph slot */
1967 FT_GlyphLoader_Add( builder->loader );
1970 FT_TRACE4(( "\n" ));
1973 case cff_op_endchar:
1974 FT_TRACE4(( " endchar\n" ));
1976 /* We are going to emulate the seac operator. */
1977 if ( num_args >= 4 )
1979 /* Save glyph width so that the subglyphs don't overwrite it. */
1980 FT_Pos glyph_width = decoder->glyph_width;
1983 error = cff_operator_seac( decoder,
1984 0L, args[-4], args[-3],
1985 (FT_Int)( args[-2] >> 16 ),
1986 (FT_Int)( args[-1] >> 16 ) );
1988 decoder->glyph_width = glyph_width;
1992 cff_builder_close_contour( builder );
1994 /* close hints recording session */
1997 if ( hinter->close( hinter->hints,
1998 builder->current->n_points ) )
2001 /* apply hints to the loaded glyph outline now */
2002 error = hinter->apply( hinter->hints,
2004 (PSH_Globals)builder->hints_globals,
2005 decoder->hint_mode );
2010 /* add current outline to the glyph slot */
2011 FT_GlyphLoader_Add( builder->loader );
2015 FT_TRACE4(( "\n" ));
2019 FT_TRACE4(( " abs\n" ));
2027 FT_TRACE4(( " add\n" ));
2034 FT_TRACE4(( " sub\n" ));
2041 FT_TRACE4(( " div\n" ));
2043 args[0] = FT_DivFix( args[0], args[1] );
2048 FT_TRACE4(( " neg\n" ));
2059 FT_TRACE4(( " rand\n" ));
2062 if ( Rand >= 0x8000L )
2066 seed = FT_MulFix( seed, 0x10000L - seed );
2074 FT_TRACE4(( " mul\n" ));
2076 args[0] = FT_MulFix( args[0], args[1] );
2081 FT_TRACE4(( " sqrt\n" ));
2086 FT_Fixed root = args[0];
2092 new_root = ( root + FT_DivFix( args[0], root ) + 1 ) >> 1;
2093 if ( new_root == root || count <= 0 )
2106 FT_TRACE4(( " drop\n" ));
2115 FT_TRACE4(( " exch\n" ));
2126 FT_Int idx = (FT_Int)( args[0] >> 16 );
2129 FT_TRACE4(( " index\n" ));
2133 else if ( idx > num_args - 2 )
2135 args[0] = args[-( idx + 1 )];
2142 FT_Int count = (FT_Int)( args[0] >> 16 );
2143 FT_Int idx = (FT_Int)( args[1] >> 16 );
2146 FT_TRACE4(( " roll\n" ));
2153 goto Stack_Underflow;
2159 FT_Fixed tmp = args[count - 1];
2163 for ( i = count - 2; i >= 0; i-- )
2164 args[i + 1] = args[i];
2173 FT_Fixed tmp = args[0];
2177 for ( i = 0; i < count - 1; i++ )
2178 args[i] = args[i + 1];
2179 args[count - 1] = tmp;
2188 FT_TRACE4(( " dup\n" ));
2196 FT_Fixed val = args[0];
2197 FT_Int idx = (FT_Int)( args[1] >> 16 );
2200 FT_TRACE4(( " put\n" ));
2202 if ( idx >= 0 && idx < CFF_MAX_TRANS_ELEMENTS )
2203 decoder->buildchar[idx] = val;
2209 FT_Int idx = (FT_Int)( args[0] >> 16 );
2213 FT_TRACE4(( " get\n" ));
2215 if ( idx >= 0 && idx < CFF_MAX_TRANS_ELEMENTS )
2216 val = decoder->buildchar[idx];
2224 FT_TRACE4(( " store\n"));
2229 FT_TRACE4(( " load\n" ));
2233 case cff_op_dotsection:
2234 /* this operator is deprecated and ignored by the parser */
2235 FT_TRACE4(( " dotsection\n" ));
2238 case cff_op_closepath:
2239 /* this is an invalid Type 2 operator; however, there */
2240 /* exist fonts which are incorrectly converted from probably */
2241 /* Type 1 to CFF, and some parsers seem to accept it */
2243 FT_TRACE4(( " closepath (invalid op)\n" ));
2249 /* this is an invalid Type 2 operator; however, there */
2250 /* exist fonts which are incorrectly converted from probably */
2251 /* Type 1 to CFF, and some parsers seem to accept it */
2253 FT_TRACE4(( " hsbw (invalid op)\n" ));
2255 decoder->glyph_width = decoder->nominal_width + ( args[1] >> 16 );
2257 decoder->builder.left_bearing.x = args[0];
2258 decoder->builder.left_bearing.y = 0;
2260 x = decoder->builder.pos_x + args[0];
2261 y = decoder->builder.pos_y;
2266 /* this is an invalid Type 2 operator; however, there */
2267 /* exist fonts which are incorrectly converted from probably */
2268 /* Type 1 to CFF, and some parsers seem to accept it */
2270 FT_TRACE4(( " sbw (invalid op)\n" ));
2272 decoder->glyph_width = decoder->nominal_width + ( args[2] >> 16 );
2274 decoder->builder.left_bearing.x = args[0];
2275 decoder->builder.left_bearing.y = args[1];
2277 x = decoder->builder.pos_x + args[0];
2278 y = decoder->builder.pos_y + args[1];
2282 case cff_op_setcurrentpoint:
2283 /* this is an invalid Type 2 operator; however, there */
2284 /* exist fonts which are incorrectly converted from probably */
2285 /* Type 1 to CFF, and some parsers seem to accept it */
2287 FT_TRACE4(( " setcurrentpoint (invalid op)\n" ));
2289 x = decoder->builder.pos_x + args[0];
2290 y = decoder->builder.pos_y + args[1];
2294 case cff_op_callothersubr:
2295 /* this is an invalid Type 2 operator; however, there */
2296 /* exist fonts which are incorrectly converted from probably */
2297 /* Type 1 to CFF, and some parsers seem to accept it */
2299 FT_TRACE4(( " callothersubr (invalid op)\n" ));
2301 /* subsequent `pop' operands should add the arguments, */
2302 /* this is the implementation described for `unknown' other */
2303 /* subroutines in the Type1 spec. */
2305 /* XXX Fix return arguments (see discussion below). */
2306 args -= 2 + ( args[-2] >> 16 );
2308 goto Stack_Underflow;
2312 /* this is an invalid Type 2 operator; however, there */
2313 /* exist fonts which are incorrectly converted from probably */
2314 /* Type 1 to CFF, and some parsers seem to accept it */
2316 FT_TRACE4(( " pop (invalid op)\n" ));
2318 /* XXX Increasing `args' is wrong: After a certain number of */
2319 /* `pop's we get a stack overflow. Reason for doing it is */
2320 /* code like this (actually found in a CFF font): */
2322 /* 17 1 3 callothersubr */
2326 /* Since we handle `callothersubr' as a no-op, and */
2327 /* `callsubr' needs at least one argument, `pop' can't be a */
2328 /* no-op too as it basically should be. */
2330 /* The right solution would be to provide real support for */
2331 /* `callothersubr' as done in `t1decode.c', however, given */
2332 /* the fact that CFF fonts with `pop' are invalid, it is */
2333 /* questionable whether it is worth the time. */
2339 FT_Fixed cond = args[0] && args[1];
2342 FT_TRACE4(( " and\n" ));
2344 args[0] = cond ? 0x10000L : 0;
2351 FT_Fixed cond = args[0] || args[1];
2354 FT_TRACE4(( " or\n" ));
2356 args[0] = cond ? 0x10000L : 0;
2363 FT_Fixed cond = !args[0];
2366 FT_TRACE4(( " eq\n" ));
2368 args[0] = cond ? 0x10000L : 0;
2375 FT_Fixed cond = ( args[2] <= args[3] );
2378 FT_TRACE4(( " ifelse\n" ));
2386 case cff_op_callsubr:
2388 FT_UInt idx = (FT_UInt)( ( args[0] >> 16 ) +
2389 decoder->locals_bias );
2392 FT_TRACE4(( " callsubr(%d)\n", idx ));
2394 if ( idx >= decoder->num_locals )
2396 FT_ERROR(( "cff_decoder_parse_charstrings:"
2397 " invalid local subr index\n" ));
2401 if ( zone - decoder->zones >= CFF_MAX_SUBRS_CALLS )
2403 FT_ERROR(( "cff_decoder_parse_charstrings:"
2404 " too many nested subrs\n" ));
2408 zone->cursor = ip; /* save current instruction pointer */
2411 zone->base = decoder->locals[idx];
2412 zone->limit = decoder->locals[idx + 1];
2413 zone->cursor = zone->base;
2415 if ( !zone->base || zone->limit == zone->base )
2417 FT_ERROR(( "cff_decoder_parse_charstrings:"
2418 " invoking empty subrs\n" ));
2422 decoder->zone = zone;
2424 limit = zone->limit;
2428 case cff_op_callgsubr:
2430 FT_UInt idx = (FT_UInt)( ( args[0] >> 16 ) +
2431 decoder->globals_bias );
2434 FT_TRACE4(( " callgsubr(%d)\n", idx ));
2436 if ( idx >= decoder->num_globals )
2438 FT_ERROR(( "cff_decoder_parse_charstrings:"
2439 " invalid global subr index\n" ));
2443 if ( zone - decoder->zones >= CFF_MAX_SUBRS_CALLS )
2445 FT_ERROR(( "cff_decoder_parse_charstrings:"
2446 " too many nested subrs\n" ));
2450 zone->cursor = ip; /* save current instruction pointer */
2453 zone->base = decoder->globals[idx];
2454 zone->limit = decoder->globals[idx + 1];
2455 zone->cursor = zone->base;
2457 if ( !zone->base || zone->limit == zone->base )
2459 FT_ERROR(( "cff_decoder_parse_charstrings:"
2460 " invoking empty subrs\n" ));
2464 decoder->zone = zone;
2466 limit = zone->limit;
2471 FT_TRACE4(( " return\n" ));
2473 if ( decoder->zone <= decoder->zones )
2475 FT_ERROR(( "cff_decoder_parse_charstrings:"
2476 " unexpected return\n" ));
2481 zone = decoder->zone;
2483 limit = zone->limit;
2488 FT_ERROR(( "Unimplemented opcode: %d", ip[-1] ));
2491 FT_ERROR(( " %d", ip[0] ));
2494 return FT_THROW( Unimplemented_Feature );
2497 decoder->top = args;
2499 if ( decoder->top - stack >= CFF_MAX_OPERANDS )
2500 goto Stack_Overflow;
2502 } /* general operator processing */
2504 } /* while ip < limit */
2506 FT_TRACE4(( "..end..\n\n" ));
2512 FT_TRACE4(( "cff_decoder_parse_charstrings: syntax error\n" ));
2513 return FT_THROW( Invalid_File_Format );
2516 FT_TRACE4(( "cff_decoder_parse_charstrings: stack underflow\n" ));
2517 return FT_THROW( Too_Few_Arguments );
2520 FT_TRACE4(( "cff_decoder_parse_charstrings: stack overflow\n" ));
2521 return FT_THROW( Stack_Overflow );
2524 #endif /* CFF_CONFIG_OPTION_OLD_ENGINE */
2527 /*************************************************************************/
2528 /*************************************************************************/
2529 /*************************************************************************/
2530 /********** *********/
2531 /********** *********/
2532 /********** COMPUTE THE MAXIMUM ADVANCE WIDTH *********/
2533 /********** *********/
2534 /********** The following code is in charge of computing *********/
2535 /********** the maximum advance width of the font. It *********/
2536 /********** quickly processes each glyph charstring to *********/
2537 /********** extract the value from either a `sbw' or `seac' *********/
2538 /********** operator. *********/
2539 /********** *********/
2540 /*************************************************************************/
2541 /*************************************************************************/
2542 /*************************************************************************/
2545 #if 0 /* unused until we support pure CFF fonts */
2548 FT_LOCAL_DEF( FT_Error )
2549 cff_compute_max_advance( TT_Face face,
2550 FT_Int* max_advance )
2552 FT_Error error = FT_Err_Ok;
2553 CFF_Decoder decoder;
2555 CFF_Font cff = (CFF_Font)face->other;
2560 /* Initialize load decoder */
2561 cff_decoder_init( &decoder, face, 0, 0, 0, 0 );
2563 decoder.builder.metrics_only = 1;
2564 decoder.builder.load_points = 0;
2566 /* For each glyph, parse the glyph charstring and extract */
2567 /* the advance width. */
2568 for ( glyph_index = 0; glyph_index < face->root.num_glyphs;
2571 FT_Byte* charstring;
2572 FT_ULong charstring_len;
2575 /* now get load the unscaled outline */
2576 error = cff_get_glyph_data( face, glyph_index,
2577 &charstring, &charstring_len );
2580 error = cff_decoder_prepare( &decoder, size, glyph_index );
2582 error = cff_decoder_parse_charstrings( &decoder,
2586 cff_free_glyph_data( face, &charstring, &charstring_len );
2589 /* ignore the error if one has occurred -- skip to next glyph */
2593 *max_advance = decoder.builder.advance.x;
2602 FT_LOCAL_DEF( FT_Error )
2603 cff_slot_load( CFF_GlyphSlot glyph,
2605 FT_UInt glyph_index,
2606 FT_Int32 load_flags )
2609 CFF_Decoder decoder;
2610 TT_Face face = (TT_Face)glyph->root.face;
2611 FT_Bool hinting, scaled, force_scaling;
2612 CFF_Font cff = (CFF_Font)face->extra.data;
2614 FT_Matrix font_matrix;
2615 FT_Vector font_offset;
2618 force_scaling = FALSE;
2620 /* in a CID-keyed font, consider `glyph_index' as a CID and map */
2621 /* it immediately to the real glyph_index -- if it isn't a */
2622 /* subsetted font, glyph_indices and CIDs are identical, though */
2623 if ( cff->top_font.font_dict.cid_registry != 0xFFFFU &&
2626 /* don't handle CID 0 (.notdef) which is directly mapped to GID 0 */
2627 if ( glyph_index != 0 )
2629 glyph_index = cff_charset_cid_to_gindex( &cff->charset,
2631 if ( glyph_index == 0 )
2632 return FT_THROW( Invalid_Argument );
2635 else if ( glyph_index >= cff->num_glyphs )
2636 return FT_THROW( Invalid_Argument );
2638 if ( load_flags & FT_LOAD_NO_RECURSE )
2639 load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
2641 glyph->x_scale = 0x10000L;
2642 glyph->y_scale = 0x10000L;
2645 glyph->x_scale = size->root.metrics.x_scale;
2646 glyph->y_scale = size->root.metrics.y_scale;
2649 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
2651 /* try to load embedded bitmap if any */
2653 /* XXX: The convention should be emphasized in */
2654 /* the documents because it can be confusing. */
2657 CFF_Face cff_face = (CFF_Face)size->root.face;
2658 SFNT_Service sfnt = (SFNT_Service)cff_face->sfnt;
2659 FT_Stream stream = cff_face->root.stream;
2662 if ( size->strike_index != 0xFFFFFFFFUL &&
2664 ( load_flags & FT_LOAD_NO_BITMAP ) == 0 )
2666 TT_SBit_MetricsRec metrics;
2669 error = sfnt->load_sbit_image( face,
2674 &glyph->root.bitmap,
2679 FT_Bool has_vertical_info;
2684 glyph->root.outline.n_points = 0;
2685 glyph->root.outline.n_contours = 0;
2687 glyph->root.metrics.width = (FT_Pos)metrics.width << 6;
2688 glyph->root.metrics.height = (FT_Pos)metrics.height << 6;
2690 glyph->root.metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6;
2691 glyph->root.metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6;
2692 glyph->root.metrics.horiAdvance = (FT_Pos)metrics.horiAdvance << 6;
2694 glyph->root.metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6;
2695 glyph->root.metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6;
2696 glyph->root.metrics.vertAdvance = (FT_Pos)metrics.vertAdvance << 6;
2698 glyph->root.format = FT_GLYPH_FORMAT_BITMAP;
2700 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
2702 glyph->root.bitmap_left = metrics.vertBearingX;
2703 glyph->root.bitmap_top = metrics.vertBearingY;
2707 glyph->root.bitmap_left = metrics.horiBearingX;
2708 glyph->root.bitmap_top = metrics.horiBearingY;
2711 /* compute linear advance widths */
2713 (void)( (SFNT_Service)face->sfnt )->get_metrics( face, 0,
2717 glyph->root.linearHoriAdvance = advance;
2719 has_vertical_info = FT_BOOL(
2720 face->vertical_info &&
2721 face->vertical.number_Of_VMetrics > 0 );
2723 /* get the vertical metrics from the vtmx table if we have one */
2724 if ( has_vertical_info )
2726 (void)( (SFNT_Service)face->sfnt )->get_metrics( face, 1,
2730 glyph->root.linearVertAdvance = advance;
2734 /* make up vertical ones */
2735 if ( face->os2.version != 0xFFFFU )
2736 glyph->root.linearVertAdvance = (FT_Pos)
2737 ( face->os2.sTypoAscender - face->os2.sTypoDescender );
2739 glyph->root.linearVertAdvance = (FT_Pos)
2740 ( face->horizontal.Ascender - face->horizontal.Descender );
2748 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
2750 /* return immediately if we only want the embedded bitmaps */
2751 if ( load_flags & FT_LOAD_SBITS_ONLY )
2752 return FT_THROW( Invalid_Argument );
2754 /* if we have a CID subfont, use its matrix (which has already */
2755 /* been multiplied with the root matrix) */
2757 /* this scaling is only relevant if the PS hinter isn't active */
2758 if ( cff->num_subfonts )
2760 FT_ULong top_upm, sub_upm;
2761 FT_Byte fd_index = cff_fd_select_get( &cff->fd_select,
2765 if ( fd_index >= cff->num_subfonts )
2766 fd_index = (FT_Byte)( cff->num_subfonts - 1 );
2768 top_upm = cff->top_font.font_dict.units_per_em;
2769 sub_upm = cff->subfonts[fd_index]->font_dict.units_per_em;
2772 font_matrix = cff->subfonts[fd_index]->font_dict.font_matrix;
2773 font_offset = cff->subfonts[fd_index]->font_dict.font_offset;
2775 if ( top_upm != sub_upm )
2777 glyph->x_scale = FT_MulDiv( glyph->x_scale, top_upm, sub_upm );
2778 glyph->y_scale = FT_MulDiv( glyph->y_scale, top_upm, sub_upm );
2780 force_scaling = TRUE;
2785 font_matrix = cff->top_font.font_dict.font_matrix;
2786 font_offset = cff->top_font.font_dict.font_offset;
2789 glyph->root.outline.n_points = 0;
2790 glyph->root.outline.n_contours = 0;
2792 /* top-level code ensures that FT_LOAD_NO_HINTING is set */
2793 /* if FT_LOAD_NO_SCALE is active */
2794 hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_HINTING ) == 0 );
2795 scaled = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 );
2797 glyph->hint = hinting;
2798 glyph->scaled = scaled;
2799 glyph->root.format = FT_GLYPH_FORMAT_OUTLINE; /* by default */
2802 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
2803 CFF_Driver driver = (CFF_Driver)FT_FACE_DRIVER( face );
2807 FT_Byte* charstring;
2808 FT_ULong charstring_len;
2811 cff_decoder_init( &decoder, face, size, glyph, hinting,
2812 FT_LOAD_TARGET_MODE( load_flags ) );
2814 if ( load_flags & FT_LOAD_ADVANCE_ONLY )
2815 decoder.width_only = TRUE;
2817 decoder.builder.no_recurse =
2818 (FT_Bool)( load_flags & FT_LOAD_NO_RECURSE );
2820 /* now load the unscaled outline */
2821 error = cff_get_glyph_data( face, glyph_index,
2822 &charstring, &charstring_len );
2824 goto Glyph_Build_Finished;
2826 error = cff_decoder_prepare( &decoder, size, glyph_index );
2828 goto Glyph_Build_Finished;
2830 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
2831 /* choose which CFF renderer to use */
2832 if ( driver->hinting_engine == FT_CFF_HINTING_FREETYPE )
2833 error = cff_decoder_parse_charstrings( &decoder,
2839 error = cf2_decoder_parse_charstrings( &decoder,
2843 /* Adobe's engine uses 16.16 numbers everywhere; */
2844 /* as a consequence, glyphs larger than 2000ppem get rejected */
2845 if ( FT_ERR_EQ( error, Glyph_Too_Big ) )
2847 /* this time, we retry unhinted and scale up the glyph later on */
2848 /* (the engine uses and sets the hardcoded value 0x10000 / 64 = */
2849 /* 0x400 for both `x_scale' and `y_scale' in this case) */
2851 force_scaling = TRUE;
2852 glyph->hint = hinting;
2854 error = cf2_decoder_parse_charstrings( &decoder,
2860 cff_free_glyph_data( face, &charstring, charstring_len );
2863 goto Glyph_Build_Finished;
2865 #ifdef FT_CONFIG_OPTION_INCREMENTAL
2866 /* Control data and length may not be available for incremental */
2868 if ( face->root.internal->incremental_interface )
2870 glyph->root.control_data = 0;
2871 glyph->root.control_len = 0;
2874 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
2876 /* We set control_data and control_len if charstrings is loaded. */
2877 /* See how charstring loads at cff_index_access_element() in */
2880 CFF_Index csindex = &cff->charstrings_index;
2883 if ( csindex->offsets )
2885 glyph->root.control_data = csindex->bytes +
2886 csindex->offsets[glyph_index] - 1;
2887 glyph->root.control_len = charstring_len;
2891 Glyph_Build_Finished:
2892 /* save new glyph tables, if no error */
2894 cff_builder_done( &decoder.builder );
2895 /* XXX: anything to do for broken glyph entry? */
2898 #ifdef FT_CONFIG_OPTION_INCREMENTAL
2900 /* Incremental fonts can optionally override the metrics. */
2902 face->root.internal->incremental_interface &&
2903 face->root.internal->incremental_interface->funcs->get_glyph_metrics )
2905 FT_Incremental_MetricsRec metrics;
2908 metrics.bearing_x = decoder.builder.left_bearing.x;
2909 metrics.bearing_y = 0;
2910 metrics.advance = decoder.builder.advance.x;
2911 metrics.advance_v = decoder.builder.advance.y;
2913 error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
2914 face->root.internal->incremental_interface->object,
2915 glyph_index, FALSE, &metrics );
2917 decoder.builder.left_bearing.x = metrics.bearing_x;
2918 decoder.builder.advance.x = metrics.advance;
2919 decoder.builder.advance.y = metrics.advance_v;
2922 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
2926 /* Now, set the metrics -- this is rather simple, as */
2927 /* the left side bearing is the xMin, and the top side */
2928 /* bearing the yMax. */
2930 /* For composite glyphs, return only left side bearing and */
2931 /* advance width. */
2932 if ( load_flags & FT_LOAD_NO_RECURSE )
2934 FT_Slot_Internal internal = glyph->root.internal;
2937 glyph->root.metrics.horiBearingX = decoder.builder.left_bearing.x;
2938 glyph->root.metrics.horiAdvance = decoder.glyph_width;
2939 internal->glyph_matrix = font_matrix;
2940 internal->glyph_delta = font_offset;
2941 internal->glyph_transformed = 1;
2946 FT_Glyph_Metrics* metrics = &glyph->root.metrics;
2948 FT_Bool has_vertical_info;
2951 /* copy the _unscaled_ advance width */
2952 metrics->horiAdvance = decoder.glyph_width;
2953 glyph->root.linearHoriAdvance = decoder.glyph_width;
2954 glyph->root.internal->glyph_transformed = 0;
2956 has_vertical_info = FT_BOOL( face->vertical_info &&
2957 face->vertical.number_Of_VMetrics > 0 );
2959 /* get the vertical metrics from the vtmx table if we have one */
2960 if ( has_vertical_info )
2962 FT_Short vertBearingY = 0;
2963 FT_UShort vertAdvance = 0;
2966 (void)( (SFNT_Service)face->sfnt )->get_metrics( face, 1,
2970 metrics->vertBearingY = vertBearingY;
2971 metrics->vertAdvance = vertAdvance;
2975 /* make up vertical ones */
2976 if ( face->os2.version != 0xFFFFU )
2977 metrics->vertAdvance = (FT_Pos)( face->os2.sTypoAscender -
2978 face->os2.sTypoDescender );
2980 metrics->vertAdvance = (FT_Pos)( face->horizontal.Ascender -
2981 face->horizontal.Descender );
2984 glyph->root.linearVertAdvance = metrics->vertAdvance;
2986 glyph->root.format = FT_GLYPH_FORMAT_OUTLINE;
2988 glyph->root.outline.flags = 0;
2989 if ( size && size->root.metrics.y_ppem < 24 )
2990 glyph->root.outline.flags |= FT_OUTLINE_HIGH_PRECISION;
2992 glyph->root.outline.flags |= FT_OUTLINE_REVERSE_FILL;
2994 if ( !( font_matrix.xx == 0x10000L &&
2995 font_matrix.yy == 0x10000L &&
2996 font_matrix.xy == 0 &&
2997 font_matrix.yx == 0 ) )
2998 FT_Outline_Transform( &glyph->root.outline, &font_matrix );
3000 if ( !( font_offset.x == 0 &&
3001 font_offset.y == 0 ) )
3002 FT_Outline_Translate( &glyph->root.outline,
3003 font_offset.x, font_offset.y );
3005 advance.x = metrics->horiAdvance;
3007 FT_Vector_Transform( &advance, &font_matrix );
3008 metrics->horiAdvance = advance.x + font_offset.x;
3011 advance.y = metrics->vertAdvance;
3012 FT_Vector_Transform( &advance, &font_matrix );
3013 metrics->vertAdvance = advance.y + font_offset.y;
3015 if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 || force_scaling )
3017 /* scale the outline and the metrics */
3019 FT_Outline* cur = &glyph->root.outline;
3020 FT_Vector* vec = cur->points;
3021 FT_Fixed x_scale = glyph->x_scale;
3022 FT_Fixed y_scale = glyph->y_scale;
3025 /* First of all, scale the points */
3026 if ( !hinting || !decoder.builder.hints_funcs )
3027 for ( n = cur->n_points; n > 0; n--, vec++ )
3029 vec->x = FT_MulFix( vec->x, x_scale );
3030 vec->y = FT_MulFix( vec->y, y_scale );
3033 /* Then scale the metrics */
3034 metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale );
3035 metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale );
3038 /* compute the other metrics */
3039 FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
3041 metrics->width = cbox.xMax - cbox.xMin;
3042 metrics->height = cbox.yMax - cbox.yMin;
3044 metrics->horiBearingX = cbox.xMin;
3045 metrics->horiBearingY = cbox.yMax;
3047 if ( has_vertical_info )
3048 metrics->vertBearingX = metrics->horiBearingX -
3049 metrics->horiAdvance / 2;
3052 if ( load_flags & FT_LOAD_VERTICAL_LAYOUT )
3053 ft_synthesize_vertical_metrics( metrics,
3054 metrics->vertAdvance );