shithub: neatpost

Download patch

ref: 890599cc49cc9cfd2a3f38367c321b05349d4276
parent: e154209b4a25647bab193519d269cf59f8b33910
author: Ali Gholami Rudi <ali@rudi.ir>
date: Fri Jun 14 19:14:35 EDT 2013

post: support "x X PS"

It also handles BeginObject and EndObject; this should be enough
for pic's fill operator.

--- a/out.c
+++ b/out.c
@@ -166,20 +166,45 @@
 	o_s = s;
 }
 
-void drawbeg(void)
+static int draw_path;	/* number of path segments */
+static int draw_point;	/* point was set for postscript newpath */
+
+static void drawmv(void)
 {
+	if (!draw_point)
+		outf("%d %d m ", o_h, o_v);
+	draw_point = 1;
+}
+
+/* if s is not NULL, start a multi-segment path */
+void drawbeg(char *s)
+{
 	o_flush();
 	out_fontup(o_f);
-	outf("newpath %d %d m ", o_h, o_v);
+	if (draw_path)
+		return;
+	draw_path = s != NULL;
+	if (s)
+		outf("gsave newpath %s\n", s);
+	else
+		outf("newpath ");
 }
 
-void drawend(void)
+void drawend(char *s)
 {
-	outf("stroke\n");
+	if (draw_path && !s)
+		return;
+	draw_path = 0;
+	draw_point = 0;
+	if (s)
+		outf("%s grestore\n", s);
+	else
+		outf("stroke\n");
 }
 
 void drawl(int h, int v)
 {
+	drawmv();
 	outrel(h, v);
 	outf("%d %d drawl ", o_h, o_v);
 }
@@ -186,6 +211,7 @@
 
 void drawc(int c)
 {
+	drawmv();
 	outrel(c, 0);
 	outf("%d %d drawe ", c, c);
 }
@@ -192,6 +218,7 @@
 
 void drawe(int h, int v)
 {
+	drawmv();
 	outrel(h, 0);
 	outf("%d %d drawe ", h, v);
 }
@@ -198,6 +225,7 @@
 
 void drawa(int h1, int v1, int h2, int v2)
 {
+	drawmv();
 	outf("%d %d %d %d drawa ", h1, v1, h2, v2);
 	outrel(h1 + h2, v1 + v2);
 }
@@ -204,6 +232,7 @@
 
 void draws(int h1, int v1, int h2, int v2)
 {
+	drawmv();
 	outf("%d %d %d %d %d %d draws ", o_h, o_v, o_h + h1, o_v + v1,
 		o_h + h1 + h2, o_v + v1 + v2);
 	outrel(h1, v1);
--- a/post.c
+++ b/post.c
@@ -5,8 +5,9 @@
  *
  * This program is released under the Modified BSD license.
  */
-#include <stdio.h>
 #include <ctype.h>
+#include <stdio.h>
+#include <string.h>
 #include "post.h"
 
 static int o_pg;
@@ -117,6 +118,20 @@
 	*s = '\0';
 }
 
+/* read until eol */
+static void readln(char *s)
+{
+	int c;
+	c = next();
+	while (c > 0 && c != '\n') {
+		*s++ = c;
+		c = next();
+	}
+	if (c == '\n')
+		back(c);
+	*s = '\0';
+}
+
 static void postspline(int h1, int v1)
 {
 	int h2, v2;
@@ -134,7 +149,7 @@
 {
 	int h1, h2, v1, v2;
 	int c = next();
-	drawbeg();
+	drawbeg(NULL);
 	switch (c) {
 	case 'l':
 		h1 = nextnum();
@@ -165,10 +180,24 @@
 			postspline(h1, v1);
 		break;
 	}
-	drawend();
+	drawend(NULL);
 	nexteol();
 }
 
+static void postps(void)
+{
+	char cmd[ILNLEN];
+	char arg[ILNLEN];
+	nextword(cmd);
+	readln(arg);
+	if (!strcmp("PS", cmd) || !strcmp("ps", cmd))
+		out("%s\n", arg);
+	if (!strcmp("BeginObject", cmd))
+		drawbeg(arg);
+	if (!strcmp("EndObject", cmd))
+		drawend(arg);
+}
+
 static char devpath[PATHLEN] = "devutf";
 
 static void postx(void)
@@ -193,6 +222,7 @@
 	case 's':
 		break;
 	case 'X':
+		postps();
 		break;
 	}
 	nexteol();
--- a/post.h
+++ b/post.h
@@ -4,7 +4,7 @@
 #define FNLEN		32	/* font name length */
 #define NGLYPHS		512	/* glyphs in fonts */
 #define GNLEN		32	/* glyph name length */
-#define ILNLEN		256	/* line limit of input files */
+#define ILNLEN		1000	/* line limit of input files */
 #define LNLEN		4000	/* line buffer length (ren.c/out.c) */
 
 #define MIN(a, b)	((a) < (b) ? (a) : (b))
@@ -64,8 +64,8 @@
 void outpage(void);
 extern char o_fonts[];
 
-void drawbeg(void);
-void drawend(void);
+void drawbeg(char *s);
+void drawend(char *s);
 void drawl(int h, int v);
 void drawc(int c);
 void drawe(int h, int v);