David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2000 Computing Research Labs, New Mexico State University |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 3 | * Copyright 2001, 2002 Francesco Zappa Nardelli |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 4 | * |
| 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 Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 24 | /*************************************************************************/ |
| 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 Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 32 | |
| 33 | #include <ft2build.h> |
| 34 | |
Werner Lemberg | 02d4d59 | 2002-05-28 22:38:05 +0000 | [diff] [blame] | 35 | #include FT_FREETYPE_H |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 36 | #include FT_INTERNAL_DEBUG_H |
| 37 | #include FT_INTERNAL_STREAM_H |
| 38 | #include FT_INTERNAL_OBJECTS_H |
| 39 | |
| 40 | #include "bdf.h" |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 41 | #include "bdferror.h" |
| 42 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 43 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 44 | /*************************************************************************/ |
| 45 | /* */ |
| 46 | /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ |
| 47 | /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ |
| 48 | /* messages during execution. */ |
| 49 | /* */ |
| 50 | #undef FT_COMPONENT |
| 51 | #define FT_COMPONENT trace_bdflib |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 52 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 53 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 54 | /*************************************************************************/ |
| 55 | /* */ |
| 56 | /* Default BDF font options. */ |
| 57 | /* */ |
| 58 | /*************************************************************************/ |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 59 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 60 | |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 61 | static const bdf_options_t _bdf_opts = |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 62 | { |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 63 | 1, /* Correct metrics. */ |
| 64 | 1, /* Preserve unencoded glyphs. */ |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 65 | 0, /* Preserve comments. */ |
| 66 | BDF_PROPORTIONAL /* Default spacing. */ |
| 67 | }; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 68 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 69 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 70 | /*************************************************************************/ |
| 71 | /* */ |
| 72 | /* Builtin BDF font properties. */ |
| 73 | /* */ |
| 74 | /*************************************************************************/ |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 75 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 76 | /* List of most properties that might appear in a font. Doesn't include */ |
| 77 | /* the RAW_* and AXIS_* properties in X11R6 polymorphic fonts. */ |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 78 | |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 79 | static const bdf_property_t _bdf_properties[] = |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 80 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 81 | { (char *)"ADD_STYLE_NAME", BDF_ATOM, 1, { 0 } }, |
| 82 | { (char *)"AVERAGE_WIDTH", BDF_INTEGER, 1, { 0 } }, |
| 83 | { (char *)"AVG_CAPITAL_WIDTH", BDF_INTEGER, 1, { 0 } }, |
| 84 | { (char *)"AVG_LOWERCASE_WIDTH", BDF_INTEGER, 1, { 0 } }, |
| 85 | { (char *)"CAP_HEIGHT", BDF_INTEGER, 1, { 0 } }, |
| 86 | { (char *)"CHARSET_COLLECTIONS", BDF_ATOM, 1, { 0 } }, |
| 87 | { (char *)"CHARSET_ENCODING", BDF_ATOM, 1, { 0 } }, |
| 88 | { (char *)"CHARSET_REGISTRY", BDF_ATOM, 1, { 0 } }, |
| 89 | { (char *)"COMMENT", BDF_ATOM, 1, { 0 } }, |
| 90 | { (char *)"COPYRIGHT", BDF_ATOM, 1, { 0 } }, |
| 91 | { (char *)"DEFAULT_CHAR", BDF_CARDINAL, 1, { 0 } }, |
| 92 | { (char *)"DESTINATION", BDF_CARDINAL, 1, { 0 } }, |
| 93 | { (char *)"DEVICE_FONT_NAME", BDF_ATOM, 1, { 0 } }, |
| 94 | { (char *)"END_SPACE", BDF_INTEGER, 1, { 0 } }, |
| 95 | { (char *)"FACE_NAME", BDF_ATOM, 1, { 0 } }, |
| 96 | { (char *)"FAMILY_NAME", BDF_ATOM, 1, { 0 } }, |
| 97 | { (char *)"FIGURE_WIDTH", BDF_INTEGER, 1, { 0 } }, |
| 98 | { (char *)"FONT", BDF_ATOM, 1, { 0 } }, |
| 99 | { (char *)"FONTNAME_REGISTRY", BDF_ATOM, 1, { 0 } }, |
| 100 | { (char *)"FONT_ASCENT", BDF_INTEGER, 1, { 0 } }, |
| 101 | { (char *)"FONT_DESCENT", BDF_INTEGER, 1, { 0 } }, |
| 102 | { (char *)"FOUNDRY", BDF_ATOM, 1, { 0 } }, |
| 103 | { (char *)"FULL_NAME", BDF_ATOM, 1, { 0 } }, |
| 104 | { (char *)"ITALIC_ANGLE", BDF_INTEGER, 1, { 0 } }, |
| 105 | { (char *)"MAX_SPACE", BDF_INTEGER, 1, { 0 } }, |
| 106 | { (char *)"MIN_SPACE", BDF_INTEGER, 1, { 0 } }, |
| 107 | { (char *)"NORM_SPACE", BDF_INTEGER, 1, { 0 } }, |
| 108 | { (char *)"NOTICE", BDF_ATOM, 1, { 0 } }, |
| 109 | { (char *)"PIXEL_SIZE", BDF_INTEGER, 1, { 0 } }, |
| 110 | { (char *)"POINT_SIZE", BDF_INTEGER, 1, { 0 } }, |
| 111 | { (char *)"QUAD_WIDTH", BDF_INTEGER, 1, { 0 } }, |
| 112 | { (char *)"RAW_ASCENT", BDF_INTEGER, 1, { 0 } }, |
| 113 | { (char *)"RAW_AVERAGE_WIDTH", BDF_INTEGER, 1, { 0 } }, |
| 114 | { (char *)"RAW_AVG_CAPITAL_WIDTH", BDF_INTEGER, 1, { 0 } }, |
| 115 | { (char *)"RAW_AVG_LOWERCASE_WIDTH", BDF_INTEGER, 1, { 0 } }, |
| 116 | { (char *)"RAW_CAP_HEIGHT", BDF_INTEGER, 1, { 0 } }, |
| 117 | { (char *)"RAW_DESCENT", BDF_INTEGER, 1, { 0 } }, |
| 118 | { (char *)"RAW_END_SPACE", BDF_INTEGER, 1, { 0 } }, |
| 119 | { (char *)"RAW_FIGURE_WIDTH", BDF_INTEGER, 1, { 0 } }, |
| 120 | { (char *)"RAW_MAX_SPACE", BDF_INTEGER, 1, { 0 } }, |
| 121 | { (char *)"RAW_MIN_SPACE", BDF_INTEGER, 1, { 0 } }, |
| 122 | { (char *)"RAW_NORM_SPACE", BDF_INTEGER, 1, { 0 } }, |
| 123 | { (char *)"RAW_PIXEL_SIZE", BDF_INTEGER, 1, { 0 } }, |
| 124 | { (char *)"RAW_POINT_SIZE", BDF_INTEGER, 1, { 0 } }, |
| 125 | { (char *)"RAW_PIXELSIZE", BDF_INTEGER, 1, { 0 } }, |
| 126 | { (char *)"RAW_POINTSIZE", BDF_INTEGER, 1, { 0 } }, |
| 127 | { (char *)"RAW_QUAD_WIDTH", BDF_INTEGER, 1, { 0 } }, |
| 128 | { (char *)"RAW_SMALL_CAP_SIZE", BDF_INTEGER, 1, { 0 } }, |
| 129 | { (char *)"RAW_STRIKEOUT_ASCENT", BDF_INTEGER, 1, { 0 } }, |
| 130 | { (char *)"RAW_STRIKEOUT_DESCENT", BDF_INTEGER, 1, { 0 } }, |
| 131 | { (char *)"RAW_SUBSCRIPT_SIZE", BDF_INTEGER, 1, { 0 } }, |
| 132 | { (char *)"RAW_SUBSCRIPT_X", BDF_INTEGER, 1, { 0 } }, |
| 133 | { (char *)"RAW_SUBSCRIPT_Y", BDF_INTEGER, 1, { 0 } }, |
| 134 | { (char *)"RAW_SUPERSCRIPT_SIZE", BDF_INTEGER, 1, { 0 } }, |
| 135 | { (char *)"RAW_SUPERSCRIPT_X", BDF_INTEGER, 1, { 0 } }, |
| 136 | { (char *)"RAW_SUPERSCRIPT_Y", BDF_INTEGER, 1, { 0 } }, |
| 137 | { (char *)"RAW_UNDERLINE_POSITION", BDF_INTEGER, 1, { 0 } }, |
| 138 | { (char *)"RAW_UNDERLINE_THICKNESS", BDF_INTEGER, 1, { 0 } }, |
| 139 | { (char *)"RAW_X_HEIGHT", BDF_INTEGER, 1, { 0 } }, |
| 140 | { (char *)"RELATIVE_SETWIDTH", BDF_CARDINAL, 1, { 0 } }, |
| 141 | { (char *)"RELATIVE_WEIGHT", BDF_CARDINAL, 1, { 0 } }, |
| 142 | { (char *)"RESOLUTION", BDF_INTEGER, 1, { 0 } }, |
| 143 | { (char *)"RESOLUTION_X", BDF_CARDINAL, 1, { 0 } }, |
| 144 | { (char *)"RESOLUTION_Y", BDF_CARDINAL, 1, { 0 } }, |
| 145 | { (char *)"SETWIDTH_NAME", BDF_ATOM, 1, { 0 } }, |
| 146 | { (char *)"SLANT", BDF_ATOM, 1, { 0 } }, |
| 147 | { (char *)"SMALL_CAP_SIZE", BDF_INTEGER, 1, { 0 } }, |
| 148 | { (char *)"SPACING", BDF_ATOM, 1, { 0 } }, |
| 149 | { (char *)"STRIKEOUT_ASCENT", BDF_INTEGER, 1, { 0 } }, |
| 150 | { (char *)"STRIKEOUT_DESCENT", BDF_INTEGER, 1, { 0 } }, |
| 151 | { (char *)"SUBSCRIPT_SIZE", BDF_INTEGER, 1, { 0 } }, |
| 152 | { (char *)"SUBSCRIPT_X", BDF_INTEGER, 1, { 0 } }, |
| 153 | { (char *)"SUBSCRIPT_Y", BDF_INTEGER, 1, { 0 } }, |
| 154 | { (char *)"SUPERSCRIPT_SIZE", BDF_INTEGER, 1, { 0 } }, |
| 155 | { (char *)"SUPERSCRIPT_X", BDF_INTEGER, 1, { 0 } }, |
| 156 | { (char *)"SUPERSCRIPT_Y", BDF_INTEGER, 1, { 0 } }, |
| 157 | { (char *)"UNDERLINE_POSITION", BDF_INTEGER, 1, { 0 } }, |
| 158 | { (char *)"UNDERLINE_THICKNESS", BDF_INTEGER, 1, { 0 } }, |
| 159 | { (char *)"WEIGHT", BDF_CARDINAL, 1, { 0 } }, |
| 160 | { (char *)"WEIGHT_NAME", BDF_ATOM, 1, { 0 } }, |
| 161 | { (char *)"X_HEIGHT", BDF_INTEGER, 1, { 0 } }, |
| 162 | { (char *)"_MULE_BASELINE_OFFSET", BDF_INTEGER, 1, { 0 } }, |
| 163 | { (char *)"_MULE_RELATIVE_COMPOSE", BDF_INTEGER, 1, { 0 } }, |
| 164 | }; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 165 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 166 | static unsigned long |
| 167 | _num_bdf_properties = sizeof ( _bdf_properties ) / |
| 168 | sizeof ( _bdf_properties[0] ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 169 | |
| 170 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 171 | /*************************************************************************/ |
| 172 | /* */ |
| 173 | /* Hash table utilities for the properties. */ |
| 174 | /* */ |
| 175 | /*************************************************************************/ |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 176 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 177 | /* XXX: Replace this with FreeType's hash functions */ |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 178 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 179 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 180 | #define INITIAL_HT_SIZE 241 |
| 181 | |
| 182 | typedef void |
| 183 | (*hash_free_func)( hashnode node ); |
| 184 | |
| 185 | static hashnode* |
| 186 | hash_bucket( char* key, |
| 187 | hashtable* ht ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 188 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 189 | char* kp = key; |
| 190 | unsigned long res = 0; |
| 191 | hashnode* bp = ht->table, *ndp; |
| 192 | |
| 193 | |
| 194 | /* Mocklisp hash function. */ |
| 195 | while ( *kp ) |
| 196 | res = ( res << 5 ) - res + *kp++; |
| 197 | |
| 198 | ndp = bp + ( res % ht->size ); |
| 199 | while ( *ndp ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 200 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 201 | kp = (*ndp)->key; |
| 202 | if ( kp[0] == key[0] && ft_strcmp( kp, key ) == 0 ) |
| 203 | break; |
| 204 | ndp--; |
| 205 | if ( ndp < bp ) |
| 206 | ndp = bp + ( ht->size - 1 ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 207 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 208 | |
| 209 | return ndp; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 210 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 211 | |
| 212 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 213 | static FT_Error |
| 214 | hash_rehash( hashtable* ht, |
| 215 | FT_Memory memory ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 216 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 217 | hashnode* obp = ht->table, *bp, *nbp; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 218 | int i, sz = ht->size; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 219 | FT_Error error = BDF_Err_Ok; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 220 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 221 | |
| 222 | ht->size <<= 1; |
| 223 | ht->limit = ht->size / 3; |
| 224 | |
| 225 | if ( FT_NEW_ARRAY( ht->table, ht->size ) ) |
| 226 | goto Exit; |
| 227 | FT_MEM_SET( ht->table, 0, sizeof ( hashnode ) * ht->size ); |
| 228 | |
| 229 | for ( i = 0, bp = obp; i < sz; i++, bp++ ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 230 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 231 | if ( *bp ) |
| 232 | { |
| 233 | nbp = hash_bucket( (*bp)->key, ht ); |
| 234 | *nbp = *bp; |
| 235 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 236 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 237 | FT_FREE( obp ); |
| 238 | |
| 239 | Exit: |
| 240 | return error; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 241 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 242 | |
| 243 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 244 | static FT_Error |
| 245 | hash_init( hashtable* ht, |
| 246 | FT_Memory memory ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 247 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 248 | int sz = INITIAL_HT_SIZE; |
| 249 | FT_Error error = BDF_Err_Ok; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 250 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 251 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 252 | ht->size = sz; |
| 253 | ht->limit = sz / 3; |
| 254 | ht->used = 0; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 255 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 256 | if ( FT_NEW_ARRAY( ht->table, sz ) ) |
| 257 | goto Exit; |
| 258 | FT_MEM_SET( ht->table, 0, sizeof ( hashnode ) * sz ); |
| 259 | |
| 260 | Exit: |
| 261 | return error; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 262 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 263 | |
| 264 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 265 | static void |
| 266 | hash_free( hashtable* ht, |
| 267 | FT_Memory memory ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 268 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 269 | if ( ht != 0 ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 270 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 271 | int i, sz = ht->size; |
| 272 | hashnode* bp = ht->table; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 273 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 274 | |
| 275 | for ( i = 0; i < sz; i++, bp++ ) |
| 276 | FT_FREE( *bp ); |
| 277 | |
| 278 | FT_FREE( ht->table ); |
| 279 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 280 | } |
| 281 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 282 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 283 | static FT_Error |
| 284 | hash_insert( char* key, |
| 285 | void* data, |
| 286 | hashtable* ht, |
| 287 | FT_Memory memory ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 288 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 289 | hashnode nn, *bp = hash_bucket( key, ht ); |
| 290 | FT_Error error = BDF_Err_Ok; |
| 291 | |
| 292 | |
| 293 | nn = *bp; |
| 294 | if ( !nn ) |
| 295 | { |
| 296 | if ( FT_NEW( nn ) ) |
| 297 | goto Exit; |
| 298 | *bp = nn; |
| 299 | |
| 300 | nn->key = key; |
| 301 | nn->data = data; |
| 302 | |
| 303 | if ( ht->used >= ht->limit ) |
| 304 | { |
| 305 | error = hash_rehash( ht, memory ); |
| 306 | if ( error ) |
| 307 | goto Exit; |
| 308 | } |
| 309 | ht->used++; |
| 310 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 311 | else |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 312 | nn->data = data; |
| 313 | |
| 314 | Exit: |
| 315 | return error; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 316 | } |
| 317 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 318 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 319 | static hashnode |
| 320 | hash_lookup( char* key, |
| 321 | hashtable* ht ) |
| 322 | { |
| 323 | hashnode *np = hash_bucket( key, ht ); |
| 324 | |
| 325 | |
| 326 | return *np; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | /*************************************************************************/ |
| 331 | /* */ |
| 332 | /* Utility types and functions. */ |
| 333 | /* */ |
| 334 | /*************************************************************************/ |
| 335 | |
| 336 | |
| 337 | /* Function type for parsing lines of a BDF font. */ |
| 338 | |
| 339 | typedef FT_Error |
| 340 | (*_bdf_line_func_t)( char* line, |
| 341 | unsigned long linelen, |
| 342 | unsigned long lineno, |
| 343 | void* call_data, |
| 344 | void* client_data ); |
| 345 | |
| 346 | |
| 347 | /* List structure for splitting lines into fields. */ |
| 348 | |
| 349 | typedef struct _bdf_list_t_ |
| 350 | { |
| 351 | char** field; |
| 352 | unsigned long size; |
| 353 | unsigned long used; |
| 354 | |
| 355 | } _bdf_list_t; |
| 356 | |
| 357 | |
| 358 | /* Structure used while loading BDF fonts. */ |
| 359 | |
| 360 | typedef struct _bdf_parse_t_ |
| 361 | { |
| 362 | unsigned long flags; |
| 363 | unsigned long cnt; |
| 364 | unsigned long row; |
| 365 | |
| 366 | short minlb; |
| 367 | short maxlb; |
| 368 | short maxrb; |
| 369 | short maxas; |
| 370 | short maxds; |
| 371 | |
| 372 | short rbearing; |
| 373 | |
| 374 | char* glyph_name; |
| 375 | long glyph_enc; |
| 376 | |
| 377 | bdf_font_t* font; |
| 378 | bdf_options_t* opts; |
| 379 | |
| 380 | unsigned long have[2048]; |
| 381 | _bdf_list_t list; |
| 382 | |
| 383 | FT_Memory memory; |
| 384 | |
| 385 | } _bdf_parse_t; |
| 386 | |
| 387 | |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 388 | #define setsbit( m, cc ) ( m[(cc) >> 3] |= (FT_Byte)( 1 << ( (cc) & 7 ) ) ) |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 389 | #define sbitset( m, cc ) ( m[(cc) >> 3] & ( 1 << ( (cc) & 7 ) ) ) |
| 390 | |
| 391 | |
| 392 | /* An empty string for empty fields. */ |
| 393 | |
| 394 | static char empty[1] = { 0 }; /* XXX eliminate this */ |
| 395 | |
| 396 | |
| 397 | /* Assume the line is NULL-terminated and that the `list' parameter */ |
| 398 | /* was initialized the first time it was used. */ |
| 399 | |
| 400 | static FT_Error |
| 401 | _bdf_split( char* separators, |
| 402 | char* line, |
| 403 | unsigned long linelen, |
| 404 | _bdf_list_t* list, |
| 405 | FT_Memory memory ) |
| 406 | { |
| 407 | int mult, final_empty; |
| 408 | char *sp, *ep, *end; |
| 409 | char seps[32]; |
| 410 | FT_Error error = BDF_Err_Ok; |
| 411 | |
| 412 | |
| 413 | /* Initialize the list. */ |
| 414 | list->used = 0; |
| 415 | |
| 416 | /* If the line is empty, then simply return. */ |
| 417 | if ( linelen == 0 || line[0] == 0 ) |
| 418 | goto Exit; |
| 419 | |
| 420 | /* In the original code, if the `separators' parameter is NULL or */ |
| 421 | /* empty, the list is split into individual bytes. We don't need */ |
| 422 | /* this, so an error is signaled. */ |
| 423 | if ( separators == 0 || *separators == 0 ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 424 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 425 | error = BDF_Err_Invalid_Argument; |
| 426 | goto Exit; |
| 427 | } |
| 428 | |
| 429 | /* Prepare the separator bitmap. */ |
| 430 | FT_MEM_SET( seps, 0, 32 ); |
| 431 | |
| 432 | /* If the very last character of the separator string is a plus, then */ |
| 433 | /* set the `mult' flag to indicate that multiple separators should be */ |
| 434 | /* collapsed into one. */ |
| 435 | for ( mult = 0, sp = separators; sp && *sp; sp++ ) |
| 436 | { |
| 437 | if ( *sp == '+' && *( sp + 1 ) == 0 ) |
| 438 | mult = 1; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 439 | else |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 440 | setsbit( seps, *sp ); |
| 441 | } |
| 442 | |
| 443 | /* Break the line up into fields. */ |
| 444 | for ( final_empty = 0, sp = ep = line, end = sp + linelen; |
| 445 | sp < end && *sp; ) |
| 446 | { |
| 447 | /* Collect everything that is not a separator. */ |
| 448 | for ( ; *ep && !sbitset( seps, *ep ); ep++ ) |
| 449 | ; |
| 450 | |
| 451 | /* Resize the list if necessary. */ |
| 452 | if ( list->used == list->size ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 453 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 454 | if ( list->size == 0 ) |
| 455 | { |
| 456 | if ( FT_NEW_ARRAY( list->field, 5 ) ) |
| 457 | goto Exit; |
| 458 | } |
| 459 | else |
| 460 | { |
| 461 | if ( FT_RENEW_ARRAY ( list->field , |
| 462 | list->size, |
| 463 | list->size + 5 ) ) |
| 464 | goto Exit; |
| 465 | } |
| 466 | |
| 467 | list->size += 5; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 468 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 469 | |
| 470 | /* Assign the field appropriately. */ |
| 471 | list->field[list->used++] = ( ep > sp ) ? sp : empty; |
| 472 | |
| 473 | sp = ep; |
| 474 | |
| 475 | if ( mult ) |
| 476 | { |
| 477 | /* If multiple separators should be collapsed, do it now by */ |
| 478 | /* setting all the separator characters to 0. */ |
| 479 | for ( ; *ep && sbitset( seps, *ep ); ep++ ) |
| 480 | *ep = 0; |
| 481 | } |
| 482 | else if ( *ep != 0 ) |
| 483 | /* Don't collapse multiple separators by making them 0, so just */ |
| 484 | /* make the one encountered 0. */ |
| 485 | *ep++ = 0; |
| 486 | |
| 487 | final_empty = ( ep > sp && *ep == 0 ); |
| 488 | sp = ep; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 491 | /* Finally, NULL-terminate the list. */ |
| 492 | if ( list->used + final_empty + 1 >= list->size ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 493 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 494 | if ( list->used == list->size ) |
| 495 | { |
| 496 | if ( list->size == 0 ) |
| 497 | { |
| 498 | if ( FT_NEW_ARRAY( list->field, 5 ) ) |
| 499 | goto Exit; |
| 500 | } |
| 501 | else |
| 502 | { |
| 503 | if ( FT_RENEW_ARRAY( list->field, |
| 504 | list->size, |
| 505 | list->size + 5 ) ) |
| 506 | goto Exit; |
| 507 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 508 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 509 | list->size += 5; |
| 510 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 513 | if ( final_empty ) |
| 514 | list->field[list->used++] = empty; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 515 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 516 | if ( list->used == list->size ) |
| 517 | { |
| 518 | if ( list->size == 0 ) |
| 519 | { |
| 520 | if ( FT_NEW_ARRAY( list->field, 5 ) ) |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 521 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 522 | } |
| 523 | else |
| 524 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 525 | if ( FT_RENEW_ARRAY( list->field, |
| 526 | list->size, |
| 527 | list->size + 5 ) ) |
| 528 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 529 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 530 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 531 | list->size += 5; |
| 532 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 533 | |
| 534 | list->field[list->used] = 0; |
| 535 | |
| 536 | Exit: |
| 537 | return error; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 538 | } |
| 539 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 540 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 541 | static void |
| 542 | _bdf_shift( unsigned long n, |
| 543 | _bdf_list_t* list ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 544 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 545 | unsigned long i, u; |
| 546 | |
| 547 | |
| 548 | if ( list == 0 || list->used == 0 || n == 0 ) |
| 549 | return; |
| 550 | |
| 551 | if ( n >= list->used ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 552 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 553 | list->used = 0; |
| 554 | return; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 555 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 556 | |
| 557 | for ( u = n, i = 0; u < list->used; i++, u++ ) |
| 558 | list->field[i] = list->field[u]; |
| 559 | list->used -= n; |
| 560 | } |
| 561 | |
| 562 | |
| 563 | static char * |
| 564 | _bdf_join( int c, |
| 565 | unsigned long* len, |
| 566 | _bdf_list_t* list ) |
| 567 | { |
| 568 | unsigned long i, j; |
| 569 | char *fp, *dp; |
| 570 | |
| 571 | |
| 572 | if ( list == 0 || list->used == 0 ) |
| 573 | return 0; |
| 574 | |
| 575 | *len = 0; |
| 576 | |
| 577 | dp = list->field[0]; |
| 578 | for ( i = j = 0; i < list->used; i++ ) |
| 579 | { |
| 580 | fp = list->field[i]; |
| 581 | while ( *fp ) |
| 582 | dp[j++] = *fp++; |
| 583 | |
| 584 | if ( i + 1 < list->used ) |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 585 | dp[j++] = (char)c; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 586 | } |
| 587 | dp[j] = 0; |
| 588 | |
| 589 | *len = j; |
| 590 | return dp; |
| 591 | } |
| 592 | |
| 593 | |
| 594 | /* High speed file reader that passes each line to a callback. */ |
| 595 | static FT_Error |
| 596 | bdf_internal_readstream( FT_Stream stream, |
| 597 | char* buffer, |
| 598 | int count, |
| 599 | int *read_bytes ) |
| 600 | { |
| 601 | int rbytes; |
| 602 | unsigned long pos = stream->pos; |
| 603 | FT_Error error = BDF_Err_Ok; |
| 604 | |
| 605 | |
| 606 | if ( pos > stream->size ) |
| 607 | { |
| 608 | FT_ERROR(( "bdf_internal_readstream:" )); |
| 609 | FT_ERROR(( " invalid i/o; pos = 0x%lx, size = 0x%lx\n", |
| 610 | pos, stream->size )); |
| 611 | error = BDF_Err_Invalid_Stream_Operation; |
| 612 | goto Exit; |
| 613 | } |
| 614 | |
| 615 | if ( stream->read ) |
| 616 | rbytes = stream->read( stream, pos, |
| 617 | (unsigned char *)buffer, count ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 618 | else |
| 619 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 620 | rbytes = stream->size - pos; |
| 621 | if ( rbytes > count ) |
| 622 | rbytes = count; |
| 623 | |
| 624 | FT_MEM_COPY( buffer, stream->base + pos, rbytes ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 625 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 626 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 627 | stream->pos = pos + rbytes; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 628 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 629 | *read_bytes = rbytes; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 630 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 631 | Exit: |
| 632 | return error; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 635 | |
| 636 | static FT_Error |
| 637 | _bdf_readstream( FT_Stream stream, |
| 638 | _bdf_line_func_t callback, |
| 639 | void* client_data, |
| 640 | unsigned long *lno ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 641 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 642 | _bdf_line_func_t cb; |
| 643 | unsigned long lineno; |
| 644 | int n, res, done, refill, bytes, hold; |
| 645 | char *ls, *le, *pp, *pe, *hp; |
| 646 | /* XXX: Use a dynamic buffer */ |
| 647 | char buf[65536L]; |
| 648 | FT_Error error = BDF_Err_Ok; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 649 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 650 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 651 | if ( callback == 0 ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 652 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 653 | error = BDF_Err_Invalid_Argument; |
| 654 | goto Exit; |
| 655 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 656 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 657 | cb = callback; |
| 658 | lineno = 1; |
| 659 | buf[0] = 0; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 660 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 661 | res = done = 0; |
| 662 | pp = ls = le = buf; |
| 663 | |
| 664 | bytes = 65536L; |
| 665 | |
| 666 | while ( !done ) |
| 667 | { |
| 668 | error = bdf_internal_readstream( stream, pp, bytes, &n ); |
| 669 | if ( error ) |
| 670 | goto Exit; |
| 671 | |
| 672 | if ( n == 0 ) |
| 673 | break; |
| 674 | |
| 675 | /* Determine the new end of the buffer pages. */ |
| 676 | pe = pp + n; |
| 677 | |
| 678 | for ( refill = 0; done == 0 && refill == 0; ) |
| 679 | { |
| 680 | while ( le < pe && *le != '\n' && *le != '\r' ) |
| 681 | le++; |
| 682 | |
| 683 | if ( le == pe ) |
| 684 | { |
| 685 | /* Hit the end of the last page in the buffer. Need to find */ |
| 686 | /* out how many pages to shift and how many pages need to be */ |
| 687 | /* read in. Adjust the line start and end pointers down to */ |
| 688 | /* point to the right places in the pages. */ |
| 689 | |
| 690 | pp = buf + ( ( ( ls - buf ) >> 13 ) << 13 ); |
| 691 | n = pp - buf; |
| 692 | ls -= n; |
| 693 | le -= n; |
| 694 | n = pe - pp; |
| 695 | |
| 696 | FT_MEM_COPY( buf, pp, n ); |
| 697 | |
| 698 | pp = buf + n; |
| 699 | bytes = 65536L - n; |
| 700 | refill = 1; |
| 701 | } |
| 702 | else |
| 703 | { |
| 704 | /* Temporarily NULL-terminate the line. */ |
| 705 | hp = le; |
| 706 | hold = *le; |
| 707 | *le = 0; |
| 708 | |
| 709 | /* XXX: Use encoding independent value for 0x1a */ |
| 710 | if ( *ls != '#' && *ls != 0x1a && |
| 711 | le > ls && |
| 712 | ( error = (*cb)( ls, le - ls, lineno, (void *)&cb, |
| 713 | client_data ) ) != BDF_Err_Ok ) |
| 714 | done = 1; |
| 715 | else |
| 716 | { |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 717 | ls = ++le; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 718 | /* Handle the case of DOS crlf sequences. */ |
| 719 | if ( le < pe && hold == '\n' && *le =='\r' ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 720 | ls = ++le; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /* Increment the line number. */ |
| 724 | lineno++; |
| 725 | |
| 726 | /* Restore the character at the end of the line. */ |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 727 | *hp = (char)hold; |
David Turner | d490e37 | 2002-05-28 23:40:37 +0000 | [diff] [blame^] | 728 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 731 | |
| 732 | *lno = lineno; |
| 733 | |
| 734 | Exit: |
| 735 | return error; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 736 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 737 | |
| 738 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 739 | /* XXX: make this work with EBCDIC also */ |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 740 | |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 741 | static const unsigned char a2i[128] = |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 742 | { |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 747 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00, |
| 748 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, |
| 749 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 750 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 751 | 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 752 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 753 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 754 | }; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 755 | |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 756 | static const unsigned char odigits[32] = |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 757 | { |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 758 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, |
| 759 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 760 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 761 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 762 | }; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 763 | |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 764 | static const unsigned char ddigits[32] = |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 765 | { |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 766 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03, |
| 767 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 768 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 769 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 770 | }; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 771 | |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 772 | static const unsigned char hdigits[32] = |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 773 | { |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 774 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03, |
| 775 | 0x7e, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, |
| 776 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 777 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 778 | }; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 779 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 780 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 781 | #define isdigok( m, d ) (m[(d) >> 3] & ( 1 << ( (d) & 7 ) ) ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 782 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 783 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 784 | /* Routine to convert an ASCII string into an unsigned long integer. */ |
| 785 | static unsigned long |
| 786 | _bdf_atoul( char* s, |
| 787 | char** end, |
| 788 | int base ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 789 | { |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 790 | unsigned long v; |
| 791 | const unsigned char* dmap; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 792 | |
| 793 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 794 | if ( s == 0 || *s == 0 ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 795 | return 0; |
| 796 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 797 | /* Make sure the radix is something recognizable. Default to 10. */ |
| 798 | switch ( base ) |
| 799 | { |
| 800 | case 8: |
| 801 | dmap = odigits; |
| 802 | break; |
| 803 | case 16: |
| 804 | dmap = hdigits; |
| 805 | break; |
| 806 | default: |
| 807 | base = 10; |
| 808 | dmap = ddigits; |
| 809 | break; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 812 | /* Check for the special hex prefix. */ |
| 813 | if ( *s == '0' && |
| 814 | ( *( s + 1 ) == 'x' || *( s + 1 ) == 'X' ) ) |
| 815 | { |
| 816 | base = 16; |
| 817 | dmap = hdigits; |
| 818 | s += 2; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 821 | for ( v = 0; isdigok( dmap, *s ); s++ ) |
| 822 | v = v * base + a2i[(int)*s]; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 823 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 824 | if ( end != 0 ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 825 | *end = s; |
| 826 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 827 | return v; |
| 828 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 829 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 830 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 831 | /* Routine to convert an ASCII string into an signed long integer. */ |
| 832 | static long |
| 833 | _bdf_atol( char* s, |
| 834 | char** end, |
| 835 | int base ) |
| 836 | { |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 837 | long v, neg; |
| 838 | const unsigned char* dmap; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 839 | |
| 840 | |
| 841 | if ( s == 0 || *s == 0 ) |
| 842 | return 0; |
| 843 | |
| 844 | /* Make sure the radix is something recognizable. Default to 10. */ |
| 845 | switch ( base ) |
| 846 | { |
| 847 | case 8: |
| 848 | dmap = odigits; |
| 849 | break; |
| 850 | case 16: |
| 851 | dmap = hdigits; |
| 852 | break; |
| 853 | default: |
| 854 | base = 10; |
| 855 | dmap = ddigits; |
| 856 | break; |
| 857 | } |
| 858 | |
| 859 | /* Check for a minus sign. */ |
| 860 | neg = 0; |
| 861 | if ( *s == '-' ) |
| 862 | { |
| 863 | s++; |
| 864 | neg = 1; |
| 865 | } |
| 866 | |
| 867 | /* Check for the special hex prefix. */ |
| 868 | if ( *s == '0' && |
| 869 | ( *( s + 1 ) == 'x' || *( s + 1 ) == 'X' ) ) |
| 870 | { |
| 871 | base = 16; |
| 872 | dmap = hdigits; |
| 873 | s += 2; |
| 874 | } |
| 875 | |
| 876 | for ( v = 0; isdigok( dmap, *s ); s++ ) |
| 877 | v = v * base + a2i[(int)*s]; |
| 878 | |
| 879 | if ( end != 0 ) |
| 880 | *end = s; |
| 881 | |
| 882 | return ( !neg ) ? v : -v; |
| 883 | } |
| 884 | |
| 885 | |
| 886 | /* Routine to convert an ASCII string into an signed short integer. */ |
| 887 | static short |
| 888 | _bdf_atos( char* s, |
| 889 | char** end, |
| 890 | int base ) |
| 891 | { |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 892 | short v, neg; |
| 893 | const unsigned char* dmap; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 894 | |
| 895 | |
| 896 | if ( s == 0 || *s == 0 ) |
| 897 | return 0; |
| 898 | |
| 899 | /* Make sure the radix is something recognizable. Default to 10. */ |
| 900 | switch ( base ) |
| 901 | { |
| 902 | case 8: |
| 903 | dmap = odigits; |
| 904 | break; |
| 905 | case 16: |
| 906 | dmap = hdigits; |
| 907 | break; |
| 908 | default: |
| 909 | base = 10; |
| 910 | dmap = ddigits; |
| 911 | break; |
| 912 | } |
| 913 | |
| 914 | /* Check for a minus. */ |
| 915 | neg = 0; |
| 916 | if ( *s == '-' ) |
| 917 | { |
| 918 | s++; |
| 919 | neg = 1; |
| 920 | } |
| 921 | |
| 922 | /* Check for the special hex prefix. */ |
| 923 | if ( *s == '0' && |
| 924 | ( *( s + 1 ) == 'x' || *( s + 1 ) == 'X' ) ) |
| 925 | { |
| 926 | base = 16; |
| 927 | dmap = hdigits; |
| 928 | s += 2; |
| 929 | } |
| 930 | |
| 931 | for ( v = 0; isdigok( dmap, *s ); s++ ) |
Werner Lemberg | 233302a | 2002-05-22 05:41:06 +0000 | [diff] [blame] | 932 | v = (short)( v * base + a2i[(int)*s] ); |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 933 | |
| 934 | if ( end != 0 ) |
| 935 | *end = s; |
| 936 | |
Werner Lemberg | 233302a | 2002-05-22 05:41:06 +0000 | [diff] [blame] | 937 | return (short)( ( !neg ) ? v : -v ); |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | |
| 941 | /* Routine to compare two glyphs by encoding so they can be sorted. */ |
| 942 | static int |
| 943 | by_encoding( const void* a, |
| 944 | const void* b ) |
| 945 | { |
| 946 | bdf_glyph_t *c1, *c2; |
| 947 | |
| 948 | |
| 949 | c1 = (bdf_glyph_t *)a; |
| 950 | c2 = (bdf_glyph_t *)b; |
| 951 | |
| 952 | if ( c1->encoding < c2->encoding ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 953 | return -1; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 954 | else if ( c1->encoding > c2->encoding ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 955 | return 1; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 956 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 957 | return 0; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 960 | |
| 961 | static FT_Error |
| 962 | bdf_create_property( char* name, |
| 963 | int format, |
| 964 | bdf_font_t* font ) |
| 965 | { |
| 966 | unsigned long n; |
| 967 | bdf_property_t* p; |
| 968 | FT_Memory memory = font->memory; |
| 969 | FT_Error error = BDF_Err_Ok; |
| 970 | |
| 971 | |
| 972 | /* First check to see if the property has */ |
| 973 | /* already been added or not. If it has, then */ |
| 974 | /* simply ignore it. */ |
| 975 | if ( hash_lookup( name, &(font->proptbl) ) ) |
| 976 | goto Exit; |
| 977 | |
| 978 | if ( font->nuser_props == 0 ) |
| 979 | { |
| 980 | if ( FT_NEW_ARRAY( font->user_props, 1 ) ) |
| 981 | goto Exit; |
| 982 | } |
| 983 | else |
| 984 | { |
| 985 | if ( FT_RENEW_ARRAY( font->user_props, |
| 986 | font->nuser_props, |
| 987 | font->nuser_props + 1 ) ) |
| 988 | goto Exit; |
| 989 | } |
| 990 | |
| 991 | p = font->user_props + font->nuser_props; |
| 992 | FT_MEM_SET( p, 0, sizeof ( bdf_property_t ) ); |
| 993 | |
| 994 | n = (unsigned long)( ft_strlen( name ) + 1 ); |
| 995 | if ( FT_NEW_ARRAY( p->name, n ) ) |
| 996 | goto Exit; |
| 997 | |
| 998 | FT_MEM_COPY( (char *)p->name, name, n ); |
| 999 | |
| 1000 | p->format = format; |
| 1001 | p->builtin = 0; |
| 1002 | |
| 1003 | n = _num_bdf_properties + font->nuser_props; |
| 1004 | |
| 1005 | error = hash_insert( p->name, (void *)n, &(font->proptbl), memory ); |
| 1006 | if ( error ) |
| 1007 | goto Exit; |
| 1008 | |
| 1009 | font->nuser_props++; |
| 1010 | |
| 1011 | Exit: |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1012 | return error; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1013 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1014 | |
| 1015 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1016 | FT_LOCAL_DEF( bdf_property_t * ) |
| 1017 | bdf_get_property( char* name, |
| 1018 | bdf_font_t* font ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1019 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1020 | hashnode hn; |
| 1021 | unsigned long propid; |
| 1022 | |
| 1023 | |
| 1024 | if ( name == 0 || *name == 0 ) |
| 1025 | return 0; |
| 1026 | |
| 1027 | if ( ( hn = hash_lookup( name, &(font->proptbl) ) ) == 0 ) |
| 1028 | return 0; |
| 1029 | |
| 1030 | propid = (unsigned long)hn->data; |
| 1031 | if ( propid >= _num_bdf_properties ) |
| 1032 | return font->user_props + ( propid - _num_bdf_properties ); |
| 1033 | |
Werner Lemberg | 233302a | 2002-05-22 05:41:06 +0000 | [diff] [blame] | 1034 | return (bdf_property_t*)_bdf_properties + propid; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1035 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1036 | |
| 1037 | |
| 1038 | /*************************************************************************/ |
| 1039 | /* */ |
| 1040 | /* BDF font file parsing flags and functions. */ |
| 1041 | /* */ |
| 1042 | /*************************************************************************/ |
| 1043 | |
| 1044 | |
| 1045 | /* Parse flags. */ |
| 1046 | |
| 1047 | #define _BDF_START 0x0001 |
| 1048 | #define _BDF_FONT_NAME 0x0002 |
| 1049 | #define _BDF_SIZE 0x0004 |
| 1050 | #define _BDF_FONT_BBX 0x0008 |
| 1051 | #define _BDF_PROPS 0x0010 |
| 1052 | #define _BDF_GLYPHS 0x0020 |
| 1053 | #define _BDF_GLYPH 0x0040 |
| 1054 | #define _BDF_ENCODING 0x0080 |
| 1055 | #define _BDF_SWIDTH 0x0100 |
| 1056 | #define _BDF_DWIDTH 0x0200 |
| 1057 | #define _BDF_BBX 0x0400 |
| 1058 | #define _BDF_BITMAP 0x0800 |
| 1059 | |
| 1060 | #define _BDF_SWIDTH_ADJ 0x1000 |
| 1061 | |
| 1062 | #define _BDF_GLYPH_BITS ( _BDF_GLYPH | \ |
| 1063 | _BDF_ENCODING | \ |
| 1064 | _BDF_SWIDTH | \ |
| 1065 | _BDF_DWIDTH | \ |
| 1066 | _BDF_BBX | \ |
| 1067 | _BDF_BITMAP ) |
| 1068 | |
| 1069 | #define _BDF_GLYPH_WIDTH_CHECK 0x40000000L |
| 1070 | #define _BDF_GLYPH_HEIGHT_CHECK 0x80000000L |
| 1071 | |
| 1072 | |
| 1073 | /* Auto correction messages. */ |
| 1074 | #define ACMSG1 "FONT_ASCENT property missing. " \ |
| 1075 | "Added \"FONT_ASCENT %hd\".\n" |
| 1076 | #define ACMSG2 "FONT_DESCENT property missing. " \ |
| 1077 | "Added \"FONT_DESCENT %hd\".\n" |
| 1078 | #define ACMSG3 "Font width != actual width. Old: %hd New: %hd.\n" |
| 1079 | #define ACMSG4 "Font left bearing != actual left bearing. " \ |
| 1080 | "Old: %hd New: %hd.\n" |
| 1081 | #define ACMSG5 "Font ascent != actual ascent. Old: %hd New: %hd.\n" |
| 1082 | #define ACMSG6 "Font descent != actual descent. Old: %hd New: %hd.\n" |
| 1083 | #define ACMSG7 "Font height != actual height. Old: %hd New: %hd.\n" |
| 1084 | #define ACMSG8 "Glyph scalable width (SWIDTH) adjustments made.\n" |
| 1085 | #define ACMSG9 "SWIDTH field missing at line %ld. Set automatically.\n" |
| 1086 | #define ACMSG10 "DWIDTH field missing at line %ld. Set to glyph width.\n" |
| 1087 | #define ACMSG11 "SIZE bits per pixel field adjusted to %hd.\n" |
| 1088 | #define ACMSG12 "Duplicate encoding %ld (%s) changed to unencoded.\n" |
| 1089 | #define ACMSG13 "Glyph %ld extra rows removed.\n" |
| 1090 | #define ACMSG14 "Glyph %ld extra columns removed.\n" |
| 1091 | #define ACMSG15 "Incorrect glyph count: %ld indicated but %ld found.\n" |
| 1092 | |
| 1093 | /* Error messages. */ |
| 1094 | #define ERRMSG1 "[line %ld] Missing \"%s\" line.\n" |
| 1095 | #define ERRMSG2 "[line %ld] Font header corrupted or missing fields.\n" |
| 1096 | #define ERRMSG3 "[line %ld] Font glyphs corrupted or missing fields.\n" |
| 1097 | |
| 1098 | |
| 1099 | static FT_Error |
| 1100 | _bdf_add_comment( bdf_font_t* font, |
| 1101 | char* comment, |
| 1102 | unsigned long len ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1103 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1104 | char* cp; |
| 1105 | FT_Memory memory = font->memory; |
| 1106 | FT_Error error = BDF_Err_Ok; |
| 1107 | |
| 1108 | |
| 1109 | if ( font->comments_len == 0 ) |
| 1110 | { |
| 1111 | if ( FT_NEW_ARRAY( font->comments, len + 1 ) ) |
| 1112 | goto Exit; |
| 1113 | } |
| 1114 | else |
| 1115 | { |
| 1116 | if ( FT_RENEW_ARRAY( font->comments, |
| 1117 | font->comments_len, |
| 1118 | font->comments_len + len + 1 ) ) |
| 1119 | goto Exit; |
| 1120 | } |
| 1121 | |
| 1122 | cp = font->comments + font->comments_len; |
| 1123 | FT_MEM_COPY( cp, comment, len ); |
| 1124 | cp += len; |
| 1125 | *cp++ = '\n'; |
| 1126 | font->comments_len += len + 1; |
| 1127 | |
| 1128 | Exit: |
| 1129 | return error; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1132 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1133 | /* Set the spacing from the font name if it exists, or set it to the */ |
| 1134 | /* default specified in the options. */ |
| 1135 | static FT_Error |
| 1136 | _bdf_set_default_spacing( bdf_font_t* font, |
| 1137 | bdf_options_t* opts ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1138 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1139 | unsigned long len; |
| 1140 | char name[128]; |
| 1141 | _bdf_list_t list; |
| 1142 | FT_Memory memory; |
| 1143 | FT_Error error = BDF_Err_Ok; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1144 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1145 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1146 | if ( font == 0 || font->name == 0 || font->name[0] == 0 ) |
| 1147 | { |
| 1148 | error = BDF_Err_Invalid_Argument; |
| 1149 | goto Exit; |
| 1150 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1151 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1152 | memory = font->memory; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1153 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1154 | font->spacing = opts->font_spacing; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1155 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1156 | len = (unsigned long)( ft_strlen( font->name ) + 1 ); |
| 1157 | FT_MEM_COPY( name, font->name, len ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1158 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1159 | list.size = list.used = 0; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1160 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1161 | error = _bdf_split( (char *)"-", name, len, &list, memory ); |
| 1162 | if ( error ) |
| 1163 | goto Exit; |
| 1164 | |
| 1165 | if ( list.used == 15 ) |
| 1166 | { |
| 1167 | switch ( list.field[11][0] ) |
| 1168 | { |
| 1169 | case 'C': |
| 1170 | case 'c': |
| 1171 | font->spacing = BDF_CHARCELL; |
| 1172 | break; |
| 1173 | case 'M': |
| 1174 | case 'm': |
| 1175 | font->spacing = BDF_MONOWIDTH; |
| 1176 | break; |
| 1177 | case 'P': |
| 1178 | case 'p': |
| 1179 | font->spacing = BDF_PROPORTIONAL; |
| 1180 | break; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1181 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | FT_FREE( list.field ); |
| 1185 | |
| 1186 | Exit: |
| 1187 | return error; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1188 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1189 | |
| 1190 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1191 | /* Determine whether the property is an atom or not. If it is, then */ |
| 1192 | /* clean it up so the double quotes are removed if they exist. */ |
| 1193 | static int |
| 1194 | _bdf_is_atom( char* line, |
| 1195 | unsigned long linelen, |
| 1196 | char** name, |
| 1197 | char** value, |
| 1198 | bdf_font_t* font ) |
| 1199 | { |
| 1200 | int hold; |
| 1201 | char *sp, *ep; |
| 1202 | bdf_property_t* p; |
| 1203 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1204 | |
| 1205 | *name = sp = ep = line; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1206 | |
| 1207 | while ( *ep && *ep != ' ' && *ep != '\t' ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1208 | ep++; |
| 1209 | |
| 1210 | hold = -1; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1211 | if ( *ep ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1212 | { |
| 1213 | hold = *ep; |
| 1214 | *ep = 0; |
| 1215 | } |
| 1216 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1217 | p = bdf_get_property( sp, font ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1218 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1219 | /* Restore the character that was saved before any return can happen. */ |
| 1220 | if ( hold != -1 ) |
Werner Lemberg | 233302a | 2002-05-22 05:41:06 +0000 | [diff] [blame] | 1221 | *ep = (char)hold; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1222 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1223 | /* If the property exists and is not an atom, just return here. */ |
| 1224 | if ( p && p->format != BDF_ATOM ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1225 | return 0; |
| 1226 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1227 | /* The property is an atom. Trim all leading and trailing whitespace */ |
| 1228 | /* and double quotes for the atom value. */ |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1229 | sp = ep; |
| 1230 | ep = line + linelen; |
| 1231 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1232 | /* Trim the leading whitespace if it exists. */ |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1233 | *sp++ = 0; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1234 | while ( *sp && |
| 1235 | ( *sp == ' ' || *sp == '\t' ) ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1236 | sp++; |
| 1237 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1238 | /* Trim the leading double quote if it exists. */ |
| 1239 | if ( *sp == '"' ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1240 | sp++; |
| 1241 | *value = sp; |
| 1242 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1243 | /* Trim the trailing whitespace if it exists. */ |
| 1244 | while ( ep > sp && |
| 1245 | ( *( ep - 1 ) == ' ' || *( ep - 1 ) == '\t' ) ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1246 | *--ep = 0; |
| 1247 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1248 | /* Trim the trailing double quote if it exists. */ |
| 1249 | if ( ep > sp && *( ep - 1 ) == '"' ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1250 | *--ep = 0; |
| 1251 | |
| 1252 | return 1; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1253 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1254 | |
| 1255 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1256 | static FT_Error |
| 1257 | _bdf_add_property( bdf_font_t* font, |
| 1258 | char* name, |
| 1259 | char* value ) |
| 1260 | { |
| 1261 | unsigned long propid; |
| 1262 | hashnode hn; |
| 1263 | int len; |
| 1264 | bdf_property_t *prop, *fp; |
| 1265 | FT_Memory memory = font->memory; |
| 1266 | FT_Error error = BDF_Err_Ok; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1267 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1268 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1269 | /* First, check to see if the property already exists in the font. */ |
| 1270 | if ( ( hn = hash_lookup( name, (hashtable *)font->internal ) ) != 0 ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1271 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1272 | /* The property already exists in the font, so simply replace */ |
| 1273 | /* the value of the property with the current value. */ |
| 1274 | fp = font->props + (unsigned long)hn->data; |
| 1275 | |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 1276 | switch ( fp->format ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1277 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1278 | case BDF_ATOM: |
| 1279 | /* Delete the current atom if it exists. */ |
| 1280 | FT_FREE( fp->value.atom ); |
| 1281 | |
| 1282 | if ( value == 0 ) |
| 1283 | len = 1; |
| 1284 | else |
| 1285 | len = ft_strlen( value ) + 1; |
| 1286 | |
| 1287 | if ( len > 1 ) |
| 1288 | { |
| 1289 | if ( FT_NEW_ARRAY( fp->value.atom, len ) ) |
| 1290 | goto Exit; |
| 1291 | FT_MEM_COPY( fp->value.atom, value, len ); |
| 1292 | } |
| 1293 | else |
| 1294 | fp->value.atom = 0; |
| 1295 | break; |
| 1296 | |
| 1297 | case BDF_INTEGER: |
| 1298 | fp->value.int32 = _bdf_atol( value, 0, 10 ); |
| 1299 | break; |
| 1300 | |
| 1301 | case BDF_CARDINAL: |
| 1302 | fp->value.card32 = _bdf_atoul( value, 0, 10 ); |
| 1303 | break; |
David Turner | d490e37 | 2002-05-28 23:40:37 +0000 | [diff] [blame^] | 1304 | |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 1305 | default: |
| 1306 | ; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1307 | } |
David Turner | d490e37 | 2002-05-28 23:40:37 +0000 | [diff] [blame^] | 1308 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1309 | goto Exit; |
| 1310 | } |
| 1311 | |
| 1312 | /* See whether this property type exists yet or not. */ |
| 1313 | /* If not, create it. */ |
| 1314 | hn = hash_lookup( name, &(font->proptbl) ); |
| 1315 | if ( hn == 0 ) |
| 1316 | { |
| 1317 | error = bdf_create_property( name, BDF_ATOM, font ); |
| 1318 | if ( error ) |
| 1319 | goto Exit; |
| 1320 | hn = hash_lookup( name, &(font->proptbl) ); |
| 1321 | } |
| 1322 | |
| 1323 | /* Allocate another property if this is overflow. */ |
| 1324 | if ( font->props_used == font->props_size ) |
| 1325 | { |
| 1326 | if ( font->props_size == 0 ) |
| 1327 | { |
| 1328 | if ( FT_NEW_ARRAY( font->props, 1 ) ) |
| 1329 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1330 | } |
| 1331 | else |
| 1332 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1333 | if ( FT_RENEW_ARRAY( font->props, |
| 1334 | font->props_size, |
| 1335 | font->props_size + 1 ) ) |
| 1336 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1337 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1338 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1339 | fp = font->props + font->props_size; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1340 | FT_MEM_SET( fp, 0, sizeof ( bdf_property_t ) ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1341 | font->props_size++; |
| 1342 | } |
| 1343 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1344 | propid = (unsigned long)hn->data; |
| 1345 | if ( propid >= _num_bdf_properties ) |
| 1346 | prop = font->user_props + ( propid - _num_bdf_properties ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1347 | else |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 1348 | prop = (bdf_property_t*)_bdf_properties + propid; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1349 | |
| 1350 | fp = font->props + font->props_used; |
| 1351 | |
| 1352 | fp->name = prop->name; |
| 1353 | fp->format = prop->format; |
| 1354 | fp->builtin = prop->builtin; |
| 1355 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1356 | switch ( prop->format ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1357 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1358 | case BDF_ATOM: |
| 1359 | if ( value == 0 ) |
| 1360 | len = 1; |
| 1361 | else |
| 1362 | len = ft_strlen( value ) + 1; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1363 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1364 | if ( len > 1 ) |
| 1365 | { |
| 1366 | if ( FT_NEW_ARRAY( fp->value.atom, len ) ) |
| 1367 | goto Exit; |
| 1368 | FT_MEM_COPY( fp->value.atom, value, len ); |
| 1369 | } |
| 1370 | else |
| 1371 | fp->value.atom = 0; |
| 1372 | break; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1373 | |
| 1374 | case BDF_INTEGER: |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1375 | fp->value.int32 = _bdf_atol( value, 0, 10 ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1376 | break; |
| 1377 | |
| 1378 | case BDF_CARDINAL: |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1379 | fp->value.card32 = _bdf_atoul( value, 0, 10 ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1380 | break; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1383 | /* If the property happens to be a comment, then it doesn't need */ |
| 1384 | /* to be added to the internal hash table. */ |
| 1385 | if ( ft_memcmp( name, "COMMENT", 7 ) != 0 ) { |
| 1386 | /* Add the property to the font property table. */ |
| 1387 | error = hash_insert( fp->name, |
| 1388 | (void *)font->props_used, |
| 1389 | (hashtable *)font->internal, |
| 1390 | memory ); |
| 1391 | if ( error ) |
| 1392 | goto Exit; |
| 1393 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1394 | |
| 1395 | font->props_used++; |
| 1396 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1397 | /* Some special cases need to be handled here. The DEFAULT_CHAR */ |
| 1398 | /* property needs to be located if it exists in the property list, the */ |
| 1399 | /* FONT_ASCENT and FONT_DESCENT need to be assigned if they are */ |
| 1400 | /* present, and the SPACING property should override the default */ |
| 1401 | /* spacing. */ |
| 1402 | if ( ft_memcmp( name, "DEFAULT_CHAR", 12 ) == 0 ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1403 | font->default_glyph = fp->value.int32; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1404 | else if ( ft_memcmp( name, "FONT_ASCENT", 11 ) == 0 ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1405 | font->font_ascent = fp->value.int32; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1406 | else if ( ft_memcmp( name, "FONT_DESCENT", 12 ) == 0 ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1407 | font->font_descent = fp->value.int32; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1408 | else if ( ft_memcmp( name, "SPACING", 7 ) == 0 ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1409 | { |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1410 | if ( fp->value.atom[0] == 'p' || fp->value.atom[0] == 'P' ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1411 | font->spacing = BDF_PROPORTIONAL; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1412 | else if ( fp->value.atom[0] == 'm' || fp->value.atom[0] == 'M' ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1413 | font->spacing = BDF_MONOWIDTH; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1414 | else if ( fp->value.atom[0] == 'c' || fp->value.atom[0] == 'C' ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1415 | font->spacing = BDF_CHARCELL; |
| 1416 | } |
| 1417 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1418 | Exit: |
| 1419 | return error; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1422 | |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 1423 | static const unsigned char nibble_mask[8] = |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1424 | { |
| 1425 | 0xFF, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE |
| 1426 | }; |
| 1427 | |
| 1428 | |
| 1429 | /* Actually parse the glyph info and bitmaps. */ |
| 1430 | static FT_Error |
| 1431 | _bdf_parse_glyphs( char* line, |
| 1432 | unsigned long linelen, |
| 1433 | unsigned long lineno, |
| 1434 | void* call_data, |
| 1435 | void* client_data ) |
| 1436 | { |
| 1437 | int c, mask_index; |
| 1438 | char* s; |
| 1439 | unsigned char* bp; |
| 1440 | unsigned long i, slen, nibbles; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1441 | |
| 1442 | _bdf_line_func_t* next; |
| 1443 | _bdf_parse_t* p; |
| 1444 | bdf_glyph_t* glyph; |
| 1445 | bdf_font_t* font; |
| 1446 | |
| 1447 | FT_Memory memory; |
| 1448 | FT_Error error = BDF_Err_Ok; |
| 1449 | |
| 1450 | FT_UNUSED( lineno ); /* only used in debug mode */ |
| 1451 | |
| 1452 | |
| 1453 | next = (_bdf_line_func_t *)call_data; |
| 1454 | p = (_bdf_parse_t *) client_data; |
| 1455 | |
| 1456 | font = p->font; |
| 1457 | memory = font->memory; |
| 1458 | |
| 1459 | /* Check for a comment. */ |
| 1460 | if ( ft_memcmp( line, "COMMENT", 7 ) == 0 ) |
| 1461 | { |
| 1462 | linelen -= 7; |
| 1463 | |
| 1464 | s = line + 7; |
| 1465 | if ( *s != 0 ) |
| 1466 | { |
| 1467 | s++; |
| 1468 | linelen--; |
| 1469 | } |
| 1470 | error = _bdf_add_comment( p->font, s, linelen ); |
| 1471 | goto Exit; |
| 1472 | } |
| 1473 | |
| 1474 | /* The very first thing expected is the number of glyphs. */ |
| 1475 | if ( !( p->flags & _BDF_GLYPHS ) ) |
| 1476 | { |
| 1477 | if ( ft_memcmp( line, "CHARS", 5 ) != 0 ) |
| 1478 | { |
| 1479 | FT_ERROR(( "_bdf_parse_glyphs: " ERRMSG1, lineno, "CHARS" )); |
| 1480 | error = BDF_Err_Missing_Chars_Field; |
| 1481 | goto Exit; |
| 1482 | } |
| 1483 | |
| 1484 | error = _bdf_split( (char *)" +", line, linelen, &p->list, memory ); |
| 1485 | if ( error ) |
| 1486 | goto Exit; |
| 1487 | p->cnt = font->glyphs_size = _bdf_atoul( p->list.field[1], 0, 10 ); |
| 1488 | |
| 1489 | /* Make sure the number of glyphs is non-zero. */ |
| 1490 | if ( p->cnt == 0 ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1491 | font->glyphs_size = 64; |
| 1492 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1493 | if ( FT_NEW_ARRAY( font->glyphs, font->glyphs_size ) ) |
| 1494 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1495 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1496 | p->flags |= _BDF_GLYPHS; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1497 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1498 | goto Exit; |
| 1499 | } |
| 1500 | |
| 1501 | /* Check for the ENDFONT field. */ |
| 1502 | if ( ft_memcmp( line, "ENDFONT", 7 ) == 0 ) |
| 1503 | { |
| 1504 | /* Sort the glyphs by encoding. */ |
| 1505 | ft_qsort( (char *)font->glyphs, |
| 1506 | font->glyphs_used, |
| 1507 | sizeof ( bdf_glyph_t ), |
| 1508 | by_encoding ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1509 | |
| 1510 | p->flags &= ~_BDF_START; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1511 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1512 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1515 | /* Check for the ENDCHAR field. */ |
| 1516 | if ( ft_memcmp( line, "ENDCHAR", 7 ) == 0 ) |
| 1517 | { |
| 1518 | p->glyph_enc = 0; |
| 1519 | p->flags &= ~_BDF_GLYPH_BITS; |
| 1520 | |
| 1521 | goto Exit; |
| 1522 | } |
| 1523 | |
| 1524 | /* Check to see whether a glyph is being scanned but should be */ |
| 1525 | /* ignored because it is an unencoded glyph. */ |
| 1526 | if ( ( p->flags & _BDF_GLYPH ) && |
| 1527 | p->glyph_enc == -1 && |
| 1528 | p->opts->keep_unencoded == 0 ) |
| 1529 | goto Exit; |
| 1530 | |
| 1531 | /* Check for the STARTCHAR field. */ |
| 1532 | if ( ft_memcmp( line, "STARTCHAR", 9 ) == 0 ) |
| 1533 | { |
| 1534 | /* Set the character name in the parse info first until the */ |
| 1535 | /* encoding can be checked for an unencoded character. */ |
| 1536 | FT_FREE( p->glyph_name ); |
| 1537 | |
| 1538 | error = _bdf_split( (char *)" +", line, linelen, &p->list,memory ); |
| 1539 | if ( error ) |
| 1540 | goto Exit; |
| 1541 | _bdf_shift( 1, &p->list ); |
| 1542 | |
| 1543 | s = _bdf_join( ' ', &slen, &p->list ); |
| 1544 | |
| 1545 | if ( FT_NEW_ARRAY( p->glyph_name, slen + 1 ) ) |
| 1546 | goto Exit; |
| 1547 | FT_MEM_COPY( p->glyph_name, s, slen + 1 ); |
| 1548 | |
| 1549 | p->flags |= _BDF_GLYPH; |
| 1550 | |
| 1551 | goto Exit; |
| 1552 | } |
| 1553 | |
| 1554 | /* Check for the ENCODING field. */ |
| 1555 | if ( ft_memcmp( line, "ENCODING", 8 ) == 0 ) |
| 1556 | { |
| 1557 | if ( !( p->flags & _BDF_GLYPH ) ) |
| 1558 | { |
| 1559 | /* Missing STARTCHAR field. */ |
| 1560 | FT_ERROR(( "_bdf_parse_glyphs: " ERRMSG1, lineno, "STARTCHAR" )); |
| 1561 | error = BDF_Err_Missing_Startchar_Field; |
| 1562 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1565 | error = _bdf_split( (char *)" +", line, linelen, &p->list, memory ); |
| 1566 | if ( error ) |
| 1567 | goto Exit; |
| 1568 | p->glyph_enc = _bdf_atol( p->list.field[1], 0, 10 ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1569 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1570 | /* Check to see whether this encoding has already been encountered. */ |
| 1571 | /* If it has then change it to unencoded so it gets added if */ |
| 1572 | /* indicated. */ |
| 1573 | if ( p->glyph_enc >= 0 ) |
| 1574 | { |
| 1575 | if ( _bdf_glyph_modified( p->have, p->glyph_enc ) ) |
| 1576 | { |
| 1577 | /* Emit a message saying a glyph has been moved to the */ |
| 1578 | /* unencoded area. */ |
| 1579 | FT_TRACE2(( "_bdf_parse_glyphs: " ACMSG12, |
| 1580 | p->glyph_enc, p->glyph_name )); |
| 1581 | p->glyph_enc = -1; |
| 1582 | font->modified = 1; |
| 1583 | } |
| 1584 | else |
| 1585 | _bdf_set_glyph_modified( p->have, p->glyph_enc ); |
| 1586 | } |
| 1587 | |
| 1588 | if ( p->glyph_enc >= 0 ) |
| 1589 | { |
| 1590 | /* Make sure there are enough glyphs allocated in case the */ |
| 1591 | /* number of characters happen to be wrong. */ |
| 1592 | if ( font->glyphs_used == font->glyphs_size ) |
| 1593 | { |
| 1594 | if ( FT_RENEW_ARRAY( font->glyphs, |
| 1595 | font->glyphs_size, |
| 1596 | font->glyphs_size + 64 ) ) |
| 1597 | goto Exit; |
| 1598 | FT_MEM_SET( font->glyphs + font->glyphs_size, |
| 1599 | 0, |
| 1600 | sizeof ( bdf_glyph_t ) * 64 ); /* FZ inutile */ |
| 1601 | font->glyphs_size += 64; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1604 | glyph = font->glyphs + font->glyphs_used++; |
| 1605 | glyph->name = p->glyph_name; |
| 1606 | glyph->encoding = p->glyph_enc; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1607 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1608 | /* Reset the initial glyph info. */ |
| 1609 | p->glyph_name = 0; |
| 1610 | } |
| 1611 | else |
| 1612 | { |
| 1613 | /* Unencoded glyph. Check to see whether it should */ |
| 1614 | /* be added or not. */ |
| 1615 | if ( p->opts->keep_unencoded != 0 ) |
| 1616 | { |
| 1617 | /* Allocate the next unencoded glyph. */ |
| 1618 | if ( font->unencoded_used == font->unencoded_size ) |
| 1619 | { |
| 1620 | if ( font->unencoded_size == 0 ) |
| 1621 | { |
| 1622 | if ( FT_NEW_ARRAY( font->unencoded, 2 ) ) |
| 1623 | goto Exit; |
| 1624 | } |
| 1625 | else |
| 1626 | { |
| 1627 | if ( FT_RENEW_ARRAY( font->unencoded , |
| 1628 | font->unencoded_size, |
| 1629 | font->unencoded_size + 4 ) ) |
| 1630 | goto Exit; |
| 1631 | } |
| 1632 | font->unencoded_size += 4; |
| 1633 | } |
| 1634 | |
| 1635 | glyph = font->unencoded + font->unencoded_used; |
| 1636 | glyph->name = p->glyph_name; |
| 1637 | glyph->encoding = font->unencoded_used++; |
| 1638 | } |
| 1639 | else |
| 1640 | /* Free up the glyph name if the unencoded shouldn't be */ |
| 1641 | /* kept. */ |
| 1642 | FT_FREE( p->glyph_name ); |
| 1643 | |
| 1644 | p->glyph_name = 0; |
| 1645 | } |
| 1646 | |
| 1647 | /* Clear the flags that might be added when width and height are */ |
| 1648 | /* checked for consistency. */ |
| 1649 | p->flags &= ~( _BDF_GLYPH_WIDTH_CHECK | _BDF_GLYPH_HEIGHT_CHECK ); |
| 1650 | |
| 1651 | p->flags |= _BDF_ENCODING; |
| 1652 | |
| 1653 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1656 | /* Point at the glyph being constructed. */ |
| 1657 | if ( p->glyph_enc == -1 ) |
| 1658 | glyph = font->unencoded + ( font->unencoded_used - 1 ); |
| 1659 | else |
| 1660 | glyph = font->glyphs + ( font->glyphs_used - 1 ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1661 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1662 | /* Check to see whether a bitmap is being constructed. */ |
| 1663 | if ( p->flags & _BDF_BITMAP ) |
| 1664 | { |
| 1665 | /* If there are more rows than are specified in the glyph metrics, */ |
| 1666 | /* ignore the remaining lines. */ |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 1667 | if ( p->row >= (unsigned long)glyph->bbx.height ) |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1668 | { |
| 1669 | if ( !( p->flags & _BDF_GLYPH_HEIGHT_CHECK ) ) |
| 1670 | { |
| 1671 | FT_TRACE2(( "_bdf_parse_glyphs: " ACMSG13, glyph->encoding )); |
| 1672 | p->flags |= _BDF_GLYPH_HEIGHT_CHECK; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1673 | font->modified = 1; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | goto Exit; |
| 1677 | } |
| 1678 | |
| 1679 | /* Only collect the number of nibbles indicated by the glyph */ |
| 1680 | /* metrics. If there are more columns, they are simply ignored. */ |
| 1681 | nibbles = glyph->bpr << 1; |
| 1682 | bp = glyph->bitmap + p->row * glyph->bpr; |
| 1683 | |
| 1684 | for ( i = 0, *bp = 0; i < nibbles; i++ ) |
| 1685 | { |
| 1686 | c = line[i]; |
Werner Lemberg | 233302a | 2002-05-22 05:41:06 +0000 | [diff] [blame] | 1687 | *bp = (FT_Byte)( ( *bp << 4 ) + a2i[c] ); |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1688 | if ( i + 1 < nibbles && ( i & 1 ) ) |
| 1689 | *++bp = 0; |
| 1690 | } |
| 1691 | |
| 1692 | /* Remove possible garbage at the right. */ |
| 1693 | mask_index = ( glyph->bbx.width * p->font->bpp ) & 7; |
| 1694 | *bp &= nibble_mask[mask_index]; |
| 1695 | |
| 1696 | /* If any line has extra columns, indicate they have been removed. */ |
| 1697 | if ( ( line[nibbles] == '0' || a2i[(int)line[nibbles]] != 0 ) && |
| 1698 | !( p->flags & _BDF_GLYPH_WIDTH_CHECK ) ) |
| 1699 | { |
| 1700 | FT_TRACE2(( "_bdf_parse_glyphs: " ACMSG14, glyph->encoding )); |
| 1701 | p->flags |= _BDF_GLYPH_WIDTH_CHECK; |
| 1702 | font->modified = 1; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | p->row++; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1706 | goto Exit; |
| 1707 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1708 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1709 | /* Expect the SWIDTH (scalable width) field next. */ |
| 1710 | if ( ft_memcmp( line, "SWIDTH", 6 ) == 0 ) |
| 1711 | { |
| 1712 | if ( !( p->flags & _BDF_ENCODING ) ) |
| 1713 | { |
| 1714 | /* Missing ENCODING field. */ |
| 1715 | FT_ERROR(( "_bdf_parse_glyphs: " ERRMSG1, lineno, "ENCODING" )); |
| 1716 | error = BDF_Err_Missing_Encoding_Field; |
| 1717 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1718 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1719 | |
| 1720 | error = _bdf_split( (char *)" +", line, linelen, &p->list, memory ); |
| 1721 | if ( error ) |
| 1722 | goto Exit; |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 1723 | glyph->swidth = (unsigned short)_bdf_atoul( p->list.field[1], 0, 10 ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1724 | p->flags |= _BDF_SWIDTH; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1725 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1726 | goto Exit; |
| 1727 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1728 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1729 | /* Expect the DWIDTH (scalable width) field next. */ |
| 1730 | if ( ft_memcmp( line, "DWIDTH", 6 ) == 0 ) |
| 1731 | { |
| 1732 | error = _bdf_split( (char *)" +", line, linelen, &p->list,memory ); |
| 1733 | if ( error ) |
| 1734 | goto Exit; |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 1735 | glyph->dwidth = (unsigned short)_bdf_atoul( p->list.field[1], 0, 10 ); |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1736 | |
| 1737 | if ( !( p->flags & _BDF_SWIDTH ) ) |
| 1738 | { |
| 1739 | /* Missing SWIDTH field. Emit an auto correction message and set */ |
| 1740 | /* the scalable width from the device width. */ |
| 1741 | FT_TRACE2(( "_bdf_parse_glyphs: " ACMSG9, lineno )); |
| 1742 | |
David Turner | d490e37 | 2002-05-28 23:40:37 +0000 | [diff] [blame^] | 1743 | ps = (double)font->point_size; |
| 1744 | rx = (double)font->resolution_x; |
| 1745 | dw = (double)glyph->dwidth; |
| 1746 | |
Werner Lemberg | 02d4d59 | 2002-05-28 22:38:05 +0000 | [diff] [blame] | 1747 | glyph->swidth = (unsigned short)FT_MulDiv( |
| 1748 | glyph->dwidth, 72000L, |
| 1749 | (FT_Long)( font->point_size * |
| 1750 | font->resolution_x ) ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | p->flags |= _BDF_DWIDTH; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1754 | goto Exit; |
| 1755 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1756 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1757 | /* Expect the BBX field next. */ |
| 1758 | if ( ft_memcmp( line, "BBX", 3 ) == 0 ) |
| 1759 | { |
| 1760 | error = _bdf_split( (char *)" +", line, linelen, &p->list, memory ); |
| 1761 | if ( error ) |
| 1762 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1763 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1764 | glyph->bbx.width = _bdf_atos( p->list.field[1], 0, 10 ); |
| 1765 | glyph->bbx.height = _bdf_atos( p->list.field[2], 0, 10 ); |
| 1766 | glyph->bbx.x_offset = _bdf_atos( p->list.field[3], 0, 10 ); |
| 1767 | glyph->bbx.y_offset = _bdf_atos( p->list.field[4], 0, 10 ); |
| 1768 | |
| 1769 | /* Generate the ascent and descent of the character. */ |
Werner Lemberg | 233302a | 2002-05-22 05:41:06 +0000 | [diff] [blame] | 1770 | glyph->bbx.ascent = (short)( glyph->bbx.height + glyph->bbx.y_offset ); |
| 1771 | glyph->bbx.descent = (short)( -glyph->bbx.y_offset ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1772 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1773 | /* Determine the overall font bounding box as the characters are */ |
| 1774 | /* loaded so corrections can be done later if indicated. */ |
| 1775 | p->maxas = MAX( glyph->bbx.ascent, p->maxas ); |
| 1776 | p->maxds = MAX( glyph->bbx.descent, p->maxds ); |
| 1777 | |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 1778 | p->rbearing = (short)( glyph->bbx.width + glyph->bbx.x_offset ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1779 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1780 | p->maxrb = MAX( p->rbearing, p->maxrb ); |
| 1781 | p->minlb = MIN( glyph->bbx.x_offset, p->minlb ); |
| 1782 | p->maxlb = MAX( glyph->bbx.x_offset, p->maxlb ); |
| 1783 | |
| 1784 | if ( !( p->flags & _BDF_DWIDTH ) ) |
| 1785 | { |
| 1786 | /* Missing DWIDTH field. Emit an auto correction message and set */ |
| 1787 | /* the device width to the glyph width. */ |
| 1788 | FT_TRACE2(( "_bdf_parse_glyphs: " ACMSG10, lineno )); |
| 1789 | glyph->dwidth = glyph->bbx.width; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1792 | /* If the BDF_CORRECT_METRICS flag is set, then adjust the SWIDTH */ |
| 1793 | /* value if necessary. */ |
| 1794 | if ( p->opts->correct_metrics != 0 ) |
| 1795 | { |
| 1796 | /* Determine the point size of the glyph. */ |
Werner Lemberg | 02d4d59 | 2002-05-28 22:38:05 +0000 | [diff] [blame] | 1797 | unsigned short sw = (unsigned short)FT_MulDiv( |
| 1798 | glyph->dwidth, 72000L, |
| 1799 | (FT_Long)( font->point_size * |
| 1800 | font->resolution_x ) ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1801 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1802 | |
| 1803 | if ( sw != glyph->swidth ) |
| 1804 | { |
| 1805 | glyph->swidth = sw; |
| 1806 | |
| 1807 | if ( p->glyph_enc == -1 ) |
| 1808 | _bdf_set_glyph_modified( font->umod, |
| 1809 | font->unencoded_used - 1 ); |
| 1810 | else |
| 1811 | _bdf_set_glyph_modified( font->nmod, glyph->encoding ); |
| 1812 | |
| 1813 | p->flags |= _BDF_SWIDTH_ADJ; |
| 1814 | font->modified = 1; |
| 1815 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1816 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1817 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1818 | p->flags |= _BDF_BBX; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1819 | goto Exit; |
| 1820 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1821 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1822 | /* And finally, gather up the bitmap. */ |
| 1823 | if ( ft_memcmp( line, "BITMAP", 6 ) == 0 ) |
| 1824 | { |
| 1825 | if ( !( p->flags & _BDF_BBX ) ) |
| 1826 | { |
| 1827 | /* Missing BBX field. */ |
| 1828 | FT_ERROR(( "_bdf_parse_glyphs: " ERRMSG1, lineno, "BBX" )); |
| 1829 | error = BDF_Err_Missing_Bbx_Field; |
| 1830 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1831 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1832 | |
| 1833 | /* Allocate enough space for the bitmap. */ |
| 1834 | glyph->bpr = ( glyph->bbx.width * p->font->bpp + 7 ) >> 3; |
Werner Lemberg | 233302a | 2002-05-22 05:41:06 +0000 | [diff] [blame] | 1835 | glyph->bytes = (unsigned short)( glyph->bpr * glyph->bbx.height ); |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1836 | |
| 1837 | if ( FT_NEW_ARRAY( glyph->bitmap, glyph->bytes ) ) |
| 1838 | goto Exit; |
| 1839 | |
| 1840 | p->row = 0; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1841 | p->flags |= _BDF_BITMAP; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1842 | |
| 1843 | goto Exit; |
| 1844 | } |
| 1845 | |
| 1846 | error = BDF_Err_Invalid_File_Format; |
| 1847 | |
| 1848 | Exit: |
| 1849 | return error; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1852 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1853 | /* Load the font properties. */ |
| 1854 | static FT_Error |
| 1855 | _bdf_parse_properties( char* line, |
| 1856 | unsigned long linelen, |
| 1857 | unsigned long lineno, |
| 1858 | void* call_data, |
| 1859 | void* client_data ) |
| 1860 | { |
| 1861 | unsigned long vlen; |
| 1862 | _bdf_line_func_t* next; |
| 1863 | _bdf_parse_t* p; |
| 1864 | char* name; |
| 1865 | char* value; |
| 1866 | char nbuf[128]; |
| 1867 | FT_Memory memory; |
| 1868 | FT_Error error = BDF_Err_Ok; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1869 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1870 | FT_UNUSED( lineno ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1871 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1872 | |
| 1873 | next = (_bdf_line_func_t *)call_data; |
| 1874 | p = (_bdf_parse_t *) client_data; |
| 1875 | |
| 1876 | memory = p->font->memory; |
| 1877 | |
| 1878 | /* Check for the end of the properties. */ |
| 1879 | if ( ft_memcmp( line, "ENDPROPERTIES", 13 ) == 0 ) |
| 1880 | { |
| 1881 | /* If the FONT_ASCENT or FONT_DESCENT properties have not been */ |
| 1882 | /* encountered yet, then make sure they are added as properties and */ |
| 1883 | /* make sure they are set from the font bounding box info. */ |
| 1884 | /* */ |
| 1885 | /* This is *always* done regardless of the options, because X11 */ |
| 1886 | /* requires these two fields to compile fonts. */ |
| 1887 | if ( bdf_get_font_property( p->font, (char *)"FONT_ASCENT" ) == 0 ) |
| 1888 | { |
| 1889 | p->font->font_ascent = p->font->bbx.ascent; |
| 1890 | ft_sprintf( nbuf, "%hd", p->font->bbx.ascent ); |
| 1891 | error = _bdf_add_property( p->font, (char *)"FONT_ASCENT", nbuf ); |
| 1892 | if ( error ) |
| 1893 | goto Exit; |
| 1894 | |
| 1895 | FT_TRACE2(( "_bdf_parse_properties: " ACMSG1, p->font->bbx.ascent )); |
| 1896 | p->font->modified = 1; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1897 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1898 | |
| 1899 | if ( bdf_get_font_property( p->font, (char *)"FONT_DESCENT" ) == 0 ) |
| 1900 | { |
| 1901 | p->font->font_descent = p->font->bbx.descent; |
| 1902 | ft_sprintf( nbuf, "%hd", p->font->bbx.descent ); |
| 1903 | error = _bdf_add_property( p->font, (char *)"FONT_DESCENT", nbuf ); |
| 1904 | if ( error ) |
| 1905 | goto Exit; |
| 1906 | |
| 1907 | FT_TRACE2(( "_bdf_parse_properties: " ACMSG2, p->font->bbx.descent )); |
| 1908 | p->font->modified = 1; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1909 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1910 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1911 | p->flags &= ~_BDF_PROPS; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1912 | *next = _bdf_parse_glyphs; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1913 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1914 | goto Exit; |
| 1915 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1916 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1917 | /* Ignore the _XFREE86_GLYPH_RANGES properties. */ |
| 1918 | if ( ft_memcmp( line, "_XFREE86_GLYPH_RANGES", 21 ) == 0 ) |
| 1919 | goto Exit; |
| 1920 | |
| 1921 | /* Handle COMMENT fields and properties in a special way to preserve */ |
| 1922 | /* the spacing. */ |
| 1923 | if ( ft_memcmp( line, "COMMENT", 7 ) == 0 ) |
| 1924 | { |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1925 | name = value = line; |
| 1926 | value += 7; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1927 | if ( *value ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1928 | *value++ = 0; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1929 | error = _bdf_add_property( p->font, name, value ); |
| 1930 | if ( error ) |
| 1931 | goto Exit; |
| 1932 | } |
| 1933 | else if ( _bdf_is_atom( line, linelen, &name, &value, p->font ) ) |
| 1934 | { |
| 1935 | error = _bdf_add_property( p->font, name, value ); |
| 1936 | if ( error ) |
| 1937 | goto Exit; |
| 1938 | } |
| 1939 | else |
| 1940 | { |
| 1941 | error = _bdf_split( (char *)" +", line, linelen, &p->list, memory ); |
| 1942 | if ( error ) |
| 1943 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1944 | name = p->list.field[0]; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1945 | |
| 1946 | _bdf_shift( 1, &p->list ); |
| 1947 | value = _bdf_join( ' ', &vlen, &p->list ); |
| 1948 | |
| 1949 | error = _bdf_add_property( p->font, name, value ); |
| 1950 | if ( error ) |
| 1951 | goto Exit; |
| 1952 | } |
| 1953 | |
| 1954 | Exit: |
| 1955 | return error; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1958 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1959 | /* Load the font header. */ |
| 1960 | static FT_Error |
| 1961 | _bdf_parse_start( char* line, |
| 1962 | unsigned long linelen, |
| 1963 | unsigned long lineno, |
| 1964 | void* call_data, |
| 1965 | void* client_data ) |
| 1966 | { |
| 1967 | unsigned long slen; |
| 1968 | _bdf_line_func_t* next; |
| 1969 | _bdf_parse_t* p; |
| 1970 | bdf_font_t* font; |
| 1971 | char *s; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1972 | |
David Turner | d490e37 | 2002-05-28 23:40:37 +0000 | [diff] [blame^] | 1973 | FT_Memory memory = NULL; |
| 1974 | FT_Error error = BDF_Err_Ok; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1975 | |
| 1976 | FT_UNUSED( lineno ); /* only used in debug mode */ |
| 1977 | |
| 1978 | |
| 1979 | next = (_bdf_line_func_t *)call_data; |
| 1980 | p = (_bdf_parse_t *) client_data; |
| 1981 | |
| 1982 | if ( p->font ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1983 | memory = p->font->memory; |
| 1984 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1985 | /* Check for a comment. This is done to handle those fonts that have */ |
| 1986 | /* comments before the STARTFONT line for some reason. */ |
| 1987 | if ( ft_memcmp( line, "COMMENT", 7 ) == 0 ) |
| 1988 | { |
| 1989 | if ( p->opts->keep_comments != 0 && p->font != 0 ) |
| 1990 | { |
| 1991 | linelen -= 7; |
| 1992 | |
| 1993 | s = line + 7; |
| 1994 | if ( *s != 0 ) |
| 1995 | { |
| 1996 | s++; |
| 1997 | linelen--; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 1998 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 1999 | |
| 2000 | error = _bdf_add_comment( p->font, s, linelen ); |
| 2001 | if ( error ) |
| 2002 | goto Exit; |
| 2003 | /* here font is not defined! */ |
| 2004 | } |
| 2005 | |
| 2006 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2009 | if ( !( p->flags & _BDF_START ) ) |
| 2010 | { |
| 2011 | memory = p->memory; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2012 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2013 | if ( ft_memcmp( line, "STARTFONT", 9 ) != 0 ) |
| 2014 | { |
| 2015 | /* No STARTFONT field is a good indication of a problem. */ |
| 2016 | error = BDF_Err_Missing_Startfont_Field; |
| 2017 | goto Exit; |
| 2018 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2019 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2020 | p->flags = _BDF_START; |
| 2021 | font = p->font = 0; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2022 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2023 | if ( FT_NEW( font ) ) |
| 2024 | goto Exit; |
| 2025 | p->font = font; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2026 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2027 | font->memory = p->memory; |
| 2028 | p->memory = 0; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2029 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2030 | { /* setup */ |
| 2031 | unsigned long i; |
| 2032 | bdf_property_t* prop; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2033 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2034 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2035 | error = hash_init( &(font->proptbl), memory ); |
| 2036 | if ( error ) |
| 2037 | goto Exit; |
Werner Lemberg | 233302a | 2002-05-22 05:41:06 +0000 | [diff] [blame] | 2038 | for ( i = 0, prop = (bdf_property_t*)_bdf_properties; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2039 | i < _num_bdf_properties; i++, prop++ ) |
| 2040 | { |
| 2041 | error = hash_insert( prop->name, (void *)i, |
| 2042 | &(font->proptbl), memory ); |
| 2043 | if ( error ) |
| 2044 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2045 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | if ( FT_ALLOC( p->font->internal, sizeof ( hashtable ) ) ) |
| 2049 | goto Exit; |
| 2050 | error = hash_init( (hashtable *)p->font->internal,memory ); |
| 2051 | if ( error ) |
| 2052 | goto Exit; |
| 2053 | p->font->spacing = p->opts->font_spacing; |
| 2054 | p->font->default_glyph = -1; |
| 2055 | |
| 2056 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2059 | /* Check for the start of the properties. */ |
| 2060 | if ( ft_memcmp( line, "STARTPROPERTIES", 15 ) == 0 ) |
| 2061 | { |
| 2062 | error = _bdf_split( (char *)" +", line, linelen, &p->list, memory ); |
| 2063 | if ( error ) |
| 2064 | goto Exit; |
| 2065 | p->cnt = p->font->props_size = _bdf_atoul( p->list.field[1], 0, 10 ); |
| 2066 | |
| 2067 | if ( FT_NEW_ARRAY( p->font->props, p->cnt ) ) |
| 2068 | goto Exit; |
| 2069 | |
| 2070 | p->flags |= _BDF_PROPS; |
| 2071 | *next = _bdf_parse_properties; |
| 2072 | |
| 2073 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2076 | /* Check for the FONTBOUNDINGBOX field. */ |
| 2077 | if ( ft_memcmp( line, "FONTBOUNDINGBOX", 15 ) == 0 ) |
| 2078 | { |
| 2079 | if ( !(p->flags & _BDF_SIZE ) ) |
| 2080 | { |
| 2081 | /* Missing the SIZE field. */ |
| 2082 | FT_ERROR(( "_bdf_parse_start: " ERRMSG1, lineno, "SIZE" )); |
| 2083 | error = BDF_Err_Missing_Size_Field; |
| 2084 | goto Exit; |
| 2085 | } |
| 2086 | |
| 2087 | error = _bdf_split( (char *)" +", line, linelen, &p->list , memory ); |
| 2088 | if ( error ) |
| 2089 | goto Exit; |
| 2090 | |
| 2091 | p->font->bbx.width = _bdf_atos( p->list.field[1], 0, 10 ); |
| 2092 | p->font->bbx.height = _bdf_atos( p->list.field[2], 0, 10 ); |
| 2093 | |
| 2094 | p->font->bbx.x_offset = _bdf_atos( p->list.field[3], 0, 10 ); |
| 2095 | p->font->bbx.y_offset = _bdf_atos( p->list.field[4], 0, 10 ); |
| 2096 | |
David Turner | d490e37 | 2002-05-28 23:40:37 +0000 | [diff] [blame^] | 2097 | p->font->bbx.ascent = (short)( p->font->bbx.height + |
David Turner | b1b4762 | 2002-05-21 21:17:43 +0000 | [diff] [blame] | 2098 | p->font->bbx.y_offset ); |
| 2099 | |
| 2100 | p->font->bbx.descent = (short)( -p->font->bbx.y_offset ); |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2101 | |
| 2102 | p->flags |= _BDF_FONT_BBX; |
| 2103 | |
| 2104 | goto Exit; |
| 2105 | } |
| 2106 | |
| 2107 | /* The next thing to check for is the FONT field. */ |
| 2108 | if ( ft_memcmp( line, "FONT", 4 ) == 0 ) |
| 2109 | { |
| 2110 | error = _bdf_split( (char *)" +", line, linelen, &p->list , memory ); |
| 2111 | if ( error ) |
| 2112 | goto Exit; |
| 2113 | _bdf_shift( 1, &p->list ); |
| 2114 | |
| 2115 | s = _bdf_join( ' ', &slen, &p->list ); |
| 2116 | if ( FT_NEW_ARRAY( p->font->name, slen + 1 ) ) |
| 2117 | goto Exit; |
| 2118 | FT_MEM_COPY( p->font->name, s, slen + 1 ); |
| 2119 | |
| 2120 | /* If the font name is an XLFD name, set the spacing to the one in */ |
| 2121 | /* the font name. If there is no spacing fall back on the default. */ |
| 2122 | error = _bdf_set_default_spacing( p->font, p->opts ); |
| 2123 | if ( error ) |
| 2124 | goto Exit; |
| 2125 | |
| 2126 | p->flags |= _BDF_FONT_NAME; |
| 2127 | |
| 2128 | goto Exit; |
| 2129 | } |
| 2130 | |
| 2131 | /* Check for the SIZE field. */ |
| 2132 | if ( ft_memcmp( line, "SIZE", 4 ) == 0 ) |
| 2133 | { |
| 2134 | if ( !( p->flags & _BDF_FONT_NAME ) ) |
| 2135 | { |
| 2136 | /* Missing the FONT field. */ |
| 2137 | FT_ERROR(( "_bdf_parse_start: " ERRMSG1, lineno, "FONT" )); |
| 2138 | error = BDF_Err_Missing_Font_Field; |
| 2139 | goto Exit; |
| 2140 | } |
| 2141 | |
| 2142 | error = _bdf_split( (char *)" +", line, linelen, &p->list, memory ); |
| 2143 | if ( error ) |
| 2144 | goto Exit; |
| 2145 | |
| 2146 | p->font->point_size = _bdf_atoul( p->list.field[1], 0, 10 ); |
| 2147 | p->font->resolution_x = _bdf_atoul( p->list.field[2], 0, 10 ); |
| 2148 | p->font->resolution_y = _bdf_atoul( p->list.field[3], 0, 10 ); |
| 2149 | |
| 2150 | /* Check for the bits per pixel field. */ |
| 2151 | if ( p->list.used == 5 ) |
| 2152 | { |
| 2153 | p->font->bpp = _bdf_atos( p->list.field[4], 0, 10 ); |
| 2154 | if ( p->font->bpp > 1 && ( p->font->bpp & 1 ) ) |
| 2155 | { |
| 2156 | /* Move up to the next bits per pixel value if an odd number */ |
| 2157 | /* is encountered. */ |
| 2158 | p->font->bpp++; |
| 2159 | if ( p->font->bpp <= 4 ) |
| 2160 | FT_TRACE2(( "_bdf_parse_start: " ACMSG11, p->font->bpp )); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2161 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2162 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2163 | if ( p->font->bpp > 4 ) |
| 2164 | { |
| 2165 | FT_TRACE2(( "_bdf_parse_start: " ACMSG11, p->font->bpp )); |
| 2166 | p->font->bpp = 4; |
| 2167 | } |
| 2168 | } |
| 2169 | else |
| 2170 | p->font->bpp = 1; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2171 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2172 | p->flags |= _BDF_SIZE; |
| 2173 | |
| 2174 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2177 | error = BDF_Err_Invalid_File_Format; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2178 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2179 | Exit: |
| 2180 | return error; |
| 2181 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2182 | |
| 2183 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2184 | /*************************************************************************/ |
| 2185 | /* */ |
| 2186 | /* API. */ |
| 2187 | /* */ |
| 2188 | /*************************************************************************/ |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2189 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2190 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2191 | FT_LOCAL_DEF( FT_Error ) |
| 2192 | bdf_load_font( FT_Stream stream, |
| 2193 | FT_Memory extmemory, |
| 2194 | bdf_options_t* opts, |
| 2195 | bdf_font_t* *font ) |
| 2196 | { |
| 2197 | unsigned long lineno; |
| 2198 | _bdf_parse_t p; |
| 2199 | |
David Turner | d490e37 | 2002-05-28 23:40:37 +0000 | [diff] [blame^] | 2200 | FT_Memory memory = NULL; |
| 2201 | FT_Error error = BDF_Err_Ok; |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2202 | |
| 2203 | |
| 2204 | FT_MEM_SET( &p, 0, sizeof ( _bdf_parse_t ) ); |
| 2205 | |
Werner Lemberg | 233302a | 2002-05-22 05:41:06 +0000 | [diff] [blame] | 2206 | p.opts = (bdf_options_t*)( ( opts != 0 ) ? opts : &_bdf_opts ); |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2207 | p.minlb = 32767; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2208 | p.memory = extmemory; /* only during font creation */ |
| 2209 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2210 | error = _bdf_readstream( stream, _bdf_parse_start, |
| 2211 | (void *)&p, &lineno ); |
| 2212 | if ( error ) |
| 2213 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2214 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2215 | if ( p.font != 0 ) |
| 2216 | { |
| 2217 | /* If the font is not proportional, set the font's monowidth */ |
| 2218 | /* field to the width of the font bounding box. */ |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2219 | memory = p.font->memory; |
| 2220 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2221 | if ( p.font->spacing != BDF_PROPORTIONAL ) |
| 2222 | p.font->monowidth = p.font->bbx.width; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2223 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2224 | /* If the number of glyphs loaded is not that of the original count, */ |
| 2225 | /* indicate the difference. */ |
| 2226 | if ( p.cnt != p.font->glyphs_used + p.font->unencoded_used ) |
| 2227 | { |
| 2228 | FT_TRACE2(( "bdf_load_font: " ACMSG15, p.cnt, |
| 2229 | p.font->glyphs_used + p.font->unencoded_used )); |
| 2230 | p.font->modified = 1; |
| 2231 | } |
| 2232 | |
| 2233 | /* Once the font has been loaded, adjust the overall font metrics if */ |
| 2234 | /* necessary. */ |
| 2235 | if ( p.opts->correct_metrics != 0 && |
| 2236 | ( p.font->glyphs_used > 0 || p.font->unencoded_used > 0 ) ) |
| 2237 | { |
| 2238 | if ( p.maxrb - p.minlb != p.font->bbx.width ) |
| 2239 | { |
| 2240 | FT_TRACE2(( "bdf_load_font: " ACMSG3, |
| 2241 | p.font->bbx.width, p.maxrb - p.minlb )); |
Werner Lemberg | 233302a | 2002-05-22 05:41:06 +0000 | [diff] [blame] | 2242 | p.font->bbx.width = (unsigned short)( p.maxrb - p.minlb ); |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2243 | p.font->modified = 1; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2246 | if ( p.font->bbx.x_offset != p.minlb ) |
| 2247 | { |
| 2248 | FT_TRACE2(( "bdf_load_font: " ACMSG4, |
| 2249 | p.font->bbx.x_offset, p.minlb )); |
| 2250 | p.font->bbx.x_offset = p.minlb; |
| 2251 | p.font->modified = 1; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2252 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2253 | |
| 2254 | if ( p.font->bbx.ascent != p.maxas ) |
| 2255 | { |
| 2256 | FT_TRACE2(( "bdf_load_font: " ACMSG5, |
| 2257 | p.font->bbx.ascent, p.maxas )); |
| 2258 | p.font->bbx.ascent = p.maxas; |
| 2259 | p.font->modified = 1; |
| 2260 | } |
| 2261 | |
| 2262 | if ( p.font->bbx.descent != p.maxds ) |
| 2263 | { |
| 2264 | FT_TRACE2(( "bdf_load_font: " ACMSG6, |
| 2265 | p.font->bbx.descent, p.maxds )); |
| 2266 | p.font->bbx.descent = p.maxds; |
Werner Lemberg | 233302a | 2002-05-22 05:41:06 +0000 | [diff] [blame] | 2267 | p.font->bbx.y_offset = (short)( -p.maxds ); |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2268 | p.font->modified = 1; |
| 2269 | } |
| 2270 | |
| 2271 | if ( p.maxas + p.maxds != p.font->bbx.height ) |
| 2272 | { |
| 2273 | FT_TRACE2(( "bdf_load_font: " ACMSG7, |
| 2274 | p.font->bbx.height, p.maxas + p.maxds )); |
Werner Lemberg | 233302a | 2002-05-22 05:41:06 +0000 | [diff] [blame] | 2275 | p.font->bbx.height = (unsigned short)( p.maxas + p.maxds ); |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | if ( p.flags & _BDF_SWIDTH_ADJ ) |
| 2279 | FT_TRACE2(( "bdf_load_font: " ACMSG8 )); |
| 2280 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2283 | if ( p.flags & _BDF_START ) |
| 2284 | { |
| 2285 | { |
| 2286 | /* The ENDFONT field was never reached or did not exist. */ |
| 2287 | if ( !( p.flags & _BDF_GLYPHS ) ) |
| 2288 | /* Error happened while parsing header. */ |
| 2289 | FT_ERROR(( "bdf_load_font: " ERRMSG2, lineno )); |
| 2290 | else |
| 2291 | /* Error happened when parsing glyphs. */ |
| 2292 | FT_ERROR(( "bdf_load_font: " ERRMSG3, lineno )); |
| 2293 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2296 | /* Free up the list used during the parsing. */ |
| 2297 | FT_FREE( p.list.field ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2298 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2299 | if ( p.font != 0 ) |
| 2300 | { |
| 2301 | /* Make sure the comments are NULL terminated if they exist. */ |
| 2302 | memory = p.font->memory; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2303 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2304 | if ( p.font->comments_len > 0 ) { |
| 2305 | if ( FT_RENEW_ARRAY( p.font->comments, |
| 2306 | p.font->comments_len, |
| 2307 | p.font->comments_len + 1 ) ) |
| 2308 | goto Exit; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2309 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2310 | p.font->comments[p.font->comments_len] = 0; |
| 2311 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2314 | *font = p.font; |
| 2315 | |
| 2316 | Exit: |
| 2317 | return error; |
| 2318 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2319 | |
| 2320 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2321 | FT_LOCAL_DEF( void ) |
| 2322 | bdf_free_font( bdf_font_t* font ) |
| 2323 | { |
| 2324 | bdf_property_t* prop; |
| 2325 | unsigned long i; |
| 2326 | bdf_glyph_t* glyphs; |
| 2327 | FT_Memory memory; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2328 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2329 | |
| 2330 | if ( font == 0 ) |
| 2331 | return; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2332 | |
| 2333 | memory = font->memory; |
| 2334 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2335 | FT_FREE( font->name ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2336 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2337 | /* Free up the internal hash table of property names. */ |
| 2338 | if ( font->internal ) |
| 2339 | { |
| 2340 | hash_free( (hashtable *)font->internal, memory ); |
| 2341 | FT_FREE( font->internal ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2344 | /* Free up the comment info. */ |
| 2345 | FT_FREE( font->comments ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2346 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2347 | /* Free up the properties. */ |
| 2348 | for ( i = 0; i < font->props_size; i++ ) |
| 2349 | { |
| 2350 | if ( font->props[i].format == BDF_ATOM ) |
| 2351 | FT_FREE( font->props[i].value.atom ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2354 | FT_FREE( font->props ); |
| 2355 | |
| 2356 | /* Free up the character info. */ |
| 2357 | for ( i = 0, glyphs = font->glyphs; |
| 2358 | i < font->glyphs_used; i++, glyphs++ ) |
| 2359 | { |
| 2360 | FT_FREE( glyphs->name ); |
| 2361 | FT_FREE( glyphs->bitmap ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2364 | for ( i = 0, glyphs = font->unencoded; i < font->unencoded_used; |
| 2365 | i++, glyphs++ ) |
| 2366 | { |
| 2367 | FT_FREE( glyphs->name ); |
| 2368 | FT_FREE( glyphs->bitmap ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2369 | } |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2370 | |
| 2371 | FT_FREE( font->glyphs ); |
| 2372 | FT_FREE( font->unencoded ); |
| 2373 | |
| 2374 | /* Free up the overflow storage if it was used. */ |
| 2375 | for ( i = 0, glyphs = font->overflow.glyphs; |
| 2376 | i < font->overflow.glyphs_used; i++, glyphs++ ) |
| 2377 | { |
| 2378 | FT_FREE( glyphs->name ); |
| 2379 | FT_FREE( glyphs->bitmap ); |
| 2380 | } |
| 2381 | |
| 2382 | FT_FREE( font->overflow.glyphs ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2383 | |
| 2384 | /* bdf_cleanup */ |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2385 | hash_free( &(font->proptbl), memory ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2386 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2387 | /* Free up the user defined properties. */ |
| 2388 | for (prop = font->user_props, i = 0; |
| 2389 | i < font->nuser_props; i++, prop++ ) |
| 2390 | { |
| 2391 | FT_FREE( prop->name ); |
| 2392 | if ( prop->format == BDF_ATOM ) |
| 2393 | FT_FREE( prop->value.atom ); |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2394 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2395 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2396 | FT_FREE( font->user_props ); |
| 2397 | |
| 2398 | /* FREE( font ); */ /* XXX Fixme */ |
| 2399 | } |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2400 | |
| 2401 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2402 | FT_LOCAL_DEF( bdf_property_t * ) |
| 2403 | bdf_get_font_property( bdf_font_t* font, |
| 2404 | char* name ) |
| 2405 | { |
| 2406 | hashnode hn; |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2407 | |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2408 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2409 | if ( font == 0 || font->props_size == 0 || name == 0 || *name == 0 ) |
David Turner | 993a8d0 | 2002-05-18 12:03:43 +0000 | [diff] [blame] | 2410 | return 0; |
| 2411 | |
Werner Lemberg | 7cf4d37 | 2002-05-21 14:13:01 +0000 | [diff] [blame] | 2412 | hn = hash_lookup( name, (hashtable *)font->internal ); |
| 2413 | |
| 2414 | return hn ? ( font->props + (unsigned long)hn->data ) : 0; |
| 2415 | } |
| 2416 | |
| 2417 | |
| 2418 | /* END */ |