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