summaryrefslogtreecommitdiff
path: root/Build/source/texk/lcdf-typetools/lcdf-typetools-2.98/t1rawafm/t1rawafm.cc
blob: 8ba7c7531fb3280fcef3abc5c3e989f1f4e3a791 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* t1rawafm.cc -- driver for generating a raw AFM file from a font
 *
 * Copyright (c) 2008-2012 Eddie Kohler
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version. This program is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <efont/psres.hh>
#include <efont/t1rw.hh>
#include <efont/t1font.hh>
#include <efont/t1item.hh>
#include <efont/t1mm.hh>
#include <efont/t1interp.hh>
#include <efont/t1bounds.hh>
#include <lcdf/clp.h>
#include <lcdf/error.hh>
#include <lcdf/globmatch.hh>
#include <lcdf/transform.hh>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include <errno.h>
#ifdef HAVE_CTIME
# include <time.h>
#endif
#if defined(_MSDOS) || defined(_WIN32)
# include <fcntl.h>
# include <io.h>
#endif

using namespace Efont;

#define VERSION_OPT	301
#define HELP_OPT	302
#define OUTPUT_OPT	303
#define SMOKE_OPT	305

const Clp_Option options[] = {
    { "help", 'h', HELP_OPT, 0, 0 },
    { "output", 'o', OUTPUT_OPT, Clp_ValString, 0 },
    { "version", 0, VERSION_OPT, 0, 0 },
};


static const char *program_name;
static PermString::Initializer initializer;


void
usage_error(ErrorHandler *errh, const char *error_message, ...)
{
    va_list val;
    va_start(val, error_message);
    if (!error_message)
	errh->message("Usage: %s [OPTION]... [FONT [OUTPUT]]", program_name);
    else
	errh->vxmessage(ErrorHandler::e_error, error_message, val);
    errh->message("Type %s --help for more information.", program_name);
    exit(1);
}

void
usage()
{
    FileErrorHandler uerrh(stdout);
    uerrh.message("\
%<T1rawafm%> generates a raw (kernless and ligatureless) AFM file corresponding\n\
to the specified Type 1 font file and writes it to the standard output.\n\
\n\
Usage: %s [OPTION]... [FONT [OUTPUT]]\n\
\n\
FONT is the name of a PFA or PFB font file. If omitted, t1rawafm will read a\n\
font file from the standard input.\n\
\n\
Options:\n\
  -o, --output=FILE            Write output to FILE instead of standard output.\n\
  -h, --help                   Print this message and exit.\n\
      --version                Print version number and exit.\n\
\n\
Report bugs to <ekohler@gmail.com>.\n", program_name);
}


// MAIN

static Type1Font *font;

static void
do_file(const char *filename, PsresDatabase *psres, ErrorHandler *errh)
{
    FILE *f;
    if (!filename || strcmp(filename, "-") == 0) {
	f = stdin;
	filename = "<stdin>";
#if defined(_MSDOS) || defined(_WIN32)
	_setmode(_fileno(f), _O_BINARY);
#endif
    } else
	f = fopen(filename, "rb");

    if (!f) {
	// check for PostScript name
	Filename fn = psres->filename_value("FontOutline", filename);
	f = fn.open_read();
    }

    if (!f)
	errh->fatal("%s: %s", filename, strerror(errno));

    Type1Reader *reader;
    int c = getc(f);
    ungetc(c, f);
    if (c == EOF)
	errh->fatal("%s: empty file", filename);
    if (c == 128)
	reader = new Type1PFBReader(f);
    else
	reader = new Type1PFAReader(f);

    font = new Type1Font(*reader);

    delete reader;
}


/*****
 * MAIN PROGRAM
 **/

struct fontinfo_t {
    const char *afm_name;
    int dict;
    const char *dict_name;
    bool as_string;
};

