• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************/
2 /*                                                                         */
3 /*  ttkern.c                                                               */
4 /*                                                                         */
5 /*    Load the basic TrueType kerning table.  This doesn't handle          */
6 /*    kerning data within the GPOS table at the moment.                    */
7 /*                                                                         */
8 /*  Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 by       */
9 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
10 /*                                                                         */
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.                                        */
16 /*                                                                         */
17 /***************************************************************************/
18 
19 
20 #include <ft2build.h>
21 #include FT_INTERNAL_DEBUG_H
22 #include FT_INTERNAL_STREAM_H
23 #include FT_TRUETYPE_TAGS_H
24 #include "ttkern.h"
25 #include "ttload.h"
26 
27 #include "sferrors.h"
28 
29 
30   /*************************************************************************/
31   /*                                                                       */
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.                                            */
35   /*                                                                       */
36 #undef  FT_COMPONENT
37 #define FT_COMPONENT  trace_ttkern
38 
39 
40 #undef  TT_KERN_INDEX
41 #define TT_KERN_INDEX( g1, g2 )  ( ( (FT_ULong)(g1) << 16 ) | (g2) )
42 
43 
44   FT_LOCAL_DEF( FT_Error )
tt_face_load_kern(TT_Face face,FT_Stream stream)45   tt_face_load_kern( TT_Face    face,
46                      FT_Stream  stream )
47   {
48     FT_Error   error;
49     FT_ULong   table_size;
50     FT_Byte*   p;
51     FT_Byte*   p_limit;
52     FT_UInt    nn, num_tables;
53     FT_UInt32  avail = 0, ordered = 0;
54 
55 
56     /* the kern table is optional; exit silently if it is missing */
57     error = face->goto_table( face, TTAG_kern, stream, &table_size );
58     if ( error )
59       goto Exit;
60 
61     if ( table_size < 4 )  /* the case of a malformed table */
62     {
63       FT_ERROR(( "kerning table is too small - ignored\n" ));
64       error = SFNT_Err_Table_Missing;
65       goto Exit;
66     }
67 
68     if ( FT_FRAME_EXTRACT( table_size, face->kern_table ) )
69     {
70       FT_ERROR(( "could not extract kerning table\n" ));
71       goto Exit;
72     }
73 
74     face->kern_table_size = table_size;
75 
76     p       = face->kern_table;
77     p_limit = p + table_size;
78 
79     p         += 2; /* skip version */
80     num_tables = FT_NEXT_USHORT( p );
81 
82     if ( num_tables > 32 ) /* we only support up to 32 sub-tables */
83       num_tables = 32;
84 
85     for ( nn = 0; nn < num_tables; nn++ )
86     {
87       FT_UInt    num_pairs, length, coverage;
88       FT_Byte*   p_next;
89       FT_UInt32  mask = 1UL << nn;
90 
91 
92       if ( p + 6 > p_limit )
93         break;
94 
95       p_next = p;
96 
97       p += 2; /* skip version */
98       length   = FT_NEXT_USHORT( p );
99       coverage = FT_NEXT_USHORT( p );
100 
101       if ( length <= 6 )
102         break;
103 
104       p_next += length;
105 
106       if ( p_next > p_limit )  /* handle broken table */
107         p_next = p_limit;
108 
109       /* only use horizontal kerning tables */
110       if ( ( coverage & ~8 ) != 0x0001 ||
111            p + 8 > p_limit             )
112         goto NextTable;
113 
114       num_pairs = FT_NEXT_USHORT( p );
115       p        += 6;
116 
117       if ( ( p_next - p ) / 6 < (int)num_pairs ) /* handle broken count */
118         num_pairs = (FT_UInt)( ( p_next - p ) / 6 );
119 
120       avail |= mask;
121 
122       /*
123        *  Now check whether the pairs in this table are ordered.
124        *  We then can use binary search.
125        */
126       if ( num_pairs > 0 )
127       {
128         FT_UInt  count;
129         FT_UInt  old_pair;
130 
131 
132         old_pair = FT_NEXT_ULONG( p );
133         p       += 2;
134 
135         for ( count = num_pairs - 1; count > 0; count-- )
136         {
137           FT_UInt32  cur_pair;
138 
139 
140           cur_pair = FT_NEXT_ULONG( p );
141           if ( cur_pair <= old_pair )
142             break;
143 
144           p += 2;
145           old_pair = cur_pair;
146         }
147 
148         if ( count == 0 )
149           ordered |= mask;
150       }
151 
152     NextTable:
153       p = p_next;
154     }
155 
156     face->num_kern_tables = nn;
157     face->kern_avail_bits = avail;
158     face->kern_order_bits = ordered;
159 
160   Exit:
161     return error;
162   }
163 
164 
165   FT_LOCAL_DEF( void )
tt_face_done_kern(TT_Face face)166   tt_face_done_kern( TT_Face  face )
167   {
168     FT_Stream  stream = face->root.stream;
169 
170 
171     FT_FRAME_RELEASE( face->kern_table );
172     face->kern_table_size = 0;
173     face->num_kern_tables = 0;
174     face->kern_avail_bits = 0;
175     face->kern_order_bits = 0;
176   }
177 
178 
179   FT_LOCAL_DEF( FT_Int )
tt_face_get_kerning(TT_Face face,FT_UInt left_glyph,FT_UInt right_glyph)180   tt_face_get_kerning( TT_Face  face,
181                        FT_UInt  left_glyph,
182                        FT_UInt  right_glyph )
183   {
184     FT_Int    result = 0;
185     FT_UInt   count, mask = 1;
186     FT_Byte*  p       = face->kern_table;
187     FT_Byte*  p_limit = p + face->kern_table_size;
188 
189 
190     p   += 4;
191     mask = 0x0001;
192 
193     for ( count = face->num_kern_tables;
194           count > 0 && p + 6 <= p_limit;
195           count--, mask <<= 1 )
196     {
197       FT_Byte* base     = p;
198       FT_Byte* next     = base;
199       FT_UInt  version  = FT_NEXT_USHORT( p );
200       FT_UInt  length   = FT_NEXT_USHORT( p );
201       FT_UInt  coverage = FT_NEXT_USHORT( p );
202       FT_UInt  num_pairs;
203       FT_Int   value    = 0;
204 
205       FT_UNUSED( version );
206 
207 
208       next = base + length;
209 
210       if ( next > p_limit )  /* handle broken table */
211         next = p_limit;
212 
213       if ( ( face->kern_avail_bits & mask ) == 0 )
214         goto NextTable;
215 
216       if ( p + 8 > next )
217         goto NextTable;
218 
219       num_pairs = FT_NEXT_USHORT( p );
220       p        += 6;
221 
222       if ( ( next - p ) / 6 < (int)num_pairs )  /* handle broken count  */
223         num_pairs = (FT_UInt)( ( next - p ) / 6 );
224 
225       switch ( coverage >> 8 )
226       {
227       case 0:
228         {
229           FT_ULong  key0 = TT_KERN_INDEX( left_glyph, right_glyph );
230 
231 
232           if ( face->kern_order_bits & mask )   /* binary search */
233           {
234             FT_UInt   min = 0;
235             FT_UInt   max = num_pairs;
236 
237 
238             while ( min < max )
239             {
240               FT_UInt   mid = ( min + max ) >> 1;
241               FT_Byte*  q   = p + 6 * mid;
242               FT_ULong  key;
243 
244 
245               key = FT_NEXT_ULONG( q );
246 
247               if ( key == key0 )
248               {
249                 value = FT_PEEK_SHORT( q );
250                 goto Found;
251               }
252               if ( key < key0 )
253                 min = mid + 1;
254               else
255                 max = mid;
256             }
257           }
258           else /* linear search */
259           {
260             FT_UInt  count2;
261 
262 
263             for ( count2 = num_pairs; count2 > 0; count2-- )
264             {
265               FT_ULong  key = FT_NEXT_ULONG( p );
266 
267 
268               if ( key == key0 )
269               {
270                 value = FT_PEEK_SHORT( p );
271                 goto Found;
272               }
273               p += 2;
274             }
275           }
276         }
277         break;
278 
279        /*
280         *  We don't support format 2 because we haven't seen a single font
281         *  using it in real life...
282         */
283 
284       default:
285         ;
286       }
287 
288       goto NextTable;
289 
290     Found:
291       if ( coverage & 8 ) /* override or add */
292         result = value;
293       else
294         result += value;
295 
296     NextTable:
297       p = next;
298     }
299 
300     return result;
301   }
302 
303 #undef TT_KERN_INDEX
304 
305 /* END */
306