| 1 |
/* cscheck.{cc,hh} -- checking Type 1 charstrings for validity |
| 2 |
* |
| 3 |
* Copyright (c) 1999-2023 Eddie Kohler |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify it |
| 6 |
* under the terms of the GNU General Public License as published by the Free |
| 7 |
* Software Foundation; either version 2 of the License, or (at your option) |
| 8 |
* any later version. This program is distributed in the hope that it will be |
| 9 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 11 |
* Public License for more details. |
| 12 |
*/ |
| 13 |
|
| 14 |
#ifdef HAVE_CONFIG_H |
| 15 |
# include <config.h> |
| 16 |
#endif |
| 17 |
#include "cscheck.hh" |
| 18 |
#include <efont/t1item.hh> |
| 19 |
#include <lcdf/error.hh> |
| 20 |
#include <efont/t1unparser.hh> |
| 21 |
|
| 22 |
using namespace Efont; |
| 23 |
|
| 24 |
#define CHECK_STACK_GE(numargs) do { if (size() < numargs) return error(errUnderflow, cmd); } while (0) |
| 25 |
#define CHECK_STACK_EQ(numargs) do { CHECK_STACK_GE(numargs); if (size() > numargs) _errh->warning("too many arguments to %<%s%> (have %d, expected %d)", Charstring::command_name(cmd).c_str(), size(), numargs); } while (0) |
| 26 |
#define CHECK_STACK_CPEQ(numargs) do { CHECK_STACK_EQ(numargs); if (!_cp_exists) return error(errCurrentPoint, cmd); } while (0) |
| 27 |
|
| 28 |
|
| 29 |
CharstringChecker::CharstringChecker() |
| 30 |
: CharstringInterp(), _errh(0) |
| 31 |
{ |
| 32 |
set_careful(true); |
| 33 |
} |
| 34 |
|
| 35 |
CharstringChecker::CharstringChecker(const Vector<double> &weight) |
| 36 |
: CharstringInterp(weight), _errh(0) |
| 37 |
{ |
| 38 |
set_careful(true); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
void |
| 43 |
CharstringChecker::stem(double y, double dy, const char *cmd_name) |
| 44 |
{ |
| 45 |
bool is_v = (cmd_name[0] == 'v'); |
| 46 |
Vector<double> &hints = (is_v ? _h_vstem : _h_hstem); |
| 47 |
const char *dimen_name = (is_v ? "x" : "y"); |
| 48 |
if (dy < 0) { |
| 49 |
y += dy; |
| 50 |
dy = -dy; |
| 51 |
} |
| 52 |
if (dy < 0.5) |
| 53 |
_errh->warning("small delta-%s in %<%s%> (%g)", dimen_name, cmd_name, dy); |
| 54 |
for (int i = 0; i < hints.size(); i += 2) |
| 55 |
if ((hints[i] >= y && hints[i+1] <= y) |
| 56 |
|| (hints[i] >= y+dy && hints[i+1] <= y+dy)) |
| 57 |
_errh->warning("overlapping %<%s%> hints", cmd_name); |
| 58 |
hints.push_back(y); |
| 59 |
hints.push_back(y+dy); |
| 60 |
} |
| 61 |
|
| 62 |
void |
| 63 |
CharstringChecker::check_stem3(const char *cmd_name) |
| 64 |
{ |
| 65 |
bool is_v = (cmd_name[0] == 'v'); |
| 66 |
Vector<double> &hints = (is_v ? _h_vstem : _h_hstem); |
| 67 |
Vector<double> &old_hints = (is_v ? _h_vstem3 : _h_hstem3); |
| 68 |
assert(hints.size() == 6); |
| 69 |
|
| 70 |
// sort hints |
| 71 |
int i0, i1, i2; |
| 72 |
if (hints[0] > hints[2]) |
| 73 |
i0 = 2, i1 = 0; |
| 74 |
else |
| 75 |
i0 = 0, i1 = 2; |
| 76 |
if (hints[4] < hints[i0]) |
| 77 |
i2 = i1, i1 = i0, i0 = 4; |
| 78 |
else if (hints[4] < hints[i1]) |
| 79 |
i2 = i1, i1 = 4; |
| 80 |
else |
| 81 |
i2 = 4; |
| 82 |
|
| 83 |
// check constraints. count "almost equal" as equal |
| 84 |
double stemw0 = hints[i0+1] - hints[i0]; |
| 85 |
double stemw2 = hints[i2+1] - hints[i2]; |
| 86 |
if ((int)(1024*(stemw0 - stemw2) + .5) != 0) |
| 87 |
_errh->error("bad %<%s%>: extreme stem widths unequal (%g, %g)", cmd_name, stemw0, stemw2); |
| 88 |
|
| 89 |
double c0 = (hints[i0] + hints[i0+1])/2; |
| 90 |
double c1 = (hints[i1] + hints[i1+1])/2; |
| 91 |
double c2 = (hints[i2] + hints[i2+1])/2; |
| 92 |
if ((int)(1024*((c1 - c0) - (c2 - c1)) + .5) != 0) |
| 93 |
_errh->error("bad %<%s%>: stem gaps unequal (%g, %g)", cmd_name, c1-c0, c2-c1); |
| 94 |
|
| 95 |
// compare to old hints |
| 96 |
if (old_hints.size()) { |
| 97 |
for (int i = 0; i < old_hints.size(); i++) |
| 98 |
if (hints[i] != old_hints[i]) { |
| 99 |
_errh->warning("%<%s%> conflicts with old %<%s%>", cmd_name, cmd_name); |
| 100 |
break; |
| 101 |
} |
| 102 |
} |
| 103 |
old_hints = hints; |
| 104 |
} |
| 105 |
|
| 106 |
void |
| 107 |
CharstringChecker::moveto(double, double, bool cp_exists) |
| 108 |
{ |
| 109 |
_cp_exists = cp_exists; |
| 110 |
} |
| 111 |
|
| 112 |
void |
| 113 |
CharstringChecker::rmoveto(double, double) |
| 114 |
{ |
| 115 |
_cp_exists = true; |
| 116 |
_just_flexed = false; |
| 117 |
} |
| 118 |
|
| 119 |
void |
| 120 |
CharstringChecker::rlineto(double, double) |
| 121 |
{ |
| 122 |
} |
| 123 |
|
| 124 |
void |
| 125 |
CharstringChecker::rrcurveto(double, double, double, double, double, double) |
| 126 |
{ |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
bool |
| 131 |
CharstringChecker::error(int which, int data) |
| 132 |
{ |
| 133 |
CharstringInterp::error(which, data); |
| 134 |
_errh->error("%s", error_string().c_str()); |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
bool |
| 139 |
CharstringChecker::callothersubr() |
| 140 |
{ |
| 141 |
int othersubrnum = (int)top(0); |
| 142 |
int n = (int)top(1); |
| 143 |
int i; |
| 144 |
|
| 145 |
pop(2); |
| 146 |
if (othersubrnum < 0 || size() < n) |
| 147 |
return false; |
| 148 |
|
| 149 |
if (!_started && (othersubrnum < Cs::othcCountercontrolpart |
| 150 |
|| othersubrnum > Cs::othcMM6)) |
| 151 |
_errh->warning("first command not %<hsbw%> or %<sbw%>"); |
| 152 |
|
| 153 |
bool retval = true; |
| 154 |
|
| 155 |
switch (othersubrnum) { |
| 156 |
|
| 157 |
case Cs::othcFlexend: |
| 158 |
if (n != 3) { |
| 159 |
_errh->error("wrong number of arguments to Flex"); |
| 160 |
goto unknown; |
| 161 |
} |
| 162 |
if (!_flex) { |
| 163 |
_errh->error("no Flex in progress"); |
| 164 |
retval = false; |
| 165 |
} else if (ps_size() != 16) { |
| 166 |
_errh->error("Flex needs 16 arguments, have %d", ps_size()); |
| 167 |
retval = false; |
| 168 |
} else { |
| 169 |
ps_clear(); |
| 170 |
ps_push(top(0)); |
| 171 |
ps_push(top(1)); |
| 172 |
_flex = false; |
| 173 |
} |
| 174 |
break; |
| 175 |
|
| 176 |
case Cs::othcFlexbegin: |
| 177 |
if (n != 0) { |
| 178 |
_errh->error("wrong number of arguments to Flex"); |
| 179 |
goto unknown; |
| 180 |
} |
| 181 |
ps_clear(); |
| 182 |
ps_push(_cp.x); |
| 183 |
ps_push(_cp.y); |
| 184 |
_flex = true; |
| 185 |
_just_flexed = true; |
| 186 |
//_flex_connect = _connect; |
| 187 |
break; |
| 188 |
|
| 189 |
case Cs::othcFlexmiddle: |
| 190 |
if (n != 0) { |
| 191 |
_errh->error("wrong number of arguments to Flex"); |
| 192 |
goto unknown; |
| 193 |
} |
| 194 |
if (!_flex) |
| 195 |
retval = error(errFlex, 0); |
| 196 |
else { |
| 197 |
if (_just_flexed) |
| 198 |
_errh->error("Flex control points must be separated by a moveto"); |
| 199 |
ps_push(_cp.x); |
| 200 |
ps_push(_cp.y); |
| 201 |
_just_flexed = true; |
| 202 |
} |
| 203 |
break; |
| 204 |
|
| 205 |
case Cs::othcReplacehints: |
| 206 |
if (n != 1) { |
| 207 |
_errh->error("wrong number of arguments to hint replacement"); |
| 208 |
goto unknown; |
| 209 |
} |
| 210 |
ps_clear(); |
| 211 |
ps_push(top()); |
| 212 |
_h_hstem.clear(); |
| 213 |
_h_vstem.clear(); |
| 214 |
break; |
| 215 |
|
| 216 |
case Cs::othcCountercontrolpart: |
| 217 |
case Cs::othcCountercontrol: |
| 218 |
if (_counter_controlled) |
| 219 |
_errh->error("duplicate counter control instructions"); |
| 220 |
else if (_started |
| 221 |
&& _last_command != 256 + Cs::othcCountercontrolpart |
| 222 |
&& _last_command != Cs::cSbw |
| 223 |
&& _last_command != Cs::cHsbw) |
| 224 |
_errh->error("counter control must appear immediately after %<sbw%> or %<hsbw%>"); |
| 225 |
if (n < 0 || n > 22) |
| 226 |
_errh->error("too many arguments to counter control, max 22"); |
| 227 |
else if (n != size()) { |
| 228 |
_errh->error("too few arguments to counter control, expected %d", size()); |
| 229 |
n = size(); |
| 230 |
} |
| 231 |
ps_clear(); |
| 232 |
_counter_controlled = (othersubrnum == Cs::othcCountercontrol); |
| 233 |
break; |
| 234 |
|
| 235 |
case Cs::othcMM1: |
| 236 |
case Cs::othcMM2: |
| 237 |
case Cs::othcMM3: |
| 238 |
case Cs::othcMM4: |
| 239 |
case Cs::othcMM6: |
| 240 |
retval = mm_command(othersubrnum, n); |
| 241 |
goto skip_pop; |
| 242 |
|
| 243 |
default: // unknown |
| 244 |
unknown: |
| 245 |
_errh->warning("unknown callothersubr %<%d%>", othersubrnum); |
| 246 |
ps_clear(); |
| 247 |
for (i = 0; i < n; i++) |
| 248 |
ps_push(top(i)); |
| 249 |
break; |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
pop(n); |
| 254 |
if (_last_command == 256 + Cs::othcCountercontrolpart |
| 255 |
&& othersubrnum != Cs::othcCountercontrol) |
| 256 |
_errh->error("partial counter control instruction"); |
| 257 |
_last_command = 256 + othersubrnum; |
| 258 |
skip_pop: |
| 259 |
return retval; |
| 260 |
} |
| 261 |
|
| 262 |
//#define DEBUG(s) printf s |
| 263 |
#define DEBUG(s) |
| 264 |
|
| 265 |
bool |
| 266 |
CharstringChecker::type1_command(int cmd) |
| 267 |
{ |
| 268 |
if (_subrno < 0) |
| 269 |
++_ncommand; |
| 270 |
if (cmd == Cs::cCallsubr) { |
| 271 |
int old_subrno = _subrno; |
| 272 |
_subrno = (size() > 1 ? (int) top(0) : -300); |
| 273 |
bool result = callsubr_command(); |
| 274 |
_subrno = old_subrno; |
| 275 |
return result; |
| 276 |
} else if (cmd == Cs::cCallothersubr) { |
| 277 |
CHECK_STACK_GE(2); |
| 278 |
return callothersubr(); |
| 279 |
} else if (cmd == Cs::cReturn) { |
| 280 |
return false; |
| 281 |
} else if (cmd == Cs::cPop || cmd == Cs::cDiv) { |
| 282 |
return arith_command(cmd); |
| 283 |
} |
| 284 |
|
| 285 |
if (cmd != Cs::cHsbw && cmd != Cs::cSbw) { |
| 286 |
if (!_started) |
| 287 |
_errh->warning("first command not %<hsbw%> or %<sbw%>"); |
| 288 |
} else { |
| 289 |
if (_started) |
| 290 |
_errh->error("duplicate %<hsbw%> or %<sbw%>"); |
| 291 |
} |
| 292 |
_started = true; |
| 293 |
|
| 294 |
switch (cmd) { |
| 295 |
|
| 296 |
case Cs::cHsbw: |
| 297 |
CHECK_STACK_EQ(2); |
| 298 |
moveto(at(0), 0, false); |
| 299 |
clear(); |
| 300 |
break; |
| 301 |
|
| 302 |
case Cs::cSbw: |
| 303 |
CHECK_STACK_EQ(4); |
| 304 |
moveto(at(0), at(1), false); |
| 305 |
clear(); |
| 306 |
break; |
| 307 |
|
| 308 |
case Cs::cClosepath: |
| 309 |
_cp_exists = false; |
| 310 |
clear(); |
| 311 |
break; |
| 312 |
|
| 313 |
case Cs::cHlineto: |
| 314 |
CHECK_STACK_CPEQ(1); |
| 315 |
rlineto(at(0), 0); |
| 316 |
clear(); |
| 317 |
break; |
| 318 |
|
| 319 |
case Cs::cHmoveto: |
| 320 |
CHECK_STACK_EQ(1); |
| 321 |
rmoveto(at(0), 0); |
| 322 |
clear(); |
| 323 |
break; |
| 324 |
|
| 325 |
case Cs::cHvcurveto: |
| 326 |
CHECK_STACK_CPEQ(4); |
| 327 |
rrcurveto(at(0), 0, at(1), at(2), 0, at(3)); |
| 328 |
clear(); |
| 329 |
break; |
| 330 |
|
| 331 |
case Cs::cRlineto: |
| 332 |
CHECK_STACK_CPEQ(2); |
| 333 |
rlineto(at(0), at(1)); |
| 334 |
clear(); |
| 335 |
break; |
| 336 |
|
| 337 |
case Cs::cRmoveto: |
| 338 |
CHECK_STACK_EQ(2); |
| 339 |
rmoveto(at(0), at(1)); |
| 340 |
clear(); |
| 341 |
break; |
| 342 |
|
| 343 |
case Cs::cRrcurveto: |
| 344 |
CHECK_STACK_CPEQ(6); |
| 345 |
rrcurveto(at(0), at(1), at(2), at(3), at(4), at(5)); |
| 346 |
clear(); |
| 347 |
break; |
| 348 |
|
| 349 |
case Cs::cVhcurveto: |
| 350 |
CHECK_STACK_CPEQ(4); |
| 351 |
rrcurveto(0, at(0), at(1), at(2), at(3), 0); |
| 352 |
clear(); |
| 353 |
break; |
| 354 |
|
| 355 |
case Cs::cVlineto: |
| 356 |
CHECK_STACK_CPEQ(1); |
| 357 |
rlineto(0, at(0)); |
| 358 |
clear(); |
| 359 |
break; |
| 360 |
|
| 361 |
case Cs::cVmoveto: |
| 362 |
CHECK_STACK_EQ(1); |
| 363 |
rmoveto(0, at(0)); |
| 364 |
clear(); |
| 365 |
break; |
| 366 |
|
| 367 |
case Cs::cHstem: |
| 368 |
CHECK_STACK_EQ(2); |
| 369 |
if (_hstem3 && !_hstem) |
| 370 |
_errh->error("charstring has both %<hstem%> and %<hstem3%>"); |
| 371 |
_hstem = true; |
| 372 |
stem(at(0), at(1), "hstem"); |
| 373 |
clear(); |
| 374 |
break; |
| 375 |
|
| 376 |
case Cs::cVstem: |
| 377 |
CHECK_STACK_EQ(2); |
| 378 |
if (_vstem3 && !_vstem) |
| 379 |
_errh->error("charstring has both %<vstem%> and %<vstem3%>"); |
| 380 |
_vstem = true; |
| 381 |
stem(at(0), at(1), "vstem"); |
| 382 |
clear(); |
| 383 |
break; |
| 384 |
|
| 385 |
case Cs::cEndchar: |
| 386 |
set_done(); |
| 387 |
_last_command = cmd; |
| 388 |
return false; |
| 389 |
|
| 390 |
case Cs::cDotsection: |
| 391 |
break; |
| 392 |
|
| 393 |
case Cs::cVstem3: |
| 394 |
CHECK_STACK_EQ(6); |
| 395 |
if (_vstem && !_vstem3) |
| 396 |
_errh->error("charstring has both %<vstem%> and %<vstem3%>"); |
| 397 |
_vstem3 = true; |
| 398 |
_h_vstem.clear(); |
| 399 |
stem(at(0), at(1), "vstem3"); |
| 400 |
stem(at(2), at(3), "vstem3"); |
| 401 |
stem(at(4), at(5), "vstem3"); |
| 402 |
check_stem3("vstem3"); |
| 403 |
clear(); |
| 404 |
break; |
| 405 |
|
| 406 |
case Cs::cHstem3: |
| 407 |
CHECK_STACK_EQ(6); |
| 408 |
if (_hstem && !_hstem3) |
| 409 |
_errh->error("charstring has both %<hstem%> and %<hstem3%>"); |
| 410 |
_hstem3 = true; |
| 411 |
_h_hstem.clear(); |
| 412 |
stem(at(0), at(1), "hstem3"); |
| 413 |
stem(at(2), at(3), "hstem3"); |
| 414 |
stem(at(4), at(5), "hstem3"); |
| 415 |
check_stem3("hstem3"); |
| 416 |
clear(); |
| 417 |
break; |
| 418 |
|
| 419 |
case Cs::cSeac: { |
| 420 |
CHECK_STACK_EQ(5); |
| 421 |
#if 0 |
| 422 |
double asb = at(0); |
| 423 |
double adx = at(1); |
| 424 |
double ady = at(2); |
| 425 |
int bchar = (int)at(3); |
| 426 |
int achar = (int)at(4); |
| 427 |
|
| 428 |
double ax = adx - asb + _sidebearing.x; |
| 429 |
double ay = ady + _sidebearing.y; |
| 430 |
|
| 431 |
Point real_sidebearing = _sidebearing; |
| 432 |
Point real_char_width = _char_width; |
| 433 |
Point original_origin = _origin; |
| 434 |
|
| 435 |
Type1Encoding *adobe = Type1Encoding::standard_encoding(); |
| 436 |
if (!adobe) |
| 437 |
return error(errInternal, cmd); |
| 438 |
Type1Charstring *t1cs = get_glyph((*adobe)[achar]); |
| 439 |
if (!t1cs) ERROR(errGlyph); |
| 440 |
_origin = Point(ax, ay); |
| 441 |
init(); |
| 442 |
t1cs->run(*this); |
| 443 |
_origin = original_origin; |
| 444 |
if (error()) return false; |
| 445 |
|
| 446 |
t1cs = get_glyph((*adobe)[bchar]); |
| 447 |
if (!t1cs) ERROR(errGlyph); |
| 448 |
init(); |
| 449 |
t1cs->run(*this); |
| 450 |
_sidebearing = real_sidebearing; |
| 451 |
_char_width = real_char_width; |
| 452 |
#endif |
| 453 |
return false; |
| 454 |
} |
| 455 |
|
| 456 |
case Cs::cSetcurrentpoint: |
| 457 |
CHECK_STACK_EQ(2); |
| 458 |
_cp = Point(at(0), at(1)); |
| 459 |
_cp_exists = true; |
| 460 |
clear(); |
| 461 |
break; |
| 462 |
|
| 463 |
case Cs::cPut: |
| 464 |
case Cs::cGet: |
| 465 |
case Cs::cStore: |
| 466 |
case Cs::cLoad: |
| 467 |
return vector_command(cmd); |
| 468 |
|
| 469 |
default: |
| 470 |
return arith_command(cmd); |
| 471 |
|
| 472 |
} |
| 473 |
|
| 474 |
if (_last_command == 256 + Cs::othcCountercontrolpart) |
| 475 |
_errh->error("partial counter control instruction"); |
| 476 |
_last_command = cmd; |
| 477 |
return true; |
| 478 |
} |
| 479 |
|
| 480 |
bool |
| 481 |
CharstringChecker::check(const CharstringContext &g, ErrorHandler *errh) |
| 482 |
{ |
| 483 |
CharstringCheckerErrorHandler<CharstringChecker> merrh(errh, this); |
| 484 |
_errh = &merrh; |
| 485 |
int old_errors = errh->nerrors(); |
| 486 |
|
| 487 |
_started = false; |
| 488 |
_flex = false; |
| 489 |
_hstem = _hstem3 = _vstem = _vstem3 = false; |
| 490 |
_just_flexed = _counter_controlled = false; |
| 491 |
_last_command = -1; |
| 492 |
_h_hstem.clear(); |
| 493 |
_h_vstem.clear(); |
| 494 |
_h_hstem3.clear(); |
| 495 |
_h_vstem3.clear(); |
| 496 |
_ncommand = 0; |
| 497 |
_subrno = -1; |
| 498 |
|
| 499 |
CharstringInterp::interpret(g); |
| 500 |
|
| 501 |
return errh->nerrors() == old_errors; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
CharstringSubrChecker::CharstringSubrChecker() |
| 507 |
: CharstringInterp(), _errh(0) |
| 508 |
{ |
| 509 |
set_careful(true); |
| 510 |
} |
| 511 |
|
| 512 |
CharstringSubrChecker::CharstringSubrChecker(const Vector<double> &weight) |
| 513 |
: CharstringInterp(weight), _errh(0) |
| 514 |
{ |
| 515 |
set_careful(true); |
| 516 |
} |
| 517 |
|
| 518 |
bool |
| 519 |
CharstringSubrChecker::error(int error, int data) |
| 520 |
{ |
| 521 |
CharstringInterp::error(error, data); |
| 522 |
_errh->error("%s", error_string().c_str()); |
| 523 |
return false; |
| 524 |
} |
| 525 |
|
| 526 |
bool |
| 527 |
CharstringSubrChecker::type1_command(int cmd) |
| 528 |
{ |
| 529 |
switch (cmd) { |
| 530 |
case Cs::cReturn: |
| 531 |
_returned = true; |
| 532 |
return false; |
| 533 |
default: |
| 534 |
clear(); |
| 535 |
return true; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
bool |
| 540 |
CharstringSubrChecker::check(const CharstringContext &g, ErrorHandler *errh) |
| 541 |
{ |
| 542 |
CharstringCheckerErrorHandler<CharstringSubrChecker> merrh(errh, this); |
| 543 |
_errh = &merrh; |
| 544 |
int old_errors = errh->nerrors(); |
| 545 |
|
| 546 |
_returned = false; |
| 547 |
|
| 548 |
CharstringInterp::interpret(g); |
| 549 |
|
| 550 |
if (!_returned) |
| 551 |
_errh->error("subroutine does not return"); |
| 552 |
return errh->nerrors() == old_errors; |
| 553 |
} |