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 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
30 /*************************************************************************/
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. */
37 #define FT_COMPONENT trace_ttkern
41 #define TT_KERN_INDEX( g1, g2 ) ( ( (FT_ULong)(g1) << 16 ) | (g2) )
44 FT_LOCAL_DEF( FT_Error
)
45 tt_face_load_kern( TT_Face face
,
52 FT_UInt nn
, num_tables
;
53 FT_UInt32 avail
= 0, ordered
= 0;
56 /* the kern table is optional; exit silently if it is missing */
57 error
= face
->goto_table( face
, TTAG_kern
, stream
, &table_size
);
61 if ( table_size
< 4 ) /* the case of a malformed table */
63 FT_ERROR(( "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(( "could not extract kerning table\n" ));
74 face
->kern_table_size
= table_size
;
77 p_limit
= p
+ table_size
;
79 p
+= 2; /* skip version */
80 num_tables
= FT_NEXT_USHORT( p
);
82 if ( num_tables
> 32 ) /* we only support up to 32 sub-tables */
85 for ( nn
= 0; nn
< num_tables
; nn
++ )
87 FT_UInt num_pairs
, length
, coverage
;
89 FT_UInt32 mask
= 1UL << nn
;
92 if ( p
+ 6 > p_limit
)
97 p
+= 2; /* skip version */
98 length
= FT_NEXT_USHORT( p
);
99 coverage
= FT_NEXT_USHORT( p
);
106 /* only use horizontal kerning tables */
107 if ( ( coverage
& ~8 ) != 0x0001 ||
111 num_pairs
= FT_NEXT_USHORT( p
);
114 if ( p
+ 6 * num_pairs
> p_limit
)
120 * Now check whether the pairs in this table are ordered.
121 * We then can use binary search.
129 old_pair
= FT_NEXT_ULONG( p
);
132 for ( count
= num_pairs
- 1; count
> 0; count
-- )
137 cur_pair
= FT_NEXT_ULONG( p
);
138 if ( cur_pair
<= old_pair
)
153 face
->num_kern_tables
= nn
;
154 face
->kern_avail_bits
= avail
;
155 face
->kern_order_bits
= ordered
;
163 tt_face_done_kern( TT_Face face
)
165 FT_Stream stream
= face
->root
.stream
;
168 FT_FRAME_RELEASE( face
->kern_table
);
169 face
->kern_table_size
= 0;
170 face
->num_kern_tables
= 0;
171 face
->kern_avail_bits
= 0;
172 face
->kern_order_bits
= 0;
176 FT_LOCAL_DEF( FT_Int
)
177 tt_face_get_kerning( TT_Face face
,
179 FT_UInt right_glyph
)
182 FT_UInt count
, mask
= 1;
183 FT_Byte
* p
= face
->kern_table
;
189 for ( count
= face
->num_kern_tables
; count
> 0; count
--, mask
<<= 1 )
192 FT_Byte
* next
= base
;
193 FT_UInt version
= FT_NEXT_USHORT( p
);
194 FT_UInt length
= FT_NEXT_USHORT( p
);
195 FT_UInt coverage
= FT_NEXT_USHORT( p
);
198 FT_UNUSED( version
);
201 next
= base
+ length
;
203 if ( ( face
->kern_avail_bits
& mask
) == 0 )
209 switch ( coverage
>> 8 )
213 FT_UInt num_pairs
= FT_NEXT_USHORT( p
);
214 FT_ULong key0
= TT_KERN_INDEX( left_glyph
, right_glyph
);
219 if ( face
->kern_order_bits
& mask
) /* binary search */
222 FT_UInt max
= num_pairs
;
227 FT_UInt mid
= ( min
+ max
) >> 1;
228 FT_Byte
* q
= p
+ 6 * mid
;
232 key
= FT_NEXT_ULONG( q
);
236 value
= FT_PEEK_SHORT( q
);
245 else /* linear search */
250 for ( count2
= num_pairs
; count2
> 0; count2
-- )
252 FT_ULong key
= FT_NEXT_ULONG( p
);
257 value
= FT_PEEK_SHORT( p
);
267 * We don't support format 2 because we haven't seen a single font
268 * using it in real life...
278 if ( coverage
& 8 ) /* override or add */