[FREETYPE] Update to v2.6.2. CORE-10614
[reactos.git] / reactos / lib / 3rdparty / freetype / src / pfr / pfrload.c
1 /***************************************************************************/
2 /* */
3 /* pfrload.c */
4 /* */
5 /* FreeType PFR loader (body). */
6 /* */
7 /* Copyright 2002-2015 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 "pfrload.h"
20 #include FT_INTERNAL_DEBUG_H
21 #include FT_INTERNAL_STREAM_H
22
23 #include "pfrerror.h"
24
25 #undef FT_COMPONENT
26 #define FT_COMPONENT trace_pfr
27
28
29 /*
30 * The overall structure of a PFR file is as follows.
31 *
32 * PFR header
33 * 58 bytes (contains nPhysFonts)
34 *
35 * Logical font directory (size at most 2^16 bytes)
36 * 2 bytes (nLogFonts)
37 * + nLogFonts * 5 bytes
38 *
39 * ==> nLogFonts <= 13106
40 *
41 * Logical font section (size at most 2^24 bytes)
42 * nLogFonts * logFontRecord
43 *
44 * logFontRecord (size at most 2^16 bytes)
45 * 12 bytes (fontMatrix)
46 * + 1 byte (flags)
47 * + 0-5 bytes (depending on `flags')
48 * + 0-(1+255*(2+255)) = 0-65536 (depending on `flags')
49 * + 5 bytes (physical font info)
50 * + 0-1 bytes (depending on PFR header)
51 *
52 * ==> minimum size 18 bytes
53 *
54 * Physical font section (size at most 2^24 bytes)
55 * nPhysFonts * (physFontRecord
56 * + nBitmapSizes * nBmapChars * bmapCharRecord)
57 *
58 * physFontRecord (size at most 2^24 bytes)
59 * 14 bytes (font info)
60 * + 1 byte (flags)
61 * + 0-2 (depending on `flags')
62 * + 0-? (structure too complicated to be shown here; depending on
63 * `flags'; contains `nBitmapSizes' and `nBmapChars')
64 * + 3 bytes (nAuxBytes)
65 * + nAuxBytes
66 * + 1 byte (nBlueValues)
67 * + 2 * nBlueValues
68 * + 6 bytes (hinting data)
69 * + 2 bytes (nCharacters)
70 * + nCharacters * (4-10 bytes) (depending on `flags')
71 *
72 * ==> minimum size 27 bytes
73 *
74 * bmapCharRecord
75 * 4-7 bytes
76 *
77 * Glyph program strings (three possible types: simpleGps, compoundGps,
78 * and bitmapGps; size at most 2^24 bytes)
79 * simpleGps (size at most 2^16 bytes)
80 * 1 byte (flags)
81 * 1-2 bytes (n[XY]orus, depending on `flags')
82 * 0-(64+512*2) = 0-1088 bytes (depending on `n[XY]orus')
83 * 0-? (structure too complicated to be shown here; depending on
84 * `flags')
85 * 1-? glyph data (faintly resembling PS Type 1 charstrings)
86 *
87 * ==> minimum size 3 bytes
88 *
89 * compoundGps (size at most 2^16 bytes)
90 * 1 byte (nElements <= 63, flags)
91 * + 0-(1+255*(2+255)) = 0-65536 (depending on `flags')
92 * + nElements * (6-14 bytes)
93 *
94 * bitmapGps (size at most 2^16 bytes)
95 * 1 byte (flags)
96 * 3-13 bytes (position info, depending on `flags')
97 * 0-? bitmap data
98 *
99 * ==> minimum size 4 bytes
100 *
101 * PFR trailer
102 * 8 bytes
103 *
104 *
105 * ==> minimum size of a valid PFR:
106 * 58 (header)
107 * + 2 (nLogFonts)
108 * + 27 (1 physFontRecord)
109 * + 8 (trailer)
110 * -----
111 * 95 bytes
112 *
113 */
114
115
116 /*************************************************************************/
117 /*************************************************************************/
118 /***** *****/
119 /***** EXTRA ITEMS *****/
120 /***** *****/
121 /*************************************************************************/
122 /*************************************************************************/
123
124
125 FT_LOCAL_DEF( FT_Error )
126 pfr_extra_items_skip( FT_Byte* *pp,
127 FT_Byte* limit )
128 {
129 return pfr_extra_items_parse( pp, limit, NULL, NULL );
130 }
131
132
133 FT_LOCAL_DEF( FT_Error )
134 pfr_extra_items_parse( FT_Byte* *pp,
135 FT_Byte* limit,
136 PFR_ExtraItem item_list,
137 FT_Pointer item_data )
138 {
139 FT_Error error = FT_Err_Ok;
140 FT_Byte* p = *pp;
141 FT_UInt num_items, item_type, item_size;
142
143
144 PFR_CHECK( 1 );
145 num_items = PFR_NEXT_BYTE( p );
146
147 for ( ; num_items > 0; num_items-- )
148 {
149 PFR_CHECK( 2 );
150 item_size = PFR_NEXT_BYTE( p );
151 item_type = PFR_NEXT_BYTE( p );
152
153 PFR_CHECK( item_size );
154
155 if ( item_list )
156 {
157 PFR_ExtraItem extra = item_list;
158
159
160 for ( extra = item_list; extra->parser != NULL; extra++ )
161 {
162 if ( extra->type == item_type )
163 {
164 error = extra->parser( p, p + item_size, item_data );
165 if ( error )
166 goto Exit;
167
168 break;
169 }
170 }
171 }
172
173 p += item_size;
174 }
175
176 Exit:
177 *pp = p;
178 return error;
179
180 Too_Short:
181 FT_ERROR(( "pfr_extra_items_parse: invalid extra items table\n" ));
182 error = FT_THROW( Invalid_Table );
183 goto Exit;
184 }
185
186
187 /*************************************************************************/
188 /*************************************************************************/
189 /***** *****/
190 /***** PFR HEADER *****/
191 /***** *****/
192 /*************************************************************************/
193 /*************************************************************************/
194
195 static const FT_Frame_Field pfr_header_fields[] =
196 {
197 #undef FT_STRUCTURE
198 #define FT_STRUCTURE PFR_HeaderRec
199
200 FT_FRAME_START( 58 ),
201 FT_FRAME_ULONG ( signature ),
202 FT_FRAME_USHORT( version ),
203 FT_FRAME_USHORT( signature2 ),
204 FT_FRAME_USHORT( header_size ),
205
206 FT_FRAME_USHORT( log_dir_size ),
207 FT_FRAME_USHORT( log_dir_offset ),
208
209 FT_FRAME_USHORT( log_font_max_size ),
210 FT_FRAME_UOFF3 ( log_font_section_size ),
211 FT_FRAME_UOFF3 ( log_font_section_offset ),
212
213 FT_FRAME_USHORT( phy_font_max_size ),
214 FT_FRAME_UOFF3 ( phy_font_section_size ),
215 FT_FRAME_UOFF3 ( phy_font_section_offset ),
216
217 FT_FRAME_USHORT( gps_max_size ),
218 FT_FRAME_UOFF3 ( gps_section_size ),
219 FT_FRAME_UOFF3 ( gps_section_offset ),
220
221 FT_FRAME_BYTE ( max_blue_values ),
222 FT_FRAME_BYTE ( max_x_orus ),
223 FT_FRAME_BYTE ( max_y_orus ),
224
225 FT_FRAME_BYTE ( phy_font_max_size_high ),
226 FT_FRAME_BYTE ( color_flags ),
227
228 FT_FRAME_UOFF3 ( bct_max_size ),
229 FT_FRAME_UOFF3 ( bct_set_max_size ),
230 FT_FRAME_UOFF3 ( phy_bct_set_max_size ),
231
232 FT_FRAME_USHORT( num_phy_fonts ),
233 FT_FRAME_BYTE ( max_vert_stem_snap ),
234 FT_FRAME_BYTE ( max_horz_stem_snap ),
235 FT_FRAME_USHORT( max_chars ),
236 FT_FRAME_END
237 };
238
239
240 FT_LOCAL_DEF( FT_Error )
241 pfr_header_load( PFR_Header header,
242 FT_Stream stream )
243 {
244 FT_Error error;
245
246
247 /* read header directly */
248 if ( !FT_STREAM_SEEK( 0 ) &&
249 !FT_STREAM_READ_FIELDS( pfr_header_fields, header ) )
250 {
251 /* make a few adjustments to the header */
252 header->phy_font_max_size +=
253 (FT_UInt32)header->phy_font_max_size_high << 16;
254 }
255
256 return error;
257 }
258
259
260 FT_LOCAL_DEF( FT_Bool )
261 pfr_header_check( PFR_Header header )
262 {
263 FT_Bool result = 1;
264
265
266 /* check signature and header size */
267 if ( header->signature != 0x50465230L || /* "PFR0" */
268 header->version > 4 ||
269 header->header_size < 58 ||
270 header->signature2 != 0x0D0A ) /* CR/LF */
271 {
272 result = 0;
273 }
274
275 return result;
276 }
277
278
279 /***********************************************************************/
280 /***********************************************************************/
281 /***** *****/
282 /***** PFR LOGICAL FONTS *****/
283 /***** *****/
284 /***********************************************************************/
285 /***********************************************************************/
286
287
288 FT_LOCAL_DEF( FT_Error )
289 pfr_log_font_count( FT_Stream stream,
290 FT_UInt32 section_offset,
291 FT_Long *acount )
292 {
293 FT_Error error;
294 FT_UInt count;
295 FT_UInt result = 0;
296
297
298 if ( FT_STREAM_SEEK( section_offset ) ||
299 FT_READ_USHORT( count ) )
300 goto Exit;
301
302 /* check maximum value and a rough minimum size */
303 if ( count > ( ( 1 << 16 ) - 2 ) / 5 ||
304 2 + count * 5 >= stream->size - section_offset )
305 {
306 FT_ERROR(( "pfr_log_font_count:"
307 " invalid number of logical fonts\n" ));
308 error = FT_THROW( Invalid_Table );
309 goto Exit;
310 }
311
312 result = count;
313
314 Exit:
315 *acount = (FT_Long)result;
316 return error;
317 }
318
319
320 FT_LOCAL_DEF( FT_Error )
321 pfr_log_font_load( PFR_LogFont log_font,
322 FT_Stream stream,
323 FT_UInt idx,
324 FT_UInt32 section_offset,
325 FT_Bool size_increment )
326 {
327 FT_UInt num_log_fonts;
328 FT_UInt flags;
329 FT_UInt32 offset;
330 FT_UInt32 size;
331 FT_Error error;
332
333
334 if ( FT_STREAM_SEEK( section_offset ) ||
335 FT_READ_USHORT( num_log_fonts ) )
336 goto Exit;
337
338 if ( idx >= num_log_fonts )
339 return FT_THROW( Invalid_Argument );
340
341 if ( FT_STREAM_SKIP( idx * 5 ) ||
342 FT_READ_USHORT( size ) ||
343 FT_READ_UOFF3 ( offset ) )
344 goto Exit;
345
346 /* save logical font size and offset */
347 log_font->size = size;
348 log_font->offset = offset;
349
350 /* now, check the rest of the table before loading it */
351 {
352 FT_Byte* p;
353 FT_Byte* limit;
354 FT_UInt local;
355
356
357 if ( FT_STREAM_SEEK( offset ) ||
358 FT_FRAME_ENTER( size ) )
359 goto Exit;
360
361 p = stream->cursor;
362 limit = p + size;
363
364 PFR_CHECK( 13 );
365
366 log_font->matrix[0] = PFR_NEXT_LONG( p );
367 log_font->matrix[1] = PFR_NEXT_LONG( p );
368 log_font->matrix[2] = PFR_NEXT_LONG( p );
369 log_font->matrix[3] = PFR_NEXT_LONG( p );
370
371 flags = PFR_NEXT_BYTE( p );
372
373 local = 0;
374 if ( flags & PFR_LOG_STROKE )
375 {
376 local++;
377 if ( flags & PFR_LOG_2BYTE_STROKE )
378 local++;
379
380 if ( (flags & PFR_LINE_JOIN_MASK) == PFR_LINE_JOIN_MITER )
381 local += 3;
382 }
383 if ( flags & PFR_LOG_BOLD )
384 {
385 local++;
386 if ( flags & PFR_LOG_2BYTE_BOLD )
387 local++;
388 }
389
390 PFR_CHECK( local );
391
392 if ( flags & PFR_LOG_STROKE )
393 {
394 log_font->stroke_thickness = ( flags & PFR_LOG_2BYTE_STROKE )
395 ? PFR_NEXT_SHORT( p )
396 : PFR_NEXT_BYTE( p );
397
398 if ( ( flags & PFR_LINE_JOIN_MASK ) == PFR_LINE_JOIN_MITER )
399 log_font->miter_limit = PFR_NEXT_LONG( p );
400 }
401
402 if ( flags & PFR_LOG_BOLD )
403 {
404 log_font->bold_thickness = ( flags & PFR_LOG_2BYTE_BOLD )
405 ? PFR_NEXT_SHORT( p )
406 : PFR_NEXT_BYTE( p );
407 }
408
409 if ( flags & PFR_LOG_EXTRA_ITEMS )
410 {
411 error = pfr_extra_items_skip( &p, limit );
412 if ( error )
413 goto Fail;
414 }
415
416 PFR_CHECK( 5 );
417 log_font->phys_size = PFR_NEXT_USHORT( p );
418 log_font->phys_offset = PFR_NEXT_ULONG( p );
419 if ( size_increment )
420 {
421 PFR_CHECK( 1 );
422 log_font->phys_size += (FT_UInt32)PFR_NEXT_BYTE( p ) << 16;
423 }
424 }
425
426 Fail:
427 FT_FRAME_EXIT();
428
429 Exit:
430 return error;
431
432 Too_Short:
433 FT_ERROR(( "pfr_log_font_load: invalid logical font table\n" ));
434 error = FT_THROW( Invalid_Table );
435 goto Fail;
436 }
437
438
439 /***********************************************************************/
440 /***********************************************************************/
441 /***** *****/
442 /***** PFR PHYSICAL FONTS *****/
443 /***** *****/
444 /***********************************************************************/
445 /***********************************************************************/
446
447
448 /* load bitmap strikes lists */
449 FT_CALLBACK_DEF( FT_Error )
450 pfr_extra_item_load_bitmap_info( FT_Byte* p,
451 FT_Byte* limit,
452 PFR_PhyFont phy_font )
453 {
454 FT_Memory memory = phy_font->memory;
455 PFR_Strike strike;
456 FT_UInt flags0;
457 FT_UInt n, count, size1;
458 FT_Error error = FT_Err_Ok;
459
460
461 PFR_CHECK( 5 );
462
463 p += 3; /* skip bctSize */
464 flags0 = PFR_NEXT_BYTE( p );
465 count = PFR_NEXT_BYTE( p );
466
467 /* re-allocate when needed */
468 if ( phy_font->num_strikes + count > phy_font->max_strikes )
469 {
470 FT_UInt new_max = FT_PAD_CEIL( phy_font->num_strikes + count, 4 );
471
472
473 if ( FT_RENEW_ARRAY( phy_font->strikes,
474 phy_font->num_strikes,
475 new_max ) )
476 goto Exit;
477
478 phy_font->max_strikes = new_max;
479 }
480
481 size1 = 1 + 1 + 1 + 2 + 2 + 1;
482 if ( flags0 & PFR_STRIKE_2BYTE_XPPM )
483 size1++;
484
485 if ( flags0 & PFR_STRIKE_2BYTE_YPPM )
486 size1++;
487
488 if ( flags0 & PFR_STRIKE_3BYTE_SIZE )
489 size1++;
490
491 if ( flags0 & PFR_STRIKE_3BYTE_OFFSET )
492 size1++;
493
494 if ( flags0 & PFR_STRIKE_2BYTE_COUNT )
495 size1++;
496
497 strike = phy_font->strikes + phy_font->num_strikes;
498
499 PFR_CHECK( count * size1 );
500
501 for ( n = 0; n < count; n++, strike++ )
502 {
503 strike->x_ppm = ( flags0 & PFR_STRIKE_2BYTE_XPPM )
504 ? PFR_NEXT_USHORT( p )
505 : PFR_NEXT_BYTE( p );
506
507 strike->y_ppm = ( flags0 & PFR_STRIKE_2BYTE_YPPM )
508 ? PFR_NEXT_USHORT( p )
509 : PFR_NEXT_BYTE( p );
510
511 strike->flags = PFR_NEXT_BYTE( p );
512
513 strike->bct_size = ( flags0 & PFR_STRIKE_3BYTE_SIZE )
514 ? PFR_NEXT_ULONG( p )
515 : PFR_NEXT_USHORT( p );
516
517 strike->bct_offset = ( flags0 & PFR_STRIKE_3BYTE_OFFSET )
518 ? PFR_NEXT_ULONG( p )
519 : PFR_NEXT_USHORT( p );
520
521 strike->num_bitmaps = ( flags0 & PFR_STRIKE_2BYTE_COUNT )
522 ? PFR_NEXT_USHORT( p )
523 : PFR_NEXT_BYTE( p );
524 }
525
526 phy_font->num_strikes += count;
527
528 Exit:
529 return error;
530
531 Too_Short:
532 error = FT_THROW( Invalid_Table );
533 FT_ERROR(( "pfr_extra_item_load_bitmap_info:"
534 " invalid bitmap info table\n" ));
535 goto Exit;
536 }
537
538
539 /* Load font ID. This is a so-called `unique' name that is rather
540 * long and descriptive (like `Tiresias ScreenFont v7.51').
541 *
542 * Note that a PFR font's family name is contained in an *undocumented*
543 * string of the `auxiliary data' portion of a physical font record. This
544 * may also contain the `real' style name!
545 *
546 * If no family name is present, the font ID is used instead for the
547 * family.
548 */
549 FT_CALLBACK_DEF( FT_Error )
550 pfr_extra_item_load_font_id( FT_Byte* p,
551 FT_Byte* limit,
552 PFR_PhyFont phy_font )
553 {
554 FT_Error error = FT_Err_Ok;
555 FT_Memory memory = phy_font->memory;
556 FT_UInt len = (FT_UInt)( limit - p );
557
558
559 if ( phy_font->font_id != NULL )
560 goto Exit;
561
562 if ( FT_ALLOC( phy_font->font_id, len + 1 ) )
563 goto Exit;
564
565 /* copy font ID name, and terminate it for safety */
566 FT_MEM_COPY( phy_font->font_id, p, len );
567 phy_font->font_id[len] = 0;
568
569 Exit:
570 return error;
571 }
572
573
574 /* load stem snap tables */
575 FT_CALLBACK_DEF( FT_Error )
576 pfr_extra_item_load_stem_snaps( FT_Byte* p,
577 FT_Byte* limit,
578 PFR_PhyFont phy_font )
579 {
580 FT_UInt count, num_vert, num_horz;
581 FT_Int* snaps = NULL;
582 FT_Error error = FT_Err_Ok;
583 FT_Memory memory = phy_font->memory;
584
585
586 if ( phy_font->vertical.stem_snaps != NULL )
587 goto Exit;
588
589 PFR_CHECK( 1 );
590 count = PFR_NEXT_BYTE( p );
591
592 num_vert = count & 15;
593 num_horz = count >> 4;
594 count = num_vert + num_horz;
595
596 PFR_CHECK( count * 2 );
597
598 if ( FT_NEW_ARRAY( snaps, count ) )
599 goto Exit;
600
601 phy_font->vertical.stem_snaps = snaps;
602 phy_font->horizontal.stem_snaps = snaps + num_vert;
603
604 for ( ; count > 0; count--, snaps++ )
605 *snaps = FT_NEXT_SHORT( p );
606
607 Exit:
608 return error;
609
610 Too_Short:
611 error = FT_THROW( Invalid_Table );
612 FT_ERROR(( "pfr_exta_item_load_stem_snaps:"
613 " invalid stem snaps table\n" ));
614 goto Exit;
615 }
616
617
618
619 /* load kerning pair data */
620 FT_CALLBACK_DEF( FT_Error )
621 pfr_extra_item_load_kerning_pairs( FT_Byte* p,
622 FT_Byte* limit,
623 PFR_PhyFont phy_font )
624 {
625 PFR_KernItem item = NULL;
626 FT_Error error = FT_Err_Ok;
627 FT_Memory memory = phy_font->memory;
628
629
630 if ( FT_NEW( item ) )
631 goto Exit;
632
633 PFR_CHECK( 4 );
634
635 item->pair_count = PFR_NEXT_BYTE( p );
636 item->base_adj = PFR_NEXT_SHORT( p );
637 item->flags = PFR_NEXT_BYTE( p );
638 item->offset = phy_font->offset +
639 (FT_Offset)( p - phy_font->cursor );
640
641 #ifndef PFR_CONFIG_NO_CHECKS
642 item->pair_size = 3;
643
644 if ( item->flags & PFR_KERN_2BYTE_CHAR )
645 item->pair_size += 2;
646
647 if ( item->flags & PFR_KERN_2BYTE_ADJ )
648 item->pair_size += 1;
649
650 PFR_CHECK( item->pair_count * item->pair_size );
651 #endif
652
653 /* load first and last pairs into the item to speed up */
654 /* lookup later... */
655 if ( item->pair_count > 0 )
656 {
657 FT_UInt char1, char2;
658 FT_Byte* q;
659
660
661 if ( item->flags & PFR_KERN_2BYTE_CHAR )
662 {
663 q = p;
664 char1 = PFR_NEXT_USHORT( q );
665 char2 = PFR_NEXT_USHORT( q );
666
667 item->pair1 = PFR_KERN_INDEX( char1, char2 );
668
669 q = p + item->pair_size * ( item->pair_count - 1 );
670 char1 = PFR_NEXT_USHORT( q );
671 char2 = PFR_NEXT_USHORT( q );
672
673 item->pair2 = PFR_KERN_INDEX( char1, char2 );
674 }
675 else
676 {
677 q = p;
678 char1 = PFR_NEXT_BYTE( q );
679 char2 = PFR_NEXT_BYTE( q );
680
681 item->pair1 = PFR_KERN_INDEX( char1, char2 );
682
683 q = p + item->pair_size * ( item->pair_count - 1 );
684 char1 = PFR_NEXT_BYTE( q );
685 char2 = PFR_NEXT_BYTE( q );
686
687 item->pair2 = PFR_KERN_INDEX( char1, char2 );
688 }
689
690 /* add new item to the current list */
691 item->next = NULL;
692 *phy_font->kern_items_tail = item;
693 phy_font->kern_items_tail = &item->next;
694 phy_font->num_kern_pairs += item->pair_count;
695 }
696 else
697 {
698 /* empty item! */
699 FT_FREE( item );
700 }
701
702 Exit:
703 return error;
704
705 Too_Short:
706 FT_FREE( item );
707
708 error = FT_THROW( Invalid_Table );
709 FT_ERROR(( "pfr_extra_item_load_kerning_pairs:"
710 " invalid kerning pairs table\n" ));
711 goto Exit;
712 }
713
714
715 static const PFR_ExtraItemRec pfr_phy_font_extra_items[] =
716 {
717 { 1, (PFR_ExtraItem_ParseFunc)pfr_extra_item_load_bitmap_info },
718 { 2, (PFR_ExtraItem_ParseFunc)pfr_extra_item_load_font_id },
719 { 3, (PFR_ExtraItem_ParseFunc)pfr_extra_item_load_stem_snaps },
720 { 4, (PFR_ExtraItem_ParseFunc)pfr_extra_item_load_kerning_pairs },
721 { 0, NULL }
722 };
723
724
725 /*
726 * Load a name from the auxiliary data. Since this extracts undocumented
727 * strings from the font file, we need to be careful here.
728 */
729 static FT_Error
730 pfr_aux_name_load( FT_Byte* p,
731 FT_UInt len,
732 FT_Memory memory,
733 FT_String* *astring )
734 {
735 FT_Error error = FT_Err_Ok;
736 FT_String* result = NULL;
737 FT_UInt n, ok;
738
739
740 if ( len > 0 && p[len - 1] == 0 )
741 len--;
742
743 /* check that each character is ASCII for making sure not to
744 load garbage
745 */
746 ok = ( len > 0 );
747 for ( n = 0; n < len; n++ )
748 if ( p[n] < 32 || p[n] > 127 )
749 {
750 ok = 0;
751 break;
752 }
753
754 if ( ok )
755 {
756 if ( FT_ALLOC( result, len + 1 ) )
757 goto Exit;
758
759 FT_MEM_COPY( result, p, len );
760 result[len] = 0;
761 }
762 Exit:
763 *astring = result;
764 return error;
765 }
766
767
768 FT_LOCAL_DEF( void )
769 pfr_phy_font_done( PFR_PhyFont phy_font,
770 FT_Memory memory )
771 {
772 FT_FREE( phy_font->font_id );
773 FT_FREE( phy_font->family_name );
774 FT_FREE( phy_font->style_name );
775
776 FT_FREE( phy_font->vertical.stem_snaps );
777 phy_font->vertical.num_stem_snaps = 0;
778
779 phy_font->horizontal.stem_snaps = NULL;
780 phy_font->horizontal.num_stem_snaps = 0;
781
782 FT_FREE( phy_font->strikes );
783 phy_font->num_strikes = 0;
784 phy_font->max_strikes = 0;
785
786 FT_FREE( phy_font->chars );
787 phy_font->num_chars = 0;
788 phy_font->chars_offset = 0;
789
790 FT_FREE( phy_font->blue_values );
791 phy_font->num_blue_values = 0;
792
793 {
794 PFR_KernItem item, next;
795
796
797 item = phy_font->kern_items;
798 while ( item )
799 {
800 next = item->next;
801 FT_FREE( item );
802 item = next;
803 }
804 phy_font->kern_items = NULL;
805 phy_font->kern_items_tail = NULL;
806 }
807
808 phy_font->num_kern_pairs = 0;
809 }
810
811
812 FT_LOCAL_DEF( FT_Error )
813 pfr_phy_font_load( PFR_PhyFont phy_font,
814 FT_Stream stream,
815 FT_UInt32 offset,
816 FT_UInt32 size )
817 {
818 FT_Error error;
819 FT_Memory memory = stream->memory;
820 FT_UInt flags;
821 FT_ULong num_aux;
822 FT_Byte* p;
823 FT_Byte* limit;
824
825
826 phy_font->memory = memory;
827 phy_font->offset = offset;
828
829 phy_font->kern_items = NULL;
830 phy_font->kern_items_tail = &phy_font->kern_items;
831
832 if ( FT_STREAM_SEEK( offset ) ||
833 FT_FRAME_ENTER( size ) )
834 goto Exit;
835
836 phy_font->cursor = stream->cursor;
837
838 p = stream->cursor;
839 limit = p + size;
840
841 PFR_CHECK( 15 );
842 phy_font->font_ref_number = PFR_NEXT_USHORT( p );
843 phy_font->outline_resolution = PFR_NEXT_USHORT( p );
844 phy_font->metrics_resolution = PFR_NEXT_USHORT( p );
845 phy_font->bbox.xMin = PFR_NEXT_SHORT( p );
846 phy_font->bbox.yMin = PFR_NEXT_SHORT( p );
847 phy_font->bbox.xMax = PFR_NEXT_SHORT( p );
848 phy_font->bbox.yMax = PFR_NEXT_SHORT( p );
849 phy_font->flags = flags = PFR_NEXT_BYTE( p );
850
851 /* get the standard advance for non-proportional fonts */
852 if ( !(flags & PFR_PHY_PROPORTIONAL) )
853 {
854 PFR_CHECK( 2 );
855 phy_font->standard_advance = PFR_NEXT_SHORT( p );
856 }
857
858 /* load the extra items when present */
859 if ( flags & PFR_PHY_EXTRA_ITEMS )
860 {
861 error = pfr_extra_items_parse( &p, limit,
862 pfr_phy_font_extra_items, phy_font );
863
864 if ( error )
865 goto Fail;
866 }
867
868 /* In certain fonts, the auxiliary bytes contain interesting */
869 /* information. These are not in the specification but can be */
870 /* guessed by looking at the content of a few PFR0 fonts. */
871 PFR_CHECK( 3 );
872 num_aux = PFR_NEXT_ULONG( p );
873
874 if ( num_aux > 0 )
875 {
876 FT_Byte* q = p;
877 FT_Byte* q2;
878
879
880 PFR_CHECK_SIZE( num_aux );
881 p += num_aux;
882
883 while ( num_aux > 0 )
884 {
885 FT_UInt length, type;
886
887
888 if ( q + 4 > p )
889 break;
890
891 length = PFR_NEXT_USHORT( q );
892 if ( length < 4 || length > num_aux )
893 break;
894
895 q2 = q + length - 2;
896 type = PFR_NEXT_USHORT( q );
897
898 switch ( type )
899 {
900 case 1:
901 /* this seems to correspond to the font's family name, padded to */
902 /* an even number of bytes with a zero byte appended if needed */
903 error = pfr_aux_name_load( q, length - 4U, memory,
904 &phy_font->family_name );
905 if ( error )
906 goto Exit;
907 break;
908
909 case 2:
910 if ( q + 32 > q2 )
911 break;
912
913 q += 10;
914 phy_font->ascent = PFR_NEXT_SHORT( q );
915 phy_font->descent = PFR_NEXT_SHORT( q );
916 phy_font->leading = PFR_NEXT_SHORT( q );
917 break;
918
919 case 3:
920 /* this seems to correspond to the font's style name, padded to */
921 /* an even number of bytes with a zero byte appended if needed */
922 error = pfr_aux_name_load( q, length - 4U, memory,
923 &phy_font->style_name );
924 if ( error )
925 goto Exit;
926 break;
927
928 default:
929 ;
930 }
931
932 q = q2;
933 num_aux -= length;
934 }
935 }
936
937 /* read the blue values */
938 {
939 FT_UInt n, count;
940
941
942 PFR_CHECK( 1 );
943 phy_font->num_blue_values = count = PFR_NEXT_BYTE( p );
944
945 PFR_CHECK( count * 2 );
946
947 if ( FT_NEW_ARRAY( phy_font->blue_values, count ) )
948 goto Fail;
949
950 for ( n = 0; n < count; n++ )
951 phy_font->blue_values[n] = PFR_NEXT_SHORT( p );
952 }
953
954 PFR_CHECK( 8 );
955 phy_font->blue_fuzz = PFR_NEXT_BYTE( p );
956 phy_font->blue_scale = PFR_NEXT_BYTE( p );
957
958 phy_font->vertical.standard = PFR_NEXT_USHORT( p );
959 phy_font->horizontal.standard = PFR_NEXT_USHORT( p );
960
961 /* read the character descriptors */
962 {
963 FT_UInt n, count, Size;
964
965
966 phy_font->num_chars = count = PFR_NEXT_USHORT( p );
967 phy_font->chars_offset = offset + (FT_Offset)( p - stream->cursor );
968
969 Size = 1 + 1 + 2;
970 if ( flags & PFR_PHY_2BYTE_CHARCODE )
971 Size += 1;
972
973 if ( flags & PFR_PHY_PROPORTIONAL )
974 Size += 2;
975
976 if ( flags & PFR_PHY_ASCII_CODE )
977 Size += 1;
978
979 if ( flags & PFR_PHY_2BYTE_GPS_SIZE )
980 Size += 1;
981
982 if ( flags & PFR_PHY_3BYTE_GPS_OFFSET )
983 Size += 1;
984
985 PFR_CHECK_SIZE( count * Size );
986
987 if ( FT_NEW_ARRAY( phy_font->chars, count ) )
988 goto Fail;
989
990 for ( n = 0; n < count; n++ )
991 {
992 PFR_Char cur = &phy_font->chars[n];
993
994
995 cur->char_code = ( flags & PFR_PHY_2BYTE_CHARCODE )
996 ? PFR_NEXT_USHORT( p )
997 : PFR_NEXT_BYTE( p );
998
999 cur->advance = ( flags & PFR_PHY_PROPORTIONAL )
1000 ? PFR_NEXT_SHORT( p )
1001 : phy_font->standard_advance;
1002
1003 #if 0
1004 cur->ascii = ( flags & PFR_PHY_ASCII_CODE )
1005 ? PFR_NEXT_BYTE( p )
1006 : 0;
1007 #else
1008 if ( flags & PFR_PHY_ASCII_CODE )
1009 p += 1;
1010 #endif
1011 cur->gps_size = ( flags & PFR_PHY_2BYTE_GPS_SIZE )
1012 ? PFR_NEXT_USHORT( p )
1013 : PFR_NEXT_BYTE( p );
1014
1015 cur->gps_offset = ( flags & PFR_PHY_3BYTE_GPS_OFFSET )
1016 ? PFR_NEXT_ULONG( p )
1017 : PFR_NEXT_USHORT( p );
1018 }
1019 }
1020
1021 /* that's it! */
1022
1023 Fail:
1024 FT_FRAME_EXIT();
1025
1026 /* save position of bitmap info */
1027 phy_font->bct_offset = FT_STREAM_POS();
1028 phy_font->cursor = NULL;
1029
1030 Exit:
1031 return error;
1032
1033 Too_Short:
1034 error = FT_THROW( Invalid_Table );
1035 FT_ERROR(( "pfr_phy_font_load: invalid physical font table\n" ));
1036 goto Fail;
1037 }
1038
1039
1040 /* END */