2 /* Copyright (C) 1994 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 2, or (at your option) any later
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 along
18 with groff; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 option to use beziers for circle/ellipse/arc
25 option to use lines for spline (for LJ3)
27 duplex short/long edge options
28 left/top offset registration
29 output bin selection option
31 output non-integer parameters using fixed point numbers
32 X command to insert contents of file
33 X command to specify inline escape sequence (how to specify unprintable chars?)
34 X command to include bitmap graphics
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 paper_size = -1;
58 static int landscape_flag = 0;
60 // An upper limit on the paper size in centipoints,
61 // used for setting HPGL picture frame.
62 #define MAX_PAPER_WIDTH (12*720)
63 #define MAX_PAPER_HEIGHT (17*720)
65 // Dotted lines that are thinner than this don't work right.
66 #define MIN_DOT_PEN_WIDTH .351
68 #ifndef DEFAULT_LINE_WIDTH_FACTOR
70 #define DEFAULT_LINE_WIDTH_FACTOR 40
73 const int DEFAULT_HPGL_UNITS = 1016;
74 int line_width_factor = DEFAULT_LINE_WIDTH_FACTOR;
75 unsigned ncopies = 0; // 0 means don't send ncopies command
77 class lj4_font : public font {
80 void handle_unknown_font_command(const char *command, const char *arg,
81 const char *filename, int lineno);
82 static lj4_font *load_lj4_font(const char *);
88 lj4_font(const char *);
91 lj4_font::lj4_font(const char *nm)
92 : font(nm), weight(0), style(0), proportional(0), typeface(0)
100 lj4_font *lj4_font::load_lj4_font(const char *s)
102 lj4_font *f = new lj4_font(s);
115 } command_table[] = {
116 { "pclweight", &lj4_font::weight, -7, 7 },
117 { "pclstyle", &lj4_font::style, 0, 32767 },
118 { "pclproportional", &lj4_font::proportional, 0, 1 },
119 { "pcltypeface", &lj4_font::typeface, 0, 65535 },
122 void lj4_font::handle_unknown_font_command(const char *command,
124 const char *filename, int lineno)
126 for (int i = 0; i < sizeof(command_table)/sizeof(command_table[0]); i++) {
127 if (strcmp(command, command_table[i].s) == 0) {
129 fatal_with_file_and_line(filename, lineno,
130 "`%1' command requires an argument",
133 long n = strtol(arg, &ptr, 10);
134 if (n == 0 && ptr == arg)
135 fatal_with_file_and_line(filename, lineno,
136 "`%1' command requires numeric argument",
138 if (n < command_table[i].min) {
139 error_with_file_and_line(filename, lineno,
140 "argument for `%1' command must not be less than %2",
141 command, command_table[i].min);
142 n = command_table[i].min;
144 else if (n > command_table[i].max) {
145 error_with_file_and_line(filename, lineno,
146 "argument for `%1' command must not be greater than %2",
147 command, command_table[i].max);
148 n = command_table[i].max;
150 this->*command_table[i].ptr = int(n);
156 class lj4_printer : public printer {
160 void set_char(int, font *, const environment *, int);
161 void draw(int code, int *p, int np, const environment *env);
162 void begin_page(int);
163 void end_page(int page_length);
164 font *make_font(const char *);
167 void set_line_thickness(int size, int dot = 0);
171 int moveto(int hpos, int vpos);
172 int moveto1(int hpos, int vpos);
178 unsigned short cur_symbol_set;
187 int lj4_printer::moveto(int hpos, int vpos)
189 if (cur_hpos != hpos || cur_vpos != vpos || cur_hpos < 0)
190 return moveto1(hpos, vpos);
196 void lj4_printer::hpgl_start()
198 fputs("\033%1B", stdout);
202 void lj4_printer::hpgl_end()
204 fputs(";\033%0A", stdout);
207 lj4_printer::lj4_printer()
216 if (7200 % font::res != 0)
217 fatal("invalid resolution %1: resolution must be a factor of 7200",
219 fputs("\033E", stdout); // reset
220 if (font::res != 300)
221 printf("\033&u%dD", font::res); // unit of measure
223 printf("\033&l%uX", ncopies);
225 paper_size = 0; // default to letter
226 printf("\033&l%dA" // paper size
227 "\033&l%dO" // orientation
228 "\033&l0E", // no top margin
229 paper_table[paper_size].code,
230 landscape_flag != 0);
232 x_offset = paper_table[paper_size].x_offset_landscape;
234 x_offset = paper_table[paper_size].x_offset_portrait;
235 x_offset = (x_offset * font::res) / 300;
238 lj4_printer::~lj4_printer()
240 fputs("\033E", stdout);
243 void lj4_printer::begin_page(int)
247 void lj4_printer::end_page(int)
253 void lj4_printer::end_of_line()
255 cur_hpos = -1; // force absolute motion
259 int is_unprintable(unsigned char c)
261 return c < 32 && (c == 0 || (7 <= c && c <= 15) || c == 27);
264 void lj4_printer::set_char(int index, font *f, const environment *env, int w)
266 int code = f->get_code(index);
268 unsigned char ch = code & 0xff;
269 unsigned short symbol_set = code >> 8;
270 if (symbol_set != cur_symbol_set) {
271 printf("\033(%d%c", symbol_set/32, (symbol_set & 31) + 64);
272 cur_symbol_set = symbol_set;
275 lj4_font *psf = (lj4_font *)f;
276 // FIXME only output those that are needed
277 printf("\033(s%dp%ds%db%dT",
282 if (!psf->proportional || !cur_font || !cur_font->proportional)
286 if (env->size != cur_size) {
287 if (cur_font->proportional) {
288 static const char *quarters[] = { "", ".25", ".5", ".75" };
289 printf("\033(s%d%sV", env->size/4, quarters[env->size & 3]);
292 double pitch = double(font::res)/w;
293 // PCL uses the next largest pitch, so round it down.
294 pitch = floor(pitch*100.0)/100.0;
295 printf("\033(s%.2fH", pitch);
297 cur_size = env->size;
299 if (!moveto(env->hpos, env->vpos))
301 if (is_unprintable(ch))
302 fputs("\033&p1X", stdout);
307 int lj4_printer::moveto1(int hpos, int vpos)
309 if (hpos < x_offset || vpos < 0)
311 fputs("\033*p", stdout);
313 printf("%dx%dY", hpos - x_offset, vpos);
315 if (cur_hpos != hpos)
316 printf("%s%d%c", hpos > cur_hpos ? "+" : "",
317 hpos - cur_hpos, vpos == cur_vpos ? 'X' : 'x');
318 if (cur_vpos != vpos)
319 printf("%s%dY", vpos > cur_vpos ? "+" : "", vpos - cur_vpos);
326 void lj4_printer::draw(int code, int *p, int np, const environment *env)
332 error("2 arguments required for rule");
335 int hpos = env->hpos;
336 int vpos = env->vpos;
347 if (!moveto(hpos, vpos))
349 printf("\033*c%da%db0P", hsize, vsize);
354 error("2 arguments required for line");
358 if (!moveto(env->hpos, env->vpos))
361 set_line_thickness(env->size, p[0] == 0 && p[1] == 0);
362 printf("PD%d,%d", p[0], p[1]);
369 error("even number of arguments required for polygon");
373 error("no arguments for polygon");
377 if (!moveto(env->hpos, env->vpos))
381 set_line_thickness(env->size);
382 printf("PMPD%d", p[0]);
383 for (int i = 1; i < np; i++)
385 printf("PM2%cP", code == 'p' ? 'E' : 'F');
392 error("even number of arguments required for spline");
396 error("no arguments for spline");
400 if (!moveto(env->hpos, env->vpos))
403 set_line_thickness(env->size);
404 printf("PD%d,%d", p[0]/2, p[1]/2);
409 for (int i = 0; i < np - 2; i += 2) {
412 printf("%d,%d,%d,%d,%d,%d",
413 (p[i]*tnum)/(2*tden),
414 (p[i + 1]*tnum)/(2*tden),
415 p[i]/2 + (p[i + 2]*(tden - tnum))/(2*tden),
416 p[i + 1]/2 + (p[i + 3]*(tden - tnum))/(2*tden),
417 (p[i] - p[i]/2) + p[i + 2]/2,
418 (p[i + 1] - p[i + 1]/2) + p[i + 3]/2);
421 printf("PR%d,%d", p[np - 2] - p[np - 2]/2, p[np - 1] - p[np - 1]/2);
427 // troff adds an extra argument to C
428 if (np != 1 && !(code == 'C' && np == 2)) {
429 error("1 argument required for circle");
433 if (!moveto(env->hpos + p[0]/2, env->vpos))
437 set_line_thickness(env->size);
438 printf("CI%d", p[0]/2);
441 printf("WG%d,0,360", p[0]/2);
447 error("2 arguments required for ellipse");
451 if (!moveto(env->hpos + p[0]/2, env->vpos))
454 printf("SC0,%.4f,0,-%.4f,2", hpgl_scale * double(p[0])/p[1], hpgl_scale);
456 set_line_thickness(env->size);
457 printf("CI%d", p[1]/2);
460 printf("WG%d,0,360", p[1]/2);
461 printf("SC0,%.4f,0,-%.4f,2", hpgl_scale, hpgl_scale);
467 error("4 arguments required for arc");
471 if (!moveto(env->hpos, env->vpos))
474 set_line_thickness(env->size);
476 if (adjust_arc_center(p, c)) {
477 double sweep = ((atan2(p[1] + p[3] - c[1], p[0] + p[2] - c[0])
478 - atan2(-c[1], -c[0]))
482 printf("PDAR%d,%d,%f", int(c[0]), int(c[1]), sweep);
485 printf("PD%d,%d", p[0] + p[2], p[1] + p[3]);
490 if (np != 1 && np != 2) {
491 error("1 argument required for fill");
496 if (p[0] >= 0 && p[0] <= 1000)
497 printf("FT10,%d", p[0]/10);
506 // troff gratuitously adds an extra 0
507 if (np != 1 && np != 2) {
508 error("0 or 1 argument required for thickness");
511 line_thickness = p[0];
516 error("unrecognised drawing command `%1'", char(code));
521 void lj4_printer::hpgl_init()
526 hpgl_scale = double(DEFAULT_HPGL_UNITS)/font::res;
527 printf("\033&f0S" // push position
528 "\033*p0x0Y" // move to 0,0
529 "\033*c%dx%dy0T" // establish picture frame
530 "\033%%1B" // switch to HPGL
531 "SP1SC0,%.4f,0,-%.4f,2IR0,100,0,100" // set up scaling
532 "LA1,4,2,4" // round line ends and joins
533 "PR" // relative plotting
535 ";\033%%1A" // back to PCL
536 "\033&f1S", // pop position
537 MAX_PAPER_WIDTH, MAX_PAPER_HEIGHT,
538 hpgl_scale, hpgl_scale);
541 void lj4_printer::set_line_thickness(int size, int dot)
544 if (line_thickness < 0)
545 pw = (size * (line_width_factor * 25.4))/(font::sizescale * 72000.0);
547 pw = line_thickness*25.4/font::res;
548 if (dot && pw < MIN_DOT_PEN_WIDTH)
549 pw = MIN_DOT_PEN_WIDTH;
550 if (pw != pen_width) {
556 font *lj4_printer::make_font(const char *nm)
558 return lj4_font::load_lj4_font(nm);
561 printer *make_printer()
563 return new lj4_printer;
567 int lookup_paper_size(const char *s)
569 for (int i = 0; i < sizeof(paper_table)/sizeof(paper_table[0]); i++)
570 // FIXME Do this case-insensitively.
571 // Perhaps allow unique prefix.
572 if (strcmp(s, paper_table[i].name) == 0)
578 void handle_unknown_desc_command(const char *command, const char *arg,
579 const char *filename, int lineno)
581 if (strcmp(command, "papersize") == 0) {
583 error_with_file_and_line(filename, lineno,
584 "`papersize' command requires an argument");
585 else if (paper_size < 0) {
586 int n = lookup_paper_size(arg);
588 error_with_file_and_line(filename, lineno,
589 "unknown paper size `%1'", arg);
598 int main(int argc, char **argv)
600 program_name = argv[0];
601 static char stderr_buf[BUFSIZ];
602 setbuf(stderr, stderr_buf);
603 font::set_unknown_desc_command_handler(handle_unknown_desc_command);
605 while ((c = getopt(argc, argv, "F:p:lvw:c:")) != EOF)
612 int n = lookup_paper_size(optarg);
614 error("unknown paper size `%1'", optarg);
621 extern const char *version_string;
622 fprintf(stderr, "grolj4 version %s\n", version_string);
627 font::command_line_font_dir(optarg);
632 long n = strtol(optarg, &ptr, 10);
633 if (n == 0 && ptr == optarg)
634 error("argument for -c must be a positive integer");
635 else if (n <= 0 || n > 32767)
636 error("out of range argument for -c");
638 ncopies = unsigned(n);
644 long n = strtol(optarg, &ptr, 10);
645 if (n == 0 && ptr == optarg)
646 error("argument for -w must be a non-negative integer");
647 else if (n < 0 || n > INT_MAX)
648 error("out of range argument for -w");
650 line_width_factor = int(n);
662 for (int i = optind; i < argc; i++)
672 "usage: %s [-lv] [-c n] [-p paper_size] [-w n] [-F dir] [files ...]\n",