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 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 #ifdef FT_OPTIMIZE_MEMORY
46 FT_LOCAL_DEF( FT_Error
)
47 tt_face_load_kern( TT_Face face
,
54 FT_UInt nn
, num_tables
;
55 FT_UInt32 avail
= 0, ordered
= 0;
58 /* the kern table is optional; exit silently if it is missing */
59 error
= face
->goto_table( face
, TTAG_kern
, stream
, &table_size
);
63 if ( table_size
< 4 ) /* the case of a malformed table */
65 FT_ERROR(( "kerning table is too small - ignored\n" ));
66 error
= SFNT_Err_Table_Missing
;
70 if ( FT_FRAME_EXTRACT( table_size
, face
->kern_table
) )
72 FT_ERROR(( "could not extract kerning table\n" ));
76 face
->kern_table_size
= table_size
;
79 p_limit
= p
+ table_size
;
81 p
+= 2; /* skip version */
82 num_tables
= FT_NEXT_USHORT( p
);
84 if ( num_tables
> 32 ) /* we only support up to 32 sub-tables */
87 for ( nn
= 0; nn
< num_tables
; nn
++ )
89 FT_UInt num_pairs
, version
, length
, coverage
;
91 FT_UInt32 mask
= 1UL << nn
;
94 if ( p
+ 6 > p_limit
)
99 version
= FT_NEXT_USHORT( p
);
100 length
= FT_NEXT_USHORT( p
);
101 coverage
= FT_NEXT_USHORT( p
);
108 /* only use horizontal kerning tables */
109 if ( ( coverage
& ~8 ) != 0x0001 ||
113 num_pairs
= FT_NEXT_USHORT( p
);
116 if ( p
+ 6 * num_pairs
> p_limit
)
122 * Now check whether the pairs in this table are ordered.
123 * We then can use binary search.
131 old_pair
= FT_NEXT_ULONG( p
);
134 for ( count
= num_pairs
- 1; count
> 0; count
-- )
139 cur_pair
= FT_NEXT_ULONG( p
);
140 if ( cur_pair
<= old_pair
)
155 face
->num_kern_tables
= nn
;
156 face
->kern_avail_bits
= avail
;
157 face
->kern_order_bits
= ordered
;
165 tt_face_done_kern( TT_Face face
)
167 FT_Stream stream
= face
->root
.stream
;
170 FT_FRAME_RELEASE( face
->kern_table
);
171 face
->kern_table_size
= 0;
172 face
->num_kern_tables
= 0;
173 face
->kern_avail_bits
= 0;
174 face
->kern_order_bits
= 0;
178 FT_LOCAL_DEF( FT_Int
)
179 tt_face_get_kerning( TT_Face face
,
181 FT_UInt right_glyph
)
184 FT_UInt count
, mask
= 1;
185 FT_Byte
* p
= face
->kern_table
;
191 for ( count
= face
->num_kern_tables
; count
> 0; count
--, mask
<<= 1 )
194 FT_Byte
* next
= base
;
195 FT_UInt version
= FT_NEXT_USHORT( p
);
196 FT_UInt length
= FT_NEXT_USHORT( p
);
197 FT_UInt coverage
= FT_NEXT_USHORT( p
);
200 FT_UNUSED( version
);
203 next
= base
+ length
;
205 if ( ( face
->kern_avail_bits
& mask
) == 0 )
211 switch ( coverage
>> 8 )
215 FT_UInt num_pairs
= FT_NEXT_USHORT( p
);
216 FT_ULong key0
= TT_KERN_INDEX( left_glyph
, right_glyph
);
221 if ( face
->kern_order_bits
& mask
) /* binary search */
224 FT_UInt max
= num_pairs
;
229 FT_UInt mid
= ( min
+ max
) >> 1;
230 FT_Byte
* q
= p
+ 6 * mid
;
234 key
= FT_NEXT_ULONG( q
);
238 value
= FT_PEEK_SHORT( q
);
247 else /* linear search */
249 for ( count
= num_pairs
; count
> 0; count
-- )
251 FT_ULong key
= FT_NEXT_ULONG( p
);
256 value
= FT_PEEK_SHORT( p
);
266 * We don't support format 2 because we've never seen a single font
267 * using it in real life...
277 if ( coverage
& 8 ) /* overide or add */
289 #else /* !OPTIMIZE_MEMORY */
291 FT_CALLBACK_DEF( int )
292 tt_kern_pair_compare( const void* a
,
296 FT_LOCAL_DEF( FT_Error
)
297 tt_face_load_kern( TT_Face face
,
301 FT_Memory memory
= stream
->memory
;
303 FT_UInt n
, num_tables
;
306 /* the kern table is optional; exit silently if it is missing */
307 error
= face
->goto_table( face
, TTAG_kern
, stream
, 0 );
311 if ( FT_FRAME_ENTER( 4L ) )
314 (void)FT_GET_USHORT(); /* version */
315 num_tables
= FT_GET_USHORT();
319 for ( n
= 0; n
< num_tables
; n
++ )
325 if ( FT_FRAME_ENTER( 6L ) )
328 (void)FT_GET_USHORT(); /* version */
329 length
= FT_GET_USHORT() - 6; /* substract header length */
330 coverage
= FT_GET_USHORT();
334 if ( coverage
== 0x0001 )
341 /* found a horizontal format 0 kerning table! */
342 if ( FT_FRAME_ENTER( 8L ) )
345 num_pairs
= FT_GET_USHORT();
351 /* allocate array of kerning pairs */
352 if ( FT_QNEW_ARRAY( face
->kern_pairs
, num_pairs
) ||
353 FT_FRAME_ENTER( 6L * num_pairs
) )
356 pair
= face
->kern_pairs
;
357 limit
= pair
+ num_pairs
;
358 for ( ; pair
< limit
; pair
++ )
360 pair
->left
= FT_GET_USHORT();
361 pair
->right
= FT_GET_USHORT();
362 pair
->value
= FT_GET_USHORT();
367 face
->num_kern_pairs
= num_pairs
;
368 face
->kern_table_index
= n
;
370 /* ensure that the kerning pair table is sorted (yes, some */
371 /* fonts have unsorted tables!) */
375 TT_Kern0_Pair pair0
= face
->kern_pairs
;
376 FT_ULong prev
= TT_KERN_INDEX( pair0
->left
, pair0
->right
);
379 for ( pair0
++; pair0
< limit
; pair0
++ )
381 FT_ULong next
= TT_KERN_INDEX( pair0
->left
, pair0
->right
);
392 ft_qsort( (void*)face
->kern_pairs
, (int)num_pairs
,
393 sizeof ( TT_Kern0_PairRec
), tt_kern_pair_compare
);
399 if ( FT_STREAM_SKIP( length
) )
403 /* no kern table found -- doesn't matter */
404 face
->kern_table_index
= -1;
405 face
->num_kern_pairs
= 0;
406 face
->kern_pairs
= NULL
;
413 FT_CALLBACK_DEF( int )
414 tt_kern_pair_compare( const void* a
,
417 TT_Kern0_Pair pair1
= (TT_Kern0_Pair
)a
;
418 TT_Kern0_Pair pair2
= (TT_Kern0_Pair
)b
;
420 FT_ULong index1
= TT_KERN_INDEX( pair1
->left
, pair1
->right
);
421 FT_ULong index2
= TT_KERN_INDEX( pair2
->left
, pair2
->right
);
423 return index1
< index2
? -1
424 : ( index1
> index2
? 1
430 tt_face_done_kern( TT_Face face
)
432 FT_Memory memory
= face
->root
.stream
->memory
;
435 FT_FREE( face
->kern_pairs
);
436 face
->num_kern_pairs
= 0;
440 FT_LOCAL_DEF( FT_Int
)
441 tt_face_get_kerning( TT_Face face
,
443 FT_UInt right_glyph
)
449 if ( face
&& face
->kern_pairs
)
451 /* there are some kerning pairs in this font file! */
452 FT_ULong search_tag
= TT_KERN_INDEX( left_glyph
, right_glyph
);
457 right
= face
->num_kern_pairs
- 1;
459 while ( left
<= right
)
461 FT_Long middle
= left
+ ( ( right
- left
) >> 1 );
465 pair
= face
->kern_pairs
+ middle
;
466 cur_pair
= TT_KERN_INDEX( pair
->left
, pair
->right
);
468 if ( cur_pair
== search_tag
)
471 if ( cur_pair
< search_tag
)
482 result
= pair
->value
;
486 #endif /* !OPTIMIZE_MEMORY */