2 /* Copyright (C) 1994, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.com)
6 This file is part of groff.
8 groff is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 groff is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with groff; see the file COPYING. If not, write to the Free Software
20 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
25 option to use beziers for circle/ellipse/arc
26 option to use lines for spline (for LJ3)
27 left/top offset registration
28 output bin selection option
30 output non-integer parameters using fixed point numbers
31 X command to insert contents of file
32 X command to specify inline escape sequence (how to specify unprintable chars?)
33 X command to include bitmap graphics
39 extern "C" const char *Version_string;
45 int x_offset_portrait;
46 int x_offset_landscape;
48 { "letter", 2, 75, 60 },
49 { "legal", 3, 75, 60 },
50 { "executive", 1, 75, 60 },
52 { "com10", 81, 75, 60 },
53 { "monarch", 80, 75, 60 },
55 { "b5", 100, 71, 59 },
59 static int user_paper_size = -1;
60 static int landscape_flag = 0;
61 static int duplex_flag = 0;
63 // An upper limit on the paper size in centipoints,
64 // used for setting HPGL picture frame.
65 #define MAX_PAPER_WIDTH (12*720)
66 #define MAX_PAPER_HEIGHT (17*720)
68 // Dotted lines that are thinner than this don't work right.
69 #define MIN_DOT_PEN_WIDTH .351
71 #ifndef DEFAULT_LINE_WIDTH_FACTOR
73 #define DEFAULT_LINE_WIDTH_FACTOR 40
76 const int DEFAULT_HPGL_UNITS = 1016;
77 int line_width_factor = DEFAULT_LINE_WIDTH_FACTOR;
78 unsigned ncopies = 0; // 0 means don't send ncopies command
80 static int lookup_paper_size(const char *);
82 class lj4_font : public font {
85 void handle_unknown_font_command(const char *command, const char *arg,
86 const char *filename, int lineno);
87 static lj4_font *load_lj4_font(const char *);
93 lj4_font(const char *);
96 lj4_font::lj4_font(const char *nm)
97 : font(nm), weight(0), style(0), proportional(0), typeface(0)
101 lj4_font::~lj4_font()
105 lj4_font *lj4_font::load_lj4_font(const char *s)
107 lj4_font *f = new lj4_font(s);
120 } command_table[] = {
121 { "pclweight", &lj4_font::weight, -7, 7 },
122 { "pclstyle", &lj4_font::style, 0, 32767 },
123 { "pclproportional", &lj4_font::proportional, 0, 1 },
124 { "pcltypeface", &lj4_font::typeface, 0, 65535 },
127 void lj4_font::handle_unknown_font_command(const char *command,
129 const char *filename, int lineno)
131 for (unsigned int i = 0;
132 i < sizeof(command_table)/sizeof(command_table[0]); i++) {
133 if (strcmp(command, command_table[i].s) == 0) {
135 fatal_with_file_and_line(filename, lineno,
136 "`%1' command requires an argument",
139 long n = strtol(arg, &ptr, 10);
140 if (n == 0 && ptr == arg)
141 fatal_with_file_and_line(filename, lineno,
142 "`%1' command requires numeric argument",
144 if (n < command_table[i].min) {
145 error_with_file_and_line(filename, lineno,
146 "argument for `%1' command must not be less than %2",
147 command, command_table[i].min);
148 n = command_table[i].min;
150 else if (n > command_table[i].max) {
151 error_with_file_and_line(filename, lineno,
152 "argument for `%1' command must not be greater than %2",
153 command, command_table[i].max);
154 n = command_table[i].max;
156 this->*command_table[i].ptr = int(n);
162 class lj4_printer : public printer {
166 void set_char(int, font *, const environment *, int, const char *name);
167 void draw(int code, int *p, int np, const environment *env);
168 void begin_page(int);
169 void end_page(int page_length);
170 font *make_font(const char *);
173 void set_line_thickness(int size, int dot = 0);
177 int moveto(int hpos, int vpos);
178 int moveto1(int hpos, int vpos);
184 unsigned short cur_symbol_set;
194 int lj4_printer::moveto(int hpos, int vpos)
196 if (cur_hpos != hpos || cur_vpos != vpos || cur_hpos < 0)
197 return moveto1(hpos, vpos);
203 void lj4_printer::hpgl_start()
205 fputs("\033%1B", stdout);
209 void lj4_printer::hpgl_end()
211 fputs(";\033%0A", stdout);
214 lj4_printer::lj4_printer(int ps)
223 if (7200 % font::res != 0)
224 fatal("invalid resolution %1: resolution must be a factor of 7200",
226 fputs("\033E", stdout); // reset
227 if (font::res != 300)
228 printf("\033&u%dD", font::res); // unit of measure
230 printf("\033&l%uX", ncopies);
231 paper_size = 0; // default to letter
232 if (font::papersize) {
233 int n = lookup_paper_size(font::papersize);
235 error("unknown paper size `%1'", font::papersize);
241 printf("\033&l%dA" // paper size
242 "\033&l%dO" // orientation
243 "\033&l0E", // no top margin
244 paper_table[paper_size].code,
245 landscape_flag != 0);
247 x_offset = paper_table[paper_size].x_offset_landscape;
249 x_offset = paper_table[paper_size].x_offset_portrait;
250 x_offset = (x_offset * font::res) / 300;
252 printf("\033&l%dS", duplex_flag);
255 lj4_printer::~lj4_printer()
257 fputs("\033E", stdout);
260 void lj4_printer::begin_page(int)
264 void lj4_printer::end_page(int)
270 void lj4_printer::end_of_line()
272 cur_hpos = -1; // force absolute motion
276 int is_unprintable(unsigned char c)
278 return c < 32 && (c == 0 || (7 <= c && c <= 15) || c == 27);
281 void lj4_printer::set_char(int idx, font *f, const environment *env,
284 int code = f->get_code(idx);
286 unsigned char ch = code & 0xff;
287 unsigned short symbol_set = code >> 8;
288 if (symbol_set != cur_symbol_set) {
289 printf("\033(%d%c", symbol_set/32, (symbol_set & 31) + 64);
290 cur_symbol_set = symbol_set;
293 lj4_font *psf = (lj4_font *)f;
294 // FIXME only output those that are needed
295 printf("\033(s%dp%ds%db%dT",
300 if (!psf->proportional || !cur_font || !cur_font->proportional)
304 if (env->size != cur_size) {
305 if (cur_font->proportional) {
306 static const char *quarters[] = { "", ".25", ".5", ".75" };
307 printf("\033(s%d%sV", env->size/4, quarters[env->size & 3]);
310 double pitch = double(font::res)/w;
311 // PCL uses the next largest pitch, so round it down.
312 pitch = floor(pitch*100.0)/100.0;
313 printf("\033(s%.2fH", pitch);
315 cur_size = env->size;
317 if (!moveto(env->hpos, env->vpos))
319 if (is_unprintable(ch))
320 fputs("\033&p1X", stdout);
325 int lj4_printer::moveto1(int hpos, int vpos)
327 if (hpos < x_offset || vpos < 0)
329 fputs("\033*p", stdout);
331 printf("%dx%dY", hpos - x_offset, vpos);
333 if (cur_hpos != hpos)
334 printf("%s%d%c", hpos > cur_hpos ? "+" : "",
335 hpos - cur_hpos, vpos == cur_vpos ? 'X' : 'x');
336 if (cur_vpos != vpos)
337 printf("%s%dY", vpos > cur_vpos ? "+" : "", vpos - cur_vpos);
344 void lj4_printer::draw(int code, int *p, int np, const environment *env)
350 error("2 arguments required for rule");
353 int hpos = env->hpos;
354 int vpos = env->vpos;
365 if (!moveto(hpos, vpos))
367 printf("\033*c%da%db0P", hsize, vsize);
372 error("2 arguments required for line");
376 if (!moveto(env->hpos, env->vpos))
379 set_line_thickness(env->size, p[0] == 0 && p[1] == 0);
380 printf("PD%d,%d", p[0], p[1]);
387 error("even number of arguments required for polygon");
391 error("no arguments for polygon");
395 if (!moveto(env->hpos, env->vpos))
399 set_line_thickness(env->size);
400 printf("PMPD%d", p[0]);
401 for (int i = 1; i < np; i++)
403 printf("PM2%cP", code == 'p' ? 'E' : 'F');
410 error("even number of arguments required for spline");
414 error("no arguments for spline");
418 if (!moveto(env->hpos, env->vpos))
421 set_line_thickness(env->size);
422 printf("PD%d,%d", p[0]/2, p[1]/2);
427 for (int i = 0; i < np - 2; i += 2) {
430 printf("%d,%d,%d,%d,%d,%d",
431 (p[i]*tnum)/(2*tden),
432 (p[i + 1]*tnum)/(2*tden),
433 p[i]/2 + (p[i + 2]*(tden - tnum))/(2*tden),
434 p[i + 1]/2 + (p[i + 3]*(tden - tnum))/(2*tden),
435 (p[i] - p[i]/2) + p[i + 2]/2,
436 (p[i + 1] - p[i + 1]/2) + p[i + 3]/2);
439 printf("PR%d,%d", p[np - 2] - p[np - 2]/2, p[np - 1] - p[np - 1]/2);
445 // troff adds an extra argument to C
446 if (np != 1 && !(code == 'C' && np == 2)) {
447 error("1 argument required for circle");
451 if (!moveto(env->hpos + p[0]/2, env->vpos))
455 set_line_thickness(env->size);
456 printf("CI%d", p[0]/2);
459 printf("WG%d,0,360", p[0]/2);
465 error("2 arguments required for ellipse");
469 if (!moveto(env->hpos + p[0]/2, env->vpos))
472 printf("SC0,%.4f,0,-%.4f,2", hpgl_scale * double(p[0])/p[1], hpgl_scale);
474 set_line_thickness(env->size);
475 printf("CI%d", p[1]/2);
478 printf("WG%d,0,360", p[1]/2);
479 printf("SC0,%.4f,0,-%.4f,2", hpgl_scale, hpgl_scale);
485 error("4 arguments required for arc");
489 if (!moveto(env->hpos, env->vpos))
492 set_line_thickness(env->size);
494 if (adjust_arc_center(p, c)) {
495 double sweep = ((atan2(p[1] + p[3] - c[1], p[0] + p[2] - c[0])
496 - atan2(-c[1], -c[0]))
500 printf("PDAR%d,%d,%f", int(c[0]), int(c[1]), sweep);
503 printf("PD%d,%d", p[0] + p[2], p[1] + p[3]);
508 if (np != 1 && np != 2) {
509 error("1 argument required for fill");
514 if (p[0] >= 0 && p[0] <= 1000)
515 printf("FT10,%d", p[0]/10);
519 // not implemented yet
527 // troff gratuitously adds an extra 0
528 if (np != 1 && np != 2) {
529 error("0 or 1 argument required for thickness");
532 line_thickness = p[0];
537 error("unrecognised drawing command `%1'", char(code));
542 void lj4_printer::hpgl_init()
547 hpgl_scale = double(DEFAULT_HPGL_UNITS)/font::res;
548 printf("\033&f0S" // push position
549 "\033*p0x0Y" // move to 0,0
550 "\033*c%dx%dy0T" // establish picture frame
551 "\033%%1B" // switch to HPGL
552 "SP1SC0,%.4f,0,-%.4f,2IR0,100,0,100" // set up scaling
553 "LA1,4,2,4" // round line ends and joins
554 "PR" // relative plotting
556 ";\033%%1A" // back to PCL
557 "\033&f1S", // pop position
558 MAX_PAPER_WIDTH, MAX_PAPER_HEIGHT,
559 hpgl_scale, hpgl_scale);
562 void lj4_printer::set_line_thickness(int size, int dot)
565 if (line_thickness < 0)
566 pw = (size * (line_width_factor * 25.4))/(font::sizescale * 72000.0);
568 pw = line_thickness*25.4/font::res;
569 if (dot && pw < MIN_DOT_PEN_WIDTH)
570 pw = MIN_DOT_PEN_WIDTH;
571 if (pw != pen_width) {
577 font *lj4_printer::make_font(const char *nm)
579 return lj4_font::load_lj4_font(nm);
582 printer *make_printer()
584 return new lj4_printer(user_paper_size);
588 int lookup_paper_size(const char *s)
590 for (unsigned int i = 0;
591 i < sizeof(paper_table)/sizeof(paper_table[0]); i++) {
592 // FIXME Perhaps allow unique prefix.
593 if (strcasecmp(s, paper_table[i].name) == 0)
599 static void usage(FILE *stream);
601 extern "C" int optopt, optind;
603 int main(int argc, char **argv)
605 setlocale(LC_NUMERIC, "C");
606 program_name = argv[0];
607 static char stderr_buf[BUFSIZ];
608 setbuf(stderr, stderr_buf);
610 static const struct option long_options[] = {
611 { "help", no_argument, 0, CHAR_MAX + 1 },
612 { "version", no_argument, 0, 'v' },
615 while ((c = getopt_long(argc, argv, "c:d:F:I:lp:vw:", long_options, NULL))
622 // ignore include search path
626 fprintf(stderr, "duplex assumed to be long-side\n");
629 fprintf(stderr, "option -%c requires an argument\n", optopt);
633 if (!isdigit(*optarg)) // this ugly hack prevents -d without
634 optind--; // args from messing up the arg list
635 duplex_flag = atoi(optarg);
636 if (duplex_flag != 1 && duplex_flag != 2) {
637 fprintf(stderr, "odd value for duplex; assumed to be long-side\n");
643 int n = lookup_paper_size(optarg);
645 error("unknown paper size `%1'", optarg);
651 printf("GNU grolj4 (groff) version %s\n", Version_string);
655 font::command_line_font_dir(optarg);
660 long n = strtol(optarg, &ptr, 10);
661 if (n == 0 && ptr == optarg)
662 error("argument for -c must be a positive integer");
663 else if (n <= 0 || n > 32767)
664 error("out of range argument for -c");
666 ncopies = unsigned(n);
672 long n = strtol(optarg, &ptr, 10);
673 if (n == 0 && ptr == optarg)
674 error("argument for -w must be a non-negative integer");
675 else if (n < 0 || n > INT_MAX)
676 error("out of range argument for -w");
678 line_width_factor = int(n);
681 case CHAR_MAX + 1: // --help
692 SET_BINARY(fileno(stdout));
696 for (int i = optind; i < argc; i++)
702 static void usage(FILE *stream)
705 "usage: %s [-lv] [-d [n]] [-c n] [-p paper_size]\n"
706 " [-w n] [-F dir] [files ...]\n",