| 1 |
/* t1dotlessj.cc -- driver for creating dotlessj characters from Type 1 fonts |
| 2 |
* |
| 3 |
* Copyright (c) 2003-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/t1csgen.hh> |
| 22 |
#include <efont/t1bounds.hh> |
| 23 |
#include <efont/t1unparser.hh> |
| 24 |
#include <lcdf/clp.h> |
| 25 |
#include <lcdf/error.hh> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdarg.h> |
| 30 |
#include <ctype.h> |
| 31 |
#include <errno.h> |
| 32 |
#ifdef HAVE_CTIME |
| 33 |
# include <time.h> |
| 34 |
#endif |
| 35 |
#if defined(_MSDOS) || defined(_WIN32) |
| 36 |
# include <fcntl.h> |
| 37 |
# include <io.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
// also see otftotfm/automatic.cc |
| 41 |
enum { EXIT_NORMAL = 0, EXIT_DOTLESSJ_EXISTS = 1, EXIT_J_NODOT = 2, |
| 42 |
EXIT_NO_J = 3, EXIT_ERROR = 4 }; |
| 43 |
|
| 44 |
using namespace Efont; |
| 45 |
|
| 46 |
#define VERSION_OPT 301 |
| 47 |
#define HELP_OPT 302 |
| 48 |
#define QUIET_OPT 303 |
| 49 |
#define PFA_OPT 304 |
| 50 |
#define PFB_OPT 305 |
| 51 |
#define OUTPUT_OPT 306 |
| 52 |
#define NAME_OPT 307 |
| 53 |
|
| 54 |
const Clp_Option options[] = { |
| 55 |
{ "help", 'h', HELP_OPT, 0, 0 }, |
| 56 |
{ "name", 'n', NAME_OPT, Clp_ValString, 0 }, |
| 57 |
{ "output", 'o', OUTPUT_OPT, Clp_ValString, 0 }, |
| 58 |
{ "pfa", 'a', PFA_OPT, 0, 0 }, |
| 59 |
{ "pfb", 'b', PFB_OPT, 0, 0 }, |
| 60 |
{ "quiet", 'q', QUIET_OPT, 0, Clp_Negate }, |
| 61 |
{ "version", 0, VERSION_OPT, 0, 0 }, |
| 62 |
}; |
| 63 |
|
| 64 |
|
| 65 |
static const char *program_name; |
| 66 |
|
| 67 |
|
| 68 |
void |
| 69 |
usage_error(ErrorHandler *errh, const char *error_message, ...) |
| 70 |
{ |
| 71 |
va_list val; |
| 72 |
va_start(val, error_message); |
| 73 |
if (!error_message) |
| 74 |
errh->message("Usage: %s [OPTIONS] [FONTFILE [OUTPUTFILE]]", program_name); |
| 75 |
else |
| 76 |
errh->xmessage(ErrorHandler::e_error, error_message, val); |
| 77 |
errh->message("Type %s --help for more information.", program_name); |
| 78 |
exit(EXIT_ERROR); |
| 79 |
va_end(val); |
| 80 |
} |
| 81 |
|
| 82 |
void |
| 83 |
usage() |
| 84 |
{ |
| 85 |
FileErrorHandler uerrh(stdout); |
| 86 |
uerrh.message("\ |
| 87 |
%<T1dotlessj%> reads a PostScript Type 1 font, derives a new PostScript Type 1\n\ |
| 88 |
font containing just a dotlessj character (by chopping the dot from the j),\n\ |
| 89 |
and writes it to the standard output.\n\ |
| 90 |
\n\ |
| 91 |
Usage: %s [OPTIONS] [FONTFILE [OUTPUTFILE]]\n\ |
| 92 |
\n\ |
| 93 |
Options:\n\ |
| 94 |
-a, --pfa Output PFA font.\n\ |
| 95 |
-b, --pfb Output PFB font. This is the default.\n\ |
| 96 |
-o, --output=FILE Write output to FILE instead of standard output.\n\ |
| 97 |
-n, --name=NAME Set output font%,s PostScript name.\n\ |
| 98 |
-h, --help Print this message and exit.\n\ |
| 99 |
-q, --quiet Do not report errors to standard error.\n\ |
| 100 |
--version Print version number and exit.\n\ |
| 101 |
\n\ |
| 102 |
Report bugs to <[email protected]>.\n", program_name); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
// Sectioner |
| 107 |
|
| 108 |
class Sectioner : public Type1CharstringGenInterp { public: |
| 109 |
|
| 110 |
Sectioner(int precision); |
| 111 |
|
| 112 |
void act_line(int, const Point &, const Point &); |
| 113 |
void act_curve(int, const Point &, const Point &, const Point &, const Point &); |
| 114 |
void act_closepath(int); |
| 115 |
void act_flex(int, const Point &, const Point &, const Point &, const Point &, const Point &, const Point &, const Point &, double); |
| 116 |
|
| 117 |
void run(const CharstringContext &g); |
| 118 |
void undot(PermString, ErrorHandler *); |
| 119 |
Type1Charstring gen(Type1Font *); |
| 120 |
|
| 121 |
private: |
| 122 |
|
| 123 |
CharstringBounds _boundser; |
| 124 |
|
| 125 |
Vector<String> _sections; |
| 126 |
Vector<int> _bounds; |
| 127 |
|
| 128 |
void append_bounds(); |
| 129 |
|
| 130 |
}; |
| 131 |
|
| 132 |
Sectioner::Sectioner(int precision) |
| 133 |
: Type1CharstringGenInterp(precision) |
| 134 |
{ |
| 135 |
set_direct_hint_replacement(true); |
| 136 |
} |
| 137 |
|
| 138 |
void |
| 139 |
Sectioner::act_line(int cmd, const Point &p0, const Point &p1) |
| 140 |
{ |
| 141 |
Type1CharstringGenInterp::act_line(cmd, p0, p1); |
| 142 |
_boundser.act_line(cmd, p0, p1); |
| 143 |
} |
| 144 |
|
| 145 |
void |
| 146 |
Sectioner::act_curve(int cmd, const Point &p0, const Point &p1, const Point &p2, const Point &p3) |
| 147 |
{ |
| 148 |
Type1CharstringGenInterp::act_curve(cmd, p0, p1, p2, p3); |
| 149 |
_boundser.act_curve(cmd, p0, p1, p2, p3); |
| 150 |
} |
| 151 |
|
| 152 |
void |
| 153 |
Sectioner::act_flex(int cmd, const Point &p0, const Point &p1, const Point &p2, const Point &p3_4, const Point &p5, const Point &p6, const Point &p7, double flex_depth) |
| 154 |
{ |
| 155 |
Type1CharstringGenInterp::act_flex(cmd, p0, p1, p2, p3_4, p5, p6, p7, flex_depth); |
| 156 |
_boundser.act_flex(cmd, p0, p1, p2, p3_4, p5, p6, p7, flex_depth); |
| 157 |
} |
| 158 |
|
| 159 |
void Sectioner::append_bounds() { |
| 160 |
double bb[5]; |
| 161 |
_boundser.output(bb, bb[4]); |
| 162 |
_bounds.push_back((int) floor(bb[0])); |
| 163 |
_bounds.push_back((int) floor(bb[1])); |
| 164 |
_bounds.push_back((int) ceil(bb[2])); |
| 165 |
_bounds.push_back((int) ceil(bb[3])); |
| 166 |
} |
| 167 |
|
| 168 |
void |
| 169 |
Sectioner::act_closepath(int cmd) |
| 170 |
{ |
| 171 |
Type1CharstringGenInterp::act_closepath(cmd); |
| 172 |
Type1Charstring result; |
| 173 |
Type1CharstringGenInterp::intermediate_output(result); |
| 174 |
_sections.push_back(result.data_string()); |
| 175 |
append_bounds(); |
| 176 |
_boundser.clear(); |
| 177 |
} |
| 178 |
|
| 179 |
void |
| 180 |
Sectioner::run(const CharstringContext &g) |
| 181 |
{ |
| 182 |
_boundser.clear(); |
| 183 |
Type1Charstring last_section; |
| 184 |
Type1CharstringGenInterp::run(g, last_section); |
| 185 |
_sections.push_back(last_section.data_string()); |
| 186 |
append_bounds(); |
| 187 |
} |
| 188 |
|
| 189 |
void |
| 190 |
Sectioner::undot(PermString font_name, ErrorHandler *errh) |
| 191 |
{ |
| 192 |
//for (String *s = _sections.begin(); s < _sections.end(); s++) |
| 193 |
// fprintf(stderr, "%d %s\n", s - _sections.begin(), CharstringUnparser::unparse(Type1Charstring(*s)).c_str()); |
| 194 |
|
| 195 |
if (_sections.size() < 3) |
| 196 |
errh->fatal("<%d>%s: %<j%> is already dotless", -EXIT_J_NODOT, font_name.c_str()); |
| 197 |
|
| 198 |
int topmost = -1; |
| 199 |
for (int i = 0; i < _sections.size() - 1; i++) |
| 200 |
if (topmost < 0 || _bounds[i*4 + 1] > _bounds[topmost*4 + 1]) |
| 201 |
topmost = i; |
| 202 |
|
| 203 |
// check if any sections are below this |
| 204 |
for (int i = 0; i < _sections.size() - 1; i++) |
| 205 |
if (_bounds[i*4 + 1] < _bounds[topmost*4 + 1]) |
| 206 |
goto found_below; |
| 207 |
errh->fatal("<%d>%s: %<j%> is already dotless", -EXIT_J_NODOT, font_name.c_str()); |
| 208 |
|
| 209 |
found_below: |
| 210 |
_sections[topmost] = String(); |
| 211 |
} |
| 212 |
|
| 213 |
Type1Charstring |
| 214 |
Sectioner::gen(Type1Font *font) |
| 215 |
{ |
| 216 |
StringAccum sa; |
| 217 |
for (String *s = _sections.begin(); s < _sections.end(); s++) |
| 218 |
sa << *s; |
| 219 |
Type1Charstring in(sa.take_string()), out; |
| 220 |
Type1CharstringGenInterp gen(precision()); |
| 221 |
gen.set_hint_replacement_storage(font); |
| 222 |
gen.run(CharstringContext(program(), &in), out); |
| 223 |
return out; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
// MAIN |
| 228 |
|
| 229 |
static Type1Font * |
| 230 |
do_file(const char *filename, PsresDatabase *psres, ErrorHandler *errh) |
| 231 |
{ |
| 232 |
FILE *f; |
| 233 |
if (!filename || strcmp(filename, "-") == 0) { |
| 234 |
f = stdin; |
| 235 |
filename = "<stdin>"; |
| 236 |
#if defined(_MSDOS) || defined(_WIN32) |
| 237 |
_setmode(_fileno(f), _O_BINARY); |
| 238 |
#endif |
| 239 |
} else |
| 240 |
f = fopen(filename, "rb"); |
| 241 |
|
| 242 |
if (!f) { |
| 243 |
// check for PostScript name |
| 244 |
Filename fn = psres->filename_value("FontOutline", filename); |
| 245 |
f = fn.open_read(); |
| 246 |
} |
| 247 |
|
| 248 |
if (!f) |
| 249 |
errh->fatal("<%d>%s: %s", -EXIT_ERROR, filename, strerror(errno)); |
| 250 |
|
| 251 |
Type1Reader *reader; |
| 252 |
int c = getc(f); |
| 253 |
ungetc(c, f); |
| 254 |
if (c == EOF) |
| 255 |
errh->fatal("<%d>%s: empty file", -EXIT_ERROR, filename); |
| 256 |
if (c == 128) |
| 257 |
reader = new Type1PFBReader(f); |
| 258 |
else |
| 259 |
reader = new Type1PFAReader(f); |
| 260 |
|
| 261 |
Type1Font *font = new Type1Font(*reader); |
| 262 |
if (!font->ok()) |
| 263 |
errh->fatal("<%d>%s: no glyphs in font", -EXIT_ERROR, filename); |
| 264 |
|
| 265 |
delete reader; |
| 266 |
return font; |
| 267 |
} |
| 268 |
|
| 269 |
int |
| 270 |
main(int argc, char *argv[]) |
| 271 |
{ |
| 272 |
PsresDatabase *psres = new PsresDatabase; |
| 273 |
psres->add_psres_path(getenv("PSRESOURCEPATH"), 0, false); |
| 274 |
|
| 275 |
Clp_Parser *clp = |
| 276 |
Clp_NewParser(argc, (const char * const *)argv, sizeof(options) / sizeof(options[0]), options); |
| 277 |
program_name = Clp_ProgramName(clp); |
| 278 |
|
| 279 |
ErrorHandler *errh = ErrorHandler::static_initialize(new FileErrorHandler(stderr)); |
| 280 |
const char *input_file = 0; |
| 281 |
FILE *outputf = 0; |
| 282 |
const char *private_use_dotlessj = "uniF6BE"; |
| 283 |
bool binary = true; |
| 284 |
const char *font_name = 0; |
| 285 |
|
| 286 |
while (1) { |
| 287 |
int opt = Clp_Next(clp); |
| 288 |
switch (opt) { |
| 289 |
|
| 290 |
case QUIET_OPT: |
| 291 |
if (clp->negated) |
| 292 |
errh = ErrorHandler::default_handler(); |
| 293 |
else |
| 294 |
errh = new SilentErrorHandler; |
| 295 |
break; |
| 296 |
|
| 297 |
case NAME_OPT: |
| 298 |
font_name = clp->vstr; |
| 299 |
break; |
| 300 |
|
| 301 |
case PFA_OPT: |
| 302 |
binary = false; |
| 303 |
break; |
| 304 |
|
| 305 |
case PFB_OPT: |
| 306 |
binary = true; |
| 307 |
break; |
| 308 |
|
| 309 |
case OUTPUT_OPT: |
| 310 |
output_file: |
| 311 |
if (outputf) |
| 312 |
usage_error(errh, "output file specified twice"); |
| 313 |
if (strcmp(clp->vstr, "-") == 0) |
| 314 |
outputf = stdout; |
| 315 |
else if (!(outputf = fopen(clp->vstr, "wb"))) |
| 316 |
errh->fatal("<%d>%s: %s", -EXIT_ERROR, clp->vstr, strerror(errno)); |
| 317 |
break; |
| 318 |
|
| 319 |
case VERSION_OPT: |
| 320 |
printf("t1dotlessj (LCDF typetools) %s\n", VERSION); |
| 321 |
printf("Copyright (C) 2003-2023 Eddie Kohler\n\ |
| 322 |
This is free software; see the source for copying conditions.\n\ |
| 323 |
There is NO warranty, not even for merchantability or fitness for a\n\ |
| 324 |
particular purpose.\n"); |
| 325 |
exit(0); |
| 326 |
break; |
| 327 |
|
| 328 |
case HELP_OPT: |
| 329 |
usage(); |
| 330 |
exit(0); |
| 331 |
break; |
| 332 |
|
| 333 |
case Clp_NotOption: |
| 334 |
if (input_file && outputf) |
| 335 |
usage_error(errh, "too many arguments"); |
| 336 |
else if (input_file) |
| 337 |
goto output_file; |
| 338 |
else |
| 339 |
input_file = clp->vstr; |
| 340 |
break; |
| 341 |
|
| 342 |
case Clp_Done: |
| 343 |
goto done; |
| 344 |
|
| 345 |
case Clp_BadOption: |
| 346 |
usage_error(errh, 0); |
| 347 |
break; |
| 348 |
|
| 349 |
default: |
| 350 |
break; |
| 351 |
|
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
done: |
| 356 |
Type1Font *font = do_file(input_file, psres, errh); |
| 357 |
if (!input_file || strcmp(input_file, "-") == 0) |
| 358 |
input_file = "<stdin>"; |
| 359 |
|
| 360 |
// check for existing dotlessj |
| 361 |
if (font->glyph("dotlessj")) |
| 362 |
errh->fatal("<%d>%s: already has a %<dotlessj%> glyph", -EXIT_DOTLESSJ_EXISTS, font->font_name().c_str()); |
| 363 |
else if (font->glyph("uni0237")) |
| 364 |
errh->fatal("<%d>%s: already has a dotlessj glyph at %<uni0237%>", -EXIT_DOTLESSJ_EXISTS, font->font_name().c_str()); |
| 365 |
else if (font->glyph("u0237")) |
| 366 |
errh->fatal("<%d>%s: already has a dotlessj glyph at %<u0237%>", -EXIT_DOTLESSJ_EXISTS, font->font_name().c_str()); |
| 367 |
else if (private_use_dotlessj && font->glyph(private_use_dotlessj)) |
| 368 |
errh->fatal("<%d>%s: already has a dotlessj glyph at %<%s%>", -EXIT_DOTLESSJ_EXISTS, font->font_name().c_str(), private_use_dotlessj); |
| 369 |
|
| 370 |
// check for j |
| 371 |
Type1Charstring *j_cs = font->glyph("j"); |
| 372 |
if (!j_cs) |
| 373 |
j_cs = font->glyph("uni006A"); |
| 374 |
if (!j_cs) |
| 375 |
j_cs = font->glyph("u006A"); |
| 376 |
if (!j_cs) |
| 377 |
errh->fatal("<%d>%s: has no %<j%> glyph to make dotless", -EXIT_NO_J, font->font_name().c_str()); |
| 378 |
|
| 379 |
// make new font |
| 380 |
String actual_font_name = (font_name ? String(font_name) : font->font_name() + String("LCDFJ")); |
| 381 |
if (actual_font_name.length() > 29 && !font_name) { |
| 382 |
errh->warning("derived font name %<%s%> longer than 29 characters", actual_font_name.c_str()); |
| 383 |
errh->message("(Use the %<--name%> option to supply your own name.)"); |
| 384 |
} |
| 385 |
|
| 386 |
Vector<double> xuid_extension; |
| 387 |
xuid_extension.push_back(0x00237237); |
| 388 |
Type1Font *dotless_font = Type1Font::skeleton_make_copy(font, actual_font_name, &xuid_extension); |
| 389 |
dotless_font->skeleton_common_subrs(); |
| 390 |
|
| 391 |
// copy space and .notdef |
| 392 |
if (Type1Charstring *notdef = font->glyph(".notdef")) |
| 393 |
dotless_font->add_glyph(Type1Subr::make_glyph(".notdef", *notdef, " |-")); |
| 394 |
if (Type1Charstring *space = font->glyph("space")) { |
| 395 |
dotless_font->add_glyph(Type1Subr::make_glyph("space", *space, " |-")); |
| 396 |
dotless_font->type1_encoding()->put(' ', "space"); |
| 397 |
} |
| 398 |
|
| 399 |
// create dotless j |
| 400 |
Sectioner sec(5); |
| 401 |
sec.run(CharstringContext(font, j_cs)); |
| 402 |
sec.undot(font->font_name(), errh); |
| 403 |
Type1Subr *dotlessj = Type1Subr::make_glyph("uni0237", sec.gen(dotless_font), " |-"); |
| 404 |
dotless_font->add_glyph(dotlessj); |
| 405 |
//fprintf(stderr, "! %s\n", CharstringUnparser::unparse(dotlessj->t1cs()).c_str()); |
| 406 |
|
| 407 |
// encode dotless j |
| 408 |
dotless_font->type1_encoding()->clear(); |
| 409 |
dotless_font->type1_encoding()->put('j', "uni0237"); |
| 410 |
|
| 411 |
// write it to output |
| 412 |
if (!outputf) |
| 413 |
outputf = stdout; |
| 414 |
#if defined(_MSDOS) || defined(_WIN32) |
| 415 |
_setmode(_fileno(outputf), _O_BINARY); |
| 416 |
#endif |
| 417 |
if (binary) { |
| 418 |
Type1PFBWriter w(outputf); |
| 419 |
dotless_font->write(w); |
| 420 |
} else { |
| 421 |
Type1PFAWriter w(outputf); |
| 422 |
dotless_font->write(w); |
| 423 |
} |
| 424 |
|
| 425 |
return (errh->nerrors() == 0 ? EXIT_NORMAL : EXIT_ERROR); |
| 426 |
} |