1
/***************************************************************************/
5
/* AFM support for Type 1 fonts (body). */
7
/* Copyright 1996-2001, 2002, 2003 by */
8
/* David Turner, Robert Wilhelm, and Werner Lemberg. */
10
/* This file is part of the FreeType project, and may only be used, */
11
/* modified, and distributed under the terms of the FreeType project */
12
/* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13
/* this file you indicate that you have read the license and */
14
/* understand and accept it fully. */
16
/***************************************************************************/
21
#include FT_INTERNAL_STREAM_H
22
#include FT_INTERNAL_TYPE1_TYPES_H
25
/*************************************************************************/
27
/* The macro FT_COMPONENT is used in trace mode. It is an implicit */
28
/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
29
/* messages during execution. */
32
#define FT_COMPONENT trace_t1afm
36
T1_Done_AFM( FT_Memory memory,
39
FT_FREE( afm->kern_pairs );
46
#define IS_KERN_PAIR( p ) ( p[0] == 'K' && p[1] == 'P' )
48
#define IS_ALPHANUM( c ) ( ft_isalnum( c ) || \
53
/* read a glyph name and return the equivalent glyph index */
55
afm_atoindex( FT_Byte** start,
66
while ( ( *p == ' ' || *p == '\t' || *p == ':' || *p == ';' ) &&
71
/* now, read glyph name */
72
while ( IS_ALPHANUM( *p ) && p < limit )
77
if ( len > 0 && len < 64 )
82
/* copy glyph name to intermediate array */
83
FT_MEM_COPY( temp, *start, len );
86
/* lookup glyph name in face array */
87
for ( n = 0; n < type1->num_glyphs; n++ )
89
char* gname = (char*)type1->glyph_names[n];
92
if ( gname && gname[0] == temp[0] && ft_strcmp( gname, temp ) == 0 )
104
/* read an integer */
106
afm_atoi( FT_Byte** start,
114
/* skip everything that is not a number */
115
while ( p < limit && !isdigit( *p ) )
124
while ( p < limit && isdigit( *p ) )
126
sum = sum * 10 + ( *p - '0' );
136
#define KERN_INDEX( g1, g2 ) ( ( (FT_ULong)g1 << 16 ) | g2 )
139
/* compare two kerning pairs */
140
FT_CALLBACK_DEF( int )
141
compare_kern_pairs( const void* a,
144
T1_Kern_Pair* pair1 = (T1_Kern_Pair*)a;
145
T1_Kern_Pair* pair2 = (T1_Kern_Pair*)b;
147
FT_ULong index1 = KERN_INDEX( pair1->glyph1, pair1->glyph2 );
148
FT_ULong index2 = KERN_INDEX( pair2->glyph1, pair2->glyph2 );
151
return (int)( index1 - index2 );
155
/* parse an AFM file -- for now, only read the kerning pairs */
156
FT_LOCAL_DEF( FT_Error )
157
T1_Read_AFM( FT_Face t1_face,
161
FT_Memory memory = stream->memory;
167
T1_Font type1 = &((T1_Face)t1_face)->type1;
171
if ( FT_FRAME_ENTER( stream->size ) )
174
start = (FT_Byte*)stream->cursor;
175
limit = (FT_Byte*)stream->limit;
178
/* we are now going to count the occurences of `KP' or `KPX' in */
181
for ( p = start; p < limit - 3; p++ )
183
if ( IS_KERN_PAIR( p ) )
187
/* Actually, kerning pairs are simply optional! */
191
/* allocate the pairs */
192
if ( FT_NEW( afm ) || FT_NEW_ARRAY( afm->kern_pairs, count ) )
195
/* now, read each kern pair */
196
pair = afm->kern_pairs;
197
afm->num_pairs = count;
199
/* save in face object */
200
((T1_Face)t1_face)->afm_data = afm;
202
t1_face->face_flags |= FT_FACE_FLAG_KERNING;
204
for ( p = start; p < limit - 3; p++ )
206
if ( IS_KERN_PAIR( p ) )
211
/* skip keyword (KP or KPX) */
216
pair->glyph1 = afm_atoindex( &q, limit, type1 );
217
pair->glyph2 = afm_atoindex( &q, limit, type1 );
218
pair->kerning.x = afm_atoi( &q, limit );
222
pair->kerning.y = afm_atoi( &q, limit );
228
/* now, sort the kern pairs according to their glyph indices */
229
ft_qsort( afm->kern_pairs, count, sizeof ( T1_Kern_Pair ),
230
compare_kern_pairs );
242
/* find the kerning for a given glyph pair */
244
T1_Get_Kerning( T1_AFM* afm,
249
T1_Kern_Pair *min, *mid, *max;
250
FT_ULong idx = KERN_INDEX( glyph1, glyph2 );
253
/* simple binary search */
254
min = afm->kern_pairs;
255
max = min + afm->num_pairs - 1;
262
mid = min + ( max - min ) / 2;
263
midi = KERN_INDEX( mid->glyph1, mid->glyph2 );
267
*kerning = mid->kerning;