1 /***************************************************************************/
5 /* OpenType GPOS table validation (body). */
7 /* Copyright 2002-2017 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
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. */
16 /***************************************************************************/
24 /*************************************************************************/
26 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
27 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
28 /* messages during execution. */
31 #define FT_COMPONENT trace_otvgpos
35 otv_Anchor_validate( FT_Bytes table,
36 OTV_Validator valid );
39 otv_MarkArray_validate( FT_Bytes table,
40 OTV_Validator valid );
43 /*************************************************************************/
44 /*************************************************************************/
46 /***** UTILITY FUNCTIONS *****/
48 /*************************************************************************/
49 /*************************************************************************/
51 #define BaseArrayFunc otv_x_sxy
52 #define LigatureAttachFunc otv_x_sxy
53 #define Mark2ArrayFunc otv_x_sxy
55 /* uses valid->extra1 (counter) */
56 /* uses valid->extra2 (boolean to handle NULL anchor field) */
59 otv_x_sxy( FT_Bytes table,
60 OTV_Validator otvalid )
63 FT_UInt Count, count1, table_size;
70 Count = FT_NEXT_USHORT( p );
72 OTV_TRACE(( " (Count = %d)\n", Count ));
74 OTV_LIMIT_CHECK( Count * otvalid->extra1 * 2 );
76 table_size = Count * otvalid->extra1 * 2 + 2;
78 for ( ; Count > 0; Count-- )
79 for ( count1 = otvalid->extra1; count1 > 0; count1-- )
81 OTV_OPTIONAL_TABLE( anchor_offset );
84 OTV_OPTIONAL_OFFSET( anchor_offset );
86 if ( otvalid->extra2 )
88 OTV_SIZE_CHECK( anchor_offset );
90 otv_Anchor_validate( table + anchor_offset, otvalid );
93 otv_Anchor_validate( table + anchor_offset, otvalid );
100 #define MarkBasePosFormat1Func otv_u_O_O_u_O_O
101 #define MarkLigPosFormat1Func otv_u_O_O_u_O_O
102 #define MarkMarkPosFormat1Func otv_u_O_O_u_O_O
104 /* sets otvalid->extra1 (class count) */
107 otv_u_O_O_u_O_O( FT_Bytes table,
108 OTV_Validator otvalid )
111 FT_UInt Coverage1, Coverage2, ClassCount;
112 FT_UInt Array1, Array2;
113 OTV_Validate_Func func;
118 p += 2; /* skip PosFormat */
120 OTV_LIMIT_CHECK( 10 );
121 Coverage1 = FT_NEXT_USHORT( p );
122 Coverage2 = FT_NEXT_USHORT( p );
123 ClassCount = FT_NEXT_USHORT( p );
124 Array1 = FT_NEXT_USHORT( p );
125 Array2 = FT_NEXT_USHORT( p );
127 otv_Coverage_validate( table + Coverage1, otvalid, -1 );
128 otv_Coverage_validate( table + Coverage2, otvalid, -1 );
130 otv_MarkArray_validate( table + Array1, otvalid );
132 otvalid->nesting_level++;
133 func = otvalid->func[otvalid->nesting_level];
134 otvalid->extra1 = ClassCount;
136 func( table + Array2, otvalid );
138 otvalid->nesting_level--;
144 /*************************************************************************/
145 /*************************************************************************/
147 /***** VALUE RECORDS *****/
149 /*************************************************************************/
150 /*************************************************************************/
153 otv_value_length( FT_UInt format )
158 count = ( ( format & 0xAA ) >> 1 ) + ( format & 0x55 );
159 count = ( ( count & 0xCC ) >> 2 ) + ( count & 0x33 );
160 count = ( ( count & 0xF0 ) >> 4 ) + ( count & 0x0F );
166 /* uses otvalid->extra3 (pointer to base table) */
169 otv_ValueRecord_validate( FT_Bytes table,
171 OTV_Validator otvalid )
176 #ifdef FT_DEBUG_LEVEL_TRACE
181 OTV_NAME_ENTER( "ValueRecord" );
183 /* display `format' in dual representation */
184 for ( loop = 7; loop >= 0; loop-- )
187 res += ( format >> loop ) & 1;
190 OTV_TRACE(( " (format 0b%08lx)\n", res ));
193 if ( format >= 0x100 )
196 for ( count = 4; count > 0; count-- )
200 /* XPlacement, YPlacement, XAdvance, YAdvance */
201 OTV_LIMIT_CHECK( 2 );
208 for ( count = 4; count > 0; count-- )
212 FT_PtrDist table_size;
214 OTV_OPTIONAL_TABLE( device );
217 /* XPlaDevice, YPlaDevice, XAdvDevice, YAdvDevice */
218 OTV_LIMIT_CHECK( 2 );
219 OTV_OPTIONAL_OFFSET( device );
221 /* XXX: this value is usually too small, especially if the current */
222 /* ValueRecord is part of an array -- getting the correct table */
223 /* size is probably not worth the trouble */
225 table_size = p - otvalid->extra3;
227 OTV_SIZE_CHECK( device );
229 otv_Device_validate( otvalid->extra3 + device, otvalid );
238 /*************************************************************************/
239 /*************************************************************************/
241 /***** ANCHORS *****/
243 /*************************************************************************/
244 /*************************************************************************/
247 otv_Anchor_validate( FT_Bytes table,
248 OTV_Validator otvalid )
251 FT_UInt AnchorFormat;
254 OTV_NAME_ENTER( "Anchor");
256 OTV_LIMIT_CHECK( 6 );
257 AnchorFormat = FT_NEXT_USHORT( p );
259 OTV_TRACE(( " (format %d)\n", AnchorFormat ));
261 p += 4; /* skip XCoordinate and YCoordinate */
263 switch ( AnchorFormat )
269 OTV_LIMIT_CHECK( 2 ); /* AnchorPoint */
276 OTV_OPTIONAL_TABLE( XDeviceTable );
277 OTV_OPTIONAL_TABLE( YDeviceTable );
280 OTV_LIMIT_CHECK( 4 );
281 OTV_OPTIONAL_OFFSET( XDeviceTable );
282 OTV_OPTIONAL_OFFSET( YDeviceTable );
286 OTV_SIZE_CHECK( XDeviceTable );
288 otv_Device_validate( table + XDeviceTable, otvalid );
290 OTV_SIZE_CHECK( YDeviceTable );
292 otv_Device_validate( table + YDeviceTable, otvalid );
304 /*************************************************************************/
305 /*************************************************************************/
307 /***** MARK ARRAYS *****/
309 /*************************************************************************/
310 /*************************************************************************/
313 otv_MarkArray_validate( FT_Bytes table,
314 OTV_Validator otvalid )
320 OTV_NAME_ENTER( "MarkArray" );
322 OTV_LIMIT_CHECK( 2 );
323 MarkCount = FT_NEXT_USHORT( p );
325 OTV_TRACE(( " (MarkCount = %d)\n", MarkCount ));
327 OTV_LIMIT_CHECK( MarkCount * 4 );
330 for ( ; MarkCount > 0; MarkCount-- )
332 p += 2; /* skip Class */
334 otv_Anchor_validate( table + FT_NEXT_USHORT( p ), otvalid );
341 /*************************************************************************/
342 /*************************************************************************/
344 /***** GPOS LOOKUP TYPE 1 *****/
346 /*************************************************************************/
347 /*************************************************************************/
349 /* sets otvalid->extra3 (pointer to base table) */
352 otv_SinglePos_validate( FT_Bytes table,
353 OTV_Validator otvalid )
359 OTV_NAME_ENTER( "SinglePos" );
361 OTV_LIMIT_CHECK( 2 );
362 PosFormat = FT_NEXT_USHORT( p );
364 OTV_TRACE(( " (format %d)\n", PosFormat ));
366 otvalid->extra3 = table;
370 case 1: /* SinglePosFormat1 */
372 FT_UInt Coverage, ValueFormat;
375 OTV_LIMIT_CHECK( 4 );
376 Coverage = FT_NEXT_USHORT( p );
377 ValueFormat = FT_NEXT_USHORT( p );
379 otv_Coverage_validate( table + Coverage, otvalid, -1 );
380 otv_ValueRecord_validate( p, ValueFormat, otvalid ); /* Value */
384 case 2: /* SinglePosFormat2 */
386 FT_UInt Coverage, ValueFormat, ValueCount, len_value;
389 OTV_LIMIT_CHECK( 6 );
390 Coverage = FT_NEXT_USHORT( p );
391 ValueFormat = FT_NEXT_USHORT( p );
392 ValueCount = FT_NEXT_USHORT( p );
394 OTV_TRACE(( " (ValueCount = %d)\n", ValueCount ));
396 len_value = otv_value_length( ValueFormat );
398 otv_Coverage_validate( table + Coverage,
400 (FT_Int)ValueCount );
402 OTV_LIMIT_CHECK( ValueCount * len_value );
405 for ( ; ValueCount > 0; ValueCount-- )
407 otv_ValueRecord_validate( p, ValueFormat, otvalid );
421 /*************************************************************************/
422 /*************************************************************************/
424 /***** GPOS LOOKUP TYPE 2 *****/
426 /*************************************************************************/
427 /*************************************************************************/
430 otv_PairSet_validate( FT_Bytes table,
433 OTV_Validator otvalid )
436 FT_UInt value_len1, value_len2, PairValueCount;
439 OTV_NAME_ENTER( "PairSet" );
441 OTV_LIMIT_CHECK( 2 );
442 PairValueCount = FT_NEXT_USHORT( p );
444 OTV_TRACE(( " (PairValueCount = %d)\n", PairValueCount ));
446 value_len1 = otv_value_length( format1 );
447 value_len2 = otv_value_length( format2 );
449 OTV_LIMIT_CHECK( PairValueCount * ( value_len1 + value_len2 + 2 ) );
451 /* PairValueRecord */
452 for ( ; PairValueCount > 0; PairValueCount-- )
454 p += 2; /* skip SecondGlyph */
457 otv_ValueRecord_validate( p, format1, otvalid ); /* Value1 */
461 otv_ValueRecord_validate( p, format2, otvalid ); /* Value2 */
469 /* sets otvalid->extra3 (pointer to base table) */
472 otv_PairPos_validate( FT_Bytes table,
473 OTV_Validator otvalid )
479 OTV_NAME_ENTER( "PairPos" );
481 OTV_LIMIT_CHECK( 2 );
482 PosFormat = FT_NEXT_USHORT( p );
484 OTV_TRACE(( " (format %d)\n", PosFormat ));
486 otvalid->extra3 = table;
490 case 1: /* PairPosFormat1 */
492 FT_UInt Coverage, ValueFormat1, ValueFormat2, PairSetCount;
495 OTV_LIMIT_CHECK( 8 );
496 Coverage = FT_NEXT_USHORT( p );
497 ValueFormat1 = FT_NEXT_USHORT( p );
498 ValueFormat2 = FT_NEXT_USHORT( p );
499 PairSetCount = FT_NEXT_USHORT( p );
501 OTV_TRACE(( " (PairSetCount = %d)\n", PairSetCount ));
503 otv_Coverage_validate( table + Coverage, otvalid, -1 );
505 OTV_LIMIT_CHECK( PairSetCount * 2 );
508 for ( ; PairSetCount > 0; PairSetCount-- )
509 otv_PairSet_validate( table + FT_NEXT_USHORT( p ),
510 ValueFormat1, ValueFormat2, otvalid );
514 case 2: /* PairPosFormat2 */
516 FT_UInt Coverage, ValueFormat1, ValueFormat2, ClassDef1, ClassDef2;
517 FT_UInt ClassCount1, ClassCount2, len_value1, len_value2, count;
520 OTV_LIMIT_CHECK( 14 );
521 Coverage = FT_NEXT_USHORT( p );
522 ValueFormat1 = FT_NEXT_USHORT( p );
523 ValueFormat2 = FT_NEXT_USHORT( p );
524 ClassDef1 = FT_NEXT_USHORT( p );
525 ClassDef2 = FT_NEXT_USHORT( p );
526 ClassCount1 = FT_NEXT_USHORT( p );
527 ClassCount2 = FT_NEXT_USHORT( p );
529 OTV_TRACE(( " (ClassCount1 = %d)\n", ClassCount1 ));
530 OTV_TRACE(( " (ClassCount2 = %d)\n", ClassCount2 ));
532 len_value1 = otv_value_length( ValueFormat1 );
533 len_value2 = otv_value_length( ValueFormat2 );
535 otv_Coverage_validate( table + Coverage, otvalid, -1 );
536 otv_ClassDef_validate( table + ClassDef1, otvalid );
537 otv_ClassDef_validate( table + ClassDef2, otvalid );
539 OTV_LIMIT_CHECK( ClassCount1 * ClassCount2 *
540 ( len_value1 + len_value2 ) );
543 for ( ; ClassCount1 > 0; ClassCount1-- )
546 for ( count = ClassCount2; count > 0; count-- )
550 otv_ValueRecord_validate( p, ValueFormat1, otvalid );
555 otv_ValueRecord_validate( p, ValueFormat2, otvalid );
570 /*************************************************************************/
571 /*************************************************************************/
573 /***** GPOS LOOKUP TYPE 3 *****/
575 /*************************************************************************/
576 /*************************************************************************/
579 otv_CursivePos_validate( FT_Bytes table,
580 OTV_Validator otvalid )
586 OTV_NAME_ENTER( "CursivePos" );
588 OTV_LIMIT_CHECK( 2 );
589 PosFormat = FT_NEXT_USHORT( p );
591 OTV_TRACE(( " (format %d)\n", PosFormat ));
595 case 1: /* CursivePosFormat1 */
598 FT_UInt Coverage, EntryExitCount;
600 OTV_OPTIONAL_TABLE( EntryAnchor );
601 OTV_OPTIONAL_TABLE( ExitAnchor );
604 OTV_LIMIT_CHECK( 4 );
605 Coverage = FT_NEXT_USHORT( p );
606 EntryExitCount = FT_NEXT_USHORT( p );
608 OTV_TRACE(( " (EntryExitCount = %d)\n", EntryExitCount ));
610 otv_Coverage_validate( table + Coverage,
612 (FT_Int)EntryExitCount );
614 OTV_LIMIT_CHECK( EntryExitCount * 4 );
616 table_size = EntryExitCount * 4 + 4;
618 /* EntryExitRecord */
619 for ( ; EntryExitCount > 0; EntryExitCount-- )
621 OTV_OPTIONAL_OFFSET( EntryAnchor );
622 OTV_OPTIONAL_OFFSET( ExitAnchor );
624 OTV_SIZE_CHECK( EntryAnchor );
626 otv_Anchor_validate( table + EntryAnchor, otvalid );
628 OTV_SIZE_CHECK( ExitAnchor );
630 otv_Anchor_validate( table + ExitAnchor, otvalid );
643 /*************************************************************************/
644 /*************************************************************************/
646 /***** GPOS LOOKUP TYPE 4 *****/
648 /*************************************************************************/
649 /*************************************************************************/
651 /* UNDOCUMENTED (in OpenType 1.5): */
652 /* BaseRecord tables can contain NULL pointers. */
654 /* sets otvalid->extra2 (1) */
657 otv_MarkBasePos_validate( FT_Bytes table,
658 OTV_Validator otvalid )
664 OTV_NAME_ENTER( "MarkBasePos" );
666 OTV_LIMIT_CHECK( 2 );
667 PosFormat = FT_NEXT_USHORT( p );
669 OTV_TRACE(( " (format %d)\n", PosFormat ));
675 OTV_NEST2( MarkBasePosFormat1, BaseArray );
676 OTV_RUN( table, otvalid );
687 /*************************************************************************/
688 /*************************************************************************/
690 /***** GPOS LOOKUP TYPE 5 *****/
692 /*************************************************************************/
693 /*************************************************************************/
695 /* sets otvalid->extra2 (1) */
698 otv_MarkLigPos_validate( FT_Bytes table,
699 OTV_Validator otvalid )
705 OTV_NAME_ENTER( "MarkLigPos" );
707 OTV_LIMIT_CHECK( 2 );
708 PosFormat = FT_NEXT_USHORT( p );
710 OTV_TRACE(( " (format %d)\n", PosFormat ));
716 OTV_NEST3( MarkLigPosFormat1, LigatureArray, LigatureAttach );
717 OTV_RUN( table, otvalid );
728 /*************************************************************************/
729 /*************************************************************************/
731 /***** GPOS LOOKUP TYPE 6 *****/
733 /*************************************************************************/
734 /*************************************************************************/
736 /* sets otvalid->extra2 (0) */
739 otv_MarkMarkPos_validate( FT_Bytes table,
740 OTV_Validator otvalid )
746 OTV_NAME_ENTER( "MarkMarkPos" );
748 OTV_LIMIT_CHECK( 2 );
749 PosFormat = FT_NEXT_USHORT( p );
751 OTV_TRACE(( " (format %d)\n", PosFormat ));
757 OTV_NEST2( MarkMarkPosFormat1, Mark2Array );
758 OTV_RUN( table, otvalid );
769 /*************************************************************************/
770 /*************************************************************************/
772 /***** GPOS LOOKUP TYPE 7 *****/
774 /*************************************************************************/
775 /*************************************************************************/
777 /* sets otvalid->extra1 (lookup count) */
780 otv_ContextPos_validate( FT_Bytes table,
781 OTV_Validator otvalid )
787 OTV_NAME_ENTER( "ContextPos" );
789 OTV_LIMIT_CHECK( 2 );
790 PosFormat = FT_NEXT_USHORT( p );
792 OTV_TRACE(( " (format %d)\n", PosFormat ));
797 /* no need to check glyph indices/classes used as input for these */
798 /* context rules since even invalid glyph indices/classes return */
799 /* meaningful results */
801 otvalid->extra1 = otvalid->lookup_count;
802 OTV_NEST3( ContextPosFormat1, PosRuleSet, PosRule );
803 OTV_RUN( table, otvalid );
807 /* no need to check glyph indices/classes used as input for these */
808 /* context rules since even invalid glyph indices/classes return */
809 /* meaningful results */
811 OTV_NEST3( ContextPosFormat2, PosClassSet, PosClassRule );
812 OTV_RUN( table, otvalid );
816 OTV_NEST1( ContextPosFormat3 );
817 OTV_RUN( table, otvalid );
828 /*************************************************************************/
829 /*************************************************************************/
831 /***** GPOS LOOKUP TYPE 8 *****/
833 /*************************************************************************/
834 /*************************************************************************/
836 /* sets otvalid->extra1 (lookup count) */
839 otv_ChainContextPos_validate( FT_Bytes table,
840 OTV_Validator otvalid )
846 OTV_NAME_ENTER( "ChainContextPos" );
848 OTV_LIMIT_CHECK( 2 );
849 PosFormat = FT_NEXT_USHORT( p );
851 OTV_TRACE(( " (format %d)\n", PosFormat ));
856 /* no need to check glyph indices/classes used as input for these */
857 /* context rules since even invalid glyph indices/classes return */
858 /* meaningful results */
860 otvalid->extra1 = otvalid->lookup_count;
861 OTV_NEST3( ChainContextPosFormat1,
862 ChainPosRuleSet, ChainPosRule );
863 OTV_RUN( table, otvalid );
867 /* no need to check glyph indices/classes used as input for these */
868 /* context rules since even invalid glyph indices/classes return */
869 /* meaningful results */
871 OTV_NEST3( ChainContextPosFormat2,
872 ChainPosClassSet, ChainPosClassRule );
873 OTV_RUN( table, otvalid );
877 OTV_NEST1( ChainContextPosFormat3 );
878 OTV_RUN( table, otvalid );
889 /*************************************************************************/
890 /*************************************************************************/
892 /***** GPOS LOOKUP TYPE 9 *****/
894 /*************************************************************************/
895 /*************************************************************************/
897 /* uses otvalid->type_funcs */
900 otv_ExtensionPos_validate( FT_Bytes table,
901 OTV_Validator otvalid )
907 OTV_NAME_ENTER( "ExtensionPos" );
909 OTV_LIMIT_CHECK( 2 );
910 PosFormat = FT_NEXT_USHORT( p );
912 OTV_TRACE(( " (format %d)\n", PosFormat ));
916 case 1: /* ExtensionPosFormat1 */
918 FT_UInt ExtensionLookupType;
919 FT_ULong ExtensionOffset;
920 OTV_Validate_Func validate;
923 OTV_LIMIT_CHECK( 6 );
924 ExtensionLookupType = FT_NEXT_USHORT( p );
925 ExtensionOffset = FT_NEXT_ULONG( p );
927 if ( ExtensionLookupType == 0 || ExtensionLookupType >= 9 )
930 validate = otvalid->type_funcs[ExtensionLookupType - 1];
931 validate( table + ExtensionOffset, otvalid );
943 static const OTV_Validate_Func otv_gpos_validate_funcs[9] =
945 otv_SinglePos_validate,
946 otv_PairPos_validate,
947 otv_CursivePos_validate,
948 otv_MarkBasePos_validate,
949 otv_MarkLigPos_validate,
950 otv_MarkMarkPos_validate,
951 otv_ContextPos_validate,
952 otv_ChainContextPos_validate,
953 otv_ExtensionPos_validate
957 /* sets otvalid->type_count */
958 /* sets otvalid->type_funcs */
961 otv_GPOS_subtable_validate( FT_Bytes table,
962 OTV_Validator otvalid )
964 otvalid->type_count = 9;
965 otvalid->type_funcs = (OTV_Validate_Func*)otv_gpos_validate_funcs;
967 otv_Lookup_validate( table, otvalid );
971 /*************************************************************************/
972 /*************************************************************************/
974 /***** GPOS TABLE *****/
976 /*************************************************************************/
977 /*************************************************************************/
979 /* sets otvalid->glyph_count */
982 otv_GPOS_validate( FT_Bytes table,
984 FT_Validator ftvalid )
986 OTV_ValidatorRec validrec;
987 OTV_Validator otvalid = &validrec;
989 FT_UInt ScriptList, FeatureList, LookupList;
992 otvalid->root = ftvalid;
994 FT_TRACE3(( "validating GPOS table\n" ));
997 OTV_LIMIT_CHECK( 10 );
999 if ( FT_NEXT_ULONG( p ) != 0x10000UL ) /* Version */
1002 ScriptList = FT_NEXT_USHORT( p );
1003 FeatureList = FT_NEXT_USHORT( p );
1004 LookupList = FT_NEXT_USHORT( p );
1006 otvalid->type_count = 9;
1007 otvalid->type_funcs = (OTV_Validate_Func*)otv_gpos_validate_funcs;
1008 otvalid->glyph_count = glyph_count;
1010 otv_LookupList_validate( table + LookupList,
1012 otv_FeatureList_validate( table + FeatureList, table + LookupList,
1014 otv_ScriptList_validate( table + ScriptList, table + FeatureList,
1017 FT_TRACE4(( "\n" ));