2 /* Copyright (C) 1994-2014 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.com)
5 This file is part of groff.
7 groff is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 groff is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 option to use beziers for circle/ellipse/arc
24 option to use lines for spline (for LJ3)
25 left/top offset registration
26 output bin selection option
28 output non-integer parameters using fixed point numbers
29 X command to insert contents of file
30 X command to specify inline escape sequence (how to specify unprintable chars?)
31 X command to include bitmap graphics
37 extern "C" const char *Version_string;
43 int x_offset_portrait;
44 int x_offset_landscape;
46 { "letter", 2, 75, 60 },
47 { "legal", 3, 75, 60 },
48 { "executive", 1, 75, 60 },
50 { "com10", 81, 75, 60 },
51 { "monarch", 80, 75, 60 },
53 { "b5", 100, 71, 59 },
57 static int user_paper_size = -1;
58 static int landscape_flag = 0;
59 static int duplex_flag = 0;
61 // An upper limit on the paper size in centipoints,
62 // used for setting HPGL picture frame.
63 #define MAX_PAPER_WIDTH (12*720)
64 #define MAX_PAPER_HEIGHT (17*720)
66 // Dotted lines that are thinner than this don't work right.
67 #define MIN_DOT_PEN_WIDTH .351
69 #ifndef DEFAULT_LINE_WIDTH_FACTOR
71 #define DEFAULT_LINE_WIDTH_FACTOR 40
74 const int DEFAULT_HPGL_UNITS = 1016;
75 int line_width_factor = DEFAULT_LINE_WIDTH_FACTOR;
76 unsigned ncopies = 0; // 0 means don't send ncopies command
78 static int lookup_paper_size(const char *);
80 class lj4_font : public font {
83 void handle_unknown_font_command(const char *command, const char *arg,
84 const char *filename, int lineno);
85 static lj4_font *load_lj4_font(const char *);
91 lj4_font(const char *);
94 lj4_font::lj4_font(const char *nm)
95 : font(nm), weight(0), style(0), proportional(0), typeface(0)
103 lj4_font *lj4_font::load_lj4_font(const char *s)
105 lj4_font *f = new lj4_font(s);
118 } command_table[] = {
119 { "pclweight", &lj4_font::weight, -7, 7 },
120 { "pclstyle", &lj4_font::style, 0, 32767 },
121 { "pclproportional", &lj4_font::proportional, 0, 1 },
122 { "pcltypeface", &lj4_font::typeface, 0, 65535 },
125 void lj4_font::handle_unknown_font_command(const char *command,
127 const char *filename, int lineno)
129 for (unsigned int i = 0;
130 i < sizeof(command_table)/sizeof(command_table[0]); i++) {
131 if (strcmp(command, command_table[i].s) == 0) {
133 fatal_with_file_and_line(filename, lineno,
134 "`%1' command requires an argument",
137 long n = strtol(arg, &ptr, 10);
138 if (n == 0 && ptr == arg)
139 fatal_with_file_and_line(filename, lineno,
140 "`%1' command requires numeric argument",
142 if (n < command_table[i].min) {
143 error_with_file_and_line(filename, lineno,
144 "argument for `%1' command must not be less than %2",
145 command, command_table[i].min);
146 n = command_table[i].min;
148 else if (n > command_table[i].max) {
149 error_with_file_and_line(filename, lineno,
150 "argument for `%1' command must not be greater than %2",
151 command, command_table[i].max);
152 n = command_table[i].max;
154 this->*command_table[i].ptr = int(n);
160 class lj4_printer : public printer {
164 void set_char(glyph *, font *, const environment *, int, const char *name);
165 void draw(int code, int *p, int np, const environment *env);
166 void begin_page(int);
167 void end_page(int page_length);
168 font *make_font(const char *);
171 void set_line_thickness(int size, int dot = 0);
175 int moveto(int hpos, int vpos);
176 int moveto1(int hpos, int vpos);
182 unsigned short cur_symbol_set;
192 int lj4_printer::moveto(int hpos, int vpos)
194 if (cur_hpos != hpos || cur_vpos != vpos || cur_hpos < 0)
195 return moveto1(hpos, vpos);
201 void lj4_printer::hpgl_start()
203 fputs("\033%1B", stdout);
207 void lj4_printer::hpgl_end()
209 fputs(";\033%0A", stdout);
212 lj4_printer::lj4_printer(int ps)
221 if (7200 % font::res != 0)
222 fatal("invalid resolution %1: resolution must be a factor of 7200",
224 fputs("\033E", stdout); // reset
225 if (font::res != 300)
226 printf("\033&u%dD", font::res); // unit of measure
228 printf("\033&l%uX", ncopies);
229 paper_size = 0; // default to letter
230 if (font::papersize) {
231 int n = lookup_paper_size(font::papersize);
233 error("unknown paper size `%1'", font::papersize);
239 printf("\033&l%dA" // paper size
240 "\033&l%dO" // orientation
241 "\033&l0E", // no top margin
242 paper_table[paper_size].code,
243 landscape_flag != 0);
245 x_offset = paper_table[paper_size].x_offset_landscape;
247 x_offset = paper_table[paper_size].x_offset_portrait;
248 x_offset = (x_offset * font::res) / 300;
250 printf("\033&l%dS", duplex_flag);
253 lj4_printer::~lj4_printer()
255 fputs("\033E", stdout);
258 void lj4_printer::begin_page(int)
262 void lj4_printer::end_page(int)
268 void lj4_printer::end_of_line()
270 cur_hpos = -1; // force absolute motion
274 int is_unprintable(unsigned char c)
276 return c < 32 && (c == 0 || (7 <= c && c <= 15) || c == 27);
279 void lj4_printer::set_char(glyph *g, font *f, const environment *env,
282 int code = f->get_code(g);
284 unsigned char ch = code & 0xff;
285 unsigned short symbol_set = code >> 8;
286 if (symbol_set != cur_symbol_set) {
287 printf("\033(%d%c", symbol_set/32, (symbol_set & 31) + 64);
288 cur_symbol_set = symbol_set;
291 lj4_font *psf = (lj4_font *)f;
292 // FIXME only output those that are needed
293 printf("\033(s%dp%ds%db%dT",
298 if (!psf->proportional || !cur_font || !cur_font->proportional)
302 if (env->size != cur_size) {
303 if (cur_font->proportional) {
304 static const char *quarters[] = { "", ".25", ".5", ".75" };
305 printf("\033(s%d%sV", env->size/4, quarters[env->size & 3]);
308 double pitch = double(font::res)/w;
309 // PCL uses the next largest pitch, so round it down.
310 pitch = floor(pitch*100.0)/100.0;
311 printf("\033(s%.2fH", pitch);
313 cur_size = env->size;
315 if (!moveto(env->hpos, env->vpos))
317 if (is_unprintable(ch))
318 fputs("\033&p1X", stdout);
323 int lj4_printer::moveto1(int hpos, int vpos)
325 if (hpos < x_offset || vpos < 0)
327 fputs("\033*p", stdout);
329 printf("%dx%dY", hpos - x_offset, vpos);
331 if (cur_hpos != hpos)
332 printf("%s%d%c", hpos > cur_hpos ? "+" : "",
333 hpos - cur_hpos, vpos == cur_vpos ? 'X' : 'x');
334 if (cur_vpos != vpos)
335 printf("%s%dY", vpos > cur_vpos ? "+" : "", vpos - cur_vpos);
342 void lj4_printer::draw(int code, int *p, int np, const environment *env)
348 error("2 arguments required for rule");
351 int hpos = env->hpos;
352 int vpos = env->vpos;
363 if (!moveto(hpos, vpos))
365 printf("\033*c%da%db0P", hsize, vsize);
370 error("2 arguments required for line");
374 if (!moveto(env->hpos, env->vpos))
377 set_line_thickness(env->size, p[0] == 0 && p[1] == 0);
378 printf("PD%d,%d", p[0], p[1]);
385 error("even number of arguments required for polygon");
389 error("no arguments for polygon");
393 if (!moveto(env->hpos, env->vpos))
397 set_line_thickness(env->size);
398 printf("PMPD%d", p[0]);
399 for (int i = 1; i < np; i++)
401 printf("PM2%cP", code == 'p' ? 'E' : 'F');
408 error("even number of arguments required for spline");
412 error("no arguments for spline");
416 if (!moveto(env->hpos, env->vpos))
419 set_line_thickness(env->size);
420 printf("PD%d,%d", p[0]/2, p[1]/2);
425 for (int i = 0; i < np - 2; i += 2) {
428 printf("%d,%d,%d,%d,%d,%d",
429 (p[i]*tnum)/(2*tden),
430 (p[i + 1]*tnum)/(2*tden),
431 p[i]/2 + (p[i + 2]*(tden - tnum))/(2*tden),
432 p[i + 1]/2 + (p[i + 3]*(tden - tnum))/(2*tden),
433 (p[i] - p[i]/2) + p[i + 2]/2,
434 (p[i + 1] - p[i + 1]/2) + p[i + 3]/2);
437 printf("PR%d,%d", p[np - 2] - p[np - 2]/2, p[np - 1] - p[np - 1]/2);
443 // troff adds an extra argument to C
444 if (np != 1 && !(code == 'C' && np == 2)) {
445 error("1 argument required for circle");
449 if (!moveto(env->hpos + p[0]/2, env->vpos))
453 set_line_thickness(env->size);
454 printf("CI%d", p[0]/2);
457 printf("WG%d,0,360", p[0]/2);
463 error("2 arguments required for ellipse");
467 if (!moveto(env->hpos + p[0]/2, env->vpos))
470 printf("SC0,%.4f,0,-%.4f,2", hpgl_scale * double(p[0])/p[1], hpgl_scale);
472 set_line_thickness(env->size);
473 printf("CI%d", p[1]/2);
476 printf("WG%d,0,360", p[1]/2);
477 printf("SC0,%.4f,0,-%.4f,2", hpgl_scale, hpgl_scale);
483 error("4 arguments required for arc");
487 if (!moveto(env->hpos, env->vpos))
490 set_line_thickness(env->size);
492 if (adjust_arc_center(p, c)) {
493 double sweep = ((atan2(p[1] + p[3] - c[1], p[0] + p[2] - c[0])
494 - atan2(-c[1], -c[0]))
498 printf("PDAR%d,%d,%f", int(c[0]), int(c[1]), sweep);
501 printf("PD%d,%d", p[0] + p[2], p[1] + p[3]);
506 if (np != 1 && np != 2) {
507 error("1 argument required for fill");
512 if (p[0] >= 0 && p[0] <= 1000)
513 printf("FT10,%d", p[0]/10);
517 // not implemented yet
525 // troff gratuitously adds an extra 0
526 if (np != 1 && np != 2) {
527 error("0 or 1 argument required for thickness");
530 line_thickness = p[0];
535 error("unrecognised drawing command `%1'", char(code));
540 void lj4_printer::hpgl_init()
545 hpgl_scale = double(DEFAULT_HPGL_UNITS)/font::res;
546 printf("\033&f0S" // push position
547 "\033*p0x0Y" // move to 0,0
548 "\033*c%dx%dy0T" // establish picture frame
549 "\033%%1B" // switch to HPGL
550 "SP1SC0,%.4f,0,-%.4f,2IR0,100,0,100" // set up scaling
551 "LA1,4,2,4" // round line ends and joins
552 "PR" // relative plotting
554 ";\033%%1A" // back to PCL
555 "\033&f1S", // pop position
556 MAX_PAPER_WIDTH, MAX_PAPER_HEIGHT,
557 hpgl_scale, hpgl_scale);
560 void lj4_printer::set_line_thickness(int size, int dot)
563 if (line_thickness < 0)
564 pw = (size * (line_width_factor * 25.4))/(font::sizescale * 72000.0);
566 pw = line_thickness*25.4/font::res;
567 if (dot && pw < MIN_DOT_PEN_WIDTH)
568 pw = MIN_DOT_PEN_WIDTH;
569 if (pw != pen_width) {
575 font *lj4_printer::make_font(const char *nm)
577 return lj4_font::load_lj4_font(nm);
580 printer *make_printer()
582 return new lj4_printer(user_paper_size);
586 int lookup_paper_size(const char *s)
588 for (unsigned int i = 0;
589 i < sizeof(paper_table)/sizeof(paper_table[0]); i++) {
590 // FIXME Perhaps allow unique prefix.
591 if (strcasecmp(s, paper_table[i].name) == 0)
597 static void usage(FILE *stream);
599 extern "C" int optopt, optind;
601 int main(int argc, char **argv)
603 setlocale(LC_NUMERIC, "C");
604 program_name = argv[0];
605 static char stderr_buf[BUFSIZ];
606 setbuf(stderr, stderr_buf);
608 static const struct option long_options[] = {
609 { "help", no_argument, 0, CHAR_MAX + 1 },
610 { "version", no_argument, 0, 'v' },
613 while ((c = getopt_long(argc, argv, "c:d:F:I:lp:vw:", long_options, NULL))
620 // ignore include search path
624 fprintf(stderr, "duplex assumed to be long-side\n");
627 fprintf(stderr, "option -%c requires an argument\n", optopt);
631 if (!isdigit(*optarg)) // this ugly hack prevents -d without
632 optind--; // args from messing up the arg list
633 duplex_flag = atoi(optarg);
634 if (duplex_flag != 1 && duplex_flag != 2) {
635 fprintf(stderr, "odd value for duplex; assumed to be long-side\n");
641 int n = lookup_paper_size(optarg);
643 error("unknown paper size `%1'", optarg);
649 printf("GNU grolj4 (groff) version %s\n", Version_string);
653 font::command_line_font_dir(optarg);
658 long n = strtol(optarg, &ptr, 10);
659 if (n == 0 && ptr == optarg)
660 error("argument for -c must be a positive integer");
661 else if (n <= 0 || n > 32767)
662 error("out of range argument for -c");
664 ncopies = unsigned(n);
670 long n = strtol(optarg, &ptr, 10);
671 if (n == 0 && ptr == optarg)
672 error("argument for -w must be a non-negative integer");
673 else if (n < 0 || n > INT_MAX)
674 error("out of range argument for -w");
676 line_width_factor = int(n);
679 case CHAR_MAX + 1: // --help
690 SET_BINARY(fileno(stdout));
694 for (int i = optind; i < argc; i++)
700 static void usage(FILE *stream)
703 "usage: %s [-lv] [-d [n]] [-c n] [-p paper_size]\n"
704 " [-w n] [-F dir] [files ...]\n",