static const fontinfo_t fontinfo_strings[] = {
    { "Version", Type1Font::dFI, "version", true },
    { "Notice", Type1Font::dFI, "Notice", true },
    { "FullName", Type1Font::dFI, "FullName", true },
    { "FamilyName", Type1Font::dFI, "FamilyName", true },
    { "Weight", Type1Font::dFI, "Weight", true },
    { "ItalicAngle", Type1Font::dFI, "ItalicAngle", false },
    { "IsFixedPitch", Type1Font::dFI, "isFixedPitch", false },
    { "UnderlinePosition", Type1Font::dFI, "UnderlinePosition", false },
    { "UnderlineThickness", Type1Font::dFI, "UnderlineThickness", false }
};

static String
strip_newlines(const String &str)
{
    StringAccum sa;
    const char *end = str.end(), *last = str.begin();
    for (const char *s = str.begin(); s != end; ++s)
	if (*s == '\n' || *s == '\r' || *s == '\f' || *s == '\v') {
	    sa.append(last, s);
	    last = s + 1;
	}
    if (last == str.begin())
	return str;
    else {
	sa.append(last, end);
	return sa.take_string();
    }
}

static void
write_char(FILE *outf, int c, PermString n, Type1Charstring *g, const Transform &font_transform, Type1Font *font)
{
    int bb[4], wx;
    CharstringBounds::bounds(font_transform, CharstringContext(font, g), bb, wx);
    fprintf(outf, "C %d ; WX %d ; N %s ; B %d %d %d %d ;\n",
	    c, wx, n.c_str(), bb[0], bb[1], bb[2], bb[3]);
}

static void
write_afm(FILE *outf, Type1Font *font)
{
    fprintf(outf, "StartFontMetrics 2.0\n\
Comment Generated by t1rawafm\n");

    for (size_t i = 0; i < sizeof(fontinfo_strings) / sizeof(fontinfo_t); ++i)
	if (Type1Definition *t1d = font->dict(fontinfo_strings[i].dict,
					      fontinfo_strings[i].dict_name)) {
	    if (!fontinfo_strings[i].as_string)
		fprintf(outf, "%s %s\n", fontinfo_strings[i].afm_name, t1d->value().c_str());
	    else {
		String s;
		if (t1d->value_string(s))
		    fprintf(outf, "%s %s\n", fontinfo_strings[i].afm_name, strip_newlines(s).c_str());
	    }
	}

    Transform font_transform;
    {
	double font_matrix[6];
	font->font_matrix(font_matrix);
	font_transform = Transform(font_matrix);
	font_transform.scale(1000);
    }

    int bb[4], wx;
    if (Type1Charstring *t1cs = font->glyph("H")) {
	CharstringBounds::bounds(font_transform, CharstringContext(font, t1cs), bb, wx);
	if (bb[3])
	    fprintf(outf, "Ascender %d\n", bb[3]);
    }
    if (Type1Charstring *t1cs = font->glyph("x")) {
	CharstringBounds::bounds(font_transform, CharstringContext(font, t1cs), bb, wx);
	if (bb[3])
	    fprintf(outf, "XHeight %d\n", bb[3]);
    }
    if (Type1Charstring *t1cs = font->glyph("d")) {
	CharstringBounds::bounds(font_transform, CharstringContext(font, t1cs), bb, wx);
	if (bb[3])
	    fprintf(outf, "Ascender %d\n", bb[3]);
    }
    if (Type1Charstring *t1cs = font->glyph("p")) {
	CharstringBounds::bounds(font_transform, CharstringContext(font, t1cs), bb, wx);
	if (bb[3])
	    fprintf(outf, "Descender %d\n", bb[1]);
    }

    Vector<double> vd;
    if (Type1Definition *t1d = font->p_dict("StdHW"))
	if (t1d->value_numvec(vd) && vd.size() > 0)
	    fprintf(outf, "StdHW %d\n", (int) ceil(vd[0]));
    if (Type1Definition *t1d = font->p_dict("StdVW"))
	if (t1d->value_numvec(vd) && vd.size() > 0)
	    fprintf(outf, "StdVW %d\n", (int) ceil(vd[0]));

    int fontbb[4] = { 1000000, 1000000, -1000000, -1000000 };
    for (int i = 0; i < font->nglyphs(); ++i) {
	CharstringBounds::bounds(font_transform, CharstringContext(font, font->glyph(i)), bb, wx);
	fontbb[0] = std::min(fontbb[0], bb[0]);
	fontbb[1] = std::min(fontbb[1], bb[1]);
	fontbb[2] = std::max(fontbb[2], bb[2]);
	fontbb[3] = std::max(fontbb[3], bb[3]);
    }
    fprintf(outf, "FontBBox %d %d %d %d\n", fontbb[0], fontbb[1], fontbb[2], fontbb[3]);

    fprintf(outf, "FontName %s\n", font->font_name().c_str());

    int nglyphs = font->nglyphs();
    PermString dot_notdef(".notdef");
    if (font->glyph(dot_notdef))
	--nglyphs;
    fprintf(outf, "Characters %d\n", nglyphs);

    fprintf(outf, "StartCharMetrics %d\n", nglyphs);
    Type1Encoding *enc = font->type1_encoding();
    HashMap<PermString, int> done_yet(0);
    done_yet.insert(dot_notdef, 1);
    for (int i = 0; i < 256; ++i) {
	PermString n = enc->elt(i);
	if (!done_yet[n])
	    if (Type1Charstring *g = font->glyph(n)) {
		write_char(outf, i, n, g, font_transform, font);
		done_yet.insert(n, true);
	    }
    }
    for (int i = 0; i < font->nglyphs(); ++i) {
	PermString n = font->glyph_name(i);
	if (!done_yet[n])
	    write_char(outf, -1, n, font->glyph(i), font_transform, font);
    }
    fprintf(outf, "EndCharMetrics\n");

    fprintf(outf, "EndFontMetrics\n");
}

