| 1 |
// -*- related-file-name: "../include/efont/afmparse.hh" -*- |
| 2 |
|
| 3 |
/* afmparse.{cc,hh} -- Adobe Font Metrics parsing |
| 4 |
* |
| 5 |
* Copyright (c) 1998-2019 Eddie Kohler |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify it |
| 8 |
* under the terms of the GNU General Public License as published by the Free |
| 9 |
* Software Foundation; either version 2 of the License, or (at your option) |
| 10 |
* any later version. This program is distributed in the hope that it will be |
| 11 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 13 |
* Public License for more details. |
| 14 |
*/ |
| 15 |
|
| 16 |
#ifdef HAVE_CONFIG_H |
| 17 |
# include <config.h> |
| 18 |
#endif |
| 19 |
#include <efont/afmparse.hh> |
| 20 |
#include <efont/t1cs.hh> |
| 21 |
#include <efont/metrics.hh> |
| 22 |
#include <lcdf/strtonum.h> |
| 23 |
#include <string.h> |
| 24 |
#include <stdarg.h> |
| 25 |
#include <ctype.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#if HAVE_BROKEN_STRTOD |
| 28 |
# define strtod good_strtod |
| 29 |
#endif |
| 30 |
namespace Efont { |
| 31 |
|
| 32 |
static bool name_enders[256]; |
| 33 |
static char xvalue[256]; |
| 34 |
|
| 35 |
|
| 36 |
AfmParser::AfmParser(Slurper &slurp) |
| 37 |
: _slurper(slurp), |
| 38 |
_line(0), _pos(0), _length(0) |
| 39 |
{ |
| 40 |
static_initialize(); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
void |
| 45 |
AfmParser::trim_end() |
| 46 |
{ |
| 47 |
int l = _length - 1; |
| 48 |
while (l >= 0 && isspace(_line[l])) { |
| 49 |
_line[l] = 0; |
| 50 |
l--; |
| 51 |
} |
| 52 |
_length = l + 1; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
void |
| 57 |
AfmParser::static_initialize() |
| 58 |
{ |
| 59 |
if (name_enders[(uint8_t)'(']) |
| 60 |
return; |
| 61 |
name_enders[(uint8_t)' '] = name_enders[(uint8_t)'\t'] |
| 62 |
= name_enders[(uint8_t)'\r'] = name_enders[(uint8_t)'\f'] |
| 63 |
= name_enders[(uint8_t)'\v'] = name_enders[(uint8_t)'\n'] |
| 64 |
= name_enders[(uint8_t)'\0'] = name_enders[(uint8_t)'['] |
| 65 |
= name_enders[(uint8_t)']'] = name_enders[(uint8_t)'/'] |
| 66 |
= name_enders[(uint8_t)'('] = name_enders[(uint8_t)')'] |
| 67 |
= name_enders[(uint8_t)';'] = true; |
| 68 |
|
| 69 |
xvalue[(uint8_t)'0'] = 0; |
| 70 |
xvalue[(uint8_t)'1'] = 1; |
| 71 |
xvalue[(uint8_t)'2'] = 2; |
| 72 |
xvalue[(uint8_t)'3'] = 3; |
| 73 |
xvalue[(uint8_t)'4'] = 4; |
| 74 |
xvalue[(uint8_t)'5'] = 5; |
| 75 |
xvalue[(uint8_t)'6'] = 6; |
| 76 |
xvalue[(uint8_t)'7'] = 7; |
| 77 |
xvalue[(uint8_t)'8'] = 8; |
| 78 |
xvalue[(uint8_t)'9'] = 9; |
| 79 |
xvalue[(uint8_t)'a'] = 10; |
| 80 |
xvalue[(uint8_t)'b'] = 11; |
| 81 |
xvalue[(uint8_t)'c'] = 12; |
| 82 |
xvalue[(uint8_t)'d'] = 13; |
| 83 |
xvalue[(uint8_t)'e'] = 14; |
| 84 |
xvalue[(uint8_t)'f'] = 15; |
| 85 |
xvalue[(uint8_t)'A'] = 10; |
| 86 |
xvalue[(uint8_t)'B'] = 11; |
| 87 |
xvalue[(uint8_t)'C'] = 12; |
| 88 |
xvalue[(uint8_t)'D'] = 13; |
| 89 |
xvalue[(uint8_t)'E'] = 14; |
| 90 |
xvalue[(uint8_t)'F'] = 15; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
// isall == is total == is all of the string |
| 95 |
|
| 96 |
// %s whitespace-terminated PermString - non-zero-length |
| 97 |
// %/s name-terminated PermString - non-zero-length |
| 98 |
// %+s rest of line PermString |
| 99 |
// %= rest of line/until next `=' PermString; beginning & ending spaces |
| 100 |
// elided |
| 101 |
// %d integer |
| 102 |
// %g number |
| 103 |
// %x hex integer |
| 104 |
// %b boolean |
| 105 |
// %c character |
| 106 |
// %< hex string; argument is String *stored |
| 107 |
// %. any non-alphanumeric |
| 108 |
// ???????????? EOS vs. don't-skip-to-EOS ??????????? |
| 109 |
|
| 110 |
|
| 111 |
#define FAIL(s) do { _message = s; return 0; } while (0) |
| 112 |
|
| 113 |
unsigned char * |
| 114 |
AfmParser::vis(const char *formatsigned, va_list valist) |
| 115 |
{ |
| 116 |
const unsigned char *format = (const unsigned char *)formatsigned; |
| 117 |
unsigned char *str = _pos; |
| 118 |
|
| 119 |
// Oftentimes, we'll get a keyword first. So handle that simple case |
| 120 |
// with a tight loop for (possibly) better performance and slightly different |
| 121 |
// semantics. (A keyword that comes first in the format must be followed |
| 122 |
// by a nonalphanumeric in the input to match.) |
| 123 |
if (isalpha(*format)) { |
| 124 |
bool ok = true; |
| 125 |
for (; *format && *format != ' ' && ok; format++, str++) { |
| 126 |
assert(isalnum(*format)); |
| 127 |
if (*format != *str) ok = false; |
| 128 |
} |
| 129 |
if (!ok || isalnum(*str)) { |
| 130 |
if (_message) return 0; |
| 131 |
_fail_field = -1; |
| 132 |
FAIL("keyword mismatch"); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
int fplus, fslash; |
| 137 |
_fail_field = 0; |
| 138 |
_message = PermString(); |
| 139 |
|
| 140 |
while (1) { |
| 141 |
|
| 142 |
switch (*format) { |
| 143 |
|
| 144 |
/* - - - - - - - - percent specifications - - - - - - - */ |
| 145 |
case '%': |
| 146 |
fplus = fslash = 0; |
| 147 |
_fail_field++; |
| 148 |
|
| 149 |
percentspec: |
| 150 |
switch (*++format) { |
| 151 |
|
| 152 |
// FLAGS |
| 153 |
case '+': |
| 154 |
fplus++; |
| 155 |
goto percentspec; |
| 156 |
case '/': |
| 157 |
fslash++; |
| 158 |
goto percentspec; |
| 159 |
|
| 160 |
case '%': |
| 161 |
goto matchchar; |
| 162 |
|
| 163 |
case 'b': |
| 164 |
case 's': { |
| 165 |
int len; |
| 166 |
|
| 167 |
if (fplus) |
| 168 |
len = strlen((char *)str); |
| 169 |
else if (fslash) { |
| 170 |
for (len = 0; !name_enders[ str[len] ]; len++) |
| 171 |
; |
| 172 |
if (len == 0) |
| 173 |
FAIL("should be a string"); |
| 174 |
} else { |
| 175 |
for (len = 0; !isspace(str[len]) && str[len]; len++) |
| 176 |
; |
| 177 |
if (len == 0) |
| 178 |
FAIL("should be a string"); |
| 179 |
} |
| 180 |
|
| 181 |
PermString s = PermString((char *)str, len); |
| 182 |
str += len; |
| 183 |
|
| 184 |
// Now we have the string. What to do with it? Depends on format. |
| 185 |
if (*format == 'b') { |
| 186 |
|
| 187 |
bool *bstore = va_arg(valist, bool *); |
| 188 |
if (s == "true") { |
| 189 |
if (bstore) *bstore = 1; |
| 190 |
} else if (s == "false") { |
| 191 |
if (bstore) *bstore = 0; |
| 192 |
} else |
| 193 |
FAIL("should be `true' or `false'"); |
| 194 |
|
| 195 |
} else { |
| 196 |
PermString *sstore = va_arg(valist, PermString *); |
| 197 |
if (sstore) *sstore = s; |
| 198 |
} |
| 199 |
break; |
| 200 |
} |
| 201 |
|
| 202 |
case '(': { |
| 203 |
if (*str++ != '(') FAIL("should be a parenthesized string"); |
| 204 |
unsigned char *last = str; |
| 205 |
int paren_level = 0; |
| 206 |
while (*last && paren_level >= 0) { |
| 207 |
if (*last == '(') paren_level++; |
| 208 |
if (*last == ')') paren_level--; |
| 209 |
last++; |
| 210 |
} |
| 211 |
if (paren_level >= 0) FAIL("had unbalanced parentheses"); |
| 212 |
|
| 213 |
PermString *sstore = va_arg(valist, PermString *); |
| 214 |
if (sstore) *sstore = PermString((char *)str, last - str - 1); |
| 215 |
str = last; |
| 216 |
break; |
| 217 |
} |
| 218 |
|
| 219 |
case 'd': |
| 220 |
case 'i': { |
| 221 |
union { unsigned char *uc; char *c; } new_str; |
| 222 |
int v = strtol((char *)str, &new_str.c, 10); |
| 223 |
if (new_str.uc == str) FAIL("should be an integer"); |
| 224 |
|
| 225 |
str = new_str.uc; |
| 226 |
int *istore = va_arg(valist, int *); |
| 227 |
if (istore) *istore = v; |
| 228 |
break; |
| 229 |
} |
| 230 |
|
| 231 |
case 'x': { |
| 232 |
union { unsigned char *uc; char *c; } new_str; |
| 233 |
int v = strtol((char *)str, &new_str.c, 16); |
| 234 |
if (new_str.uc == str) FAIL("should be a hex integer"); |
| 235 |
|
| 236 |
str = new_str.uc; |
| 237 |
int *istore = va_arg(valist, int *); |
| 238 |
if (istore) *istore = v; |
| 239 |
break; |
| 240 |
} |
| 241 |
|
| 242 |
case 'e': |
| 243 |
case 'f': |
| 244 |
case 'g': { |
| 245 |
union { unsigned char *uc; char *c; } new_str; |
| 246 |
double v = strtonumber((char *)str, &new_str.c); |
| 247 |
if (v < MIN_KNOWN_DOUBLE) v = MIN_KNOWN_DOUBLE; |
| 248 |
if (new_str.uc == str) FAIL("should be a real number"); |
| 249 |
|
| 250 |
str = new_str.uc; |
| 251 |
double *dstore = va_arg(valist, double *); |
| 252 |
if (dstore) *dstore = v; |
| 253 |
break; |
| 254 |
} |
| 255 |
|
| 256 |
case '<': { |
| 257 |
unsigned char *endbrack = |
| 258 |
(unsigned char *)strchr((char *)str, '>'); |
| 259 |
int n = (endbrack ? endbrack - (str + 1) : 0); |
| 260 |
if (!endbrack || n % 2 != 0) |
| 261 |
FAIL("should be hex values in <angle brackets>"); |
| 262 |
String s = String::make_uninitialized(n/2); |
| 263 |
unsigned char *data = s.mutable_udata(); |
| 264 |
|
| 265 |
str++; |
| 266 |
while (*str != '>') { |
| 267 |
if (isxdigit(str[0]) && isxdigit(str[1])) { |
| 268 |
*data++ = xvalue[str[0]] * 16 + xvalue[str[1]]; |
| 269 |
str += 2; |
| 270 |
} else |
| 271 |
FAIL("had non-hex digits in the angle brackets"); |
| 272 |
} |
| 273 |
str++; |
| 274 |
|
| 275 |
if (String *datastore = va_arg(valist, String *)) |
| 276 |
*datastore = s; |
| 277 |
break; |
| 278 |
} |
| 279 |
|
| 280 |
default: |
| 281 |
assert(0 /* internal error: bad % */); |
| 282 |
FAIL(""); |
| 283 |
|
| 284 |
} |
| 285 |
break; |
| 286 |
/* - - - - - - - - end percent specifications - - - - - - - */ |
| 287 |
|
| 288 |
case ' ': |
| 289 |
if (!isspace(*str)) |
| 290 |
FAIL("should be followed by whitespace"); |
| 291 |
/* FALLTHRU */ |
| 292 |
|
| 293 |
case '-': |
| 294 |
while (isspace(*str)) str++; |
| 295 |
break; |
| 296 |
|
| 297 |
case 0: |
| 298 |
// always eat space at end of format |
| 299 |
while (isspace(*str)) str++; |
| 300 |
return str; |
| 301 |
|
| 302 |
default: |
| 303 |
matchchar: |
| 304 |
if (*str++ != *format) |
| 305 |
FAIL(permprintf("- expected `%c'", *format)); |
| 306 |
break; |
| 307 |
|
| 308 |
} |
| 309 |
|
| 310 |
format++; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
bool |
| 316 |
AfmParser::isall(const char *format, ...) |
| 317 |
{ |
| 318 |
va_list valist; |
| 319 |
va_start(valist, format); |
| 320 |
unsigned char *new_pos = vis(format, valist); |
| 321 |
va_end(valist); |
| 322 |
if (new_pos && *new_pos == 0) { |
| 323 |
_pos = new_pos; |
| 324 |
return 1; |
| 325 |
} else |
| 326 |
return 0; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
bool |
| 331 |
AfmParser::is(const char *format, ...) |
| 332 |
{ |
| 333 |
va_list valist; |
| 334 |
va_start(valist, format); |
| 335 |
unsigned char *new_pos = vis(format, valist); |
| 336 |
va_end(valist); |
| 337 |
if (new_pos) { |
| 338 |
_pos = new_pos; |
| 339 |
return 1; |
| 340 |
} else |
| 341 |
return 0; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
PermString |
| 346 |
AfmParser::keyword() const |
| 347 |
{ |
| 348 |
const char *f = (const char *)_pos; |
| 349 |
while (isspace((unsigned char) *f)) |
| 350 |
f++; |
| 351 |
const char *l = f; |
| 352 |
while (isalnum((unsigned char) *l) || *l == '_') |
| 353 |
l++; |
| 354 |
return PermString(f, l - f); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
void |
| 359 |
AfmParser::skip_until(unsigned char c) |
| 360 |
{ |
| 361 |
while (*_pos && *_pos != c) |
| 362 |
_pos++; |
| 363 |
} |
| 364 |
|
| 365 |
} |