shithub: neatmkfn

Download patch

ref: 562f75d41c37b75af3a25fa3c326b96b26c235ac
parent: 5d3e4c0c7c1bc23a6e9f163718d40e348f4789ca
author: Ali Gholami Rudi <[email protected]>
date: Wed Feb 19 17:04:26 EST 2014

mktrfn: include glyph bounding box with -b

--- a/gen.sh
+++ b/gen.sh
@@ -53,9 +53,9 @@
 
 # The standard fonts
 afmconv R	Times-Roman		n021003l.afm
-afmconv I	Times-Italic		n021023l.afm
+afmconv I	Times-Italic		n021023l.afm -b
 afmconv B	Times-Bold		n021004l.afm
-afmconv BI	Times-BoldItalic	n021024l.afm
+afmconv BI	Times-BoldItalic	n021024l.afm -b
 afmconv S	Symbol			s050000l.afm -s
 afmconv S1	Times-Roman		n021003l.afm -s
 afmconv AR	AvantGarde-Book		a010013l.afm
@@ -63,32 +63,32 @@
 afmconv AB	AvantGarde-Demi		a010015l.afm
 afmconv AX	AvantGarde-DemiOblique	a010035l.afm
 afmconv H	Helvetica		n019043l.afm
-afmconv HI	Helvetica-Oblique	n019063l.afm
+afmconv HI	Helvetica-Oblique	n019063l.afm -b
 afmconv HB	Helvetica-Bold		n019044l.afm
-afmconv HX	Helvetica-BoldOblique	n019064l.afm
+afmconv HX	Helvetica-BoldOblique	n019064l.afm -b
 afmconv Hr	Helvetica-Narrow	n019043l.afm
-afmconv Hi	Helvetica-Narrow-Oblique	n019063l.afm
+afmconv Hi	Helvetica-Narrow-Oblique	n019063l.afm -b
 afmconv Hb	Helvetica-Narrow-Bold	n019044l.afm
-afmconv Hx	Helvetica-Narrow-BoldOblique	n019064l.afm
+afmconv Hx	Helvetica-Narrow-BoldOblique	n019064l.afm -b
 afmconv KR	Bookman-Light		b018012l.afm
-afmconv KI	Bookman-LightItalic	b018032l.afm
+afmconv KI	Bookman-LightItalic	b018032l.afm -b
 afmconv KB	Bookman-Demi		b018015l.afm
-afmconv KX	Bookman-DemiItalic	b018035l.afm
+afmconv KX	Bookman-DemiItalic	b018035l.afm -b
 afmconv NR	NewCenturySchlbk-Roman	c059013l.afm
-afmconv NI	NewCenturySchlbk-Italic	c059033l.afm
+afmconv NI	NewCenturySchlbk-Italic	c059033l.afm -b
 afmconv NB	NewCenturySchlbk-Bold	c059016l.afm
-afmconv NX	NewCenturySchlbk-BoldItalic	c059036l.afm
+afmconv NX	NewCenturySchlbk-BoldItalic	c059036l.afm -b
 afmconv PA	Palatino-Roman		p052003l.afm
 afmconv PR	Palatino-Roman		p052003l.afm
-afmconv PI	Palatino-Italic		p052023l.afm
+afmconv PI	Palatino-Italic		p052023l.afm -b
 afmconv PB	Palatino-Bold		p052004l.afm
-afmconv PX	Palatino-BoldItalic	p052024l.afm
+afmconv PX	Palatino-BoldItalic	p052024l.afm -b
 afmconv C	Courier			n022003l.afm
 afmconv CO	Courier			n022003l.afm
 afmconv CW	Courier			n022003l.afm
-afmconv CI	Courier-Oblique		n022023l.afm
+afmconv CI	Courier-Oblique		n022023l.afm -b
 afmconv CB	Courier-Bold		n022004l.afm
-afmconv CX	Courier-BoldOblique	n022024l.afm
+afmconv CX	Courier-BoldOblique	n022024l.afm -b
 afmconv ZI	ZapfChancery-MediumItalic	z003034l.afm
 afmconv ZD	ZapfDingbats		d050000l.afm
 
--- a/mktrfn.c
+++ b/mktrfn.c
@@ -29,7 +29,7 @@
 		}
 		if (!strcmp("char", cmd)) {
 			scanf("%s width %s", ch, wid);
-			trfn_char(ch, NULL, atoi(wid), -1);
+			trfn_char(ch, NULL, atoi(wid), -1, 0, 0, 0, 0);
 		}
 		if (!strcmp("kernpair", cmd)) {
 			scanf("%s %s width %s", c1, c2, wid);
@@ -42,12 +42,25 @@
 	}
 }
 
