The BIG graph update
[rrdtool.git] / libraries / freetype-2.0.5 / cidload.c
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 /***************************************************************************/
17
18
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
24
25 #include "cidload.h"
26
27 #include "ciderrs.h"
28
29 #include <stdio.h>
30 #include <ctype.h>  /* for isspace(), isalnum() */
31
32
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
41
42
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;
50
51
52     for ( result = 0; offsize > 0; offsize-- )
53     {
54       result <<= 8;
55       result  |= *p++;
56     }
57
58     *start = p;
59     return result;
60   }
61
62
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;
71
72
73       plain     = (FT_Byte)( *buffer ^ ( seed >> 8 ) );
74       seed      = (FT_UShort)( ( *buffer + seed ) * 52845U + 22719 );
75       *buffer++ = plain;
76       length--;
77     }
78   }
79
80
81   /*************************************************************************/
82   /*************************************************************************/
83   /*****                                                               *****/
84   /*****                    TYPE 1 SYMBOL PARSING                      *****/
85   /*****                                                               *****/
86   /*************************************************************************/
87   /*************************************************************************/
88
89
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;
100
101
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     }
109
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;
116
117     case t1_field_font_info:
118       object = (FT_Byte*)&cid->font_info;
119       break;
120
121     default:
122       {
123         CID_FontDict*  dict;
124
125
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         }
133
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;
140
141         default:
142           object = (FT_Byte*)dict;
143         }
144       }
145     }
146
147     dummy_object = object;
148
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   }
159
160
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;
167
168
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] );
174
175     return CID_Err_Ok;       /* this is a callback function; */
176                             /* we must return an error code */
177   }
178
179
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;
190
191
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;
197
198       (void)CID_ToFixedArray( parser, 6, temp, 3 );
199
200       temp_scale = ABS( temp[3] );
201
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 ) ) );
207
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       }
218
219       matrix->xx = temp[0];
220       matrix->yx = temp[1];
221       matrix->xy = temp[2];
222       matrix->yy = temp[3];
223
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     }
228
229     return CID_Err_Ok;       /* this is a callback function; */
230                             /* we must return an error code */
231   }
232
233
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;
242
243
244     num_dicts = CID_ToInt( parser );
245
246     if ( !cid->font_dicts )
247     {
248       FT_Int  n;
249
250
251       if ( ALLOC_ARRAY( cid->font_dicts, num_dicts, CID_FontDict ) )
252         goto Exit;
253
254       cid->num_dicts = (FT_UInt)num_dicts;
255
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;
260
261
262         /* default value for lenIV */
263         dict->private_dict.lenIV = 4;
264       }
265     }
266
267   Exit:
268     return error;
269   }
270
271
272   static
273   const T1_Field  cid_field_records[] =
274   {
275
276 #include "cidtoken.h"
277
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   };
283
284
285   static int
286   is_alpha( char  c )
287   {
288     return ( isalnum( (int)c ) ||
289              c == '.'          ||
290              c == '_'          );
291   }
292
293
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;
301
302
303     parser->root.cursor = base;
304     parser->root.limit  = base + size;
305     parser->root.error  = 0;
306
307     {
308       FT_Byte*  cur   = base;
309       FT_Byte*  limit = cur + size;
310
311
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;
319
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;
330
331
332           cur++;
333
334           cur2 = cur;
335           while ( cur2 < limit && is_alpha( *cur2 ) )
336             cur2++;
337
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;
343
344
345             for (;;)
346             {
347               FT_Byte*  name;
348
349
350               name = (FT_Byte*)keyword->ident;
351               if ( !name )
352                 break;
353
354               if ( cur[0] == name[0]                          &&
355                    len == (FT_Int)strlen( (const char*)name ) )
356               {
357                 FT_Int  n;
358
359
360                 for ( n = 1; n < len; n++ )
361                   if ( cur[n] != name[n] )
362                     break;
363
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;
374
375                   cur = parser->root.cursor;
376                   break;
377                 }
378               }
379               keyword++;
380             }
381           }
382         }
383       }
384     }
385     return parser->root.error;
386   }
387
388
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;
401
402
403     if ( ALLOC_ARRAY( face->subrs, cid->num_dicts, CID_Subrs ) )
404       goto Exit;
405
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;
414
415
416       /* reallocate offsets array if needed */
417       if ( num_subrs + 1 > max_offsets )
418       {
419         FT_UInt  new_max = ( num_subrs + 1 + 3 ) & -4;
420
421
422         if ( REALLOC_ARRAY( offsets, max_offsets, new_max, FT_ULong ) )
423           goto Fail;
424
425         max_offsets = new_max;
426       }
427
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;
432
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 );
436
437       FORGET_Frame();
438
439       /* now, compute the size of subrs charstrings, */
440       /* allocate, and read them                     */
441       data_len = offsets[num_subrs] - offsets[0];
442
443       if ( ALLOC_ARRAY( subr->code, num_subrs + 1, FT_Byte* ) ||
444            ALLOC( subr->code[0], data_len )                   )
445         goto Fail;
446
447       if ( FILE_Seek( cid->data_offset + offsets[0] ) ||
448            FILE_Read( subr->code[0], data_len )  )
449         goto Exit;
450
451       /* set up pointers */
452       for ( count = 1; count <= num_subrs; count++ )
453       {
454         FT_UInt  len;
455
456
457         len               = offsets[count] - offsets[count - 1];
458         subr->code[count] = subr->code[count - 1] + len;
459       }
460
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;
467
468
469           len = offsets[count + 1] - offsets[count];
470           cid_decrypt( subr->code[count], len, 4330 );
471         }
472       }
473
474       subr->num_subrs = num_subrs;
475     }
476
477   Exit:
478     FREE( offsets );
479     return error;
480
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] );
488
489         FREE( face->subrs[n].code );
490       }
491       FREE( face->subrs );
492     }
493     goto Exit;
494   }
495
496
497   static void
498   t1_init_loader( CID_Loader*  loader,
499                   CID_Face     face )
500   {
501     FT_UNUSED( face );
502
503     MEM_Set( loader, 0, sizeof ( *loader ) );
504   }
505
506
507   static void
508   t1_done_loader( CID_Loader*  loader )
509   {
510     CID_Parser*  parser = &loader->parser;
511
512
513     /* finalize parser */
514     CID_Done_Parser( parser );
515   }
516
517
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;
524
525
526     t1_init_loader( &loader, face );
527
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;
533
534     error = cid_parse_dict( face, &loader,
535                             parser->postscript,
536                             parser->postscript_len );
537     if ( error )
538       goto Exit;
539
540     face->cid.data_offset = loader.parser.data_offset;
541     error = cid_read_subrs( face );
542
543   Exit:
544     t1_done_loader( &loader );
545     return error;
546   }
547
548
549 /* END */