- Create another branch for networking fixes
[reactos.git] / lib / 3rdparty / freetype / src / cid / cidload.c
1 /***************************************************************************/
2 /* */
3 /* cidload.c */
4 /* */
5 /* CID-keyed Type1 font loader (body). */
6 /* */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006 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
30 /*************************************************************************/
31 /* */
32 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
33 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
34 /* messages during execution. */
35 /* */
36 #undef FT_COMPONENT
37 #define FT_COMPONENT trace_cidload
38
39
40 /* read a single offset */
41 FT_LOCAL_DEF( FT_Long )
42 cid_get_offset( FT_Byte* *start,
43 FT_Byte offsize )
44 {
45 FT_Long result;
46 FT_Byte* p = *start;
47
48
49 for ( result = 0; offsize > 0; offsize-- )
50 {
51 result <<= 8;
52 result |= *p++;
53 }
54
55 *start = p;
56 return result;
57 }
58
59
60 /*************************************************************************/
61 /*************************************************************************/
62 /***** *****/
63 /***** TYPE 1 SYMBOL PARSING *****/
64 /***** *****/
65 /*************************************************************************/
66 /*************************************************************************/
67
68
69 static FT_Error
70 cid_load_keyword( CID_Face face,
71 CID_Loader* loader,
72 const T1_Field keyword )
73 {
74 FT_Error error;
75 CID_Parser* parser = &loader->parser;
76 FT_Byte* object;
77 void* dummy_object;
78 CID_FaceInfo cid = &face->cid;
79
80
81 /* if the keyword has a dedicated callback, call it */
82 if ( keyword->type == T1_FIELD_TYPE_CALLBACK )
83 {
84 keyword->reader( (FT_Face)face, parser );
85 error = parser->root.error;
86 goto Exit;
87 }
88
89 /* we must now compute the address of our target object */
90 switch ( keyword->location )
91 {
92 case T1_FIELD_LOCATION_CID_INFO:
93 object = (FT_Byte*)cid;
94 break;
95
96 case T1_FIELD_LOCATION_FONT_INFO:
97 object = (FT_Byte*)&cid->font_info;
98 break;
99
100 case T1_FIELD_LOCATION_BBOX:
101 object = (FT_Byte*)&cid->font_bbox;
102 break;
103
104 default:
105 {
106 CID_FaceDict dict;
107
108
109 if ( parser->num_dict < 0 )
110 {
111 FT_ERROR(( "cid_load_keyword: invalid use of `%s'!\n",
112 keyword->ident ));
113 error = CID_Err_Syntax_Error;
114 goto Exit;
115 }
116
117 dict = cid->font_dicts + parser->num_dict;
118 switch ( keyword->location )
119 {
120 case T1_FIELD_LOCATION_PRIVATE:
121 object = (FT_Byte*)&dict->private_dict;
122 break;
123
124 default:
125 object = (FT_Byte*)dict;
126 }
127 }
128 }
129
130 dummy_object = object;
131
132 /* now, load the keyword data in the object's field(s) */
133 if ( keyword->type == T1_FIELD_TYPE_INTEGER_ARRAY ||
134 keyword->type == T1_FIELD_TYPE_FIXED_ARRAY )
135 error = cid_parser_load_field_table( &loader->parser, keyword,
136 &dummy_object );
137 else
138 error = cid_parser_load_field( &loader->parser,
139 keyword, &dummy_object );
140 Exit:
141 return error;
142 }
143
144
145 FT_CALLBACK_DEF( FT_Error )
146 parse_font_matrix( CID_Face face,
147 CID_Parser* parser )
148 {
149 FT_Matrix* matrix;
150 FT_Vector* offset;
151 CID_FaceDict dict;
152 FT_Face root = (FT_Face)&face->root;
153 FT_Fixed temp[6];
154 FT_Fixed temp_scale;
155
156
157 if ( parser->num_dict >= 0 )
158 {
159 dict = face->cid.font_dicts + parser->num_dict;
160 matrix = &dict->font_matrix;
161 offset = &dict->font_offset;
162
163 (void)cid_parser_to_fixed_array( parser, 6, temp, 3 );
164
165 temp_scale = FT_ABS( temp[3] );
166
167 /* Set units per EM based on FontMatrix values. We set the value to */
168 /* `1000/temp_scale', because temp_scale was already multiplied by */
169 /* 1000 (in `t1_tofixed', from psobjs.c). */
170 root->units_per_EM = (FT_UShort)( FT_DivFix( 0x10000L,
171 FT_DivFix( temp_scale, 1000 ) ) );
172
173 /* we need to scale the values by 1.0/temp[3] */
174 if ( temp_scale != 0x10000L )
175 {
176 temp[0] = FT_DivFix( temp[0], temp_scale );
177 temp[1] = FT_DivFix( temp[1], temp_scale );
178 temp[2] = FT_DivFix( temp[2], temp_scale );
179 temp[4] = FT_DivFix( temp[4], temp_scale );
180 temp[5] = FT_DivFix( temp[5], temp_scale );
181 temp[3] = 0x10000L;
182 }
183
184 matrix->xx = temp[0];
185 matrix->yx = temp[1];
186 matrix->xy = temp[2];
187 matrix->yy = temp[3];
188
189 /* note that the font offsets are expressed in integer font units */
190 offset->x = temp[4] >> 16;
191 offset->y = temp[5] >> 16;
192 }
193
194 return CID_Err_Ok; /* this is a callback function; */
195 /* we must return an error code */
196 }
197
198
199 FT_CALLBACK_DEF( FT_Error )
200 parse_fd_array( CID_Face face,
201 CID_Parser* parser )
202 {
203 CID_FaceInfo cid = &face->cid;
204 FT_Memory memory = face->root.memory;
205 FT_Error error = CID_Err_Ok;
206 FT_Long num_dicts;
207
208
209 num_dicts = cid_parser_to_int( parser );
210
211 if ( !cid->font_dicts )
212 {
213 FT_Int n;
214
215
216 if ( FT_NEW_ARRAY( cid->font_dicts, num_dicts ) )
217 goto Exit;
218
219 cid->num_dicts = (FT_UInt)num_dicts;
220
221 /* don't forget to set a few defaults */
222 for ( n = 0; n < cid->num_dicts; n++ )
223 {
224 CID_FaceDict dict = cid->font_dicts + n;
225
226
227 /* default value for lenIV */
228 dict->private_dict.lenIV = 4;
229 }
230 }
231
232 Exit:
233 return error;
234 }
235
236
237 static
238 const T1_FieldRec cid_field_records[] =
239 {
240
241 #include "cidtoken.h"
242
243 T1_FIELD_CALLBACK( "FDArray", parse_fd_array, 0 )
244 T1_FIELD_CALLBACK( "FontMatrix", parse_font_matrix, 0 )
245
246 { 0, T1_FIELD_LOCATION_CID_INFO, T1_FIELD_TYPE_NONE, 0, 0, 0, 0, 0, 0 }
247 };
248
249
250 static FT_Error
251 cid_parse_dict( CID_Face face,
252 CID_Loader* loader,
253 FT_Byte* base,
254 FT_Long size )
255 {
256 CID_Parser* parser = &loader->parser;
257
258
259 parser->root.cursor = base;
260 parser->root.limit = base + size;
261 parser->root.error = CID_Err_Ok;
262
263 {
264 FT_Byte* cur = base;
265 FT_Byte* limit = cur + size;
266
267
268 for (;;)
269 {
270 FT_Byte* newlimit;
271
272
273 parser->root.cursor = cur;
274 cid_parser_skip_spaces( parser );
275
276 if ( parser->root.cursor >= limit )
277 newlimit = limit - 1 - 17;
278 else
279 newlimit = parser->root.cursor - 17;
280
281 /* look for `%ADOBeginFontDict' */
282 for ( ; cur < newlimit; cur++ )
283 {
284 if ( *cur == '%' &&
285 ft_strncmp( (char*)cur, "%ADOBeginFontDict", 17 ) == 0 )
286 {
287 /* if /FDArray was found, then cid->num_dicts is > 0, and */
288 /* we can start increasing parser->num_dict */
289 if ( face->cid.num_dicts > 0 )
290 parser->num_dict++;
291 }
292 }
293
294 cur = parser->root.cursor;
295 /* no error can occur in cid_parser_skip_spaces */
296 if ( cur >= limit )
297 break;
298
299 cid_parser_skip_PS_token( parser );
300 if ( parser->root.cursor >= limit || parser->root.error )
301 break;
302
303 /* look for immediates */
304 if ( *cur == '/' && cur + 2 < limit )
305 {
306 FT_PtrDist len;
307
308
309 cur++;
310 len = parser->root.cursor - cur;
311
312 if ( len > 0 && len < 22 )
313 {
314 /* now compare the immediate name to the keyword table */
315 T1_Field keyword = (T1_Field)cid_field_records;
316
317
318 for (;;)
319 {
320 FT_Byte* name;
321
322
323 name = (FT_Byte*)keyword->ident;
324 if ( !name )
325 break;
326
327 if ( cur[0] == name[0] &&
328 len == (FT_PtrDist)ft_strlen( (const char*)name ) )
329 {
330 FT_PtrDist n;
331
332
333 for ( n = 1; n < len; n++ )
334 if ( cur[n] != name[n] )
335 break;
336
337 if ( n >= len )
338 {
339 /* we found it - run the parsing callback */
340 parser->root.error = cid_load_keyword( face,
341 loader,
342 keyword );
343 if ( parser->root.error )
344 return parser->root.error;
345 break;
346 }
347 }
348 keyword++;
349 }
350 }
351 }
352
353 cur = parser->root.cursor;
354 }
355 }
356 return parser->root.error;
357 }
358
359
360 /* read the subrmap and the subrs of each font dict */
361 static FT_Error
362 cid_read_subrs( CID_Face face )
363 {
364 CID_FaceInfo cid = &face->cid;
365 FT_Memory memory = face->root.memory;
366 FT_Stream stream = face->cid_stream;
367 FT_Error error;
368 FT_Int n;
369 CID_Subrs subr;
370 FT_UInt max_offsets = 0;
371 FT_ULong* offsets = 0;
372 PSAux_Service psaux = (PSAux_Service)face->psaux;
373
374
375 if ( FT_NEW_ARRAY( face->subrs, cid->num_dicts ) )
376 goto Exit;
377
378 subr = face->subrs;
379 for ( n = 0; n < cid->num_dicts; n++, subr++ )
380 {
381 CID_FaceDict dict = cid->font_dicts + n;
382 FT_Int lenIV = dict->private_dict.lenIV;
383 FT_UInt count, num_subrs = dict->num_subrs;
384 FT_ULong data_len;
385 FT_Byte* p;
386
387
388 /* reallocate offsets array if needed */
389 if ( num_subrs + 1 > max_offsets )
390 {
391 FT_UInt new_max = FT_PAD_CEIL( num_subrs + 1, 4 );
392
393
394 if ( FT_RENEW_ARRAY( offsets, max_offsets, new_max ) )
395 goto Fail;
396
397 max_offsets = new_max;
398 }
399
400 /* read the subrmap's offsets */
401 if ( FT_STREAM_SEEK( cid->data_offset + dict->subrmap_offset ) ||
402 FT_FRAME_ENTER( ( num_subrs + 1 ) * dict->sd_bytes ) )
403 goto Fail;
404
405 p = (FT_Byte*)stream->cursor;
406 for ( count = 0; count <= num_subrs; count++ )
407 offsets[count] = cid_get_offset( &p, (FT_Byte)dict->sd_bytes );
408
409 FT_FRAME_EXIT();
410
411 /* now, compute the size of subrs charstrings, */
412 /* allocate, and read them */
413 data_len = offsets[num_subrs] - offsets[0];
414
415 if ( FT_NEW_ARRAY( subr->code, num_subrs + 1 ) ||
416 FT_ALLOC( subr->code[0], data_len ) )
417 goto Fail;
418
419 if ( FT_STREAM_SEEK( cid->data_offset + offsets[0] ) ||
420 FT_STREAM_READ( subr->code[0], data_len ) )
421 goto Fail;
422
423 /* set up pointers */
424 for ( count = 1; count <= num_subrs; count++ )
425 {
426 FT_ULong len;
427
428
429 len = offsets[count] - offsets[count - 1];
430 subr->code[count] = subr->code[count - 1] + len;
431 }
432
433 /* decrypt subroutines, but only if lenIV >= 0 */
434 if ( lenIV >= 0 )
435 {
436 for ( count = 0; count < num_subrs; count++ )
437 {
438 FT_ULong len;
439
440
441 len = offsets[count + 1] - offsets[count];
442 psaux->t1_decrypt( subr->code[count], len, 4330 );
443 }
444 }
445
446 subr->num_subrs = num_subrs;
447 }
448
449 Exit:
450 FT_FREE( offsets );
451 return error;
452
453 Fail:
454 if ( face->subrs )
455 {
456 for ( n = 0; n < cid->num_dicts; n++ )
457 {
458 if ( face->subrs[n].code )
459 FT_FREE( face->subrs[n].code[0] );
460
461 FT_FREE( face->subrs[n].code );
462 }
463 FT_FREE( face->subrs );
464 }
465 goto Exit;
466 }
467
468
469 static void
470 t1_init_loader( CID_Loader* loader,
471 CID_Face face )
472 {
473 FT_UNUSED( face );
474
475 FT_MEM_ZERO( loader, sizeof ( *loader ) );
476 }
477
478
479 static void
480 t1_done_loader( CID_Loader* loader )
481 {
482 CID_Parser* parser = &loader->parser;
483
484
485 /* finalize parser */
486 cid_parser_done( parser );
487 }
488
489
490 static FT_Error
491 cid_hex_to_binary( FT_Byte* data,
492 FT_Long data_len,
493 FT_ULong offset,
494 CID_Face face )
495 {
496 FT_Stream stream = face->root.stream;
497 FT_Error error;
498
499 FT_Byte buffer[256];
500 FT_Byte *p, *plimit;
501 FT_Byte *d, *dlimit;
502 FT_Byte val;
503
504 FT_Bool upper_nibble, done;
505
506
507 if ( FT_STREAM_SEEK( offset ) )
508 goto Exit;
509
510 d = data;
511 dlimit = d + data_len;
512 p = buffer;
513 plimit = p;
514
515 upper_nibble = 1;
516 done = 0;
517
518 while ( d < dlimit )
519 {
520 if ( p >= plimit )
521 {
522 FT_ULong oldpos = FT_STREAM_POS();
523 FT_ULong size = stream->size - oldpos;
524
525
526 if ( size == 0 )
527 {
528 error = CID_Err_Syntax_Error;
529 goto Exit;
530 }
531
532 if ( FT_STREAM_READ( buffer, 256 > size ? size : 256 ) )
533 goto Exit;
534 p = buffer;
535 plimit = p + FT_STREAM_POS() - oldpos;
536 }
537
538 if ( ft_isdigit( *p ) )
539 val = (FT_Byte)( *p - '0' );
540 else if ( *p >= 'a' && *p <= 'f' )
541 val = (FT_Byte)( *p - 'a' );
542 else if ( *p >= 'A' && *p <= 'F' )
543 val = (FT_Byte)( *p - 'A' + 10 );
544 else if ( *p == ' ' ||
545 *p == '\t' ||
546 *p == '\r' ||
547 *p == '\n' ||
548 *p == '\f' ||
549 *p == '\0' )
550 {
551 p++;
552 continue;
553 }
554 else if ( *p == '>' )
555 {
556 val = 0;
557 done = 1;
558 }
559 else
560 {
561 error = CID_Err_Syntax_Error;
562 goto Exit;
563 }
564
565 if ( upper_nibble )
566 *d = (FT_Byte)( val << 4 );
567 else
568 {
569 *d = (FT_Byte)( *d + val );
570 d++;
571 }
572
573 upper_nibble = (FT_Byte)( 1 - upper_nibble );
574
575 if ( done )
576 break;
577
578 p++;
579 }
580
581 error = CID_Err_Ok;
582
583 Exit:
584 return error;
585 }
586
587
588 FT_LOCAL_DEF( FT_Error )
589 cid_face_open( CID_Face face,
590 FT_Int face_index )
591 {
592 CID_Loader loader;
593 CID_Parser* parser;
594 FT_Memory memory = face->root.memory;
595 FT_Error error;
596
597
598 t1_init_loader( &loader, face );
599
600 parser = &loader.parser;
601 error = cid_parser_new( parser, face->root.stream, face->root.memory,
602 (PSAux_Service)face->psaux );
603 if ( error )
604 goto Exit;
605
606 error = cid_parse_dict( face, &loader,
607 parser->postscript,
608 parser->postscript_len );
609 if ( error )
610 goto Exit;
611
612 if ( face_index < 0 )
613 goto Exit;
614
615 if ( FT_NEW( face->cid_stream ) )
616 goto Exit;
617
618 if ( parser->binary_length )
619 {
620 /* we must convert the data section from hexadecimal to binary */
621 if ( FT_ALLOC( face->binary_data, parser->binary_length ) ||
622 cid_hex_to_binary( face->binary_data, parser->binary_length,
623 parser->data_offset, face ) )
624 goto Exit;
625
626 FT_Stream_OpenMemory( face->cid_stream,
627 face->binary_data, parser->binary_length );
628 face->cid.data_offset = 0;
629 }
630 else
631 {
632 *face->cid_stream = *face->root.stream;
633 face->cid.data_offset = loader.parser.data_offset;
634 }
635
636 error = cid_read_subrs( face );
637
638 Exit:
639 t1_done_loader( &loader );
640 return error;
641 }
642
643
644 /* END */