shithub: neatpost

Download patch

ref: c6cca8873d5a2f4fa5682984d5ac454dcf4d3ec3
parent: 0df35af1c2bbe3ebf58e9fa93abf3a609b4da6ee
author: Ali Gholami Rudi <ali@rudi.ir>
date: Mon Jun 16 20:41:07 EDT 2014

post: add -p for specifying paper size

Valid arguments of -p are ISO paper sizes (e.g., a4), custom paper
sizes in millimetres (e.g., 210x297), and other predefined paper sizes
like letter.  Also -w specifies drawing line thickness as in Groff.

--- a/post.c
+++ b/post.c
@@ -11,7 +11,10 @@
 #include <string.h>
 #include "post.h"
 
-static int o_pg;
+static int ps_pagewidth = 216;	/* page width */
+static int ps_pageheight = 279;	/* page height */
+static int ps_linewidth = 40;	/* drawing line thickness in thousandths of an em */
+static int o_pages;		/* output pages */
 
 static int next(void)
 {
@@ -233,7 +236,7 @@
 			fprintf(stderr, "neatpost: cannot open device %s\n", devname);
 			exit(1);
 		}
-		ps_header();
+		ps_header(ps_pagewidth, ps_pageheight, ps_linewidth);
 		break;
 	case 'T':
 		nextword(devname);
@@ -291,10 +294,10 @@
 		outc(cs);
 		break;
 	case 'p':
-		if (o_pg)
-			ps_pageend(o_pg);
-		o_pg = nextnum();
-		ps_pagebeg(o_pg);
+		if (o_pages)
+			ps_pageend(o_pages);
+		o_pages = nextnum();
+		ps_pagebeg(o_pages);
 		outpage();
 		break;
 	case 'w':
@@ -323,14 +326,57 @@
 	while ((c = next()) >= 0)
 		if (!isspace(c))
 			postcmd(c);
-	if (o_pg)
-		ps_pageend(o_pg);
+	if (o_pages)
+		ps_pageend(o_pages);
 }
 
+#define ISODIM(n, d1, d2)	((((n) & 1) ? d2 : d1) >> (n >> 1))
+
+static void setpagesize(char *s)
+{
+	int d1, d2, n;
+	/* predefined paper sizes */
+	if (!strcmp("letter", s)) {
+		ps_pagewidth = 216;
+		ps_pageheight = 279;
+		return;
+	}
+	/* custom paper size in mm, like 210x297 for a4 */
+	if (isdigit(s[0]) && strchr(s, 'x')) {
+		ps_pagewidth = atoi(s);
+		ps_pageheight = atoi(strchr(s, 'x') + 1);
+		return;
+	}
+	/* ISO paper sizes */
+	if (!isdigit(s[1]))
+		return;
+	n = s[1] - '0';
+	switch (tolower(s[0])) {
+	case 'a':
+		d1 = 841;
+		d2 = 1189;
+		break;
+	case 'b':
+		d1 = 1000;
+		d2 = 1414;
+		break;
+	case 'c':
+		d1 = 917;
+		d2 = 1297;
+		break;
+	default:
+		return;
+	}
+	ps_pagewidth = ((n & 1) ? d2 : d1) >> ((n + 1) >> 1);
+	ps_pageheight = ((n & 1) ? d1 : d2) >> (n >> 1);
+}
+
 static char *usage =
-	"Usage: neatpost [options] <input >output\n\n"
+	"Usage: neatpost [options] <input >output\n"
 	"Options:\n"
-	"  -Fdir \tset font directory (" TROFFFDIR ")\n";
+	"  -F dir  \tset font directory (" TROFFFDIR ")\n"
+	"  -p size \tset paper size (a4)\n"
+	"  -w lwid \tdrawing line thickness in thousandths of an em\n";
 
 int main(int argc, char *argv[])
 {
@@ -338,6 +384,10 @@
 	for (i = 1; i < argc; i++) {
 		if (argv[i][0] == '-' && argv[i][1] == 'F') {
 			strcpy(devpath, argv[i][2] ? argv[i] + 2 : argv[++i]);
+		} else if (argv[i][0] == '-' && argv[i][1] == 'p') {
+			setpagesize(argv[i][2] ? argv[i] + 2 : argv[++i]);
+		} else if (argv[i][0] == '-' && argv[i][1] == 'w') {
+			ps_linewidth = atoi(argv[i][2] ? argv[i] + 2 : argv[++i]);
 		} else {
 			printf("%s", usage);
 			return 0;
@@ -344,6 +394,6 @@
 		}
 	}
 	post();
-	ps_trailer(o_pg, o_fonts);
+	ps_trailer(o_pages, o_fonts);
 	return 0;
 }
--- a/post.h
+++ b/post.h
@@ -85,7 +85,7 @@
 void draws(int h1, int v1, int h2, int v2);
 
 /* postscript functions */
-void ps_header(void);
+void ps_header(int pagewidth, int pageheight, int linewidth);
 void ps_trailer(int pages, char *fonts);
 void ps_pagebeg(int n);
 void ps_pageend(int n);
--- a/ps.c
+++ b/ps.c
@@ -25,9 +25,6 @@
 }
 
 static char *prolog =
-	"/linewidth .4 def\n"
-	"/pagesize [612 792] def\n"
-	"\n"
 	"/setup {\n"
 	"	counttomark 2 idiv {def} repeat pop\n"
 	"	/scaling 72 resolution div def\n"
@@ -119,7 +116,7 @@
 	"	curveto\n"
 	"} bind def\n";
 
-void ps_header(void)
+void ps_header(int pagewidth, int pageheight, int linewidth)
 {
 	out("%%!PS-Adobe-2.0\n");
 	out("%%%%Version: 1.0\n");
@@ -130,6 +127,9 @@
 
 	out("%%%%BeginProlog\n");
 	out("/resolution %d def\n", dev_res);
+	out("/pagesize [%d %d] def\n", (pagewidth * 720 + 127) / 254,
+					(pageheight * 720 + 127) / 254);
+	out("/linewidth %d.%02d def\n\n", linewidth / 100, linewidth % 100);
 	out("%s", prolog);
 	out("%%%%EndProlog\n");
 	out("%%%%BeginSetup\n");