| 1 |
/* t1rawafm.cc -- driver for generating a raw AFM file from a font |
| 2 |
* |
| 3 |
* Copyright (c) 2008-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/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 SMOKE_OPT 305 |
| 49 |
|
| 50 |
const Clp_Option options[] = { |
| 51 |
{ "help", 'h', HELP_OPT, 0, 0 }, |
| 52 |
{ "output", 'o', OUTPUT_OPT, Clp_ValString, 0 }, |
| 53 |
{ "version", 0, VERSION_OPT, 0, 0 }, |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
static const char *program_name; |
| 58 |
static PermString::Initializer initializer; |
| 59 |
|
| 60 |
|
| 61 |
void |
| 62 |
usage_error(ErrorHandler *errh, const char *error_message, ...) |
| 63 |
{ |
| 64 |
va_list val; |
| 65 |
va_start(val, error_message); |
| 66 |
if (!error_message) |
| 67 |
errh->message("Usage: %s [OPTION]... [FONT [OUTPUT]]", program_name); |
| 68 |
else |
| 69 |
errh->xmessage(ErrorHandler::e_error, error_message, val); |
| 70 |
errh->message("Type %s --help for more information.", program_name); |
| 71 |
exit(1); |
| 72 |
va_end(val); |
| 73 |
} |
| 74 |
|
| 75 |
void |
| 76 |
usage() |
| 77 |
{ |
| 78 |
FileErrorHandler uerrh(stdout); |
| 79 |
uerrh.message("\ |
| 80 |
%<T1rawafm%> generates a raw (kernless and ligatureless) AFM file corresponding\n\ |
| 81 |
to the specified Type 1 font file and writes it to the standard output.\n\ |
| 82 |
\n\ |
| 83 |
Usage: %s [OPTION]... [FONT [OUTPUT]]\n\ |
| 84 |
\n\ |
| 85 |
FONT is the name of a PFA or PFB font file. If omitted, t1rawafm will read a\n\ |
| 86 |
font file from the standard input.\n\ |
| 87 |
\n\ |
| 88 |
Options:\n\ |
| 89 |
-o, --output=FILE Write output to FILE instead of standard output.\n\ |
| 90 |
-h, --help Print this message and exit.\n\ |
| 91 |
--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 |
static Type1Font *font; |
| 100 |
|
| 101 |
static void |
| 102 |
do_file(const char *filename, PsresDatabase *psres, ErrorHandler *errh) |
| 103 |
{ |
| 104 |
FILE *f; |
| 105 |
if (!filename || strcmp(filename, "-") == 0) { |
| 106 |
f = stdin; |
| 107 |
filename = "<stdin>"; |
| 108 |
#if defined(_MSDOS) || defined(_WIN32) |
| 109 |
_setmode(_fileno(f), _O_BINARY); |
| 110 |
#endif |
| 111 |
} else |
| 112 |
f = fopen(filename, "rb"); |
| 113 |
|
| 114 |
if (!f) { |
| 115 |
// check for PostScript name |
| 116 |
Filename fn = psres->filename_value("FontOutline", filename); |
| 117 |
f = fn.open_read(); |
| 118 |
} |
| 119 |
|
| 120 |
if (!f) |
| 121 |
errh->fatal("%s: %s", filename, strerror(errno)); |
| 122 |
|
| 123 |
Type1Reader *reader; |
| 124 |
int c = getc(f); |
| 125 |
ungetc(c, f); |
| 126 |
if (c == EOF) |
| 127 |
errh->fatal("%s: empty file", filename); |
| 128 |
if (c == 128) |
| 129 |
reader = new Type1PFBReader(f); |
| 130 |
else |
| 131 |
reader = new Type1PFAReader(f); |
| 132 |
|
| 133 |
font = new Type1Font(*reader); |
| 134 |
if (!font->ok()) |
| 135 |
errh->fatal("%s: not a Type 1 font", filename); |
| 136 |
|
| 137 |
delete reader; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
/***** |
| 142 |
* MAIN PROGRAM |
| 143 |
**/ |
| 144 |
|
| 145 |
struct fontinfo_t { |
| 146 |
const char *afm_name; |
| 147 |
int dict; |
| 148 |
const char *dict_name; |
| 149 |
bool as_string; |
| 150 |
}; |
| 151 |
|
| 152 |
static const fontinfo_t fontinfo_strings[] = { |
| 153 |
{ "Version", Type1Font::dFI, "version", true }, |
| 154 |
{ "Notice", Type1Font::dFI, "Notice", true }, |
| 155 |
{ "FullName", Type1Font::dFI, "FullName", true }, |
| 156 |
{ "FamilyName", Type1Font::dFI, "FamilyName", true }, |
| 157 |
{ "Weight", Type1Font::dFI, "Weight", true }, |
| 158 |
{ "ItalicAngle", Type1Font::dFI, "ItalicAngle", false }, |
| 159 |
{ "IsFixedPitch", Type1Font::dFI, "isFixedPitch", false }, |
| 160 |
{ "UnderlinePosition", Type1Font::dFI, "UnderlinePosition", false }, |
| 161 |
{ "UnderlineThickness", Type1Font::dFI, "UnderlineThickness", false } |
| 162 |
}; |
| 163 |
|
| 164 |
static String |
| 165 |
strip_newlines(const String &str) |
| 166 |
{ |
| 167 |
StringAccum sa; |
| 168 |
const char *end = str.end(), *last = str.begin(); |
| 169 |
for (const char *s = str.begin(); s != end; ++s) |
| 170 |
if (*s == '\n' || *s == '\r' || *s == '\f' || *s == '\v') { |
| 171 |
sa.append(last, s); |
| 172 |
last = s + 1; |
| 173 |
} |
| 174 |
if (last == str.begin()) |
| 175 |
return str; |
| 176 |
else { |
| 177 |
sa.append(last, end); |
| 178 |
return sa.take_string(); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
static void |
| 183 |
write_char(FILE *outf, int c, PermString n, Type1Charstring *g, const Transform &font_transform, Type1Font *font) |
| 184 |
{ |
| 185 |
double bb[4], wx; |
| 186 |
CharstringBounds::bounds(font_transform, CharstringContext(font, g), bb, wx); |
| 187 |
fprintf(outf, "C %d ; WX %d ; N %s ; B %d %d %d %d ;\n", |
| 188 |
c, (int) ceil(wx), n.c_str(), |
| 189 |
(int) floor(bb[0]), (int) floor(bb[1]), |
| 190 |
(int) ceil(bb[2]), (int) ceil(bb[3])); |
| 191 |
} |
| 192 |
|
| 193 |
static void |
| 194 |
write_afm(FILE *outf, Type1Font *font) |
| 195 |
{ |
| 196 |
fprintf(outf, "StartFontMetrics 2.0\n\ |
| 197 |
Comment Generated by t1rawafm\n"); |
| 198 |
|
| 199 |
for (size_t i = 0; i < sizeof(fontinfo_strings) / sizeof(fontinfo_t); ++i) |
| 200 |
if (Type1Definition *t1d = font->dict(fontinfo_strings[i].dict, |
| 201 |
fontinfo_strings[i].dict_name)) { |
| 202 |
if (!fontinfo_strings[i].as_string) |
| 203 |
fprintf(outf, "%s %s\n", fontinfo_strings[i].afm_name, t1d->value().c_str()); |
| 204 |
else { |
| 205 |
String s; |
| 206 |
if (t1d->value_string(s)) |
| 207 |
fprintf(outf, "%s %s\n", fontinfo_strings[i].afm_name, strip_newlines(s).c_str()); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
Transform font_transform; |
| 212 |
{ |
| 213 |
double font_matrix[6]; |
| 214 |
font->font_matrix(font_matrix); |
| 215 |
font_transform = Transform(font_matrix); |
| 216 |
font_transform.scale(1000); |
| 217 |
} |
| 218 |
|
| 219 |
double bb[4], wx; |
| 220 |
if (Type1Charstring *t1cs = font->glyph("H")) { |
| 221 |
CharstringBounds::bounds(font_transform, CharstringContext(font, t1cs), bb, wx); |
| 222 |
if (bb[3]) |
| 223 |
fprintf(outf, "CapHeight %d\n", (int) ceil(bb[3])); |
| 224 |
} |
| 225 |
if (Type1Charstring *t1cs = font->glyph("x")) { |
| 226 |
CharstringBounds::bounds(font_transform, CharstringContext(font, t1cs), bb, wx); |
| 227 |
if (bb[3]) |
| 228 |
fprintf(outf, "XHeight %d\n", (int) ceil(bb[3])); |
| 229 |
} |
| 230 |
if (Type1Charstring *t1cs = font->glyph("d")) { |
| 231 |
CharstringBounds::bounds(font_transform, CharstringContext(font, t1cs), bb, wx); |
| 232 |
if (bb[3]) |
| 233 |
fprintf(outf, "Ascender %d\n", (int) ceil(bb[3])); |
| 234 |
} |
| 235 |
if (Type1Charstring *t1cs = font->glyph("p")) { |
| 236 |
CharstringBounds::bounds(font_transform, CharstringContext(font, t1cs), bb, wx); |
| 237 |
if (bb[1]) |
| 238 |
fprintf(outf, "Descender %d\n", (int) floor(bb[1])); |
| 239 |
} |
| 240 |
|
| 241 |
Vector<double> vd; |
| 242 |
if (Type1Definition *t1d = font->p_dict("StdHW")) |
| 243 |
if (t1d->value_numvec(vd) && vd.size() > 0) |
| 244 |
fprintf(outf, "StdHW %d\n", (int) ceil(vd[0])); |
| 245 |
if (Type1Definition *t1d = font->p_dict("StdVW")) |
| 246 |
if (t1d->value_numvec(vd) && vd.size() > 0) |
| 247 |
fprintf(outf, "StdVW %d\n", (int) ceil(vd[0])); |
| 248 |
|
| 249 |
double fontbb[4] = { 1000000, 1000000, -1000000, -1000000 }; |
| 250 |
for (int i = 0; i < font->nglyphs(); ++i) { |
| 251 |
CharstringBounds::bounds(font_transform, CharstringContext(font, font->glyph(i)), bb, wx); |
| 252 |
fontbb[0] = std::min(fontbb[0], bb[0]); |
| 253 |
fontbb[1] = std::min(fontbb[1], bb[1]); |
| 254 |
fontbb[2] = std::max(fontbb[2], bb[2]); |
| 255 |
fontbb[3] = std::max(fontbb[3], bb[3]); |
| 256 |
} |
| 257 |
fprintf(outf, "FontBBox %d %d %d %d\n", |
| 258 |
(int) floor(fontbb[0]), (int) floor(fontbb[1]), |
| 259 |
(int) ceil(fontbb[2]), (int) ceil(fontbb[3])); |
| 260 |
|
| 261 |
fprintf(outf, "FontName %s\n", font->font_name().c_str()); |
| 262 |
|
| 263 |
int nglyphs = font->nglyphs(); |
| 264 |
PermString dot_notdef(".notdef"); |
| 265 |
if (font->glyph(dot_notdef)) |
| 266 |
--nglyphs; |
| 267 |
fprintf(outf, "Characters %d\n", nglyphs); |
| 268 |
|
| 269 |
fprintf(outf, "StartCharMetrics %d\n", nglyphs); |
| 270 |
HashMap<PermString, int> done_yet(0); |
| 271 |
done_yet.insert(dot_notdef, 1); |
| 272 |
if (Type1Encoding *enc = font->type1_encoding()) { |
| 273 |
for (int i = 0; i < 256; ++i) { |
| 274 |
PermString n = enc->elt(i); |
| 275 |
if (!done_yet[n]) |
| 276 |
if (Type1Charstring *g = font->glyph(n)) { |
| 277 |
write_char(outf, i, n, g, font_transform, font); |
| 278 |
done_yet.insert(n, true); |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
for (int i = 0; i < font->nglyphs(); ++i) { |
| 283 |
PermString n = font->glyph_name(i); |
| 284 |
if (!done_yet[n]) |
| 285 |
write_char(outf, -1, n, font->glyph(i), font_transform, font); |
| 286 |
} |
| 287 |
fprintf(outf, "EndCharMetrics\n"); |
| 288 |
|
| 289 |
fprintf(outf, "EndFontMetrics\n"); |
| 290 |
} |
| 291 |
|
| 292 |
int |
| 293 |
main(int argc, char *argv[]) |
| 294 |
{ |
| 295 |
PsresDatabase *psres = new PsresDatabase; |
| 296 |
psres->add_psres_path(getenv("PSRESOURCEPATH"), 0, false); |
| 297 |
|
| 298 |
Clp_Parser *clp = |
| 299 |
Clp_NewParser(argc, (const char * const *)argv, sizeof(options) / sizeof(options[0]), options); |
| 300 |
program_name = Clp_ProgramName(clp); |
| 301 |
|
| 302 |
ErrorHandler *errh = ErrorHandler::static_initialize(new FileErrorHandler(stderr)); |
| 303 |
const char *output_file = 0; |
| 304 |
Vector<String> glyph_patterns; |
| 305 |
|
| 306 |
while (1) { |
| 307 |
int opt = Clp_Next(clp); |
| 308 |
switch (opt) { |
| 309 |
|
| 310 |
case OUTPUT_OPT: |
| 311 |
output_file: |
| 312 |
if (output_file) |
| 313 |
errh->fatal("output file already specified"); |
| 314 |
output_file = clp->vstr; |
| 315 |
break; |
| 316 |
|
| 317 |
case VERSION_OPT: |
| 318 |
printf("t1rawafm (LCDF typetools) %s\n", VERSION); |
| 319 |
printf("Copyright (C) 2008-2023 Eddie Kohler\n\ |
| 320 |
This is free software; see the source for copying conditions.\n\ |
| 321 |
There is NO warranty, not even for merchantability or fitness for a\n\ |
| 322 |
particular purpose.\n"); |
| 323 |
exit(0); |
| 324 |
break; |
| 325 |
|
| 326 |
case HELP_OPT: |
| 327 |
usage(); |
| 328 |
exit(0); |
| 329 |
break; |
| 330 |
|
| 331 |
case Clp_NotOption: |
| 332 |
if (font) |
| 333 |
goto output_file; |
| 334 |
else |
| 335 |
do_file(clp->vstr, psres, errh); |
| 336 |
break; |
| 337 |
|
| 338 |
case Clp_Done: |
| 339 |
goto done; |
| 340 |
|
| 341 |
case Clp_BadOption: |
| 342 |
usage_error(errh, 0); |
| 343 |
break; |
| 344 |
|
| 345 |
default: |
| 346 |
break; |
| 347 |
|
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
done: |
| 352 |
if (!font) |
| 353 |
do_file(0, psres, errh); |
| 354 |
|
| 355 |
FILE *outf; |
| 356 |
if (!output_file || strcmp(output_file, "-") == 0) |
| 357 |
outf = stdout; |
| 358 |
else { |
| 359 |
outf = fopen(output_file, "w"); |
| 360 |
if (!outf) |
| 361 |
errh->fatal("%s: %s", output_file, strerror(errno)); |
| 362 |
} |
| 363 |
#if defined(_MSDOS) || defined(_WIN32) |
| 364 |
_setmode(_fileno(outf), _O_BINARY); |
| 365 |
#endif |
| 366 |
|
| 367 |
write_afm(outf, font); |
| 368 |
|
| 369 |
exit(0); |
| 370 |
} |