shithub: neatpost

Download patch

ref: 76db4d3edc8fb8e1c2d6afb8c21bc2894307f43a
parent: 0557bd6c8fa6cf90da90dfb48c60c84889514888
author: Ali Gholami Rudi <ali@rudi.ir>
date: Wed May 2 21:36:07 EDT 2018

post: support x X info

The following sets document title in Neatroff after this change:
  \X'info Title Neatroff Introduction'

--- a/pdf.c
+++ b/pdf.c
@@ -8,7 +8,8 @@
 #include <unistd.h>
 #include "post.h"
 
-static char *pdf_title;		/* document title */
+static char pdf_title[256];	/* document title */
+static char pdf_author[256];	/* document author */
 static int pdf_width;		/* page width */
 static int pdf_height;		/* page height */
 static int pdf_pages;		/* pages object id */
@@ -483,31 +484,10 @@
 {
 }
 
-void outeps(char *eps)
+void outeps(char *eps, int hwid, int vwid)
 {
 }
 
-static char *strcut(char *dst, char *src)
-{
-	while (*src == ' ' || *src == '\n')
-		src++;
-	if (src[0] == '"') {
-		src++;
-		while (*src && (src[0] != '"' || src[1] == '"')) {
-			if (*src == '"')
-				src++;
-			*dst++ = *src++;
-		}
-		if (*src == '"')
-			src++;
-	} else {
-		while (*src && *src != ' ' && *src != '\n')
-			*dst++ = *src++;
-	}
-	*dst = '\0';
-	return src;
-}
-
 /* return a copy of a PDF object; returns a static buffer */
 static char *pdf_copy(char *pdf, int len, int pos)
 {
@@ -700,23 +680,12 @@
 	return xobj_n - 1;
 }
 
-void outpdf(char *spec)
+void outpdf(char *pdf, int hwid, int vwid)
 {
-	char pdf[1 << 12];
 	char buf[1 << 12];
 	struct sbuf *sb;
-	int hwid, vwid, nspec;
 	int xobj_id;
 	int fd, nr;
-	spec = strcut(pdf, spec);
-	if (!pdf[0])
-		return;
-	/* requested image dimensions */
-	nspec = sscanf(spec, "%d %d", &hwid, &vwid);
-	if (nspec < 1)
-		hwid = 0;
-	if (nspec < 2)
-		vwid = 0;
 	/* reading the pdf file */
 	sb = sbuf_make();
 	fd = open(pdf, O_RDONLY);
@@ -734,16 +703,10 @@
 	p_v = -1;
 }
 
-void outlink(char *spec)
+void outlink(char *lnk, int hwid, int vwid)
 {
-	char lnk[1 << 12];
-	int hwid, vwid;
-	int nspec;
 	if (ann_n == LEN(ann))
 		return;
-	spec = strcut(lnk, spec);
-	if (!lnk[0] || (nspec = sscanf(spec, "%d %d", &hwid, &vwid)) != 2)
-		return;
 	o_flush();
 	ann[ann_n++] = obj_beg(0);
 	pdfout("<<\n");
@@ -757,6 +720,14 @@
 	obj_end();
 }
 
