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