Disabling auto-refresh of game list by default, as it is causing bugs sometimes
[open-ps2-loader.git] / thirdparty / freetype-2.3.12 / src / type1 / t1load.c
blobd867e942c95f300efdf234df3ad984e3ea2ca4b5
1 /***************************************************************************/
2 /* */
3 /* t1load.c */
4 /* */
5 /* Type 1 font loader (body). */
6 /* */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
9 /* */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
15 /* */
16 /***************************************************************************/
19 /*************************************************************************/
20 /* */
21 /* This is the new and improved Type 1 data loader for FreeType 2. The */
22 /* old loader has several problems: it is slow, complex, difficult to */
23 /* maintain, and contains incredible hacks to make it accept some */
24 /* ill-formed Type 1 fonts without hiccup-ing. Moreover, about 5% of */
25 /* the Type 1 fonts on my machine still aren't loaded correctly by it. */
26 /* */
27 /* This version is much simpler, much faster and also easier to read and */
28 /* maintain by a great order of magnitude. The idea behind it is to */
29 /* _not_ try to read the Type 1 token stream with a state machine (i.e. */
30 /* a Postscript-like interpreter) but rather to perform simple pattern */
31 /* matching. */
32 /* */
33 /* Indeed, nearly all data definitions follow a simple pattern like */
34 /* */
35 /* ... /Field <data> ... */
36 /* */
37 /* where <data> can be a number, a boolean, a string, or an array of */
38 /* numbers. There are a few exceptions, namely the encoding, font name, */
39 /* charstrings, and subrs; they are handled with a special pattern */
40 /* matching routine. */
41 /* */
42 /* All other common cases are handled very simply. The matching rules */
43 /* are defined in the file `t1tokens.h' through the use of several */
44 /* macros calls PARSE_XXX. This file is included twice here; the first */
45 /* time to generate parsing callback functions, the second time to */
46 /* generate a table of keywords (with pointers to the associated */
47 /* callback functions). */
48 /* */
49 /* The function `parse_dict' simply scans *linearly* a given dictionary */
50 /* (either the top-level or private one) and calls the appropriate */
51 /* callback when it encounters an immediate keyword. */
52 /* */
53 /* This is by far the fastest way one can find to parse and read all */
54 /* data. */
55 /* */
56 /* This led to tremendous code size reduction. Note that later, the */
57 /* glyph loader will also be _greatly_ simplified, and the automatic */
58 /* hinter will replace the clumsy `t1hinter'. */
59 /* */
60 /*************************************************************************/
63 #include <ft2build.h>
64 #include FT_INTERNAL_DEBUG_H
65 #include FT_CONFIG_CONFIG_H
66 #include FT_MULTIPLE_MASTERS_H
67 #include FT_INTERNAL_TYPE1_TYPES_H
68 #include FT_INTERNAL_CALC_H
70 #include "t1load.h"
71 #include "t1errors.h"
74 /*************************************************************************/
75 /* */
76 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
77 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
78 /* messages during execution. */
79 /* */
80 #undef FT_COMPONENT
81 #define FT_COMPONENT trace_t1load
84 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
87 /*************************************************************************/
88 /*************************************************************************/
89 /***** *****/
90 /***** MULTIPLE MASTERS SUPPORT *****/
91 /***** *****/
92 /*************************************************************************/
93 /*************************************************************************/
95 static FT_Error
96 t1_allocate_blend( T1_Face face,
97 FT_UInt num_designs,
98 FT_UInt num_axis )
100 PS_Blend blend;
101 FT_Memory memory = face->root.memory;
102 FT_Error error = T1_Err_Ok;
105 blend = face->blend;
106 if ( !blend )
108 if ( FT_NEW( blend ) )
109 goto Exit;
111 blend->num_default_design_vector = 0;
113 face->blend = blend;
116 /* allocate design data if needed */
117 if ( num_designs > 0 )
119 if ( blend->num_designs == 0 )
121 FT_UInt nn;
124 /* allocate the blend `private' and `font_info' dictionaries */
125 if ( FT_NEW_ARRAY( blend->font_infos[1], num_designs ) ||
126 FT_NEW_ARRAY( blend->privates[1], num_designs ) ||
127 FT_NEW_ARRAY( blend->bboxes[1], num_designs ) ||
128 FT_NEW_ARRAY( blend->weight_vector, num_designs * 2 ) )
129 goto Exit;
131 blend->default_weight_vector = blend->weight_vector + num_designs;
133 blend->font_infos[0] = &face->type1.font_info;
134 blend->privates [0] = &face->type1.private_dict;
135 blend->bboxes [0] = &face->type1.font_bbox;
137 for ( nn = 2; nn <= num_designs; nn++ )
139 blend->privates[nn] = blend->privates [nn - 1] + 1;
140 blend->font_infos[nn] = blend->font_infos[nn - 1] + 1;
141 blend->bboxes[nn] = blend->bboxes [nn - 1] + 1;
144 blend->num_designs = num_designs;
146 else if ( blend->num_designs != num_designs )
147 goto Fail;
150 /* allocate axis data if needed */
151 if ( num_axis > 0 )
153 if ( blend->num_axis != 0 && blend->num_axis != num_axis )
154 goto Fail;
156 blend->num_axis = num_axis;
159 /* allocate the blend design pos table if needed */
160 num_designs = blend->num_designs;
161 num_axis = blend->num_axis;
162 if ( num_designs && num_axis && blend->design_pos[0] == 0 )
164 FT_UInt n;
167 if ( FT_NEW_ARRAY( blend->design_pos[0], num_designs * num_axis ) )
168 goto Exit;
170 for ( n = 1; n < num_designs; n++ )
171 blend->design_pos[n] = blend->design_pos[0] + num_axis * n;
174 Exit:
175 return error;
177 Fail:
178 error = T1_Err_Invalid_File_Format;
179 goto Exit;
183 FT_LOCAL_DEF( FT_Error )
184 T1_Get_Multi_Master( T1_Face face,
185 FT_Multi_Master* master )
187 PS_Blend blend = face->blend;
188 FT_UInt n;
189 FT_Error error;
192 error = T1_Err_Invalid_Argument;
194 if ( blend )
196 master->num_axis = blend->num_axis;
197 master->num_designs = blend->num_designs;
199 for ( n = 0; n < blend->num_axis; n++ )
201 FT_MM_Axis* axis = master->axis + n;
202 PS_DesignMap map = blend->design_map + n;
205 axis->name = blend->axis_names[n];
206 axis->minimum = map->design_points[0];
207 axis->maximum = map->design_points[map->num_points - 1];
210 error = T1_Err_Ok;
213 return error;
217 /*************************************************************************/
218 /* */
219 /* Given a normalized (blend) coordinate, figure out the design */
220 /* coordinate appropriate for that value. */
221 /* */
222 FT_LOCAL_DEF( FT_Fixed )
223 mm_axis_unmap( PS_DesignMap axismap,
224 FT_Fixed ncv )
226 int j;
229 if ( ncv <= axismap->blend_points[0] )
230 return INT_TO_FIXED( axismap->design_points[0] );
232 for ( j = 1; j < axismap->num_points; ++j )
234 if ( ncv <= axismap->blend_points[j] )
236 FT_Fixed t = FT_MulDiv( ncv - axismap->blend_points[j - 1],
237 0x10000L,
238 axismap->blend_points[j] -
239 axismap->blend_points[j - 1] );
241 return INT_TO_FIXED( axismap->design_points[j - 1] ) +
242 FT_MulDiv( t,
243 axismap->design_points[j] -
244 axismap->design_points[j - 1],
245 1L );
249 return INT_TO_FIXED( axismap->design_points[axismap->num_points - 1] );
253 /*************************************************************************/
254 /* */
255 /* Given a vector of weights, one for each design, figure out the */
256 /* normalized axis coordinates which gave rise to those weights. */
257 /* */
258 FT_LOCAL_DEF( void )
259 mm_weights_unmap( FT_Fixed* weights,
260 FT_Fixed* axiscoords,
261 FT_UInt axis_count )
263 FT_ASSERT( axis_count <= T1_MAX_MM_AXIS );
265 if ( axis_count == 1 )
266 axiscoords[0] = weights[1];
268 else if ( axis_count == 2 )
270 axiscoords[0] = weights[3] + weights[1];
271 axiscoords[1] = weights[3] + weights[2];
274 else if ( axis_count == 3 )
276 axiscoords[0] = weights[7] + weights[5] + weights[3] + weights[1];
277 axiscoords[1] = weights[7] + weights[6] + weights[3] + weights[2];
278 axiscoords[2] = weights[7] + weights[6] + weights[5] + weights[4];
281 else
283 axiscoords[0] = weights[15] + weights[13] + weights[11] + weights[9] +
284 weights[7] + weights[5] + weights[3] + weights[1];
285 axiscoords[1] = weights[15] + weights[14] + weights[11] + weights[10] +
286 weights[7] + weights[6] + weights[3] + weights[2];
287 axiscoords[2] = weights[15] + weights[14] + weights[13] + weights[12] +
288 weights[7] + weights[6] + weights[5] + weights[4];
289 axiscoords[3] = weights[15] + weights[14] + weights[13] + weights[12] +
290 weights[11] + weights[10] + weights[9] + weights[8];
295 /*************************************************************************/
296 /* */
297 /* Just a wrapper around T1_Get_Multi_Master to support the different */
298 /* arguments needed by the GX var distortable fonts. */
299 /* */
300 FT_LOCAL_DEF( FT_Error )
301 T1_Get_MM_Var( T1_Face face,
302 FT_MM_Var* *master )
304 FT_Memory memory = face->root.memory;
305 FT_MM_Var *mmvar;
306 FT_Multi_Master mmaster;
307 FT_Error error;
308 FT_UInt i;
309 FT_Fixed axiscoords[T1_MAX_MM_AXIS];
310 PS_Blend blend = face->blend;
313 error = T1_Get_Multi_Master( face, &mmaster );
314 if ( error )
315 goto Exit;
316 if ( FT_ALLOC( mmvar,
317 sizeof ( FT_MM_Var ) +
318 mmaster.num_axis * sizeof ( FT_Var_Axis ) ) )
319 goto Exit;
321 mmvar->num_axis = mmaster.num_axis;
322 mmvar->num_designs = mmaster.num_designs;
323 mmvar->num_namedstyles = (FT_UInt)-1; /* Does not apply */
324 mmvar->axis = (FT_Var_Axis*)&mmvar[1];
325 /* Point to axes after MM_Var struct */
326 mmvar->namedstyle = NULL;
328 for ( i = 0 ; i < mmaster.num_axis; ++i )
330 mmvar->axis[i].name = mmaster.axis[i].name;
331 mmvar->axis[i].minimum = INT_TO_FIXED( mmaster.axis[i].minimum);
332 mmvar->axis[i].maximum = INT_TO_FIXED( mmaster.axis[i].maximum);
333 mmvar->axis[i].def = ( mmvar->axis[i].minimum +
334 mmvar->axis[i].maximum ) / 2;
335 /* Does not apply. But this value is in range */
336 mmvar->axis[i].strid = (FT_UInt)-1; /* Does not apply */
337 mmvar->axis[i].tag = (FT_ULong)-1; /* Does not apply */
339 if ( ft_strcmp( mmvar->axis[i].name, "Weight" ) == 0 )
340 mmvar->axis[i].tag = FT_MAKE_TAG( 'w', 'g', 'h', 't' );
341 else if ( ft_strcmp( mmvar->axis[i].name, "Width" ) == 0 )
342 mmvar->axis[i].tag = FT_MAKE_TAG( 'w', 'd', 't', 'h' );
343 else if ( ft_strcmp( mmvar->axis[i].name, "OpticalSize" ) == 0 )
344 mmvar->axis[i].tag = FT_MAKE_TAG( 'o', 'p', 's', 'z' );
347 if ( blend->num_designs == ( 1U << blend->num_axis ) )
349 mm_weights_unmap( blend->default_weight_vector,
350 axiscoords,
351 blend->num_axis );
353 for ( i = 0; i < mmaster.num_axis; ++i )
354 mmvar->axis[i].def = mm_axis_unmap( &blend->design_map[i],
355 axiscoords[i] );
358 *master = mmvar;
360 Exit:
361 return error;
365 FT_LOCAL_DEF( FT_Error )
366 T1_Set_MM_Blend( T1_Face face,
367 FT_UInt num_coords,
368 FT_Fixed* coords )
370 PS_Blend blend = face->blend;
371 FT_Error error;
372 FT_UInt n, m;
375 error = T1_Err_Invalid_Argument;
377 if ( blend && blend->num_axis == num_coords )
379 /* recompute the weight vector from the blend coordinates */
380 error = T1_Err_Ok;
382 for ( n = 0; n < blend->num_designs; n++ )
384 FT_Fixed result = 0x10000L; /* 1.0 fixed */
387 for ( m = 0; m < blend->num_axis; m++ )
389 FT_Fixed factor;
392 /* get current blend axis position */
393 factor = coords[m];
394 if ( factor < 0 ) factor = 0;
395 if ( factor > 0x10000L ) factor = 0x10000L;
397 if ( ( n & ( 1 << m ) ) == 0 )
398 factor = 0x10000L - factor;
400 result = FT_MulFix( result, factor );
402 blend->weight_vector[n] = result;
405 error = T1_Err_Ok;
408 return error;
412 FT_LOCAL_DEF( FT_Error )
413 T1_Set_MM_Design( T1_Face face,
414 FT_UInt num_coords,
415 FT_Long* coords )
417 PS_Blend blend = face->blend;
418 FT_Error error;
419 FT_UInt n, p;
422 error = T1_Err_Invalid_Argument;
423 if ( blend && blend->num_axis == num_coords )
425 /* compute the blend coordinates through the blend design map */
426 FT_Fixed final_blends[T1_MAX_MM_DESIGNS];
429 for ( n = 0; n < blend->num_axis; n++ )
431 FT_Long design = coords[n];
432 FT_Fixed the_blend;
433 PS_DesignMap map = blend->design_map + n;
434 FT_Long* designs = map->design_points;
435 FT_Fixed* blends = map->blend_points;
436 FT_Int before = -1, after = -1;
439 for ( p = 0; p < (FT_UInt)map->num_points; p++ )
441 FT_Long p_design = designs[p];
444 /* exact match? */
445 if ( design == p_design )
447 the_blend = blends[p];
448 goto Found;
451 if ( design < p_design )
453 after = p;
454 break;
457 before = p;
460 /* now interpolate if necessary */
461 if ( before < 0 )
462 the_blend = blends[0];
464 else if ( after < 0 )
465 the_blend = blends[map->num_points - 1];
467 else
468 the_blend = FT_MulDiv( design - designs[before],
469 blends [after] - blends [before],
470 designs[after] - designs[before] );
472 Found:
473 final_blends[n] = the_blend;
476 error = T1_Set_MM_Blend( face, num_coords, final_blends );
479 return error;
483 /*************************************************************************/
484 /* */
485 /* Just a wrapper around T1_Set_MM_Design to support the different */
486 /* arguments needed by the GX var distortable fonts. */
487 /* */
488 FT_LOCAL_DEF( FT_Error )
489 T1_Set_Var_Design( T1_Face face,
490 FT_UInt num_coords,
491 FT_Fixed* coords )
493 FT_Long lcoords[4]; /* maximum axis count is 4 */
494 FT_UInt i;
495 FT_Error error;
498 error = T1_Err_Invalid_Argument;
499 if ( num_coords <= 4 && num_coords > 0 )
501 for ( i = 0; i < num_coords; ++i )
502 lcoords[i] = FIXED_TO_INT( coords[i] );
503 error = T1_Set_MM_Design( face, num_coords, lcoords );
506 return error;
510 FT_LOCAL_DEF( void )
511 T1_Done_Blend( T1_Face face )
513 FT_Memory memory = face->root.memory;
514 PS_Blend blend = face->blend;
517 if ( blend )
519 FT_UInt num_designs = blend->num_designs;
520 FT_UInt num_axis = blend->num_axis;
521 FT_UInt n;
524 /* release design pos table */
525 FT_FREE( blend->design_pos[0] );
526 for ( n = 1; n < num_designs; n++ )
527 blend->design_pos[n] = 0;
529 /* release blend `private' and `font info' dictionaries */
530 FT_FREE( blend->privates[1] );
531 FT_FREE( blend->font_infos[1] );
532 FT_FREE( blend->bboxes[1] );
534 for ( n = 0; n < num_designs; n++ )
536 blend->privates [n] = 0;
537 blend->font_infos[n] = 0;
538 blend->bboxes [n] = 0;
541 /* release weight vectors */
542 FT_FREE( blend->weight_vector );
543 blend->default_weight_vector = 0;
545 /* release axis names */
546 for ( n = 0; n < num_axis; n++ )
547 FT_FREE( blend->axis_names[n] );
549 /* release design map */
550 for ( n = 0; n < num_axis; n++ )
552 PS_DesignMap dmap = blend->design_map + n;
555 FT_FREE( dmap->design_points );
556 dmap->num_points = 0;
559 FT_FREE( face->blend );
564 static void
565 parse_blend_axis_types( T1_Face face,
566 T1_Loader loader )
568 T1_TokenRec axis_tokens[T1_MAX_MM_AXIS];
569 FT_Int n, num_axis;
570 FT_Error error = T1_Err_Ok;
571 PS_Blend blend;
572 FT_Memory memory;
575 /* take an array of objects */
576 T1_ToTokenArray( &loader->parser, axis_tokens,
577 T1_MAX_MM_AXIS, &num_axis );
578 if ( num_axis < 0 )
580 error = T1_Err_Ignore;
581 goto Exit;
583 if ( num_axis == 0 || num_axis > T1_MAX_MM_AXIS )
585 FT_ERROR(( "parse_blend_axis_types: incorrect number of axes: %d\n",
586 num_axis ));
587 error = T1_Err_Invalid_File_Format;
588 goto Exit;
591 /* allocate blend if necessary */
592 error = t1_allocate_blend( face, 0, (FT_UInt)num_axis );
593 if ( error )
594 goto Exit;
596 blend = face->blend;
597 memory = face->root.memory;
599 /* each token is an immediate containing the name of the axis */
600 for ( n = 0; n < num_axis; n++ )
602 T1_Token token = axis_tokens + n;
603 FT_Byte* name;
604 FT_PtrDist len;
607 /* skip first slash, if any */
608 if ( token->start[0] == '/' )
609 token->start++;
611 len = token->limit - token->start;
612 if ( len == 0 )
614 error = T1_Err_Invalid_File_Format;
615 goto Exit;
618 if ( FT_ALLOC( blend->axis_names[n], len + 1 ) )
619 goto Exit;
621 name = (FT_Byte*)blend->axis_names[n];
622 FT_MEM_COPY( name, token->start, len );
623 name[len] = 0;
626 Exit:
627 loader->parser.root.error = error;
631 static void
632 parse_blend_design_positions( T1_Face face,
633 T1_Loader loader )
635 T1_TokenRec design_tokens[T1_MAX_MM_DESIGNS];
636 FT_Int num_designs;
637 FT_Int num_axis;
638 T1_Parser parser = &loader->parser;
640 FT_Error error = T1_Err_Ok;
641 PS_Blend blend;
644 /* get the array of design tokens -- compute number of designs */
645 T1_ToTokenArray( parser, design_tokens,
646 T1_MAX_MM_DESIGNS, &num_designs );
647 if ( num_designs < 0 )
649 error = T1_Err_Ignore;
650 goto Exit;
652 if ( num_designs == 0 || num_designs > T1_MAX_MM_DESIGNS )
654 FT_ERROR(( "parse_blend_design_positions:"
655 " incorrect number of designs: %d\n",
656 num_designs ));
657 error = T1_Err_Invalid_File_Format;
658 goto Exit;
662 FT_Byte* old_cursor = parser->root.cursor;
663 FT_Byte* old_limit = parser->root.limit;
664 FT_Int n;
667 blend = face->blend;
668 num_axis = 0; /* make compiler happy */
670 for ( n = 0; n < num_designs; n++ )
672 T1_TokenRec axis_tokens[T1_MAX_MM_AXIS];
673 T1_Token token;
674 FT_Int axis, n_axis;
677 /* read axis/coordinates tokens */
678 token = design_tokens + n;
679 parser->root.cursor = token->start;
680 parser->root.limit = token->limit;
681 T1_ToTokenArray( parser, axis_tokens, T1_MAX_MM_AXIS, &n_axis );
683 if ( n == 0 )
685 if ( n_axis <= 0 || n_axis > T1_MAX_MM_AXIS )
687 FT_ERROR(( "parse_blend_design_positions:"
688 " invalid number of axes: %d\n",
689 n_axis ));
690 error = T1_Err_Invalid_File_Format;
691 goto Exit;
694 num_axis = n_axis;
695 error = t1_allocate_blend( face, num_designs, num_axis );
696 if ( error )
697 goto Exit;
698 blend = face->blend;
700 else if ( n_axis != num_axis )
702 FT_ERROR(( "parse_blend_design_positions: incorrect table\n" ));
703 error = T1_Err_Invalid_File_Format;
704 goto Exit;
707 /* now read each axis token into the design position */
708 for ( axis = 0; axis < n_axis; axis++ )
710 T1_Token token2 = axis_tokens + axis;
713 parser->root.cursor = token2->start;
714 parser->root.limit = token2->limit;
715 blend->design_pos[n][axis] = T1_ToFixed( parser, 0 );
719 loader->parser.root.cursor = old_cursor;
720 loader->parser.root.limit = old_limit;
723 Exit:
724 loader->parser.root.error = error;
728 static void
729 parse_blend_design_map( T1_Face face,
730 T1_Loader loader )
732 FT_Error error = T1_Err_Ok;
733 T1_Parser parser = &loader->parser;
734 PS_Blend blend;
735 T1_TokenRec axis_tokens[T1_MAX_MM_AXIS];
736 FT_Int n, num_axis;
737 FT_Byte* old_cursor;
738 FT_Byte* old_limit;
739 FT_Memory memory = face->root.memory;
742 T1_ToTokenArray( parser, axis_tokens,
743 T1_MAX_MM_AXIS, &num_axis );
744 if ( num_axis < 0 )
746 error = T1_Err_Ignore;
747 goto Exit;
749 if ( num_axis == 0 || num_axis > T1_MAX_MM_AXIS )
751 FT_ERROR(( "parse_blend_design_map: incorrect number of axes: %d\n",
752 num_axis ));
753 error = T1_Err_Invalid_File_Format;
754 goto Exit;
757 old_cursor = parser->root.cursor;
758 old_limit = parser->root.limit;
760 error = t1_allocate_blend( face, 0, num_axis );
761 if ( error )
762 goto Exit;
763 blend = face->blend;
765 /* now read each axis design map */
766 for ( n = 0; n < num_axis; n++ )
768 PS_DesignMap map = blend->design_map + n;
769 T1_Token axis_token;
770 T1_TokenRec point_tokens[T1_MAX_MM_MAP_POINTS];
771 FT_Int p, num_points;
774 axis_token = axis_tokens + n;
776 parser->root.cursor = axis_token->start;
777 parser->root.limit = axis_token->limit;
778 T1_ToTokenArray( parser, point_tokens,
779 T1_MAX_MM_MAP_POINTS, &num_points );
781 if ( num_points <= 0 || num_points > T1_MAX_MM_MAP_POINTS )
783 FT_ERROR(( "parse_blend_design_map: incorrect table\n" ));
784 error = T1_Err_Invalid_File_Format;
785 goto Exit;
788 /* allocate design map data */
789 if ( FT_NEW_ARRAY( map->design_points, num_points * 2 ) )
790 goto Exit;
791 map->blend_points = map->design_points + num_points;
792 map->num_points = (FT_Byte)num_points;
794 for ( p = 0; p < num_points; p++ )
796 T1_Token point_token;
799 point_token = point_tokens + p;
801 /* don't include delimiting brackets */
802 parser->root.cursor = point_token->start + 1;
803 parser->root.limit = point_token->limit - 1;
805 map->design_points[p] = T1_ToInt( parser );
806 map->blend_points [p] = T1_ToFixed( parser, 0 );
810 parser->root.cursor = old_cursor;
811 parser->root.limit = old_limit;
813 Exit:
814 parser->root.error = error;
818 static void
819 parse_weight_vector( T1_Face face,
820 T1_Loader loader )
822 T1_TokenRec design_tokens[T1_MAX_MM_DESIGNS];
823 FT_Int num_designs;
824 FT_Error error = T1_Err_Ok;
825 T1_Parser parser = &loader->parser;
826 PS_Blend blend = face->blend;
827 T1_Token token;
828 FT_Int n;
829 FT_Byte* old_cursor;
830 FT_Byte* old_limit;
833 T1_ToTokenArray( parser, design_tokens,
834 T1_MAX_MM_DESIGNS, &num_designs );
835 if ( num_designs < 0 )
837 error = T1_Err_Ignore;
838 goto Exit;
840 if ( num_designs == 0 || num_designs > T1_MAX_MM_DESIGNS )
842 FT_ERROR(( "parse_weight_vector:"
843 " incorrect number of designs: %d\n",
844 num_designs ));
845 error = T1_Err_Invalid_File_Format;
846 goto Exit;
849 if ( !blend || !blend->num_designs )
851 error = t1_allocate_blend( face, num_designs, 0 );
852 if ( error )
853 goto Exit;
854 blend = face->blend;
856 else if ( blend->num_designs != (FT_UInt)num_designs )
858 FT_ERROR(( "parse_weight_vector:"
859 " /BlendDesignPosition and /WeightVector have\n"
861 " different number of elements\n" ));
862 error = T1_Err_Invalid_File_Format;
863 goto Exit;
866 old_cursor = parser->root.cursor;
867 old_limit = parser->root.limit;
869 for ( n = 0; n < num_designs; n++ )
871 token = design_tokens + n;
872 parser->root.cursor = token->start;
873 parser->root.limit = token->limit;
875 blend->default_weight_vector[n] =
876 blend->weight_vector[n] = T1_ToFixed( parser, 0 );
879 parser->root.cursor = old_cursor;
880 parser->root.limit = old_limit;
882 Exit:
883 parser->root.error = error;
887 /* e.g., /BuildCharArray [0 0 0 0 0 0 0 0] def */
888 /* we're only interested in the number of array elements */
889 static void
890 parse_buildchar( T1_Face face,
891 T1_Loader loader )
893 face->len_buildchar = T1_ToFixedArray( &loader->parser, 0, NULL, 0 );
895 return;
899 #endif /* T1_CONFIG_OPTION_NO_MM_SUPPORT */
904 /*************************************************************************/
905 /*************************************************************************/
906 /***** *****/
907 /***** TYPE 1 SYMBOL PARSING *****/
908 /***** *****/
909 /*************************************************************************/
910 /*************************************************************************/
912 static FT_Error
913 t1_load_keyword( T1_Face face,
914 T1_Loader loader,
915 const T1_Field field )
917 FT_Error error;
918 void* dummy_object;
919 void** objects;
920 FT_UInt max_objects;
921 PS_Blend blend = face->blend;
924 /* if the keyword has a dedicated callback, call it */
925 if ( field->type == T1_FIELD_TYPE_CALLBACK )
927 field->reader( (FT_Face)face, loader );
928 error = loader->parser.root.error;
929 goto Exit;
932 /* now, the keyword is either a simple field, or a table of fields; */
933 /* we are now going to take care of it */
934 switch ( field->location )
936 case T1_FIELD_LOCATION_FONT_INFO:
937 dummy_object = &face->type1.font_info;
938 objects = &dummy_object;
939 max_objects = 0;
941 if ( blend )
943 objects = (void**)blend->font_infos;
944 max_objects = blend->num_designs;
946 break;
948 case T1_FIELD_LOCATION_FONT_EXTRA:
949 dummy_object = &face->type1.font_extra;
950 objects = &dummy_object;
951 max_objects = 0;
952 break;
954 case T1_FIELD_LOCATION_PRIVATE:
955 dummy_object = &face->type1.private_dict;
956 objects = &dummy_object;
957 max_objects = 0;
959 if ( blend )
961 objects = (void**)blend->privates;
962 max_objects = blend->num_designs;
964 break;
966 case T1_FIELD_LOCATION_BBOX:
967 dummy_object = &face->type1.font_bbox;
968 objects = &dummy_object;
969 max_objects = 0;
971 if ( blend )
973 objects = (void**)blend->bboxes;
974 max_objects = blend->num_designs;
976 break;
978 case T1_FIELD_LOCATION_LOADER:
979 dummy_object = loader;
980 objects = &dummy_object;
981 max_objects = 0;
982 break;
984 case T1_FIELD_LOCATION_FACE:
985 dummy_object = face;
986 objects = &dummy_object;
987 max_objects = 0;
988 break;
990 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
991 case T1_FIELD_LOCATION_BLEND:
992 dummy_object = face->blend;
993 objects = &dummy_object;
994 max_objects = 0;
995 break;
996 #endif
998 default:
999 dummy_object = &face->type1;
1000 objects = &dummy_object;
1001 max_objects = 0;
1004 if ( field->type == T1_FIELD_TYPE_INTEGER_ARRAY ||
1005 field->type == T1_FIELD_TYPE_FIXED_ARRAY )
1006 error = T1_Load_Field_Table( &loader->parser, field,
1007 objects, max_objects, 0 );
1008 else
1009 error = T1_Load_Field( &loader->parser, field,
1010 objects, max_objects, 0 );
1012 Exit:
1013 return error;
1017 static void
1018 parse_private( T1_Face face,
1019 T1_Loader loader )
1021 FT_UNUSED( face );
1023 loader->keywords_encountered |= T1_PRIVATE;
1027 static int
1028 read_binary_data( T1_Parser parser,
1029 FT_Long* size,
1030 FT_Byte** base )
1032 FT_Byte* cur;
1033 FT_Byte* limit = parser->root.limit;
1036 /* the binary data has one of the following formats */
1037 /* */
1038 /* `size' [white*] RD white ....... ND */
1039 /* `size' [white*] -| white ....... |- */
1040 /* */
1042 T1_Skip_Spaces( parser );
1044 cur = parser->root.cursor;
1046 if ( cur < limit && ft_isdigit( *cur ) )
1048 *size = T1_ToInt( parser );
1050 T1_Skip_PS_Token( parser ); /* `RD' or `-|' or something else */
1052 /* there is only one whitespace char after the */
1053 /* `RD' or `-|' token */
1054 *base = parser->root.cursor + 1;
1056 parser->root.cursor += *size + 1;
1057 return !parser->root.error;
1060 FT_ERROR(( "read_binary_data: invalid size field\n" ));
1061 parser->root.error = T1_Err_Invalid_File_Format;
1062 return 0;
1066 /* We now define the routines to handle the `/Encoding', `/Subrs', */
1067 /* and `/CharStrings' dictionaries. */
1069 static void
1070 parse_font_matrix( T1_Face face,
1071 T1_Loader loader )
1073 T1_Parser parser = &loader->parser;
1074 FT_Matrix* matrix = &face->type1.font_matrix;
1075 FT_Vector* offset = &face->type1.font_offset;
1076 FT_Face root = (FT_Face)&face->root;
1077 FT_Fixed temp[6];
1078 FT_Fixed temp_scale;
1079 FT_Int result;
1082 result = T1_ToFixedArray( parser, 6, temp, 3 );
1084 if ( result < 0 )
1086 parser->root.error = T1_Err_Invalid_File_Format;
1087 return;
1090 temp_scale = FT_ABS( temp[3] );
1092 if ( temp_scale == 0 )
1094 FT_ERROR(( "parse_font_matrix: invalid font matrix\n" ));
1095 parser->root.error = T1_Err_Invalid_File_Format;
1096 return;
1099 /* Set Units per EM based on FontMatrix values. We set the value to */
1100 /* 1000 / temp_scale, because temp_scale was already multiplied by */
1101 /* 1000 (in t1_tofixed, from psobjs.c). */
1103 root->units_per_EM = (FT_UShort)( FT_DivFix( 1000 * 0x10000L,
1104 temp_scale ) >> 16 );
1106 /* we need to scale the values by 1.0/temp_scale */
1107 if ( temp_scale != 0x10000L )
1109 temp[0] = FT_DivFix( temp[0], temp_scale );
1110 temp[1] = FT_DivFix( temp[1], temp_scale );
1111 temp[2] = FT_DivFix( temp[2], temp_scale );
1112 temp[4] = FT_DivFix( temp[4], temp_scale );
1113 temp[5] = FT_DivFix( temp[5], temp_scale );
1114 temp[3] = 0x10000L;
1117 matrix->xx = temp[0];
1118 matrix->yx = temp[1];
1119 matrix->xy = temp[2];
1120 matrix->yy = temp[3];
1122 /* note that the offsets must be expressed in integer font units */
1123 offset->x = temp[4] >> 16;
1124 offset->y = temp[5] >> 16;
1128 static void
1129 parse_encoding( T1_Face face,
1130 T1_Loader loader )
1132 T1_Parser parser = &loader->parser;
1133 FT_Byte* cur;
1134 FT_Byte* limit = parser->root.limit;
1136 PSAux_Service psaux = (PSAux_Service)face->psaux;
1139 T1_Skip_Spaces( parser );
1140 cur = parser->root.cursor;
1141 if ( cur >= limit )
1143 FT_ERROR(( "parse_encoding: out of bounds\n" ));
1144 parser->root.error = T1_Err_Invalid_File_Format;
1145 return;
1148 /* if we have a number or `[', the encoding is an array, */
1149 /* and we must load it now */
1150 if ( ft_isdigit( *cur ) || *cur == '[' )
1152 T1_Encoding encode = &face->type1.encoding;
1153 FT_Int count, n;
1154 PS_Table char_table = &loader->encoding_table;
1155 FT_Memory memory = parser->root.memory;
1156 FT_Error error;
1157 FT_Bool only_immediates = 0;
1160 /* read the number of entries in the encoding; should be 256 */
1161 if ( *cur == '[' )
1163 count = 256;
1164 only_immediates = 1;
1165 parser->root.cursor++;
1167 else
1168 count = (FT_Int)T1_ToInt( parser );
1170 T1_Skip_Spaces( parser );
1171 if ( parser->root.cursor >= limit )
1172 return;
1174 /* we use a T1_Table to store our charnames */
1175 loader->num_chars = encode->num_chars = count;
1176 if ( FT_NEW_ARRAY( encode->char_index, count ) ||
1177 FT_NEW_ARRAY( encode->char_name, count ) ||
1178 FT_SET_ERROR( psaux->ps_table_funcs->init(
1179 char_table, count, memory ) ) )
1181 parser->root.error = error;
1182 return;
1185 /* We need to `zero' out encoding_table.elements */
1186 for ( n = 0; n < count; n++ )
1188 char* notdef = (char *)".notdef";
1191 T1_Add_Table( char_table, n, notdef, 8 );
1194 /* Now we need to read records of the form */
1195 /* */
1196 /* ... charcode /charname ... */
1197 /* */
1198 /* for each entry in our table. */
1199 /* */
1200 /* We simply look for a number followed by an immediate */
1201 /* name. Note that this ignores correctly the sequence */
1202 /* that is often seen in type1 fonts: */
1203 /* */
1204 /* 0 1 255 { 1 index exch /.notdef put } for dup */
1205 /* */
1206 /* used to clean the encoding array before anything else. */
1207 /* */
1208 /* Alternatively, if the array is directly given as */
1209 /* */
1210 /* /Encoding [ ... ] */
1211 /* */
1212 /* we only read immediates. */
1214 n = 0;
1215 T1_Skip_Spaces( parser );
1217 while ( parser->root.cursor < limit )
1219 cur = parser->root.cursor;
1221 /* we stop when we encounter a `def' or `]' */
1222 if ( *cur == 'd' && cur + 3 < limit )
1224 if ( cur[1] == 'e' &&
1225 cur[2] == 'f' &&
1226 IS_PS_DELIM( cur[3] ) )
1228 FT_TRACE6(( "encoding end\n" ));
1229 cur += 3;
1230 break;
1233 if ( *cur == ']' )
1235 FT_TRACE6(( "encoding end\n" ));
1236 cur++;
1237 break;
1240 /* check whether we've found an entry */
1241 if ( ft_isdigit( *cur ) || only_immediates )
1243 FT_Int charcode;
1246 if ( only_immediates )
1247 charcode = n;
1248 else
1250 charcode = (FT_Int)T1_ToInt( parser );
1251 T1_Skip_Spaces( parser );
1254 cur = parser->root.cursor;
1256 if ( *cur == '/' && cur + 2 < limit && n < count )
1258 FT_PtrDist len;
1261 cur++;
1263 parser->root.cursor = cur;
1264 T1_Skip_PS_Token( parser );
1265 if ( parser->root.error )
1266 return;
1268 len = parser->root.cursor - cur;
1270 parser->root.error = T1_Add_Table( char_table, charcode,
1271 cur, len + 1 );
1272 if ( parser->root.error )
1273 return;
1274 char_table->elements[charcode][len] = '\0';
1276 n++;
1278 else if ( only_immediates )
1280 /* Since the current position is not updated for */
1281 /* immediates-only mode we would get an infinite loop if */
1282 /* we don't do anything here. */
1283 /* */
1284 /* This encoding array is not valid according to the type1 */
1285 /* specification (it might be an encoding for a CID type1 */
1286 /* font, however), so we conclude that this font is NOT a */
1287 /* type1 font. */
1288 parser->root.error = FT_Err_Unknown_File_Format;
1289 return;
1292 else
1294 T1_Skip_PS_Token( parser );
1295 if ( parser->root.error )
1296 return;
1299 T1_Skip_Spaces( parser );
1302 face->type1.encoding_type = T1_ENCODING_TYPE_ARRAY;
1303 parser->root.cursor = cur;
1306 /* Otherwise, we should have either `StandardEncoding', */
1307 /* `ExpertEncoding', or `ISOLatin1Encoding' */
1308 else
1310 if ( cur + 17 < limit &&
1311 ft_strncmp( (const char*)cur, "StandardEncoding", 16 ) == 0 )
1312 face->type1.encoding_type = T1_ENCODING_TYPE_STANDARD;
1314 else if ( cur + 15 < limit &&
1315 ft_strncmp( (const char*)cur, "ExpertEncoding", 14 ) == 0 )
1316 face->type1.encoding_type = T1_ENCODING_TYPE_EXPERT;
1318 else if ( cur + 18 < limit &&
1319 ft_strncmp( (const char*)cur, "ISOLatin1Encoding", 17 ) == 0 )
1320 face->type1.encoding_type = T1_ENCODING_TYPE_ISOLATIN1;
1322 else
1323 parser->root.error = T1_Err_Ignore;
1328 static void
1329 parse_subrs( T1_Face face,
1330 T1_Loader loader )
1332 T1_Parser parser = &loader->parser;
1333 PS_Table table = &loader->subrs;
1334 FT_Memory memory = parser->root.memory;
1335 FT_Error error;
1336 FT_Int num_subrs;
1338 PSAux_Service psaux = (PSAux_Service)face->psaux;
1341 T1_Skip_Spaces( parser );
1343 /* test for empty array */
1344 if ( parser->root.cursor < parser->root.limit &&
1345 *parser->root.cursor == '[' )
1347 T1_Skip_PS_Token( parser );
1348 T1_Skip_Spaces ( parser );
1349 if ( parser->root.cursor >= parser->root.limit ||
1350 *parser->root.cursor != ']' )
1351 parser->root.error = T1_Err_Invalid_File_Format;
1352 return;
1355 num_subrs = (FT_Int)T1_ToInt( parser );
1357 /* position the parser right before the `dup' of the first subr */
1358 T1_Skip_PS_Token( parser ); /* `array' */
1359 if ( parser->root.error )
1360 return;
1361 T1_Skip_Spaces( parser );
1363 /* initialize subrs array -- with synthetic fonts it is possible */
1364 /* we get here twice */
1365 if ( !loader->num_subrs )
1367 error = psaux->ps_table_funcs->init( table, num_subrs, memory );
1368 if ( error )
1369 goto Fail;
1372 /* the format is simple: */
1373 /* */
1374 /* `index' + binary data */
1375 /* */
1376 for (;;)
1378 FT_Long idx, size;
1379 FT_Byte* base;
1382 /* If the next token isn't `dup' we are done. */
1383 if ( ft_strncmp( (char*)parser->root.cursor, "dup", 3 ) != 0 )
1384 break;
1386 T1_Skip_PS_Token( parser ); /* `dup' */
1388 idx = T1_ToInt( parser );
1390 if ( !read_binary_data( parser, &size, &base ) )
1391 return;
1393 /* The binary string is followed by one token, e.g. `NP' */
1394 /* (bound to `noaccess put') or by two separate tokens: */
1395 /* `noaccess' & `put'. We position the parser right */
1396 /* before the next `dup', if any. */
1397 T1_Skip_PS_Token( parser ); /* `NP' or `|' or `noaccess' */
1398 if ( parser->root.error )
1399 return;
1400 T1_Skip_Spaces ( parser );
1402 if ( ft_strncmp( (char*)parser->root.cursor, "put", 3 ) == 0 )
1404 T1_Skip_PS_Token( parser ); /* skip `put' */
1405 T1_Skip_Spaces ( parser );
1408 /* with synthetic fonts it is possible we get here twice */
1409 if ( loader->num_subrs )
1410 continue;
1412 /* some fonts use a value of -1 for lenIV to indicate that */
1413 /* the charstrings are unencoded */
1414 /* */
1415 /* thanks to Tom Kacvinsky for pointing this out */
1416 /* */
1417 if ( face->type1.private_dict.lenIV >= 0 )
1419 FT_Byte* temp;
1422 /* some fonts define empty subr records -- this is not totally */
1423 /* compliant to the specification (which says they should at */
1424 /* least contain a `return'), but we support them anyway */
1425 if ( size < face->type1.private_dict.lenIV )
1427 error = T1_Err_Invalid_File_Format;
1428 goto Fail;
1431 /* t1_decrypt() shouldn't write to base -- make temporary copy */
1432 if ( FT_ALLOC( temp, size ) )
1433 goto Fail;
1434 FT_MEM_COPY( temp, base, size );
1435 psaux->t1_decrypt( temp, size, 4330 );
1436 size -= face->type1.private_dict.lenIV;
1437 error = T1_Add_Table( table, (FT_Int)idx,
1438 temp + face->type1.private_dict.lenIV, size );
1439 FT_FREE( temp );
1441 else
1442 error = T1_Add_Table( table, (FT_Int)idx, base, size );
1443 if ( error )
1444 goto Fail;
1447 if ( !loader->num_subrs )
1448 loader->num_subrs = num_subrs;
1450 return;
1452 Fail:
1453 parser->root.error = error;
1457 #define TABLE_EXTEND 5
1460 static void
1461 parse_charstrings( T1_Face face,
1462 T1_Loader loader )
1464 T1_Parser parser = &loader->parser;
1465 PS_Table code_table = &loader->charstrings;
1466 PS_Table name_table = &loader->glyph_names;
1467 PS_Table swap_table = &loader->swap_table;
1468 FT_Memory memory = parser->root.memory;
1469 FT_Error error;
1471 PSAux_Service psaux = (PSAux_Service)face->psaux;
1473 FT_Byte* cur;
1474 FT_Byte* limit = parser->root.limit;
1475 FT_Int n, num_glyphs;
1476 FT_UInt notdef_index = 0;
1477 FT_Byte notdef_found = 0;
1480 num_glyphs = (FT_Int)T1_ToInt( parser );
1481 /* some fonts like Optima-Oblique not only define the /CharStrings */
1482 /* array but access it also */
1483 if ( num_glyphs == 0 || parser->root.error )
1484 return;
1486 /* initialize tables, leaving space for addition of .notdef, */
1487 /* if necessary, and a few other glyphs to handle buggy */
1488 /* fonts which have more glyphs than specified. */
1490 /* for some non-standard fonts like `Optima' which provides */
1491 /* different outlines depending on the resolution it is */
1492 /* possible to get here twice */
1493 if ( !loader->num_glyphs )
1495 error = psaux->ps_table_funcs->init(
1496 code_table, num_glyphs + 1 + TABLE_EXTEND, memory );
1497 if ( error )
1498 goto Fail;
1500 error = psaux->ps_table_funcs->init(
1501 name_table, num_glyphs + 1 + TABLE_EXTEND, memory );
1502 if ( error )
1503 goto Fail;
1505 /* Initialize table for swapping index notdef_index and */
1506 /* index 0 names and codes (if necessary). */
1508 error = psaux->ps_table_funcs->init( swap_table, 4, memory );
1509 if ( error )
1510 goto Fail;
1513 n = 0;
1515 for (;;)
1517 FT_Long size;
1518 FT_Byte* base;
1521 /* the format is simple: */
1522 /* `/glyphname' + binary data */
1524 T1_Skip_Spaces( parser );
1526 cur = parser->root.cursor;
1527 if ( cur >= limit )
1528 break;
1530 /* we stop when we find a `def' or `end' keyword */
1531 if ( cur + 3 < limit && IS_PS_DELIM( cur[3] ) )
1533 if ( cur[0] == 'd' &&
1534 cur[1] == 'e' &&
1535 cur[2] == 'f' )
1537 /* There are fonts which have this: */
1538 /* */
1539 /* /CharStrings 118 dict def */
1540 /* Private begin */
1541 /* CharStrings begin */
1542 /* ... */
1543 /* */
1544 /* To catch this we ignore `def' if */
1545 /* no charstring has actually been */
1546 /* seen. */
1547 if ( n )
1548 break;
1551 if ( cur[0] == 'e' &&
1552 cur[1] == 'n' &&
1553 cur[2] == 'd' )
1554 break;
1557 T1_Skip_PS_Token( parser );
1558 if ( parser->root.error )
1559 return;
1561 if ( *cur == '/' )
1563 FT_PtrDist len;
1566 if ( cur + 1 >= limit )
1568 error = T1_Err_Invalid_File_Format;
1569 goto Fail;
1572 cur++; /* skip `/' */
1573 len = parser->root.cursor - cur;
1575 if ( !read_binary_data( parser, &size, &base ) )
1576 return;
1578 /* for some non-standard fonts like `Optima' which provides */
1579 /* different outlines depending on the resolution it is */
1580 /* possible to get here twice */
1581 if ( loader->num_glyphs )
1582 continue;
1584 error = T1_Add_Table( name_table, n, cur, len + 1 );
1585 if ( error )
1586 goto Fail;
1588 /* add a trailing zero to the name table */
1589 name_table->elements[n][len] = '\0';
1591 /* record index of /.notdef */
1592 if ( *cur == '.' &&
1593 ft_strcmp( ".notdef",
1594 (const char*)(name_table->elements[n]) ) == 0 )
1596 notdef_index = n;
1597 notdef_found = 1;
1600 if ( face->type1.private_dict.lenIV >= 0 &&
1601 n < num_glyphs + TABLE_EXTEND )
1603 FT_Byte* temp;
1606 if ( size <= face->type1.private_dict.lenIV )
1608 error = T1_Err_Invalid_File_Format;
1609 goto Fail;
1612 /* t1_decrypt() shouldn't write to base -- make temporary copy */
1613 if ( FT_ALLOC( temp, size ) )
1614 goto Fail;
1615 FT_MEM_COPY( temp, base, size );
1616 psaux->t1_decrypt( temp, size, 4330 );
1617 size -= face->type1.private_dict.lenIV;
1618 error = T1_Add_Table( code_table, n,
1619 temp + face->type1.private_dict.lenIV, size );
1620 FT_FREE( temp );
1622 else
1623 error = T1_Add_Table( code_table, n, base, size );
1624 if ( error )
1625 goto Fail;
1627 n++;
1631 loader->num_glyphs = n;
1633 /* if /.notdef is found but does not occupy index 0, do our magic. */
1634 if ( notdef_found &&
1635 ft_strcmp( ".notdef", (const char*)name_table->elements[0] ) )
1637 /* Swap glyph in index 0 with /.notdef glyph. First, add index 0 */
1638 /* name and code entries to swap_table. Then place notdef_index */
1639 /* name and code entries into swap_table. Then swap name and code */
1640 /* entries at indices notdef_index and 0 using values stored in */
1641 /* swap_table. */
1643 /* Index 0 name */
1644 error = T1_Add_Table( swap_table, 0,
1645 name_table->elements[0],
1646 name_table->lengths [0] );
1647 if ( error )
1648 goto Fail;
1650 /* Index 0 code */
1651 error = T1_Add_Table( swap_table, 1,
1652 code_table->elements[0],
1653 code_table->lengths [0] );
1654 if ( error )
1655 goto Fail;
1657 /* Index notdef_index name */
1658 error = T1_Add_Table( swap_table, 2,
1659 name_table->elements[notdef_index],
1660 name_table->lengths [notdef_index] );
1661 if ( error )
1662 goto Fail;
1664 /* Index notdef_index code */
1665 error = T1_Add_Table( swap_table, 3,
1666 code_table->elements[notdef_index],
1667 code_table->lengths [notdef_index] );
1668 if ( error )
1669 goto Fail;
1671 error = T1_Add_Table( name_table, notdef_index,
1672 swap_table->elements[0],
1673 swap_table->lengths [0] );
1674 if ( error )
1675 goto Fail;
1677 error = T1_Add_Table( code_table, notdef_index,
1678 swap_table->elements[1],
1679 swap_table->lengths [1] );
1680 if ( error )
1681 goto Fail;
1683 error = T1_Add_Table( name_table, 0,
1684 swap_table->elements[2],
1685 swap_table->lengths [2] );
1686 if ( error )
1687 goto Fail;
1689 error = T1_Add_Table( code_table, 0,
1690 swap_table->elements[3],
1691 swap_table->lengths [3] );
1692 if ( error )
1693 goto Fail;
1696 else if ( !notdef_found )
1698 /* notdef_index is already 0, or /.notdef is undefined in */
1699 /* charstrings dictionary. Worry about /.notdef undefined. */
1700 /* We take index 0 and add it to the end of the table(s) */
1701 /* and add our own /.notdef glyph to index 0. */
1703 /* 0 333 hsbw endchar */
1704 FT_Byte notdef_glyph[] = { 0x8B, 0xF7, 0xE1, 0x0D, 0x0E };
1705 char* notdef_name = (char *)".notdef";
1708 error = T1_Add_Table( swap_table, 0,
1709 name_table->elements[0],
1710 name_table->lengths [0] );
1711 if ( error )
1712 goto Fail;
1714 error = T1_Add_Table( swap_table, 1,
1715 code_table->elements[0],
1716 code_table->lengths [0] );
1717 if ( error )
1718 goto Fail;
1720 error = T1_Add_Table( name_table, 0, notdef_name, 8 );
1721 if ( error )
1722 goto Fail;
1724 error = T1_Add_Table( code_table, 0, notdef_glyph, 5 );
1726 if ( error )
1727 goto Fail;
1729 error = T1_Add_Table( name_table, n,
1730 swap_table->elements[0],
1731 swap_table->lengths [0] );
1732 if ( error )
1733 goto Fail;
1735 error = T1_Add_Table( code_table, n,
1736 swap_table->elements[1],
1737 swap_table->lengths [1] );
1738 if ( error )
1739 goto Fail;
1741 /* we added a glyph. */
1742 loader->num_glyphs += 1;
1745 return;
1747 Fail:
1748 parser->root.error = error;
1752 /*************************************************************************/
1753 /* */
1754 /* Define the token field static variables. This is a set of */
1755 /* T1_FieldRec variables. */
1756 /* */
1757 /*************************************************************************/
1760 static
1761 const T1_FieldRec t1_keywords[] =
1764 #include "t1tokens.h"
1766 /* now add the special functions... */
1767 T1_FIELD_CALLBACK( "FontMatrix", parse_font_matrix,
1768 T1_FIELD_DICT_FONTDICT )
1769 T1_FIELD_CALLBACK( "Encoding", parse_encoding,
1770 T1_FIELD_DICT_FONTDICT )
1771 T1_FIELD_CALLBACK( "Subrs", parse_subrs,
1772 T1_FIELD_DICT_PRIVATE )
1773 T1_FIELD_CALLBACK( "CharStrings", parse_charstrings,
1774 T1_FIELD_DICT_PRIVATE )
1775 T1_FIELD_CALLBACK( "Private", parse_private,
1776 T1_FIELD_DICT_FONTDICT )
1778 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
1779 T1_FIELD_CALLBACK( "BlendDesignPositions", parse_blend_design_positions,
1780 T1_FIELD_DICT_FONTDICT )
1781 T1_FIELD_CALLBACK( "BlendDesignMap", parse_blend_design_map,
1782 T1_FIELD_DICT_FONTDICT )
1783 T1_FIELD_CALLBACK( "BlendAxisTypes", parse_blend_axis_types,
1784 T1_FIELD_DICT_FONTDICT )
1785 T1_FIELD_CALLBACK( "WeightVector", parse_weight_vector,
1786 T1_FIELD_DICT_FONTDICT )
1787 T1_FIELD_CALLBACK( "BuildCharArray", parse_buildchar,
1788 T1_FIELD_DICT_PRIVATE )
1789 #endif
1791 { 0, T1_FIELD_LOCATION_CID_INFO, T1_FIELD_TYPE_NONE, 0, 0, 0, 0, 0, 0 }
1795 #define T1_FIELD_COUNT \
1796 ( sizeof ( t1_keywords ) / sizeof ( t1_keywords[0] ) )
1799 static FT_Error
1800 parse_dict( T1_Face face,
1801 T1_Loader loader,
1802 FT_Byte* base,
1803 FT_Long size )
1805 T1_Parser parser = &loader->parser;
1806 FT_Byte *limit, *start_binary = NULL;
1807 FT_Bool have_integer = 0;
1810 parser->root.cursor = base;
1811 parser->root.limit = base + size;
1812 parser->root.error = T1_Err_Ok;
1814 limit = parser->root.limit;
1816 T1_Skip_Spaces( parser );
1818 while ( parser->root.cursor < limit )
1820 FT_Byte* cur;
1823 cur = parser->root.cursor;
1825 /* look for `eexec' */
1826 if ( IS_PS_TOKEN( cur, limit, "eexec" ) )
1827 break;
1829 /* look for `closefile' which ends the eexec section */
1830 else if ( IS_PS_TOKEN( cur, limit, "closefile" ) )
1831 break;
1833 /* in a synthetic font the base font starts after a */
1834 /* `FontDictionary' token that is placed after a Private dict */
1835 else if ( IS_PS_TOKEN( cur, limit, "FontDirectory" ) )
1837 if ( loader->keywords_encountered & T1_PRIVATE )
1838 loader->keywords_encountered |=
1839 T1_FONTDIR_AFTER_PRIVATE;
1840 parser->root.cursor += 13;
1843 /* check whether we have an integer */
1844 else if ( ft_isdigit( *cur ) )
1846 start_binary = cur;
1847 T1_Skip_PS_Token( parser );
1848 if ( parser->root.error )
1849 goto Exit;
1850 have_integer = 1;
1853 /* in valid Type 1 fonts we don't see `RD' or `-|' directly */
1854 /* since those tokens are handled by parse_subrs and */
1855 /* parse_charstrings */
1856 else if ( *cur == 'R' && cur + 6 < limit && *(cur + 1) == 'D' &&
1857 have_integer )
1859 FT_Long s;
1860 FT_Byte* b;
1863 parser->root.cursor = start_binary;
1864 if ( !read_binary_data( parser, &s, &b ) )
1865 return T1_Err_Invalid_File_Format;
1866 have_integer = 0;
1869 else if ( *cur == '-' && cur + 6 < limit && *(cur + 1) == '|' &&
1870 have_integer )
1872 FT_Long s;
1873 FT_Byte* b;
1876 parser->root.cursor = start_binary;
1877 if ( !read_binary_data( parser, &s, &b ) )
1878 return T1_Err_Invalid_File_Format;
1879 have_integer = 0;
1882 /* look for immediates */
1883 else if ( *cur == '/' && cur + 2 < limit )
1885 FT_PtrDist len;
1888 cur++;
1890 parser->root.cursor = cur;
1891 T1_Skip_PS_Token( parser );
1892 if ( parser->root.error )
1893 goto Exit;
1895 len = parser->root.cursor - cur;
1897 if ( len > 0 && len < 22 && parser->root.cursor < limit )
1899 /* now compare the immediate name to the keyword table */
1900 T1_Field keyword = (T1_Field)t1_keywords;
1903 for (;;)
1905 FT_Byte* name;
1908 name = (FT_Byte*)keyword->ident;
1909 if ( !name )
1910 break;
1912 if ( cur[0] == name[0] &&
1913 len == (FT_PtrDist)ft_strlen( (const char *)name ) &&
1914 ft_memcmp( cur, name, len ) == 0 )
1916 /* We found it -- run the parsing callback! */
1917 /* We record every instance of every field */
1918 /* (until we reach the base font of a */
1919 /* synthetic font) to deal adequately with */
1920 /* multiple master fonts; this is also */
1921 /* necessary because later PostScript */
1922 /* definitions override earlier ones. */
1924 /* Once we encounter `FontDirectory' after */
1925 /* `/Private', we know that this is a synthetic */
1926 /* font; except for `/CharStrings' we are not */
1927 /* interested in anything that follows this */
1928 /* `FontDirectory'. */
1930 /* MM fonts have more than one /Private token at */
1931 /* the top level; let's hope that all the junk */
1932 /* that follows the first /Private token is not */
1933 /* interesting to us. */
1935 /* According to Adobe Tech Note #5175 (CID-Keyed */
1936 /* Font Installation for ATM Software) a `begin' */
1937 /* must be followed by exactly one `end', and */
1938 /* `begin' -- `end' pairs must be accurately */
1939 /* paired. We could use this to distinguish */
1940 /* between the global Private and the Private */
1941 /* dict that is a member of the Blend dict. */
1943 const FT_UInt dict =
1944 ( loader->keywords_encountered & T1_PRIVATE )
1945 ? T1_FIELD_DICT_PRIVATE
1946 : T1_FIELD_DICT_FONTDICT;
1948 if ( !( dict & keyword->dict ) )
1950 FT_TRACE1(( "parse_dict: found %s but ignoring it "
1951 "since it is in the wrong dictionary\n",
1952 keyword->ident ));
1953 break;
1956 if ( !( loader->keywords_encountered &
1957 T1_FONTDIR_AFTER_PRIVATE ) ||
1958 ft_strcmp( (const char*)name, "CharStrings" ) == 0 )
1960 parser->root.error = t1_load_keyword( face,
1961 loader,
1962 keyword );
1963 if ( parser->root.error != T1_Err_Ok )
1965 if ( FT_ERROR_BASE( parser->root.error ) == FT_Err_Ignore )
1966 parser->root.error = T1_Err_Ok;
1967 else
1968 return parser->root.error;
1971 break;
1974 keyword++;
1978 have_integer = 0;
1980 else
1982 T1_Skip_PS_Token( parser );
1983 if ( parser->root.error )
1984 goto Exit;
1985 have_integer = 0;
1988 T1_Skip_Spaces( parser );
1991 Exit:
1992 return parser->root.error;
1996 static void
1997 t1_init_loader( T1_Loader loader,
1998 T1_Face face )
2000 FT_UNUSED( face );
2002 FT_MEM_ZERO( loader, sizeof ( *loader ) );
2003 loader->num_glyphs = 0;
2004 loader->num_chars = 0;
2006 /* initialize the tables -- simply set their `init' field to 0 */
2007 loader->encoding_table.init = 0;
2008 loader->charstrings.init = 0;
2009 loader->glyph_names.init = 0;
2010 loader->subrs.init = 0;
2011 loader->swap_table.init = 0;
2012 loader->fontdata = 0;
2013 loader->keywords_encountered = 0;
2017 static void
2018 t1_done_loader( T1_Loader loader )
2020 T1_Parser parser = &loader->parser;
2023 /* finalize tables */
2024 T1_Release_Table( &loader->encoding_table );
2025 T1_Release_Table( &loader->charstrings );
2026 T1_Release_Table( &loader->glyph_names );
2027 T1_Release_Table( &loader->swap_table );
2028 T1_Release_Table( &loader->subrs );
2030 /* finalize parser */
2031 T1_Finalize_Parser( parser );
2035 FT_LOCAL_DEF( FT_Error )
2036 T1_Open_Face( T1_Face face )
2038 T1_LoaderRec loader;
2039 T1_Parser parser;
2040 T1_Font type1 = &face->type1;
2041 PS_Private priv = &type1->private_dict;
2042 FT_Error error;
2044 PSAux_Service psaux = (PSAux_Service)face->psaux;
2047 t1_init_loader( &loader, face );
2049 /* default values */
2050 face->ndv_idx = -1;
2051 face->cdv_idx = -1;
2052 face->len_buildchar = 0;
2054 priv->blue_shift = 7;
2055 priv->blue_fuzz = 1;
2056 priv->lenIV = 4;
2057 priv->expansion_factor = (FT_Fixed)( 0.06 * 0x10000L );
2058 priv->blue_scale = (FT_Fixed)( 0.039625 * 0x10000L * 1000 );
2060 parser = &loader.parser;
2061 error = T1_New_Parser( parser,
2062 face->root.stream,
2063 face->root.memory,
2064 psaux );
2065 if ( error )
2066 goto Exit;
2068 error = parse_dict( face, &loader,
2069 parser->base_dict, parser->base_len );
2070 if ( error )
2071 goto Exit;
2073 error = T1_Get_Private_Dict( parser, psaux );
2074 if ( error )
2075 goto Exit;
2077 error = parse_dict( face, &loader,
2078 parser->private_dict, parser->private_len );
2079 if ( error )
2080 goto Exit;
2082 /* ensure even-ness of `num_blue_values' */
2083 priv->num_blue_values &= ~1;
2085 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
2087 if ( face->blend &&
2088 face->blend->num_default_design_vector != 0 &&
2089 face->blend->num_default_design_vector != face->blend->num_axis )
2091 /* we don't use it currently so just warn, reset, and ignore */
2092 FT_ERROR(( "T1_Open_Face(): /DesignVector contains %u entries "
2093 "while there are %u axes.\n",
2094 face->blend->num_default_design_vector,
2095 face->blend->num_axis ));
2097 face->blend->num_default_design_vector = 0;
2100 /* the following can happen for MM instances; we then treat the */
2101 /* font as a normal PS font */
2102 if ( face->blend &&
2103 ( !face->blend->num_designs || !face->blend->num_axis ) )
2104 T1_Done_Blend( face );
2106 /* another safety check */
2107 if ( face->blend )
2109 FT_UInt i;
2112 for ( i = 0; i < face->blend->num_axis; i++ )
2113 if ( !face->blend->design_map[i].num_points )
2115 T1_Done_Blend( face );
2116 break;
2120 if ( face->blend )
2122 if ( face->len_buildchar > 0 )
2124 FT_Memory memory = face->root.memory;
2127 if ( FT_NEW_ARRAY( face->buildchar, face->len_buildchar ) )
2129 FT_ERROR(( "T1_Open_Face: cannot allocate BuildCharArray\n" ));
2130 face->len_buildchar = 0;
2131 goto Exit;
2136 #endif /* T1_CONFIG_OPTION_NO_MM_SUPPORT */
2138 /* now, propagate the subrs, charstrings, and glyphnames tables */
2139 /* to the Type1 data */
2140 type1->num_glyphs = loader.num_glyphs;
2142 if ( loader.subrs.init )
2144 loader.subrs.init = 0;
2145 type1->num_subrs = loader.num_subrs;
2146 type1->subrs_block = loader.subrs.block;
2147 type1->subrs = loader.subrs.elements;
2148 type1->subrs_len = loader.subrs.lengths;
2151 #ifdef FT_CONFIG_OPTION_INCREMENTAL
2152 if ( !face->root.internal->incremental_interface )
2153 #endif
2154 if ( !loader.charstrings.init )
2156 FT_ERROR(( "T1_Open_Face: no `/CharStrings' array in face\n" ));
2157 error = T1_Err_Invalid_File_Format;
2160 loader.charstrings.init = 0;
2161 type1->charstrings_block = loader.charstrings.block;
2162 type1->charstrings = loader.charstrings.elements;
2163 type1->charstrings_len = loader.charstrings.lengths;
2165 /* we copy the glyph names `block' and `elements' fields; */
2166 /* the `lengths' field must be released later */
2167 type1->glyph_names_block = loader.glyph_names.block;
2168 type1->glyph_names = (FT_String**)loader.glyph_names.elements;
2169 loader.glyph_names.block = 0;
2170 loader.glyph_names.elements = 0;
2172 /* we must now build type1.encoding when we have a custom array */
2173 if ( type1->encoding_type == T1_ENCODING_TYPE_ARRAY )
2175 FT_Int charcode, idx, min_char, max_char;
2176 FT_Byte* char_name;
2177 FT_Byte* glyph_name;
2180 /* OK, we do the following: for each element in the encoding */
2181 /* table, look up the index of the glyph having the same name */
2182 /* the index is then stored in type1.encoding.char_index, and */
2183 /* a the name to type1.encoding.char_name */
2185 min_char = 0;
2186 max_char = 0;
2188 charcode = 0;
2189 for ( ; charcode < loader.encoding_table.max_elems; charcode++ )
2191 type1->encoding.char_index[charcode] = 0;
2192 type1->encoding.char_name [charcode] = (char *)".notdef";
2194 char_name = loader.encoding_table.elements[charcode];
2195 if ( char_name )
2196 for ( idx = 0; idx < type1->num_glyphs; idx++ )
2198 glyph_name = (FT_Byte*)type1->glyph_names[idx];
2199 if ( ft_strcmp( (const char*)char_name,
2200 (const char*)glyph_name ) == 0 )
2202 type1->encoding.char_index[charcode] = (FT_UShort)idx;
2203 type1->encoding.char_name [charcode] = (char*)glyph_name;
2205 /* Change min/max encoded char only if glyph name is */
2206 /* not /.notdef */
2207 if ( ft_strcmp( (const char*)".notdef",
2208 (const char*)glyph_name ) != 0 )
2210 if ( charcode < min_char )
2211 min_char = charcode;
2212 if ( charcode >= max_char )
2213 max_char = charcode + 1;
2215 break;
2220 type1->encoding.code_first = min_char;
2221 type1->encoding.code_last = max_char;
2222 type1->encoding.num_chars = loader.num_chars;
2225 Exit:
2226 t1_done_loader( &loader );
2227 return error;
2231 /* END */