shithub: neatpost

Download patch

ref: 272cc55e2f6be4ae04e0f06f4dafa6cb9e132727
parent: d6306656f955b87f37634bcc5f6bd699b71fdfb1
author: Ali Gholami Rudi <ali@rudi.ir>
date: Sat Jul 19 09:50:32 EDT 2014

post: include eps files with "x X eps epsfile [width [height]]"

In neatroff \X'esp epsfile' includes the given EPS file with its lower
left corner at the current point.  For \X'epsfile w h' the EPS file is
scaled such that the width of its bounding box equals w and its height
equals h.  If h is missing, the height of the bounding box is scaled
as its width.

--- a/out.c
+++ b/out.c
@@ -245,3 +245,49 @@
 		o_h + h1 + h2, o_v + v1 + v2);
 	outrel(h1, v1);
 }
+
+void outeps(char *spec)
+{
+	char eps[1 << 12];
+	char buf[1 << 12];
+	int llx, lly, urx, ury;
+	int hwid, vwid;
+	FILE *filp;
+	int nspec, nbb;
+	nspec = sscanf(spec, "%s %d %d", eps, &hwid, &vwid);
+	if (nspec < 1)
+		return;
+	if (!(filp = fopen(eps, "r")))
+		return;
+	if (!fgets(buf, sizeof(buf), filp) ||
+			(strcmp(buf, "%!PS-Adobe-2.0 EPSF-2.0\n") &&
+			strcmp(buf, "%!PS-Adobe-3.0 EPSF-3.0\n"))) {
+		fclose(filp);
+		return;
+	}
+	nbb = 0;
+	while (fgets(buf, sizeof(buf), filp))
+		if (!strncmp(buf, "%%BoundingBox: ", 15))
+			if ((nbb = sscanf(buf + 15, "%d %d %d %d",
+					&llx, &lly, &urx, &ury)) == 4)
+				break;
+	fclose(filp);
+	if (nbb < 4)		/* no BoundingBox comment */
+		return;
+	if (nspec == 1)
+		hwid = urx - llx;
+	if (nspec <= 2)
+		vwid = (ury - lly) * hwid / (urx - llx);
+	/* output the EPS file */
+	o_flush();
+	out_fontup(o_f);
+	outf("%d %d %d %d %d %d %d %d EPSFBEG\n",
+		llx, lly, hwid, urx - llx, vwid, ury - lly, o_h, o_v);
+	outf("%%%%BeginDocument: %s\n", eps);
+	filp = fopen(eps, "r");
+	while (fgets(buf, sizeof(buf), filp))
+		out("%s", buf);
+	fclose(filp);
+	outf("%%%%EndDocument\n");
+	outf("EPSFEND\n");
+}
--- a/post.c
+++ b/post.c
@@ -211,6 +211,8 @@
 		out("%s\n", arg);
 	if (!strcmp("rotate", cmd))
 		outrotate(atoi(arg));
+	if (!strcmp("eps", cmd))
+		outeps(arg);
 	if (!strcmp("BeginObject", cmd))
 		drawmbeg(arg);
 	if (!strcmp("EndObject", cmd))
--- a/post.h
+++ b/post.h
@@ -71,6 +71,7 @@
 void outsize(int s);
 void outcolor(int c);
 void outrotate(int deg);
+void outeps(char *eps);
 void outpage(void);
 void outmnt(int f);
 extern char o_fonts[];
--- a/ps.c
+++ b/ps.c
@@ -95,6 +95,24 @@
 	"	x1 x2 add 2 div\n"
 	"	y1 y2 add -2 div\n"
 	"	curveto\n"
+	"} bind def\n"
+	"% including EPS files\n"
+	"/EPSFBEG {\n"
+	"	/epsf_state save def\n"
+	"	neg translate\n"
+	"	div 3 1 roll div exch scale\n"
+	"	neg exch neg exch translate\n"
+	"	/dict_count countdictstack def\n"
+	"	/op_count count 1 sub def\n"
+	"	userdict begin\n"
+	"	/showpage { } def\n"
+	"	0 setgray 0 setlinecap 1 setlinewidth 0 setlinejoin\n"
+	"	10 setmiterlimit [ ] 0 setdash newpath\n"
+	"} bind def\n"
+	"/EPSFEND {\n"
+	"	count op_count sub {pop} repeat\n"
+	"	countdictstack dict_count sub {end} repeat\n"
+	"	epsf_state restore\n"
 	"} bind def\n";
 
 /* pagewidth and pageheight are in tenths of a millimetre */