#ifndef lint
#ifdef __GNUC__
static char rcsid[] __attribute__ ((unused)) = "Id: ttf2bdf.c,v 1.25 1999/10/21 16:31:54 mleisher Exp $";
#else
static char rcsid[] = "Id: ttf2bdf.c,v 1.25 1999/10/21 16:31:54 mleisher Exp $";
#endif
#endif
#include <stdio.h>
#ifdef WIN32
#include <windows.h>
#else
#include <stdlib.h>
#include <unistd.h>
#endif
#include <string.h>
#include "freetype.h"
#include "remap.h"
#define TTF2BDF_VERSION "2.8"
#ifndef DEFAULT_PLATFORM_ID
#define DEFAULT_PLATFORM_ID 3
#endif
#ifndef DEFAULT_ENCODING_ID
#define DEFAULT_ENCODING_ID 1
#endif
#ifndef DEFAULT_POINT_SIZE
#define DEFAULT_POINT_SIZE 12
#endif
#ifndef DEFAULT_RESOLUTION
#define DEFAULT_RESOLUTION 100
#endif
#ifndef DEFAULT_XLFD_CSET
#define DEFAULT_XLFD_CSET "-FontSpecific-0"
#endif
#define TTF_COPYRIGHT 0
#define TTF_TYPEFACE 1
#define TTF_PSNAME 6
#ifndef MAX
#define MAX(h,i) ((h) > (i) ? (h) : (i))
#endif
#ifndef MIN
#define MIN(l,o) ((l) < (o) ? (l) : (o))
#endif
static char *prog;
static int verbose = 0;
static int load_flags = TTLOAD_SCALE_GLYPH | TTLOAD_HINT_GLYPH;
static int pid = DEFAULT_PLATFORM_ID;
static int eid = DEFAULT_ENCODING_ID;
static int point_size = DEFAULT_POINT_SIZE;
static int hres = DEFAULT_RESOLUTION;
static int vres = DEFAULT_RESOLUTION;
static char *foundry_name = 0;
static char *face_name = 0;
static char *weight_name = 0;
static char *slant_name = 0;
static char *width_name = 0;
static char *style_name = 0;
static int spacing = 0;
static int dashchar = ' ';
static int do_subset = 0;
static unsigned short maxcode;
static unsigned long subset[2048];
static int do_remap = 0;
typedef struct {
short minlb;
short maxlb;
short maxrb;
short maxas;
short maxds;
short rbearing;
} bbx_t;
static bbx_t bbx;
#define TTF2BDF_IOBUFSIZ 8192
static char iobuf[TTF2BDF_IOBUFSIZ];
static TT_UShort upm;
static TT_UShort nocmap;
static double swscale;
static char *mac_encodings[] = {
"-MacRoman-0", "-MacJapanese-0", "-MacChinese-0", "-MacKorean-0",
"-MacArabic-0", "-MacHebrew-0", "-MacGreek-0", "-MacRussian-0",
"-MacRSymbol-0", "-MacDevanagari-0", "-MacGurmukhi-0", "-MacGujarati-0",
"-MacOriya-0", "-MacBengali-0", "-MacTamil-0", "-MacTelugu-0",
"-MacKannada-0", "-MacMalayalam-0", "-MacSinhalese-0", "-MacBurmese-0",
"-MacKhmer-0", "-MacThai-0", "-MacLaotian-0", "-MacGeorgian-0",
"-MacArmenian-0", "-MacMaldivian-0", "-MacTibetan-0", "-MacMongolian-0",
"-MacGeez-0", "-MacSlavic-0", "-MacVietnamese-0","-MacSindhi-0",
"-MacUninterp-0"
};
static int num_mac_encodings = sizeof(mac_encodings) /
sizeof(mac_encodings[0]);
static char *iso_encodings[] = {
"-ASCII-0", "-ISO10646-0", "-ISO8859-1"
};
static int num_iso_encodings = sizeof(iso_encodings) /
sizeof(iso_encodings[0]);
static char *ms_encodings[] = {
"-Symbol-0", "-ISO10646-1", "-ShiftJIS-0", "-GB2312.1980-0", "-Big5-0",
"-KSC5601.1987-0", "-KSC5601.1992-0"
};
static int num_ms_encodings = sizeof(ms_encodings) /
sizeof(ms_encodings[0]);
static char *xlfd_props[] = {
"FOUNDRY",
"FAMILY_NAME",
"WEIGHT_NAME",
"SLANT",
"SETWIDTH_NAME",
"ADD_STYLE_NAME",
"PIXEL_SIZE",
"POINT_SIZE",
"RESOLUTION_X",
"RESOLUTION_Y",
"SPACING",
"AVERAGE_WIDTH",
"CHARSET_REGISTRY",
"CHARSET_ENCODING",
};
static TT_Engine engine;
static TT_Face face;
static TT_Face_Properties properties;
static TT_Instance instance;
static TT_Glyph glyph;
static TT_Glyph_Metrics metrics;
static TT_Instance_Metrics imetrics;
static TT_Raster_Map raster;
static TT_CharMap cmap;
static int
#ifdef __STDC__
ttf_get_english_name(char *name, int nameID, int dash)
#else
ttf_get_english_name(name, nameID, dash)
char *name;
int nameID, dash;
#endif
{
TT_UShort slen;
int i, j, encid, nrec;
unsigned short nrPlatformID, nrEncodingID, nrLanguageID, nrNameID;
char *s;
nrec = TT_Get_Name_Count(face);
for (encid = 1, j = 0; j < 2; j++, encid--) {
for (i = 0; i < nrec; i++) {
TT_Get_Name_ID(face, i, &nrPlatformID, &nrEncodingID,
&nrLanguageID, &nrNameID);
if (nrPlatformID == 3 &&
nrEncodingID == encid &&
nrNameID == nameID &&
(nrLanguageID == 0x0409 || nrLanguageID == 0x0809 ||
nrLanguageID == 0x0c09 || nrLanguageID == 0x1009 ||
nrLanguageID == 0x1409 || nrLanguageID == 0x1809)) {
TT_Get_Name_String(face, i, &s, &slen);
break;
}
}
if (i < nrec) {
for (i = 1; s != 0 && i < slen; i += 2) {
if (dash)
*name++ = (s[i] == '-' || s[i] == ' ') ? dash : s[i];
else if (s[i] == '\r' || s[i] == '\n') {
if (s[i] == '\r' && i + 2 < slen && s[i + 2] == '\n')
i += 2;
*name++ = ' ';
*name++ = ' ';
} else
*name++ = s[i];
}
*name = 0;
return (slen >> 1);
}
}
for (i = 0; i < nrec; i++) {
TT_Get_Name_ID(face, i, &nrPlatformID, &nrEncodingID,
&nrLanguageID, &nrNameID);
if (nrPlatformID == 0 && nrLanguageID == 0 &&
nrNameID == nameID) {
TT_Get_Name_String(face, i, &s, &slen);
break;
}
}
if (i < nrec) {
for (i = 1; s != 0 && i < slen; i += 2) {
if (dash)
*name++ = (s[i] == '-' || s[i] == ' ') ? dash : s[i];
else if (s[i] == '\r' || s[i] == '\n') {
if (s[i] == '\r' && i + 2 < slen && s[i + 2] == '\n')
i += 2;
*name++ = ' ';
*name++ = ' ';
} else
*name++ = s[i];
}
*name = 0;
return (slen >> 1);
}
return 0;
}
static void
#ifdef __STDC__
make_xlfd_name(char *name, TT_Long awidth, int ismono)
#else
make_xlfd_name(name, awidth, ismono)
char *name;
TT_Long awidth;
int ismono;
#endif
{
TT_Long i;
TT_ULong val;
char *r, *e;
double dr, dp;
if (foundry_name == 0) {
(void) strcpy(name, "-FreeType");
name += 9;
} else {
*(name++)='-';
strcpy(name,foundry_name);
name+=strlen(foundry_name);
}
*name++ = '-';
if (face_name == 0) {
if((i = ttf_get_english_name(name, TTF_TYPEFACE, dashchar)))
name += i;
else {
(void) strcpy(name, "Unknown");
name += 7;
}
} else {
(void) strcpy(name, face_name);
name += strlen(face_name);
}
if (weight_name != 0) {
sprintf(name, "-%s", weight_name);
name += strlen(weight_name) + 1;
} else {
if (properties.os2->fsSelection & 0x20) {
(void) strcpy(name, "-Bold");
name += 5;
} else {
(void) strcpy(name, "-Medium");
name += 7;
}
}
if (slant_name) {
sprintf(name, "-%s", slant_name);
name += strlen(slant_name) + 1;
} else {
*name++ = '-';
if (properties.os2->fsSelection & 0x01)
*name++ = 'I';
else
*name++ = 'R';
}
if (width_name == 0) {
(void) strcpy(name, "-Normal");
name += 7;
} else {
*(name++)='-';
strcpy(name,width_name);
name+=strlen(width_name);
}
*name++ = '-';
if (style_name != 0) {
strcpy(name,style_name);
name+=strlen(style_name);
}
dr = (double) vres;
dp = (double) (point_size * 10);
val = (unsigned long) (((dp * dr) / 722.7) + 0.5);
sprintf(name, "-%ld-%d-%d-%d", val, point_size * 10, hres, vres);
name += strlen(name);
switch (spacing) {
case 'p': case 'P': spacing = 'P'; break;
case 'm': case 'M': spacing = 'M'; break;
case 'c': case 'C': spacing = 'C'; break;
default: spacing = 0; break;
}
if (!spacing)
spacing = (ismono) ? 'M' : 'P';
*name++ = '-';
*name++ = spacing;
sprintf(name, "-%ld", awidth);
name += strlen(name);
ttf2bdf_remap_charset(&r, &e);
if (r != 0 && e != 0) {
sprintf(name, "-%s-%s", r, e);
return;
}
if (nocmap || pid > 3)
(void) strcpy(name, DEFAULT_XLFD_CSET);
else {
switch (pid) {
case 0:
(void) strcpy(name, "-Unicode-2.0");
break;
case 1:
if (eid < 0 || eid >= num_mac_encodings)
(void) strcpy(name, DEFAULT_XLFD_CSET);
else
(void) strcpy(name, mac_encodings[eid]);
break;
case 2:
if (eid < 0 || eid >= num_iso_encodings)
(void) strcpy(name, DEFAULT_XLFD_CSET);
else
(void) strcpy(name, iso_encodings[eid]);
break;
case 3:
if (eid < 0 || eid >= num_ms_encodings)
(void) strcpy(name, DEFAULT_XLFD_CSET);
else
(void) strcpy(name, ms_encodings[eid]);
break;
}
}
}
static int
#ifdef __STDC__
generate_font(FILE *out, char *iname, char *oname)
#else
generate_font(out, iname, oname)
FILE *out;
char *iname, *oname;
#endif
{
int eof, ismono, i;
FILE *tmp;
TT_Short maxx, maxy, minx, miny, xoff, yoff, dwidth, swidth;
TT_Short y_off, x_off;
TT_UShort sx, sy, ex, ey, wd, ht;
TT_Long code, idx, ng, aw;
TT_UShort remapped_code;
unsigned char *bmap;
double dw;
char *xp, xlfd[256];
char *tmpdir, tmpfile[BUFSIZ];
if ((tmpdir = getenv("TMPDIR")) == 0)
tmpdir = "/tmp";
sprintf(tmpfile, "%s/ttf2bdf%ld", tmpdir, (long) getpid());
if ((tmp = fopen(tmpfile, "w")) == 0) {
fprintf(stderr, "%s: unable to open temporary file '%s'.\n",
prog, tmpfile);
return -1;
}
swscale = ((double) vres) * ((double) point_size);
minx = (properties.header->xMin * imetrics.x_ppem) / upm;
miny = (properties.header->yMin * imetrics.y_ppem) / upm;
maxx = (properties.header->xMax * imetrics.x_ppem) / upm;
maxy = (properties.header->yMax * imetrics.y_ppem) / upm;
maxx -= minx; ++maxx;
maxy -= miny; ++maxy;
wd = 0xffff;
ismono = 1;
raster.flow = TT_Flow_Up;
raster.width = maxx;
raster.rows = maxy;
raster.cols = (maxx + 7) >> 3;
raster.size = raster.cols * raster.rows;
raster.bitmap = (void *) malloc(raster.size);
for (ng = code = 0, eof = 0, aw = 0; eof != EOF && code < 0xffff; code++) {
remapped_code = (TT_UShort) code;
if (do_remap && !ttf2bdf_remap(&remapped_code))
continue;
if (do_subset && remapped_code > maxcode)
break;
if (do_subset &&
!(subset[remapped_code >> 5] & (1 << (remapped_code & 31))))
continue;
if (nocmap) {
if (code >= properties.num_Glyphs)
break;
idx = code;
} else
idx = TT_Char_Index(cmap, code);
if (idx <= 0 || TT_Load_Glyph(instance, glyph, idx, load_flags))
continue;
(void) TT_Get_Glyph_Metrics(glyph, &metrics);
(void) memset((char *) raster.bitmap, 0, raster.size);
xoff = (63 - metrics.bbox.xMin) & -64;
yoff = (63 - metrics.bbox.yMin) & -64;
if (TT_Get_Glyph_Bitmap(glyph, &raster, xoff, yoff))
continue;
dwidth = metrics.advance >> 6;
dw = (double) dwidth;
swidth = (TT_Short) ((dw * 72000.0) / swscale);
sx = ey = 0xffff;
sy = ex = 0;
bmap = (unsigned char *) raster.bitmap;
for (miny = 0; miny < raster.rows; miny++) {
for (minx = 0; minx < raster.width; minx++) {
if (bmap[(miny * raster.cols) + (minx >> 3)] &
(0x80 >> (minx & 7))) {
if (minx < sx)
sx = minx;
if (minx > ex)
ex = minx;
if (miny > sy)
sy = miny;
if (miny < ey)
ey = miny;
}
}
}
if (sx == 0xffff && ey == 0xffff && sy == 0 && ex == 0)
sx = ex = sy = ey = 0;
ng++;
if (ismono && (ex - sx) + 1 != wd)
ismono = 0;
wd = (ex - sx) + 1;
ht = (sy - ey) + 1;
x_off = sx - (xoff >> 6);
y_off = ey - (yoff >> 6);
bbx.maxas = MAX(bbx.maxas, ht + y_off);
bbx.maxds = MAX(bbx.maxds, -y_off);
bbx.rbearing = wd + x_off;
bbx.maxrb = MAX(bbx.maxrb, bbx.rbearing);
bbx.minlb = MIN(bbx.minlb, x_off);
bbx.maxlb = MAX(bbx.maxlb, x_off);
aw += wd;
fprintf(tmp, "STARTCHAR %04lX\nENCODING %ld\n", code,
(long) remapped_code);
fprintf(tmp, "SWIDTH %hd 0\n", swidth);
fprintf(tmp, "DWIDTH %hd 0\n", dwidth);
fprintf(tmp, "BBX %hd %hd %hd %hd\n", wd, ht, x_off, y_off);
eof = fprintf(tmp, "BITMAP\n");
for (miny = sy; eof != EOF && miny >= ey; miny--) {
for (idx = 0, minx = sx; eof != EOF && minx <= ex; minx++) {
if (minx > sx && ((minx - sx) & 7) == 0) {
eof = fprintf(tmp, "%02lX", idx & 0xff);
idx = 0;
}
if (bmap[(miny * raster.cols) + (minx >> 3)] &
(0x80 >> (minx & 7)))
idx |= 0x80 >> ((minx - sx) & 7);
}
if (eof != EOF)
fprintf(tmp, "%02lX\n", idx & 0xff);
}
if (eof != EOF)
fprintf(tmp, "ENDCHAR\n");
}
fclose(tmp);
if (eof == EOF) {
(void) unlink(tmpfile);
fprintf(stderr, "%s: problem writing to temporary file '%s'.\n",
prog, tmpfile);
if (raster.size > 0)
free((char *) raster.bitmap);
return -1;
}
if (ng == 0) {
(void) unlink(tmpfile);
fprintf(stderr, "%s: no glyphs generated from '%s'.\n", prog, iname);
if (raster.size > 0)
free((char *) raster.bitmap);
return -1;
}
if ((tmp = fopen(tmpfile, "r")) == 0) {
(void) unlink(tmpfile);
fprintf(stderr, "%s: unable to open temporary file '%s' for read.\n",
prog, tmpfile);
if (raster.size > 0)
free((char *) raster.bitmap);
return -1;
}
if (raster.size > 0)
free((char *) raster.bitmap);
aw = (TT_Long) ((((double) aw / (double) ng) + 0.5) * 10.0);
make_xlfd_name(xlfd, aw, ismono);
fprintf(out, "STARTFONT 2.1\n");
fprintf(out, "COMMENT\n");
fprintf(out, "COMMENT Converted from TrueType font \"%s\" by \"%s %s\".\n",
iname, prog, TTF2BDF_VERSION);
fprintf(out, "COMMENT\n");
fprintf(out, "FONT %s\n", xlfd);
fprintf(out, "SIZE %d %d %d\n", point_size, hres, vres);
fprintf(out, "FONTBOUNDINGBOX %hd %hd %hd %hd\n",
bbx.maxrb - bbx.minlb, bbx.maxas + bbx.maxds,
bbx.minlb, -bbx.maxds);
fprintf(out, "STARTPROPERTIES %hd\n", 19);
for (i = 0, xp = xlfd; i < 14; i++) {
fprintf(out, "%s ", xlfd_props[i]);
if (i < 6 || i == 10 || i > 11)
putc('"', out);
xp++;
for (; *xp && *xp != '-'; xp++)
putc(*xp, out);
if (i < 6 || i == 10 || i > 11)
putc('"', out);
putc('\n', out);
}
fprintf(out, "FONT_ASCENT %hd\nFONT_DESCENT %hd\n",
(properties.horizontal->Ascender * imetrics.y_ppem) / upm,
-((properties.horizontal->Descender * imetrics.y_ppem) / upm));
(void) ttf_get_english_name(xlfd, TTF_COPYRIGHT, 0);
fprintf(out, "COPYRIGHT \"%s\"\n", xlfd);
(void) ttf_get_english_name(xlfd, TTF_PSNAME, 0);
fprintf(out, "_TTF_FONTFILE \"%s\"\n_TTF_PSNAME \"%s\"\n", iname, xlfd);
fprintf(out, "ENDPROPERTIES\n");
eof = fprintf(out, "CHARS %ld\n", ng);
while (eof != EOF && (ng = fread(iobuf, 1, TTF2BDF_IOBUFSIZ, tmp))) {
if (fwrite(iobuf, 1, ng, out) == 0)
eof = EOF;
}
fclose(tmp);
(void) unlink(tmpfile);
if (eof == EOF) {
fprintf(stderr, "%s: problem writing to output file '%s'.\n",
prog, oname);
if (raster.size > 0)
free((char *) raster.bitmap);
return -1;
}
eof = fprintf(out, "ENDFONT\n");
return eof;
}
static int
#ifdef __STDC__
generate_bdf(FILE *out, char *iname, char *oname)
#else
generate_bdf(out, iname, oname)
FILE *out;
char *iname, *oname;
#endif
{
TT_Long i;
TT_UShort p, e;
for (i = 0; i < TT_Get_CharMap_Count(face); i++) {
if (!TT_Get_CharMap_ID(face, i, &p, &e) &&
p == pid && e == eid)
break;
}
if (i == TT_Get_CharMap_Count(face) && pid == 3 && eid == 1) {
for (i = 0; i < TT_Get_CharMap_Count(face); i++) {
if (!TT_Get_CharMap_ID(face, i, &p, &e) &&
p == pid && e == 0)
break;
}
if (i < TT_Get_CharMap_Count(face)) {
if (!TT_Get_CharMap(face, i, &cmap))
eid = 0;
else
nocmap = 1;
}
} else {
if (i < TT_Get_CharMap_Count(face) && TT_Get_CharMap(face, i, &cmap))
nocmap = 1;
else
nocmap = 0;
}
if (nocmap && verbose) {
fprintf(stderr,
"%s: no character map for platform %d encoding %d. ",
prog, pid, eid);
fprintf(stderr, "Generating all glyphs.\n");
}
return generate_font(out, iname, oname);
}
#define isdig(cc) ((cc) >= '0' && (cc) <= '9')
static void
#ifdef __STDC__
parse_subset(char *s)
#else
parse_subset(s)
char *s;
#endif
{
long l, r;
maxcode = 0;
do_subset = 0;
(void) memset((char *) subset, 0, sizeof(unsigned long) * 2048);
while (*s) {
for (l = r = 0; *s && isdig(*s); s++)
l = (l * 10) + (*s - '0');
if (*s == '_') {
s++;
for (; *s && isdig(*s); s++)
r = (r * 10) + (*s - '0');
} else
r = l;
for (; l <= r; l++) {
do_subset = 1;
subset[l >> 5] |= (1 << (l & 31));
if (l > maxcode)
maxcode = l;
}
while (*s && !isdig(*s))
s++;
}
}
static void
#ifdef __STDC__
usage(int eval)
#else
usage(eval)
int eval;
#endif
{
fprintf(stderr, "Usage: %s [options below] font.ttf\n", prog);
fprintf(stderr, "-h\t\tThis message.\n");
fprintf(stderr, "-v\t\tPrint warning messages during conversion.\n");
fprintf(stderr,
"-l \"subset\"\tSpecify a subset of glyphs to generate.\n");
fprintf(stderr, "-m mapfile\tGlyph reencoding file.\n");
fprintf(stderr, "-n\t\tTurn off glyph hinting.\n");
fprintf(stderr,
"-c c\t\tSet the character spacing (default: from font).\n");
fprintf(stderr,
"-f name\t\tSet the foundry name (default: freetype).\n");
fprintf(stderr,
"-t name\t\tSet the typeface name (default: from font).\n");
fprintf(stderr, "-w name\t\tSet the weight name (default: Medium).\n");
fprintf(stderr, "-s name\t\tSet the slant name (default: R).\n");
fprintf(stderr, "-k name\t\tSet the width name (default: Normal).\n");
fprintf(stderr,
"-d name\t\tSet the additional style name (default: empty).\n");
fprintf(stderr, "-u char\t\tSet the character to replace '-' in names ");
fprintf(stderr, "(default: space).\n");
fprintf(stderr,
"-pid id\t\tSet the platform ID for encoding (default: %d).\n",
DEFAULT_PLATFORM_ID);
fprintf(stderr,
"-eid id\t\tSet the encoding ID for encoding (default: %d).\n",
DEFAULT_ENCODING_ID);
fprintf(stderr, "-p n\t\tSet the point size (default: %dpt).\n",
DEFAULT_POINT_SIZE);
fprintf(stderr, "-r n\t\tSet the horizontal and vertical resolution ");
fprintf(stderr, "(default: %ddpi).\n", DEFAULT_RESOLUTION);
fprintf(stderr, "-rh n\t\tSet the horizontal resolution ");
fprintf(stderr, "(default: %ddpi)\n", DEFAULT_RESOLUTION);
fprintf(stderr, "-rv n\t\tSet the vertical resolution ");
fprintf(stderr, "(default: %ddpi)\n", DEFAULT_RESOLUTION);
fprintf(stderr,
"-o outfile\tSet the output filename (default: stdout).\n");
exit(eval);
}
int
#ifdef __STDC__
main(int argc, char *argv[])
#else
main(argc, argv)
int argc;
char *argv[];
#endif
{
int res;
char *infile, *outfile, *iname, *oname;
FILE *out, *mapin;
if ((prog = strrchr(argv[0], '/')))
prog++;
else
prog = argv[0];
out = stdout;
infile = outfile = 0;
argc--;
argv++;
while (argc > 0) {
if (argv[0][0] == '-') {
switch (argv[0][1]) {
case 'v': case 'V':
verbose = 1;
break;
case 'l': case 'L':
argc--;
argv++;
parse_subset(argv[0]);
break;
case 'n': case 'N':
load_flags &= ~TTLOAD_HINT_GLYPH;
break;
case 'c': case 'C':
argc--;
argv++;
spacing = argv[0][0];
break;
case 't': case 'T':
argc--;
argv++;
face_name = argv[0];
break;
case 'w': case 'W':
argc--;
argv++;
weight_name = argv[0];
break;
case 's': case 'S':
argc--;
argv++;
slant_name = argv[0];
break;
case 'k': case 'K':
argc--;
argv++;
width_name = argv[0];
break;
case 'd': case 'D':
argc--;
argv++;
style_name = argv[0];
break;
case 'f': case 'F':
argc--;
argv++;
foundry_name = argv[0];
break;
case 'u': case 'U':
argc--;
argv++;
dashchar = argv[0][0];
break;
case 'p': case 'P':
res = argv[0][2];
argc--;
argv++;
if (res == 'i' || res == 'I')
pid = atoi(argv[0]);
else
point_size = atoi(argv[0]);
break;
case 'e': case 'E':
argc--;
argv++;
eid = atoi(argv[0]);
break;
case 'r':
if (argv[0][2] == 'h')
hres = atoi(argv[1]);
else if (argv[0][2] == 'v')
vres = atoi(argv[1]);
else
hres = vres = atoi(argv[1]);
argc--;
argv++;
break;
case 'm': case 'M':
argc--;
argv++;
do_remap = 0;
if ((mapin = fopen(argv[0], "r")) == 0)
fprintf(stderr, "%s: unable to open the remap table '%s'.\n",
prog, argv[0]);
else {
if (ttf2bdf_load_map(mapin) < 0) {
fprintf(stderr,
"%s: problem loading remap table '%s'.\n",
prog, argv[0]);
do_remap = 0;
} else
do_remap = 1;
fclose(mapin);
}
break;
case 'o': case 'O':
argc--;
argv++;
outfile = argv[0];
break;
default:
usage(1);
}
} else
infile = argv[0];
argc--;
argv++;
}
if (infile == 0) {
fprintf(stderr, "%s: no input file provided.\n", prog);
usage(1);
}
if ((iname = strrchr(infile, '/')))
iname++;
else
iname = infile;
if (pid < 0 || pid > 255) {
fprintf(stderr, "%s: invalid platform ID '%d'.\n", prog, pid);
exit(1);
}
if (eid < 0 || eid > 65535) {
fprintf(stderr, "%s: invalid encoding ID '%d'.\n", prog, eid);
exit(1);
}
if (point_size < 2 || point_size > 256) {
fprintf(stderr, "%s: invalid point size '%dpt'.\n", prog, point_size);
exit(1);
}
if (hres < 10 || hres > 1200) {
fprintf(stderr, "%s: invalid horizontal resolution '%ddpi'.\n",
prog, hres);
exit(1);
}
if (vres < 10 || vres > 1200) {
fprintf(stderr, "%s: invalid vertical resolution '%ddpi'.\n",
prog, vres);
exit(1);
}
if (outfile != 0) {
if ((out = fopen(outfile, "w")) == 0) {
fprintf(stderr, "%s: unable to open the output file '%s'.\n",
prog, outfile);
exit(1);
}
if ((oname = strrchr(outfile, '/')))
oname++;
else
oname = outfile;
} else
oname = "<stdout>";
if ((res = TT_Init_FreeType(&engine))) {
if (out != stdout) {
fclose(out);
(void) unlink(outfile);
}
fprintf(stderr, "%s[%d]: unable to initialize renderer.\n",
prog, res);
exit(1);
}
if ((res = TT_Open_Face(engine, infile, &face))) {
if (out != stdout) {
fclose(out);
(void) unlink(outfile);
}
fprintf(stderr, "%s[%d]: unable to open input file '%s'.\n",
prog, res, infile);
exit(1);
}
if ((res = TT_New_Instance(face, &instance))) {
(void) TT_Close_Face(face);
if (out != stdout) {
fclose(out);
(void) unlink(outfile);
}
fprintf(stderr, "%s[%d]: unable to create instance.\n",
prog, res);
exit(1);
}
(void) TT_Set_Instance_Resolutions(instance, hres, vres);
(void) TT_Set_Instance_CharSize(instance, point_size*64);
(void) TT_Get_Instance_Metrics(instance, &imetrics);
(void) TT_Get_Face_Properties(face, &properties);
upm = properties.header->Units_Per_EM;
if ((res = TT_New_Glyph(face, &glyph))) {
(void) TT_Done_Instance(instance);
(void) TT_Close_Face(face);
if (out != stdout) {
fclose(out);
(void) unlink(outfile);
}
fprintf(stderr, "%s[%d]: unable to create glyph.\n",
prog, res);
exit(1);
}
res = generate_bdf(out, iname, oname);
ttf2bdf_free_map();
(void) TT_Close_Face(face);
if (out != stdout) {
fclose(out);
if (res < 0)
(void) unlink(outfile);
}
(void) TT_Done_FreeType(engine);
exit(res);
return 0;
}