int
main(int argc, char *argv[])
{
    PsresDatabase *psres = new PsresDatabase;
    psres->add_psres_path(getenv("PSRESOURCEPATH"), 0, false);

    Clp_Parser *clp =
	Clp_NewParser(argc, (const char * const *)argv, sizeof(options) / sizeof(options[0]), options);
    program_name = Clp_ProgramName(clp);

    ErrorHandler *errh = ErrorHandler::static_initialize(new FileErrorHandler(stderr));
    const char *output_file = 0;
    Vector<String> glyph_patterns;

    while (1) {
	int opt = Clp_Next(clp);
	switch (opt) {

	case OUTPUT_OPT:
	output_file:
	    if (output_file)
		errh->fatal("output file already specified");
	    output_file = clp->vstr;
	    break;

	  case VERSION_OPT:
	    printf("t1rawafm (LCDF typetools) %s\n", VERSION);
	    printf("Copyright (C) 2008-2012 Eddie Kohler\n\
This is free software; see the source for copying conditions.\n\
There is NO warranty, not even for merchantability or fitness for a\n\
particular purpose.\n");
	    exit(0);
	    break;

	  case HELP_OPT:
	    usage();
	    exit(0);
	    break;

	  case Clp_NotOption:
	    if (font)
		goto output_file;
	    else
		do_file(clp->vstr, psres, errh);
	    break;

	  case Clp_Done:
	    goto done;

	  case Clp_BadOption:
	    usage_error(errh, 0);
	    break;

	  default:
	    break;

	}
    }

  done:
    if (!font)
	do_file(0, psres, errh);

    FILE *outf;
    if (!output_file || strcmp(output_file, "-") == 0)
	outf = stdout;
    else {
	outf = fopen(output_file, "w");
	if (!outf)
	    errh->fatal("%s: %s", output_file, strerror(errno));
    }
#if defined(_MSDOS) || defined(_WIN32)
    _setmode(_fileno(outf), _O_BINARY);
#endif

    write_afm(outf, font);

    exit(0);
}