1 /***************************************************************************/
2 /* */
3 /* cidload.c */
4 /* */
5 /* CID-keyed Type1 font loader (body). */
6 /* */
7 /* Copyright 1996-2001 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 #include <ft2build.h>
20 #include FT_INTERNAL_DEBUG_H
21 #include FT_CONFIG_CONFIG_H
22 #include FT_MULTIPLE_MASTERS_H
23 #include FT_INTERNAL_TYPE1_TYPES_H
25 #include "cidload.h"
27 #include "ciderrs.h"
29 #include <stdio.h>
30 #include <ctype.h> /* for isspace(), isalnum() */
33 /*************************************************************************/
34 /* */
35 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
36 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
37 /* messages during execution. */
38 /* */
39 #undef FT_COMPONENT
40 #define FT_COMPONENT trace_cidload
43 /* read a single offset */
44 FT_LOCAL_DEF FT_Long
45 cid_get_offset( FT_Byte** start,
46 FT_Byte offsize )
47 {
48 FT_Long result;
49 FT_Byte* p = *start;
52 for ( result = 0; offsize > 0; offsize-- )
53 {
54 result <<= 8;
55 result |= *p++;
56 }
58 *start = p;
59 return result;
60 }
63 FT_LOCAL_DEF void
64 cid_decrypt( FT_Byte* buffer,
65 FT_Offset length,
66 FT_UShort seed )
67 {
68 while ( length > 0 )
69 {
70 FT_Byte plain;
73 plain = (FT_Byte)( *buffer ^ ( seed >> 8 ) );
74 seed = (FT_UShort)( ( *buffer + seed ) * 52845U + 22719 );
75 *buffer++ = plain;
76 length--;
77 }
78 }
81 /*************************************************************************/
82 /*************************************************************************/
83 /***** *****/
84 /***** TYPE 1 SYMBOL PARSING *****/
85 /***** *****/
86 /*************************************************************************/
87 /*************************************************************************/
90 static FT_Error
91 cid_load_keyword( CID_Face face,
92 CID_Loader* loader,
93 const T1_Field* keyword )
94 {
95 FT_Error error;
96 CID_Parser* parser = &loader->parser;
97 FT_Byte* object;
98 void* dummy_object;
99 CID_Info* cid = &face->cid;
102 /* if the keyword has a dedicated callback, call it */
103 if ( keyword->type == t1_field_callback )
104 {
105 keyword->reader( (FT_Face)face, parser );
106 error = parser->root.error;
107 goto Exit;
108 }
110 /* we must now compute the address of our target object */
111 switch ( keyword->location )
112 {
113 case t1_field_cid_info:
114 object = (FT_Byte*)cid;
115 break;
117 case t1_field_font_info:
118 object = (FT_Byte*)&cid->font_info;
119 break;
121 default:
122 {
123 CID_FontDict* dict;
126 if ( parser->num_dict < 0 )
127 {
128 FT_ERROR(( "cid_load_keyword: invalid use of `%s'!\n",
129 keyword->ident ));
130 error = CID_Err_Syntax_Error;
131 goto Exit;
132 }
134 dict = cid->font_dicts + parser->num_dict;
135 switch ( keyword->location )
136 {
137 case t1_field_private:
138 object = (FT_Byte*)&dict->private_dict;
139 break;
141 default:
142 object = (FT_Byte*)dict;
143 }
144 }
145 }
147 dummy_object = object;
149 /* now, load the keyword data in the object's field(s) */
150 if ( keyword->type == t1_field_integer_array ||
151 keyword->type == t1_field_fixed_array )
152 error = CID_Load_Field_Table( &loader->parser, keyword,
153 &dummy_object );
154 else
155 error = CID_Load_Field( &loader->parser, keyword, &dummy_object );
156 Exit:
157 return error;
158 }
161 FT_CALLBACK_DEF( FT_Error )
162 parse_font_bbox( CID_Face face,
163 CID_Parser* parser )
164 {
165 FT_Fixed temp[4];
166 FT_BBox* bbox = &face->cid.font_bbox;
169 (void)CID_ToFixedArray( parser, 4, temp, 0 );
170 bbox->xMin = FT_RoundFix( temp[0] );
171 bbox->yMin = FT_RoundFix( temp[1] );
172 bbox->xMax = FT_RoundFix( temp[2] );
173 bbox->yMax = FT_RoundFix( temp[3] );
175 return CID_Err_Ok; /* this is a callback function; */
176 /* we must return an error code */
177 }
180 FT_CALLBACK_DEF( FT_Error )
181 parse_font_matrix( CID_Face face,
182 CID_Parser* parser )
183 {
184 FT_Matrix* matrix;
185 FT_Vector* offset;
186 CID_FontDict* dict;
187 FT_Face root = (FT_Face)&face->root;
188 FT_Fixed temp[6];
189 FT_Fixed temp_scale;
192 if ( parser->num_dict >= 0 )
193 {
194 dict = face->cid.font_dicts + parser->num_dict;
195 matrix = &dict->font_matrix;
196 offset = &dict->font_offset;
198 (void)CID_ToFixedArray( parser, 6, temp, 3 );
200 temp_scale = ABS( temp[3] );
202 /* Set Units per EM based on FontMatrix values. We set the value to */
203 /* `1000/temp_scale', because temp_scale was already multiplied by */
204 /* 1000 (in t1_tofixed(), from psobjs.c). */
205 root->units_per_EM = (FT_UShort)( FT_DivFix( 0x10000L,
206 FT_DivFix( temp_scale, 1000 ) ) );
208 /* we need to scale the values by 1.0/temp[3] */
209 if ( temp_scale != 0x10000L )
210 {
211 temp[0] = FT_DivFix( temp[0], temp_scale );
212 temp[1] = FT_DivFix( temp[1], temp_scale );
213 temp[2] = FT_DivFix( temp[2], temp_scale );
214 temp[4] = FT_DivFix( temp[4], temp_scale );
215 temp[5] = FT_DivFix( temp[5], temp_scale );
216 temp[3] = 0x10000L;
217 }
219 matrix->xx = temp[0];
220 matrix->yx = temp[1];
221 matrix->xy = temp[2];
222 matrix->yy = temp[3];
224 /* note that the font offsets are expressed in integer font units */
225 offset->x = temp[4] >> 16;
226 offset->y = temp[5] >> 16;
227 }
229 return CID_Err_Ok; /* this is a callback function; */
230 /* we must return an error code */
231 }
234 FT_CALLBACK_DEF( FT_Error )
235 parse_fd_array( CID_Face face,
236 CID_Parser* parser )
237 {
238 CID_Info* cid = &face->cid;
239 FT_Memory memory = face->root.memory;
240 FT_Error error = CID_Err_Ok;
241 FT_Long num_dicts;
244 num_dicts = CID_ToInt( parser );
246 if ( !cid->font_dicts )
247 {
248 FT_Int n;
251 if ( ALLOC_ARRAY( cid->font_dicts, num_dicts, CID_FontDict ) )
252 goto Exit;
254 cid->num_dicts = (FT_UInt)num_dicts;
256 /* don't forget to set a few defaults */
257 for ( n = 0; n < cid->num_dicts; n++ )
258 {
259 CID_FontDict* dict = cid->font_dicts + n;
262 /* default value for lenIV */
263 dict->private_dict.lenIV = 4;
264 }
265 }
267 Exit:
268 return error;
269 }
272 static
273 const T1_Field cid_field_records[] =
274 {
276 #include "cidtoken.h"
278 T1_FIELD_CALLBACK( "FontBBox", parse_font_bbox )
279 T1_FIELD_CALLBACK( "FDArray", parse_fd_array )
280 T1_FIELD_CALLBACK( "FontMatrix", parse_font_matrix )
281 { 0, t1_field_cid_info, t1_field_none, 0, 0, 0, 0, 0 }
282 };
285 static int
286 is_alpha( char c )
287 {
288 return ( isalnum( (int)c ) ||
289 c == '.' ||
290 c == '_' );
291 }
294 static FT_Error
295 cid_parse_dict( CID_Face face,
296 CID_Loader* loader,
297 FT_Byte* base,
298 FT_Long size )
299 {
300 CID_Parser* parser = &loader->parser;
303 parser->root.cursor = base;
304 parser->root.limit = base + size;
305 parser->root.error = 0;
307 {
308 FT_Byte* cur = base;
309 FT_Byte* limit = cur + size;
312 for ( ;cur < limit; cur++ )
313 {
314 /* look for `%ADOBeginFontDict' */
315 if ( *cur == '%' && cur + 20 < limit &&
316 strncmp( (char*)cur, "%ADOBeginFontDict", 17 ) == 0 )
317 {
318 cur += 17;
320 /* if /FDArray was found, then cid->num_dicts is > 0, and */
321 /* we can start increasing parser->num_dict */
322 if ( face->cid.num_dicts > 0 )
323 parser->num_dict++;
324 }
325 /* look for immediates */
326 else if ( *cur == '/' && cur + 2 < limit )
327 {
328 FT_Byte* cur2;
329 FT_Int len;
332 cur++;
334 cur2 = cur;
335 while ( cur2 < limit && is_alpha( *cur2 ) )
336 cur2++;
338 len = (FT_Int)( cur2 - cur );
339 if ( len > 0 && len < 22 )
340 {
341 /* now compare the immediate name to the keyword table */
342 const T1_Field* keyword = cid_field_records;
345 for (;;)
346 {
347 FT_Byte* name;
350 name = (FT_Byte*)keyword->ident;
351 if ( !name )
352 break;
354 if ( cur[0] == name[0] &&
355 len == (FT_Int)strlen( (const char*)name ) )
356 {
357 FT_Int n;
360 for ( n = 1; n < len; n++ )
361 if ( cur[n] != name[n] )
362 break;
364 if ( n >= len )
365 {
366 /* we found it - run the parsing callback */
367 parser->root.cursor = cur2;
368 CID_Skip_Spaces( parser );
369 parser->root.error = cid_load_keyword( face,
370 loader,
371 keyword );
372 if ( parser->root.error )
373 return parser->root.error;
375 cur = parser->root.cursor;
376 break;
377 }
378 }
379 keyword++;
380 }
381 }
382 }
383 }
384 }
385 return parser->root.error;
386 }
389 /* read the subrmap and the subrs of each font dict */
390 static FT_Error
391 cid_read_subrs( CID_Face face )
392 {
393 CID_Info* cid = &face->cid;
394 FT_Memory memory = face->root.memory;
395 FT_Stream stream = face->root.stream;
396 FT_Error error;
397 FT_Int n;
398 CID_Subrs* subr;
399 FT_UInt max_offsets = 0;
400 FT_ULong* offsets = 0;
403 if ( ALLOC_ARRAY( face->subrs, cid->num_dicts, CID_Subrs ) )
404 goto Exit;
406 subr = face->subrs;
407 for ( n = 0; n < cid->num_dicts; n++, subr++ )
408 {
409 CID_FontDict* dict = cid->font_dicts + n;
410 FT_Int lenIV = dict->private_dict.lenIV;
411 FT_UInt count, num_subrs = dict->num_subrs;
412 FT_ULong data_len;
413 FT_Byte* p;
416 /* reallocate offsets array if needed */
417 if ( num_subrs + 1 > max_offsets )
418 {
419 FT_UInt new_max = ( num_subrs + 1 + 3 ) & -4;
422 if ( REALLOC_ARRAY( offsets, max_offsets, new_max, FT_ULong ) )
423 goto Fail;
425 max_offsets = new_max;
426 }
428 /* read the subrmap's offsets */
429 if ( FILE_Seek( cid->data_offset + dict->subrmap_offset ) ||
430 ACCESS_Frame( ( num_subrs + 1 ) * dict->sd_bytes ) )
431 goto Fail;
433 p = (FT_Byte*)stream->cursor;
434 for ( count = 0; count <= num_subrs; count++ )
435 offsets[count] = cid_get_offset( &p, (FT_Byte)dict->sd_bytes );
437 FORGET_Frame();
439 /* now, compute the size of subrs charstrings, */
440 /* allocate, and read them */
441 data_len = offsets[num_subrs] - offsets[0];
443 if ( ALLOC_ARRAY( subr->code, num_subrs + 1, FT_Byte* ) ||
444 ALLOC( subr->code[0], data_len ) )
445 goto Fail;
447 if ( FILE_Seek( cid->data_offset + offsets[0] ) ||
448 FILE_Read( subr->code[0], data_len ) )
449 goto Exit;
451 /* set up pointers */
452 for ( count = 1; count <= num_subrs; count++ )
453 {
454 FT_UInt len;
457 len = offsets[count] - offsets[count - 1];
458 subr->code[count] = subr->code[count - 1] + len;
459 }
461 /* decrypt subroutines, but only if lenIV >= 0 */
462 if ( lenIV >= 0 )
463 {
464 for ( count = 0; count < num_subrs; count++ )
465 {
466 FT_UInt len;
469 len = offsets[count + 1] - offsets[count];
470 cid_decrypt( subr->code[count], len, 4330 );
471 }
472 }
474 subr->num_subrs = num_subrs;
475 }
477 Exit:
478 FREE( offsets );
479 return error;
481 Fail:
482 if ( face->subrs )
483 {
484 for ( n = 0; n < cid->num_dicts; n++ )
485 {
486 if ( face->subrs[n].code )
487 FREE( face->subrs[n].code[0] );
489 FREE( face->subrs[n].code );
490 }
491 FREE( face->subrs );
492 }
493 goto Exit;
494 }
497 static void
498 t1_init_loader( CID_Loader* loader,
499 CID_Face face )
500 {
501 FT_UNUSED( face );
503 MEM_Set( loader, 0, sizeof ( *loader ) );
504 }
507 static void
508 t1_done_loader( CID_Loader* loader )
509 {
510 CID_Parser* parser = &loader->parser;
513 /* finalize parser */
514 CID_Done_Parser( parser );
515 }
518 FT_LOCAL_DEF FT_Error
519 CID_Open_Face( CID_Face face )
520 {
521 CID_Loader loader;
522 CID_Parser* parser;
523 FT_Error error;
526 t1_init_loader( &loader, face );
528 parser = &loader.parser;
529 error = CID_New_Parser( parser, face->root.stream, face->root.memory,
530 (PSAux_Interface*)face->psaux );
531 if ( error )
532 goto Exit;
534 error = cid_parse_dict( face, &loader,
535 parser->postscript,
536 parser->postscript_len );
537 if ( error )
538 goto Exit;
540 face->cid.data_offset = loader.parser.data_offset;
541 error = cid_read_subrs( face );
543 Exit:
544 t1_done_loader( &loader );
545 return error;
546 }
549 /* END */