1
/***************************************************************************/
5
/* OpenType Glyph Loader (body). */
7
/* Copyright 1996-2001, 2002 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_CALC_H
22
#include FT_INTERNAL_STREAM_H
23
#include FT_INTERNAL_SFNT_H
25
#include FT_TRUETYPE_TAGS_H
26
#include FT_INTERNAL_POSTSCRIPT_HINTS_H
35
/*************************************************************************/
37
/* The macro FT_COMPONENT is used in trace mode. It is an implicit */
38
/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
39
/* messages during execution. */
42
#define FT_COMPONENT trace_cffgload
45
typedef enum CFF_Operator_
79
cff_op_dotsection, /* deprecated, acts as no-op */
119
#define CFF_COUNT_CHECK_WIDTH 0x80
120
#define CFF_COUNT_EXACT 0x40
121
#define CFF_COUNT_CLEAR_STACK 0x20
124
static const FT_Byte cff_argument_counts[] =
128
2 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT, /* rmoveto */
129
1 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT,
130
1 | CFF_COUNT_CHECK_WIDTH | CFF_COUNT_EXACT,
132
0 | CFF_COUNT_CLEAR_STACK, /* rlineto */
133
0 | CFF_COUNT_CLEAR_STACK,
134
0 | CFF_COUNT_CLEAR_STACK,
136
0 | CFF_COUNT_CLEAR_STACK, /* rrcurveto */
137
0 | CFF_COUNT_CLEAR_STACK,
138
0 | CFF_COUNT_CLEAR_STACK,
139
0 | CFF_COUNT_CLEAR_STACK,
140
0 | CFF_COUNT_CLEAR_STACK,
141
0 | CFF_COUNT_CLEAR_STACK,
142
0 | CFF_COUNT_CLEAR_STACK,
149
0 | CFF_COUNT_CHECK_WIDTH, /* endchar */
151
2 | CFF_COUNT_CHECK_WIDTH, /* hstem */
152
2 | CFF_COUNT_CHECK_WIDTH,
153
2 | CFF_COUNT_CHECK_WIDTH,
154
2 | CFF_COUNT_CHECK_WIDTH,
156
0 | CFF_COUNT_CHECK_WIDTH, /* hintmask */
157
0 | CFF_COUNT_CHECK_WIDTH, /* cntrmask */
194
/*************************************************************************/
195
/*************************************************************************/
196
/*************************************************************************/
197
/********** *********/
198
/********** *********/
199
/********** GENERIC CHARSTRING PARSING *********/
200
/********** *********/
201
/********** *********/
202
/*************************************************************************/
203
/*************************************************************************/
204
/*************************************************************************/
207
/*************************************************************************/
210
/* CFF_Builder_Init */
213
/* Initializes a given glyph builder. */
216
/* builder :: A pointer to the glyph builder to initialize. */
219
/* face :: The current face object. */
221
/* size :: The current size object. */
223
/* glyph :: The current glyph object. */
226
CFF_Builder_Init( CFF_Builder* builder,
232
builder->path_begun = 0;
233
builder->load_points = 1;
235
builder->face = face;
236
builder->glyph = glyph;
237
builder->memory = face->root.memory;
241
FT_GlyphLoader loader = glyph->root.internal->loader;
244
builder->loader = loader;
245
builder->base = &loader->base.outline;
246
builder->current = &loader->current.outline;
247
FT_GlyphLoader_Rewind( loader );
249
builder->hints_globals = 0;
250
builder->hints_funcs = 0;
252
if ( hinting && size )
254
builder->hints_globals = size->internal;
255
builder->hints_funcs = glyph->root.internal->glyph_hints;
261
builder->scale_x = size->metrics.x_scale;
262
builder->scale_y = size->metrics.y_scale;
268
builder->left_bearing.x = 0;
269
builder->left_bearing.y = 0;
270
builder->advance.x = 0;
271
builder->advance.y = 0;
275
/*************************************************************************/
278
/* CFF_Builder_Done */
281
/* Finalizes a given glyph builder. Its contents can still be used */
282
/* after the call, but the function saves important information */
283
/* within the corresponding glyph slot. */
286
/* builder :: A pointer to the glyph builder to finalize. */
289
CFF_Builder_Done( CFF_Builder* builder )
291
CFF_GlyphSlot glyph = builder->glyph;
295
glyph->root.outline = *builder->base;
299
/*************************************************************************/
302
/* cff_compute_bias */
305
/* Computes the bias value in dependence of the number of glyph */
309
/* num_subrs :: The number of glyph subroutines. */
312
/* The bias value. */
314
cff_compute_bias( FT_UInt num_subrs )
319
if ( num_subrs < 1240 )
321
else if ( num_subrs < 33900U )
330
/*************************************************************************/
333
/* CFF_Init_Decoder */
336
/* Initializes a given glyph decoder. */
339
/* decoder :: A pointer to the glyph builder to initialize. */
342
/* face :: The current face object. */
344
/* size :: The current size object. */
346
/* slot :: The current glyph object. */
349
CFF_Init_Decoder( CFF_Decoder* decoder,
355
CFF_Font cff = (CFF_Font)face->extra.data;
358
/* clear everything */
359
FT_MEM_SET( decoder, 0, sizeof ( *decoder ) );
361
/* initialize builder */
362
CFF_Builder_Init( &decoder->builder, face, size, slot, hinting );
364
/* initialize Type2 decoder */
365
decoder->num_globals = cff->num_global_subrs;
366
decoder->globals = cff->global_subrs;
367
decoder->globals_bias = cff_compute_bias( decoder->num_globals );
371
/* this function is used to select the locals subrs array */
373
CFF_Prepare_Decoder( CFF_Decoder* decoder,
374
FT_UInt glyph_index )
376
CFF_Font cff = (CFF_Font)decoder->builder.face->extra.data;
377
CFF_SubFont sub = &cff->top_font;
380
/* manage CID fonts */
381
if ( cff->num_subfonts >= 1 )
383
FT_Byte fd_index = CFF_Get_FD( &cff->fd_select, glyph_index );
386
sub = cff->subfonts[fd_index];
389
decoder->num_locals = sub->num_local_subrs;
390
decoder->locals = sub->local_subrs;
391
decoder->locals_bias = cff_compute_bias( decoder->num_locals );
393
decoder->glyph_width = sub->private_dict.default_width;
394
decoder->nominal_width = sub->private_dict.nominal_width;
398
/* check that there is enough room for `count' more points */
400
check_points( CFF_Builder* builder,
403
return FT_GlyphLoader_CheckPoints( builder->loader, count, 0 );
407
/* add a new point, do not check space */
409
add_point( CFF_Builder* builder,
414
FT_Outline* outline = builder->current;
417
if ( builder->load_points )
419
FT_Vector* point = outline->points + outline->n_points;
420
FT_Byte* control = (FT_Byte*)outline->tags + outline->n_points;
425
*control = (FT_Byte)( flag ? FT_Curve_Tag_On : FT_Curve_Tag_Cubic );
427
builder->last = *point;
434
/* check space for a new on-curve point, then add it */
436
add_point1( CFF_Builder* builder,
443
error = check_points( builder, 1 );
445
add_point( builder, x, y, 1 );
451
/* check room for a new contour, then add it */
453
add_contour( CFF_Builder* builder )
455
FT_Outline* outline = builder->current;
459
if ( !builder->load_points )
461
outline->n_contours++;
465
error = FT_GlyphLoader_CheckPoints( builder->loader, 0, 1 );
468
if ( outline->n_contours > 0 )
469
outline->contours[outline->n_contours - 1] =
470
(short)( outline->n_points - 1 );
472
outline->n_contours++;
479
/* if a path was begun, add its first on-curve point */
481
start_point( CFF_Builder* builder,
488
/* test whether we are building a new contour */
489
if ( !builder->path_begun )
491
builder->path_begun = 1;
492
error = add_contour( builder );
494
error = add_point1( builder, x, y );
501
/* close the current contour */
503
close_contour( CFF_Builder* builder )
505
FT_Outline* outline = builder->current;
508
/* XXXX: We must not include the last point in the path if it */
509
/* is located on the first point. */
510
if ( outline->n_points > 1 )
513
FT_Vector* p1 = outline->points + first;
514
FT_Vector* p2 = outline->points + outline->n_points - 1;
515
FT_Byte* control = (FT_Byte*)outline->tags + outline->n_points - 1;
518
if ( outline->n_contours > 1 )
520
first = outline->contours[outline->n_contours - 2] + 1;
521
p1 = outline->points + first;
524
/* `delete' last point only if it coincides with the first */
525
/* point and if it is not a control point (which can happen). */
526
if ( p1->x == p2->x && p1->y == p2->y )
527
if ( *control == FT_Curve_Tag_On )
531
if ( outline->n_contours > 0 )
532
outline->contours[outline->n_contours - 1] =
533
(short)( outline->n_points - 1 );
538
cff_lookup_glyph_by_stdcharcode( CFF_Font cff,
545
/* check range of standard char code */
546
if ( charcode < 0 || charcode > 255 )
549
/* Get code to SID mapping from `cff_standard_encoding'. */
550
glyph_sid = CFF_Get_Standard_Encoding( (FT_UInt)charcode );
552
for ( n = 0; n < cff->num_glyphs; n++ )
554
if ( cff->charset.sids[n] == glyph_sid )
563
cff_operator_seac( CFF_Decoder* decoder,
570
FT_Int bchar_index, achar_index, n_base_points;
571
FT_Outline* base = decoder->builder.base;
572
TT_Face face = decoder->builder.face;
573
CFF_Font cff = (CFF_Font)(face->extra.data);
574
FT_Vector left_bearing, advance;
576
FT_ULong charstring_len;
579
bchar_index = cff_lookup_glyph_by_stdcharcode( cff, bchar );
580
achar_index = cff_lookup_glyph_by_stdcharcode( cff, achar );
582
if ( bchar_index < 0 || achar_index < 0 )
584
FT_ERROR(( "cff_operator_seac:" ));
585
FT_ERROR(( " invalid seac character code arguments\n" ));
586
return CFF_Err_Syntax_Error;
589
/* If we are trying to load a composite glyph, do not load the */
590
/* accent character and return the array of subglyphs. */
591
if ( decoder->builder.no_recurse )
593
FT_GlyphSlot glyph = (FT_GlyphSlot)decoder->builder.glyph;
594
FT_GlyphLoader loader = glyph->internal->loader;
598
/* reallocate subglyph array if necessary */
599
error = FT_GlyphLoader_CheckSubGlyphs( loader, 2 );
603
subg = loader->current.subglyphs;
605
/* subglyph 0 = base character */
606
subg->index = bchar_index;
607
subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES |
608
FT_SUBGLYPH_FLAG_USE_MY_METRICS;
613
/* subglyph 1 = accent character */
614
subg->index = achar_index;
615
subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES;
619
/* set up remaining glyph fields */
620
glyph->num_subglyphs = 2;
621
glyph->subglyphs = loader->base.subglyphs;
622
glyph->format = ft_glyph_format_composite;
624
loader->current.num_subglyphs = 2;
627
/* First load `bchar' in builder */
628
error = CFF_Access_Element( &cff->charstrings_index, bchar_index,
629
&charstring, &charstring_len );
632
error = CFF_Parse_CharStrings( decoder, charstring, charstring_len );
637
CFF_Forget_Element( &cff->charstrings_index, &charstring );
640
n_base_points = base->n_points;
642
/* Save the left bearing and width of the base character */
643
/* as they will be erased by the next load. */
645
left_bearing = decoder->builder.left_bearing;
646
advance = decoder->builder.advance;
648
decoder->builder.left_bearing.x = 0;
649
decoder->builder.left_bearing.y = 0;
651
/* Now load `achar' on top of the base outline. */
652
error = CFF_Access_Element( &cff->charstrings_index, achar_index,
653
&charstring, &charstring_len );
656
error = CFF_Parse_CharStrings( decoder, charstring, charstring_len );
661
CFF_Forget_Element( &cff->charstrings_index, &charstring );
664
/* Restore the left side bearing and advance width */
665
/* of the base character. */
666
decoder->builder.left_bearing = left_bearing;
667
decoder->builder.advance = advance;
669
/* Finally, move the accent. */
670
if ( decoder->builder.load_points )
675
dummy.n_points = (short)( base->n_points - n_base_points );
676
dummy.points = base->points + n_base_points;
678
FT_Outline_Translate( &dummy, adx, ady );
686
/*************************************************************************/
689
/* CFF_Parse_CharStrings */
692
/* Parses a given Type 2 charstrings program. */
695
/* decoder :: The current Type 1 decoder. */
698
/* charstring_base :: The base of the charstring stream. */
700
/* charstring_len :: The length in bytes of the charstring stream. */
703
/* FreeType error code. 0 means success. */
705
FT_LOCAL_DEF( FT_Error )
706
CFF_Parse_CharStrings( CFF_Decoder* decoder,
707
FT_Byte* charstring_base,
708
FT_Int charstring_len )
711
CFF_Decoder_Zone* zone;
714
CFF_Builder* builder = &decoder->builder;
719
T2_Hints_Funcs hinter;
722
/* set default width */
723
decoder->num_hints = 0;
724
decoder->read_width = 1;
726
/* compute random seed from stack address of parameter */
727
seed = (FT_Fixed)(char*)&seed ^
728
(FT_Fixed)(char*)&decoder ^
729
(FT_Fixed)(char*)&charstring_base;
730
seed = ( seed ^ ( seed >> 10 ) ^ ( seed >> 20 ) ) & 0xFFFF;
734
/* initialize the decoder */
735
decoder->top = decoder->stack;
736
decoder->zone = decoder->zones;
737
zone = decoder->zones;
738
stack = decoder->top;
740
hinter = (T2_Hints_Funcs) builder->hints_funcs;
742
builder->path_begun = 0;
744
zone->base = charstring_base;
745
limit = zone->limit = charstring_base + charstring_len;
746
ip = zone->cursor = zone->base;
753
/* begin hints recording session, if any */
755
hinter->open( hinter->hints );
757
/* now, execute loop */
764
/********************************************************************/
766
/* Decode operator or operand */
769
if ( v >= 32 || v == 28 )
775
/* this is an operand, push it on the stack */
778
if ( ip + 1 >= limit )
780
val = (FT_Short)( ( (FT_Short)ip[0] << 8 ) | ip[1] );
784
val = (FT_Long)v - 139;
789
val = ( (FT_Long)v - 247 ) * 256 + *ip++ + 108;
795
val = -( (FT_Long)v - 251 ) * 256 - *ip++ - 108;
799
if ( ip + 3 >= limit )
801
val = ( (FT_Int32)ip[0] << 24 ) |
802
( (FT_Int32)ip[1] << 16 ) |
803
( (FT_Int32)ip[2] << 8 ) |
808
if ( decoder->top - stack >= CFF_MAX_OPERANDS )
812
*decoder->top++ = val;
814
#ifdef FT_DEBUG_LEVEL_TRACE
815
if ( !( val & 0xFFFF ) )
816
FT_TRACE4(( " %d", (FT_Int32)( val >> 16 ) ));
818
FT_TRACE4(( " %.2f", val / 65536.0 ));
824
FT_Fixed* args = decoder->top;
825
FT_Int num_args = args - decoder->stack;
853
op = cff_op_rrcurveto;
856
op = cff_op_callsubr;
870
op = cff_op_dotsection;
951
/* decrement ip for syntax error message */
966
op = cff_op_hintmask;
969
op = cff_op_cntrmask;
981
op = cff_op_rcurveline;
984
op = cff_op_rlinecurve;
987
op = cff_op_vvcurveto;
990
op = cff_op_hhcurveto;
993
op = cff_op_callgsubr;
996
op = cff_op_vhcurveto;
999
op = cff_op_hvcurveto;
1004
if ( op == cff_op_unknown )
1007
/* check arguments */
1008
req_args = cff_argument_counts[op];
1009
if ( req_args & CFF_COUNT_CHECK_WIDTH )
1013
if ( num_args > 0 && decoder->read_width )
1015
/* If `nominal_width' is non-zero, the number is really a */
1016
/* difference against `nominal_width'. Else, the number here */
1017
/* is truly a width, not a difference against `nominal_width'. */
1018
/* If the font does not set `nominal_width', then */
1019
/* `nominal_width' defaults to zero, and so we can set */
1020
/* `glyph_width' to `nominal_width' plus number on the stack */
1021
/* -- for either case. */
1023
FT_Int set_width_ok;
1028
case cff_op_hmoveto:
1029
case cff_op_vmoveto:
1030
set_width_ok = num_args & 2;
1035
case cff_op_hstemhm:
1036
case cff_op_vstemhm:
1037
case cff_op_rmoveto:
1038
set_width_ok = num_args & 1;
1041
case cff_op_endchar:
1042
/* If there is a width specified for endchar, we either have */
1043
/* 1 argument or 5 arguments. We like to argue. */
1044
set_width_ok = ( ( num_args == 5 ) || ( num_args == 1 ) );
1054
decoder->glyph_width = decoder->nominal_width +
1057
/* Consumed an argument. */
1063
decoder->read_width = 0;
1068
if ( num_args < req_args )
1069
goto Stack_Underflow;
1071
num_args -= req_args;
1077
case cff_op_hstemhm:
1078
case cff_op_vstemhm:
1079
/* the number of arguments is always even here */
1080
FT_TRACE4(( op == cff_op_hstem ? " hstem" :
1081
( op == cff_op_vstem ? " vstem" :
1082
( op == cff_op_hstemhm ? " hstemhm" : " vstemhm" ) ) ));
1085
hinter->stems( hinter->hints,
1086
( op == cff_op_hstem || op == cff_op_hstemhm ),
1090
decoder->num_hints += num_args / 2;
1094
case cff_op_hintmask:
1095
case cff_op_cntrmask:
1096
FT_TRACE4(( op == cff_op_hintmask ? " hintmask" : " cntrmask" ));
1098
/* implement vstem when needed -- */
1099
/* the specification doesn't say it, but this also works */
1100
/* with the 'cntrmask' operator */
1105
hinter->stems( hinter->hints,
1110
decoder->num_hints += num_args / 2;
1115
if ( op == cff_op_hintmask )
1116
hinter->hintmask( hinter->hints,
1117
builder->current->n_points,
1121
hinter->counter( hinter->hints,
1126
#ifdef FT_DEBUG_LEVEL_TRACE
1134
maskbyte < (FT_UInt)(( decoder->num_hints + 7 ) >> 3);
1136
FT_TRACE4(( "%02X", *ip ));
1139
ip += ( decoder->num_hints + 7 ) >> 3;
1146
case cff_op_rmoveto:
1147
FT_TRACE4(( " rmoveto" ));
1149
close_contour( builder );
1150
builder->path_begun = 0;
1156
case cff_op_vmoveto:
1157
FT_TRACE4(( " vmoveto" ));
1159
close_contour( builder );
1160
builder->path_begun = 0;
1165
case cff_op_hmoveto:
1166
FT_TRACE4(( " hmoveto" ));
1168
close_contour( builder );
1169
builder->path_begun = 0;
1174
case cff_op_rlineto:
1175
FT_TRACE4(( " rlineto" ));
1177
if ( start_point ( builder, x, y ) ||
1178
check_points( builder, num_args / 2 ) )
1181
if ( num_args < 2 || num_args & 1 )
1182
goto Stack_Underflow;
1185
while ( args < decoder->top )
1189
add_point( builder, x, y, 1 );
1195
case cff_op_hlineto:
1196
case cff_op_vlineto:
1198
FT_Int phase = ( op == cff_op_hlineto );
1201
FT_TRACE4(( op == cff_op_hlineto ? " hlineto"
1204
if ( start_point ( builder, x, y ) ||
1205
check_points( builder, num_args ) )
1209
while (args < decoder->top )
1216
if ( add_point1( builder, x, y ) )
1226
case cff_op_rrcurveto:
1227
FT_TRACE4(( " rrcurveto" ));
1229
/* check number of arguments; must be a multiple of 6 */
1230
if ( num_args % 6 != 0 )
1231
goto Stack_Underflow;
1233
if ( start_point ( builder, x, y ) ||
1234
check_points( builder, num_args / 2 ) )
1238
while ( args < decoder->top )
1242
add_point( builder, x, y, 0 );
1245
add_point( builder, x, y, 0 );
1248
add_point( builder, x, y, 1 );
1254
case cff_op_vvcurveto:
1255
FT_TRACE4(( " vvcurveto" ));
1257
if ( start_point ( builder, x, y ) )
1268
if ( num_args % 4 != 0 )
1269
goto Stack_Underflow;
1271
if ( check_points( builder, 3 * ( num_args / 4 ) ) )
1274
while ( args < decoder->top )
1277
add_point( builder, x, y, 0 );
1280
add_point( builder, x, y, 0 );
1282
add_point( builder, x, y, 1 );
1288
case cff_op_hhcurveto:
1289
FT_TRACE4(( " hhcurveto" ));
1291
if ( start_point ( builder, x, y ) )
1302
if ( num_args % 4 != 0 )
1303
goto Stack_Underflow;
1305
if ( check_points( builder, 3 * ( num_args / 4 ) ) )
1308
while ( args < decoder->top )
1311
add_point( builder, x, y, 0 );
1314
add_point( builder, x, y, 0 );
1316
add_point( builder, x, y, 1 );
1322
case cff_op_vhcurveto:
1323
case cff_op_hvcurveto:
1328
FT_TRACE4(( op == cff_op_vhcurveto ? " vhcurveto"
1331
if ( start_point ( builder, x, y ) )
1335
if (num_args < 4 || ( num_args % 4 ) > 1 )
1336
goto Stack_Underflow;
1338
if ( check_points( builder, ( num_args / 4 ) * 3 ) )
1339
goto Stack_Underflow;
1341
phase = ( op == cff_op_hvcurveto );
1343
while ( num_args >= 4 )
1349
add_point( builder, x, y, 0 );
1352
add_point( builder, x, y, 0 );
1354
if ( num_args == 1 )
1356
add_point( builder, x, y, 1 );
1361
add_point( builder, x, y, 0 );
1364
add_point( builder, x, y, 0 );
1366
if ( num_args == 1 )
1368
add_point( builder, x, y, 1 );
1377
case cff_op_rlinecurve:
1379
FT_Int num_lines = ( num_args - 6 ) / 2;
1382
FT_TRACE4(( " rlinecurve" ));
1384
if ( num_args < 8 || ( num_args - 6 ) & 1 )
1385
goto Stack_Underflow;
1387
if ( start_point( builder, x, y ) ||
1388
check_points( builder, num_lines + 3 ) )
1393
/* first, add the line segments */
1394
while ( num_lines > 0 )
1398
add_point( builder, x, y, 1 );
1403
/* then the curve */
1406
add_point( builder, x, y, 0 );
1409
add_point( builder, x, y, 0 );
1412
add_point( builder, x, y, 1 );
1417
case cff_op_rcurveline:
1419
FT_Int num_curves = ( num_args - 2 ) / 6;
1422
FT_TRACE4(( " rcurveline" ));
1424
if ( num_args < 8 || ( num_args - 2 ) % 6 )
1425
goto Stack_Underflow;
1427
if ( start_point ( builder, x, y ) ||
1428
check_points( builder, num_curves*3 + 2 ) )
1433
/* first, add the curves */
1434
while ( num_curves > 0 )
1438
add_point( builder, x, y, 0 );
1441
add_point( builder, x, y, 0 );
1444
add_point( builder, x, y, 1 );
1449
/* then the final line */
1452
add_point( builder, x, y, 1 );
1462
FT_TRACE4(( " hflex1" ));
1466
/* adding five more points; 4 control points, 1 on-curve point */
1467
/* make sure we have enough space for the start point if it */
1468
/* needs to be added.. */
1469
if ( start_point( builder, x, y ) ||
1470
check_points( builder, 6 ) )
1473
/* Record the starting point's y postion for later use */
1476
/* first control point */
1479
add_point( builder, x, y, 0 );
1481
/* second control point */
1484
add_point( builder, x, y, 0 );
1486
/* join point; on curve, with y-value the same as the last */
1487
/* control point's y-value */
1489
add_point( builder, x, y, 1 );
1491
/* third control point, with y-value the same as the join */
1492
/* point's y-value */
1494
add_point( builder, x, y, 0 );
1496
/* fourth control point */
1499
add_point( builder, x, y, 0 );
1501
/* ending point, with y-value the same as the start */
1504
add_point( builder, x, y, 1 );
1515
FT_TRACE4(( " hflex" ));
1519
/* adding six more points; 4 control points, 2 on-curve points */
1520
if ( start_point( builder, x, y ) ||
1521
check_points ( builder, 6 ) )
1524
/* record the starting point's y-position for later use */
1527
/* first control point */
1529
add_point( builder, x, y, 0 );
1531
/* second control point */
1534
add_point( builder, x, y, 0 );
1536
/* join point; on curve, with y-value the same as the last */
1537
/* control point's y-value */
1539
add_point( builder, x, y, 1 );
1541
/* third control point, with y-value the same as the join */
1542
/* point's y-value */
1544
add_point( builder, x, y, 0 );
1546
/* fourth control point */
1549
add_point( builder, x, y, 0 );
1551
/* ending point, with y-value the same as the start point's */
1552
/* y-value -- we don't add this point, though */
1554
add_point( builder, x, y, 1 );
1562
FT_Pos start_x, start_y; /* record start x, y values for alter */
1564
FT_Int dx = 0, dy = 0; /* used in horizontal/vertical */
1565
/* algorithm below */
1566
FT_Int horizontal, count;
1569
FT_TRACE4(( " flex1" ));
1571
/* adding six more points; 4 control points, 2 on-curve points */
1572
if ( start_point( builder, x, y ) ||
1573
check_points( builder, 6 ) )
1576
/* record the starting point's x, y postion for later use */
1580
/* XXX: figure out whether this is supposed to be a horizontal */
1581
/* or vertical flex; the Type 2 specification is vague... */
1585
/* grab up to the last argument */
1586
for ( count = 5; count > 0; count-- )
1596
if ( dx < 0 ) dx = -dx;
1597
if ( dy < 0 ) dy = -dy;
1599
/* strange test, but here it is... */
1600
horizontal = ( dx > dy );
1602
for ( count = 5; count > 0; count-- )
1606
add_point( builder, x, y, (FT_Bool)( count == 3 ) );
1610
/* is last operand an x- or y-delta? */
1622
add_point( builder, x, y, 1 );
1633
FT_TRACE4(( " flex" ));
1635
if ( start_point( builder, x, y ) ||
1636
check_points( builder, 6 ) )
1640
for ( count = 6; count > 0; count-- )
1644
add_point( builder, x, y,
1645
(FT_Bool)( count == 3 || count == 0 ) );
1653
case cff_op_endchar:
1654
FT_TRACE4(( " endchar" ));
1656
/* We are going to emulate the seac operator. */
1657
if ( num_args == 4 )
1659
error = cff_operator_seac( decoder,
1660
args[0] >> 16, args[1] >> 16,
1661
args[2] >> 16, args[3] >> 16 );
1668
close_contour( builder );
1670
/* close hints recording session */
1673
if (hinter->close( hinter->hints, builder->current->n_points ) )
1676
/* apply hints to the loaded glyph outline now */
1677
hinter->apply( hinter->hints,
1679
(PSH_Globals)builder->hints_globals );
1682
/* add current outline to the glyph slot */
1683
FT_GlyphLoader_Add( builder->loader );
1686
FT_TRACE4(( "\n\n" ));
1690
FT_TRACE4(( " abs" ));
1698
FT_TRACE4(( " add" ));
1705
FT_TRACE4(( " sub" ));
1712
FT_TRACE4(( " div" ));
1714
args[0] = FT_DivFix( args[0], args[1] );
1719
FT_TRACE4(( " neg" ));
1730
FT_TRACE4(( " rand" ));
1733
if ( Rand >= 0x8000 )
1737
seed = FT_MulFix( seed, 0x10000L - seed );
1745
FT_TRACE4(( " mul" ));
1747
args[0] = FT_MulFix( args[0], args[1] );
1752
FT_TRACE4(( " sqrt" ));
1757
FT_Fixed root = args[0];
1763
new_root = ( root + FT_DivFix( args[0], root ) + 1 ) >> 1;
1764
if ( new_root == root || count <= 0 )
1777
FT_TRACE4(( " drop" ));
1786
FT_TRACE4(( " exch" ));
1797
FT_Int idx = args[0] >> 16;
1800
FT_TRACE4(( " index" ));
1804
else if ( idx > num_args - 2 )
1806
args[0] = args[-( idx + 1 )];
1813
FT_Int count = (FT_Int)( args[0] >> 16 );
1814
FT_Int idx = (FT_Int)( args[1] >> 16 );
1817
FT_TRACE4(( " roll" ));
1824
goto Stack_Underflow;
1830
FT_Fixed tmp = args[count - 1];
1834
for ( i = count - 2; i >= 0; i-- )
1835
args[i + 1] = args[i];
1844
FT_Fixed tmp = args[0];
1848
for ( i = 0; i < count - 1; i++ )
1849
args[i] = args[i + 1];
1850
args[count - 1] = tmp;
1859
FT_TRACE4(( " dup" ));
1867
FT_Fixed val = args[0];
1868
FT_Int idx = (FT_Int)( args[1] >> 16 );
1871
FT_TRACE4(( " put" ));
1873
if ( idx >= 0 && idx < decoder->len_buildchar )
1874
decoder->buildchar[idx] = val;
1880
FT_Int idx = (FT_Int)( args[0] >> 16 );
1884
FT_TRACE4(( " get" ));
1886
if ( idx >= 0 && idx < decoder->len_buildchar )
1887
val = decoder->buildchar[idx];
1895
FT_TRACE4(( " store "));
1900
FT_TRACE4(( " load" ));
1904
case cff_op_dotsection:
1905
/* this operator is deprecated and ignored by the parser */
1906
FT_TRACE4(( " dotsection" ));
1911
FT_Fixed cond = args[0] && args[1];
1914
FT_TRACE4(( " and" ));
1916
args[0] = cond ? 0x10000L : 0;
1923
FT_Fixed cond = args[0] || args[1];
1926
FT_TRACE4(( " or" ));
1928
args[0] = cond ? 0x10000L : 0;
1935
FT_Fixed cond = !args[0];
1938
FT_TRACE4(( " eq" ));
1940
args[0] = cond ? 0x10000L : 0;
1947
FT_Fixed cond = (args[2] <= args[3]);
1950
FT_TRACE4(( " ifelse" ));
1958
case cff_op_callsubr:
1960
FT_UInt idx = (FT_UInt)( ( args[0] >> 16 ) +
1961
decoder->locals_bias );
1964
FT_TRACE4(( " callsubr(%d)", idx ));
1966
if ( idx >= decoder->num_locals )
1968
FT_ERROR(( "CFF_Parse_CharStrings:" ));
1969
FT_ERROR(( " invalid local subr index\n" ));
1973
if ( zone - decoder->zones >= CFF_MAX_SUBRS_CALLS )
1975
FT_ERROR(( "CFF_Parse_CharStrings: too many nested subrs\n" ));
1979
zone->cursor = ip; /* save current instruction pointer */
1982
zone->base = decoder->locals[idx];
1983
zone->limit = decoder->locals[idx + 1];
1984
zone->cursor = zone->base;
1988
FT_ERROR(( "CFF_Parse_CharStrings: invoking empty subrs!\n" ));
1992
decoder->zone = zone;
1994
limit = zone->limit;
1998
case cff_op_callgsubr:
2000
FT_UInt idx = (FT_UInt)( ( args[0] >> 16 ) +
2001
decoder->globals_bias );
2004
FT_TRACE4(( " callgsubr(%d)", idx ));
2006
if ( idx >= decoder->num_globals )
2008
FT_ERROR(( "CFF_Parse_CharStrings:" ));
2009
FT_ERROR(( " invalid global subr index\n" ));
2013
if ( zone - decoder->zones >= CFF_MAX_SUBRS_CALLS )
2015
FT_ERROR(( "CFF_Parse_CharStrings: too many nested subrs\n" ));
2019
zone->cursor = ip; /* save current instruction pointer */
2022
zone->base = decoder->globals[idx];
2023
zone->limit = decoder->globals[idx + 1];
2024
zone->cursor = zone->base;
2028
FT_ERROR(( "CFF_Parse_CharStrings: invoking empty subrs!\n" ));
2032
decoder->zone = zone;
2034
limit = zone->limit;
2039
FT_TRACE4(( " return" ));
2041
if ( decoder->zone <= decoder->zones )
2043
FT_ERROR(( "CFF_Parse_CharStrings: unexpected return\n" ));
2048
zone = decoder->zone;
2050
limit = zone->limit;
2055
FT_ERROR(( "Unimplemented opcode: %d", ip[-1] ));
2058
FT_ERROR(( " %d", ip[0] ));
2061
return CFF_Err_Unimplemented_Feature;
2064
decoder->top = args;
2066
} /* general operator processing */
2068
} /* while ip < limit */
2070
FT_TRACE4(( "..end..\n\n" ));
2075
FT_TRACE4(( "CFF_Parse_CharStrings: syntax error!" ));
2076
return CFF_Err_Invalid_File_Format;
2079
FT_TRACE4(( "CFF_Parse_CharStrings: stack underflow!" ));
2080
return CFF_Err_Too_Few_Arguments;
2083
FT_TRACE4(( "CFF_Parse_CharStrings: stack overflow!" ));
2084
return CFF_Err_Stack_Overflow;
2087
return builder->error;
2091
/*************************************************************************/
2092
/*************************************************************************/
2093
/*************************************************************************/
2094
/********** *********/
2095
/********** *********/
2096
/********** COMPUTE THE MAXIMUM ADVANCE WIDTH *********/
2097
/********** *********/
2098
/********** The following code is in charge of computing *********/
2099
/********** the maximum advance width of the font. It *********/
2100
/********** quickly processes each glyph charstring to *********/
2101
/********** extract the value from either a `sbw' or `seac' *********/
2102
/********** operator. *********/
2103
/********** *********/
2104
/*************************************************************************/
2105
/*************************************************************************/
2106
/*************************************************************************/
2109
#if 0 /* unused until we support pure CFF fonts */
2112
FT_LOCAL_DEF( FT_Error )
2113
CFF_Compute_Max_Advance( TT_Face face,
2114
FT_Int* max_advance )
2117
CFF_Decoder decoder;
2119
CFF_Font cff = (CFF_Font)face->other;
2124
/* Initialize load decoder */
2125
CFF_Init_Decoder( &decoder, face, 0, 0, 0 );
2127
decoder.builder.metrics_only = 1;
2128
decoder.builder.load_points = 0;
2130
/* For each glyph, parse the glyph charstring and extract */
2131
/* the advance width. */
2132
for ( glyph_index = 0; glyph_index < face->root.num_glyphs;
2135
FT_Byte* charstring;
2136
FT_ULong charstring_len;
2139
/* now get load the unscaled outline */
2140
error = CFF_Access_Element( &cff->charstrings_index, glyph_index,
2141
&charstring, &charstring_len );
2144
CFF_Prepare_Decoder( &decoder, glyph_index );
2145
error = CFF_Parse_CharStrings( &decoder, charstring, charstring_len );
2147
CFF_Forget_Element( &cff->charstrings_index, &charstring );
2150
/* ignore the error if one has occurred -- skip to next glyph */
2154
*max_advance = decoder.builder.advance.x;
2163
/*************************************************************************/
2164
/*************************************************************************/
2165
/*************************************************************************/
2166
/********** *********/
2167
/********** *********/
2168
/********** UNHINTED GLYPH LOADER *********/
2169
/********** *********/
2170
/********** The following code is in charge of loading a *********/
2171
/********** single outline. It completely ignores hinting *********/
2172
/********** and is used when FT_LOAD_NO_HINTING is set. *********/
2173
/********** *********/
2174
/*************************************************************************/
2175
/*************************************************************************/
2176
/*************************************************************************/
2179
FT_LOCAL_DEF( FT_Error )
2180
CFF_Load_Glyph( CFF_GlyphSlot glyph,
2186
CFF_Decoder decoder;
2187
TT_Face face = (TT_Face)glyph->root.face;
2189
CFF_Font cff = (CFF_Font)face->extra.data;
2191
FT_Matrix font_matrix;
2192
FT_Vector font_offset;
2195
if ( load_flags & FT_LOAD_NO_RECURSE )
2196
load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
2198
glyph->x_scale = 0x10000L;
2199
glyph->y_scale = 0x10000L;
2202
glyph->x_scale = size->metrics.x_scale;
2203
glyph->y_scale = size->metrics.y_scale;
2206
glyph->root.outline.n_points = 0;
2207
glyph->root.outline.n_contours = 0;
2209
hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 &&
2210
( load_flags & FT_LOAD_NO_HINTING ) == 0 );
2212
glyph->root.format = ft_glyph_format_outline; /* by default */
2215
FT_Byte* charstring;
2216
FT_ULong charstring_len;
2219
CFF_Init_Decoder( &decoder, face, size, glyph, hinting );
2221
decoder.builder.no_recurse =
2222
(FT_Bool)( ( load_flags & FT_LOAD_NO_RECURSE ) != 0 );
2224
/* now load the unscaled outline */
2225
error = CFF_Access_Element( &cff->charstrings_index, glyph_index,
2226
&charstring, &charstring_len );
2229
CFF_IndexRec csindex = cff->charstrings_index;
2232
CFF_Prepare_Decoder( &decoder, glyph_index );
2233
error = CFF_Parse_CharStrings( &decoder, charstring, charstring_len );
2235
CFF_Forget_Element( &cff->charstrings_index, &charstring );
2237
/* We set control_data and control_len if charstrings is loaded. */
2238
/* See how charstring loads at CFF_Access_Element() in cffload.c. */
2240
glyph->root.control_data =
2241
csindex.bytes + csindex.offsets[glyph_index] - 1;
2242
glyph->root.control_len =
2246
/* save new glyph tables */
2247
CFF_Builder_Done( &decoder.builder );
2250
font_matrix = cff->top_font.font_dict.font_matrix;
2251
font_offset = cff->top_font.font_dict.font_offset;
2253
/* Now, set the metrics -- this is rather simple, as */
2254
/* the left side bearing is the xMin, and the top side */
2255
/* bearing the yMax. */
2258
/* For composite glyphs, return only left side bearing and */
2259
/* advance width. */
2260
if ( load_flags & FT_LOAD_NO_RECURSE )
2262
FT_Slot_Internal internal = glyph->root.internal;
2265
glyph->root.metrics.horiBearingX = decoder.builder.left_bearing.x;
2266
glyph->root.metrics.horiAdvance = decoder.glyph_width;
2267
internal->glyph_matrix = font_matrix;
2268
internal->glyph_delta = font_offset;
2269
internal->glyph_transformed = 1;
2274
FT_Glyph_Metrics* metrics = &glyph->root.metrics;
2277
/* copy the _unscaled_ advance width */
2278
metrics->horiAdvance = decoder.glyph_width;
2279
glyph->root.linearHoriAdvance = decoder.glyph_width;
2280
glyph->root.internal->glyph_transformed = 0;
2282
/* make up vertical metrics */
2283
metrics->vertBearingX = 0;
2284
metrics->vertBearingY = 0;
2285
metrics->vertAdvance = 0;
2287
glyph->root.linearVertAdvance = 0;
2289
glyph->root.format = ft_glyph_format_outline;
2291
glyph->root.outline.flags = 0;
2292
if ( size && size->metrics.y_ppem < 24 )
2293
glyph->root.outline.flags |= ft_outline_high_precision;
2295
glyph->root.outline.flags |= ft_outline_reverse_fill;
2297
/* apply the font matrix */
2298
FT_Outline_Transform( &glyph->root.outline, &font_matrix );
2300
FT_Outline_Translate( &glyph->root.outline,
2304
if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 )
2306
/* scale the outline and the metrics */
2308
FT_Outline* cur = &glyph->root.outline;
2309
FT_Vector* vec = cur->points;
2310
FT_Fixed x_scale = glyph->x_scale;
2311
FT_Fixed y_scale = glyph->y_scale;
2314
/* First of all, scale the points */
2316
for ( n = cur->n_points; n > 0; n--, vec++ )
2318
vec->x = FT_MulFix( vec->x, x_scale );
2319
vec->y = FT_MulFix( vec->y, y_scale );
2322
FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
2324
/* Then scale the metrics */
2325
metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale );
2326
metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale );
2328
metrics->vertBearingX = FT_MulFix( metrics->vertBearingX, x_scale );
2329
metrics->vertBearingY = FT_MulFix( metrics->vertBearingY, y_scale );
2333
metrics->horiAdvance = ( metrics->horiAdvance + 32 ) & -64;
2334
metrics->vertAdvance = ( metrics->vertAdvance + 32 ) & -64;
2336
metrics->vertBearingX = ( metrics->vertBearingX + 32 ) & -64;
2337
metrics->vertBearingY = ( metrics->vertBearingY + 32 ) & -64;
2341
/* compute the other metrics */
2342
FT_Outline_Get_CBox( &glyph->root.outline, &cbox );
2344
/* grid fit the bounding box if necessary */
2349
cbox.xMax = ( cbox.xMax + 63 ) & -64;
2350
cbox.yMax = ( cbox.yMax + 63 ) & -64;
2353
metrics->width = cbox.xMax - cbox.xMin;
2354
metrics->height = cbox.yMax - cbox.yMin;
2356
metrics->horiBearingX = cbox.xMin;
2357
metrics->horiBearingY = cbox.yMax;