misc fixes to get rrdtool working without included libraries.
[rrdtool.git] / libraries / freetype-2.0.5 / t1afm.c
1 /***************************************************************************/
2 /*                                                                         */
3 /*  t1afm.c                                                                */
4 /*                                                                         */
5 /*    AFM support for Type 1 fonts (body).                                 */
6 /*                                                                         */
7 /*  Copyright 1996-2001 by                                                 */
8 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
9 /*                                                                         */
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.                                        */
15 /*                                                                         */
16 /***************************************************************************/
17
18
19 #include <ft2build.h>
20 #include "t1afm.h"
21 #include FT_INTERNAL_STREAM_H
22 #include FT_INTERNAL_TYPE1_TYPES_H
23 #include <stdlib.h>  /* for qsort()   */
24 #include <string.h>  /* for strcmp()  */
25 #include <ctype.h>   /* for isalnum() */
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  trace_t1afm
36
37
38   FT_LOCAL_DEF void
39   T1_Done_AFM( FT_Memory  memory,
40                T1_AFM*    afm )
41   {
42     FREE( afm->kern_pairs );
43     afm->num_pairs = 0;
44     FREE( afm );
45   }
46
47
48 #undef  IS_KERN_PAIR
49 #define IS_KERN_PAIR( p )  ( p[0] == 'K' && p[1] == 'P' )
50
51 #define IS_ALPHANUM( c )  ( isalnum( c ) || \
52                             c == '_'     || \
53                             c == '.'     )
54
55
56   /* read a glyph name and return the equivalent glyph index */
57   static FT_UInt
58   afm_atoindex( FT_Byte**  start,
59                 FT_Byte*   limit,
60                 T1_Font*   type1 )
61   {
62     FT_Byte*  p = *start;
63     FT_Int    len;
64     FT_UInt   result = 0;
65     char      temp[64];
66
67
68     /* skip whitespace */
69     while ( ( *p == ' ' || *p == '\t' || *p == ':' || *p == ';' ) &&
70             p < limit                                             )
71       p++;
72     *start = p;
73
74     /* now, read glyph name */
75     while ( IS_ALPHANUM( *p ) && p < limit )
76       p++;
77
78     len = (FT_Int)( p - *start );
79
80     if ( len > 0 && len < 64 )
81     {
82       FT_Int  n;
83
84
85       /* copy glyph name to intermediate array */
86       MEM_Copy( temp, *start, len );
87       temp[len] = 0;
88
89       /* lookup glyph name in face array */
90       for ( n = 0; n < type1->num_glyphs; n++ )
91       {
92         char*  gname = (char*)type1->glyph_names[n];
93
94
95         if ( gname && gname[0] == temp[0] && strcmp( gname, temp ) == 0 )
96         {
97           result = n;
98           break;
99         }
100       }
101     }
102     *start = p;
103     return result;
104   }
105
106
107   /* read an integer */
108   static int
109   afm_atoi( FT_Byte**  start,
110             FT_Byte*   limit )
111   {
112     FT_Byte*  p    = *start;
113     int       sum  = 0;
114     int       sign = 1;
115
116
117     /* skip everything that is not a number */
118     while ( p < limit && !isdigit( *p ) )
119     {
120       sign = 1;
121       if ( *p == '-' )
122         sign = -1;
123
124       p++;
125     }
126
127     while ( p < limit && isdigit( *p ) )
128     {
129       sum = sum * 10 + ( *p - '0' );
130       p++;
131     }
132     *start = p;
133
134     return sum * sign;
135   }
136
137
138 #undef  KERN_INDEX
139 #define KERN_INDEX( g1, g2 )  ( ( (FT_ULong)g1 << 16 ) | g2 )
140
141
142   /* compare two kerning pairs */
143   FT_CALLBACK_DEF( int )
144   compare_kern_pairs( const void*  a,
145                       const void*  b )
146   {
147     T1_Kern_Pair*  pair1 = (T1_Kern_Pair*)a;
148     T1_Kern_Pair*  pair2 = (T1_Kern_Pair*)b;
149
150     FT_ULong  index1 = KERN_INDEX( pair1->glyph1, pair1->glyph2 );
151     FT_ULong  index2 = KERN_INDEX( pair2->glyph1, pair2->glyph2 );
152
153
154     return ( index1 - index2 );
155   }
156
157
158   /* parse an AFM file -- for now, only read the kerning pairs */
159   FT_LOCAL_DEF FT_Error
160   T1_Read_AFM( FT_Face    t1_face,
161                FT_Stream  stream )
162   {
163     FT_Error       error;
164     FT_Memory      memory = stream->memory;
165     FT_Byte*       start;
166     FT_Byte*       limit;
167     FT_Byte*       p;
168     FT_Int         count = 0;
169     T1_Kern_Pair*  pair;
170     T1_Font*       type1 = &((T1_Face)t1_face)->type1;
171     T1_AFM*        afm   = 0;
172
173
174     if ( ACCESS_Frame( stream->size ) )
175       return error;
176
177     start = (FT_Byte*)stream->cursor;
178     limit = (FT_Byte*)stream->limit;
179     p     = start;
180
181     /* we are now going to count the occurences of `KP' or `KPX' in */
182     /* the AFM file                                                 */
183     count = 0;
184     for ( p = start; p < limit - 3; p++ )
185     {
186       if ( IS_KERN_PAIR( p ) )
187         count++;
188     }
189
190     /* Actually, kerning pairs are simply optional! */
191     if ( count == 0 )
192       goto Exit;
193
194     /* allocate the pairs */
195     if ( ALLOC( afm, sizeof ( *afm ) )                       ||
196          ALLOC_ARRAY( afm->kern_pairs, count, T1_Kern_Pair ) )
197       goto Exit;
198
199     /* now, read each kern pair */
200     pair           = afm->kern_pairs;
201     afm->num_pairs = count;
202
203     /* save in face object */
204     ((T1_Face)t1_face)->afm_data = afm;
205
206     t1_face->face_flags |= FT_FACE_FLAG_KERNING;
207
208     for ( p = start; p < limit - 3; p++ )
209     {
210       if ( IS_KERN_PAIR( p ) )
211       {
212         FT_Byte*  q;
213
214
215         /* skip keyword (KP or KPX) */
216         q = p + 2;
217         if ( *q == 'X' )
218           q++;
219
220         pair->glyph1    = afm_atoindex( &q, limit, type1 );
221         pair->glyph2    = afm_atoindex( &q, limit, type1 );
222         pair->kerning.x = afm_atoi( &q, limit );
223
224         pair->kerning.y = 0;
225         if ( p[2] != 'X' )
226           pair->kerning.y = afm_atoi( &q, limit );
227
228         pair++;
229       }
230     }
231
232     /* now, sort the kern pairs according to their glyph indices */
233     qsort( afm->kern_pairs, count, sizeof ( T1_Kern_Pair ),
234            compare_kern_pairs );
235
236   Exit:
237     if ( error )
238       FREE( afm );
239
240     FORGET_Frame();
241
242     return error;
243   }
244
245
246   /* find the kerning for a given glyph pair */
247   FT_LOCAL_DEF void
248   T1_Get_Kerning( T1_AFM*     afm,
249                   FT_UInt     glyph1,
250                   FT_UInt     glyph2,
251                   FT_Vector*  kerning )
252   {
253     T1_Kern_Pair  *min, *mid, *max;
254     FT_ULong      index = KERN_INDEX( glyph1, glyph2 );
255
256
257     /* simple binary search */
258     min = afm->kern_pairs;
259     max = min + afm->num_pairs - 1;
260
261     while ( min <= max )
262     {
263       FT_ULong  midi;
264
265
266       mid  = min + ( max - min ) / 2;
267       midi = KERN_INDEX( mid->glyph1, mid->glyph2 );
268
269       if ( midi == index )
270       {
271         *kerning = mid->kerning;
272         return;
273       }
274
275       if ( midi < index )
276         min = mid + 1;
277       else
278         max = mid - 1;
279     }
280
281     kerning->x = 0;
282     kerning->y = 0;
283   }
284
285
286 /* END */