1 /***************************************************************************/
5 /* Type 1 font loader (body). */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, */
9 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
11 /* This file is part of the FreeType project, and may only be used, */
12 /* modified, and distributed under the terms of the FreeType project */
13 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
14 /* this file you indicate that you have read the license and */
15 /* understand and accept it fully. */
17 /***************************************************************************/
20 /*************************************************************************/
22 /* This is the new and improved Type 1 data loader for FreeType 2. The */
23 /* old loader has several problems: it is slow, complex, difficult to */
24 /* maintain, and contains incredible hacks to make it accept some */
25 /* ill-formed Type 1 fonts without hiccup-ing. Moreover, about 5% of */
26 /* the Type 1 fonts on my machine still aren't loaded correctly by it. */
28 /* This version is much simpler, much faster and also easier to read and */
29 /* maintain by a great order of magnitude. The idea behind it is to */
30 /* _not_ try to read the Type 1 token stream with a state machine (i.e. */
31 /* a Postscript-like interpreter) but rather to perform simple pattern */
34 /* Indeed, nearly all data definitions follow a simple pattern like */
36 /* ... /Field <data> ... */
38 /* where <data> can be a number, a boolean, a string, or an array of */
39 /* numbers. There are a few exceptions, namely the encoding, font name, */
40 /* charstrings, and subrs; they are handled with a special pattern */
41 /* matching routine. */
43 /* All other common cases are handled very simply. The matching rules */
44 /* are defined in the file `t1tokens.h' through the use of several */
45 /* macros calls PARSE_XXX. This file is included twice here; the first */
46 /* time to generate parsing callback functions, the second time to */
47 /* generate a table of keywords (with pointers to the associated */
48 /* callback functions). */
50 /* The function `parse_dict' simply scans *linearly* a given dictionary */
51 /* (either the top-level or private one) and calls the appropriate */
52 /* callback when it encounters an immediate keyword. */
54 /* This is by far the fastest way one can find to parse and read all */
57 /* This led to tremendous code size reduction. Note that later, the */
58 /* glyph loader will also be _greatly_ simplified, and the automatic */
59 /* hinter will replace the clumsy `t1hinter'. */
61 /*************************************************************************/
65 #include FT_INTERNAL_DEBUG_H
66 #include FT_CONFIG_CONFIG_H
67 #include FT_MULTIPLE_MASTERS_H
68 #include FT_INTERNAL_TYPE1_TYPES_H
69 #include FT_INTERNAL_CALC_H
75 /*************************************************************************/
77 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
78 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
79 /* messages during execution. */
82 #define FT_COMPONENT trace_t1load
85 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
88 /*************************************************************************/
89 /*************************************************************************/
91 /***** MULTIPLE MASTERS SUPPORT *****/
93 /*************************************************************************/
94 /*************************************************************************/
97 t1_allocate_blend( T1_Face face
,
102 FT_Memory memory
= face
->root
.memory
;
103 FT_Error error
= T1_Err_Ok
;
109 if ( FT_NEW( blend
) )
112 blend
->num_default_design_vector
= 0;
117 /* allocate design data if needed */
118 if ( num_designs
> 0 )
120 if ( blend
->num_designs
== 0 )
125 /* allocate the blend `private' and `font_info' dictionaries */
126 if ( FT_NEW_ARRAY( blend
->font_infos
[1], num_designs
) ||
127 FT_NEW_ARRAY( blend
->privates
[1], num_designs
) ||
128 FT_NEW_ARRAY( blend
->bboxes
[1], num_designs
) ||
129 FT_NEW_ARRAY( blend
->weight_vector
, num_designs
* 2 ) )
132 blend
->default_weight_vector
= blend
->weight_vector
+ num_designs
;
134 blend
->font_infos
[0] = &face
->type1
.font_info
;
135 blend
->privates
[0] = &face
->type1
.private_dict
;
136 blend
->bboxes
[0] = &face
->type1
.font_bbox
;
138 for ( nn
= 2; nn
<= num_designs
; nn
++ )
140 blend
->privates
[nn
] = blend
->privates
[nn
- 1] + 1;
141 blend
->font_infos
[nn
] = blend
->font_infos
[nn
- 1] + 1;
142 blend
->bboxes
[nn
] = blend
->bboxes
[nn
- 1] + 1;
145 blend
->num_designs
= num_designs
;
147 else if ( blend
->num_designs
!= num_designs
)
151 /* allocate axis data if needed */
154 if ( blend
->num_axis
!= 0 && blend
->num_axis
!= num_axis
)
157 blend
->num_axis
= num_axis
;
160 /* allocate the blend design pos table if needed */
161 num_designs
= blend
->num_designs
;
162 num_axis
= blend
->num_axis
;
163 if ( num_designs
&& num_axis
&& blend
->design_pos
[0] == 0 )
168 if ( FT_NEW_ARRAY( blend
->design_pos
[0], num_designs
* num_axis
) )
171 for ( n
= 1; n
< num_designs
; n
++ )
172 blend
->design_pos
[n
] = blend
->design_pos
[0] + num_axis
* n
;
179 error
= T1_Err_Invalid_File_Format
;
184 FT_LOCAL_DEF( FT_Error
)
185 T1_Get_Multi_Master( T1_Face face
,
186 FT_Multi_Master
* master
)
188 PS_Blend blend
= face
->blend
;
193 error
= T1_Err_Invalid_Argument
;
197 master
->num_axis
= blend
->num_axis
;
198 master
->num_designs
= blend
->num_designs
;
200 for ( n
= 0; n
< blend
->num_axis
; n
++ )
202 FT_MM_Axis
* axis
= master
->axis
+ n
;
203 PS_DesignMap map
= blend
->design_map
+ n
;
206 axis
->name
= blend
->axis_names
[n
];
207 axis
->minimum
= map
->design_points
[0];
208 axis
->maximum
= map
->design_points
[map
->num_points
- 1];
218 /*************************************************************************/
220 /* Given a normalized (blend) coordinate, figure out the design */
221 /* coordinate appropriate for that value. */
223 FT_LOCAL_DEF( FT_Fixed
)
224 mm_axis_unmap( PS_DesignMap axismap
,
230 if ( ncv
<= axismap
->blend_points
[0] )
231 return INT_TO_FIXED( axismap
->design_points
[0] );
233 for ( j
= 1; j
< axismap
->num_points
; ++j
)
235 if ( ncv
<= axismap
->blend_points
[j
] )
237 FT_Fixed t
= FT_MulDiv( ncv
- axismap
->blend_points
[j
- 1],
239 axismap
->blend_points
[j
] -
240 axismap
->blend_points
[j
- 1] );
242 return INT_TO_FIXED( axismap
->design_points
[j
- 1] ) +
244 axismap
->design_points
[j
] -
245 axismap
->design_points
[j
- 1],
250 return INT_TO_FIXED( axismap
->design_points
[axismap
->num_points
- 1] );
254 /*************************************************************************/
256 /* Given a vector of weights, one for each design, figure out the */
257 /* normalized axis coordinates which gave rise to those weights. */
260 mm_weights_unmap( FT_Fixed
* weights
,
261 FT_Fixed
* axiscoords
,
264 FT_ASSERT( axis_count
<= T1_MAX_MM_AXIS
);
266 if ( axis_count
== 1 )
267 axiscoords
[0] = weights
[1];
269 else if ( axis_count
== 2 )
271 axiscoords
[0] = weights
[3] + weights
[1];
272 axiscoords
[1] = weights
[3] + weights
[2];
275 else if ( axis_count
== 3 )
277 axiscoords
[0] = weights
[7] + weights
[5] + weights
[3] + weights
[1];
278 axiscoords
[1] = weights
[7] + weights
[6] + weights
[3] + weights
[2];
279 axiscoords
[2] = weights
[7] + weights
[6] + weights
[5] + weights
[4];
284 axiscoords
[0] = weights
[15] + weights
[13] + weights
[11] + weights
[9] +
285 weights
[7] + weights
[5] + weights
[3] + weights
[1];
286 axiscoords
[1] = weights
[15] + weights
[14] + weights
[11] + weights
[10] +
287 weights
[7] + weights
[6] + weights
[3] + weights
[2];
288 axiscoords
[2] = weights
[15] + weights
[14] + weights
[13] + weights
[12] +
289 weights
[7] + weights
[6] + weights
[5] + weights
[4];
290 axiscoords
[3] = weights
[15] + weights
[14] + weights
[13] + weights
[12] +
291 weights
[11] + weights
[10] + weights
[9] + weights
[8];
296 /*************************************************************************/
298 /* Just a wrapper around T1_Get_Multi_Master to support the different */
299 /* arguments needed by the GX var distortable fonts. */
301 FT_LOCAL_DEF( FT_Error
)
302 T1_Get_MM_Var( T1_Face face
,
305 FT_Memory memory
= face
->root
.memory
;
307 FT_Multi_Master mmaster
;
310 FT_Fixed axiscoords
[T1_MAX_MM_AXIS
];
311 PS_Blend blend
= face
->blend
;
314 error
= T1_Get_Multi_Master( face
, &mmaster
);
317 if ( FT_ALLOC( mmvar
,
318 sizeof ( FT_MM_Var
) +
319 mmaster
.num_axis
* sizeof ( FT_Var_Axis
) ) )
322 mmvar
->num_axis
= mmaster
.num_axis
;
323 mmvar
->num_designs
= mmaster
.num_designs
;
324 mmvar
->num_namedstyles
= (FT_UInt
)-1; /* Does not apply */
325 mmvar
->axis
= (FT_Var_Axis
*)&mmvar
[1];
326 /* Point to axes after MM_Var struct */
327 mmvar
->namedstyle
= NULL
;
329 for ( i
= 0 ; i
< mmaster
.num_axis
; ++i
)
331 mmvar
->axis
[i
].name
= mmaster
.axis
[i
].name
;
332 mmvar
->axis
[i
].minimum
= INT_TO_FIXED( mmaster
.axis
[i
].minimum
);
333 mmvar
->axis
[i
].maximum
= INT_TO_FIXED( mmaster
.axis
[i
].maximum
);
334 mmvar
->axis
[i
].def
= ( mmvar
->axis
[i
].minimum
+
335 mmvar
->axis
[i
].maximum
) / 2;
336 /* Does not apply. But this value is in range */
337 mmvar
->axis
[i
].strid
= (FT_UInt
)-1; /* Does not apply */
338 mmvar
->axis
[i
].tag
= (FT_ULong
)-1; /* Does not apply */
340 if ( ft_strcmp( mmvar
->axis
[i
].name
, "Weight" ) == 0 )
341 mmvar
->axis
[i
].tag
= FT_MAKE_TAG( 'w', 'g', 'h', 't' );
342 else if ( ft_strcmp( mmvar
->axis
[i
].name
, "Width" ) == 0 )
343 mmvar
->axis
[i
].tag
= FT_MAKE_TAG( 'w', 'd', 't', 'h' );
344 else if ( ft_strcmp( mmvar
->axis
[i
].name
, "OpticalSize" ) == 0 )
345 mmvar
->axis
[i
].tag
= FT_MAKE_TAG( 'o', 'p', 's', 'z' );
348 if ( blend
->num_designs
== ( 1U << blend
->num_axis
) )
350 mm_weights_unmap( blend
->default_weight_vector
,
354 for ( i
= 0; i
< mmaster
.num_axis
; ++i
)
355 mmvar
->axis
[i
].def
= mm_axis_unmap( &blend
->design_map
[i
],
366 FT_LOCAL_DEF( FT_Error
)
367 T1_Set_MM_Blend( T1_Face face
,
371 PS_Blend blend
= face
->blend
;
376 error
= T1_Err_Invalid_Argument
;
378 if ( blend
&& blend
->num_axis
== num_coords
)
380 /* recompute the weight vector from the blend coordinates */
383 for ( n
= 0; n
< blend
->num_designs
; n
++ )
385 FT_Fixed result
= 0x10000L
; /* 1.0 fixed */
388 for ( m
= 0; m
< blend
->num_axis
; m
++ )
393 /* get current blend axis position */
395 if ( factor
< 0 ) factor
= 0;
396 if ( factor
> 0x10000L
) factor
= 0x10000L
;
398 if ( ( n
& ( 1 << m
) ) == 0 )
399 factor
= 0x10000L
- factor
;
401 result
= FT_MulFix( result
, factor
);
403 blend
->weight_vector
[n
] = result
;
413 FT_LOCAL_DEF( FT_Error
)
414 T1_Set_MM_Design( T1_Face face
,
418 PS_Blend blend
= face
->blend
;
423 error
= T1_Err_Invalid_Argument
;
424 if ( blend
&& blend
->num_axis
== num_coords
)
426 /* compute the blend coordinates through the blend design map */
427 FT_Fixed final_blends
[T1_MAX_MM_DESIGNS
];
430 for ( n
= 0; n
< blend
->num_axis
; n
++ )
432 FT_Long design
= coords
[n
];
434 PS_DesignMap map
= blend
->design_map
+ n
;
435 FT_Long
* designs
= map
->design_points
;
436 FT_Fixed
* blends
= map
->blend_points
;
437 FT_Int before
= -1, after
= -1;
440 for ( p
= 0; p
< (FT_UInt
)map
->num_points
; p
++ )
442 FT_Long p_design
= designs
[p
];
446 if ( design
== p_design
)
448 the_blend
= blends
[p
];
452 if ( design
< p_design
)
461 /* now interpolate if necessary */
463 the_blend
= blends
[0];
465 else if ( after
< 0 )
466 the_blend
= blends
[map
->num_points
- 1];
469 the_blend
= FT_MulDiv( design
- designs
[before
],
470 blends
[after
] - blends
[before
],
471 designs
[after
] - designs
[before
] );
474 final_blends
[n
] = the_blend
;
477 error
= T1_Set_MM_Blend( face
, num_coords
, final_blends
);
484 /*************************************************************************/
486 /* Just a wrapper around T1_Set_MM_Design to support the different */
487 /* arguments needed by the GX var distortable fonts. */
489 FT_LOCAL_DEF( FT_Error
)
490 T1_Set_Var_Design( T1_Face face
,
494 FT_Long lcoords
[4]; /* maximum axis count is 4 */
499 error
= T1_Err_Invalid_Argument
;
500 if ( num_coords
<= 4 && num_coords
> 0 )
502 for ( i
= 0; i
< num_coords
; ++i
)
503 lcoords
[i
] = FIXED_TO_INT( coords
[i
] );
504 error
= T1_Set_MM_Design( face
, num_coords
, lcoords
);
512 T1_Done_Blend( T1_Face face
)
514 FT_Memory memory
= face
->root
.memory
;
515 PS_Blend blend
= face
->blend
;
520 FT_UInt num_designs
= blend
->num_designs
;
521 FT_UInt num_axis
= blend
->num_axis
;
525 /* release design pos table */
526 FT_FREE( blend
->design_pos
[0] );
527 for ( n
= 1; n
< num_designs
; n
++ )
528 blend
->design_pos
[n
] = 0;
530 /* release blend `private' and `font info' dictionaries */
531 FT_FREE( blend
->privates
[1] );
532 FT_FREE( blend
->font_infos
[1] );
533 FT_FREE( blend
->bboxes
[1] );
535 for ( n
= 0; n
< num_designs
; n
++ )
537 blend
->privates
[n
] = 0;
538 blend
->font_infos
[n
] = 0;
539 blend
->bboxes
[n
] = 0;
542 /* release weight vectors */
543 FT_FREE( blend
->weight_vector
);
544 blend
->default_weight_vector
= 0;
546 /* release axis names */
547 for ( n
= 0; n
< num_axis
; n
++ )
548 FT_FREE( blend
->axis_names
[n
] );
550 /* release design map */
551 for ( n
= 0; n
< num_axis
; n
++ )
553 PS_DesignMap dmap
= blend
->design_map
+ n
;
556 FT_FREE( dmap
->design_points
);
557 dmap
->num_points
= 0;
560 FT_FREE( face
->blend
);
566 parse_blend_axis_types( T1_Face face
,
569 T1_TokenRec axis_tokens
[T1_MAX_MM_AXIS
];
571 FT_Error error
= T1_Err_Ok
;
576 /* take an array of objects */
577 T1_ToTokenArray( &loader
->parser
, axis_tokens
,
578 T1_MAX_MM_AXIS
, &num_axis
);
581 error
= T1_Err_Ignore
;
584 if ( num_axis
== 0 || num_axis
> T1_MAX_MM_AXIS
)
586 FT_ERROR(( "parse_blend_axis_types: incorrect number of axes: %d\n",
588 error
= T1_Err_Invalid_File_Format
;
592 /* allocate blend if necessary */
593 error
= t1_allocate_blend( face
, 0, (FT_UInt
)num_axis
);
598 memory
= face
->root
.memory
;
600 /* each token is an immediate containing the name of the axis */
601 for ( n
= 0; n
< num_axis
; n
++ )
603 T1_Token token
= axis_tokens
+ n
;
608 /* skip first slash, if any */
609 if ( token
->start
[0] == '/' )
612 len
= token
->limit
- token
->start
;
615 error
= T1_Err_Invalid_File_Format
;
619 if ( FT_ALLOC( blend
->axis_names
[n
], len
+ 1 ) )
622 name
= (FT_Byte
*)blend
->axis_names
[n
];
623 FT_MEM_COPY( name
, token
->start
, len
);
628 loader
->parser
.root
.error
= error
;
633 parse_blend_design_positions( T1_Face face
,
636 T1_TokenRec design_tokens
[T1_MAX_MM_DESIGNS
];
639 T1_Parser parser
= &loader
->parser
;
641 FT_Error error
= T1_Err_Ok
;
645 /* get the array of design tokens -- compute number of designs */
646 T1_ToTokenArray( parser
, design_tokens
,
647 T1_MAX_MM_DESIGNS
, &num_designs
);
648 if ( num_designs
< 0 )
650 error
= T1_Err_Ignore
;
653 if ( num_designs
== 0 || num_designs
> T1_MAX_MM_DESIGNS
)
655 FT_ERROR(( "parse_blend_design_positions:"
656 " incorrect number of designs: %d\n",
658 error
= T1_Err_Invalid_File_Format
;
663 FT_Byte
* old_cursor
= parser
->root
.cursor
;
664 FT_Byte
* old_limit
= parser
->root
.limit
;
669 num_axis
= 0; /* make compiler happy */
671 for ( n
= 0; n
< num_designs
; n
++ )
673 T1_TokenRec axis_tokens
[T1_MAX_MM_AXIS
];
678 /* read axis/coordinates tokens */
679 token
= design_tokens
+ n
;
680 parser
->root
.cursor
= token
->start
;
681 parser
->root
.limit
= token
->limit
;
682 T1_ToTokenArray( parser
, axis_tokens
, T1_MAX_MM_AXIS
, &n_axis
);
686 if ( n_axis
<= 0 || n_axis
> T1_MAX_MM_AXIS
)
688 FT_ERROR(( "parse_blend_design_positions:"
689 " invalid number of axes: %d\n",
691 error
= T1_Err_Invalid_File_Format
;
696 error
= t1_allocate_blend( face
, num_designs
, num_axis
);
701 else if ( n_axis
!= num_axis
)
703 FT_ERROR(( "parse_blend_design_positions: incorrect table\n" ));
704 error
= T1_Err_Invalid_File_Format
;
708 /* now read each axis token into the design position */
709 for ( axis
= 0; axis
< n_axis
; axis
++ )
711 T1_Token token2
= axis_tokens
+ axis
;
714 parser
->root
.cursor
= token2
->start
;
715 parser
->root
.limit
= token2
->limit
;
716 blend
->design_pos
[n
][axis
] = T1_ToFixed( parser
, 0 );
720 loader
->parser
.root
.cursor
= old_cursor
;
721 loader
->parser
.root
.limit
= old_limit
;
725 loader
->parser
.root
.error
= error
;
730 parse_blend_design_map( T1_Face face
,
733 FT_Error error
= T1_Err_Ok
;
734 T1_Parser parser
= &loader
->parser
;
736 T1_TokenRec axis_tokens
[T1_MAX_MM_AXIS
];
740 FT_Memory memory
= face
->root
.memory
;
743 T1_ToTokenArray( parser
, axis_tokens
,
744 T1_MAX_MM_AXIS
, &num_axis
);
747 error
= T1_Err_Ignore
;
750 if ( num_axis
== 0 || num_axis
> T1_MAX_MM_AXIS
)
752 FT_ERROR(( "parse_blend_design_map: incorrect number of axes: %d\n",
754 error
= T1_Err_Invalid_File_Format
;
758 old_cursor
= parser
->root
.cursor
;
759 old_limit
= parser
->root
.limit
;
761 error
= t1_allocate_blend( face
, 0, num_axis
);
766 /* now read each axis design map */
767 for ( n
= 0; n
< num_axis
; n
++ )
769 PS_DesignMap map
= blend
->design_map
+ n
;
771 T1_TokenRec point_tokens
[T1_MAX_MM_MAP_POINTS
];
772 FT_Int p
, num_points
;
775 axis_token
= axis_tokens
+ n
;
777 parser
->root
.cursor
= axis_token
->start
;
778 parser
->root
.limit
= axis_token
->limit
;
779 T1_ToTokenArray( parser
, point_tokens
,
780 T1_MAX_MM_MAP_POINTS
, &num_points
);
782 if ( num_points
<= 0 || num_points
> T1_MAX_MM_MAP_POINTS
)
784 FT_ERROR(( "parse_blend_design_map: incorrect table\n" ));
785 error
= T1_Err_Invalid_File_Format
;
789 /* allocate design map data */
790 if ( FT_NEW_ARRAY( map
->design_points
, num_points
* 2 ) )
792 map
->blend_points
= map
->design_points
+ num_points
;
793 map
->num_points
= (FT_Byte
)num_points
;
795 for ( p
= 0; p
< num_points
; p
++ )
797 T1_Token point_token
;
800 point_token
= point_tokens
+ p
;
802 /* don't include delimiting brackets */
803 parser
->root
.cursor
= point_token
->start
+ 1;
804 parser
->root
.limit
= point_token
->limit
- 1;
806 map
->design_points
[p
] = T1_ToInt( parser
);
807 map
->blend_points
[p
] = T1_ToFixed( parser
, 0 );
811 parser
->root
.cursor
= old_cursor
;
812 parser
->root
.limit
= old_limit
;
815 parser
->root
.error
= error
;
820 parse_weight_vector( T1_Face face
,
823 T1_TokenRec design_tokens
[T1_MAX_MM_DESIGNS
];
825 FT_Error error
= T1_Err_Ok
;
826 T1_Parser parser
= &loader
->parser
;
827 PS_Blend blend
= face
->blend
;
834 T1_ToTokenArray( parser
, design_tokens
,
835 T1_MAX_MM_DESIGNS
, &num_designs
);
836 if ( num_designs
< 0 )
838 error
= T1_Err_Ignore
;
841 if ( num_designs
== 0 || num_designs
> T1_MAX_MM_DESIGNS
)
843 FT_ERROR(( "parse_weight_vector:"
844 " incorrect number of designs: %d\n",
846 error
= T1_Err_Invalid_File_Format
;
850 if ( !blend
|| !blend
->num_designs
)
852 error
= t1_allocate_blend( face
, num_designs
, 0 );
857 else if ( blend
->num_designs
!= (FT_UInt
)num_designs
)
859 FT_ERROR(( "parse_weight_vector:"
860 " /BlendDesignPosition and /WeightVector have\n"
862 " different number of elements\n" ));
863 error
= T1_Err_Invalid_File_Format
;
867 old_cursor
= parser
->root
.cursor
;
868 old_limit
= parser
->root
.limit
;
870 for ( n
= 0; n
< num_designs
; n
++ )
872 token
= design_tokens
+ n
;
873 parser
->root
.cursor
= token
->start
;
874 parser
->root
.limit
= token
->limit
;
876 blend
->default_weight_vector
[n
] =
877 blend
->weight_vector
[n
] = T1_ToFixed( parser
, 0 );
880 parser
->root
.cursor
= old_cursor
;
881 parser
->root
.limit
= old_limit
;
884 parser
->root
.error
= error
;
888 /* e.g., /BuildCharArray [0 0 0 0 0 0 0 0] def */
889 /* we're only interested in the number of array elements */
891 parse_buildchar( T1_Face face
,
894 face
->len_buildchar
= T1_ToFixedArray( &loader
->parser
, 0, NULL
, 0 );
900 #endif /* T1_CONFIG_OPTION_NO_MM_SUPPORT */
905 /*************************************************************************/
906 /*************************************************************************/
908 /***** TYPE 1 SYMBOL PARSING *****/
910 /*************************************************************************/
911 /*************************************************************************/
914 t1_load_keyword( T1_Face face
,
916 const T1_Field field
)
922 PS_Blend blend
= face
->blend
;
925 /* if the keyword has a dedicated callback, call it */
926 if ( field
->type
== T1_FIELD_TYPE_CALLBACK
)
928 field
->reader( (FT_Face
)face
, loader
);
929 error
= loader
->parser
.root
.error
;
933 /* now, the keyword is either a simple field, or a table of fields; */
934 /* we are now going to take care of it */
935 switch ( field
->location
)
937 case T1_FIELD_LOCATION_FONT_INFO
:
938 dummy_object
= &face
->type1
.font_info
;
939 objects
= &dummy_object
;
944 objects
= (void**)blend
->font_infos
;
945 max_objects
= blend
->num_designs
;
949 case T1_FIELD_LOCATION_FONT_EXTRA
:
950 dummy_object
= &face
->type1
.font_extra
;
951 objects
= &dummy_object
;
955 case T1_FIELD_LOCATION_PRIVATE
:
956 dummy_object
= &face
->type1
.private_dict
;
957 objects
= &dummy_object
;
962 objects
= (void**)blend
->privates
;
963 max_objects
= blend
->num_designs
;
967 case T1_FIELD_LOCATION_BBOX
:
968 dummy_object
= &face
->type1
.font_bbox
;
969 objects
= &dummy_object
;
974 objects
= (void**)blend
->bboxes
;
975 max_objects
= blend
->num_designs
;
979 case T1_FIELD_LOCATION_LOADER
:
980 dummy_object
= loader
;
981 objects
= &dummy_object
;
985 case T1_FIELD_LOCATION_FACE
:
987 objects
= &dummy_object
;
991 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
992 case T1_FIELD_LOCATION_BLEND
:
993 dummy_object
= face
->blend
;
994 objects
= &dummy_object
;
1000 dummy_object
= &face
->type1
;
1001 objects
= &dummy_object
;
1005 if ( field
->type
== T1_FIELD_TYPE_INTEGER_ARRAY
||
1006 field
->type
== T1_FIELD_TYPE_FIXED_ARRAY
)
1007 error
= T1_Load_Field_Table( &loader
->parser
, field
,
1008 objects
, max_objects
, 0 );
1010 error
= T1_Load_Field( &loader
->parser
, field
,
1011 objects
, max_objects
, 0 );
1019 parse_private( T1_Face face
,
1024 loader
->keywords_encountered
|= T1_PRIVATE
;
1029 read_binary_data( T1_Parser parser
,
1034 FT_Byte
* limit
= parser
->root
.limit
;
1037 /* the binary data has one of the following formats */
1039 /* `size' [white*] RD white ....... ND */
1040 /* `size' [white*] -| white ....... |- */
1043 T1_Skip_Spaces( parser
);
1045 cur
= parser
->root
.cursor
;
1047 if ( cur
< limit
&& ft_isdigit( *cur
) )
1049 FT_Long s
= T1_ToInt( parser
);
1052 T1_Skip_PS_Token( parser
); /* `RD' or `-|' or something else */
1054 /* there is only one whitespace char after the */
1055 /* `RD' or `-|' token */
1056 *base
= parser
->root
.cursor
+ 1;
1058 if ( s
>= 0 && s
< limit
- *base
)
1060 parser
->root
.cursor
+= s
+ 1;
1062 return !parser
->root
.error
;
1066 FT_ERROR(( "read_binary_data: invalid size field\n" ));
1067 parser
->root
.error
= T1_Err_Invalid_File_Format
;
1072 /* We now define the routines to handle the `/Encoding', `/Subrs', */
1073 /* and `/CharStrings' dictionaries. */
1076 parse_font_matrix( T1_Face face
,
1079 T1_Parser parser
= &loader
->parser
;
1080 FT_Matrix
* matrix
= &face
->type1
.font_matrix
;
1081 FT_Vector
* offset
= &face
->type1
.font_offset
;
1082 FT_Face root
= (FT_Face
)&face
->root
;
1084 FT_Fixed temp_scale
;
1088 result
= T1_ToFixedArray( parser
, 6, temp
, 3 );
1092 parser
->root
.error
= T1_Err_Invalid_File_Format
;
1096 temp_scale
= FT_ABS( temp
[3] );
1098 if ( temp_scale
== 0 )
1100 FT_ERROR(( "parse_font_matrix: invalid font matrix\n" ));
1101 parser
->root
.error
= T1_Err_Invalid_File_Format
;
1105 /* Set Units per EM based on FontMatrix values. We set the value to */
1106 /* 1000 / temp_scale, because temp_scale was already multiplied by */
1107 /* 1000 (in t1_tofixed, from psobjs.c). */
1109 root
->units_per_EM
= (FT_UShort
)( FT_DivFix( 1000 * 0x10000L
,
1110 temp_scale
) >> 16 );
1112 /* we need to scale the values by 1.0/temp_scale */
1113 if ( temp_scale
!= 0x10000L
)
1115 temp
[0] = FT_DivFix( temp
[0], temp_scale
);
1116 temp
[1] = FT_DivFix( temp
[1], temp_scale
);
1117 temp
[2] = FT_DivFix( temp
[2], temp_scale
);
1118 temp
[4] = FT_DivFix( temp
[4], temp_scale
);
1119 temp
[5] = FT_DivFix( temp
[5], temp_scale
);
1120 temp
[3] = temp
[3] < 0 ? -0x10000L
: 0x10000L
;
1123 matrix
->xx
= temp
[0];
1124 matrix
->yx
= temp
[1];
1125 matrix
->xy
= temp
[2];
1126 matrix
->yy
= temp
[3];
1128 /* note that the offsets must be expressed in integer font units */
1129 offset
->x
= temp
[4] >> 16;
1130 offset
->y
= temp
[5] >> 16;
1135 parse_encoding( T1_Face face
,
1138 T1_Parser parser
= &loader
->parser
;
1140 FT_Byte
* limit
= parser
->root
.limit
;
1142 PSAux_Service psaux
= (PSAux_Service
)face
->psaux
;
1145 T1_Skip_Spaces( parser
);
1146 cur
= parser
->root
.cursor
;
1149 FT_ERROR(( "parse_encoding: out of bounds\n" ));
1150 parser
->root
.error
= T1_Err_Invalid_File_Format
;
1154 /* if we have a number or `[', the encoding is an array, */
1155 /* and we must load it now */
1156 if ( ft_isdigit( *cur
) || *cur
== '[' )
1158 T1_Encoding encode
= &face
->type1
.encoding
;
1160 PS_Table char_table
= &loader
->encoding_table
;
1161 FT_Memory memory
= parser
->root
.memory
;
1163 FT_Bool only_immediates
= 0;
1166 /* read the number of entries in the encoding; should be 256 */
1170 only_immediates
= 1;
1171 parser
->root
.cursor
++;
1174 count
= (FT_Int
)T1_ToInt( parser
);
1176 T1_Skip_Spaces( parser
);
1177 if ( parser
->root
.cursor
>= limit
)
1180 /* we use a T1_Table to store our charnames */
1181 loader
->num_chars
= encode
->num_chars
= count
;
1182 if ( FT_NEW_ARRAY( encode
->char_index
, count
) ||
1183 FT_NEW_ARRAY( encode
->char_name
, count
) ||
1184 FT_SET_ERROR( psaux
->ps_table_funcs
->init(
1185 char_table
, count
, memory
) ) )
1187 parser
->root
.error
= error
;
1191 /* We need to `zero' out encoding_table.elements */
1192 for ( n
= 0; n
< count
; n
++ )
1194 char* notdef
= (char *)".notdef";
1197 T1_Add_Table( char_table
, n
, notdef
, 8 );
1200 /* Now we need to read records of the form */
1202 /* ... charcode /charname ... */
1204 /* for each entry in our table. */
1206 /* We simply look for a number followed by an immediate */
1207 /* name. Note that this ignores correctly the sequence */
1208 /* that is often seen in type1 fonts: */
1210 /* 0 1 255 { 1 index exch /.notdef put } for dup */
1212 /* used to clean the encoding array before anything else. */
1214 /* Alternatively, if the array is directly given as */
1216 /* /Encoding [ ... ] */
1218 /* we only read immediates. */
1221 T1_Skip_Spaces( parser
);
1223 while ( parser
->root
.cursor
< limit
)
1225 cur
= parser
->root
.cursor
;
1227 /* we stop when we encounter a `def' or `]' */
1228 if ( *cur
== 'd' && cur
+ 3 < limit
)
1230 if ( cur
[1] == 'e' &&
1232 IS_PS_DELIM( cur
[3] ) )
1234 FT_TRACE6(( "encoding end\n" ));
1241 FT_TRACE6(( "encoding end\n" ));
1246 /* check whether we've found an entry */
1247 if ( ft_isdigit( *cur
) || only_immediates
)
1252 if ( only_immediates
)
1256 charcode
= (FT_Int
)T1_ToInt( parser
);
1257 T1_Skip_Spaces( parser
);
1260 cur
= parser
->root
.cursor
;
1262 if ( *cur
== '/' && cur
+ 2 < limit
&& n
< count
)
1269 parser
->root
.cursor
= cur
;
1270 T1_Skip_PS_Token( parser
);
1271 if ( parser
->root
.error
)
1274 len
= parser
->root
.cursor
- cur
;
1276 parser
->root
.error
= T1_Add_Table( char_table
, charcode
,
1278 if ( parser
->root
.error
)
1280 char_table
->elements
[charcode
][len
] = '\0';
1284 else if ( only_immediates
)
1286 /* Since the current position is not updated for */
1287 /* immediates-only mode we would get an infinite loop if */
1288 /* we don't do anything here. */
1290 /* This encoding array is not valid according to the type1 */
1291 /* specification (it might be an encoding for a CID type1 */
1292 /* font, however), so we conclude that this font is NOT a */
1294 parser
->root
.error
= FT_Err_Unknown_File_Format
;
1300 T1_Skip_PS_Token( parser
);
1301 if ( parser
->root
.error
)
1305 T1_Skip_Spaces( parser
);
1308 face
->type1
.encoding_type
= T1_ENCODING_TYPE_ARRAY
;
1309 parser
->root
.cursor
= cur
;
1312 /* Otherwise, we should have either `StandardEncoding', */
1313 /* `ExpertEncoding', or `ISOLatin1Encoding' */
1316 if ( cur
+ 17 < limit
&&
1317 ft_strncmp( (const char*)cur
, "StandardEncoding", 16 ) == 0 )
1318 face
->type1
.encoding_type
= T1_ENCODING_TYPE_STANDARD
;
1320 else if ( cur
+ 15 < limit
&&
1321 ft_strncmp( (const char*)cur
, "ExpertEncoding", 14 ) == 0 )
1322 face
->type1
.encoding_type
= T1_ENCODING_TYPE_EXPERT
;
1324 else if ( cur
+ 18 < limit
&&
1325 ft_strncmp( (const char*)cur
, "ISOLatin1Encoding", 17 ) == 0 )
1326 face
->type1
.encoding_type
= T1_ENCODING_TYPE_ISOLATIN1
;
1329 parser
->root
.error
= T1_Err_Ignore
;
1335 parse_subrs( T1_Face face
,
1338 T1_Parser parser
= &loader
->parser
;
1339 PS_Table table
= &loader
->subrs
;
1340 FT_Memory memory
= parser
->root
.memory
;
1344 PSAux_Service psaux
= (PSAux_Service
)face
->psaux
;
1347 T1_Skip_Spaces( parser
);
1349 /* test for empty array */
1350 if ( parser
->root
.cursor
< parser
->root
.limit
&&
1351 *parser
->root
.cursor
== '[' )
1353 T1_Skip_PS_Token( parser
);
1354 T1_Skip_Spaces ( parser
);
1355 if ( parser
->root
.cursor
>= parser
->root
.limit
||
1356 *parser
->root
.cursor
!= ']' )
1357 parser
->root
.error
= T1_Err_Invalid_File_Format
;
1361 num_subrs
= (FT_Int
)T1_ToInt( parser
);
1363 /* position the parser right before the `dup' of the first subr */
1364 T1_Skip_PS_Token( parser
); /* `array' */
1365 if ( parser
->root
.error
)
1367 T1_Skip_Spaces( parser
);
1369 /* initialize subrs array -- with synthetic fonts it is possible */
1370 /* we get here twice */
1371 if ( !loader
->num_subrs
)
1373 error
= psaux
->ps_table_funcs
->init( table
, num_subrs
, memory
);
1378 /* the format is simple: */
1380 /* `index' + binary data */
1388 /* If the next token isn't `dup' we are done. */
1389 if ( ft_strncmp( (char*)parser
->root
.cursor
, "dup", 3 ) != 0 )
1392 T1_Skip_PS_Token( parser
); /* `dup' */
1394 idx
= T1_ToInt( parser
);
1396 if ( !read_binary_data( parser
, &size
, &base
) )
1399 /* The binary string is followed by one token, e.g. `NP' */
1400 /* (bound to `noaccess put') or by two separate tokens: */
1401 /* `noaccess' & `put'. We position the parser right */
1402 /* before the next `dup', if any. */
1403 T1_Skip_PS_Token( parser
); /* `NP' or `|' or `noaccess' */
1404 if ( parser
->root
.error
)
1406 T1_Skip_Spaces ( parser
);
1408 if ( ft_strncmp( (char*)parser
->root
.cursor
, "put", 3 ) == 0 )
1410 T1_Skip_PS_Token( parser
); /* skip `put' */
1411 T1_Skip_Spaces ( parser
);
1414 /* with synthetic fonts it is possible we get here twice */
1415 if ( loader
->num_subrs
)
1418 /* some fonts use a value of -1 for lenIV to indicate that */
1419 /* the charstrings are unencoded */
1421 /* thanks to Tom Kacvinsky for pointing this out */
1423 if ( face
->type1
.private_dict
.lenIV
>= 0 )
1428 /* some fonts define empty subr records -- this is not totally */
1429 /* compliant to the specification (which says they should at */
1430 /* least contain a `return'), but we support them anyway */
1431 if ( size
< face
->type1
.private_dict
.lenIV
)
1433 error
= T1_Err_Invalid_File_Format
;
1437 /* t1_decrypt() shouldn't write to base -- make temporary copy */
1438 if ( FT_ALLOC( temp
, size
) )
1440 FT_MEM_COPY( temp
, base
, size
);
1441 psaux
->t1_decrypt( temp
, size
, 4330 );
1442 size
-= face
->type1
.private_dict
.lenIV
;
1443 error
= T1_Add_Table( table
, (FT_Int
)idx
,
1444 temp
+ face
->type1
.private_dict
.lenIV
, size
);
1448 error
= T1_Add_Table( table
, (FT_Int
)idx
, base
, size
);
1453 if ( !loader
->num_subrs
)
1454 loader
->num_subrs
= num_subrs
;
1459 parser
->root
.error
= error
;
1463 #define TABLE_EXTEND 5
1467 parse_charstrings( T1_Face face
,
1470 T1_Parser parser
= &loader
->parser
;
1471 PS_Table code_table
= &loader
->charstrings
;
1472 PS_Table name_table
= &loader
->glyph_names
;
1473 PS_Table swap_table
= &loader
->swap_table
;
1474 FT_Memory memory
= parser
->root
.memory
;
1477 PSAux_Service psaux
= (PSAux_Service
)face
->psaux
;
1480 FT_Byte
* limit
= parser
->root
.limit
;
1481 FT_Int n
, num_glyphs
;
1482 FT_UInt notdef_index
= 0;
1483 FT_Byte notdef_found
= 0;
1486 num_glyphs
= (FT_Int
)T1_ToInt( parser
);
1487 /* some fonts like Optima-Oblique not only define the /CharStrings */
1488 /* array but access it also */
1489 if ( num_glyphs
== 0 || parser
->root
.error
)
1492 /* initialize tables, leaving space for addition of .notdef, */
1493 /* if necessary, and a few other glyphs to handle buggy */
1494 /* fonts which have more glyphs than specified. */
1496 /* for some non-standard fonts like `Optima' which provides */
1497 /* different outlines depending on the resolution it is */
1498 /* possible to get here twice */
1499 if ( !loader
->num_glyphs
)
1501 error
= psaux
->ps_table_funcs
->init(
1502 code_table
, num_glyphs
+ 1 + TABLE_EXTEND
, memory
);
1506 error
= psaux
->ps_table_funcs
->init(
1507 name_table
, num_glyphs
+ 1 + TABLE_EXTEND
, memory
);
1511 /* Initialize table for swapping index notdef_index and */
1512 /* index 0 names and codes (if necessary). */
1514 error
= psaux
->ps_table_funcs
->init( swap_table
, 4, memory
);
1527 /* the format is simple: */
1528 /* `/glyphname' + binary data */
1530 T1_Skip_Spaces( parser
);
1532 cur
= parser
->root
.cursor
;
1536 /* we stop when we find a `def' or `end' keyword */
1537 if ( cur
+ 3 < limit
&& IS_PS_DELIM( cur
[3] ) )
1539 if ( cur
[0] == 'd' &&
1543 /* There are fonts which have this: */
1545 /* /CharStrings 118 dict def */
1547 /* CharStrings begin */
1550 /* To catch this we ignore `def' if */
1551 /* no charstring has actually been */
1557 if ( cur
[0] == 'e' &&
1563 T1_Skip_PS_Token( parser
);
1564 if ( parser
->root
.error
)
1572 if ( cur
+ 1 >= limit
)
1574 error
= T1_Err_Invalid_File_Format
;
1578 cur
++; /* skip `/' */
1579 len
= parser
->root
.cursor
- cur
;
1581 if ( !read_binary_data( parser
, &size
, &base
) )
1584 /* for some non-standard fonts like `Optima' which provides */
1585 /* different outlines depending on the resolution it is */
1586 /* possible to get here twice */
1587 if ( loader
->num_glyphs
)
1590 error
= T1_Add_Table( name_table
, n
, cur
, len
+ 1 );
1594 /* add a trailing zero to the name table */
1595 name_table
->elements
[n
][len
] = '\0';
1597 /* record index of /.notdef */
1599 ft_strcmp( ".notdef",
1600 (const char*)(name_table
->elements
[n
]) ) == 0 )
1606 if ( face
->type1
.private_dict
.lenIV
>= 0 &&
1607 n
< num_glyphs
+ TABLE_EXTEND
)
1612 if ( size
<= face
->type1
.private_dict
.lenIV
)
1614 error
= T1_Err_Invalid_File_Format
;
1618 /* t1_decrypt() shouldn't write to base -- make temporary copy */
1619 if ( FT_ALLOC( temp
, size
) )
1621 FT_MEM_COPY( temp
, base
, size
);
1622 psaux
->t1_decrypt( temp
, size
, 4330 );
1623 size
-= face
->type1
.private_dict
.lenIV
;
1624 error
= T1_Add_Table( code_table
, n
,
1625 temp
+ face
->type1
.private_dict
.lenIV
, size
);
1629 error
= T1_Add_Table( code_table
, n
, base
, size
);
1637 loader
->num_glyphs
= n
;
1639 /* if /.notdef is found but does not occupy index 0, do our magic. */
1640 if ( notdef_found
&&
1641 ft_strcmp( ".notdef", (const char*)name_table
->elements
[0] ) )
1643 /* Swap glyph in index 0 with /.notdef glyph. First, add index 0 */
1644 /* name and code entries to swap_table. Then place notdef_index */
1645 /* name and code entries into swap_table. Then swap name and code */
1646 /* entries at indices notdef_index and 0 using values stored in */
1650 error
= T1_Add_Table( swap_table
, 0,
1651 name_table
->elements
[0],
1652 name_table
->lengths
[0] );
1657 error
= T1_Add_Table( swap_table
, 1,
1658 code_table
->elements
[0],
1659 code_table
->lengths
[0] );
1663 /* Index notdef_index name */
1664 error
= T1_Add_Table( swap_table
, 2,
1665 name_table
->elements
[notdef_index
],
1666 name_table
->lengths
[notdef_index
] );
1670 /* Index notdef_index code */
1671 error
= T1_Add_Table( swap_table
, 3,
1672 code_table
->elements
[notdef_index
],
1673 code_table
->lengths
[notdef_index
] );
1677 error
= T1_Add_Table( name_table
, notdef_index
,
1678 swap_table
->elements
[0],
1679 swap_table
->lengths
[0] );
1683 error
= T1_Add_Table( code_table
, notdef_index
,
1684 swap_table
->elements
[1],
1685 swap_table
->lengths
[1] );
1689 error
= T1_Add_Table( name_table
, 0,
1690 swap_table
->elements
[2],
1691 swap_table
->lengths
[2] );
1695 error
= T1_Add_Table( code_table
, 0,
1696 swap_table
->elements
[3],
1697 swap_table
->lengths
[3] );
1702 else if ( !notdef_found
)
1704 /* notdef_index is already 0, or /.notdef is undefined in */
1705 /* charstrings dictionary. Worry about /.notdef undefined. */
1706 /* We take index 0 and add it to the end of the table(s) */
1707 /* and add our own /.notdef glyph to index 0. */
1709 /* 0 333 hsbw endchar */
1710 FT_Byte notdef_glyph
[] = { 0x8B, 0xF7, 0xE1, 0x0D, 0x0E };
1711 char* notdef_name
= (char *)".notdef";
1714 error
= T1_Add_Table( swap_table
, 0,
1715 name_table
->elements
[0],
1716 name_table
->lengths
[0] );
1720 error
= T1_Add_Table( swap_table
, 1,
1721 code_table
->elements
[0],
1722 code_table
->lengths
[0] );
1726 error
= T1_Add_Table( name_table
, 0, notdef_name
, 8 );
1730 error
= T1_Add_Table( code_table
, 0, notdef_glyph
, 5 );
1735 error
= T1_Add_Table( name_table
, n
,
1736 swap_table
->elements
[0],
1737 swap_table
->lengths
[0] );
1741 error
= T1_Add_Table( code_table
, n
,
1742 swap_table
->elements
[1],
1743 swap_table
->lengths
[1] );
1747 /* we added a glyph. */
1748 loader
->num_glyphs
+= 1;
1754 parser
->root
.error
= error
;
1758 /*************************************************************************/
1760 /* Define the token field static variables. This is a set of */
1761 /* T1_FieldRec variables. */
1763 /*************************************************************************/
1767 const T1_FieldRec t1_keywords
[] =
1770 #include "t1tokens.h"
1772 /* now add the special functions... */
1773 T1_FIELD_CALLBACK( "FontMatrix", parse_font_matrix
,
1774 T1_FIELD_DICT_FONTDICT
)
1775 T1_FIELD_CALLBACK( "Encoding", parse_encoding
,
1776 T1_FIELD_DICT_FONTDICT
)
1777 T1_FIELD_CALLBACK( "Subrs", parse_subrs
,
1778 T1_FIELD_DICT_PRIVATE
)
1779 T1_FIELD_CALLBACK( "CharStrings", parse_charstrings
,
1780 T1_FIELD_DICT_PRIVATE
)
1781 T1_FIELD_CALLBACK( "Private", parse_private
,
1782 T1_FIELD_DICT_FONTDICT
)
1784 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
1785 T1_FIELD_CALLBACK( "BlendDesignPositions", parse_blend_design_positions
,
1786 T1_FIELD_DICT_FONTDICT
)
1787 T1_FIELD_CALLBACK( "BlendDesignMap", parse_blend_design_map
,
1788 T1_FIELD_DICT_FONTDICT
)
1789 T1_FIELD_CALLBACK( "BlendAxisTypes", parse_blend_axis_types
,
1790 T1_FIELD_DICT_FONTDICT
)
1791 T1_FIELD_CALLBACK( "WeightVector", parse_weight_vector
,
1792 T1_FIELD_DICT_FONTDICT
)
1793 T1_FIELD_CALLBACK( "BuildCharArray", parse_buildchar
,
1794 T1_FIELD_DICT_PRIVATE
)
1797 { 0, T1_FIELD_LOCATION_CID_INFO
, T1_FIELD_TYPE_NONE
, 0, 0, 0, 0, 0, 0 }
1801 #define T1_FIELD_COUNT \
1802 ( sizeof ( t1_keywords ) / sizeof ( t1_keywords[0] ) )
1806 parse_dict( T1_Face face
,
1811 T1_Parser parser
= &loader
->parser
;
1812 FT_Byte
*limit
, *start_binary
= NULL
;
1813 FT_Bool have_integer
= 0;
1816 parser
->root
.cursor
= base
;
1817 parser
->root
.limit
= base
+ size
;
1818 parser
->root
.error
= T1_Err_Ok
;
1820 limit
= parser
->root
.limit
;
1822 T1_Skip_Spaces( parser
);
1824 while ( parser
->root
.cursor
< limit
)
1829 cur
= parser
->root
.cursor
;
1831 /* look for `eexec' */
1832 if ( IS_PS_TOKEN( cur
, limit
, "eexec" ) )
1835 /* look for `closefile' which ends the eexec section */
1836 else if ( IS_PS_TOKEN( cur
, limit
, "closefile" ) )
1839 /* in a synthetic font the base font starts after a */
1840 /* `FontDictionary' token that is placed after a Private dict */
1841 else if ( IS_PS_TOKEN( cur
, limit
, "FontDirectory" ) )
1843 if ( loader
->keywords_encountered
& T1_PRIVATE
)
1844 loader
->keywords_encountered
|=
1845 T1_FONTDIR_AFTER_PRIVATE
;
1846 parser
->root
.cursor
+= 13;
1849 /* check whether we have an integer */
1850 else if ( ft_isdigit( *cur
) )
1853 T1_Skip_PS_Token( parser
);
1854 if ( parser
->root
.error
)
1859 /* in valid Type 1 fonts we don't see `RD' or `-|' directly */
1860 /* since those tokens are handled by parse_subrs and */
1861 /* parse_charstrings */
1862 else if ( *cur
== 'R' && cur
+ 6 < limit
&& *(cur
+ 1) == 'D' &&
1869 parser
->root
.cursor
= start_binary
;
1870 if ( !read_binary_data( parser
, &s
, &b
) )
1871 return T1_Err_Invalid_File_Format
;
1875 else if ( *cur
== '-' && cur
+ 6 < limit
&& *(cur
+ 1) == '|' &&
1882 parser
->root
.cursor
= start_binary
;
1883 if ( !read_binary_data( parser
, &s
, &b
) )
1884 return T1_Err_Invalid_File_Format
;
1888 /* look for immediates */
1889 else if ( *cur
== '/' && cur
+ 2 < limit
)
1896 parser
->root
.cursor
= cur
;
1897 T1_Skip_PS_Token( parser
);
1898 if ( parser
->root
.error
)
1901 len
= parser
->root
.cursor
- cur
;
1903 if ( len
> 0 && len
< 22 && parser
->root
.cursor
< limit
)
1905 /* now compare the immediate name to the keyword table */
1906 T1_Field keyword
= (T1_Field
)t1_keywords
;
1914 name
= (FT_Byte
*)keyword
->ident
;
1918 if ( cur
[0] == name
[0] &&
1919 len
== (FT_PtrDist
)ft_strlen( (const char *)name
) &&
1920 ft_memcmp( cur
, name
, len
) == 0 )
1922 /* We found it -- run the parsing callback! */
1923 /* We record every instance of every field */
1924 /* (until we reach the base font of a */
1925 /* synthetic font) to deal adequately with */
1926 /* multiple master fonts; this is also */
1927 /* necessary because later PostScript */
1928 /* definitions override earlier ones. */
1930 /* Once we encounter `FontDirectory' after */
1931 /* `/Private', we know that this is a synthetic */
1932 /* font; except for `/CharStrings' we are not */
1933 /* interested in anything that follows this */
1934 /* `FontDirectory'. */
1936 /* MM fonts have more than one /Private token at */
1937 /* the top level; let's hope that all the junk */
1938 /* that follows the first /Private token is not */
1939 /* interesting to us. */
1941 /* According to Adobe Tech Note #5175 (CID-Keyed */
1942 /* Font Installation for ATM Software) a `begin' */
1943 /* must be followed by exactly one `end', and */
1944 /* `begin' -- `end' pairs must be accurately */
1945 /* paired. We could use this to distinguish */
1946 /* between the global Private and the Private */
1947 /* dict that is a member of the Blend dict. */
1949 const FT_UInt dict
=
1950 ( loader
->keywords_encountered
& T1_PRIVATE
)
1951 ? T1_FIELD_DICT_PRIVATE
1952 : T1_FIELD_DICT_FONTDICT
;
1954 if ( !( dict
& keyword
->dict
) )
1956 FT_TRACE1(( "parse_dict: found %s but ignoring it "
1957 "since it is in the wrong dictionary\n",
1962 if ( !( loader
->keywords_encountered
&
1963 T1_FONTDIR_AFTER_PRIVATE
) ||
1964 ft_strcmp( (const char*)name
, "CharStrings" ) == 0 )
1966 parser
->root
.error
= t1_load_keyword( face
,
1969 if ( parser
->root
.error
!= T1_Err_Ok
)
1971 if ( FT_ERROR_BASE( parser
->root
.error
) == FT_Err_Ignore
)
1972 parser
->root
.error
= T1_Err_Ok
;
1974 return parser
->root
.error
;
1988 T1_Skip_PS_Token( parser
);
1989 if ( parser
->root
.error
)
1994 T1_Skip_Spaces( parser
);
1998 return parser
->root
.error
;
2003 t1_init_loader( T1_Loader loader
,
2008 FT_MEM_ZERO( loader
, sizeof ( *loader
) );
2009 loader
->num_glyphs
= 0;
2010 loader
->num_chars
= 0;
2012 /* initialize the tables -- simply set their `init' field to 0 */
2013 loader
->encoding_table
.init
= 0;
2014 loader
->charstrings
.init
= 0;
2015 loader
->glyph_names
.init
= 0;
2016 loader
->subrs
.init
= 0;
2017 loader
->swap_table
.init
= 0;
2018 loader
->fontdata
= 0;
2019 loader
->keywords_encountered
= 0;
2024 t1_done_loader( T1_Loader loader
)
2026 T1_Parser parser
= &loader
->parser
;
2029 /* finalize tables */
2030 T1_Release_Table( &loader
->encoding_table
);
2031 T1_Release_Table( &loader
->charstrings
);
2032 T1_Release_Table( &loader
->glyph_names
);
2033 T1_Release_Table( &loader
->swap_table
);
2034 T1_Release_Table( &loader
->subrs
);
2036 /* finalize parser */
2037 T1_Finalize_Parser( parser
);
2041 FT_LOCAL_DEF( FT_Error
)
2042 T1_Open_Face( T1_Face face
)
2044 T1_LoaderRec loader
;
2046 T1_Font type1
= &face
->type1
;
2047 PS_Private priv
= &type1
->private_dict
;
2050 PSAux_Service psaux
= (PSAux_Service
)face
->psaux
;
2053 t1_init_loader( &loader
, face
);
2055 /* default values */
2058 face
->len_buildchar
= 0;
2060 priv
->blue_shift
= 7;
2061 priv
->blue_fuzz
= 1;
2063 priv
->expansion_factor
= (FT_Fixed
)( 0.06 * 0x10000L
);
2064 priv
->blue_scale
= (FT_Fixed
)( 0.039625 * 0x10000L
* 1000 );
2066 parser
= &loader
.parser
;
2067 error
= T1_New_Parser( parser
,
2074 error
= parse_dict( face
, &loader
,
2075 parser
->base_dict
, parser
->base_len
);
2079 error
= T1_Get_Private_Dict( parser
, psaux
);
2083 error
= parse_dict( face
, &loader
,
2084 parser
->private_dict
, parser
->private_len
);
2088 /* ensure even-ness of `num_blue_values' */
2089 priv
->num_blue_values
&= ~1;
2091 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
2094 face
->blend
->num_default_design_vector
!= 0 &&
2095 face
->blend
->num_default_design_vector
!= face
->blend
->num_axis
)
2097 /* we don't use it currently so just warn, reset, and ignore */
2098 FT_ERROR(( "T1_Open_Face(): /DesignVector contains %u entries "
2099 "while there are %u axes.\n",
2100 face
->blend
->num_default_design_vector
,
2101 face
->blend
->num_axis
));
2103 face
->blend
->num_default_design_vector
= 0;
2106 /* the following can happen for MM instances; we then treat the */
2107 /* font as a normal PS font */
2109 ( !face
->blend
->num_designs
|| !face
->blend
->num_axis
) )
2110 T1_Done_Blend( face
);
2112 /* another safety check */
2118 for ( i
= 0; i
< face
->blend
->num_axis
; i
++ )
2119 if ( !face
->blend
->design_map
[i
].num_points
)
2121 T1_Done_Blend( face
);
2128 if ( face
->len_buildchar
> 0 )
2130 FT_Memory memory
= face
->root
.memory
;
2133 if ( FT_NEW_ARRAY( face
->buildchar
, face
->len_buildchar
) )
2135 FT_ERROR(( "T1_Open_Face: cannot allocate BuildCharArray\n" ));
2136 face
->len_buildchar
= 0;
2142 #endif /* T1_CONFIG_OPTION_NO_MM_SUPPORT */
2144 /* now, propagate the subrs, charstrings, and glyphnames tables */
2145 /* to the Type1 data */
2146 type1
->num_glyphs
= loader
.num_glyphs
;
2148 if ( loader
.subrs
.init
)
2150 loader
.subrs
.init
= 0;
2151 type1
->num_subrs
= loader
.num_subrs
;
2152 type1
->subrs_block
= loader
.subrs
.block
;
2153 type1
->subrs
= loader
.subrs
.elements
;
2154 type1
->subrs_len
= loader
.subrs
.lengths
;
2157 #ifdef FT_CONFIG_OPTION_INCREMENTAL
2158 if ( !face
->root
.internal
->incremental_interface
)
2160 if ( !loader
.charstrings
.init
)
2162 FT_ERROR(( "T1_Open_Face: no `/CharStrings' array in face\n" ));
2163 error
= T1_Err_Invalid_File_Format
;
2166 loader
.charstrings
.init
= 0;
2167 type1
->charstrings_block
= loader
.charstrings
.block
;
2168 type1
->charstrings
= loader
.charstrings
.elements
;
2169 type1
->charstrings_len
= loader
.charstrings
.lengths
;
2171 /* we copy the glyph names `block' and `elements' fields; */
2172 /* the `lengths' field must be released later */
2173 type1
->glyph_names_block
= loader
.glyph_names
.block
;
2174 type1
->glyph_names
= (FT_String
**)loader
.glyph_names
.elements
;
2175 loader
.glyph_names
.block
= 0;
2176 loader
.glyph_names
.elements
= 0;
2178 /* we must now build type1.encoding when we have a custom array */
2179 if ( type1
->encoding_type
== T1_ENCODING_TYPE_ARRAY
)
2181 FT_Int charcode
, idx
, min_char
, max_char
;
2183 FT_Byte
* glyph_name
;
2186 /* OK, we do the following: for each element in the encoding */
2187 /* table, look up the index of the glyph having the same name */
2188 /* the index is then stored in type1.encoding.char_index, and */
2189 /* the name to type1.encoding.char_name */
2195 for ( ; charcode
< loader
.encoding_table
.max_elems
; charcode
++ )
2197 type1
->encoding
.char_index
[charcode
] = 0;
2198 type1
->encoding
.char_name
[charcode
] = (char *)".notdef";
2200 char_name
= loader
.encoding_table
.elements
[charcode
];
2202 for ( idx
= 0; idx
< type1
->num_glyphs
; idx
++ )
2204 glyph_name
= (FT_Byte
*)type1
->glyph_names
[idx
];
2205 if ( ft_strcmp( (const char*)char_name
,
2206 (const char*)glyph_name
) == 0 )
2208 type1
->encoding
.char_index
[charcode
] = (FT_UShort
)idx
;
2209 type1
->encoding
.char_name
[charcode
] = (char*)glyph_name
;
2211 /* Change min/max encoded char only if glyph name is */
2213 if ( ft_strcmp( (const char*)".notdef",
2214 (const char*)glyph_name
) != 0 )
2216 if ( charcode
< min_char
)
2217 min_char
= charcode
;
2218 if ( charcode
>= max_char
)
2219 max_char
= charcode
+ 1;
2226 type1
->encoding
.code_first
= min_char
;
2227 type1
->encoding
.code_last
= max_char
;
2228 type1
->encoding
.num_chars
= loader
.num_chars
;
2232 t1_done_loader( &loader
);