| 1 |
/* cfftot1.cc -- driver for translating CFF fonts to Type 1 fonts |
| 2 |
* |
| 3 |
* Copyright (c) 2002-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/t1rw.hh> |
| 18 |
#include <efont/t1font.hh> |
| 19 |
#include <efont/t1item.hh> |
| 20 |
#include <lcdf/clp.h> |
| 21 |
#include <lcdf/error.hh> |
| 22 |
#include "maket1font.hh" |
| 23 |
#include <efont/cff.hh> |
| 24 |
#include <efont/otf.hh> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <stdarg.h> |
| 29 |
#include <ctype.h> |
| 30 |
#include <errno.h> |
| 31 |
#ifdef HAVE_CTIME |
| 32 |
# include <time.h> |
| 33 |
#endif |
| 34 |
#if defined(_MSDOS) || defined(_WIN32) |
| 35 |
# include <fcntl.h> |
| 36 |
# include <io.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
using namespace Efont; |
| 40 |
|
| 41 |
#define VERSION_OPT 301 |
| 42 |
#define HELP_OPT 302 |
| 43 |
#define QUIET_OPT 303 |
| 44 |
#define PFB_OPT 304 |
| 45 |
#define PFA_OPT 305 |
| 46 |
#define OUTPUT_OPT 306 |
| 47 |
#define NAME_OPT 307 |
| 48 |
|
| 49 |
const Clp_Option options[] = { |
| 50 |
{ "ascii", 'a', PFA_OPT, 0, 0 }, |
| 51 |
{ "binary", 'b', PFB_OPT, 0, 0 }, |
| 52 |
{ "help", 'h', HELP_OPT, 0, 0 }, |
| 53 |
{ "name", 'n', NAME_OPT, Clp_ValString, 0 }, |
| 54 |
{ "output", 'o', OUTPUT_OPT, Clp_ValString, 0 }, |
| 55 |
{ "pfa", 'a', PFA_OPT, 0, 0 }, |
| 56 |
{ "pfb", 'b', PFB_OPT, 0, 0 }, |
| 57 |
{ "quiet", 'q', QUIET_OPT, 0, Clp_Negate }, |
| 58 |
{ "version", 'v', VERSION_OPT, 0, 0 }, |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
static const char *program_name; |
| 63 |
static bool binary = true; |
| 64 |
|
| 65 |
|
| 66 |
void |
| 67 |
usage_error(ErrorHandler *errh, const char *error_message, ...) |
| 68 |
{ |
| 69 |
va_list val; |
| 70 |
va_start(val, error_message); |
| 71 |
if (!error_message) |
| 72 |
errh->message("Usage: %s [OPTIONS] [FONTFILE [OUTPUTFILE]]", program_name); |
| 73 |
else |
| 74 |
errh->xmessage(ErrorHandler::e_error, error_message, val); |
| 75 |
errh->message("Type %s --help for more information.", program_name); |
| 76 |
exit(1); |
| 77 |
va_end(val); |
| 78 |
} |
| 79 |
|
| 80 |
void |
| 81 |
usage() |
| 82 |
{ |
| 83 |
FileErrorHandler uerrh(stdout); |
| 84 |
uerrh.message("\ |
| 85 |
%<Cfftot1%> translates a PostScript font from the Compact Font Format (CFF) to\n\ |
| 86 |
the usual Type 1 format. The input file should be either a raw CFF font or a\n\ |
| 87 |
PostScript-flavored OpenType font. The result, which is usually written to the\n\ |
| 88 |
standard output, is written in PFB or PFA format.\n\ |
| 89 |
\n\ |
| 90 |
Usage: %s [OPTIONS] [FONTFILE [OUTPUTFILE]]\n\ |
| 91 |
\n\ |
| 92 |
Options:\n\ |
| 93 |
-a, --pfa Output PFA font.\n\ |
| 94 |
-b, --pfb Output PFB font. This is the default.\n\ |
| 95 |
-n, --name=NAME Select font NAME from CFF.\n\ |
| 96 |
-o, --output=FILE Write output to FILE.\n\ |
| 97 |
-q, --quiet Do not generate any error messages.\n\ |
| 98 |
-h, --help Print this message and exit.\n\ |
| 99 |
-v, --version Print version number and exit.\n\ |
| 100 |
\n\ |
| 101 |
Report bugs to <[email protected]>.\n", program_name); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
// MAIN |
| 106 |
|
| 107 |
static void |
| 108 |
do_file(const char *infn, const char *outfn, PermString name, ErrorHandler *errh) |
| 109 |
{ |
| 110 |
FILE *f; |
| 111 |
if (!infn || strcmp(infn, "-") == 0) { |
| 112 |
f = stdin; |
| 113 |
infn = "<stdin>"; |
| 114 |
#if defined(_MSDOS) || defined(_WIN32) |
| 115 |
_setmode(_fileno(f), _O_BINARY); |
| 116 |
#endif |
| 117 |
} else if (!(f = fopen(infn, "rb"))) |
| 118 |
errh->fatal("%s: %s", infn, strerror(errno)); |
| 119 |
|
| 120 |
int c = getc(f); |
| 121 |
ungetc(c, f); |
| 122 |
|
| 123 |
Cff::Font *font = 0; |
| 124 |
|
| 125 |
if (c == EOF) |
| 126 |
errh->fatal("%s: empty file", infn); |
| 127 |
if (c != 1 && c != 'O') |
| 128 |
errh->fatal("%s: not a CFF or OpenType/CFF font", infn); |
| 129 |
|
| 130 |
StringAccum sa(150000); |
| 131 |
int amt; |
| 132 |
do { |
| 133 |
if (char *x = sa.reserve(32768)) { |
| 134 |
amt = fread(x, 1, 32768, f); |
| 135 |
sa.adjust_length(amt); |
| 136 |
} else |
| 137 |
amt = 0; |
| 138 |
} while (amt != 0); |
| 139 |
if (!feof(f) || ferror(f)) |
| 140 |
errh->lerror(infn, "%s", strerror(errno)); |
| 141 |
if (f != stdin) |
| 142 |
fclose(f); |
| 143 |
|
| 144 |
ContextErrorHandler cerrh(errh, "While processing %s:", infn); |
| 145 |
cerrh.set_indent(0); |
| 146 |
String data = sa.take_string(); |
| 147 |
unsigned units_per_em = 0; |
| 148 |
if (c == 'O') { |
| 149 |
Efont::OpenType::Font font(data, &cerrh); |
| 150 |
data = font.table("CFF"); |
| 151 |
units_per_em = font.units_per_em(); |
| 152 |
} |
| 153 |
|
| 154 |
Cff *cff = new Cff(data, units_per_em, &cerrh); |
| 155 |
Cff::FontParent *fp = cff->font(name, &cerrh); |
| 156 |
if (errh->nerrors() == 0 |
| 157 |
&& !(font = dynamic_cast<Cff::Font *>(fp))) |
| 158 |
errh->fatal("%s: CID-keyed fonts not supported", infn); |
| 159 |
|
| 160 |
if (errh->nerrors() > 0) |
| 161 |
return; |
| 162 |
|
| 163 |
Type1Font *font1 = create_type1_font(font, &cerrh); |
| 164 |
|
| 165 |
if (!outfn || strcmp(outfn, "-") == 0) { |
| 166 |
f = stdout; |
| 167 |
outfn = "<stdout>"; |
| 168 |
} else if (!(f = fopen(outfn, "wb"))) |
| 169 |
errh->fatal("%s: %s", outfn, strerror(errno)); |
| 170 |
|
| 171 |
if (binary) { |
| 172 |
#if defined(_MSDOS) || defined(_WIN32) |
| 173 |
_setmode(_fileno(f), _O_BINARY); |
| 174 |
#endif |
| 175 |
Type1PFBWriter t1w(f); |
| 176 |
font1->write(t1w); |
| 177 |
} else { |
| 178 |
Type1PFAWriter t1w(f); |
| 179 |
font1->write(t1w); |
| 180 |
} |
| 181 |
|
| 182 |
if (f != stdout) |
| 183 |
fclose(f); |
| 184 |
} |
| 185 |
|
| 186 |
int |
| 187 |
main(int argc, char *argv[]) |
| 188 |
{ |
| 189 |
Clp_Parser *clp = |
| 190 |
Clp_NewParser(argc, (const char * const *)argv, sizeof(options) / sizeof(options[0]), options); |
| 191 |
program_name = Clp_ProgramName(clp); |
| 192 |
|
| 193 |
ErrorHandler *errh = ErrorHandler::static_initialize(new FileErrorHandler(stderr, String(program_name) + ": ")); |
| 194 |
const char *input_file = 0; |
| 195 |
const char *output_file = 0; |
| 196 |
const char *font_name = 0; |
| 197 |
|
| 198 |
while (1) { |
| 199 |
int opt = Clp_Next(clp); |
| 200 |
switch (opt) { |
| 201 |
|
| 202 |
case PFA_OPT: |
| 203 |
binary = false; |
| 204 |
break; |
| 205 |
|
| 206 |
case PFB_OPT: |
| 207 |
binary = true; |
| 208 |
break; |
| 209 |
|
| 210 |
case NAME_OPT: |
| 211 |
if (font_name) |
| 212 |
usage_error(errh, "font name specified twice"); |
| 213 |
font_name = clp->vstr; |
| 214 |
break; |
| 215 |
|
| 216 |
case QUIET_OPT: |
| 217 |
if (clp->negated) |
| 218 |
errh = ErrorHandler::default_handler(); |
| 219 |
else |
| 220 |
errh = new SilentErrorHandler; |
| 221 |
break; |
| 222 |
|
| 223 |
case VERSION_OPT: |
| 224 |
printf("cfftot1 (LCDF typetools) %s\n", VERSION); |
| 225 |
printf("Copyright (C) 2002-2023 Eddie Kohler\n\ |
| 226 |
This is free software; see the source for copying conditions.\n\ |
| 227 |
There is NO warranty, not even for merchantability or fitness for a\n\ |
| 228 |
particular purpose.\n"); |
| 229 |
exit(0); |
| 230 |
break; |
| 231 |
|
| 232 |
case HELP_OPT: |
| 233 |
usage(); |
| 234 |
exit(0); |
| 235 |
break; |
| 236 |
|
| 237 |
case OUTPUT_OPT: |
| 238 |
output_file: |
| 239 |
if (output_file) |
| 240 |
usage_error(errh, "output file specified twice"); |
| 241 |
output_file = clp->vstr; |
| 242 |
break; |
| 243 |
|
| 244 |
case Clp_NotOption: |
| 245 |
if (input_file && output_file) |
| 246 |
usage_error(errh, "too many arguments"); |
| 247 |
else if (input_file) |
| 248 |
goto output_file; |
| 249 |
else |
| 250 |
input_file = clp->vstr; |
| 251 |
break; |
| 252 |
|
| 253 |
case Clp_Done: |
| 254 |
goto done; |
| 255 |
|
| 256 |
case Clp_BadOption: |
| 257 |
usage_error(errh, 0); |
| 258 |
break; |
| 259 |
|
| 260 |
default: |
| 261 |
break; |
| 262 |
|
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
done: |
| 267 |
do_file(input_file, output_file, font_name, errh); |
| 268 |
|
| 269 |
return (errh->nerrors() == 0 ? 0 : 1); |
| 270 |
} |