| 1 |
/* ttftotype42.cc -- driver for translating TrueType fonts to Type 42 fonts |
| 2 |
* |
| 3 |
* Copyright (c) 2006-2019 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/t1rw.hh> |
| 18 |
#include <efont/t1font.hh> |
| 19 |
#include <efont/t1item.hh> |
| 20 |
#include <lcdf/clp.h> |
| 21 |
#include <lcdf/error.hh> |
| 22 |
#include <efont/cff.hh> |
| 23 |
#include <efont/otf.hh> |
| 24 |
#include <efont/otfname.hh> |
| 25 |
#include <efont/otfpost.hh> |
| 26 |
#include <efont/otfcmap.hh> |
| 27 |
#include <efont/ttfcs.hh> |
| 28 |
#include <lcdf/md5.h> |
| 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 |
#include <math.h> |
| 36 |
#ifdef HAVE_CTIME |
| 37 |
# include <time.h> |
| 38 |
#endif |
| 39 |
#if defined(_MSDOS) || defined(_WIN32) |
| 40 |
# include <fcntl.h> |
| 41 |
# include <io.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
using namespace Efont; |
| 45 |
|
| 46 |
#define VERSION_OPT 301 |
| 47 |
#define HELP_OPT 302 |
| 48 |
#define QUIET_OPT 303 |
| 49 |
#define OUTPUT_OPT 306 |
| 50 |
|
| 51 |
const Clp_Option options[] = { |
| 52 |
{ "help", 'h', HELP_OPT, 0, 0 }, |
| 53 |
{ "output", 'o', OUTPUT_OPT, Clp_ValString, 0 }, |
| 54 |
{ "quiet", 'q', QUIET_OPT, 0, Clp_Negate }, |
| 55 |
{ "version", 'v', VERSION_OPT, 0, 0 }, |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
static const char *program_name; |
| 60 |
|
| 61 |
|
| 62 |
void |
| 63 |
usage_error(ErrorHandler *errh, const char *error_message, ...) |
| 64 |
{ |
| 65 |
va_list val; |
| 66 |
va_start(val, error_message); |
| 67 |
if (!error_message) |
| 68 |
errh->message("Usage: %s [OPTIONS] [FONTFILE [OUTPUTFILE]]", program_name); |
| 69 |
else |
| 70 |
errh->xmessage(ErrorHandler::e_error, error_message, val); |
| 71 |
errh->message("Type %s --help for more information.", program_name); |
| 72 |
exit(1); |
| 73 |
va_end(val); |
| 74 |
} |
| 75 |
|
| 76 |
void |
| 77 |
usage() |
| 78 |
{ |
| 79 |
FileErrorHandler uerrh(stdout); |
| 80 |
uerrh.message("\ |
| 81 |
%<Ttftotype42%> translates a TrueType or TrueType-flavored OpenType font into\n\ |
| 82 |
PostScript Type 42 format, which is suitable for inclusion in PostScript\n\ |
| 83 |
files. The result is usually written to the standard output.\n\ |
| 84 |
\n\ |
| 85 |
Usage: %s [OPTIONS] [FONTFILE [OUTPUTFILE]]\n\ |
| 86 |
\n\ |
| 87 |
Options:\n\ |
| 88 |
-o, --output=FILE Write output to FILE.\n\ |
| 89 |
-q, --quiet Do not generate any error messages.\n\ |
| 90 |
-h, --help Print this message and exit.\n\ |
| 91 |
-v, --version Print version number and exit.\n\ |
| 92 |
\n\ |
| 93 |
Report bugs to <[email protected]>.\n", program_name); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
// MAIN |
| 98 |
|
| 99 |
// This is the list of tables Adobe recommends be included from the source TTF, |
| 100 |
// plus the 'cmap' table, which helps to make PDF results searchable. |
| 101 |
static const char * const t42_tables[] = { |
| 102 |
"cmap", "cvt ", "fpgm", "glyf", "head", "hhea", "hmtx", |
| 103 |
"loca", "maxp", "prep", "vhea", "vmtx", 0 |
| 104 |
}; |
| 105 |
|
| 106 |
struct NameId { |
| 107 |
const char *name; |
| 108 |
int nameid; |
| 109 |
}; |
| 110 |
|
| 111 |
static const NameId fontinfo_names[] = { |
| 112 |
{ "version", OpenType::Name::N_VERSION }, |
| 113 |
{ "Notice", OpenType::Name::N_TRADEMARK }, |
| 114 |
{ "Copyright", OpenType::Name::N_COPYRIGHT }, |
| 115 |
{ "FullName", OpenType::Name::N_FULLNAME }, |
| 116 |
{ "FamilyName", OpenType::Name::N_FAMILY }, |
| 117 |
{ "Weight", OpenType::Name::N_SUBFAMILY }, |
| 118 |
{ 0, 0 } |
| 119 |
}; |
| 120 |
|
| 121 |
static void |
| 122 |
fprint_sfnts(FILE *f, const String &data, bool glyf, const OpenType::Font &font) |
| 123 |
{ |
| 124 |
OpenType::Data head = font.table("head"); |
| 125 |
bool loca_long = (head.length() >= 52 && head.u16(50) != 0); |
| 126 |
// Do not split fonts with long offsets -- Werner Lemberg |
| 127 |
if (glyf && data.length() >= 65535 && !loca_long) { |
| 128 |
OpenType::Data loca = font.table("loca"); |
| 129 |
int loca_size = (loca_long ? 4 : 2); |
| 130 |
uint32_t first_offset = 0; |
| 131 |
for (int i = 1; i * loca_size < loca.length(); i++) { |
| 132 |
uint32_t next_offset = (loca_long ? loca.u32(4*i) : loca.u16(2*i) * 2); |
| 133 |
if (next_offset - first_offset >= 65535) { |
| 134 |
uint32_t prev_offset = (loca_long ? loca.u32(4*i - 4) : loca.u16(2*i - 2) * 2); |
| 135 |
fprint_sfnts(f, data.substring(first_offset, prev_offset - first_offset), false, font); |
| 136 |
first_offset = prev_offset; |
| 137 |
} |
| 138 |
} |
| 139 |
fprint_sfnts(f, data.substring(first_offset), false, font); |
| 140 |
} else if (data.length() >= 65535) { |
| 141 |
for (int offset = 0; offset < data.length(); ) { |
| 142 |
int next_offset = offset + 65534; |
| 143 |
if (next_offset > data.length()) |
| 144 |
next_offset = data.length(); |
| 145 |
fprint_sfnts(f, data.substring(offset, next_offset - offset), false, font); |
| 146 |
offset = next_offset; |
| 147 |
} |
| 148 |
} else { |
| 149 |
fputc('<', f); |
| 150 |
const uint8_t *s = data.udata(); |
| 151 |
for (int i = 0; i < data.length(); i++) { |
| 152 |
if (i && (i % 38) == 0) |
| 153 |
fputc('\n', f); |
| 154 |
fputc("0123456789ABCDEF"[(s[i] >> 4) & 0xF], f); |
| 155 |
fputc("0123456789ABCDEF"[s[i] & 0xF], f); |
| 156 |
} |
| 157 |
if ((data.length() % 38) == 0) |
| 158 |
fputc('\n', f); |
| 159 |
fputs("00>\n", f); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
static void |
| 164 |
do_file(const char *infn, const char *outfn, ErrorHandler *errh) |
| 165 |
{ |
| 166 |
FILE *f; |
| 167 |
if (!infn || strcmp(infn, "-") == 0) { |
| 168 |
f = stdin; |
| 169 |
infn = "<stdin>"; |
| 170 |
#if defined(_MSDOS) || defined(_WIN32) |
| 171 |
_setmode(_fileno(f), _O_BINARY); |
| 172 |
#endif |
| 173 |
} else if (!(f = fopen(infn, "rb"))) |
| 174 |
errh->fatal("%s: %s", infn, strerror(errno)); |
| 175 |
|
| 176 |
int c = getc(f); |
| 177 |
ungetc(c, f); |
| 178 |
|
| 179 |
if (c == EOF) |
| 180 |
errh->fatal("%s: empty file", infn); |
| 181 |
|
| 182 |
StringAccum sa(150000); |
| 183 |
int amt; |
| 184 |
do { |
| 185 |
if (char *x = sa.reserve(32768)) { |
| 186 |
amt = fread(x, 1, 32768, f); |
| 187 |
sa.adjust_length(amt); |
| 188 |
} else |
| 189 |
amt = 0; |
| 190 |
} while (amt != 0); |
| 191 |
if (!feof(f) || ferror(f)) |
| 192 |
errh->error("%s: %s", infn, strerror(errno)); |
| 193 |
if (f != stdin) |
| 194 |
fclose(f); |
| 195 |
|
| 196 |
LandmarkErrorHandler cerrh(errh, infn); |
| 197 |
OpenType::Font otf(sa.take_string(), &cerrh); |
| 198 |
if (!otf.ok() || !otf.check_checksums(&cerrh)) |
| 199 |
return; |
| 200 |
if (otf.table("CFF")) |
| 201 |
cerrh.fatal("CFF-flavored OpenType font not suitable for Type 42"); |
| 202 |
|
| 203 |
OpenType::Name name(otf.table("name"), &cerrh); |
| 204 |
OpenType::Data head_data = otf.table("head"); |
| 205 |
if (!otf.table("glyf") || head_data.length() <= 52 || !name.ok()) |
| 206 |
cerrh.fatal("font appears to lack required tables"); |
| 207 |
|
| 208 |
// create reduced font |
| 209 |
Vector<OpenType::Tag> tags; |
| 210 |
Vector<String> tables; |
| 211 |
for (const char * const *table = t42_tables; *table; table++) |
| 212 |
if (String s = otf.table(*table)) { |
| 213 |
tags.push_back(*table); |
| 214 |
tables.push_back(s); |
| 215 |
} |
| 216 |
OpenType::Font reduced_font = OpenType::Font::make(true, tags, tables); |
| 217 |
|
| 218 |
// output file |
| 219 |
if (!outfn || strcmp(outfn, "-") == 0) { |
| 220 |
f = stdout; |
| 221 |
outfn = "<stdout>"; |
| 222 |
#if defined(_MSDOS) || defined(_WIN32) |
| 223 |
_setmode(_fileno(f), _O_BINARY); |
| 224 |
#endif |
| 225 |
} else if (!(f = fopen(outfn, "wb"))) |
| 226 |
errh->fatal("%s: %s", outfn, strerror(errno)); |
| 227 |
|
| 228 |
// fprintf(f, "%%!\n"); |
| 229 |
|
| 230 |
// get glyph names |
| 231 |
TrueTypeBoundsCharstringProgram ttbprog(&otf); |
| 232 |
Vector<PermString> gn; |
| 233 |
ttbprog.glyph_names(gn); |
| 234 |
OpenType::Post post(otf.table("post")); |
| 235 |
OpenType::Cmap cmap(otf.table("cmap")); |
| 236 |
double emunits = head_data.u16(18); |
| 237 |
|
| 238 |
// font opener |
| 239 |
fprintf(f, "%%!PS-TrueTypeFont-65536-%u-1\n", head_data.u32(4)); |
| 240 |
if (post.ok()) |
| 241 |
fprintf(f, "%%%%VMusage: %u %u\n", post.mem_type42(false), post.mem_type42(true)); |
| 242 |
fprintf(f, "11 dict begin\n"); |
| 243 |
fprintf(f, "/FontName /%s def\n", name.english_name(OpenType::Name::N_POSTSCRIPT).c_str()); |
| 244 |
fprintf(f, "/FontType 42 def\n"); |
| 245 |
fprintf(f, "/FontMatrix [1 0 0 1 0 0] def\n"); |
| 246 |
fprintf(f, "/FontBBox [%g %g %g %g] readonly def\n", |
| 247 |
/* head_data.s16(36), head_data.s16(38), |
| 248 |
head_data.s16(40), head_data.s16(42)); */ |
| 249 |
head_data.s16(36) / emunits, head_data.s16(38) / emunits, |
| 250 |
head_data.s16(40) / emunits, head_data.s16(42) / emunits); |
| 251 |
fprintf(f, "/PaintType 0 def\n"); |
| 252 |
|
| 253 |
// XUID (MD5 sum of font data) |
| 254 |
{ |
| 255 |
MD5_CONTEXT md5; |
| 256 |
md5_init(&md5); |
| 257 |
md5_update(&md5, (const unsigned char *) reduced_font.data(), reduced_font.length()); |
| 258 |
unsigned char result[MD5_DIGEST_SIZE + 3]; |
| 259 |
memset(result, 0, sizeof(result)); |
| 260 |
md5_final(result, &md5); |
| 261 |
fprintf(f, "/XUID [42"); |
| 262 |
for (int i = 0; i < MD5_DIGEST_SIZE; i += 3) |
| 263 |
fprintf(f, " 16#%X", result[i] + result[i+1]*256 + result[i+2]*256*256); |
| 264 |
fprintf(f, "] def\n"); |
| 265 |
} |
| 266 |
|
| 267 |
// FontInfo dictionary |
| 268 |
fprintf(f, "/FontInfo 10 dict dup begin\n"); |
| 269 |
for (const NameId *n = fontinfo_names; n->name; n++) |
| 270 |
if (String s = name.english_name(n->nameid)) { |
| 271 |
fprintf(f, "/%s (", n->name); |
| 272 |
for (const char *x = s.begin(); x < s.end(); x++) |
| 273 |
if (*x == '(' || *x == '\\' || *x == ')') |
| 274 |
fprintf(f, "\\%c", *x); |
| 275 |
else if (*x == '\n' || (*x >= ' ' && *x <= '~')) |
| 276 |
fputc(*x, f); |
| 277 |
else |
| 278 |
fprintf(f, "\\%03o", (unsigned char) *x); |
| 279 |
fprintf(f, ") readonly def\n"); |
| 280 |
} |
| 281 |
if (post.ok()) { |
| 282 |
fprintf(f, "/isFixedPitch %s def\n", (post.is_fixed_pitch() ? "true" : "false")); |
| 283 |
fprintf(f, "/ItalicAngle %g def\n", post.italic_angle()); |
| 284 |
fprintf(f, "/UnderlinePosition %g def\n", (post.underline_position() - (post.underline_thickness() / 2)) / emunits); |
| 285 |
fprintf(f, "/UnderlineThickness %g def\n", post.underline_thickness() / emunits); |
| 286 |
} |
| 287 |
fprintf(f, "end readonly def\n"); |
| 288 |
|
| 289 |
// encoding |
| 290 |
fprintf(f, "/Encoding 256 array\n0 1 255{1 index exch/.notdef put}for\n"); |
| 291 |
for (int i = 0; i < 256; i++) |
| 292 |
if (OpenType::Glyph g = cmap.map_uni(i)) |
| 293 |
fprintf(f, "dup %d /%s put\n", i, gn[g].c_str()); |
| 294 |
fprintf(f, "readonly def\n"); |
| 295 |
|
| 296 |
// print 'sfnts' array |
| 297 |
OpenType::Data sfnts = reduced_font.data_string(); |
| 298 |
fprintf(f, "/sfnts[\n"); |
| 299 |
fprint_sfnts(f, sfnts.substring(0, OpenType::Font::HEADER_SIZE + OpenType::Font::TABLE_DIR_ENTRY_SIZE * reduced_font.ntables()), false, reduced_font); |
| 300 |
for (int i = 0; i < reduced_font.ntables(); i++) { |
| 301 |
int off = OpenType::Font::HEADER_SIZE + OpenType::Font::TABLE_DIR_ENTRY_SIZE * i; |
| 302 |
uint32_t offset = sfnts.u32(off + 8); |
| 303 |
uint32_t length = (sfnts.u32(off + 12) + 3) & ~3; |
| 304 |
fprint_sfnts(f, sfnts.substring(offset, length), sfnts.u32(off) == 0x676C7966 /*glyf*/, reduced_font); |
| 305 |
} |
| 306 |
fprintf(f, "] def\n"); |
| 307 |
|
| 308 |
// print CharStrings data |
| 309 |
fprintf(f, "/CharStrings %d dict dup begin\n", ttbprog.nglyphs()); |
| 310 |
for (int i = 0; i < gn.size(); i++) |
| 311 |
fprintf(f, "/%s %d def\n", gn[i].c_str(), i); |
| 312 |
fprintf(f, "end readonly def\n"); |
| 313 |
|
| 314 |
// complete font |
| 315 |
fprintf(f, "FontName currentdict end definefont pop\n"); |
| 316 |
|
| 317 |
// fprintf(f, "/%s 100 selectfont 30 30 moveto (Hello! 9) show showpage\n", name.english_name(OpenType::Name::N_POSTSCRIPT).c_str()); |
| 318 |
|
| 319 |
if (f != stdout) |
| 320 |
fclose(f); |
| 321 |
} |
| 322 |
|
| 323 |
#if 0 |
| 324 |
static void check_md5sum() { |
| 325 |
MD5_CONTEXT md5; |
| 326 |
md5_init(&md5); |
| 327 |
md5_update(&md5, (const unsigned char *) "message digest", 14); |
| 328 |
unsigned char result[MD5_DIGEST_SIZE]; |
| 329 |
md5_final(result, &md5); |
| 330 |
assert(memcmp(result, "\xF9\x6B\x69\x7D\x7C\xB7\x93\x8D\x52\x5A\x2F\x31\xAA\xF1\x61\xD0", MD5_DIGEST_SIZE) == 0); |
| 331 |
} |
| 332 |
#endif |
| 333 |
|
| 334 |
int |
| 335 |
main(int argc, char *argv[]) |
| 336 |
{ |
| 337 |
Clp_Parser *clp = |
| 338 |
Clp_NewParser(argc, (const char * const *)argv, sizeof(options) / sizeof(options[0]), options); |
| 339 |
program_name = Clp_ProgramName(clp); |
| 340 |
|
| 341 |
ErrorHandler *errh = ErrorHandler::static_initialize(new FileErrorHandler(stderr, String(program_name) + ": ")); |
| 342 |
const char *input_file = 0; |
| 343 |
const char *output_file = 0; |
| 344 |
|
| 345 |
while (1) { |
| 346 |
int opt = Clp_Next(clp); |
| 347 |
switch (opt) { |
| 348 |
|
| 349 |
case QUIET_OPT: |
| 350 |
if (clp->negated) |
| 351 |
errh = ErrorHandler::default_handler(); |
| 352 |
else |
| 353 |
errh = new SilentErrorHandler; |
| 354 |
break; |
| 355 |
|
| 356 |
case VERSION_OPT: |
| 357 |
printf("ttftotype42 (LCDF typetools) %s\n", VERSION); |
| 358 |
printf("Copyright (C) 2006-2023 Eddie Kohler\n\ |
| 359 |
This is free software; see the source for copying conditions.\n\ |
| 360 |
There is NO warranty, not even for merchantability or fitness for a\n\ |
| 361 |
particular purpose.\n"); |
| 362 |
exit(0); |
| 363 |
break; |
| 364 |
|
| 365 |
case HELP_OPT: |
| 366 |
usage(); |
| 367 |
exit(0); |
| 368 |
break; |
| 369 |
|
| 370 |
case OUTPUT_OPT: |
| 371 |
output_file: |
| 372 |
if (output_file) |
| 373 |
usage_error(errh, "output file specified twice"); |
| 374 |
output_file = clp->vstr; |
| 375 |
break; |
| 376 |
|
| 377 |
case Clp_NotOption: |
| 378 |
if (input_file && output_file) |
| 379 |
usage_error(errh, "too many arguments"); |
| 380 |
else if (input_file) |
| 381 |
goto output_file; |
| 382 |
else |
| 383 |
input_file = clp->vstr; |
| 384 |
break; |
| 385 |
|
| 386 |
case Clp_Done: |
| 387 |
goto done; |
| 388 |
|
| 389 |
case Clp_BadOption: |
| 390 |
usage_error(errh, 0); |
| 391 |
break; |
| 392 |
|
| 393 |
default: |
| 394 |
break; |
| 395 |
|
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
done: |
| 400 |
do_file(input_file, output_file, errh); |
| 401 |
|
| 402 |
return (errh->nerrors() == 0 ? 0 : 1); |
| 403 |
} |