1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
|
/*******************************************************************
*
* ftxkern.c 1.0
*
* Kerning support extension.
*
* Copyright 1996-2000 by
* David Turner, Robert Wilhelm, and Werner Lemberg.
*
* This file is part of the FreeType project, and may only be used
* modified and distributed under the terms of the FreeType project
* license, LICENSE.TXT. By continuing to use, modify, or distribute
* this file you indicate that you have read the license and
* understand and accept it fully.
*
*
* The kerning support is currently part of the engine extensions.
*
******************************************************************/
#include "ttconfig.h"
#include "ftxkern.h"
#include "ttextend.h"
#include "tttypes.h"
#include "ttdebug.h"
#include "ttmemory.h"
#include "ttfile.h"
#include "ttobjs.h"
#include "ttload.h" /* For the macros */
#include "tttags.h"
#include <stdlib.h> /* for qsort */
/* Required by the tracing mode */
#undef TT_COMPONENT
#define TT_COMPONENT trace_any
#define KERNING_ID Build_Extension_ID( 'k', 'e', 'r', 'n' )
#undef KERN_INDEX
#define KERN_INDEX(g1,g2) (((TT_ULong)g1 << 16) | g2)
/* compare two kerning pairs, for qsort() */
static
int __cdecl compare_kern_pairs( const void* a, const void* b )
{
TT_Kern_0_Pair* pair1 = (TT_Kern_0_Pair*)a;
TT_Kern_0_Pair* pair2 = (TT_Kern_0_Pair*)b;
TT_ULong index1 = KERN_INDEX( pair1->left, pair1->right );
TT_ULong index2 = KERN_INDEX( pair2->left, pair2->right );
return ( index1 < index2 ? -1 :
( index1 > index2 ? 1 : 0 ));
}
/* check whether the kern table is sorted */
static
int is_sorted( TT_Kern_0_Pair* table, TT_ULong num_pairs )
{
TT_ULong i;
for ( i=1; i<num_pairs; i++ )
{
if ( compare_kern_pairs(&table[i-1], &table[i]) != -1 )
return 0;
}
return 1;
}
/*******************************************************************
*
* Function : SubTable_Load_0
*
* Description : Loads a format 0 kerning subtable data.
*
* Input : kern0 pointer to the kerning subtable
*
* Output : error code
*
* Notes : - Assumes that the stream is already `used'
*
* - the file cursor must be set by the caller
*
* - in case of error, the function _must_ destroy
* the data it allocates!
*
******************************************************************/
static TT_Error Subtable_Load_0( TT_Kern_0* kern0,
PFace input )
{
DEFINE_LOAD_LOCALS( input->stream );
UShort num_pairs, n;
if ( ACCESS_Frame( 8L ) )
return error;
num_pairs = GET_UShort();
kern0->nPairs = 0;
kern0->searchRange = GET_UShort();
kern0->entrySelector = GET_UShort();
kern0->rangeShift = GET_UShort();
/* we only set kern0->nPairs if the subtable has been loaded */
FORGET_Frame();
if ( ALLOC_ARRAY( kern0->pairs, num_pairs, TT_Kern_0_Pair ) )
return error;
if ( ACCESS_Frame( num_pairs * 6L ) )
goto Fail;
for ( n = 0; n < num_pairs; n++ )
{
kern0->pairs[n].left = GET_UShort();
kern0->pairs[n].right = GET_UShort();
kern0->pairs[n].value = GET_UShort();
if ( kern0->pairs[n].left >= input->numGlyphs ||
kern0->pairs[n].right >= input->numGlyphs )
{
FORGET_Frame();
error = TT_Err_Invalid_Kerning_Table;
goto Fail;
}
}
FORGET_Frame();
/* we're ok, set the pairs count */
kern0->nPairs = num_pairs;
/* the spec says that the kerning pairs must be sorted,
but some brain damaged font producers don't do that correctly.. (JvR 3/4/2000) */
if ( !is_sorted( kern0->pairs, num_pairs ) )
qsort( kern0->pairs, num_pairs, sizeof(TT_Kern_0_Pair), compare_kern_pairs );
return TT_Err_Ok;
Fail:
FREE( kern0->pairs );
return error;
}
/*******************************************************************
*
* Function : SubTable_Load_2
*
* Description : Loads a format 2 kerning subtable data.
*
* Input : kern2 pointer to the kerning subtable
* length subtable length. This is required as
* the subheader doesn't give any indication
* of the size of the `array' table.
*
* Output : error code
*
* Notes : - Assumes that the stream is already `used'
*
* - the file cursor must be set by the caller
*
* - in case of error, the function _must_ destroy
* the data it allocates!
*
******************************************************************/
static TT_Error Subtable_Load_2( TT_Kern_2* kern2,
PFace input )
{
DEFINE_LOAD_LOCALS( input->stream );
Long table_base;
UShort left_offset, right_offset, array_offset;
ULong array_size;
UShort left_max, right_max, n;
/* record the table offset */
table_base = FILE_Pos();
if ( ACCESS_Frame( 8L ) )
return error;
kern2->rowWidth = GET_UShort();
left_offset = GET_UShort();
right_offset = GET_UShort();
array_offset = GET_UShort();
FORGET_Frame();
/* first load left and right glyph classes */
if ( FILE_Seek( table_base + left_offset ) ||
ACCESS_Frame( 4L ) )
return error;
kern2->leftClass.firstGlyph = GET_UShort();
kern2->leftClass.nGlyphs = GET_UShort();
FORGET_Frame();
if ( ALLOC_ARRAY( kern2->leftClass.classes,
kern2->leftClass.nGlyphs,
UShort ) )
return error;
/* load left offsets */
if ( ACCESS_Frame( kern2->leftClass.nGlyphs * 2L ) )
goto Fail_Left;
for ( n = 0; n < kern2->leftClass.nGlyphs; n++ )
kern2->leftClass.classes[n] = GET_UShort();
FORGET_Frame();
/* right class */
if ( FILE_Seek( table_base + right_offset ) ||
ACCESS_Frame( 4L ) )
goto Fail_Left;
kern2->rightClass.firstGlyph = GET_UShort();
kern2->rightClass.nGlyphs = GET_UShort();
FORGET_Frame();
if ( ALLOC_ARRAY( kern2->rightClass.classes,
kern2->rightClass.nGlyphs,
UShort ) )
goto Fail_Left;
/* load right offsets */
if ( ACCESS_Frame( kern2->rightClass.nGlyphs * 2L ) )
goto Fail_Right;
for ( n = 0; n < kern2->rightClass.nGlyphs; n++ )
kern2->rightClass.classes[n] = GET_UShort();
FORGET_Frame();
/* Now load the kerning array. We don't have its size, we */
/* must compute it from what we know. */
/* We thus compute the maximum left and right offsets and */
/* add them to get the array size. */
left_max = right_max = 0;
for ( n = 0; n < kern2->leftClass.nGlyphs; n++ )
left_max = MAX( left_max, kern2->leftClass.classes[n] );
for ( n = 0; n < kern2->rightClass.nGlyphs; n++ )
right_max = MAX( right_max, kern2->leftClass.classes[n] );
array_size = left_max + right_max + 2;
if ( ALLOC( kern2->array, array_size ) )
goto Fail_Right;
if ( ACCESS_Frame( array_size ) )
goto Fail_Array;
for ( n = 0; n < array_size/2; n++ )
kern2->array[n] = GET_Short();
FORGET_Frame();
/* we're good now */
return TT_Err_Ok;
Fail_Array:
FREE( kern2->array );
Fail_Right:
FREE( kern2->rightClass.classes );
kern2->rightClass.nGlyphs = 0;
Fail_Left:
FREE( kern2->leftClass.classes );
kern2->leftClass.nGlyphs = 0;
return error;
}
/*******************************************************************
*
* Function : Kerning_Create
*
* Description : Creates the kerning directory if a face is
* loaded. The tables however are loaded on
* demand to save space.
*
* Input : face pointer to the parent face object
* kern pointer to the extension's kerning field
*
* Output : error code
*
* Notes : as in all constructors, the memory allocated isn't
* released in case of failure. Rather, the task is left
* to the destructor (which is called if an error
* occurs during the loading of a face).
*
******************************************************************/
static TT_Error Kerning_Create( void* ext,
PFace face )
{
DEFINE_LOAD_LOCALS( face->stream );
TT_Kerning* kern = (TT_Kerning*)ext;
UShort num_tables;
Long table;
TT_Kern_Subtable* sub;
/* by convention */
if ( !kern )
return TT_Err_Ok;
/* Now load the kerning directory. We're called from the face */
/* constructor. We thus need not use the stream. */
kern->version = 0;
kern->nTables = 0;
kern->tables = NULL;
table = TT_LookUp_Table( face, TTAG_kern );
if ( table < 0 )
return TT_Err_Ok; /* The table is optional */
if ( FILE_Seek( face->dirTables[table].Offset ) ||
ACCESS_Frame( 4L ) )
return error;
kern->version = GET_UShort();
num_tables = GET_UShort();
FORGET_Frame();
/* we don't set kern->nTables until we have allocated the array */
if ( ALLOC_ARRAY( kern->tables, num_tables, TT_Kern_Subtable ) )
return error;
kern->nTables = num_tables;
/* now load the directory entries, but do _not_ load the tables ! */
sub = kern->tables;
for ( table = 0; table < num_tables; table++ )
{
if ( ACCESS_Frame( 6L ) )
return error;
sub->loaded = FALSE; /* redundant, but good to see */
sub->version = GET_UShort();
sub->length = GET_UShort() - 6; /* substract header length */
sub->format = GET_Byte();
sub->coverage = GET_Byte();
FORGET_Frame();
sub->offset = FILE_Pos();
/* now skip to the next table */
if ( FILE_Skip( sub->length ) )
return error;
sub++;
}
/* that's fine, leave now */
return TT_Err_Ok;
}
/*******************************************************************
*
* Function : Kerning_Destroy
*
* Description : Destroys all kerning information.
*
* Input : kern pointer to the extension's kerning field
*
* Output : error code
*
* Notes : This function is a destructor; it must be able
* to destroy partially built tables.
*
******************************************************************/
static TT_Error Kerning_Destroy( void* ext,
PFace face )
{
TT_Kerning* kern = (TT_Kerning*)ext;
TT_Kern_Subtable* sub;
UShort n;
/* by convention */
if ( !kern )
return TT_Err_Ok;
if ( kern->nTables == 0 )
return TT_Err_Ok; /* no tables to release */
/* scan the table directory and release loaded entries */
sub = kern->tables;
for ( n = 0; n < kern->nTables; n++ )
{
if ( sub->loaded )
{
switch ( sub->format )
{
case 0:
FREE( sub->t.kern0.pairs );
sub->t.kern0.nPairs = 0;
sub->t.kern0.searchRange = 0;
sub->t.kern0.entrySelector = 0;
sub->t.kern0.rangeShift = 0;
break;
case 2:
FREE( sub->t.kern2.leftClass.classes );
sub->t.kern2.leftClass.firstGlyph = 0;
sub->t.kern2.leftClass.nGlyphs = 0;
FREE( sub->t.kern2.rightClass.classes );
sub->t.kern2.rightClass.firstGlyph = 0;
sub->t.kern2.rightClass.nGlyphs = 0;
FREE( sub->t.kern2.array );
sub->t.kern2.rowWidth = 0;
break;
default:
; /* invalid subtable format - do nothing */
}
sub->loaded = FALSE;
sub->version = 0;
sub->offset = 0;
sub->length = 0;
sub->coverage = 0;
sub->format = 0;
}
sub++;
}
FREE( kern->tables );
kern->nTables = 0;
return TT_Err_Ok;
}
/*******************************************************************
*
* Function : TT_Get_Kerning_Directory
*
* Description : Returns a given face's kerning directory.
*
* Input : face handle to the face object
* directory pointer to client's target directory
*
* Output : error code
*
* Notes : The kerning table directory is loaded with the face
* through the extension constructor. However, the kerning
* tables themselves are only loaded on demand, as they
* may represent a lot of data, unneeded by most uses of
* the engine.
*
******************************************************************/
EXPORT_FUNC
TT_Error TT_Get_Kerning_Directory( TT_Face face,
TT_Kerning* directory )
{
PFace faze = HANDLE_Face( face );
TT_Error error;
TT_Kerning* kerning;
if ( !faze )
return TT_Err_Invalid_Face_Handle;
/* copy directory header */
error = TT_Extension_Get( faze, KERNING_ID, (void**)&kerning );
if ( !error )
*directory = *kerning;
return error;
}
/*******************************************************************
*
* Function : TT_Load_Kerning_Table
*
* Description : Loads a kerning table intro memory.
*
* Input : face face handle
* kern_index index in the face's kerning directory
*
* Output : error code
*
* Notes :
*
******************************************************************/
EXPORT_FUNC
TT_Error TT_Load_Kerning_Table( TT_Face face,
TT_UShort kern_index )
{
TT_Error error;
TT_Stream stream;
TT_Kerning* kern;
TT_Kern_Subtable* sub;
PFace faze = HANDLE_Face( face );
if ( !faze )
return TT_Err_Invalid_Face_Handle;
error = TT_Extension_Get( faze, KERNING_ID, (void**)&kern );
if ( error )
return error;
if ( kern->nTables == 0 )
return TT_Err_Table_Missing;
if ( kern_index >= kern->nTables )
return TT_Err_Invalid_Argument;
sub = kern->tables + kern_index;
if ( sub->format != 0 && sub->format != 2 )
return TT_Err_Invalid_Kerning_Table_Format;
/* now access stream */
if ( USE_Stream( faze->stream, stream ) )
return error;
if ( FILE_Seek( sub->offset ) )
goto Fail;
if ( sub->format == 0 )
error = Subtable_Load_0( &sub->t.kern0, faze );
else if ( sub->format == 2 )
error = Subtable_Load_2( &sub->t.kern2, faze );
if ( !error )
sub->loaded = TRUE;
Fail:
/* release stream */
DONE_Stream( stream );
return error;
}
EXPORT_FUNC
TT_Error TT_Init_Kerning_Extension( TT_Engine engine )
{
PEngine_Instance _engine = HANDLE_Engine( engine );
TT_Error error;
if ( !_engine )
return TT_Err_Invalid_Engine;
error = TT_Register_Extension( _engine,
KERNING_ID,
sizeof ( TT_Kerning ),
Kerning_Create,
Kerning_Destroy );
return error;
}
/* END */
|