ReactOS 0.4.16-dev-2332-g4cba65d
ttkern.c
Go to the documentation of this file.
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 (C) 1996-2020 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
22#include <freetype/tttags.h>
23#include "ttkern.h"
24
25#include "sferrors.h"
26
27
28 /**************************************************************************
29 *
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.
33 */
34#undef FT_COMPONENT
35#define FT_COMPONENT ttkern
36
37
38#undef TT_KERN_INDEX
39#define TT_KERN_INDEX( g1, g2 ) ( ( (FT_ULong)(g1) << 16 ) | (g2) )
40
41
45 {
48 FT_Byte* p;
49 FT_Byte* p_limit;
50 FT_UInt nn, num_tables;
51 FT_UInt32 avail = 0, ordered = 0;
52
53
54 /* the kern table is optional; exit silently if it is missing */
55 error = face->goto_table( face, TTAG_kern, stream, &table_size );
56 if ( error )
57 goto Exit;
58
59 if ( table_size < 4 ) /* the case of a malformed table */
60 {
61 FT_ERROR(( "tt_face_load_kern:"
62 " kerning table is too small - ignored\n" ));
63 error = FT_THROW( Table_Missing );
64 goto Exit;
65 }
66
67 if ( FT_FRAME_EXTRACT( table_size, face->kern_table ) )
68 {
69 FT_ERROR(( "tt_face_load_kern:"
70 " 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, format;
88 FT_Byte* p_next;
89 FT_UInt32 mask = (FT_UInt32)1UL << nn;
90
91
92 if ( p + 6 > p_limit )
93 break;
94
95 p_next = p;
96
97 p += 2; /* skip version */
99 coverage = FT_NEXT_USHORT( p );
100
101 if ( length <= 6 + 8 )
102 break;
103
104 p_next += length;
105
106 if ( p_next > p_limit ) /* handle broken table */
107 p_next = p_limit;
108
109 format = coverage >> 8;
110
111 /* we currently only support format 0 kerning tables */
112 if ( format != 0 )
113 goto NextTable;
114
115 /* only use horizontal kerning tables */
116 if ( ( coverage & 3U ) != 0x0001 ||
117 p + 8 > p_next )
118 goto NextTable;
119
120 num_pairs = FT_NEXT_USHORT( p );
121 p += 6;
122
123 if ( ( p_next - p ) < 6 * (int)num_pairs ) /* handle broken count */
124 num_pairs = (FT_UInt)( ( p_next - p ) / 6 );
125
126 avail |= mask;
127
128 /*
129 * Now check whether the pairs in this table are ordered.
130 * We then can use binary search.
131 */
132 if ( num_pairs > 0 )
133 {
135 FT_ULong old_pair;
136
137
138 old_pair = FT_NEXT_ULONG( p );
139 p += 2;
140
141 for ( count = num_pairs - 1; count > 0; count-- )
142 {
143 FT_UInt32 cur_pair;
144
145
146 cur_pair = FT_NEXT_ULONG( p );
147 if ( cur_pair <= old_pair )
148 break;
149
150 p += 2;
151 old_pair = cur_pair;
152 }
153
154 if ( count == 0 )
155 ordered |= mask;
156 }
157
158 NextTable:
159 p = p_next;
160 }
161
162 face->num_kern_tables = nn;
163 face->kern_avail_bits = avail;
164 face->kern_order_bits = ordered;
165
166 Exit:
167 return error;
168 }
169
170
171 FT_LOCAL_DEF( void )
173 {
174 FT_Stream stream = face->root.stream;
175
176
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;
182 }
183
184
189 {
190 FT_Int result = 0;
192 FT_Byte* p = face->kern_table;
193 FT_Byte* p_limit = p + face->kern_table_size;
194
195
196 p += 4;
197 mask = 0x0001;
198
199 for ( count = face->num_kern_tables;
200 count > 0 && p + 6 <= p_limit;
201 count--, mask <<= 1 )
202 {
203 FT_Byte* base = p;
204 FT_Byte* next;
207 FT_UInt coverage = FT_NEXT_USHORT( p );
208 FT_UInt num_pairs;
209 FT_Int value = 0;
210
212
213
214 next = base + length;
215
216 if ( next > p_limit ) /* handle broken table */
217 next = p_limit;
218
219 if ( ( face->kern_avail_bits & mask ) == 0 )
220 goto NextTable;
221
222 FT_ASSERT( p + 8 <= next ); /* tested in tt_face_load_kern */
223
224 num_pairs = FT_NEXT_USHORT( p );
225 p += 6;
226
227 if ( ( next - p ) < 6 * (int)num_pairs ) /* handle broken count */
228 num_pairs = (FT_UInt)( ( next - p ) / 6 );
229
230 switch ( coverage >> 8 )
231 {
232 case 0:
233 {
235
236
237 if ( face->kern_order_bits & mask ) /* binary search */
238 {
239 FT_UInt min = 0;
240 FT_UInt max = num_pairs;
241
242
243 while ( min < max )
244 {
245 FT_UInt mid = ( min + max ) >> 1;
246 FT_Byte* q = p + 6 * mid;
248
249
250 key = FT_NEXT_ULONG( q );
251
252 if ( key == key0 )
253 {
254 value = FT_PEEK_SHORT( q );
255 goto Found;
256 }
257 if ( key < key0 )
258 min = mid + 1;
259 else
260 max = mid;
261 }
262 }
263 else /* linear search */
264 {
265 FT_UInt count2;
266
267
268 for ( count2 = num_pairs; count2 > 0; count2-- )
269 {
271
272
273 if ( key == key0 )
274 {
275 value = FT_PEEK_SHORT( p );
276 goto Found;
277 }
278 p += 2;
279 }
280 }
281 }
282 break;
283
284 /*
285 * We don't support format 2 because we haven't seen a single font
286 * using it in real life...
287 */
288
289 default:
290 ;
291 }
292
293 goto NextTable;
294
295 Found:
296 if ( coverage & 8 ) /* override or add */
297 result = value;
298 else
299 result += value;
300
301 NextTable:
302 p = next;
303 }
304
305 return result;
306 }
307
308#undef TT_KERN_INDEX
309
310/* END */
static int avail
Definition: adh-main.c:39
return Found
Definition: dirsup.c:1270
#define FT_LOCAL_DEF(x)
static const WCHAR version[]
Definition: asmname.c:66
#define FT_ASSERT(condition)
Definition: ftdebug.h:241
#define FT_ERROR(varformat)
Definition: ftdebug.h:211
#define FT_THROW(e)
Definition: ftdebug.h:243
#define FT_FRAME_RELEASE(bytes)
Definition: ftstream.h:562
#define FT_NEXT_USHORT(buffer)
Definition: ftstream.h:244
#define FT_PEEK_SHORT(p)
Definition: ftstream.h:183
#define FT_FRAME_EXTRACT(size, bytes)
Definition: ftstream.h:556
#define FT_NEXT_ULONG(buffer)
Definition: ftstream.h:256
unsigned long FT_ULong
Definition: fttypes.h:253
unsigned char FT_Byte
Definition: fttypes.h:154
int FT_Error
Definition: fttypes.h:299
unsigned int FT_UInt
Definition: fttypes.h:231
signed int FT_Int
Definition: fttypes.h:220
GLuint GLuint GLsizei count
Definition: gl.h:1545
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
GLenum GLint GLuint mask
Definition: glext.h:6028
GLenum GLuint GLint GLenum face
Definition: glext.h:7025
GLuint GLsizei GLsizei * length
Definition: glext.h:6040
GLuint64EXT * result
Definition: glext.h:11304
GLfloat GLfloat p
Definition: glext.h:8902
GLuint64EXT GLuint GLuint GLenum GLenum GLuint GLuint key0
Definition: glext.h:10608
#define error(str)
Definition: mkdosfs.c:1605
#define min(a, b)
Definition: monoChain.cc:55
#define FT_UNUSED(arg)
static unsigned __int64 next
Definition: rand_nt.c:6
LOCAL int table_size
Definition: write.c:65
static void Exit(void)
Definition: sock.c:1330
Definition: format.c:58
Definition: copy.c:22
Definition: parse.h:23
#define max(a, b)
Definition: svc.c:63
FT_UInt left_glyph
Definition: ttdriver.c:203
FT_UInt FT_UInt right_glyph
Definition: ttdriver.c:204
tt_face_done_kern(TT_Face face)
Definition: ttkern.c:172
#define TT_KERN_INDEX(g1, g2)
Definition: ttkern.c:39
tt_face_get_kerning(TT_Face face, FT_UInt left_glyph, FT_UInt right_glyph)
Definition: ttkern.c:186
tt_face_load_kern(TT_Face face, FT_Stream stream)
Definition: ttkern.c:43
#define TTAG_kern
Definition: tttags.h:73
Definition: pdh_main.c:96