shithub: neatpost

Download patch

ref: 957e74b6757ff6dac220d494c1bd4fbee8d0a01b
parent: 1c3135b842d4715d7458c8118d9a95ecb79112ff
author: Ali Gholami Rudi <ali@rudi.ir>
date: Tue May 31 13:30:12 EDT 2016

post: extend Dp command to include arcs or splines

--- a/post.c
+++ b/post.c
@@ -75,8 +75,8 @@
 	nextskip();
 	while (1) {
 		c = next();
-		if (!n && c == '-') {
-			neg = 1;
+		if (!n && (c == '-' || c == '+')) {
+			neg = c == '-';
 			continue;
 		}
 		if (!isdigit(c))
@@ -88,6 +88,20 @@
 	return neg ? -n : n;
 }
 
+static int readnum(int *n)
+{
+	int c;
+	do {
+		c = next();
+	} while (c == ' ');
+	back(c);
+	if (c == '-' || c == '+' || (c >= '0' && c <= '9')) {
+		*n = nextnum();
+		return 0;
+	}
+	return 1;
+}
+
 static int iseol(void)
 {
 	int c;
@@ -138,13 +152,17 @@
 static void postline(void)
 {
 	int h, v;
-	while (!iseol()) {
-		h = nextnum();
-		v = nextnum();
+	while (!readnum(&h) && !readnum(&v))
 		drawl(h, v);
-	}
 }
 
+static void postarc(void)
+{
+	int h1, v1, h2, v2;
+	if (!readnum(&h1) && !readnum(&v1) && !readnum(&h2) && !readnum(&v2))
+		drawa(h1, v1, h2, v2);
+}
+
 static void postspline(void)
 {
 	int h2, v2;
@@ -154,9 +172,7 @@
 		drawl(h1, v1);
 		return;
 	}
-	while (!iseol()) {
-		h2 = nextnum();
-		v2 = nextnum();
+	while (!readnum(&h2) && !readnum(&v2)) {
 		draws(h1, v1, h2, v2);
 		h1 = h2;
 		v1 = v2;
@@ -164,9 +180,33 @@
 	draws(h1, v1, 0, 0);
 }
 
+static void postpoly(void)
+{
+	int l = 'l';
+	int c;
+	while (!iseol() && (l == 'l' || l == '~' || l == 'a')) {
+		do {
+			c = next();
+		} while (c == ' ');
+		back(c);
+		if (c != '-' && c != '+' && (c < '0' || c > '9')) {
+			l = c;
+			while (c >= 0 && !isspace(c))
+				c = next();
+			continue;
+		}
+		if (l == 'l')
+			postline();
+		if (l == '~')
+			postspline();
+		if (l == 'a')
+			postarc();
+	}
+}
+
 static void postdraw(void)
 {
-	int h1, h2, v1, v2;
+	int h1, v1;
 	int c = next();
 	drawbeg();
 	switch (tolower(c)) {
@@ -184,17 +224,13 @@
 		drawe(h1, v1);
 		break;
 	case 'a':
-		h1 = nextnum();
-		v1 = nextnum();
-		h2 = nextnum();
-		v2 = nextnum();
-		drawa(h1, v1, h2, v2);
+		postarc();
 		break;
 	case '~':
 		postspline();
 		break;
 	case 'p':
-		postline();
+		postpoly();
 		break;
 	}
 	drawend(c == 'p' || c == 'P', c == 'E' || c == 'C' || c == 'P');