1 /****************************************************************************
5 * Load the basic TrueType kerning table. This doesn't handle
6 * kerning data within the GPOS table at the moment.
8 * Copyright (C) 1996-2023 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.
20 #include <freetype/internal/ftdebug.h>
21 #include <freetype/internal/ftstream.h>
22 #include <freetype/tttags.h>
28 /**************************************************************************
30 * The macro FT_COMPONENT is used in trace mode. It is an implicit
31 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
32 * messages during execution.
35 #define FT_COMPONENT ttkern
39 #define TT_KERN_INDEX( g1, g2 ) ( ( (FT_ULong)(g1) << 16 ) | (g2) )
42 FT_LOCAL_DEF( FT_Error )
43 tt_face_load_kern( TT_Face face,
50 FT_UInt nn, num_tables;
51 FT_UInt32 avail = 0, ordered = 0;
54 /* the kern table is optional; exit silently if it is missing */
55 error = face->goto_table( face, TTAG_kern, stream, &table_size );
59 if ( table_size < 4 ) /* the case of a malformed table */
61 FT_ERROR(( "tt_face_load_kern:"
62 " kerning table is too small - ignored\n" ));
63 error = FT_THROW( Table_Missing );
67 if ( FT_FRAME_EXTRACT( table_size, face->kern_table ) )
69 FT_ERROR(( "tt_face_load_kern:"
70 " 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, format;
89 FT_UInt32 mask = (FT_UInt32)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 );
101 if ( length <= 6 + 8 )
106 if ( p_next > p_limit ) /* handle broken table */
109 format = coverage >> 8;
111 /* we currently only support format 0 kerning tables */
115 /* only use horizontal kerning tables */
116 if ( ( coverage & 3U ) != 0x0001 ||
120 num_pairs = FT_NEXT_USHORT( p );
123 if ( ( p_next - p ) < 6 * (int)num_pairs ) /* handle broken count */
124 num_pairs = (FT_UInt)( ( p_next - p ) / 6 );
129 * Now check whether the pairs in this table are ordered.
130 * We then can use binary search.
138 old_pair = FT_NEXT_ULONG( p );
141 for ( count = num_pairs - 1; count > 0; count-- )
146 cur_pair = FT_NEXT_ULONG( p );
147 if ( cur_pair < old_pair )
162 face->num_kern_tables = nn;
163 face->kern_avail_bits = avail;
164 face->kern_order_bits = ordered;
172 tt_face_done_kern( TT_Face face )
174 FT_Stream stream = face->root.stream;
177 FT_FRAME_RELEASE( face->kern_table );
178 face->kern_table_size = 0;
179 face->num_kern_tables = 0;
180 face->kern_avail_bits = 0;
181 face->kern_order_bits = 0;
185 FT_LOCAL_DEF( FT_Int )
186 tt_face_get_kerning( TT_Face face,
188 FT_UInt right_glyph )
197 if ( !face->kern_table )
200 p = face->kern_table;
201 p_limit = p + face->kern_table_size;
206 for ( count = face->num_kern_tables;
207 count > 0 && p + 6 <= p_limit;
208 count--, mask <<= 1 )
212 FT_UInt version = FT_NEXT_USHORT( p );
213 FT_UInt length = FT_NEXT_USHORT( p );
214 FT_UInt coverage = FT_NEXT_USHORT( p );
218 FT_UNUSED( version );
221 next = base + length;
223 if ( next > p_limit ) /* handle broken table */
226 if ( ( face->kern_avail_bits & mask ) == 0 )
229 FT_ASSERT( p + 8 <= next ); /* tested in tt_face_load_kern */
231 num_pairs = FT_NEXT_USHORT( p );
234 if ( ( next - p ) < 6 * (int)num_pairs ) /* handle broken count */
235 num_pairs = (FT_UInt)( ( next - p ) / 6 );
237 switch ( coverage >> 8 )
241 FT_ULong key0 = TT_KERN_INDEX( left_glyph, right_glyph );
244 if ( face->kern_order_bits & mask ) /* binary search */
247 FT_UInt max = num_pairs;
252 FT_UInt mid = ( min + max ) >> 1;
253 FT_Byte* q = p + 6 * mid;
257 key = FT_NEXT_ULONG( q );
261 value = FT_PEEK_SHORT( q );
270 else /* linear search */
275 for ( count2 = num_pairs; count2 > 0; count2-- )
277 FT_ULong key = FT_NEXT_ULONG( p );
282 value = FT_PEEK_SHORT( p );
292 * We don't support format 2 because we haven't seen a single font
293 * using it in real life...
303 if ( coverage & 8 ) /* override or add */