1 /***************************************************************************/
5 /* TrueTypeGX/AAT common tables validation (body). */
7 /* Copyright 2004, 2005, 2009 */
8 /* by suzuki toshiya, Masatake YAMATO, Red Hat K.K., */
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 /***************************************************************************/
19 /***************************************************************************/
21 /* gxvalid is derived from both gxlayout module and otvalid module. */
22 /* Development of gxlayout is supported by the Information-technology */
23 /* Promotion Agency(IPA), Japan. */
25 /***************************************************************************/
31 /*************************************************************************/
33 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
34 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
35 /* messages during execution. */
38 #define FT_COMPONENT trace_gxvcommon
41 /*************************************************************************/
42 /*************************************************************************/
44 /***** 16bit offset sorter *****/
46 /*************************************************************************/
47 /*************************************************************************/
50 gxv_compare_ushort_offset( FT_UShort
* a
,
63 gxv_set_length_by_ushort_offset( FT_UShort
* offset
,
73 for ( i
= 0; i
< nmemb
; i
++ )
76 for ( i
= 0; i
< nmemb
; i
++ )
80 ft_qsort( buff
, ( nmemb
+ 1 ), sizeof ( FT_UShort
),
81 ( int(*)(const void*, const void*) )gxv_compare_ushort_offset
);
83 if ( buff
[nmemb
] > limit
)
86 for ( i
= 0; i
< nmemb
; i
++ )
91 for ( j
= 0; j
< nmemb
; j
++ )
92 if ( buff
[j
] == offset
[i
] )
98 *(length
[i
]) = (FT_UShort
)( buff
[j
+ 1] - buff
[j
] );
100 if ( 0 != offset
[i
] && 0 == *(length
[i
]) )
106 /*************************************************************************/
107 /*************************************************************************/
109 /***** 32bit offset sorter *****/
111 /*************************************************************************/
112 /*************************************************************************/
115 gxv_compare_ulong_offset( FT_ULong
* a
,
128 gxv_set_length_by_ulong_offset( FT_ULong
* offset
,
138 for ( i
= 0; i
< nmemb
; i
++ )
141 for ( i
= 0; i
< nmemb
; i
++ )
145 ft_qsort( buff
, ( nmemb
+ 1 ), sizeof ( FT_ULong
),
146 ( int(*)(const void*, const void*) )gxv_compare_ulong_offset
);
148 if ( buff
[nmemb
] > limit
)
151 for ( i
= 0; i
< nmemb
; i
++ )
156 for ( j
= 0; j
< nmemb
; j
++ )
157 if ( buff
[j
] == offset
[i
] )
163 *(length
[i
]) = buff
[j
+ 1] - buff
[j
];
165 if ( 0 != offset
[i
] && 0 == *(length
[i
]) )
171 /*************************************************************************/
172 /*************************************************************************/
174 /***** scan value array and get min & max *****/
176 /*************************************************************************/
177 /*************************************************************************/
181 gxv_array_getlimits_byte( FT_Bytes table
,
185 GXV_Validator valid
)
198 GXV_LIMIT_CHECK( 1 );
199 val
= FT_NEXT_BYTE( p
);
201 *min
= (FT_Byte
)FT_MIN( *min
, val
);
202 *max
= (FT_Byte
)FT_MAX( *max
, val
);
205 valid
->subtable_length
= p
- table
;
210 gxv_array_getlimits_ushort( FT_Bytes table
,
214 GXV_Validator valid
)
227 GXV_LIMIT_CHECK( 2 );
228 val
= FT_NEXT_USHORT( p
);
230 *min
= (FT_Byte
)FT_MIN( *min
, val
);
231 *max
= (FT_Byte
)FT_MAX( *max
, val
);
234 valid
->subtable_length
= p
- table
;
238 /*************************************************************************/
239 /*************************************************************************/
241 /***** BINSEARCHHEADER *****/
243 /*************************************************************************/
244 /*************************************************************************/
246 typedef struct GXV_BinSrchHeader_
250 FT_UShort searchRange
;
251 FT_UShort entrySelector
;
252 FT_UShort rangeShift
;
258 gxv_BinSrchHeader_check_consistency( GXV_BinSrchHeader
* binSrchHeader
,
259 GXV_Validator valid
)
261 FT_UShort searchRange
;
262 FT_UShort entrySelector
;
263 FT_UShort rangeShift
;
266 if ( binSrchHeader
->unitSize
== 0 )
269 if ( binSrchHeader
->nUnits
== 0 )
271 if ( binSrchHeader
->searchRange
== 0 &&
272 binSrchHeader
->entrySelector
== 0 &&
273 binSrchHeader
->rangeShift
== 0 )
279 for ( searchRange
= 1, entrySelector
= 1;
280 ( searchRange
* 2 ) <= binSrchHeader
->nUnits
&&
281 searchRange
< 0x8000U
;
282 searchRange
*= 2, entrySelector
++ )
286 searchRange
= (FT_UShort
)( searchRange
* binSrchHeader
->unitSize
);
287 rangeShift
= (FT_UShort
)( binSrchHeader
->nUnits
* binSrchHeader
->unitSize
290 if ( searchRange
!= binSrchHeader
->searchRange
||
291 entrySelector
!= binSrchHeader
->entrySelector
||
292 rangeShift
!= binSrchHeader
->rangeShift
)
294 GXV_TRACE(( "Inconsistency found in BinSrchHeader\n" ));
295 GXV_TRACE(( "originally: unitSize=%d, nUnits=%d, "
296 "searchRange=%d, entrySelector=%d, "
298 binSrchHeader
->unitSize
, binSrchHeader
->nUnits
,
299 binSrchHeader
->searchRange
, binSrchHeader
->entrySelector
,
300 binSrchHeader
->rangeShift
));
301 GXV_TRACE(( "calculated: unitSize=%d, nUnits=%d, "
302 "searchRange=%d, entrySelector=%d, "
304 binSrchHeader
->unitSize
, binSrchHeader
->nUnits
,
305 searchRange
, entrySelector
, rangeShift
));
307 if ( valid
->root
->level
>= FT_VALIDATE_PARANOID
)
314 * parser & validator of BinSrchHeader
315 * which is used in LookupTable format 2, 4, 6.
317 * Essential parameters (unitSize, nUnits) are returned by
318 * given pointer, others (searchRange, entrySelector, rangeShift)
319 * can be calculated by essential parameters, so they are just
320 * validated and discarded.
322 * However, wrong values in searchRange, entrySelector, rangeShift
323 * won't cause fatal errors, because these parameters might be
324 * only used in old m68k font driver in MacOS.
325 * -- suzuki toshiya <mpsuzuki@hiroshima-u.ac.jp>
329 gxv_BinSrchHeader_validate( FT_Bytes table
,
331 FT_UShort
* unitSize_p
,
333 GXV_Validator valid
)
336 GXV_BinSrchHeader binSrchHeader
;
339 GXV_NAME_ENTER( "BinSrchHeader validate" );
341 if ( *unitSize_p
== 0 )
343 GXV_LIMIT_CHECK( 2 );
344 binSrchHeader
.unitSize
= FT_NEXT_USHORT( p
);
347 binSrchHeader
.unitSize
= *unitSize_p
;
349 if ( *nUnits_p
== 0 )
351 GXV_LIMIT_CHECK( 2 );
352 binSrchHeader
.nUnits
= FT_NEXT_USHORT( p
);
355 binSrchHeader
.nUnits
= *nUnits_p
;
357 GXV_LIMIT_CHECK( 2 + 2 + 2 );
358 binSrchHeader
.searchRange
= FT_NEXT_USHORT( p
);
359 binSrchHeader
.entrySelector
= FT_NEXT_USHORT( p
);
360 binSrchHeader
.rangeShift
= FT_NEXT_USHORT( p
);
361 GXV_TRACE(( "nUnits %d\n", binSrchHeader
.nUnits
));
363 gxv_BinSrchHeader_check_consistency( &binSrchHeader
, valid
);
365 if ( *unitSize_p
== 0 )
366 *unitSize_p
= binSrchHeader
.unitSize
;
368 if ( *nUnits_p
== 0 )
369 *nUnits_p
= binSrchHeader
.nUnits
;
371 valid
->subtable_length
= p
- table
;
376 /*************************************************************************/
377 /*************************************************************************/
379 /***** LOOKUP TABLE *****/
381 /*************************************************************************/
382 /*************************************************************************/
384 #define GXV_LOOKUP_VALUE_LOAD( P, SIGNSPEC ) \
385 ( P += 2, gxv_lookup_value_load( P - 2, SIGNSPEC ) )
387 static GXV_LookupValueDesc
388 gxv_lookup_value_load( FT_Bytes p
,
391 GXV_LookupValueDesc v
;
394 if ( signspec
== GXV_LOOKUPVALUE_UNSIGNED
)
395 v
.u
= FT_NEXT_USHORT( p
);
397 v
.s
= FT_NEXT_SHORT( p
);
403 #define GXV_UNITSIZE_VALIDATE( FORMAT, UNITSIZE, NUNITS, CORRECTSIZE ) \
405 if ( UNITSIZE != CORRECTSIZE ) \
407 FT_ERROR(( "unitSize=%d differs from" \
408 " expected unitSize=%d" \
409 " in LookupTable %s\n", \
410 UNITSIZE, CORRECTSIZE, FORMAT )); \
411 if ( UNITSIZE != 0 && NUNITS != 0 ) \
413 FT_ERROR(( " cannot validate anymore\n" )); \
417 FT_ERROR(( " forcibly continues\n" )); \
422 /* ================= Simple Array Format 0 Lookup Table ================ */
424 gxv_LookupTable_fmt0_validate( FT_Bytes table
,
426 GXV_Validator valid
)
431 GXV_LookupValueDesc value
;
434 GXV_NAME_ENTER( "LookupTable format 0" );
436 GXV_LIMIT_CHECK( 2 * valid
->face
->num_glyphs
);
438 for ( i
= 0; i
< valid
->face
->num_glyphs
; i
++ )
440 GXV_LIMIT_CHECK( 2 );
441 if ( p
+ 2 >= limit
) /* some fonts have too-short fmt0 array */
443 GXV_TRACE(( "too short, glyphs %d - %d are missing\n",
444 i
, valid
->face
->num_glyphs
));
445 if ( valid
->root
->level
>= FT_VALIDATE_PARANOID
)
450 value
= GXV_LOOKUP_VALUE_LOAD( p
, valid
->lookupval_sign
);
451 valid
->lookupval_func( i
, &value
, valid
);
454 valid
->subtable_length
= p
- table
;
459 /* ================= Segment Single Format 2 Loolup Table ============== */
463 * To guarantee that a binary search terminates, you must include one or
464 * more special `end of search table' values at the end of the data to
465 * be searched. The number of termination values that need to be
466 * included is table-specific. The value that indicates binary search
467 * termination is 0xFFFF.
469 * The problem is that nUnits does not include this end-marker. It's
470 * quite difficult to discriminate whether the following 0xFFFF comes from
471 * the end-marker or some next data.
473 * -- suzuki toshiya <mpsuzuki@hiroshima-u.ac.jp>
476 gxv_LookupTable_fmt2_skip_endmarkers( FT_Bytes table
,
478 GXV_Validator valid
)
483 while ( ( p
+ 4 ) < valid
->root
->limit
)
485 if ( p
[0] != 0xFF || p
[1] != 0xFF || /* lastGlyph */
486 p
[2] != 0xFF || p
[3] != 0xFF ) /* firstGlyph */
491 valid
->subtable_length
= p
- table
;
496 gxv_LookupTable_fmt2_validate( FT_Bytes table
,
498 GXV_Validator valid
)
507 FT_UShort firstGlyph
;
508 GXV_LookupValueDesc value
;
511 GXV_NAME_ENTER( "LookupTable format 2" );
513 unitSize
= nUnits
= 0;
514 gxv_BinSrchHeader_validate( p
, limit
, &unitSize
, &nUnits
, valid
);
515 p
+= valid
->subtable_length
;
517 GXV_UNITSIZE_VALIDATE( "format2", unitSize
, nUnits
, 6 );
519 for ( unit
= 0, gid
= 0; unit
< nUnits
; unit
++ )
521 GXV_LIMIT_CHECK( 2 + 2 + 2 );
522 lastGlyph
= FT_NEXT_USHORT( p
);
523 firstGlyph
= FT_NEXT_USHORT( p
);
524 value
= GXV_LOOKUP_VALUE_LOAD( p
, valid
->lookupval_sign
);
526 gxv_glyphid_validate( firstGlyph
, valid
);
527 gxv_glyphid_validate( lastGlyph
, valid
);
529 if ( lastGlyph
< gid
)
531 GXV_TRACE(( "reverse ordered segment specification:"
532 " lastGlyph[%d]=%d < lastGlyph[%d]=%d\n",
533 unit
, lastGlyph
, unit
- 1 , gid
));
534 if ( valid
->root
->level
>= FT_VALIDATE_PARANOID
)
538 if ( lastGlyph
< firstGlyph
)
540 GXV_TRACE(( "reverse ordered range specification at unit %d:",
541 " lastGlyph %d < firstGlyph %d ",
542 unit
, lastGlyph
, firstGlyph
));
543 if ( valid
->root
->level
>= FT_VALIDATE_PARANOID
)
546 if ( valid
->root
->level
== FT_VALIDATE_TIGHT
)
547 continue; /* ftxvalidator silently skips such an entry */
549 FT_TRACE4(( "continuing with exchanged values\n" ));
551 firstGlyph
= lastGlyph
;
555 for ( gid
= firstGlyph
; gid
<= lastGlyph
; gid
++ )
556 valid
->lookupval_func( gid
, &value
, valid
);
559 gxv_LookupTable_fmt2_skip_endmarkers( p
, unitSize
, valid
);
560 p
+= valid
->subtable_length
;
562 valid
->subtable_length
= p
- table
;
567 /* ================= Segment Array Format 4 Lookup Table =============== */
569 gxv_LookupTable_fmt4_validate( FT_Bytes table
,
571 GXV_Validator valid
)
580 FT_UShort firstGlyph
;
581 GXV_LookupValueDesc base_value
;
582 GXV_LookupValueDesc value
;
585 GXV_NAME_ENTER( "LookupTable format 4" );
587 unitSize
= nUnits
= 0;
588 gxv_BinSrchHeader_validate( p
, limit
, &unitSize
, &nUnits
, valid
);
589 p
+= valid
->subtable_length
;
591 GXV_UNITSIZE_VALIDATE( "format4", unitSize
, nUnits
, 6 );
593 for ( unit
= 0, gid
= 0; unit
< nUnits
; unit
++ )
595 GXV_LIMIT_CHECK( 2 + 2 );
596 lastGlyph
= FT_NEXT_USHORT( p
);
597 firstGlyph
= FT_NEXT_USHORT( p
);
599 gxv_glyphid_validate( firstGlyph
, valid
);
600 gxv_glyphid_validate( lastGlyph
, valid
);
602 if ( lastGlyph
< gid
)
604 GXV_TRACE(( "reverse ordered segment specification:"
605 " lastGlyph[%d]=%d < lastGlyph[%d]=%d\n",
606 unit
, lastGlyph
, unit
- 1 , gid
));
607 if ( valid
->root
->level
>= FT_VALIDATE_PARANOID
)
611 if ( lastGlyph
< firstGlyph
)
613 GXV_TRACE(( "reverse ordered range specification at unit %d:",
614 " lastGlyph %d < firstGlyph %d ",
615 unit
, lastGlyph
, firstGlyph
));
616 if ( valid
->root
->level
>= FT_VALIDATE_PARANOID
)
619 if ( valid
->root
->level
== FT_VALIDATE_TIGHT
)
620 continue; /* ftxvalidator silently skips such an entry */
622 FT_TRACE4(( "continuing with exchanged values\n" ));
624 firstGlyph
= lastGlyph
;
628 GXV_LIMIT_CHECK( 2 );
629 base_value
= GXV_LOOKUP_VALUE_LOAD( p
, GXV_LOOKUPVALUE_UNSIGNED
);
631 for ( gid
= firstGlyph
; gid
<= lastGlyph
; gid
++ )
633 value
= valid
->lookupfmt4_trans( (FT_UShort
)( gid
- firstGlyph
),
638 valid
->lookupval_func( gid
, &value
, valid
);
642 gxv_LookupTable_fmt2_skip_endmarkers( p
, unitSize
, valid
);
643 p
+= valid
->subtable_length
;
645 valid
->subtable_length
= p
- table
;
650 /* ================= Segment Table Format 6 Lookup Table =============== */
652 gxv_LookupTable_fmt6_skip_endmarkers( FT_Bytes table
,
654 GXV_Validator valid
)
659 while ( p
< valid
->root
->limit
)
661 if ( p
[0] != 0xFF || p
[1] != 0xFF )
666 valid
->subtable_length
= p
- table
;
671 gxv_LookupTable_fmt6_validate( FT_Bytes table
,
673 GXV_Validator valid
)
677 FT_UShort prev_glyph
;
682 GXV_LookupValueDesc value
;
685 GXV_NAME_ENTER( "LookupTable format 6" );
687 unitSize
= nUnits
= 0;
688 gxv_BinSrchHeader_validate( p
, limit
, &unitSize
, &nUnits
, valid
);
689 p
+= valid
->subtable_length
;
691 GXV_UNITSIZE_VALIDATE( "format6", unitSize
, nUnits
, 4 );
693 for ( unit
= 0, prev_glyph
= 0; unit
< nUnits
; unit
++ )
695 GXV_LIMIT_CHECK( 2 + 2 );
696 glyph
= FT_NEXT_USHORT( p
);
697 value
= GXV_LOOKUP_VALUE_LOAD( p
, valid
->lookupval_sign
);
699 if ( gxv_glyphid_validate( glyph
, valid
) )
700 GXV_TRACE(( " endmarker found within defined range"
701 " (entry %d < nUnits=%d)\n",
704 if ( prev_glyph
> glyph
)
706 GXV_TRACE(( "current gid 0x%04x < previous gid 0x%04x\n",
707 glyph
, prev_glyph
));
708 if ( valid
->root
->level
>= FT_VALIDATE_PARANOID
)
713 valid
->lookupval_func( glyph
, &value
, valid
);
716 gxv_LookupTable_fmt6_skip_endmarkers( p
, unitSize
, valid
);
717 p
+= valid
->subtable_length
;
719 valid
->subtable_length
= p
- table
;
724 /* ================= Trimmed Array Format 8 Lookup Table =============== */
726 gxv_LookupTable_fmt8_validate( FT_Bytes table
,
728 GXV_Validator valid
)
733 GXV_LookupValueDesc value
;
734 FT_UShort firstGlyph
;
735 FT_UShort glyphCount
;
738 GXV_NAME_ENTER( "LookupTable format 8" );
740 /* firstGlyph + glyphCount */
741 GXV_LIMIT_CHECK( 2 + 2 );
742 firstGlyph
= FT_NEXT_USHORT( p
);
743 glyphCount
= FT_NEXT_USHORT( p
);
745 gxv_glyphid_validate( firstGlyph
, valid
);
746 gxv_glyphid_validate( (FT_UShort
)( firstGlyph
+ glyphCount
), valid
);
749 for ( i
= 0; i
< glyphCount
; i
++ )
751 GXV_LIMIT_CHECK( 2 );
752 value
= GXV_LOOKUP_VALUE_LOAD( p
, valid
->lookupval_sign
);
753 valid
->lookupval_func( (FT_UShort
)( firstGlyph
+ i
), &value
, valid
);
756 valid
->subtable_length
= p
- table
;
762 gxv_LookupTable_validate( FT_Bytes table
,
764 GXV_Validator valid
)
769 GXV_Validate_Func fmt_funcs_table
[] =
771 gxv_LookupTable_fmt0_validate
, /* 0 */
773 gxv_LookupTable_fmt2_validate
, /* 2 */
775 gxv_LookupTable_fmt4_validate
, /* 4 */
777 gxv_LookupTable_fmt6_validate
, /* 6 */
779 gxv_LookupTable_fmt8_validate
, /* 8 */
782 GXV_Validate_Func func
;
785 GXV_NAME_ENTER( "LookupTable" );
787 /* lookuptbl_head may be used in fmt4 transit function. */
788 valid
->lookuptbl_head
= table
;
791 GXV_LIMIT_CHECK( 2 );
792 format
= FT_NEXT_USHORT( p
);
793 GXV_TRACE(( " (format %d)\n", format
));
798 func
= fmt_funcs_table
[format
];
802 func( p
, limit
, valid
);
803 p
+= valid
->subtable_length
;
805 valid
->subtable_length
= p
- table
;
811 /*************************************************************************/
812 /*************************************************************************/
814 /***** Glyph ID *****/
816 /*************************************************************************/
817 /*************************************************************************/
819 FT_LOCAL_DEF( FT_Int
)
820 gxv_glyphid_validate( FT_UShort gid
,
821 GXV_Validator valid
)
826 if ( gid
== 0xFFFFU
)
833 if ( face
->num_glyphs
< gid
)
835 GXV_TRACE(( " gxv_glyphid_check() gid overflow: num_glyphs %d < %d\n",
836 face
->num_glyphs
, gid
));
837 if ( valid
->root
->level
>= FT_VALIDATE_PARANOID
)
845 /*************************************************************************/
846 /*************************************************************************/
848 /***** CONTROL POINT *****/
850 /*************************************************************************/
851 /*************************************************************************/
854 gxv_ctlPoint_validate( FT_UShort gid
,
856 GXV_Validator valid
)
868 error
= FT_Load_Glyph( face
,
870 FT_LOAD_NO_BITMAP
| FT_LOAD_IGNORE_TRANSFORM
);
875 outline
= glyph
->outline
;
876 n_points
= outline
.n_points
;
879 if ( !( ctl_point
< n_points
) )
884 /*************************************************************************/
885 /*************************************************************************/
887 /***** SFNT NAME *****/
889 /*************************************************************************/
890 /*************************************************************************/
893 gxv_sfntName_validate( FT_UShort name_index
,
896 GXV_Validator valid
)
903 GXV_NAME_ENTER( "sfntName" );
905 if ( name_index
< min_index
|| max_index
< name_index
)
908 nnames
= FT_Get_Sfnt_Name_Count( valid
->face
);
909 for ( i
= 0; i
< nnames
; i
++ )
911 if ( FT_Get_Sfnt_Name( valid
->face
, i
, &name
) != FT_Err_Ok
)
914 if ( name
.name_id
== name_index
)
918 GXV_TRACE(( " nameIndex = %d (UNTITLED)\n", name_index
));
920 goto Exit
; /* make compiler happy */
923 FT_TRACE1(( " nameIndex = %d (", name_index
));
924 GXV_TRACE_HEXDUMP_SFNTNAME( name
);
925 FT_TRACE1(( ")\n" ));
932 /*************************************************************************/
933 /*************************************************************************/
935 /***** STATE TABLE *****/
937 /*************************************************************************/
938 /*************************************************************************/
940 /* -------------------------- Class Table --------------------------- */
943 * highestClass specifies how many classes are defined in this
944 * Class Subtable. Apple spec does not mention whether undefined
945 * holes in the class (e.g.: 0-3 are predefined, 4 is unused, 5 is used)
946 * are permitted. At present, holes in a defined class are not checked.
947 * -- suzuki toshiya <mpsuzuki@hiroshima-u.ac.jp>
951 gxv_ClassTable_validate( FT_Bytes table
,
954 FT_Byte
* maxClassID_p
,
955 GXV_Validator valid
)
958 FT_Bytes limit
= table
+ *length_p
;
959 FT_UShort firstGlyph
;
963 GXV_NAME_ENTER( "ClassTable" );
965 *maxClassID_p
= 3; /* Classes 0, 2, and 3 are predefined */
967 GXV_LIMIT_CHECK( 2 + 2 );
968 firstGlyph
= FT_NEXT_USHORT( p
);
969 nGlyphs
= FT_NEXT_USHORT( p
);
971 GXV_TRACE(( " (firstGlyph = %d, nGlyphs = %d)\n", firstGlyph
, nGlyphs
));
976 gxv_glyphid_validate( (FT_UShort
)( firstGlyph
+ nGlyphs
), valid
);
979 FT_Byte nGlyphInClass
[256];
984 ft_memset( nGlyphInClass
, 0, 256 );
987 for ( i
= 0; i
< nGlyphs
; i
++ )
989 GXV_LIMIT_CHECK( 1 );
990 classID
= FT_NEXT_BYTE( p
);
993 /* following classes should not appear in class array */
994 case 0: /* end of text */
995 case 2: /* out of bounds */
996 case 3: /* end of line */
1000 case 1: /* out of bounds */
1001 default: /* user-defined: 4 - ( stateSize - 1 ) */
1002 if ( classID
>= stateSize
)
1003 FT_INVALID_DATA
; /* assign glyph to undefined state */
1005 nGlyphInClass
[classID
]++;
1009 *length_p
= (FT_UShort
)( p
- table
);
1011 /* scan max ClassID in use */
1012 for ( i
= 0; i
< stateSize
; i
++ )
1013 if ( ( 3 < i
) && ( nGlyphInClass
[i
] > 0 ) )
1014 *maxClassID_p
= (FT_Byte
)i
; /* XXX: Check Range? */
1018 GXV_TRACE(( "Declared stateSize=0x%02x, Used maxClassID=0x%02x\n",
1019 stateSize
, *maxClassID_p
));
1024 /* --------------------------- State Array ----------------------------- */
1027 gxv_StateArray_validate( FT_Bytes table
,
1028 FT_UShort
* length_p
,
1030 FT_UShort stateSize
,
1031 FT_Byte
* maxState_p
,
1032 FT_Byte
* maxEntry_p
,
1033 GXV_Validator valid
)
1036 FT_Bytes limit
= table
+ *length_p
;
1040 FT_UNUSED( stateSize
); /* for the non-debugging case */
1043 GXV_NAME_ENTER( "StateArray" );
1045 GXV_TRACE(( "parse %d bytes by stateSize=%d maxClassID=%d\n",
1046 (int)(*length_p
), stateSize
, (int)(maxClassID
) ));
1049 * 2 states are predefined and must be described in StateArray:
1050 * state 0 (start of text), 1 (start of line)
1052 GXV_LIMIT_CHECK( ( 1 + maxClassID
) * 2 );
1057 /* read if enough to read another state */
1058 while ( p
+ ( 1 + maxClassID
) <= limit
)
1061 for ( clazz
= 0; clazz
<= maxClassID
; clazz
++ )
1063 entry
= FT_NEXT_BYTE( p
);
1064 *maxEntry_p
= (FT_Byte
)FT_MAX( *maxEntry_p
, entry
);
1067 GXV_TRACE(( "parsed: maxState=%d, maxEntry=%d\n",
1068 *maxState_p
, *maxEntry_p
));
1070 *length_p
= (FT_UShort
)( p
- table
);
1076 /* --------------------------- Entry Table ----------------------------- */
1079 gxv_EntryTable_validate( FT_Bytes table
,
1080 FT_UShort
* length_p
,
1082 FT_UShort stateArray
,
1083 FT_UShort stateArray_length
,
1085 FT_Bytes statetable_table
,
1086 FT_Bytes statetable_limit
,
1087 GXV_Validator valid
)
1090 FT_Bytes limit
= table
+ *length_p
;
1093 FT_Int entrySize
= 2 + 2 + GXV_GLYPHOFFSET_SIZE( statetable
);
1095 GXV_XStateTable_GlyphOffsetDesc glyphOffset
;
1098 GXV_NAME_ENTER( "EntryTable" );
1100 GXV_TRACE(( "maxEntry=%d entrySize=%d\n", maxEntry
, entrySize
));
1102 if ( ( maxEntry
+ 1 ) * entrySize
> *length_p
)
1104 if ( valid
->root
->level
>= FT_VALIDATE_PARANOID
)
1105 FT_INVALID_TOO_SHORT
;
1107 /* ftxvalidator and FontValidator both warn and continue */
1108 maxEntry
= (FT_Byte
)( *length_p
/ entrySize
- 1 );
1109 GXV_TRACE(( "too large maxEntry, shrinking to %d fit EntryTable length\n",
1113 for ( entry
= 0; entry
<= maxEntry
; entry
++ )
1119 GXV_LIMIT_CHECK( 2 + 2 );
1120 newState
= FT_NEXT_USHORT( p
);
1121 flags
= FT_NEXT_USHORT( p
);
1124 if ( newState
< stateArray
||
1125 stateArray
+ stateArray_length
< newState
)
1127 GXV_TRACE(( " newState offset 0x%04x is out of stateArray\n",
1129 if ( valid
->root
->level
>= FT_VALIDATE_PARANOID
)
1134 if ( 0 != ( ( newState
- stateArray
) % ( 1 + maxClassID
) ) )
1136 GXV_TRACE(( " newState offset 0x%04x is not aligned to %d-classes\n",
1137 newState
, 1 + maxClassID
));
1138 if ( valid
->root
->level
>= FT_VALIDATE_PARANOID
)
1143 state
= (FT_Byte
)( ( newState
- stateArray
) / ( 1 + maxClassID
) );
1145 switch ( GXV_GLYPHOFFSET_FMT( statetable
) )
1147 case GXV_GLYPHOFFSET_NONE
:
1148 glyphOffset
.uc
= 0; /* make compiler happy */
1151 case GXV_GLYPHOFFSET_UCHAR
:
1152 glyphOffset
.uc
= FT_NEXT_BYTE( p
);
1155 case GXV_GLYPHOFFSET_CHAR
:
1156 glyphOffset
.c
= FT_NEXT_CHAR( p
);
1159 case GXV_GLYPHOFFSET_USHORT
:
1160 glyphOffset
.u
= FT_NEXT_USHORT( p
);
1163 case GXV_GLYPHOFFSET_SHORT
:
1164 glyphOffset
.s
= FT_NEXT_SHORT( p
);
1167 case GXV_GLYPHOFFSET_ULONG
:
1168 glyphOffset
.ul
= FT_NEXT_ULONG( p
);
1171 case GXV_GLYPHOFFSET_LONG
:
1172 glyphOffset
.l
= FT_NEXT_LONG( p
);
1176 if ( valid
->root
->level
>= FT_VALIDATE_PARANOID
)
1181 if ( NULL
!= valid
->statetable
.entry_validate_func
)
1182 valid
->statetable
.entry_validate_func( state
,
1191 *length_p
= (FT_UShort
)( p
- table
);
1197 /* =========================== State Table ============================= */
1199 FT_LOCAL_DEF( void )
1200 gxv_StateTable_subtable_setup( FT_UShort table_size
,
1201 FT_UShort classTable
,
1202 FT_UShort stateArray
,
1203 FT_UShort entryTable
,
1204 FT_UShort
* classTable_length_p
,
1205 FT_UShort
* stateArray_length_p
,
1206 FT_UShort
* entryTable_length_p
,
1207 GXV_Validator valid
)
1217 l
[0] = classTable_length_p
;
1218 l
[1] = stateArray_length_p
;
1219 l
[2] = entryTable_length_p
;
1221 gxv_set_length_by_ushort_offset( o
, l
, buff
, 3, table_size
, valid
);
1225 FT_LOCAL_DEF( void )
1226 gxv_StateTable_validate( FT_Bytes table
,
1228 GXV_Validator valid
)
1230 FT_UShort stateSize
;
1231 FT_UShort classTable
; /* offset to Class(Sub)Table */
1232 FT_UShort stateArray
; /* offset to StateArray */
1233 FT_UShort entryTable
; /* offset to EntryTable */
1235 FT_UShort classTable_length
;
1236 FT_UShort stateArray_length
;
1237 FT_UShort entryTable_length
;
1242 GXV_StateTable_Subtable_Setup_Func setup_func
;
1247 GXV_NAME_ENTER( "StateTable" );
1249 GXV_TRACE(( "StateTable header\n" ));
1251 GXV_LIMIT_CHECK( 2 + 2 + 2 + 2 );
1252 stateSize
= FT_NEXT_USHORT( p
);
1253 classTable
= FT_NEXT_USHORT( p
);
1254 stateArray
= FT_NEXT_USHORT( p
);
1255 entryTable
= FT_NEXT_USHORT( p
);
1257 GXV_TRACE(( "stateSize=0x%04x\n", stateSize
));
1258 GXV_TRACE(( "offset to classTable=0x%04x\n", classTable
));
1259 GXV_TRACE(( "offset to stateArray=0x%04x\n", stateArray
));
1260 GXV_TRACE(( "offset to entryTable=0x%04x\n", entryTable
));
1262 if ( stateSize
> 0xFF )
1265 if ( valid
->statetable
.optdata_load_func
!= NULL
)
1266 valid
->statetable
.optdata_load_func( p
, limit
, valid
);
1268 if ( valid
->statetable
.subtable_setup_func
!= NULL
)
1269 setup_func
= valid
->statetable
.subtable_setup_func
;
1271 setup_func
= gxv_StateTable_subtable_setup
;
1273 setup_func( (FT_UShort
)( limit
- table
),
1282 GXV_TRACE(( "StateTable Subtables\n" ));
1284 if ( classTable
!= 0 )
1285 gxv_ClassTable_validate( table
+ classTable
,
1291 maxClassID
= (FT_Byte
)( stateSize
- 1 );
1293 if ( stateArray
!= 0 )
1294 gxv_StateArray_validate( table
+ stateArray
,
1303 maxState
= 1; /* 0:start of text, 1:start of line are predefined */
1307 if ( maxEntry
> 0 && entryTable
== 0 )
1310 if ( entryTable
!= 0 )
1311 gxv_EntryTable_validate( table
+ entryTable
,
1325 /* ================= eXtended State Table (for morx) =================== */
1327 FT_LOCAL_DEF( void )
1328 gxv_XStateTable_subtable_setup( FT_ULong table_size
,
1329 FT_ULong classTable
,
1330 FT_ULong stateArray
,
1331 FT_ULong entryTable
,
1332 FT_ULong
* classTable_length_p
,
1333 FT_ULong
* stateArray_length_p
,
1334 FT_ULong
* entryTable_length_p
,
1335 GXV_Validator valid
)
1345 l
[0] = classTable_length_p
;
1346 l
[1] = stateArray_length_p
;
1347 l
[2] = entryTable_length_p
;
1349 gxv_set_length_by_ulong_offset( o
, l
, buff
, 4, table_size
, valid
);
1354 gxv_XClassTable_lookupval_validate( FT_UShort glyph
,
1355 GXV_LookupValueCPtr value_p
,
1356 GXV_Validator valid
)
1360 if ( value_p
->u
>= valid
->xstatetable
.nClasses
)
1362 if ( value_p
->u
> valid
->xstatetable
.maxClassID
)
1363 valid
->xstatetable
.maxClassID
= value_p
->u
;
1368 +===============+ --------+
1375 | firstGlyph[0] | | head of lookup table
1376 +---------------+ | +
1377 | offset[0] | -> | offset [byte]
1378 +===============+ | +
1379 | lastGlyph[1] | | (glyphID - firstGlyph) * 2 [byte]
1393 static GXV_LookupValueDesc
1394 gxv_XClassTable_lookupfmt4_transit( FT_UShort relative_gindex
,
1395 GXV_LookupValueCPtr base_value_p
,
1396 FT_Bytes lookuptbl_limit
,
1397 GXV_Validator valid
)
1402 GXV_LookupValueDesc value
;
1404 /* XXX: check range? */
1405 offset
= (FT_UShort
)( base_value_p
->u
+
1406 relative_gindex
* sizeof ( FT_UShort
) );
1408 p
= valid
->lookuptbl_head
+ offset
;
1409 limit
= lookuptbl_limit
;
1411 GXV_LIMIT_CHECK ( 2 );
1412 value
.u
= FT_NEXT_USHORT( p
);
1419 gxv_XStateArray_validate( FT_Bytes table
,
1421 FT_UShort maxClassID
,
1423 FT_UShort
* maxState_p
,
1424 FT_UShort
* maxEntry_p
,
1425 GXV_Validator valid
)
1428 FT_Bytes limit
= table
+ *length_p
;
1432 FT_UNUSED( stateSize
); /* for the non-debugging case */
1435 GXV_NAME_ENTER( "XStateArray" );
1437 GXV_TRACE(( "parse % 3d bytes by stateSize=% 3d maxClassID=% 3d\n",
1438 (int)(*length_p
), stateSize
, (int)(maxClassID
) ));
1441 * 2 states are predefined and must be described:
1442 * state 0 (start of text), 1 (start of line)
1444 GXV_LIMIT_CHECK( ( 1 + maxClassID
) * 2 * 2 );
1449 /* read if enough to read another state */
1450 while ( p
+ ( ( 1 + maxClassID
) * 2 ) <= limit
)
1453 for ( clazz
= 0; clazz
<= maxClassID
; clazz
++ )
1455 entry
= FT_NEXT_USHORT( p
);
1456 *maxEntry_p
= (FT_UShort
)FT_MAX( *maxEntry_p
, entry
);
1459 GXV_TRACE(( "parsed: maxState=%d, maxEntry=%d\n",
1460 *maxState_p
, *maxEntry_p
));
1462 *length_p
= p
- table
;
1469 gxv_XEntryTable_validate( FT_Bytes table
,
1472 FT_ULong stateArray_length
,
1473 FT_UShort maxClassID
,
1474 FT_Bytes xstatetable_table
,
1475 FT_Bytes xstatetable_limit
,
1476 GXV_Validator valid
)
1479 FT_Bytes limit
= table
+ *length_p
;
1482 FT_Int entrySize
= 2 + 2 + GXV_GLYPHOFFSET_SIZE( xstatetable
);
1485 GXV_NAME_ENTER( "XEntryTable" );
1486 GXV_TRACE(( "maxEntry=%d entrySize=%d\n", maxEntry
, entrySize
));
1488 if ( ( p
+ ( maxEntry
+ 1 ) * entrySize
) > limit
)
1489 FT_INVALID_TOO_SHORT
;
1491 for (entry
= 0; entry
<= maxEntry
; entry
++ )
1493 FT_UShort newState_idx
;
1495 GXV_XStateTable_GlyphOffsetDesc glyphOffset
;
1498 GXV_LIMIT_CHECK( 2 + 2 );
1499 newState_idx
= FT_NEXT_USHORT( p
);
1500 flags
= FT_NEXT_USHORT( p
);
1502 if ( stateArray_length
< (FT_ULong
)( newState_idx
* 2 ) )
1504 GXV_TRACE(( " newState index 0x%04x points out of stateArray\n",
1506 if ( valid
->root
->level
>= FT_VALIDATE_PARANOID
)
1510 state
= (FT_UShort
)( newState_idx
/ ( 1 + maxClassID
) );
1511 if ( 0 != ( newState_idx
% ( 1 + maxClassID
) ) )
1513 FT_TRACE4(( "-> new state = %d (supposed)\n"
1514 "but newState index 0x%04x is not aligned to %d-classes\n",
1515 state
, newState_idx
, 1 + maxClassID
));
1516 if ( valid
->root
->level
>= FT_VALIDATE_PARANOID
)
1520 switch ( GXV_GLYPHOFFSET_FMT( xstatetable
) )
1522 case GXV_GLYPHOFFSET_NONE
:
1523 glyphOffset
.uc
= 0; /* make compiler happy */
1526 case GXV_GLYPHOFFSET_UCHAR
:
1527 glyphOffset
.uc
= FT_NEXT_BYTE( p
);
1530 case GXV_GLYPHOFFSET_CHAR
:
1531 glyphOffset
.c
= FT_NEXT_CHAR( p
);
1534 case GXV_GLYPHOFFSET_USHORT
:
1535 glyphOffset
.u
= FT_NEXT_USHORT( p
);
1538 case GXV_GLYPHOFFSET_SHORT
:
1539 glyphOffset
.s
= FT_NEXT_SHORT( p
);
1542 case GXV_GLYPHOFFSET_ULONG
:
1543 glyphOffset
.ul
= FT_NEXT_ULONG( p
);
1546 case GXV_GLYPHOFFSET_LONG
:
1547 glyphOffset
.l
= FT_NEXT_LONG( p
);
1551 if ( valid
->root
->level
>= FT_VALIDATE_PARANOID
)
1556 if ( NULL
!= valid
->xstatetable
.entry_validate_func
)
1557 valid
->xstatetable
.entry_validate_func( state
,
1566 *length_p
= p
- table
;
1572 FT_LOCAL_DEF( void )
1573 gxv_XStateTable_validate( FT_Bytes table
,
1575 GXV_Validator valid
)
1577 /* StateHeader members */
1578 FT_ULong classTable
; /* offset to Class(Sub)Table */
1579 FT_ULong stateArray
; /* offset to StateArray */
1580 FT_ULong entryTable
; /* offset to EntryTable */
1582 FT_ULong classTable_length
;
1583 FT_ULong stateArray_length
;
1584 FT_ULong entryTable_length
;
1588 GXV_XStateTable_Subtable_Setup_Func setup_func
;
1593 GXV_NAME_ENTER( "XStateTable" );
1595 GXV_TRACE(( "XStateTable header\n" ));
1597 GXV_LIMIT_CHECK( 4 + 4 + 4 + 4 );
1598 valid
->xstatetable
.nClasses
= FT_NEXT_ULONG( p
);
1599 classTable
= FT_NEXT_ULONG( p
);
1600 stateArray
= FT_NEXT_ULONG( p
);
1601 entryTable
= FT_NEXT_ULONG( p
);
1603 GXV_TRACE(( "nClasses =0x%08x\n", valid
->xstatetable
.nClasses
));
1604 GXV_TRACE(( "offset to classTable=0x%08x\n", classTable
));
1605 GXV_TRACE(( "offset to stateArray=0x%08x\n", stateArray
));
1606 GXV_TRACE(( "offset to entryTable=0x%08x\n", entryTable
));
1608 if ( valid
->xstatetable
.nClasses
> 0xFFFFU
)
1611 GXV_TRACE(( "StateTable Subtables\n" ));
1613 if ( valid
->xstatetable
.optdata_load_func
!= NULL
)
1614 valid
->xstatetable
.optdata_load_func( p
, limit
, valid
);
1616 if ( valid
->xstatetable
.subtable_setup_func
!= NULL
)
1617 setup_func
= valid
->xstatetable
.subtable_setup_func
;
1619 setup_func
= gxv_XStateTable_subtable_setup
;
1621 setup_func( limit
- table
,
1630 if ( classTable
!= 0 )
1632 valid
->xstatetable
.maxClassID
= 0;
1633 valid
->lookupval_sign
= GXV_LOOKUPVALUE_UNSIGNED
;
1634 valid
->lookupval_func
= gxv_XClassTable_lookupval_validate
;
1635 valid
->lookupfmt4_trans
= gxv_XClassTable_lookupfmt4_transit
;
1636 gxv_LookupTable_validate( table
+ classTable
,
1637 table
+ classTable
+ classTable_length
,
1639 if ( valid
->subtable_length
< classTable_length
)
1640 classTable_length
= valid
->subtable_length
;
1644 /* XXX: check range? */
1645 valid
->xstatetable
.maxClassID
=
1646 (FT_UShort
)( valid
->xstatetable
.nClasses
- 1 );
1649 if ( stateArray
!= 0 )
1650 gxv_XStateArray_validate( table
+ stateArray
,
1652 valid
->xstatetable
.maxClassID
,
1653 valid
->xstatetable
.nClasses
,
1659 maxState
= 1; /* 0:start of text, 1:start of line are predefined */
1663 if ( maxEntry
> 0 && entryTable
== 0 )
1666 if ( entryTable
!= 0 )
1667 gxv_XEntryTable_validate( table
+ entryTable
,
1671 valid
->xstatetable
.maxClassID
,
1680 /*************************************************************************/
1681 /*************************************************************************/
1683 /***** Table overlapping *****/
1685 /*************************************************************************/
1686 /*************************************************************************/
1689 gxv_compare_ranges( FT_Bytes table1_start
,
1690 FT_ULong table1_length
,
1691 FT_Bytes table2_start
,
1692 FT_ULong table2_length
)
1694 if ( table1_start
== table2_start
)
1696 if ( ( table1_length
== 0 || table2_length
== 0 ) )
1699 else if ( table1_start
< table2_start
)
1701 if ( ( table1_start
+ table1_length
) <= table2_start
)
1704 else if ( table1_start
> table2_start
)
1706 if ( ( table1_start
>= table2_start
+ table2_length
) )
1716 FT_LOCAL_DEF( void )
1717 gxv_odtect_add_range( FT_Bytes start
,
1719 const FT_String
* name
,
1720 GXV_odtect_Range odtect
)
1722 odtect
->range
[ odtect
->nRanges
].start
= start
;
1723 odtect
->range
[ odtect
->nRanges
].length
= length
;
1724 odtect
->range
[ odtect
->nRanges
].name
= (FT_String
*)name
;
1729 FT_LOCAL_DEF( void )
1730 gxv_odtect_validate( GXV_odtect_Range odtect
,
1731 GXV_Validator valid
)
1736 GXV_NAME_ENTER( "check overlap among multi ranges" );
1738 for ( i
= 0; i
< odtect
->nRanges
; i
++ )
1739 for ( j
= 0; j
< i
; j
++ )
1740 if ( 0 != gxv_compare_ranges( odtect
->range
[i
].start
,
1741 odtect
->range
[i
].length
,
1742 odtect
->range
[j
].start
,
1743 odtect
->range
[j
].length
) )
1745 if ( odtect
->range
[i
].name
|| odtect
->range
[j
].name
)
1746 GXV_TRACE(( "found overlap between range %d and range %d\n",
1749 GXV_TRACE(( "found overlap between `%s' and `%s\'\n",
1750 odtect
->range
[i
].name
,
1751 odtect
->range
[j
].name
));