+static char *afm_charfield(char *s, char *d)
+{
+	while (*s && !isspace(*s) && *s != ';')
+		*d++ = *s++;
+	while (isspace(*s) || *s == ';')
+		s++;
+	*d = '\0';
+	return s;
+}
+
 static void afm_read(void)
 {
-	char ch[TOKLEN], pos[TOKLEN];
-	char c1[TOKLEN], c2[TOKLEN];
-	char wid[TOKLEN];
 	char ln[1024];
+	char ch[TOKLEN] = "", pos[TOKLEN] = "";
+	char c1[TOKLEN] = "", c2[TOKLEN] = "";
+	char wid[TOKLEN] = "", field[TOKLEN] = "";
+	char llx[TOKLEN] = "0", lly[TOKLEN] = "0";
+	char urx[TOKLEN] = "0", ury[TOKLEN] = "0";
+	char *s;
 	while (fgets(ln, sizeof(ln), stdin)) {
 		if (ln[0] == '#')
 			continue;
@@ -64,8 +77,38 @@
 			continue;
 		if (!strncmp("EndCharMetrics", ln, 14))
 			break;
-		if (sscanf(ln, "C %s ; WX %s ; N %s", pos, wid, ch) == 3)
-			trfn_char(ch, pos, atoi(wid), -1);
+		s = ln;
+		while (*s) {
+			s = afm_charfield(s, field);
+			if (!strcmp("C", field)) {
+				s = afm_charfield(s, pos);
+				continue;
+			}
+			if (!strcmp("WX", field)) {
+				s = afm_charfield(s, wid);
+				continue;
+			}
+			if (!strcmp("N", field)) {
+				s = afm_charfield(s, ch);
+				continue;
+			}
+			if (!strcmp("B", field)) {
+				s = afm_charfield(s, llx);
+				s = afm_charfield(s, lly);
+				s = afm_charfield(s, urx);
+				s = afm_charfield(s, ury);
+				continue;
+			}
+			if (!strcmp("L", field)) {
+				s = afm_charfield(s, c1);
+				s = afm_charfield(s, c2);
+				continue;
+			}
+			break;
+		}
+		if (ch[0] && pos[0] && wid[0])
+			trfn_char(ch, pos, atoi(wid), -1,
+				atoi(llx), atoi(lly), atoi(urx), atoi(ury));
 	}
 	while (fgets(ln, sizeof(ln), stdin)) {
 		if (ln[0] == '#')
@@ -92,7 +135,8 @@
 	"  -p name \toverride font postscript name\n"
 	"  -t name \tset font troff name\n"
 	"  -r res  \tset device resolution (720)\n"
-	"  -k kmin \tspecify the minimum amount of kerning (0)\n";
+	"  -k kmin \tspecify the minimum amount of kerning (0)\n"
+	"  -b      \tinclude glyph bounding box\n";
 
 int main(int argc, char *argv[])
 {
@@ -101,6 +145,7 @@
 	int res = 720;
 	int spc = 0;
 	int kmin = 0;
+	int bbox = 0;
 	for (i = 1; i < argc && argv[i][0] == '-'; i++) {
 		switch (argv[i][1]) {
 		case 'a':
@@ -124,12 +169,15 @@
 		case 't':
 			trfn_trfont(argv[i][2] ? argv[i] + 2 : argv[++i]);
 			break;
+		case 'b':
+			bbox = 1;
+			break;
 		default:
 			printf("%s", usage);
 			return 0;
 		}
 	}
-	trfn_init(res, spc, kmin);
+	trfn_init(res, spc, kmin, bbox);
 	if (afm)
 		afm_read();
 	else
--- a/trfn.c
+++ b/trfn.c
@@ -22,6 +22,7 @@
 static int trfn_swid;		/* space width */
 static int trfn_special;	/* special flag */
 static int trfn_kmin;		/* minimum kerning value */
+static int trfn_bbox;		/* include bounding box */
 static char trfn_ligs[8192];	/* font ligatures */
 static char trfn_trname[256];	/* font troff name */
 static char trfn_psname[256];	/* font ps name */
@@ -285,7 +286,8 @@
 	return t ? t->type : 3;
 }
 
-void trfn_char(char *psname, char *n, int wid, int typ)
+void trfn_char(char *psname, char *n, int wid, int typ,
+		int llx, int lly, int urx, int ury)
 {
 	char uc[GNLEN];			/* mapping unicode character */
 	char *a_ps[NPSAL] = {NULL};	/* postscript glyph substitutions */
@@ -313,8 +315,11 @@
 		}
 		if (strcmp("---", uc))
 			trfn_lig(uc);
-		sbuf_printf(&sbuf_char, "char %s\t%d\t%d\t%s\t%s\n",
-				uc, WX(wid), typ, psname, pos);
+		sbuf_printf(&sbuf_char, "char %s\t%d", uc, WX(wid));
+		if (trfn_bbox && (llx || lly || urx || ury))
+			sbuf_printf(&sbuf_char, ",%d,%d,%d,%d",
+				WX(llx), WX(lly), WX(urx), WX(ury));
+		sbuf_printf(&sbuf_char, "\t%d\t%s\t%s\n", typ, psname, pos);
 		a_tr = tab_get(tab_alts, uc);
 		while (a_tr && *a_tr)
 			sbuf_printf(&sbuf_char, "char %s\t\"\n", *a_tr++);
@@ -353,12 +358,13 @@
 	printf("%s", sbuf_buf(&sbuf_kern));
 }
 
-void trfn_init(int res, int spc, int kmin)
+void trfn_init(int res, int spc, int kmin, int bbox)
 {
 	int i;
 	trfn_div = 7200 / res;
 	trfn_special = spc;
 	trfn_kmin = kmin;
+	trfn_bbox = bbox;
 	if (agl_read("glyphlist.txt"))
 		fprintf(stderr, "mktrfn: could not open glyphlist.txt\n");
 	sbuf_init(&sbuf_char);
--- a/trfn.h
+++ b/trfn.h
@@ -1,8 +1,9 @@
-void trfn_init(int res, int special, int kmin);
+void trfn_init(int res, int special, int kmin, int bbox);
 void trfn_done(void);
 void trfn_trfont(char *name);
 void trfn_psfont(char *fontname);
 void trfn_print(void);
-void trfn_char(char *c, char *n, int wid, int typ);
+void trfn_char(char *c, char *n, int wid, int typ,
+		int llx, int lly, int urx, int ury);
 void trfn_kern(char *c1, char *c2, int x);
 void trfn_sub(char *c1, char *c2);