+void outinfo(char *kwd, char *val)
+{
+	if (!strcmp("Author", kwd))
+		snprintf(pdf_author, sizeof(pdf_author), "%s", val);
+	if (!strcmp("Title", kwd))
+		snprintf(pdf_title, sizeof(pdf_title), "%s", val);
+}
+
 void outpage(void)
 {
 	o_v = 0;
@@ -878,11 +849,11 @@
 
 void docheader(char *title, int pagewidth, int pageheight, int linewidth)
 {
-	pdf_title = title;
+	if (title)
+		outinfo("Author", title);
 	obj_map();
 	pdf_root = obj_map();
 	pdf_pages = obj_map();
-	pdf_title = title;
 	pdfout("%%PDF-1.6\n\n");
 	pdf_width = (pagewidth * 72 + 127) / 254;
 	pdf_height = (pageheight * 72 + 127) / 254;
@@ -917,8 +888,10 @@
 	/* info object */
 	info_id = obj_beg(0);
 	pdfout("<<\n");
-	if (pdf_title)
+	if (pdf_title[0])
 		pdfout("  /Title (%s)\n", pdf_title);
+	if (pdf_author[0])
+		pdfout("  /Author (%s)\n", pdf_author);
 	pdfout("  /Creator (Neatroff)\n");
 	pdfout("  /Producer (Neatpost)\n");
 	pdfout(">>\n");
--- a/post.c
+++ b/post.c
@@ -248,6 +248,27 @@
 	nexteol();
 }
 
+static char *strcut(char *dst, char *src)
+{
+	while (*src == ' ' || *src == '\n')
+		src++;
+	if (src[0] == '"') {
+		src++;
+		while (*src && (src[0] != '"' || src[1] == '"')) {
+			if (*src == '"')
+				src++;
+			*dst++ = *src++;
+		}
+		if (*src == '"')
+			src++;
+	} else {
+		while (*src && *src != ' ' && *src != '\n')
+			*dst++ = *src++;
+	}
+	*dst = '\0';
+	return src;
+}
+
 static void postps(void)
 {
 	char cmd[ILNLEN];
@@ -258,12 +279,46 @@
 		out("%s\n", arg);
 	if (!strcmp("rotate", cmd))
 		outrotate(atoi(arg));
-	if (!strcmp("eps", cmd))
-		outeps(arg);
-	if (!strcmp("pdf", cmd))
-		outpdf(arg);
-	if (!strcmp("link", cmd))
-		outlink(arg);
+	if (!strcmp("eps", cmd) || !strcmp("pdf", cmd)) {
+		char path[1 << 12];
+		int hwid, vwid, nspec;
+		char *spec = arg;
+		spec = strcut(path, spec);
+		nspec = sscanf(spec, "%d %d", &hwid, &vwid);
+		if (nspec < 1)
+			hwid = 0;
+		if (nspec < 2)
+			vwid = 0;
+		if (path[0] && !strcmp("eps", cmd))
+			outeps(path, hwid, vwid);
+		if (path[0] && !strcmp("pdf", cmd))
+			outpdf(path, hwid, vwid);
+	}
+	if (!strcmp("link", cmd)) {
+		char link[1 << 12];
+		int hwid, vwid, nspec;
+		char *spec = arg;
+		spec = strcut(link, spec);
+		nspec = sscanf(spec, "%d %d", &hwid, &vwid);
+		if (link[0] && nspec == 2)
+			outlink(link, hwid, vwid);
+	}
+	if (!strcmp("info", cmd)) {
+		char *spec = arg;
+		char kwd[128];
+		int i = 0;
+		while (*spec == ' ')
+			spec++;
+		while (*spec && *spec != ' ') {
+			if (i < sizeof(kwd) - 1)
+				kwd[i++] = *spec;
+			spec++;
+		}
+		kwd[i] = '\0';
+		while (*spec == ' ')
+			spec++;
+		outinfo(kwd, spec);
+	}
 	if (!strcmp("BeginObject", cmd))
 		drawmbeg(arg);
 	if (!strcmp("EndObject", cmd))
--- a/post.h
+++ b/post.h
@@ -57,9 +57,9 @@
 void outsize(int s);
 void outcolor(int c);
 void outrotate(int deg);
-void outeps(char *spec);
-void outpdf(char *spec);
-void outlink(char *spec);
+void outeps(char *eps, int hwid, int vwid);
+void outpdf(char *pdf, int hwid, int vwid);
+void outlink(char *dst, int hwid, int vwid);
 void outpage(void);
 void outmnt(int f);
 void outgname(int g);
--- a/ps.c
+++ b/ps.c
@@ -5,6 +5,8 @@
 #include <string.h>
 #include "post.h"
 
+static char ps_title[256];	/* document title */
+static char ps_author[256];	/* document author */
 static int o_f, o_s, o_m;	/* font and size */
 static int o_h, o_v;		/* current user position */
 static int p_f, p_s, p_m;	/* output postscript font */
@@ -255,43 +257,12 @@
 	outrel(h1, v1);
 }
 
-static char *strcut(char *dst, char *src)
+void outeps(char *eps, int hwid, int vwid)
 {
-	while (*src == ' ' || *src == '\n')
-		src++;
-	if (src[0] == '"') {
-		src++;
-		while (*src && (src[0] != '"' || src[1] == '"')) {
-			if (*src == '"')
-				src++;
-			*dst++ = *src++;
-		}
-		if (*src == '"')
-			src++;
-	} else {
-		while (*src && *src != ' ' && *src != '\n')
-			*dst++ = *src++;
-	}
-	*dst = '\0';
-	return src;
-}
-
-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;
-	spec = strcut(eps, spec);
-	if (!eps[0])
-		return;
-	nspec = sscanf(spec, "%d %d", &hwid, &vwid);
-	if (nspec < 1)
-		hwid = 0;
-	if (nspec < 2)
-		vwid = 0;
+	int nbb;
 	if (!(filp = fopen(eps, "r")))
 		return;
 	if (!fgets(buf, sizeof(buf), filp) ||
@@ -330,18 +301,12 @@
 	outf("EPSFEND\n");
 }
 
-void outpdf(char *spec)
+void outpdf(char *pdf, int hwid, int vwid)
 {
 }
 
-void outlink(char *spec)
+void outlink(char *lnk, int hwid, int vwid)
 {
-	char lnk[1 << 12];
-	int hwid, vwid;
-	int nspec;
-	spec = strcut(lnk, spec);
-	if (!lnk[0] || (nspec = sscanf(spec, "%d %d", &hwid, &vwid)) != 2)
-		return;
 	o_flush();
 	if (lnk[0] == '#' || isdigit((unsigned char) lnk[0])) {
 		outf("[ /Rect [ %d %d t %d %d t ] %s%s "
@@ -357,6 +322,14 @@
 	}
 }
 
+void outinfo(char *kwd, char *val)
+{
+	if (!strcmp("Author", kwd))
+		snprintf(ps_author, sizeof(ps_author), "%s", val);
+	if (!strcmp("Title", kwd))
+		snprintf(ps_title, sizeof(ps_title), "%s", val);
+}
+
 void docpagebeg(int n)
 {
 	out("%%%%Page: %d %d\n", n, n);
@@ -374,6 +347,12 @@
 
 void doctrailer(int pages)
 {
+	out("[");
+	if (ps_title[0])
+		out(" /Title (%s)", ps_title);
+	if (ps_author[0])
+		out(" /Author (%s)", ps_author);
+	out("/Creator (Neatroff) /DOCINFO pdfmark\n");
 	out("%%%%Trailer\n");
 	out("done\n");
 	out("%%%%DocumentFonts: %s\n", o_fonts);