1 /***************************************************************************/
5 /* Load the basic TrueType kerning table. This doesn't handle */
6 /* kerning data within the GPOS table at the moment. */
8 /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 by */
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 /***************************************************************************/
21 #include FT_INTERNAL_DEBUG_H
22 #include FT_INTERNAL_STREAM_H
23 #include FT_TRUETYPE_TAGS_H
29 /*************************************************************************/
31 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
32 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
33 /* messages during execution. */
36 #define FT_COMPONENT trace_ttkern
40 #define TT_KERN_INDEX( g1, g2 ) ( ( (FT_ULong)(g1) << 16 ) | (g2) )
43 FT_LOCAL_DEF( FT_Error
)
44 tt_face_load_kern( TT_Face face
,
51 FT_UInt nn
, num_tables
;
52 FT_UInt32 avail
= 0, ordered
= 0;
55 /* the kern table is optional; exit silently if it is missing */
56 error
= face
->goto_table( face
, TTAG_kern
, stream
, &table_size
);
60 if ( table_size
< 4 ) /* the case of a malformed table */
62 FT_ERROR(( "tt_face_load_kern:"
63 " kerning table is too small - ignored\n" ));
64 error
= SFNT_Err_Table_Missing
;
68 if ( FT_FRAME_EXTRACT( table_size
, face
->kern_table
) )
70 FT_ERROR(( "tt_face_load_kern:"
71 " could not extract kerning table\n" ));
75 face
->kern_table_size
= table_size
;
78 p_limit
= p
+ table_size
;
80 p
+= 2; /* skip version */
81 num_tables
= FT_NEXT_USHORT( p
);
83 if ( num_tables
> 32 ) /* we only support up to 32 sub-tables */
86 for ( nn
= 0; nn
< num_tables
; nn
++ )
88 FT_UInt num_pairs
, length
, coverage
;
90 FT_UInt32 mask
= (FT_UInt32
)1UL << nn
;
93 if ( p
+ 6 > p_limit
)
98 p
+= 2; /* skip version */
99 length
= FT_NEXT_USHORT( p
);
100 coverage
= FT_NEXT_USHORT( p
);
107 if ( p_next
> p_limit
) /* handle broken table */
110 /* only use horizontal kerning tables */
111 if ( ( coverage
& ~8 ) != 0x0001 ||
115 num_pairs
= FT_NEXT_USHORT( p
);
118 if ( ( p_next
- p
) / 6 < (int)num_pairs
) /* handle broken count */
119 num_pairs
= (FT_UInt
)( ( p_next
- p
) / 6 );
124 * Now check whether the pairs in this table are ordered.
125 * We then can use binary search.
133 old_pair
= FT_NEXT_ULONG( p
);
136 for ( count
= num_pairs
- 1; count
> 0; count
-- )
141 cur_pair
= FT_NEXT_ULONG( p
);
142 if ( cur_pair
<= old_pair
)
157 face
->num_kern_tables
= nn
;
158 face
->kern_avail_bits
= avail
;
159 face
->kern_order_bits
= ordered
;
167 tt_face_done_kern( TT_Face face
)
169 FT_Stream stream
= face
->root
.stream
;
172 FT_FRAME_RELEASE( face
->kern_table
);
173 face
->kern_table_size
= 0;
174 face
->num_kern_tables
= 0;
175 face
->kern_avail_bits
= 0;
176 face
->kern_order_bits
= 0;
180 FT_LOCAL_DEF( FT_Int
)
181 tt_face_get_kerning( TT_Face face
,
183 FT_UInt right_glyph
)
186 FT_UInt count
, mask
= 1;
187 FT_Byte
* p
= face
->kern_table
;
188 FT_Byte
* p_limit
= p
+ face
->kern_table_size
;
194 for ( count
= face
->num_kern_tables
;
195 count
> 0 && p
+ 6 <= p_limit
;
196 count
--, mask
<<= 1 )
199 FT_Byte
* next
= base
;
200 FT_UInt version
= FT_NEXT_USHORT( p
);
201 FT_UInt length
= FT_NEXT_USHORT( p
);
202 FT_UInt coverage
= FT_NEXT_USHORT( p
);
206 FT_UNUSED( version
);
209 next
= base
+ length
;
211 if ( next
> p_limit
) /* handle broken table */
214 if ( ( face
->kern_avail_bits
& mask
) == 0 )
220 num_pairs
= FT_NEXT_USHORT( p
);
223 if ( ( next
- p
) / 6 < (int)num_pairs
) /* handle broken count */
224 num_pairs
= (FT_UInt
)( ( next
- p
) / 6 );
226 switch ( coverage
>> 8 )
230 FT_ULong key0
= TT_KERN_INDEX( left_glyph
, right_glyph
);
233 if ( face
->kern_order_bits
& mask
) /* binary search */
236 FT_UInt max
= num_pairs
;
241 FT_UInt mid
= ( min
+ max
) >> 1;
242 FT_Byte
* q
= p
+ 6 * mid
;
246 key
= FT_NEXT_ULONG( q
);
250 value
= FT_PEEK_SHORT( q
);
259 else /* linear search */
264 for ( count2
= num_pairs
; count2
> 0; count2
-- )
266 FT_ULong key
= FT_NEXT_ULONG( p
);
271 value
= FT_PEEK_SHORT( p
);
281 * We don't support format 2 because we haven't seen a single font
282 * using it in real life...
292 if ( coverage
& 8 ) /* override or add */