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