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