This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ttf2pt42.c
97 lines (77 loc) · 1.97 KB
/
ttf2pt42.c
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
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include "config.h"
#include "warn.h"
/* This should have been a shell script, but argument handling happens
to be quite unwieldy even for bash. What this does is running
gs -dBATCH -sDEVICE=nullpage -dQUIET -dNOPAUSE \
-I$BASE/ps -- $BASE/t2pt42.ps $1 ${1/.ttf/.pfa}
with some argument checking thrown in.
The actual convertion is done in Postscript, see t2pt42.ps */
void openout(const char* fname)
{
int fd = open(fname, O_WRONLY | O_CREAT, 0644);
if(fd < 0)
die("Can't open %s: %m", fname);
if(dup2(fd, 1) < 0)
die("Can't dup fd: %m");
}
void openoutresuffix(const char* fname, const char* suff, const char* repl)
{
int slen = strlen(suff);
int flen = strlen(fname);
int rlen = strlen(repl);
char buf[flen + rlen + 1];
strcpy(buf, fname);
if(flen > slen && !strcmp(fname + flen - slen, suff))
strcpy(buf + flen - slen, repl);
buf[flen + rlen] = '\0';
openout(buf);
}
int main(int argc, char** argv)
{
int gsn = argc+5+1;
int gsi = 0;
char* gs[gsn];
int i;
gs[gsi++] = GS;
gs[gsi++] = "-dBATCH";
gs[gsi++] = "-sDEVICE=nullpage";
gs[gsi++] = "-dQUIET";
gs[gsi++] = "-dNOPAUSE";
gs[gsi++] = "-I" BASE;
for(i = 1; i < argc; i++)
if(argv[i][0] != '-')
break;
else switch(argv[i][1]) {
case 'I':
case 'd':
gs[gsi++] = argv[i];
break;
case '-':
if(argv[i][2])
die("Long options are not supported\n");
i++;
break;
default:
die("Unknown option %s", argv[i]);
}
if(i >= argc)
die("Input file name required\n");
if(i < argc - 2)
die("Extra arguments\n");
/* Now there's a kind of asymmetry here, ttf2pt42.ps needs ttf to be
a file*name*, but the output is expected to be a stream (stdout). */
char* ttf = argv[i++];
if(i < argc)
openout(argv[i++]);
else
openoutresuffix(ttf, ".ttf", ".pfa");
gs[gsi++] = "--";
gs[gsi++] = BASE "/t2p42.ps";
gs[gsi++] = ttf;
gs[gsi++] = NULL;
execvp(*gs, gs);
die("cannot execute %s: %m\n", *gs);
}