blob: ce00313b626f2ceb76d9988e5b48c3c730d2b762 [file] [log] [blame]
David Turner993a8d02002-05-18 12:03:43 +00001/*
2 * Copyright 2000 Computing Research Labs, New Mexico State University
Werner Lemberg7cf4d372002-05-21 14:13:01 +00003 * Copyright 2001, 2002 Francesco Zappa Nardelli
David Turner993a8d02002-05-18 12:03:43 +00004 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
20 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
Werner Lemberg7cf4d372002-05-21 14:13:01 +000024 /*************************************************************************/
25 /* */
26 /* This file is based on bdf.c,v 1.22 2000/03/16 20:08:50 */
27 /* */
28 /* taken from Mark Leisher's xmbdfed package */
29 /* */
30 /*************************************************************************/
31
David Turner993a8d02002-05-18 12:03:43 +000032
33#include <ft2build.h>
34
35#include FT_INTERNAL_DEBUG_H
36#include FT_INTERNAL_STREAM_H
37#include FT_INTERNAL_OBJECTS_H
38
39#include "bdf.h"
David Turner993a8d02002-05-18 12:03:43 +000040#include "bdferror.h"
41
David Turner993a8d02002-05-18 12:03:43 +000042
Werner Lemberg7cf4d372002-05-21 14:13:01 +000043 /*************************************************************************/
44 /* */
45 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
46 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
47 /* messages during execution. */
48 /* */
49#undef FT_COMPONENT
50#define FT_COMPONENT trace_bdflib
David Turner993a8d02002-05-18 12:03:43 +000051
David Turner993a8d02002-05-18 12:03:43 +000052
Werner Lemberg7cf4d372002-05-21 14:13:01 +000053 /*************************************************************************/
54 /* */
55 /* Default BDF font options. */
56 /* */
57 /*************************************************************************/
David Turner993a8d02002-05-18 12:03:43 +000058
David Turner993a8d02002-05-18 12:03:43 +000059
Werner Lemberg7cf4d372002-05-21 14:13:01 +000060 static bdf_options_t _bdf_opts =
61 {
David Turner993a8d02002-05-18 12:03:43 +000062 1, /* Correct metrics. */
63 1, /* Preserve unencoded glyphs. */
Werner Lemberg7cf4d372002-05-21 14:13:01 +000064 0, /* Preserve comments. */
65 BDF_PROPORTIONAL /* Default spacing. */
66 };
David Turner993a8d02002-05-18 12:03:43 +000067
David Turner993a8d02002-05-18 12:03:43 +000068
Werner Lemberg7cf4d372002-05-21 14:13:01 +000069 /*************************************************************************/
70 /* */
71 /* Builtin BDF font properties. */
72 /* */
73 /*************************************************************************/
David Turner993a8d02002-05-18 12:03:43 +000074
Werner Lemberg7cf4d372002-05-21 14:13:01 +000075 /* List of most properties that might appear in a font. Doesn't include */
76 /* the RAW_* and AXIS_* properties in X11R6 polymorphic fonts. */
David Turner993a8d02002-05-18 12:03:43 +000077
Werner Lemberg7cf4d372002-05-21 14:13:01 +000078 static bdf_property_t _bdf_properties[] =
David Turner993a8d02002-05-18 12:03:43 +000079 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +000080 { (char *)"ADD_STYLE_NAME", BDF_ATOM, 1, { 0 } },
81 { (char *)"AVERAGE_WIDTH", BDF_INTEGER, 1, { 0 } },
82 { (char *)"AVG_CAPITAL_WIDTH", BDF_INTEGER, 1, { 0 } },
83 { (char *)"AVG_LOWERCASE_WIDTH", BDF_INTEGER, 1, { 0 } },
84 { (char *)"CAP_HEIGHT", BDF_INTEGER, 1, { 0 } },
85 { (char *)"CHARSET_COLLECTIONS", BDF_ATOM, 1, { 0 } },
86 { (char *)"CHARSET_ENCODING", BDF_ATOM, 1, { 0 } },
87 { (char *)"CHARSET_REGISTRY", BDF_ATOM, 1, { 0 } },
88 { (char *)"COMMENT", BDF_ATOM, 1, { 0 } },
89 { (char *)"COPYRIGHT", BDF_ATOM, 1, { 0 } },
90 { (char *)"DEFAULT_CHAR", BDF_CARDINAL, 1, { 0 } },
91 { (char *)"DESTINATION", BDF_CARDINAL, 1, { 0 } },
92 { (char *)"DEVICE_FONT_NAME", BDF_ATOM, 1, { 0 } },
93 { (char *)"END_SPACE", BDF_INTEGER, 1, { 0 } },
94 { (char *)"FACE_NAME", BDF_ATOM, 1, { 0 } },
95 { (char *)"FAMILY_NAME", BDF_ATOM, 1, { 0 } },
96 { (char *)"FIGURE_WIDTH", BDF_INTEGER, 1, { 0 } },
97 { (char *)"FONT", BDF_ATOM, 1, { 0 } },
98 { (char *)"FONTNAME_REGISTRY", BDF_ATOM, 1, { 0 } },
99 { (char *)"FONT_ASCENT", BDF_INTEGER, 1, { 0 } },
100 { (char *)"FONT_DESCENT", BDF_INTEGER, 1, { 0 } },
101 { (char *)"FOUNDRY", BDF_ATOM, 1, { 0 } },
102 { (char *)"FULL_NAME", BDF_ATOM, 1, { 0 } },
103 { (char *)"ITALIC_ANGLE", BDF_INTEGER, 1, { 0 } },
104 { (char *)"MAX_SPACE", BDF_INTEGER, 1, { 0 } },
105 { (char *)"MIN_SPACE", BDF_INTEGER, 1, { 0 } },
106 { (char *)"NORM_SPACE", BDF_INTEGER, 1, { 0 } },
107 { (char *)"NOTICE", BDF_ATOM, 1, { 0 } },
108 { (char *)"PIXEL_SIZE", BDF_INTEGER, 1, { 0 } },
109 { (char *)"POINT_SIZE", BDF_INTEGER, 1, { 0 } },
110 { (char *)"QUAD_WIDTH", BDF_INTEGER, 1, { 0 } },
111 { (char *)"RAW_ASCENT", BDF_INTEGER, 1, { 0 } },
112 { (char *)"RAW_AVERAGE_WIDTH", BDF_INTEGER, 1, { 0 } },
113 { (char *)"RAW_AVG_CAPITAL_WIDTH", BDF_INTEGER, 1, { 0 } },
114 { (char *)"RAW_AVG_LOWERCASE_WIDTH", BDF_INTEGER, 1, { 0 } },
115 { (char *)"RAW_CAP_HEIGHT", BDF_INTEGER, 1, { 0 } },
116 { (char *)"RAW_DESCENT", BDF_INTEGER, 1, { 0 } },
117 { (char *)"RAW_END_SPACE", BDF_INTEGER, 1, { 0 } },
118 { (char *)"RAW_FIGURE_WIDTH", BDF_INTEGER, 1, { 0 } },
119 { (char *)"RAW_MAX_SPACE", BDF_INTEGER, 1, { 0 } },
120 { (char *)"RAW_MIN_SPACE", BDF_INTEGER, 1, { 0 } },
121 { (char *)"RAW_NORM_SPACE", BDF_INTEGER, 1, { 0 } },
122 { (char *)"RAW_PIXEL_SIZE", BDF_INTEGER, 1, { 0 } },
123 { (char *)"RAW_POINT_SIZE", BDF_INTEGER, 1, { 0 } },
124 { (char *)"RAW_PIXELSIZE", BDF_INTEGER, 1, { 0 } },
125 { (char *)"RAW_POINTSIZE", BDF_INTEGER, 1, { 0 } },
126 { (char *)"RAW_QUAD_WIDTH", BDF_INTEGER, 1, { 0 } },
127 { (char *)"RAW_SMALL_CAP_SIZE", BDF_INTEGER, 1, { 0 } },
128 { (char *)"RAW_STRIKEOUT_ASCENT", BDF_INTEGER, 1, { 0 } },
129 { (char *)"RAW_STRIKEOUT_DESCENT", BDF_INTEGER, 1, { 0 } },
130 { (char *)"RAW_SUBSCRIPT_SIZE", BDF_INTEGER, 1, { 0 } },
131 { (char *)"RAW_SUBSCRIPT_X", BDF_INTEGER, 1, { 0 } },
132 { (char *)"RAW_SUBSCRIPT_Y", BDF_INTEGER, 1, { 0 } },
133 { (char *)"RAW_SUPERSCRIPT_SIZE", BDF_INTEGER, 1, { 0 } },
134 { (char *)"RAW_SUPERSCRIPT_X", BDF_INTEGER, 1, { 0 } },
135 { (char *)"RAW_SUPERSCRIPT_Y", BDF_INTEGER, 1, { 0 } },
136 { (char *)"RAW_UNDERLINE_POSITION", BDF_INTEGER, 1, { 0 } },
137 { (char *)"RAW_UNDERLINE_THICKNESS", BDF_INTEGER, 1, { 0 } },
138 { (char *)"RAW_X_HEIGHT", BDF_INTEGER, 1, { 0 } },
139 { (char *)"RELATIVE_SETWIDTH", BDF_CARDINAL, 1, { 0 } },
140 { (char *)"RELATIVE_WEIGHT", BDF_CARDINAL, 1, { 0 } },
141 { (char *)"RESOLUTION", BDF_INTEGER, 1, { 0 } },
142 { (char *)"RESOLUTION_X", BDF_CARDINAL, 1, { 0 } },
143 { (char *)"RESOLUTION_Y", BDF_CARDINAL, 1, { 0 } },
144 { (char *)"SETWIDTH_NAME", BDF_ATOM, 1, { 0 } },
145 { (char *)"SLANT", BDF_ATOM, 1, { 0 } },
146 { (char *)"SMALL_CAP_SIZE", BDF_INTEGER, 1, { 0 } },
147 { (char *)"SPACING", BDF_ATOM, 1, { 0 } },
148 { (char *)"STRIKEOUT_ASCENT", BDF_INTEGER, 1, { 0 } },
149 { (char *)"STRIKEOUT_DESCENT", BDF_INTEGER, 1, { 0 } },
150 { (char *)"SUBSCRIPT_SIZE", BDF_INTEGER, 1, { 0 } },
151 { (char *)"SUBSCRIPT_X", BDF_INTEGER, 1, { 0 } },
152 { (char *)"SUBSCRIPT_Y", BDF_INTEGER, 1, { 0 } },
153 { (char *)"SUPERSCRIPT_SIZE", BDF_INTEGER, 1, { 0 } },
154 { (char *)"SUPERSCRIPT_X", BDF_INTEGER, 1, { 0 } },
155 { (char *)"SUPERSCRIPT_Y", BDF_INTEGER, 1, { 0 } },
156 { (char *)"UNDERLINE_POSITION", BDF_INTEGER, 1, { 0 } },
157 { (char *)"UNDERLINE_THICKNESS", BDF_INTEGER, 1, { 0 } },
158 { (char *)"WEIGHT", BDF_CARDINAL, 1, { 0 } },
159 { (char *)"WEIGHT_NAME", BDF_ATOM, 1, { 0 } },
160 { (char *)"X_HEIGHT", BDF_INTEGER, 1, { 0 } },
161 { (char *)"_MULE_BASELINE_OFFSET", BDF_INTEGER, 1, { 0 } },
162 { (char *)"_MULE_RELATIVE_COMPOSE", BDF_INTEGER, 1, { 0 } },
163 };
David Turner993a8d02002-05-18 12:03:43 +0000164
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000165 static unsigned long
166 _num_bdf_properties = sizeof ( _bdf_properties ) /
167 sizeof ( _bdf_properties[0] );
David Turner993a8d02002-05-18 12:03:43 +0000168
169
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000170 /*************************************************************************/
171 /* */
172 /* Hash table utilities for the properties. */
173 /* */
174 /*************************************************************************/
David Turner993a8d02002-05-18 12:03:43 +0000175
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000176 /* XXX: Replace this with FreeType's hash functions */
David Turner993a8d02002-05-18 12:03:43 +0000177
David Turner993a8d02002-05-18 12:03:43 +0000178
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000179#define INITIAL_HT_SIZE 241
180
181 typedef void
182 (*hash_free_func)( hashnode node );
183
184 static hashnode*
185 hash_bucket( char* key,
186 hashtable* ht )
David Turner993a8d02002-05-18 12:03:43 +0000187 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000188 char* kp = key;
189 unsigned long res = 0;
190 hashnode* bp = ht->table, *ndp;
191
192
193 /* Mocklisp hash function. */
194 while ( *kp )
195 res = ( res << 5 ) - res + *kp++;
196
197 ndp = bp + ( res % ht->size );
198 while ( *ndp )
David Turner993a8d02002-05-18 12:03:43 +0000199 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000200 kp = (*ndp)->key;
201 if ( kp[0] == key[0] && ft_strcmp( kp, key ) == 0 )
202 break;
203 ndp--;
204 if ( ndp < bp )
205 ndp = bp + ( ht->size - 1 );
David Turner993a8d02002-05-18 12:03:43 +0000206 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000207
208 return ndp;
David Turner993a8d02002-05-18 12:03:43 +0000209 }
David Turner993a8d02002-05-18 12:03:43 +0000210
211
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000212 static FT_Error
213 hash_rehash( hashtable* ht,
214 FT_Memory memory )
David Turner993a8d02002-05-18 12:03:43 +0000215 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000216 hashnode* obp = ht->table, *bp, *nbp;
David Turner993a8d02002-05-18 12:03:43 +0000217 int i, sz = ht->size;
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000218 FT_Error error = BDF_Err_Ok;
David Turner993a8d02002-05-18 12:03:43 +0000219
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000220
221 ht->size <<= 1;
222 ht->limit = ht->size / 3;
223
224 if ( FT_NEW_ARRAY( ht->table, ht->size ) )
225 goto Exit;
226 FT_MEM_SET( ht->table, 0, sizeof ( hashnode ) * ht->size );
227
228 for ( i = 0, bp = obp; i < sz; i++, bp++ )
David Turner993a8d02002-05-18 12:03:43 +0000229 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000230 if ( *bp )
231 {
232 nbp = hash_bucket( (*bp)->key, ht );
233 *nbp = *bp;
234 }
David Turner993a8d02002-05-18 12:03:43 +0000235 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000236 FT_FREE( obp );
237
238 Exit:
239 return error;
David Turner993a8d02002-05-18 12:03:43 +0000240 }
David Turner993a8d02002-05-18 12:03:43 +0000241
242
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000243 static FT_Error
244 hash_init( hashtable* ht,
245 FT_Memory memory )
David Turner993a8d02002-05-18 12:03:43 +0000246 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000247 int sz = INITIAL_HT_SIZE;
248 FT_Error error = BDF_Err_Ok;
David Turner993a8d02002-05-18 12:03:43 +0000249
David Turner993a8d02002-05-18 12:03:43 +0000250
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000251 ht->size = sz;
252 ht->limit = sz / 3;
253 ht->used = 0;
David Turner993a8d02002-05-18 12:03:43 +0000254
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000255 if ( FT_NEW_ARRAY( ht->table, sz ) )
256 goto Exit;
257 FT_MEM_SET( ht->table, 0, sizeof ( hashnode ) * sz );
258
259 Exit:
260 return error;
David Turner993a8d02002-05-18 12:03:43 +0000261 }
David Turner993a8d02002-05-18 12:03:43 +0000262
263
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000264 static void
265 hash_free( hashtable* ht,
266 FT_Memory memory )
David Turner993a8d02002-05-18 12:03:43 +0000267 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000268 if ( ht != 0 )
David Turner993a8d02002-05-18 12:03:43 +0000269 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000270 int i, sz = ht->size;
271 hashnode* bp = ht->table;
David Turner993a8d02002-05-18 12:03:43 +0000272
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000273
274 for ( i = 0; i < sz; i++, bp++ )
275 FT_FREE( *bp );
276
277 FT_FREE( ht->table );
278 }
David Turner993a8d02002-05-18 12:03:43 +0000279 }
280
David Turner993a8d02002-05-18 12:03:43 +0000281
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000282 static FT_Error
283 hash_insert( char* key,
284 void* data,
285 hashtable* ht,
286 FT_Memory memory )
David Turner993a8d02002-05-18 12:03:43 +0000287 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000288 hashnode nn, *bp = hash_bucket( key, ht );
289 FT_Error error = BDF_Err_Ok;
290
291
292 nn = *bp;
293 if ( !nn )
294 {
295 if ( FT_NEW( nn ) )
296 goto Exit;
297 *bp = nn;
298
299 nn->key = key;
300 nn->data = data;
301
302 if ( ht->used >= ht->limit )
303 {
304 error = hash_rehash( ht, memory );
305 if ( error )
306 goto Exit;
307 }
308 ht->used++;
309 }
David Turner993a8d02002-05-18 12:03:43 +0000310 else
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000311 nn->data = data;
312
313 Exit:
314 return error;
David Turner993a8d02002-05-18 12:03:43 +0000315 }
316
David Turner993a8d02002-05-18 12:03:43 +0000317
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000318 static hashnode
319 hash_lookup( char* key,
320 hashtable* ht )
321 {
322 hashnode *np = hash_bucket( key, ht );
323
324
325 return *np;
326 }
327
328
329 /*************************************************************************/
330 /* */
331 /* Utility types and functions. */
332 /* */
333 /*************************************************************************/
334
335
336 /* Function type for parsing lines of a BDF font. */
337
338 typedef FT_Error
339 (*_bdf_line_func_t)( char* line,
340 unsigned long linelen,
341 unsigned long lineno,
342 void* call_data,
343 void* client_data );
344
345
346 /* List structure for splitting lines into fields. */
347
348 typedef struct _bdf_list_t_
349 {
350 char** field;
351 unsigned long size;
352 unsigned long used;
353
354 } _bdf_list_t;
355
356
357 /* Structure used while loading BDF fonts. */
358
359 typedef struct _bdf_parse_t_
360 {
361 unsigned long flags;
362 unsigned long cnt;
363 unsigned long row;
364
365 short minlb;
366 short maxlb;
367 short maxrb;
368 short maxas;
369 short maxds;
370
371 short rbearing;
372
373 char* glyph_name;
374 long glyph_enc;
375
376 bdf_font_t* font;
377 bdf_options_t* opts;
378
379 unsigned long have[2048];
380 _bdf_list_t list;
381
382 FT_Memory memory;
383
384 } _bdf_parse_t;
385
386
387#define setsbit( m, cc ) ( m[(cc) >> 3] |= ( 1 << ( (cc) & 7 ) ) )
388#define sbitset( m, cc ) ( m[(cc) >> 3] & ( 1 << ( (cc) & 7 ) ) )
389
390
391 /* An empty string for empty fields. */
392
393 static char empty[1] = { 0 }; /* XXX eliminate this */
394
395
396 /* Assume the line is NULL-terminated and that the `list' parameter */
397 /* was initialized the first time it was used. */
398
399 static FT_Error
400 _bdf_split( char* separators,
401 char* line,
402 unsigned long linelen,
403 _bdf_list_t* list,
404 FT_Memory memory )
405 {
406 int mult, final_empty;
407 char *sp, *ep, *end;
408 char seps[32];
409 FT_Error error = BDF_Err_Ok;
410
411
412 /* Initialize the list. */
413 list->used = 0;
414
415 /* If the line is empty, then simply return. */
416 if ( linelen == 0 || line[0] == 0 )
417 goto Exit;
418
419 /* In the original code, if the `separators' parameter is NULL or */
420 /* empty, the list is split into individual bytes. We don't need */
421 /* this, so an error is signaled. */
422 if ( separators == 0 || *separators == 0 )
David Turner993a8d02002-05-18 12:03:43 +0000423 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000424 error = BDF_Err_Invalid_Argument;
425 goto Exit;
426 }
427
428 /* Prepare the separator bitmap. */
429 FT_MEM_SET( seps, 0, 32 );
430
431 /* If the very last character of the separator string is a plus, then */
432 /* set the `mult' flag to indicate that multiple separators should be */
433 /* collapsed into one. */
434 for ( mult = 0, sp = separators; sp && *sp; sp++ )
435 {
436 if ( *sp == '+' && *( sp + 1 ) == 0 )
437 mult = 1;
David Turner993a8d02002-05-18 12:03:43 +0000438 else
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000439 setsbit( seps, *sp );
440 }
441
442 /* Break the line up into fields. */
443 for ( final_empty = 0, sp = ep = line, end = sp + linelen;
444 sp < end && *sp; )
445 {
446 /* Collect everything that is not a separator. */
447 for ( ; *ep && !sbitset( seps, *ep ); ep++ )
448 ;
449
450 /* Resize the list if necessary. */
451 if ( list->used == list->size )
David Turner993a8d02002-05-18 12:03:43 +0000452 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000453 if ( list->size == 0 )
454 {
455 if ( FT_NEW_ARRAY( list->field, 5 ) )
456 goto Exit;
457 }
458 else
459 {
460 if ( FT_RENEW_ARRAY ( list->field ,
461 list->size,
462 list->size + 5 ) )
463 goto Exit;
464 }
465
466 list->size += 5;
David Turner993a8d02002-05-18 12:03:43 +0000467 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000468
469 /* Assign the field appropriately. */
470 list->field[list->used++] = ( ep > sp ) ? sp : empty;
471
472 sp = ep;
473
474 if ( mult )
475 {
476 /* If multiple separators should be collapsed, do it now by */
477 /* setting all the separator characters to 0. */
478 for ( ; *ep && sbitset( seps, *ep ); ep++ )
479 *ep = 0;
480 }
481 else if ( *ep != 0 )
482 /* Don't collapse multiple separators by making them 0, so just */
483 /* make the one encountered 0. */
484 *ep++ = 0;
485
486 final_empty = ( ep > sp && *ep == 0 );
487 sp = ep;
David Turner993a8d02002-05-18 12:03:43 +0000488 }
489
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000490 /* Finally, NULL-terminate the list. */
491 if ( list->used + final_empty + 1 >= list->size )
David Turner993a8d02002-05-18 12:03:43 +0000492 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000493 if ( list->used == list->size )
494 {
495 if ( list->size == 0 )
496 {
497 if ( FT_NEW_ARRAY( list->field, 5 ) )
498 goto Exit;
499 }
500 else
501 {
502 if ( FT_RENEW_ARRAY( list->field,
503 list->size,
504 list->size + 5 ) )
505 goto Exit;
506 }
David Turner993a8d02002-05-18 12:03:43 +0000507
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000508 list->size += 5;
509 }
David Turner993a8d02002-05-18 12:03:43 +0000510 }
511
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000512 if ( final_empty )
513 list->field[list->used++] = empty;
David Turner993a8d02002-05-18 12:03:43 +0000514
David Turner993a8d02002-05-18 12:03:43 +0000515 if ( list->used == list->size )
516 {
517 if ( list->size == 0 )
518 {
519 if ( FT_NEW_ARRAY( list->field, 5 ) )
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000520 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +0000521 }
522 else
523 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000524 if ( FT_RENEW_ARRAY( list->field,
525 list->size,
526 list->size + 5 ) )
527 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +0000528 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000529
David Turner993a8d02002-05-18 12:03:43 +0000530 list->size += 5;
531 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000532
533 list->field[list->used] = 0;
534
535 Exit:
536 return error;
David Turner993a8d02002-05-18 12:03:43 +0000537 }
538
David Turner993a8d02002-05-18 12:03:43 +0000539
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000540 static void
541 _bdf_shift( unsigned long n,
542 _bdf_list_t* list )
David Turner993a8d02002-05-18 12:03:43 +0000543 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000544 unsigned long i, u;
545
546
547 if ( list == 0 || list->used == 0 || n == 0 )
548 return;
549
550 if ( n >= list->used )
David Turner993a8d02002-05-18 12:03:43 +0000551 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000552 list->used = 0;
553 return;
David Turner993a8d02002-05-18 12:03:43 +0000554 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000555
556 for ( u = n, i = 0; u < list->used; i++, u++ )
557 list->field[i] = list->field[u];
558 list->used -= n;
559 }
560
561
562 static char *
563 _bdf_join( int c,
564 unsigned long* len,
565 _bdf_list_t* list )
566 {
567 unsigned long i, j;
568 char *fp, *dp;
569
570
571 if ( list == 0 || list->used == 0 )
572 return 0;
573
574 *len = 0;
575
576 dp = list->field[0];
577 for ( i = j = 0; i < list->used; i++ )
578 {
579 fp = list->field[i];
580 while ( *fp )
581 dp[j++] = *fp++;
582
583 if ( i + 1 < list->used )
584 dp[j++] = c;
585 }
586 dp[j] = 0;
587
588 *len = j;
589 return dp;
590 }
591
592
593 /* High speed file reader that passes each line to a callback. */
594 static FT_Error
595 bdf_internal_readstream( FT_Stream stream,
596 char* buffer,
597 int count,
598 int *read_bytes )
599 {
600 int rbytes;
601 unsigned long pos = stream->pos;
602 FT_Error error = BDF_Err_Ok;
603
604
605 if ( pos > stream->size )
606 {
607 FT_ERROR(( "bdf_internal_readstream:" ));
608 FT_ERROR(( " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
609 pos, stream->size ));
610 error = BDF_Err_Invalid_Stream_Operation;
611 goto Exit;
612 }
613
614 if ( stream->read )
615 rbytes = stream->read( stream, pos,
616 (unsigned char *)buffer, count );
David Turner993a8d02002-05-18 12:03:43 +0000617 else
618 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000619 rbytes = stream->size - pos;
620 if ( rbytes > count )
621 rbytes = count;
622
623 FT_MEM_COPY( buffer, stream->base + pos, rbytes );
David Turner993a8d02002-05-18 12:03:43 +0000624 }
David Turner993a8d02002-05-18 12:03:43 +0000625
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000626 stream->pos = pos + rbytes;
David Turner993a8d02002-05-18 12:03:43 +0000627
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000628 *read_bytes = rbytes;
David Turner993a8d02002-05-18 12:03:43 +0000629
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000630 Exit:
631 return error;
David Turner993a8d02002-05-18 12:03:43 +0000632 }
633
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000634
635 static FT_Error
636 _bdf_readstream( FT_Stream stream,
637 _bdf_line_func_t callback,
638 void* client_data,
639 unsigned long *lno )
David Turner993a8d02002-05-18 12:03:43 +0000640 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000641 _bdf_line_func_t cb;
642 unsigned long lineno;
643 int n, res, done, refill, bytes, hold;
644 char *ls, *le, *pp, *pe, *hp;
645 /* XXX: Use a dynamic buffer */
646 char buf[65536L];
647 FT_Error error = BDF_Err_Ok;
David Turner993a8d02002-05-18 12:03:43 +0000648
David Turner993a8d02002-05-18 12:03:43 +0000649
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000650 if ( callback == 0 )
David Turner993a8d02002-05-18 12:03:43 +0000651 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000652 error = BDF_Err_Invalid_Argument;
653 goto Exit;
654 }
David Turner993a8d02002-05-18 12:03:43 +0000655
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000656 cb = callback;
657 lineno = 1;
658 buf[0] = 0;
David Turner993a8d02002-05-18 12:03:43 +0000659
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000660 res = done = 0;
661 pp = ls = le = buf;
662
663 bytes = 65536L;
664
665 while ( !done )
666 {
667 error = bdf_internal_readstream( stream, pp, bytes, &n );
668 if ( error )
669 goto Exit;
670
671 if ( n == 0 )
672 break;
673
674 /* Determine the new end of the buffer pages. */
675 pe = pp + n;
676
677 for ( refill = 0; done == 0 && refill == 0; )
678 {
679 while ( le < pe && *le != '\n' && *le != '\r' )
680 le++;
681
682 if ( le == pe )
683 {
684 /* Hit the end of the last page in the buffer. Need to find */
685 /* out how many pages to shift and how many pages need to be */
686 /* read in. Adjust the line start and end pointers down to */
687 /* point to the right places in the pages. */
688
689 pp = buf + ( ( ( ls - buf ) >> 13 ) << 13 );
690 n = pp - buf;
691 ls -= n;
692 le -= n;
693 n = pe - pp;
694
695 FT_MEM_COPY( buf, pp, n );
696
697 pp = buf + n;
698 bytes = 65536L - n;
699 refill = 1;
700 }
701 else
702 {
703 /* Temporarily NULL-terminate the line. */
704 hp = le;
705 hold = *le;
706 *le = 0;
707
708 /* XXX: Use encoding independent value for 0x1a */
709 if ( *ls != '#' && *ls != 0x1a &&
710 le > ls &&
711 ( error = (*cb)( ls, le - ls, lineno, (void *)&cb,
712 client_data ) ) != BDF_Err_Ok )
713 done = 1;
714 else
715 {
David Turner993a8d02002-05-18 12:03:43 +0000716 ls = ++le;
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000717 /* Handle the case of DOS crlf sequences. */
718 if ( le < pe && hold == '\n' && *le =='\r' )
David Turner993a8d02002-05-18 12:03:43 +0000719 ls = ++le;
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000720 }
721
722 /* Increment the line number. */
723 lineno++;
724
725 /* Restore the character at the end of the line. */
726 *hp = hold;
David Turner993a8d02002-05-18 12:03:43 +0000727 }
David Turner993a8d02002-05-18 12:03:43 +0000728 }
729 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000730
731 *lno = lineno;
732
733 Exit:
734 return error;
David Turner993a8d02002-05-18 12:03:43 +0000735 }
David Turner993a8d02002-05-18 12:03:43 +0000736
737
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000738 /* XXX: make this work with EBCDIC also */
David Turner993a8d02002-05-18 12:03:43 +0000739
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000740 static unsigned char a2i[128] =
741 {
David Turner993a8d02002-05-18 12:03:43 +0000742 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
743 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
744 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
745 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
746 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00,
747 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
748 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
749 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
750 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
751 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
752 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000753 };
David Turner993a8d02002-05-18 12:03:43 +0000754
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000755 static unsigned char odigits[32] =
756 {
David Turner993a8d02002-05-18 12:03:43 +0000757 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00,
758 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
759 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
760 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000761 };
David Turner993a8d02002-05-18 12:03:43 +0000762
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000763 static unsigned char ddigits[32] =
764 {
David Turner993a8d02002-05-18 12:03:43 +0000765 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03,
766 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
767 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
768 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000769 };
David Turner993a8d02002-05-18 12:03:43 +0000770
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000771 static unsigned char hdigits[32] =
772 {
David Turner993a8d02002-05-18 12:03:43 +0000773 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03,
774 0x7e, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00,
775 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
776 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000777 };
David Turner993a8d02002-05-18 12:03:43 +0000778
David Turner993a8d02002-05-18 12:03:43 +0000779
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000780#define isdigok( m, d ) (m[(d) >> 3] & ( 1 << ( (d) & 7 ) ) )
David Turner993a8d02002-05-18 12:03:43 +0000781
David Turner993a8d02002-05-18 12:03:43 +0000782
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000783 /* Routine to convert an ASCII string into an unsigned long integer. */
784 static unsigned long
785 _bdf_atoul( char* s,
786 char** end,
787 int base )
David Turner993a8d02002-05-18 12:03:43 +0000788 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000789 unsigned long v;
790 unsigned char* dmap;
David Turner993a8d02002-05-18 12:03:43 +0000791
792
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000793 if ( s == 0 || *s == 0 )
David Turner993a8d02002-05-18 12:03:43 +0000794 return 0;
795
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000796 /* Make sure the radix is something recognizable. Default to 10. */
797 switch ( base )
798 {
799 case 8:
800 dmap = odigits;
801 break;
802 case 16:
803 dmap = hdigits;
804 break;
805 default:
806 base = 10;
807 dmap = ddigits;
808 break;
David Turner993a8d02002-05-18 12:03:43 +0000809 }
810
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000811 /* Check for the special hex prefix. */
812 if ( *s == '0' &&
813 ( *( s + 1 ) == 'x' || *( s + 1 ) == 'X' ) )
814 {
815 base = 16;
816 dmap = hdigits;
817 s += 2;
David Turner993a8d02002-05-18 12:03:43 +0000818 }
819
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000820 for ( v = 0; isdigok( dmap, *s ); s++ )
821 v = v * base + a2i[(int)*s];
David Turner993a8d02002-05-18 12:03:43 +0000822
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000823 if ( end != 0 )
David Turner993a8d02002-05-18 12:03:43 +0000824 *end = s;
825
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000826 return v;
827 }
David Turner993a8d02002-05-18 12:03:43 +0000828
David Turner993a8d02002-05-18 12:03:43 +0000829
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000830 /* Routine to convert an ASCII string into an signed long integer. */
831 static long
832 _bdf_atol( char* s,
833 char** end,
834 int base )
835 {
836 long v, neg;
837 unsigned char* dmap;
838
839
840 if ( s == 0 || *s == 0 )
841 return 0;
842
843 /* Make sure the radix is something recognizable. Default to 10. */
844 switch ( base )
845 {
846 case 8:
847 dmap = odigits;
848 break;
849 case 16:
850 dmap = hdigits;
851 break;
852 default:
853 base = 10;
854 dmap = ddigits;
855 break;
856 }
857
858 /* Check for a minus sign. */
859 neg = 0;
860 if ( *s == '-' )
861 {
862 s++;
863 neg = 1;
864 }
865
866 /* Check for the special hex prefix. */
867 if ( *s == '0' &&
868 ( *( s + 1 ) == 'x' || *( s + 1 ) == 'X' ) )
869 {
870 base = 16;
871 dmap = hdigits;
872 s += 2;
873 }
874
875 for ( v = 0; isdigok( dmap, *s ); s++ )
876 v = v * base + a2i[(int)*s];
877
878 if ( end != 0 )
879 *end = s;
880
881 return ( !neg ) ? v : -v;
882 }
883
884
885 /* Routine to convert an ASCII string into an signed short integer. */
886 static short
887 _bdf_atos( char* s,
888 char** end,
889 int base )
890 {
891 short v, neg;
892 unsigned char* dmap;
893
894
895 if ( s == 0 || *s == 0 )
896 return 0;
897
898 /* Make sure the radix is something recognizable. Default to 10. */
899 switch ( base )
900 {
901 case 8:
902 dmap = odigits;
903 break;
904 case 16:
905 dmap = hdigits;
906 break;
907 default:
908 base = 10;
909 dmap = ddigits;
910 break;
911 }
912
913 /* Check for a minus. */
914 neg = 0;
915 if ( *s == '-' )
916 {
917 s++;
918 neg = 1;
919 }
920
921 /* Check for the special hex prefix. */
922 if ( *s == '0' &&
923 ( *( s + 1 ) == 'x' || *( s + 1 ) == 'X' ) )
924 {
925 base = 16;
926 dmap = hdigits;
927 s += 2;
928 }
929
930 for ( v = 0; isdigok( dmap, *s ); s++ )
931 v = v * base + a2i[(int)*s];
932
933 if ( end != 0 )
934 *end = s;
935
936 return ( !neg ) ? v : -v;
937 }
938
939
940 /* Routine to compare two glyphs by encoding so they can be sorted. */
941 static int
942 by_encoding( const void* a,
943 const void* b )
944 {
945 bdf_glyph_t *c1, *c2;
946
947
948 c1 = (bdf_glyph_t *)a;
949 c2 = (bdf_glyph_t *)b;
950
951 if ( c1->encoding < c2->encoding )
David Turner993a8d02002-05-18 12:03:43 +0000952 return -1;
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000953 else if ( c1->encoding > c2->encoding )
David Turner993a8d02002-05-18 12:03:43 +0000954 return 1;
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000955
David Turner993a8d02002-05-18 12:03:43 +0000956 return 0;
David Turner993a8d02002-05-18 12:03:43 +0000957 }
958
Werner Lemberg7cf4d372002-05-21 14:13:01 +0000959
960 static FT_Error
961 bdf_create_property( char* name,
962 int format,
963 bdf_font_t* font )
964 {
965 unsigned long n;
966 bdf_property_t* p;
967 FT_Memory memory = font->memory;
968 FT_Error error = BDF_Err_Ok;
969
970
971 /* First check to see if the property has */
972 /* already been added or not. If it has, then */
973 /* simply ignore it. */
974 if ( hash_lookup( name, &(font->proptbl) ) )
975 goto Exit;
976
977 if ( font->nuser_props == 0 )
978 {
979 if ( FT_NEW_ARRAY( font->user_props, 1 ) )
980 goto Exit;
981 }
982 else
983 {
984 if ( FT_RENEW_ARRAY( font->user_props,
985 font->nuser_props,
986 font->nuser_props + 1 ) )
987 goto Exit;
988 }
989
990 p = font->user_props + font->nuser_props;
991 FT_MEM_SET( p, 0, sizeof ( bdf_property_t ) );
992
993 n = (unsigned long)( ft_strlen( name ) + 1 );
994 if ( FT_NEW_ARRAY( p->name, n ) )
995 goto Exit;
996
997 FT_MEM_COPY( (char *)p->name, name, n );
998
999 p->format = format;
1000 p->builtin = 0;
1001
1002 n = _num_bdf_properties + font->nuser_props;
1003
1004 error = hash_insert( p->name, (void *)n, &(font->proptbl), memory );
1005 if ( error )
1006 goto Exit;
1007
1008 font->nuser_props++;
1009
1010 Exit:
David Turner993a8d02002-05-18 12:03:43 +00001011 return error;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001012 }
David Turner993a8d02002-05-18 12:03:43 +00001013
1014
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001015 FT_LOCAL_DEF( bdf_property_t * )
1016 bdf_get_property( char* name,
1017 bdf_font_t* font )
David Turner993a8d02002-05-18 12:03:43 +00001018 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001019 hashnode hn;
1020 unsigned long propid;
1021
1022
1023 if ( name == 0 || *name == 0 )
1024 return 0;
1025
1026 if ( ( hn = hash_lookup( name, &(font->proptbl) ) ) == 0 )
1027 return 0;
1028
1029 propid = (unsigned long)hn->data;
1030 if ( propid >= _num_bdf_properties )
1031 return font->user_props + ( propid - _num_bdf_properties );
1032
1033 return _bdf_properties + propid;
David Turner993a8d02002-05-18 12:03:43 +00001034 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001035
1036
1037 /*************************************************************************/
1038 /* */
1039 /* BDF font file parsing flags and functions. */
1040 /* */
1041 /*************************************************************************/
1042
1043
1044 /* Parse flags. */
1045
1046#define _BDF_START 0x0001
1047#define _BDF_FONT_NAME 0x0002
1048#define _BDF_SIZE 0x0004
1049#define _BDF_FONT_BBX 0x0008
1050#define _BDF_PROPS 0x0010
1051#define _BDF_GLYPHS 0x0020
1052#define _BDF_GLYPH 0x0040
1053#define _BDF_ENCODING 0x0080
1054#define _BDF_SWIDTH 0x0100
1055#define _BDF_DWIDTH 0x0200
1056#define _BDF_BBX 0x0400
1057#define _BDF_BITMAP 0x0800
1058
1059#define _BDF_SWIDTH_ADJ 0x1000
1060
1061#define _BDF_GLYPH_BITS ( _BDF_GLYPH | \
1062 _BDF_ENCODING | \
1063 _BDF_SWIDTH | \
1064 _BDF_DWIDTH | \
1065 _BDF_BBX | \
1066 _BDF_BITMAP )
1067
1068#define _BDF_GLYPH_WIDTH_CHECK 0x40000000L
1069#define _BDF_GLYPH_HEIGHT_CHECK 0x80000000L
1070
1071
1072 /* Auto correction messages. */
1073#define ACMSG1 "FONT_ASCENT property missing. " \
1074 "Added \"FONT_ASCENT %hd\".\n"
1075#define ACMSG2 "FONT_DESCENT property missing. " \
1076 "Added \"FONT_DESCENT %hd\".\n"
1077#define ACMSG3 "Font width != actual width. Old: %hd New: %hd.\n"
1078#define ACMSG4 "Font left bearing != actual left bearing. " \
1079 "Old: %hd New: %hd.\n"
1080#define ACMSG5 "Font ascent != actual ascent. Old: %hd New: %hd.\n"
1081#define ACMSG6 "Font descent != actual descent. Old: %hd New: %hd.\n"
1082#define ACMSG7 "Font height != actual height. Old: %hd New: %hd.\n"
1083#define ACMSG8 "Glyph scalable width (SWIDTH) adjustments made.\n"
1084#define ACMSG9 "SWIDTH field missing at line %ld. Set automatically.\n"
1085#define ACMSG10 "DWIDTH field missing at line %ld. Set to glyph width.\n"
1086#define ACMSG11 "SIZE bits per pixel field adjusted to %hd.\n"
1087#define ACMSG12 "Duplicate encoding %ld (%s) changed to unencoded.\n"
1088#define ACMSG13 "Glyph %ld extra rows removed.\n"
1089#define ACMSG14 "Glyph %ld extra columns removed.\n"
1090#define ACMSG15 "Incorrect glyph count: %ld indicated but %ld found.\n"
1091
1092 /* Error messages. */
1093#define ERRMSG1 "[line %ld] Missing \"%s\" line.\n"
1094#define ERRMSG2 "[line %ld] Font header corrupted or missing fields.\n"
1095#define ERRMSG3 "[line %ld] Font glyphs corrupted or missing fields.\n"
1096
1097
1098 static FT_Error
1099 _bdf_add_comment( bdf_font_t* font,
1100 char* comment,
1101 unsigned long len )
David Turner993a8d02002-05-18 12:03:43 +00001102 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001103 char* cp;
1104 FT_Memory memory = font->memory;
1105 FT_Error error = BDF_Err_Ok;
1106
1107
1108 if ( font->comments_len == 0 )
1109 {
1110 if ( FT_NEW_ARRAY( font->comments, len + 1 ) )
1111 goto Exit;
1112 }
1113 else
1114 {
1115 if ( FT_RENEW_ARRAY( font->comments,
1116 font->comments_len,
1117 font->comments_len + len + 1 ) )
1118 goto Exit;
1119 }
1120
1121 cp = font->comments + font->comments_len;
1122 FT_MEM_COPY( cp, comment, len );
1123 cp += len;
1124 *cp++ = '\n';
1125 font->comments_len += len + 1;
1126
1127 Exit:
1128 return error;
David Turner993a8d02002-05-18 12:03:43 +00001129 }
1130
David Turner993a8d02002-05-18 12:03:43 +00001131
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001132 /* Set the spacing from the font name if it exists, or set it to the */
1133 /* default specified in the options. */
1134 static FT_Error
1135 _bdf_set_default_spacing( bdf_font_t* font,
1136 bdf_options_t* opts )
David Turner993a8d02002-05-18 12:03:43 +00001137 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001138 unsigned long len;
1139 char name[128];
1140 _bdf_list_t list;
1141 FT_Memory memory;
1142 FT_Error error = BDF_Err_Ok;
David Turner993a8d02002-05-18 12:03:43 +00001143
David Turner993a8d02002-05-18 12:03:43 +00001144
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001145 if ( font == 0 || font->name == 0 || font->name[0] == 0 )
1146 {
1147 error = BDF_Err_Invalid_Argument;
1148 goto Exit;
1149 }
David Turner993a8d02002-05-18 12:03:43 +00001150
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001151 memory = font->memory;
David Turner993a8d02002-05-18 12:03:43 +00001152
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001153 font->spacing = opts->font_spacing;
David Turner993a8d02002-05-18 12:03:43 +00001154
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001155 len = (unsigned long)( ft_strlen( font->name ) + 1 );
1156 FT_MEM_COPY( name, font->name, len );
David Turner993a8d02002-05-18 12:03:43 +00001157
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001158 list.size = list.used = 0;
David Turner993a8d02002-05-18 12:03:43 +00001159
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001160 error = _bdf_split( (char *)"-", name, len, &list, memory );
1161 if ( error )
1162 goto Exit;
1163
1164 if ( list.used == 15 )
1165 {
1166 switch ( list.field[11][0] )
1167 {
1168 case 'C':
1169 case 'c':
1170 font->spacing = BDF_CHARCELL;
1171 break;
1172 case 'M':
1173 case 'm':
1174 font->spacing = BDF_MONOWIDTH;
1175 break;
1176 case 'P':
1177 case 'p':
1178 font->spacing = BDF_PROPORTIONAL;
1179 break;
David Turner993a8d02002-05-18 12:03:43 +00001180 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001181 }
1182
1183 FT_FREE( list.field );
1184
1185 Exit:
1186 return error;
David Turner993a8d02002-05-18 12:03:43 +00001187 }
David Turner993a8d02002-05-18 12:03:43 +00001188
1189
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001190 /* Determine whether the property is an atom or not. If it is, then */
1191 /* clean it up so the double quotes are removed if they exist. */
1192 static int
1193 _bdf_is_atom( char* line,
1194 unsigned long linelen,
1195 char** name,
1196 char** value,
1197 bdf_font_t* font )
1198 {
1199 int hold;
1200 char *sp, *ep;
1201 bdf_property_t* p;
1202
David Turner993a8d02002-05-18 12:03:43 +00001203
1204 *name = sp = ep = line;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001205
1206 while ( *ep && *ep != ' ' && *ep != '\t' )
David Turner993a8d02002-05-18 12:03:43 +00001207 ep++;
1208
1209 hold = -1;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001210 if ( *ep )
David Turner993a8d02002-05-18 12:03:43 +00001211 {
1212 hold = *ep;
1213 *ep = 0;
1214 }
1215
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001216 p = bdf_get_property( sp, font );
David Turner993a8d02002-05-18 12:03:43 +00001217
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001218 /* Restore the character that was saved before any return can happen. */
1219 if ( hold != -1 )
David Turner993a8d02002-05-18 12:03:43 +00001220 *ep = hold;
1221
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001222 /* If the property exists and is not an atom, just return here. */
1223 if ( p && p->format != BDF_ATOM )
David Turner993a8d02002-05-18 12:03:43 +00001224 return 0;
1225
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001226 /* The property is an atom. Trim all leading and trailing whitespace */
1227 /* and double quotes for the atom value. */
David Turner993a8d02002-05-18 12:03:43 +00001228 sp = ep;
1229 ep = line + linelen;
1230
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001231 /* Trim the leading whitespace if it exists. */
David Turner993a8d02002-05-18 12:03:43 +00001232 *sp++ = 0;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001233 while ( *sp &&
1234 ( *sp == ' ' || *sp == '\t' ) )
David Turner993a8d02002-05-18 12:03:43 +00001235 sp++;
1236
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001237 /* Trim the leading double quote if it exists. */
1238 if ( *sp == '"' )
David Turner993a8d02002-05-18 12:03:43 +00001239 sp++;
1240 *value = sp;
1241
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001242 /* Trim the trailing whitespace if it exists. */
1243 while ( ep > sp &&
1244 ( *( ep - 1 ) == ' ' || *( ep - 1 ) == '\t' ) )
David Turner993a8d02002-05-18 12:03:43 +00001245 *--ep = 0;
1246
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001247 /* Trim the trailing double quote if it exists. */
1248 if ( ep > sp && *( ep - 1 ) == '"' )
David Turner993a8d02002-05-18 12:03:43 +00001249 *--ep = 0;
1250
1251 return 1;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001252 }
David Turner993a8d02002-05-18 12:03:43 +00001253
1254
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001255 static FT_Error
1256 _bdf_add_property( bdf_font_t* font,
1257 char* name,
1258 char* value )
1259 {
1260 unsigned long propid;
1261 hashnode hn;
1262 int len;
1263 bdf_property_t *prop, *fp;
1264 FT_Memory memory = font->memory;
1265 FT_Error error = BDF_Err_Ok;
David Turner993a8d02002-05-18 12:03:43 +00001266
David Turner993a8d02002-05-18 12:03:43 +00001267
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001268 /* First, check to see if the property already exists in the font. */
1269 if ( ( hn = hash_lookup( name, (hashtable *)font->internal ) ) != 0 )
David Turner993a8d02002-05-18 12:03:43 +00001270 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001271 /* The property already exists in the font, so simply replace */
1272 /* the value of the property with the current value. */
1273 fp = font->props + (unsigned long)hn->data;
1274
1275 switch ( prop->format )
David Turner993a8d02002-05-18 12:03:43 +00001276 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001277 case BDF_ATOM:
1278 /* Delete the current atom if it exists. */
1279 FT_FREE( fp->value.atom );
1280
1281 if ( value == 0 )
1282 len = 1;
1283 else
1284 len = ft_strlen( value ) + 1;
1285
1286 if ( len > 1 )
1287 {
1288 if ( FT_NEW_ARRAY( fp->value.atom, len ) )
1289 goto Exit;
1290 FT_MEM_COPY( fp->value.atom, value, len );
1291 }
1292 else
1293 fp->value.atom = 0;
1294 break;
1295
1296 case BDF_INTEGER:
1297 fp->value.int32 = _bdf_atol( value, 0, 10 );
1298 break;
1299
1300 case BDF_CARDINAL:
1301 fp->value.card32 = _bdf_atoul( value, 0, 10 );
1302 break;
1303 }
1304
1305 goto Exit;
1306 }
1307
1308 /* See whether this property type exists yet or not. */
1309 /* If not, create it. */
1310 hn = hash_lookup( name, &(font->proptbl) );
1311 if ( hn == 0 )
1312 {
1313 error = bdf_create_property( name, BDF_ATOM, font );
1314 if ( error )
1315 goto Exit;
1316 hn = hash_lookup( name, &(font->proptbl) );
1317 }
1318
1319 /* Allocate another property if this is overflow. */
1320 if ( font->props_used == font->props_size )
1321 {
1322 if ( font->props_size == 0 )
1323 {
1324 if ( FT_NEW_ARRAY( font->props, 1 ) )
1325 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +00001326 }
1327 else
1328 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001329 if ( FT_RENEW_ARRAY( font->props,
1330 font->props_size,
1331 font->props_size + 1 ) )
1332 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +00001333 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001334
David Turner993a8d02002-05-18 12:03:43 +00001335 fp = font->props + font->props_size;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001336 FT_MEM_SET( fp, 0, sizeof ( bdf_property_t ) );
David Turner993a8d02002-05-18 12:03:43 +00001337 font->props_size++;
1338 }
1339
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001340 propid = (unsigned long)hn->data;
1341 if ( propid >= _num_bdf_properties )
1342 prop = font->user_props + ( propid - _num_bdf_properties );
David Turner993a8d02002-05-18 12:03:43 +00001343 else
1344 prop = _bdf_properties + propid;
1345
1346 fp = font->props + font->props_used;
1347
1348 fp->name = prop->name;
1349 fp->format = prop->format;
1350 fp->builtin = prop->builtin;
1351
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001352 switch ( prop->format )
David Turner993a8d02002-05-18 12:03:43 +00001353 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001354 case BDF_ATOM:
1355 if ( value == 0 )
1356 len = 1;
1357 else
1358 len = ft_strlen( value ) + 1;
David Turner993a8d02002-05-18 12:03:43 +00001359
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001360 if ( len > 1 )
1361 {
1362 if ( FT_NEW_ARRAY( fp->value.atom, len ) )
1363 goto Exit;
1364 FT_MEM_COPY( fp->value.atom, value, len );
1365 }
1366 else
1367 fp->value.atom = 0;
1368 break;
David Turner993a8d02002-05-18 12:03:43 +00001369
1370 case BDF_INTEGER:
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001371 fp->value.int32 = _bdf_atol( value, 0, 10 );
David Turner993a8d02002-05-18 12:03:43 +00001372 break;
1373
1374 case BDF_CARDINAL:
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001375 fp->value.card32 = _bdf_atoul( value, 0, 10 );
David Turner993a8d02002-05-18 12:03:43 +00001376 break;
David Turner993a8d02002-05-18 12:03:43 +00001377 }
1378
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001379 /* If the property happens to be a comment, then it doesn't need */
1380 /* to be added to the internal hash table. */
1381 if ( ft_memcmp( name, "COMMENT", 7 ) != 0 ) {
1382 /* Add the property to the font property table. */
1383 error = hash_insert( fp->name,
1384 (void *)font->props_used,
1385 (hashtable *)font->internal,
1386 memory );
1387 if ( error )
1388 goto Exit;
1389 }
David Turner993a8d02002-05-18 12:03:43 +00001390
1391 font->props_used++;
1392
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001393 /* Some special cases need to be handled here. The DEFAULT_CHAR */
1394 /* property needs to be located if it exists in the property list, the */
1395 /* FONT_ASCENT and FONT_DESCENT need to be assigned if they are */
1396 /* present, and the SPACING property should override the default */
1397 /* spacing. */
1398 if ( ft_memcmp( name, "DEFAULT_CHAR", 12 ) == 0 )
David Turner993a8d02002-05-18 12:03:43 +00001399 font->default_glyph = fp->value.int32;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001400 else if ( ft_memcmp( name, "FONT_ASCENT", 11 ) == 0 )
David Turner993a8d02002-05-18 12:03:43 +00001401 font->font_ascent = fp->value.int32;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001402 else if ( ft_memcmp( name, "FONT_DESCENT", 12 ) == 0 )
David Turner993a8d02002-05-18 12:03:43 +00001403 font->font_descent = fp->value.int32;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001404 else if ( ft_memcmp( name, "SPACING", 7 ) == 0 )
David Turner993a8d02002-05-18 12:03:43 +00001405 {
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001406 if ( fp->value.atom[0] == 'p' || fp->value.atom[0] == 'P' )
David Turner993a8d02002-05-18 12:03:43 +00001407 font->spacing = BDF_PROPORTIONAL;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001408 else if ( fp->value.atom[0] == 'm' || fp->value.atom[0] == 'M' )
David Turner993a8d02002-05-18 12:03:43 +00001409 font->spacing = BDF_MONOWIDTH;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001410 else if ( fp->value.atom[0] == 'c' || fp->value.atom[0] == 'C' )
David Turner993a8d02002-05-18 12:03:43 +00001411 font->spacing = BDF_CHARCELL;
1412 }
1413
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001414 Exit:
1415 return error;
David Turner993a8d02002-05-18 12:03:43 +00001416 }
1417
David Turner993a8d02002-05-18 12:03:43 +00001418
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001419 static unsigned char nibble_mask[8] =
1420 {
1421 0xFF, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE
1422 };
1423
1424
1425 /* Actually parse the glyph info and bitmaps. */
1426 static FT_Error
1427 _bdf_parse_glyphs( char* line,
1428 unsigned long linelen,
1429 unsigned long lineno,
1430 void* call_data,
1431 void* client_data )
1432 {
1433 int c, mask_index;
1434 char* s;
1435 unsigned char* bp;
1436 unsigned long i, slen, nibbles;
1437 double ps, rx, dw, sw;
1438
1439 _bdf_line_func_t* next;
1440 _bdf_parse_t* p;
1441 bdf_glyph_t* glyph;
1442 bdf_font_t* font;
1443
1444 FT_Memory memory;
1445 FT_Error error = BDF_Err_Ok;
1446
1447 FT_UNUSED( lineno ); /* only used in debug mode */
1448
1449
1450 next = (_bdf_line_func_t *)call_data;
1451 p = (_bdf_parse_t *) client_data;
1452
1453 font = p->font;
1454 memory = font->memory;
1455
1456 /* Check for a comment. */
1457 if ( ft_memcmp( line, "COMMENT", 7 ) == 0 )
1458 {
1459 linelen -= 7;
1460
1461 s = line + 7;
1462 if ( *s != 0 )
1463 {
1464 s++;
1465 linelen--;
1466 }
1467 error = _bdf_add_comment( p->font, s, linelen );
1468 goto Exit;
1469 }
1470
1471 /* The very first thing expected is the number of glyphs. */
1472 if ( !( p->flags & _BDF_GLYPHS ) )
1473 {
1474 if ( ft_memcmp( line, "CHARS", 5 ) != 0 )
1475 {
1476 FT_ERROR(( "_bdf_parse_glyphs: " ERRMSG1, lineno, "CHARS" ));
1477 error = BDF_Err_Missing_Chars_Field;
1478 goto Exit;
1479 }
1480
1481 error = _bdf_split( (char *)" +", line, linelen, &p->list, memory );
1482 if ( error )
1483 goto Exit;
1484 p->cnt = font->glyphs_size = _bdf_atoul( p->list.field[1], 0, 10 );
1485
1486 /* Make sure the number of glyphs is non-zero. */
1487 if ( p->cnt == 0 )
David Turner993a8d02002-05-18 12:03:43 +00001488 font->glyphs_size = 64;
1489
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001490 if ( FT_NEW_ARRAY( font->glyphs, font->glyphs_size ) )
1491 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +00001492
David Turner993a8d02002-05-18 12:03:43 +00001493 p->flags |= _BDF_GLYPHS;
David Turner993a8d02002-05-18 12:03:43 +00001494
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001495 goto Exit;
1496 }
1497
1498 /* Check for the ENDFONT field. */
1499 if ( ft_memcmp( line, "ENDFONT", 7 ) == 0 )
1500 {
1501 /* Sort the glyphs by encoding. */
1502 ft_qsort( (char *)font->glyphs,
1503 font->glyphs_used,
1504 sizeof ( bdf_glyph_t ),
1505 by_encoding );
David Turner993a8d02002-05-18 12:03:43 +00001506
1507 p->flags &= ~_BDF_START;
David Turner993a8d02002-05-18 12:03:43 +00001508
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001509 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +00001510 }
1511
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001512 /* Check for the ENDCHAR field. */
1513 if ( ft_memcmp( line, "ENDCHAR", 7 ) == 0 )
1514 {
1515 p->glyph_enc = 0;
1516 p->flags &= ~_BDF_GLYPH_BITS;
1517
1518 goto Exit;
1519 }
1520
1521 /* Check to see whether a glyph is being scanned but should be */
1522 /* ignored because it is an unencoded glyph. */
1523 if ( ( p->flags & _BDF_GLYPH ) &&
1524 p->glyph_enc == -1 &&
1525 p->opts->keep_unencoded == 0 )
1526 goto Exit;
1527
1528 /* Check for the STARTCHAR field. */
1529 if ( ft_memcmp( line, "STARTCHAR", 9 ) == 0 )
1530 {
1531 /* Set the character name in the parse info first until the */
1532 /* encoding can be checked for an unencoded character. */
1533 FT_FREE( p->glyph_name );
1534
1535 error = _bdf_split( (char *)" +", line, linelen, &p->list,memory );
1536 if ( error )
1537 goto Exit;
1538 _bdf_shift( 1, &p->list );
1539
1540 s = _bdf_join( ' ', &slen, &p->list );
1541
1542 if ( FT_NEW_ARRAY( p->glyph_name, slen + 1 ) )
1543 goto Exit;
1544 FT_MEM_COPY( p->glyph_name, s, slen + 1 );
1545
1546 p->flags |= _BDF_GLYPH;
1547
1548 goto Exit;
1549 }
1550
1551 /* Check for the ENCODING field. */
1552 if ( ft_memcmp( line, "ENCODING", 8 ) == 0 )
1553 {
1554 if ( !( p->flags & _BDF_GLYPH ) )
1555 {
1556 /* Missing STARTCHAR field. */
1557 FT_ERROR(( "_bdf_parse_glyphs: " ERRMSG1, lineno, "STARTCHAR" ));
1558 error = BDF_Err_Missing_Startchar_Field;
1559 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +00001560 }
1561
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001562 error = _bdf_split( (char *)" +", line, linelen, &p->list, memory );
1563 if ( error )
1564 goto Exit;
1565 p->glyph_enc = _bdf_atol( p->list.field[1], 0, 10 );
David Turner993a8d02002-05-18 12:03:43 +00001566
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001567 /* Check to see whether this encoding has already been encountered. */
1568 /* If it has then change it to unencoded so it gets added if */
1569 /* indicated. */
1570 if ( p->glyph_enc >= 0 )
1571 {
1572 if ( _bdf_glyph_modified( p->have, p->glyph_enc ) )
1573 {
1574 /* Emit a message saying a glyph has been moved to the */
1575 /* unencoded area. */
1576 FT_TRACE2(( "_bdf_parse_glyphs: " ACMSG12,
1577 p->glyph_enc, p->glyph_name ));
1578 p->glyph_enc = -1;
1579 font->modified = 1;
1580 }
1581 else
1582 _bdf_set_glyph_modified( p->have, p->glyph_enc );
1583 }
1584
1585 if ( p->glyph_enc >= 0 )
1586 {
1587 /* Make sure there are enough glyphs allocated in case the */
1588 /* number of characters happen to be wrong. */
1589 if ( font->glyphs_used == font->glyphs_size )
1590 {
1591 if ( FT_RENEW_ARRAY( font->glyphs,
1592 font->glyphs_size,
1593 font->glyphs_size + 64 ) )
1594 goto Exit;
1595 FT_MEM_SET( font->glyphs + font->glyphs_size,
1596 0,
1597 sizeof ( bdf_glyph_t ) * 64 ); /* FZ inutile */
1598 font->glyphs_size += 64;
David Turner993a8d02002-05-18 12:03:43 +00001599 }
1600
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001601 glyph = font->glyphs + font->glyphs_used++;
1602 glyph->name = p->glyph_name;
1603 glyph->encoding = p->glyph_enc;
David Turner993a8d02002-05-18 12:03:43 +00001604
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001605 /* Reset the initial glyph info. */
1606 p->glyph_name = 0;
1607 }
1608 else
1609 {
1610 /* Unencoded glyph. Check to see whether it should */
1611 /* be added or not. */
1612 if ( p->opts->keep_unencoded != 0 )
1613 {
1614 /* Allocate the next unencoded glyph. */
1615 if ( font->unencoded_used == font->unencoded_size )
1616 {
1617 if ( font->unencoded_size == 0 )
1618 {
1619 if ( FT_NEW_ARRAY( font->unencoded, 2 ) )
1620 goto Exit;
1621 }
1622 else
1623 {
1624 if ( FT_RENEW_ARRAY( font->unencoded ,
1625 font->unencoded_size,
1626 font->unencoded_size + 4 ) )
1627 goto Exit;
1628 }
1629 font->unencoded_size += 4;
1630 }
1631
1632 glyph = font->unencoded + font->unencoded_used;
1633 glyph->name = p->glyph_name;
1634 glyph->encoding = font->unencoded_used++;
1635 }
1636 else
1637 /* Free up the glyph name if the unencoded shouldn't be */
1638 /* kept. */
1639 FT_FREE( p->glyph_name );
1640
1641 p->glyph_name = 0;
1642 }
1643
1644 /* Clear the flags that might be added when width and height are */
1645 /* checked for consistency. */
1646 p->flags &= ~( _BDF_GLYPH_WIDTH_CHECK | _BDF_GLYPH_HEIGHT_CHECK );
1647
1648 p->flags |= _BDF_ENCODING;
1649
1650 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +00001651 }
1652
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001653 /* Point at the glyph being constructed. */
1654 if ( p->glyph_enc == -1 )
1655 glyph = font->unencoded + ( font->unencoded_used - 1 );
1656 else
1657 glyph = font->glyphs + ( font->glyphs_used - 1 );
David Turner993a8d02002-05-18 12:03:43 +00001658
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001659 /* Check to see whether a bitmap is being constructed. */
1660 if ( p->flags & _BDF_BITMAP )
1661 {
1662 /* If there are more rows than are specified in the glyph metrics, */
1663 /* ignore the remaining lines. */
1664 if ( p->row >= glyph->bbx.height )
1665 {
1666 if ( !( p->flags & _BDF_GLYPH_HEIGHT_CHECK ) )
1667 {
1668 FT_TRACE2(( "_bdf_parse_glyphs: " ACMSG13, glyph->encoding ));
1669 p->flags |= _BDF_GLYPH_HEIGHT_CHECK;
David Turner993a8d02002-05-18 12:03:43 +00001670 font->modified = 1;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001671 }
1672
1673 goto Exit;
1674 }
1675
1676 /* Only collect the number of nibbles indicated by the glyph */
1677 /* metrics. If there are more columns, they are simply ignored. */
1678 nibbles = glyph->bpr << 1;
1679 bp = glyph->bitmap + p->row * glyph->bpr;
1680
1681 for ( i = 0, *bp = 0; i < nibbles; i++ )
1682 {
1683 c = line[i];
1684 *bp = ( *bp << 4 ) + a2i[c];
1685 if ( i + 1 < nibbles && ( i & 1 ) )
1686 *++bp = 0;
1687 }
1688
1689 /* Remove possible garbage at the right. */
1690 mask_index = ( glyph->bbx.width * p->font->bpp ) & 7;
1691 *bp &= nibble_mask[mask_index];
1692
1693 /* If any line has extra columns, indicate they have been removed. */
1694 if ( ( line[nibbles] == '0' || a2i[(int)line[nibbles]] != 0 ) &&
1695 !( p->flags & _BDF_GLYPH_WIDTH_CHECK ) )
1696 {
1697 FT_TRACE2(( "_bdf_parse_glyphs: " ACMSG14, glyph->encoding ));
1698 p->flags |= _BDF_GLYPH_WIDTH_CHECK;
1699 font->modified = 1;
David Turner993a8d02002-05-18 12:03:43 +00001700 }
1701
1702 p->row++;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001703 goto Exit;
1704 }
David Turner993a8d02002-05-18 12:03:43 +00001705
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001706 /* Expect the SWIDTH (scalable width) field next. */
1707 if ( ft_memcmp( line, "SWIDTH", 6 ) == 0 )
1708 {
1709 if ( !( p->flags & _BDF_ENCODING ) )
1710 {
1711 /* Missing ENCODING field. */
1712 FT_ERROR(( "_bdf_parse_glyphs: " ERRMSG1, lineno, "ENCODING" ));
1713 error = BDF_Err_Missing_Encoding_Field;
1714 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +00001715 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001716
1717 error = _bdf_split( (char *)" +", line, linelen, &p->list, memory );
1718 if ( error )
1719 goto Exit;
1720 glyph->swidth = _bdf_atoul( p->list.field[1], 0, 10 );
David Turner993a8d02002-05-18 12:03:43 +00001721 p->flags |= _BDF_SWIDTH;
David Turner993a8d02002-05-18 12:03:43 +00001722
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001723 goto Exit;
1724 }
David Turner993a8d02002-05-18 12:03:43 +00001725
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001726 /* Expect the DWIDTH (scalable width) field next. */
1727 if ( ft_memcmp( line, "DWIDTH", 6 ) == 0 )
1728 {
1729 error = _bdf_split( (char *)" +", line, linelen, &p->list,memory );
1730 if ( error )
1731 goto Exit;
1732 glyph->dwidth = _bdf_atoul( p->list.field[1], 0, 10 );
1733
1734 if ( !( p->flags & _BDF_SWIDTH ) )
1735 {
1736 /* Missing SWIDTH field. Emit an auto correction message and set */
1737 /* the scalable width from the device width. */
1738 FT_TRACE2(( "_bdf_parse_glyphs: " ACMSG9, lineno ));
1739
1740 ps = (double)font->point_size;
1741 rx = (double)font->resolution_x;
1742 dw = (double)glyph->dwidth;
1743
1744 glyph->swidth = (unsigned short)( ( dw * 72000.0 ) / ( ps * rx ) );
David Turner993a8d02002-05-18 12:03:43 +00001745 }
1746
1747 p->flags |= _BDF_DWIDTH;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001748 goto Exit;
1749 }
David Turner993a8d02002-05-18 12:03:43 +00001750
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001751 /* Expect the BBX field next. */
1752 if ( ft_memcmp( line, "BBX", 3 ) == 0 )
1753 {
1754 error = _bdf_split( (char *)" +", line, linelen, &p->list, memory );
1755 if ( error )
1756 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +00001757
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001758 glyph->bbx.width = _bdf_atos( p->list.field[1], 0, 10 );
1759 glyph->bbx.height = _bdf_atos( p->list.field[2], 0, 10 );
1760 glyph->bbx.x_offset = _bdf_atos( p->list.field[3], 0, 10 );
1761 glyph->bbx.y_offset = _bdf_atos( p->list.field[4], 0, 10 );
1762
1763 /* Generate the ascent and descent of the character. */
1764 glyph->bbx.ascent = glyph->bbx.height + glyph->bbx.y_offset;
David Turner993a8d02002-05-18 12:03:43 +00001765 glyph->bbx.descent = -glyph->bbx.y_offset;
1766
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001767 /* Determine the overall font bounding box as the characters are */
1768 /* loaded so corrections can be done later if indicated. */
1769 p->maxas = MAX( glyph->bbx.ascent, p->maxas );
1770 p->maxds = MAX( glyph->bbx.descent, p->maxds );
1771
David Turner993a8d02002-05-18 12:03:43 +00001772 p->rbearing = glyph->bbx.width + glyph->bbx.x_offset;
David Turner993a8d02002-05-18 12:03:43 +00001773
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001774 p->maxrb = MAX( p->rbearing, p->maxrb );
1775 p->minlb = MIN( glyph->bbx.x_offset, p->minlb );
1776 p->maxlb = MAX( glyph->bbx.x_offset, p->maxlb );
1777
1778 if ( !( p->flags & _BDF_DWIDTH ) )
1779 {
1780 /* Missing DWIDTH field. Emit an auto correction message and set */
1781 /* the device width to the glyph width. */
1782 FT_TRACE2(( "_bdf_parse_glyphs: " ACMSG10, lineno ));
1783 glyph->dwidth = glyph->bbx.width;
David Turner993a8d02002-05-18 12:03:43 +00001784 }
1785
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001786 /* If the BDF_CORRECT_METRICS flag is set, then adjust the SWIDTH */
1787 /* value if necessary. */
1788 if ( p->opts->correct_metrics != 0 )
1789 {
1790 /* Determine the point size of the glyph. */
1791 ps = (double)font->point_size;
1792 rx = (double)font->resolution_x;
1793 dw = (double)glyph->dwidth;
David Turner993a8d02002-05-18 12:03:43 +00001794
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001795 sw = (unsigned short)( ( dw * 72000.0 ) / ( ps * rx ) );
1796
1797 if ( sw != glyph->swidth )
1798 {
1799 glyph->swidth = sw;
1800
1801 if ( p->glyph_enc == -1 )
1802 _bdf_set_glyph_modified( font->umod,
1803 font->unencoded_used - 1 );
1804 else
1805 _bdf_set_glyph_modified( font->nmod, glyph->encoding );
1806
1807 p->flags |= _BDF_SWIDTH_ADJ;
1808 font->modified = 1;
1809 }
David Turner993a8d02002-05-18 12:03:43 +00001810 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001811
David Turner993a8d02002-05-18 12:03:43 +00001812 p->flags |= _BDF_BBX;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001813 goto Exit;
1814 }
David Turner993a8d02002-05-18 12:03:43 +00001815
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001816 /* And finally, gather up the bitmap. */
1817 if ( ft_memcmp( line, "BITMAP", 6 ) == 0 )
1818 {
1819 if ( !( p->flags & _BDF_BBX ) )
1820 {
1821 /* Missing BBX field. */
1822 FT_ERROR(( "_bdf_parse_glyphs: " ERRMSG1, lineno, "BBX" ));
1823 error = BDF_Err_Missing_Bbx_Field;
1824 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +00001825 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001826
1827 /* Allocate enough space for the bitmap. */
1828 glyph->bpr = ( glyph->bbx.width * p->font->bpp + 7 ) >> 3;
1829 glyph->bytes = glyph->bpr * glyph->bbx.height;
1830
1831 if ( FT_NEW_ARRAY( glyph->bitmap, glyph->bytes ) )
1832 goto Exit;
1833
1834 p->row = 0;
David Turner993a8d02002-05-18 12:03:43 +00001835 p->flags |= _BDF_BITMAP;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001836
1837 goto Exit;
1838 }
1839
1840 error = BDF_Err_Invalid_File_Format;
1841
1842 Exit:
1843 return error;
David Turner993a8d02002-05-18 12:03:43 +00001844 }
1845
David Turner993a8d02002-05-18 12:03:43 +00001846
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001847 /* Load the font properties. */
1848 static FT_Error
1849 _bdf_parse_properties( char* line,
1850 unsigned long linelen,
1851 unsigned long lineno,
1852 void* call_data,
1853 void* client_data )
1854 {
1855 unsigned long vlen;
1856 _bdf_line_func_t* next;
1857 _bdf_parse_t* p;
1858 char* name;
1859 char* value;
1860 char nbuf[128];
1861 FT_Memory memory;
1862 FT_Error error = BDF_Err_Ok;
David Turner993a8d02002-05-18 12:03:43 +00001863
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001864 FT_UNUSED( lineno );
David Turner993a8d02002-05-18 12:03:43 +00001865
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001866
1867 next = (_bdf_line_func_t *)call_data;
1868 p = (_bdf_parse_t *) client_data;
1869
1870 memory = p->font->memory;
1871
1872 /* Check for the end of the properties. */
1873 if ( ft_memcmp( line, "ENDPROPERTIES", 13 ) == 0 )
1874 {
1875 /* If the FONT_ASCENT or FONT_DESCENT properties have not been */
1876 /* encountered yet, then make sure they are added as properties and */
1877 /* make sure they are set from the font bounding box info. */
1878 /* */
1879 /* This is *always* done regardless of the options, because X11 */
1880 /* requires these two fields to compile fonts. */
1881 if ( bdf_get_font_property( p->font, (char *)"FONT_ASCENT" ) == 0 )
1882 {
1883 p->font->font_ascent = p->font->bbx.ascent;
1884 ft_sprintf( nbuf, "%hd", p->font->bbx.ascent );
1885 error = _bdf_add_property( p->font, (char *)"FONT_ASCENT", nbuf );
1886 if ( error )
1887 goto Exit;
1888
1889 FT_TRACE2(( "_bdf_parse_properties: " ACMSG1, p->font->bbx.ascent ));
1890 p->font->modified = 1;
David Turner993a8d02002-05-18 12:03:43 +00001891 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001892
1893 if ( bdf_get_font_property( p->font, (char *)"FONT_DESCENT" ) == 0 )
1894 {
1895 p->font->font_descent = p->font->bbx.descent;
1896 ft_sprintf( nbuf, "%hd", p->font->bbx.descent );
1897 error = _bdf_add_property( p->font, (char *)"FONT_DESCENT", nbuf );
1898 if ( error )
1899 goto Exit;
1900
1901 FT_TRACE2(( "_bdf_parse_properties: " ACMSG2, p->font->bbx.descent ));
1902 p->font->modified = 1;
David Turner993a8d02002-05-18 12:03:43 +00001903 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001904
David Turner993a8d02002-05-18 12:03:43 +00001905 p->flags &= ~_BDF_PROPS;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001906 *next = _bdf_parse_glyphs;
David Turner993a8d02002-05-18 12:03:43 +00001907
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001908 goto Exit;
1909 }
David Turner993a8d02002-05-18 12:03:43 +00001910
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001911 /* Ignore the _XFREE86_GLYPH_RANGES properties. */
1912 if ( ft_memcmp( line, "_XFREE86_GLYPH_RANGES", 21 ) == 0 )
1913 goto Exit;
1914
1915 /* Handle COMMENT fields and properties in a special way to preserve */
1916 /* the spacing. */
1917 if ( ft_memcmp( line, "COMMENT", 7 ) == 0 )
1918 {
David Turner993a8d02002-05-18 12:03:43 +00001919 name = value = line;
1920 value += 7;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001921 if ( *value )
David Turner993a8d02002-05-18 12:03:43 +00001922 *value++ = 0;
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001923 error = _bdf_add_property( p->font, name, value );
1924 if ( error )
1925 goto Exit;
1926 }
1927 else if ( _bdf_is_atom( line, linelen, &name, &value, p->font ) )
1928 {
1929 error = _bdf_add_property( p->font, name, value );
1930 if ( error )
1931 goto Exit;
1932 }
1933 else
1934 {
1935 error = _bdf_split( (char *)" +", line, linelen, &p->list, memory );
1936 if ( error )
1937 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +00001938 name = p->list.field[0];
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001939
1940 _bdf_shift( 1, &p->list );
1941 value = _bdf_join( ' ', &vlen, &p->list );
1942
1943 error = _bdf_add_property( p->font, name, value );
1944 if ( error )
1945 goto Exit;
1946 }
1947
1948 Exit:
1949 return error;
David Turner993a8d02002-05-18 12:03:43 +00001950 }
1951
David Turner993a8d02002-05-18 12:03:43 +00001952
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001953 /* Load the font header. */
1954 static FT_Error
1955 _bdf_parse_start( char* line,
1956 unsigned long linelen,
1957 unsigned long lineno,
1958 void* call_data,
1959 void* client_data )
1960 {
1961 unsigned long slen;
1962 _bdf_line_func_t* next;
1963 _bdf_parse_t* p;
1964 bdf_font_t* font;
1965 char *s;
David Turner993a8d02002-05-18 12:03:43 +00001966
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001967 FT_Memory memory;
1968 FT_Error error = BDF_Err_Ok;
1969
1970 FT_UNUSED( lineno ); /* only used in debug mode */
1971
1972
1973 next = (_bdf_line_func_t *)call_data;
1974 p = (_bdf_parse_t *) client_data;
1975
1976 if ( p->font )
David Turner993a8d02002-05-18 12:03:43 +00001977 memory = p->font->memory;
1978
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001979 /* Check for a comment. This is done to handle those fonts that have */
1980 /* comments before the STARTFONT line for some reason. */
1981 if ( ft_memcmp( line, "COMMENT", 7 ) == 0 )
1982 {
1983 if ( p->opts->keep_comments != 0 && p->font != 0 )
1984 {
1985 linelen -= 7;
1986
1987 s = line + 7;
1988 if ( *s != 0 )
1989 {
1990 s++;
1991 linelen--;
David Turner993a8d02002-05-18 12:03:43 +00001992 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +00001993
1994 error = _bdf_add_comment( p->font, s, linelen );
1995 if ( error )
1996 goto Exit;
1997 /* here font is not defined! */
1998 }
1999
2000 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +00002001 }
2002
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002003 if ( !( p->flags & _BDF_START ) )
2004 {
2005 memory = p->memory;
David Turner993a8d02002-05-18 12:03:43 +00002006
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002007 if ( ft_memcmp( line, "STARTFONT", 9 ) != 0 )
2008 {
2009 /* No STARTFONT field is a good indication of a problem. */
2010 error = BDF_Err_Missing_Startfont_Field;
2011 goto Exit;
2012 }
David Turner993a8d02002-05-18 12:03:43 +00002013
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002014 p->flags = _BDF_START;
2015 font = p->font = 0;
David Turner993a8d02002-05-18 12:03:43 +00002016
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002017 if ( FT_NEW( font ) )
2018 goto Exit;
2019 p->font = font;
David Turner993a8d02002-05-18 12:03:43 +00002020
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002021 font->memory = p->memory;
2022 p->memory = 0;
David Turner993a8d02002-05-18 12:03:43 +00002023
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002024 { /* setup */
2025 unsigned long i;
2026 bdf_property_t* prop;
David Turner993a8d02002-05-18 12:03:43 +00002027
David Turner993a8d02002-05-18 12:03:43 +00002028
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002029 error = hash_init( &(font->proptbl), memory );
2030 if ( error )
2031 goto Exit;
2032 for ( i = 0, prop = _bdf_properties;
2033 i < _num_bdf_properties; i++, prop++ )
2034 {
2035 error = hash_insert( prop->name, (void *)i,
2036 &(font->proptbl), memory );
2037 if ( error )
2038 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +00002039 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002040 }
2041
2042 if ( FT_ALLOC( p->font->internal, sizeof ( hashtable ) ) )
2043 goto Exit;
2044 error = hash_init( (hashtable *)p->font->internal,memory );
2045 if ( error )
2046 goto Exit;
2047 p->font->spacing = p->opts->font_spacing;
2048 p->font->default_glyph = -1;
2049
2050 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +00002051 }
2052
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002053 /* Check for the start of the properties. */
2054 if ( ft_memcmp( line, "STARTPROPERTIES", 15 ) == 0 )
2055 {
2056 error = _bdf_split( (char *)" +", line, linelen, &p->list, memory );
2057 if ( error )
2058 goto Exit;
2059 p->cnt = p->font->props_size = _bdf_atoul( p->list.field[1], 0, 10 );
2060
2061 if ( FT_NEW_ARRAY( p->font->props, p->cnt ) )
2062 goto Exit;
2063
2064 p->flags |= _BDF_PROPS;
2065 *next = _bdf_parse_properties;
2066
2067 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +00002068 }
2069
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002070 /* Check for the FONTBOUNDINGBOX field. */
2071 if ( ft_memcmp( line, "FONTBOUNDINGBOX", 15 ) == 0 )
2072 {
2073 if ( !(p->flags & _BDF_SIZE ) )
2074 {
2075 /* Missing the SIZE field. */
2076 FT_ERROR(( "_bdf_parse_start: " ERRMSG1, lineno, "SIZE" ));
2077 error = BDF_Err_Missing_Size_Field;
2078 goto Exit;
2079 }
2080
2081 error = _bdf_split( (char *)" +", line, linelen, &p->list , memory );
2082 if ( error )
2083 goto Exit;
2084
2085 p->font->bbx.width = _bdf_atos( p->list.field[1], 0, 10 );
2086 p->font->bbx.height = _bdf_atos( p->list.field[2], 0, 10 );
2087
2088 p->font->bbx.x_offset = _bdf_atos( p->list.field[3], 0, 10 );
2089 p->font->bbx.y_offset = _bdf_atos( p->list.field[4], 0, 10 );
2090
2091 p->font->bbx.ascent = p->font->bbx.height + p->font->bbx.y_offset;
2092 p->font->bbx.descent = -p->font->bbx.y_offset;
2093
2094 p->flags |= _BDF_FONT_BBX;
2095
2096 goto Exit;
2097 }
2098
2099 /* The next thing to check for is the FONT field. */
2100 if ( ft_memcmp( line, "FONT", 4 ) == 0 )
2101 {
2102 error = _bdf_split( (char *)" +", line, linelen, &p->list , memory );
2103 if ( error )
2104 goto Exit;
2105 _bdf_shift( 1, &p->list );
2106
2107 s = _bdf_join( ' ', &slen, &p->list );
2108 if ( FT_NEW_ARRAY( p->font->name, slen + 1 ) )
2109 goto Exit;
2110 FT_MEM_COPY( p->font->name, s, slen + 1 );
2111
2112 /* If the font name is an XLFD name, set the spacing to the one in */
2113 /* the font name. If there is no spacing fall back on the default. */
2114 error = _bdf_set_default_spacing( p->font, p->opts );
2115 if ( error )
2116 goto Exit;
2117
2118 p->flags |= _BDF_FONT_NAME;
2119
2120 goto Exit;
2121 }
2122
2123 /* Check for the SIZE field. */
2124 if ( ft_memcmp( line, "SIZE", 4 ) == 0 )
2125 {
2126 if ( !( p->flags & _BDF_FONT_NAME ) )
2127 {
2128 /* Missing the FONT field. */
2129 FT_ERROR(( "_bdf_parse_start: " ERRMSG1, lineno, "FONT" ));
2130 error = BDF_Err_Missing_Font_Field;
2131 goto Exit;
2132 }
2133
2134 error = _bdf_split( (char *)" +", line, linelen, &p->list, memory );
2135 if ( error )
2136 goto Exit;
2137
2138 p->font->point_size = _bdf_atoul( p->list.field[1], 0, 10 );
2139 p->font->resolution_x = _bdf_atoul( p->list.field[2], 0, 10 );
2140 p->font->resolution_y = _bdf_atoul( p->list.field[3], 0, 10 );
2141
2142 /* Check for the bits per pixel field. */
2143 if ( p->list.used == 5 )
2144 {
2145 p->font->bpp = _bdf_atos( p->list.field[4], 0, 10 );
2146 if ( p->font->bpp > 1 && ( p->font->bpp & 1 ) )
2147 {
2148 /* Move up to the next bits per pixel value if an odd number */
2149 /* is encountered. */
2150 p->font->bpp++;
2151 if ( p->font->bpp <= 4 )
2152 FT_TRACE2(( "_bdf_parse_start: " ACMSG11, p->font->bpp ));
David Turner993a8d02002-05-18 12:03:43 +00002153 }
David Turner993a8d02002-05-18 12:03:43 +00002154
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002155 if ( p->font->bpp > 4 )
2156 {
2157 FT_TRACE2(( "_bdf_parse_start: " ACMSG11, p->font->bpp ));
2158 p->font->bpp = 4;
2159 }
2160 }
2161 else
2162 p->font->bpp = 1;
David Turner993a8d02002-05-18 12:03:43 +00002163
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002164 p->flags |= _BDF_SIZE;
2165
2166 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +00002167 }
2168
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002169 error = BDF_Err_Invalid_File_Format;
David Turner993a8d02002-05-18 12:03:43 +00002170
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002171 Exit:
2172 return error;
2173 }
David Turner993a8d02002-05-18 12:03:43 +00002174
2175
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002176 /*************************************************************************/
2177 /* */
2178 /* API. */
2179 /* */
2180 /*************************************************************************/
David Turner993a8d02002-05-18 12:03:43 +00002181
David Turner993a8d02002-05-18 12:03:43 +00002182
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002183 FT_LOCAL_DEF( FT_Error )
2184 bdf_load_font( FT_Stream stream,
2185 FT_Memory extmemory,
2186 bdf_options_t* opts,
2187 bdf_font_t* *font )
2188 {
2189 unsigned long lineno;
2190 _bdf_parse_t p;
2191
2192 FT_Memory memory;
2193 FT_Error error = BDF_Err_Ok;
2194
2195
2196 FT_MEM_SET( &p, 0, sizeof ( _bdf_parse_t ) );
2197
2198 p.opts = ( opts != 0 ) ? opts : &_bdf_opts;
2199 p.minlb = 32767;
David Turner993a8d02002-05-18 12:03:43 +00002200 p.memory = extmemory; /* only during font creation */
2201
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002202 error = _bdf_readstream( stream, _bdf_parse_start,
2203 (void *)&p, &lineno );
2204 if ( error )
2205 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +00002206
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002207 if ( p.font != 0 )
2208 {
2209 /* If the font is not proportional, set the font's monowidth */
2210 /* field to the width of the font bounding box. */
David Turner993a8d02002-05-18 12:03:43 +00002211 memory = p.font->memory;
2212
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002213 if ( p.font->spacing != BDF_PROPORTIONAL )
2214 p.font->monowidth = p.font->bbx.width;
David Turner993a8d02002-05-18 12:03:43 +00002215
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002216 /* If the number of glyphs loaded is not that of the original count, */
2217 /* indicate the difference. */
2218 if ( p.cnt != p.font->glyphs_used + p.font->unencoded_used )
2219 {
2220 FT_TRACE2(( "bdf_load_font: " ACMSG15, p.cnt,
2221 p.font->glyphs_used + p.font->unencoded_used ));
2222 p.font->modified = 1;
2223 }
2224
2225 /* Once the font has been loaded, adjust the overall font metrics if */
2226 /* necessary. */
2227 if ( p.opts->correct_metrics != 0 &&
2228 ( p.font->glyphs_used > 0 || p.font->unencoded_used > 0 ) )
2229 {
2230 if ( p.maxrb - p.minlb != p.font->bbx.width )
2231 {
2232 FT_TRACE2(( "bdf_load_font: " ACMSG3,
2233 p.font->bbx.width, p.maxrb - p.minlb ));
2234 p.font->bbx.width = p.maxrb - p.minlb;
2235 p.font->modified = 1;
David Turner993a8d02002-05-18 12:03:43 +00002236 }
2237
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002238 if ( p.font->bbx.x_offset != p.minlb )
2239 {
2240 FT_TRACE2(( "bdf_load_font: " ACMSG4,
2241 p.font->bbx.x_offset, p.minlb ));
2242 p.font->bbx.x_offset = p.minlb;
2243 p.font->modified = 1;
David Turner993a8d02002-05-18 12:03:43 +00002244 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002245
2246 if ( p.font->bbx.ascent != p.maxas )
2247 {
2248 FT_TRACE2(( "bdf_load_font: " ACMSG5,
2249 p.font->bbx.ascent, p.maxas ));
2250 p.font->bbx.ascent = p.maxas;
2251 p.font->modified = 1;
2252 }
2253
2254 if ( p.font->bbx.descent != p.maxds )
2255 {
2256 FT_TRACE2(( "bdf_load_font: " ACMSG6,
2257 p.font->bbx.descent, p.maxds ));
2258 p.font->bbx.descent = p.maxds;
2259 p.font->bbx.y_offset = -p.maxds;
2260 p.font->modified = 1;
2261 }
2262
2263 if ( p.maxas + p.maxds != p.font->bbx.height )
2264 {
2265 FT_TRACE2(( "bdf_load_font: " ACMSG7,
2266 p.font->bbx.height, p.maxas + p.maxds ));
2267 p.font->bbx.height = p.maxas + p.maxds;
2268 }
2269
2270 if ( p.flags & _BDF_SWIDTH_ADJ )
2271 FT_TRACE2(( "bdf_load_font: " ACMSG8 ));
2272 }
David Turner993a8d02002-05-18 12:03:43 +00002273 }
2274
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002275 if ( p.flags & _BDF_START )
2276 {
2277 {
2278 /* The ENDFONT field was never reached or did not exist. */
2279 if ( !( p.flags & _BDF_GLYPHS ) )
2280 /* Error happened while parsing header. */
2281 FT_ERROR(( "bdf_load_font: " ERRMSG2, lineno ));
2282 else
2283 /* Error happened when parsing glyphs. */
2284 FT_ERROR(( "bdf_load_font: " ERRMSG3, lineno ));
2285 }
David Turner993a8d02002-05-18 12:03:43 +00002286 }
2287
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002288 /* Free up the list used during the parsing. */
2289 FT_FREE( p.list.field );
David Turner993a8d02002-05-18 12:03:43 +00002290
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002291 if ( p.font != 0 )
2292 {
2293 /* Make sure the comments are NULL terminated if they exist. */
2294 memory = p.font->memory;
David Turner993a8d02002-05-18 12:03:43 +00002295
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002296 if ( p.font->comments_len > 0 ) {
2297 if ( FT_RENEW_ARRAY( p.font->comments,
2298 p.font->comments_len,
2299 p.font->comments_len + 1 ) )
2300 goto Exit;
David Turner993a8d02002-05-18 12:03:43 +00002301
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002302 p.font->comments[p.font->comments_len] = 0;
2303 }
David Turner993a8d02002-05-18 12:03:43 +00002304 }
2305
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002306 *font = p.font;
2307
2308 Exit:
2309 return error;
2310 }
David Turner993a8d02002-05-18 12:03:43 +00002311
2312
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002313 FT_LOCAL_DEF( void )
2314 bdf_free_font( bdf_font_t* font )
2315 {
2316 bdf_property_t* prop;
2317 unsigned long i;
2318 bdf_glyph_t* glyphs;
2319 FT_Memory memory;
David Turner993a8d02002-05-18 12:03:43 +00002320
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002321
2322 if ( font == 0 )
2323 return;
David Turner993a8d02002-05-18 12:03:43 +00002324
2325 memory = font->memory;
2326
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002327 FT_FREE( font->name );
David Turner993a8d02002-05-18 12:03:43 +00002328
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002329 /* Free up the internal hash table of property names. */
2330 if ( font->internal )
2331 {
2332 hash_free( (hashtable *)font->internal, memory );
2333 FT_FREE( font->internal );
David Turner993a8d02002-05-18 12:03:43 +00002334 }
2335
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002336 /* Free up the comment info. */
2337 FT_FREE( font->comments );
David Turner993a8d02002-05-18 12:03:43 +00002338
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002339 /* Free up the properties. */
2340 for ( i = 0; i < font->props_size; i++ )
2341 {
2342 if ( font->props[i].format == BDF_ATOM )
2343 FT_FREE( font->props[i].value.atom );
David Turner993a8d02002-05-18 12:03:43 +00002344 }
2345
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002346 FT_FREE( font->props );
2347
2348 /* Free up the character info. */
2349 for ( i = 0, glyphs = font->glyphs;
2350 i < font->glyphs_used; i++, glyphs++ )
2351 {
2352 FT_FREE( glyphs->name );
2353 FT_FREE( glyphs->bitmap );
David Turner993a8d02002-05-18 12:03:43 +00002354 }
2355
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002356 for ( i = 0, glyphs = font->unencoded; i < font->unencoded_used;
2357 i++, glyphs++ )
2358 {
2359 FT_FREE( glyphs->name );
2360 FT_FREE( glyphs->bitmap );
David Turner993a8d02002-05-18 12:03:43 +00002361 }
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002362
2363 FT_FREE( font->glyphs );
2364 FT_FREE( font->unencoded );
2365
2366 /* Free up the overflow storage if it was used. */
2367 for ( i = 0, glyphs = font->overflow.glyphs;
2368 i < font->overflow.glyphs_used; i++, glyphs++ )
2369 {
2370 FT_FREE( glyphs->name );
2371 FT_FREE( glyphs->bitmap );
2372 }
2373
2374 FT_FREE( font->overflow.glyphs );
David Turner993a8d02002-05-18 12:03:43 +00002375
2376 /* bdf_cleanup */
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002377 hash_free( &(font->proptbl), memory );
David Turner993a8d02002-05-18 12:03:43 +00002378
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002379 /* Free up the user defined properties. */
2380 for (prop = font->user_props, i = 0;
2381 i < font->nuser_props; i++, prop++ )
2382 {
2383 FT_FREE( prop->name );
2384 if ( prop->format == BDF_ATOM )
2385 FT_FREE( prop->value.atom );
David Turner993a8d02002-05-18 12:03:43 +00002386 }
David Turner993a8d02002-05-18 12:03:43 +00002387
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002388 FT_FREE( font->user_props );
2389
2390 /* FREE( font ); */ /* XXX Fixme */
2391 }
David Turner993a8d02002-05-18 12:03:43 +00002392
2393
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002394 FT_LOCAL_DEF( bdf_property_t * )
2395 bdf_get_font_property( bdf_font_t* font,
2396 char* name )
2397 {
2398 hashnode hn;
David Turner993a8d02002-05-18 12:03:43 +00002399
David Turner993a8d02002-05-18 12:03:43 +00002400
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002401 if ( font == 0 || font->props_size == 0 || name == 0 || *name == 0 )
David Turner993a8d02002-05-18 12:03:43 +00002402 return 0;
2403
Werner Lemberg7cf4d372002-05-21 14:13:01 +00002404 hn = hash_lookup( name, (hashtable *)font->internal );
2405
2406 return hn ? ( font->props + (unsigned long)hn->data ) : 0;
2407 }
2408
2409
2410/* END */