| 1 |
/* t1testpage.cc -- driver for generating Type 1 fonts' test pages |
| 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 <efont/psres.hh> |
| 18 |
#include <efont/t1rw.hh> |
| 19 |
#include <efont/t1font.hh> |
| 20 |
#include <efont/t1item.hh> |
| 21 |
#include <efont/t1mm.hh> |
| 22 |
#include <efont/t1interp.hh> |
| 23 |
#include <efont/t1bounds.hh> |
| 24 |
#include <lcdf/clp.h> |
| 25 |
#include <lcdf/error.hh> |
| 26 |
#include <lcdf/globmatch.hh> |
| 27 |
#include <lcdf/transform.hh> |
| 28 |
#include <algorithm> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdarg.h> |
| 33 |
#include <ctype.h> |
| 34 |
#include <errno.h> |
| 35 |
#ifdef HAVE_CTIME |
| 36 |
# include <time.h> |
| 37 |
#endif |
| 38 |
#if defined(_MSDOS) || defined(_WIN32) |
| 39 |
# include <fcntl.h> |
| 40 |
# include <io.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
using namespace Efont; |
| 44 |
|
| 45 |
#define VERSION_OPT 301 |
| 46 |
#define HELP_OPT 302 |
| 47 |
#define OUTPUT_OPT 303 |
| 48 |
#define GLYPH_OPT 304 |
| 49 |
#define SMOKE_OPT 305 |
| 50 |
|
| 51 |
const Clp_Option options[] = { |
| 52 |
{ "help", 'h', HELP_OPT, 0, 0 }, |
| 53 |
{ "output", 'o', OUTPUT_OPT, Clp_ValString, 0 }, |
| 54 |
{ "version", 0, VERSION_OPT, 0, 0 }, |
| 55 |
{ "glyph", 'g', GLYPH_OPT, Clp_ValString, 0 }, |
| 56 |
{ "smoke", 's', SMOKE_OPT, 0, Clp_Negate } |
| 57 |
}; |
| 58 |
|
| 59 |
|
| 60 |
static const char *program_name; |
| 61 |
static PermString::Initializer initializer; |
| 62 |
static HashMap<PermString, int> glyph_order(-1); |
| 63 |
|
| 64 |
|
| 65 |
void |
| 66 |
usage_error(ErrorHandler *errh, const char *error_message, ...) |
| 67 |
{ |
| 68 |
va_list val; |
| 69 |
va_start(val, error_message); |
| 70 |
if (!error_message) |
| 71 |
errh->message("Usage: %s [OPTION]... FONT", program_name); |
| 72 |
else |
| 73 |
errh->xmessage(ErrorHandler::e_error, error_message, val); |
| 74 |
errh->message("Type %s --help for more information.", program_name); |
| 75 |
exit(1); |
| 76 |
va_end(val); |
| 77 |
} |
| 78 |
|
| 79 |
void |
| 80 |
usage() |
| 81 |
{ |
| 82 |
FileErrorHandler uerrh(stdout); |
| 83 |
uerrh.message("\ |
| 84 |
%<T1testpage%> creates a PostScript proof document for the specified Type 1\n\ |
| 85 |
font file and writes it to the standard output. The proof shows every\n\ |
| 86 |
glyph in the font, including its glyph name and encoding.\n\ |
| 87 |
\n\ |
| 88 |
Usage: %s [OPTION]... [FONT...]\n\ |
| 89 |
\n\ |
| 90 |
Each FONT is the name of a PFA or PFB font file. If omitted, t1testpage will\n\ |
| 91 |
read a font file from the standard input.\n\ |
| 92 |
\n\ |
| 93 |
Options:\n\ |
| 94 |
-g, --glyph=GLYPH Limit output to one or more GLYPHs.\n\ |
| 95 |
-s, --smoke Print smoke proofs, one character per page.\n\ |
| 96 |
-o, --output=FILE Write output to FILE instead of standard out.\n\ |
| 97 |
-h, --help Print this message and exit.\n\ |
| 98 |
--version Print version number and exit.\n\ |
| 99 |
\n\ |
| 100 |
Report bugs to <[email protected]>.\n", program_name); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
// OUTPUTTER |
| 105 |
|
| 106 |
namespace { |
| 107 |
|
| 108 |
class Testpager { public: |
| 109 |
Testpager(FILE *f) |
| 110 |
: _f(f), _pageno(1) { |
| 111 |
} |
| 112 |
virtual ~Testpager() { |
| 113 |
fclose(_f); |
| 114 |
} |
| 115 |
virtual void prolog(const Vector<Type1Font *> &fonts) = 0; |
| 116 |
void newpage() { |
| 117 |
fprintf(_f, "%%%%Page: %d %d\n", _pageno, _pageno); |
| 118 |
++_pageno; |
| 119 |
} |
| 120 |
virtual void font(Type1Font *font, const Vector<PermString>& glyph_names) = 0; |
| 121 |
virtual void epilog() { |
| 122 |
fprintf(_f, "%%%%EOF\n"); |
| 123 |
} |
| 124 |
protected: |
| 125 |
FILE *_f; |
| 126 |
int _pageno; |
| 127 |
}; |
| 128 |
|
| 129 |
class GridTestpager : public Testpager { public: |
| 130 |
GridTestpager(FILE *f) |
| 131 |
: Testpager(f) { |
| 132 |
} |
| 133 |
void prolog(const Vector<Type1Font *> &fonts); |
| 134 |
void font(Type1Font *font, const Vector<PermString>& glyph_names); |
| 135 |
}; |
| 136 |
|
| 137 |
class SmokeTestpager : public Testpager { public: |
| 138 |
SmokeTestpager(FILE *f) |
| 139 |
: Testpager(f) { |
| 140 |
} |
| 141 |
void prolog(const Vector<Type1Font *> &fonts); |
| 142 |
void font(Type1Font *font, const Vector<PermString>& glyph_names); |
| 143 |
}; |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
// MAIN |
| 149 |
|
| 150 |
static Type1Font * |
| 151 |
do_file(const char *filename, PsresDatabase *psres, ErrorHandler *errh) |
| 152 |
{ |
| 153 |
FILE *f; |
| 154 |
if (!filename || strcmp(filename, "-") == 0) { |
| 155 |
f = stdin; |
| 156 |
filename = "<stdin>"; |
| 157 |
#if defined(_MSDOS) || defined(_WIN32) |
| 158 |
_setmode(_fileno(f), _O_BINARY); |
| 159 |
#endif |
| 160 |
} else |
| 161 |
f = fopen(filename, "rb"); |
| 162 |
|
| 163 |
if (!f) { |
| 164 |
// check for PostScript name |
| 165 |
Filename fn = psres->filename_value("FontOutline", filename); |
| 166 |
f = fn.open_read(); |
| 167 |
} |
| 168 |
|
| 169 |
if (!f) |
| 170 |
errh->fatal("%s: %s", filename, strerror(errno)); |
| 171 |
|
| 172 |
Type1Reader *reader; |
| 173 |
int c = getc(f); |
| 174 |
ungetc(c, f); |
| 175 |
if (c == EOF) |
| 176 |
errh->fatal("%s: empty file", filename); |
| 177 |
if (c == 128) |
| 178 |
reader = new Type1PFBReader(f); |
| 179 |
else |
| 180 |
reader = new Type1PFAReader(f); |
| 181 |
|
| 182 |
Type1Font *font = new Type1Font(*reader); |
| 183 |
|
| 184 |
delete reader; |
| 185 |
return font; |
| 186 |
} |
| 187 |
|
| 188 |
void |
| 189 |
GridTestpager::prolog(const Vector<Type1Font *> &fonts) |
| 190 |
{ |
| 191 |
fprintf(_f, "%%!PS-Adobe-3.0\n\ |
| 192 |
%%%%LanguageLevel: 2\n\ |
| 193 |
%%%%DocumentMedia: Plain 612 792 white ( )\n\ |
| 194 |
%%%%BeginProlog\n"); |
| 195 |
fprintf(_f, "/magicstr 1 string def\n\ |
| 196 |
/magicbox { %% row col char name encoding magicbox -\n\ |
| 197 |
5 3 roll 54 mul 36 add exch 54 mul neg 682 add moveto currentpoint\n\ |
| 198 |
.8 setgray 54 0 rlineto 0 54 rlineto -54 0 rlineto closepath stroke\n\ |
| 199 |
0 setgray moveto\n\ |
| 200 |
gsave /Helvetica 7 selectfont 3 1.5 rmoveto show grestore\n\ |
| 201 |
gsave /Helvetica 7 selectfont 3 45.5 rmoveto show grestore\n\ |
| 202 |
magicstr 0 3 -1 roll put\n\ |
| 203 |
magicstr stringwidth pop 54 sub -2 div 16 rmoveto magicstr show\n\ |
| 204 |
} bind def\n"); |
| 205 |
Type1PFAWriter w(_f); |
| 206 |
for (Vector<Type1Font *>::const_iterator it = fonts.begin(); |
| 207 |
it != fonts.end(); ++it) |
| 208 |
(*it)->write(w); |
| 209 |
fprintf(_f, "%%%%EndProlog\n"); |
| 210 |
} |
| 211 |
|
| 212 |
void |
| 213 |
GridTestpager::font(Type1Font* font, const Vector<PermString>& glyph_names) |
| 214 |
{ |
| 215 |
HashMap<PermString, int> encodings(-1); |
| 216 |
if (Type1Encoding *encoding = font->type1_encoding()) |
| 217 |
for (int i = 255; i >= 0; i--) |
| 218 |
if (encoding->elt(i)) |
| 219 |
encodings.insert(encoding->elt(i), i); |
| 220 |
|
| 221 |
int per_row = 10; |
| 222 |
int nrows = 13; |
| 223 |
int per_page = nrows * per_row; |
| 224 |
|
| 225 |
int page = 0, gi = -1; |
| 226 |
for (Vector<PermString>::const_iterator it = glyph_names.begin(); |
| 227 |
it != glyph_names.end(); ++it) { |
| 228 |
// allow font that doesn't have all glyphs |
| 229 |
if (!font->glyph(*it)) |
| 230 |
continue; |
| 231 |
|
| 232 |
++gi; |
| 233 |
if (gi % per_page == 0) { |
| 234 |
if (page) |
| 235 |
fprintf(_f, "showpage restore\n"); |
| 236 |
++page; |
| 237 |
newpage(); |
| 238 |
fprintf(_f, "save\n"); |
| 239 |
// make new font |
| 240 |
fprintf(_f, "/%s findfont dup length dict begin\n\ |
| 241 |
{ 1 index /FID ne {def} {pop pop} ifelse } forall\n /Encoding [", |
| 242 |
font->font_name().c_str()); |
| 243 |
int gx = 0; |
| 244 |
for (Vector<PermString>::const_iterator xit = it; |
| 245 |
xit != glyph_names.end() && gx < per_page; ++xit) |
| 246 |
if (font->glyph(*xit)) { |
| 247 |
++gx; |
| 248 |
fprintf(_f, " /%s", xit->c_str()); |
| 249 |
if (gx % 10 == 9) |
| 250 |
fprintf(_f, "\n"); |
| 251 |
} |
| 252 |
fprintf(_f, " ] def\n currentdict end /X exch definefont pop\n\ |
| 253 |
/Helvetica-Bold 16 selectfont 36 742 moveto (%s) show\n\ |
| 254 |
/X 24 selectfont\n", font->font_name().c_str()); |
| 255 |
} |
| 256 |
|
| 257 |
int row = (gi % per_page) / per_row; |
| 258 |
int col = gi % per_row; |
| 259 |
|
| 260 |
fprintf(_f, "%d %d %d (%s)", row, col, gi % per_page, it->c_str()); |
| 261 |
if (encodings[*it] >= 0) { |
| 262 |
int e = encodings[*it]; |
| 263 |
if (e == '\\') |
| 264 |
fprintf(_f, " ('\\\\\\\\')"); |
| 265 |
else if (e == '\'') |
| 266 |
fprintf(_f, " ('\\\\'')"); |
| 267 |
else if (e == '(' || e == ')') |
| 268 |
fprintf(_f, " ('\\%c')", e); |
| 269 |
else if (e >= 32 && e < 127) |
| 270 |
fprintf(_f, " ('%c')", e); |
| 271 |
else |
| 272 |
fprintf(_f, " ('\\\\%03o')", e); |
| 273 |
} else |
| 274 |
fprintf(_f, " ()"); |
| 275 |
fprintf(_f, " magicbox\n"); |
| 276 |
} |
| 277 |
|
| 278 |
if (page) |
| 279 |
fprintf(_f, "showpage restore\n"); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
/***** |
| 285 |
* SMOKE PROOFS |
| 286 |
**/ |
| 287 |
|
| 288 |
inline StringAccum& |
| 289 |
operator<<(StringAccum& sa, const Point& p) |
| 290 |
{ |
| 291 |
return sa << p.x << ' ' << p.y; |
| 292 |
} |
| 293 |
|
| 294 |
class Smoker : public CharstringInterp { public: |
| 295 |
|
| 296 |
Smoker(const Transform&); |
| 297 |
|
| 298 |
void act_line(int, const Point &, const Point &); |
| 299 |
void act_curve(int, const Point &, const Point &, const Point &, const Point &); |
| 300 |
void act_closepath(int); |
| 301 |
|
| 302 |
String char_postscript() { return _char_sa.take_string(); } |
| 303 |
String points_postscript() { return _points_sa.take_string(); } |
| 304 |
|
| 305 |
bool run(const CharstringContext&); |
| 306 |
|
| 307 |
private: |
| 308 |
|
| 309 |
Transform _xform; |
| 310 |
StringAccum _char_sa; |
| 311 |
StringAccum _points_sa; |
| 312 |
Point _char_cp; |
| 313 |
|
| 314 |
inline void maybe_move(const Point&); |
| 315 |
|
| 316 |
}; |
| 317 |
|
| 318 |
Smoker::Smoker(const Transform& xform) |
| 319 |
: _xform(xform) |
| 320 |
{ |
| 321 |
} |
| 322 |
|
| 323 |
void |
| 324 |
Smoker::maybe_move(const Point& p) |
| 325 |
{ |
| 326 |
if (_char_cp.x < -100000 || _char_cp != p) { |
| 327 |
_char_sa << (p * _xform) << " moveto\n"; |
| 328 |
_points_sa << (p * _xform) << " pA\n"; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
void |
| 333 |
Smoker::act_line(int cmd, const Point& p0, const Point& p1) |
| 334 |
{ |
| 335 |
Point xp1 = p1 * _xform; |
| 336 |
|
| 337 |
maybe_move(p0); |
| 338 |
_char_sa << xp1 << " lineto\n"; |
| 339 |
if (cmd == Charstring::cRlineto |
| 340 |
|| cmd == Charstring::cHlineto |
| 341 |
|| cmd == Charstring::cVlineto) |
| 342 |
_points_sa << xp1 << " pA\n"; |
| 343 |
_char_cp = p1; |
| 344 |
} |
| 345 |
|
| 346 |
void |
| 347 |
Smoker::act_curve(int, const Point &p0, const Point &p1, const Point &p2, const Point &p3) |
| 348 |
{ |
| 349 |
Point xp1 = p1 * _xform; |
| 350 |
Point xp2 = p2 * _xform; |
| 351 |
Point xp3 = p3 * _xform; |
| 352 |
|
| 353 |
maybe_move(p0); |
| 354 |
_char_sa << xp1 << ' ' << xp2 << ' ' << xp3 << " curveto\n"; |
| 355 |
_points_sa << xp1 << " pC " << xp2 << " pC " << xp3 << " pA\n"; |
| 356 |
_char_cp = p3; |
| 357 |
} |
| 358 |
|
| 359 |
void |
| 360 |
Smoker::act_closepath(int) |
| 361 |
{ |
| 362 |
_char_sa << "closepath\n"; |
| 363 |
} |
| 364 |
|
| 365 |
bool |
| 366 |
Smoker::run(const CharstringContext& g) |
| 367 |
{ |
| 368 |
_char_sa.clear(); |
| 369 |
_points_sa.clear(); |
| 370 |
_char_cp.x = -200000; |
| 371 |
return CharstringInterp::interpret(g); |
| 372 |
} |
| 373 |
|
| 374 |
#define LEFT_BOUND 72. |
| 375 |
#define RIGHT_BOUND (7.5 * 72) |
| 376 |
#define BOTTOM_BOUND 72. |
| 377 |
#define TOP_BOUND (7.5 * 72) |
| 378 |
|
| 379 |
static Transform |
| 380 |
bounds2xform(CharstringBounds& bounds, bool expand = false) |
| 381 |
{ |
| 382 |
if (expand) { |
| 383 |
Point vec = bounds.bb_top_right() - bounds.bb_bottom_left(); |
| 384 |
bounds.act_line(0, bounds.bb_bottom_left() - 0.1 * vec, bounds.bb_top_right() + 0.1 * vec); |
| 385 |
} |
| 386 |
bounds.act_line(0, Point(0, 0), bounds.width()); |
| 387 |
bounds.act_line(0, Point(0, 0), Point(1, 1)); |
| 388 |
|
| 389 |
double true_width = std::max(bounds.bb_right(), 0.) - std::min(bounds.bb_left(), 0.); |
| 390 |
double true_height = std::max(bounds.bb_top(), 0.) - std::min(bounds.bb_bottom(), 0.); |
| 391 |
double x_scale = (RIGHT_BOUND - LEFT_BOUND) / true_width; |
| 392 |
double y_scale = (TOP_BOUND - BOTTOM_BOUND) / true_height; |
| 393 |
double scale = std::min(x_scale, y_scale); |
| 394 |
double origin_x = LEFT_BOUND; |
| 395 |
if (bounds.bb_left() < 0) |
| 396 |
origin_x += -bounds.bb_left() * scale; |
| 397 |
double origin_y = BOTTOM_BOUND; |
| 398 |
if (bounds.bb_bottom() < 0) |
| 399 |
origin_y += -bounds.bb_bottom() * scale; |
| 400 |
return Transform(scale, 0, 0, scale, origin_x, origin_y); |
| 401 |
} |
| 402 |
|
| 403 |
static const char* const bounds_glyphs[] = { |
| 404 |
"A", "Eacute", "Ecircumflex", "l", "g", "p", "q", "j", "J", "emdash", 0 |
| 405 |
}; |
| 406 |
|
| 407 |
void |
| 408 |
SmokeTestpager::prolog(const Vector<Type1Font *> &) |
| 409 |
{ |
| 410 |
fprintf(_f, "%%!PS-Adobe-3.0\n%%%%LanguageLevel: 2\n%%%%BeginProlog\n"); |
| 411 |
fprintf(_f, "/pA { %% x y pA -\n\ |
| 412 |
moveto\n\ |
| 413 |
-2 -2 rmoveto 4 0 rlineto 0 4 rlineto -4 0 rlineto closepath\n\ |
| 414 |
gsave 1 setgray stroke grestore 0 setgray fill\n\ |
| 415 |
} bind def\n\ |
| 416 |
/pC { %% x y pC -\n\ |
| 417 |
2 copy moveto 2 0 rmoveto 2 0 360 arc\n\ |
| 418 |
gsave 1 setgray stroke grestore 0.1 setgray fill\n\ |
| 419 |
} bind def\n\ |
| 420 |
"); |
| 421 |
fprintf(_f, "%%%%EndProlog\n"); |
| 422 |
} |
| 423 |
|
| 424 |
void |
| 425 |
SmokeTestpager::font(Type1Font* font, const Vector<PermString>& glyph_names) |
| 426 |
{ |
| 427 |
HashMap<PermString, int> encodings(-1); |
| 428 |
if (Type1Encoding *encoding = font->type1_encoding()) |
| 429 |
for (int i = 255; i >= 0; i--) |
| 430 |
encodings.insert(encoding->elt(i), i); |
| 431 |
|
| 432 |
// First, calculate font bounds, and from there, the transform. |
| 433 |
CharstringBounds bounds; |
| 434 |
for (const char* const* bg = bounds_glyphs; *bg; bg++) |
| 435 |
if (CharstringContext cc = font->glyph_context(*bg)) |
| 436 |
bounds.char_bounds(cc, false); |
| 437 |
if (bounds.bb_left() == bounds.bb_right()) |
| 438 |
for (int gi = 0; gi < glyph_names.size(); gi++) |
| 439 |
bounds.char_bounds(font->glyph_context(glyph_names[gi]), false); |
| 440 |
Transform font_xform = bounds2xform(bounds, true); |
| 441 |
|
| 442 |
for (int gi = 0; gi < glyph_names.size(); gi++) { |
| 443 |
// allow font that doesn't have all glyphs |
| 444 |
if (!font->glyph(glyph_names[gi])) |
| 445 |
continue; |
| 446 |
|
| 447 |
newpage(); |
| 448 |
fprintf(_f, "save\n"); |
| 449 |
|
| 450 |
CharstringContext cc = font->glyph_context(glyph_names[gi]); |
| 451 |
bounds.clear(); |
| 452 |
bounds.char_bounds(cc, false); |
| 453 |
|
| 454 |
Transform xform(font_xform); |
| 455 |
if (bounds.bb_known()) { |
| 456 |
Point lb = bounds.bb_bottom_left() * xform; |
| 457 |
Point rt = bounds.bb_top_right() * xform; |
| 458 |
Point wd = bounds.width() * xform; |
| 459 |
if (lb.x < LEFT_BOUND || lb.y < BOTTOM_BOUND |
| 460 |
|| rt.x > RIGHT_BOUND || rt.y > TOP_BOUND |
| 461 |
|| wd.x < LEFT_BOUND || wd.x > RIGHT_BOUND |
| 462 |
|| wd.y < BOTTOM_BOUND || wd.y > TOP_BOUND) |
| 463 |
xform = bounds2xform(bounds); |
| 464 |
} |
| 465 |
|
| 466 |
Smoker smoker(xform); |
| 467 |
smoker.run(font->glyph_context(glyph_names[gi])); |
| 468 |
fprintf(_f, "%g %g moveto 0 %g rlineto %g %g moveto 0 %g rlineto %g %g moveto %g 0 rlineto 0 setgray stroke\n", |
| 469 |
(Point(0, 0) * xform).x, BOTTOM_BOUND - 36, TOP_BOUND - BOTTOM_BOUND + 72, |
| 470 |
(bounds.width() * xform).x, BOTTOM_BOUND - 36, TOP_BOUND - BOTTOM_BOUND + 72, |
| 471 |
LEFT_BOUND - 36, (Point(0, 0) * xform).y, RIGHT_BOUND - LEFT_BOUND + 72); |
| 472 |
|
| 473 |
fprintf(_f, "%s\n0.5 setgray fill\n\n1 setlinewidth\n%s\nshowpage\nrestore\n", smoker.char_postscript().c_str(), smoker.points_postscript().c_str()); |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
/***** |
| 479 |
* MAIN PROGRAM |
| 480 |
**/ |
| 481 |
|
| 482 |
int |
| 483 |
click_strcmp(PermString a, PermString b) |
| 484 |
{ |
| 485 |
const char *ad = a.c_str(), *ae = a.c_str() + a.length(); |
| 486 |
const char *bd = b.c_str(), *be = b.c_str() + b.length(); |
| 487 |
|
| 488 |
while (ad < ae && bd < be) { |
| 489 |
if (isdigit((unsigned char) *ad) && isdigit((unsigned char) *bd)) { |
| 490 |
// compare the two numbers, but don't treat them as numbers in |
| 491 |
// case of overflow |
| 492 |
// first, skip initial '0's |
| 493 |
const char *iad = ad, *ibd = bd; |
| 494 |
while (ad < ae && *ad == '0') |
| 495 |
ad++; |
| 496 |
while (bd < be && *bd == '0') |
| 497 |
bd++; |
| 498 |
int longer_zeros = (ad - iad) - (bd - ibd); |
| 499 |
// skip to end of number |
| 500 |
const char *nad = ad, *nbd = bd; |
| 501 |
while (ad < ae && isdigit((unsigned char) *ad)) |
| 502 |
ad++; |
| 503 |
while (bd < be && isdigit((unsigned char) *bd)) |
| 504 |
bd++; |
| 505 |
// longer number must be larger |
| 506 |
if ((ad - nad) != (bd - nbd)) |
| 507 |
return (ad - nad) - (bd - nbd); |
| 508 |
// otherwise, compare numbers with the same length |
| 509 |
for (; nad < ad && nbd < bd; nad++, nbd++) |
| 510 |
if (*nad != *nbd) |
| 511 |
return *nad - *nbd; |
| 512 |
// finally, longer string of initial '0's wins |
| 513 |
if (longer_zeros != 0) |
| 514 |
return longer_zeros; |
| 515 |
} else if (isdigit((unsigned char) *ad)) |
| 516 |
return (isalpha((unsigned char) *bd) ? -1 : 1); |
| 517 |
else if (isdigit((unsigned char) *bd)) |
| 518 |
return (isalpha((unsigned char) *ad) ? 1 : -1); |
| 519 |
else { |
| 520 |
int d = tolower((unsigned char) *ad) - tolower((unsigned char) *bd); |
| 521 |
if (d != 0) |
| 522 |
return d; |
| 523 |
ad++; |
| 524 |
bd++; |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
if ((ae - ad) != (be - bd)) |
| 529 |
return (ae - ad) - (be - bd); |
| 530 |
else { |
| 531 |
assert(a.length() == b.length()); |
| 532 |
return memcmp(a.c_str(), b.c_str(), a.length()); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
extern "C" { |
| 537 |
static int CDECL |
| 538 |
glyphcompare(const void *lv, const void *rv) |
| 539 |
{ |
| 540 |
const PermString* ln = (const PermString*)lv; |
| 541 |
const PermString* rn = (const PermString*)rv; |
| 542 |
|
| 543 |
// try first without the '.'s |
| 544 |
const char* ldot = strchr(ln->c_str(), '.'); |
| 545 |
const char* rdot = strchr(rn->c_str(), '.'); |
| 546 |
if (ldot == ln->begin()) |
| 547 |
ldot = 0; |
| 548 |
if (rdot == rn->begin()) |
| 549 |
rdot = 0; |
| 550 |
if (ldot || rdot) { |
| 551 |
PermString l(ln->begin(), ldot ? ldot : ln->end()); |
| 552 |
PermString r(rn->begin(), rdot ? rdot : rn->end()); |
| 553 |
int diff = glyphcompare(&l, &r); |
| 554 |
if (diff != 0) |
| 555 |
return diff; |
| 556 |
} |
| 557 |
|
| 558 |
int lorder = glyph_order[*ln]; |
| 559 |
int rorder = glyph_order[*rn]; |
| 560 |
if (lorder >= 0 && rorder >= 0) |
| 561 |
return lorder - rorder; |
| 562 |
else if (lorder >= 0) |
| 563 |
return -1; |
| 564 |
else if (rorder >= 0) |
| 565 |
return 1; |
| 566 |
else |
| 567 |
return click_strcmp(*ln, *rn); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
static bool |
| 572 |
glyph_matches(const String& glyph_name, const String* pattern_begin, const String* pattern_end) |
| 573 |
{ |
| 574 |
if (pattern_begin >= pattern_end) |
| 575 |
return true; |
| 576 |
for (; pattern_begin < pattern_end; pattern_begin++) |
| 577 |
if (glob_match(glyph_name, *pattern_begin)) |
| 578 |
return true; |
| 579 |
return false; |
| 580 |
} |
| 581 |
|
| 582 |
int |
| 583 |
main(int argc, char *argv[]) |
| 584 |
{ |
| 585 |
PsresDatabase *psres = new PsresDatabase; |
| 586 |
psres->add_psres_path(getenv("PSRESOURCEPATH"), 0, false); |
| 587 |
|
| 588 |
Clp_Parser *clp = |
| 589 |
Clp_NewParser(argc, (const char * const *)argv, sizeof(options) / sizeof(options[0]), options); |
| 590 |
program_name = Clp_ProgramName(clp); |
| 591 |
|
| 592 |
ErrorHandler *errh = ErrorHandler::static_initialize(new FileErrorHandler(stderr)); |
| 593 |
const char *output_file = 0; |
| 594 |
Vector<String> glyph_patterns; |
| 595 |
bool smoke = false; |
| 596 |
Vector<Type1Font *> fonts; |
| 597 |
|
| 598 |
while (1) { |
| 599 |
int opt = Clp_Next(clp); |
| 600 |
switch (opt) { |
| 601 |
|
| 602 |
case GLYPH_OPT: { |
| 603 |
const char* s = clp->vstr, *end = s + strlen(s); |
| 604 |
while (s < end) { |
| 605 |
while (s < end && isspace((unsigned char) *s)) |
| 606 |
s++; |
| 607 |
const char* word = s; |
| 608 |
while (s < end && !isspace((unsigned char) *s)) |
| 609 |
s++; |
| 610 |
if (word < s) |
| 611 |
glyph_patterns.push_back(String(word, s - word)); |
| 612 |
} |
| 613 |
break; |
| 614 |
} |
| 615 |
|
| 616 |
case SMOKE_OPT: |
| 617 |
smoke = !clp->negated; |
| 618 |
break; |
| 619 |
|
| 620 |
case OUTPUT_OPT: |
| 621 |
if (output_file) |
| 622 |
errh->fatal("output file already specified"); |
| 623 |
output_file = clp->vstr; |
| 624 |
break; |
| 625 |
|
| 626 |
case VERSION_OPT: |
| 627 |
printf("t1testpage (LCDF typetools) %s\n", VERSION); |
| 628 |
printf("Copyright (C) 1999-2023 Eddie Kohler\n\ |
| 629 |
This is free software; see the source for copying conditions.\n\ |
| 630 |
There is NO warranty, not even for merchantability or fitness for a\n\ |
| 631 |
particular purpose.\n"); |
| 632 |
exit(0); |
| 633 |
break; |
| 634 |
|
| 635 |
case HELP_OPT: |
| 636 |
usage(); |
| 637 |
exit(0); |
| 638 |
break; |
| 639 |
|
| 640 |
case Clp_NotOption: |
| 641 |
fonts.push_back(do_file(clp->vstr, psres, errh)); |
| 642 |
break; |
| 643 |
|
| 644 |
case Clp_Done: |
| 645 |
goto done; |
| 646 |
|
| 647 |
case Clp_BadOption: |
| 648 |
usage_error(errh, 0); |
| 649 |
break; |
| 650 |
|
| 651 |
default: |
| 652 |
break; |
| 653 |
|
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
done: |
| 658 |
if (!fonts.size()) |
| 659 |
fonts.push_back(do_file(0, psres, errh)); |
| 660 |
|
| 661 |
FILE *outf; |
| 662 |
if (!output_file || strcmp(output_file, "-") == 0) |
| 663 |
outf = stdout; |
| 664 |
else { |
| 665 |
outf = fopen(output_file, "w"); |
| 666 |
if (!outf) |
| 667 |
errh->fatal("%s: %s", output_file, strerror(errno)); |
| 668 |
} |
| 669 |
#if defined(_MSDOS) || defined(_WIN32) |
| 670 |
_setmode(_fileno(outf), _O_BINARY); |
| 671 |
#endif |
| 672 |
|
| 673 |
//font->undo_synthetic(); |
| 674 |
|
| 675 |
// Prepare glyph order table |
| 676 |
int gindex = 0; |
| 677 |
char buf[7] = "Asmall"; |
| 678 |
for (int c = 0; c < 26; c++) { |
| 679 |
buf[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[c]; |
| 680 |
glyph_order.insert(PermString(buf[0]), gindex++); |
| 681 |
glyph_order.insert(PermString("abcdefghijklmnopqrstuvwxyz"[c]), gindex++); |
| 682 |
glyph_order.insert(PermString(buf), gindex++); |
| 683 |
} |
| 684 |
glyph_order.insert("parenleft", gindex++); |
| 685 |
glyph_order.insert("period", gindex++); |
| 686 |
glyph_order.insert("comma", gindex++); |
| 687 |
glyph_order.insert("hyphen", gindex++); |
| 688 |
glyph_order.insert("ampersand", gindex++); |
| 689 |
glyph_order.insert("semicolon", gindex++); |
| 690 |
glyph_order.insert("exclamation", gindex++); |
| 691 |
glyph_order.insert("question", gindex++); |
| 692 |
glyph_order.insert("parenright", gindex++); |
| 693 |
glyph_order.insert("zero", gindex++); |
| 694 |
glyph_order.insert("one", gindex++); |
| 695 |
glyph_order.insert("two", gindex++); |
| 696 |
glyph_order.insert("three", gindex++); |
| 697 |
glyph_order.insert("four", gindex++); |
| 698 |
glyph_order.insert("five", gindex++); |
| 699 |
glyph_order.insert("six", gindex++); |
| 700 |
glyph_order.insert("seven", gindex++); |
| 701 |
glyph_order.insert("eight", gindex++); |
| 702 |
glyph_order.insert("nine", gindex++); |
| 703 |
glyph_order.insert("zerooldstyle", gindex++); |
| 704 |
glyph_order.insert("oneoldstyle", gindex++); |
| 705 |
glyph_order.insert("twooldstyle", gindex++); |
| 706 |
glyph_order.insert("threeoldstyle", gindex++); |
| 707 |
glyph_order.insert("fouroldstyle", gindex++); |
| 708 |
glyph_order.insert("fiveoldstyle", gindex++); |
| 709 |
glyph_order.insert("sixoldstyle", gindex++); |
| 710 |
glyph_order.insert("sevenoldstyle", gindex++); |
| 711 |
glyph_order.insert("eightoldstyle", gindex++); |
| 712 |
glyph_order.insert("nineoldstyle", gindex++); |
| 713 |
glyph_order.insert(".notdef", gindex++); |
| 714 |
glyph_order.insert("space", gindex++); |
| 715 |
|
| 716 |
// Get glyph names. |
| 717 |
HashMap<PermString, int> glyph_hash(0); |
| 718 |
for (Vector<Type1Font *>::iterator it = fonts.begin(); it != fonts.end(); |
| 719 |
++it) |
| 720 |
for (int i = 0; i < (*it)->nglyphs(); i++) { |
| 721 |
if (glyph_matches((*it)->glyph_name(i), |
| 722 |
glyph_patterns.begin(), glyph_patterns.end())) |
| 723 |
glyph_hash.insert((*it)->glyph_name(i), 1); |
| 724 |
} |
| 725 |
|
| 726 |
Vector<PermString> glyph_names; |
| 727 |
for (HashMap<PermString, int>::iterator it = glyph_hash.begin(); |
| 728 |
it != glyph_hash.end(); ++it) |
| 729 |
glyph_names.push_back(it.key()); |
| 730 |
if (glyph_names.size() == 0) |
| 731 |
errh->fatal("no glyphs to print"); |
| 732 |
qsort(glyph_names.begin(), glyph_names.size(), sizeof(PermString), glyphcompare); |
| 733 |
|
| 734 |
// outputs |
| 735 |
Testpager *tp; |
| 736 |
if (smoke) |
| 737 |
tp = new SmokeTestpager(outf); |
| 738 |
else |
| 739 |
tp = new GridTestpager(outf); |
| 740 |
|
| 741 |
tp->prolog(fonts); |
| 742 |
for (Vector<Type1Font *>::iterator it = fonts.begin(); it != fonts.end(); |
| 743 |
++it) |
| 744 |
tp->font(*it, glyph_names); |
| 745 |
tp->epilog(); |
| 746 |
|
| 747 |
delete tp; |
| 748 |
exit(0); |
| 749 |
} |