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