2 * neatpost troff postscript postprocessor
4 * Copyright (C) 2013-2014 Ali Gholami Rudi <ali at rudi dot ir>
6 * This program is released under the Modified BSD license.
14 static int ps_pagewidth
= 216; /* page width */
15 static int ps_pageheight
= 279; /* page height */
16 static int ps_linewidth
= 40; /* drawing line thickness in thousandths of an em */
17 static int o_pages
; /* output pages */
24 static void back(int c
)
29 static int utf8len(int c
)
46 static int nextutf8(char *s
)
54 for (i
= 1; i
< l
; i
++)
61 static void nextskip(void)
70 static int nextnum(void)
84 if (c
< 0 || !isdigit(c
))
91 static int iseol(void)
101 /* skip until the end of line */
102 static void nexteol(void)
107 } while (c
>= 0 && c
!= '\n');
110 static void nextword(char *s
)
115 while (c
>= 0 && !isspace(c
)) {
125 static void readln(char *s
)
129 while (c
> 0 && c
!= '\n') {
138 static void postline(void)
148 static void postspline(void)
160 draws(h1
, v1
, h2
, v2
);
167 static void postdraw(void)
172 switch (tolower(c
)) {
191 drawa(h1
, v1
, h2
, v2
);
200 drawend(c
== 'p' || c
== 'P', c
== 'E' || c
== 'C' || c
== 'P');
204 static void postps(void)
210 if (!strcmp("PS", cmd
) || !strcmp("ps", cmd
))
212 if (!strcmp("BeginObject", cmd
))
214 if (!strcmp("EndObject", cmd
))
218 static char devpath
[PATHLEN
] = TROFFFDIR
;
219 static char devname
[PATHLEN
] = "utf";
221 static void postx(void)
231 dev_mnt(pos
, font
, font
);
235 if (dev_open(devpath
, devname
)) {
236 fprintf(stderr
, "neatpost: cannot open device %s\n", devname
);
239 ps_header(ps_pagewidth
, ps_pageheight
, ps_linewidth
);
253 static void postcmd(int c
)
257 outrel((c
- '0') * 10 + next() - '0', 0);
276 outrel(nextnum(), 0);
279 outrel(0, nextnum());
287 outcolor(clr_get(cs
));
319 fprintf(stderr
, "neatpost: unknown command %c\n", c
);
323 static void post(void)
326 while ((c
= next()) >= 0)
333 static void setpagesize(char *s
)
336 /* predefined paper sizes */
337 if (!strcmp("letter", s
)) {
342 /* custom paper size in mm, like 210x297 for a4 */
343 if (isdigit(s
[0]) && strchr(s
, 'x')) {
344 ps_pagewidth
= atoi(s
);
345 ps_pageheight
= atoi(strchr(s
, 'x') + 1);
348 /* ISO paper sizes */
352 switch (tolower(s
[0])) {
368 ps_pagewidth
= ((n
& 1) ? d2
: d1
) >> ((n
+ 1) >> 1);
369 ps_pageheight
= ((n
& 1) ? d1
: d2
) >> (n
>> 1);
373 "Usage: neatpost [options] <input >output\n"
375 " -F dir \tset font directory (" TROFFFDIR
")\n"
376 " -p size \tset paper size (a4)\n"
377 " -w lwid \tdrawing line thickness in thousandths of an em\n";
379 int main(int argc
, char *argv
[])
382 for (i
= 1; i
< argc
; i
++) {
383 if (argv
[i
][0] == '-' && argv
[i
][1] == 'F') {
384 strcpy(devpath
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
385 } else if (argv
[i
][0] == '-' && argv
[i
][1] == 'p') {
386 setpagesize(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
387 } else if (argv
[i
][0] == '-' && argv
[i
][1] == 'w') {
388 ps_linewidth
= atoi(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
395 ps_trailer(o_pages
, o_fonts
);