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