2 /* Copyright (C) 1989-2018 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/>. */
24 extern "C" const char *Version_string;
26 #define DEFAULT_LINEWIDTH 40
27 static int linewidth = DEFAULT_LINEWIDTH;
29 static int draw_flag = 1;
31 static int landscape_flag = 0;
32 static double user_paper_length = 0;
33 static double user_paper_width = 0;
35 /* These values were chosen because:
37 (MULTIPLIER*SIZESCALE)/(RES*UNITWIDTH) == 1/(2^20 * 72.27)
39 and 57816 is an exact multiple of both 72.27*SIZESCALE and 72.
41 The width in the groff font file is the product of MULTIPLIER and the
42 width in the tfm file. */
45 #define RES_7227 (RES/7227)
46 #define UNITWIDTH 131072
50 class dvi_font : public font {
51 dvi_font(const char *);
56 void handle_unknown_font_command(const char *command, const char *arg,
57 const char *filename, int lineno);
58 static dvi_font *load_dvi_font(const char *);
61 dvi_font *dvi_font::load_dvi_font(const char *s)
63 dvi_font *f = new dvi_font(s);
71 dvi_font::dvi_font(const char *nm)
72 : font(nm), checksum(0), design_size(0)
80 void dvi_font::handle_unknown_font_command(const char *command,
82 const char *filename, int lineno)
85 if (strcmp(command, "checksum") == 0) {
87 fatal_with_file_and_line(filename, lineno,
88 "'checksum' command requires an argument");
89 checksum = int(strtol(arg, &ptr, 10));
90 if (checksum == 0 && ptr == arg) {
91 fatal_with_file_and_line(filename, lineno, "bad checksum");
94 else if (strcmp(command, "designsize") == 0) {
96 fatal_with_file_and_line(filename, lineno,
97 "'designsize' command requires an argument");
98 design_size = int(strtol(arg, &ptr, 10));
99 if (design_size == 0 && ptr == arg) {
100 fatal_with_file_and_line(filename, lineno, "bad design size");
105 #define FONTS_MAX 256
110 output_font() : f(0) { }
113 class dvi_printer : public printer {
124 output_font output_font_table[FONTS_MAX];
134 void define_font(int);
136 void possibly_begin_line();
137 void set_color(color *);
165 void moveto(int, int);
166 void out_string(const char *);
167 void out_signed(unsigned char, int);
168 void out_unsigned(unsigned char, int);
169 void do_special(const char *);
173 font *make_font(const char *);
174 void begin_page(int);
176 void set_char(glyph *, font *, const environment *, int, const char *);
177 void special(char *, const environment *, char);
179 void draw(int, int *, int, const environment *);
183 class draw_dvi_printer : public dvi_printer {
185 void set_line_thickness(const environment *);
186 void fill_next(const environment *);
190 void draw(int code, int *p, int np, const environment *env);
194 dvi_printer::dvi_printer()
195 : fp(stdout), byte_count(0), last_bop(-1), page_count(0), max_h(0), max_v(0),
196 cur_font(0), cur_point_size(-1), pushed(0), line_thickness(-1)
198 if (font::res != RES)
199 fatal("resolution must be %1", RES);
200 if (font::unitwidth != UNITWIDTH)
201 fatal("unitwidth must be %1", UNITWIDTH);
203 fatal("hor must be equal to 1");
205 fatal("vert must be equal to 1");
206 if (font::sizescale != SIZESCALE)
207 fatal("sizescale must be equal to %1", SIZESCALE);
208 max_drift = font::res/1000; // this is fairly arbitrary
212 dvi_printer::~dvi_printer()
218 draw_dvi_printer::draw_dvi_printer()
219 : output_pen_size(-1)
223 draw_dvi_printer::~draw_dvi_printer()
228 void dvi_printer::out1(int n)
234 void dvi_printer::out2(int n)
237 putc((n >> 8) & 0xff, fp);
241 void dvi_printer::out3(int n)
244 putc((n >> 16) & 0xff, fp);
245 putc((n >> 8) & 0xff, fp);
249 void dvi_printer::out4(int n)
252 putc((n >> 24) & 0xff, fp);
253 putc((n >> 16) & 0xff, fp);
254 putc((n >> 8) & 0xff, fp);
258 void dvi_printer::out_string(const char *s)
266 void dvi_printer::end_of_line()
276 void dvi_printer::possibly_begin_line()
279 have_pushed = pushed = 1;
286 int scale(int x, int z)
291 alpha = 16*z; beta = 16;
292 while (z >= 040000000L) {
299 sw = (((((d * z) / 0400) + (c * z)) / 0400) + (b * z)) / beta;
307 void dvi_printer::set_color(color *col)
311 unsigned int components[4];
312 color_scheme cs = col->get_components(components);
315 sprintf(buf, "color gray 0");
318 sprintf(buf, "color rgb %.3g %.3g %.3g",
319 double(Red) / color::MAX_COLOR_VAL,
320 double(Green) / color::MAX_COLOR_VAL,
321 double(Blue) / color::MAX_COLOR_VAL);
324 col->get_cmyk(&Cyan, &Magenta, &Yellow, &Black);
327 sprintf(buf, "color cmyk %.3g %.3g %.3g %.3g",
328 double(Cyan) / color::MAX_COLOR_VAL,
329 double(Magenta) / color::MAX_COLOR_VAL,
330 double(Yellow) / color::MAX_COLOR_VAL,
331 double(Black) / color::MAX_COLOR_VAL);
334 sprintf(buf, "color gray %.3g",
335 double(Gray) / color::MAX_COLOR_VAL);
341 void dvi_printer::set_char(glyph *g, font *f, const environment *env,
344 if (*env->col != cur_color)
346 int code = f->get_code(g);
347 if (env->size != cur_point_size || f != cur_font) {
349 cur_point_size = env->size;
352 if (i >= FONTS_MAX) {
353 fatal("too many output fonts required");
355 if (output_font_table[i].f == 0) {
356 output_font_table[i].f = (dvi_font *)cur_font;
357 output_font_table[i].point_size = cur_point_size;
360 if (output_font_table[i].f == cur_font
361 && output_font_table[i].point_size == cur_point_size)
366 int distance = env->hpos - cur_h;
367 if (env->hpos != end_h && distance != 0) {
368 out_signed(right1, distance);
371 else if (distance > max_drift) {
372 out_signed(right1, distance - max_drift);
373 cur_h = env->hpos - max_drift;
375 else if (distance < -max_drift) {
376 out_signed(right1, distance + max_drift);
377 cur_h = env->hpos + max_drift;
379 if (env->vpos != cur_v) {
380 out_signed(down1, env->vpos - cur_v);
383 possibly_begin_line();
384 end_h = env->hpos + w;
385 cur_h += scale(f->get_width(g, UNITWIDTH) / MULTIPLIER,
386 cur_point_size * RES_7227);
391 if (code >= 0 && code <= 127)
394 out_unsigned(set1, code);
397 void dvi_printer::define_font(int i)
399 out_unsigned(fnt_def1, i);
400 dvi_font *f = output_font_table[i].f;
402 out4(output_font_table[i].point_size*RES_7227);
403 out4(int((double(f->design_size)/(1<<20))*RES_7227*100 + .5));
404 const char *nm = f->get_internal_name();
409 void dvi_printer::set_font(int i)
411 if (i >= 0 && i <= 63)
414 out_unsigned(fnt1, i);
417 void dvi_printer::out_signed(unsigned char base, int param)
419 if (-128 <= param && param < 128) {
423 else if (-32768 <= param && param < 32768) {
427 else if (-(1 << 23) <= param && param < (1 << 23)) {
437 void dvi_printer::out_unsigned(unsigned char base, int param)
444 else if (param < 65536) {
448 else if (param < (1 << 24)) {
463 void dvi_printer::preamble()
473 void dvi_printer::postamble()
475 int tem = byte_count;
483 out2(have_pushed); // stack depth
486 for (i = 0; i < FONTS_MAX && output_font_table[i].f != 0; i++)
491 for (i = 0; i < 4 || byte_count % 4 != 0; i++)
495 void dvi_printer::begin_page(int i)
498 int tem = byte_count;
501 for (int j = 1; j < 10; j++)
505 // By convention position (0,0) in a dvi file is placed at (1in, 1in).
509 if (page_count == 1) {
511 // at least dvips uses this
512 double length = user_paper_length ? user_paper_length :
513 double(font::paperlength) / font::res;
514 double width = user_paper_width ? user_paper_width :
515 double(font::paperwidth) / font::res;
516 if (width > 0 && length > 0) {
517 sprintf(buf, "papersize=%.3fin,%.3fin",
518 landscape_flag ? length : width,
519 landscape_flag ? width : length);
523 if (cur_color != default_color)
524 set_color(&cur_color);
527 void dvi_printer::end_page(int)
529 set_color(&default_color);
536 void draw_dvi_printer::end_page(int len)
538 dvi_printer::end_page(len);
539 output_pen_size = -1;
542 void dvi_printer::do_special(const char *s)
547 possibly_begin_line();
548 out_unsigned(xxx1, len);
553 void dvi_printer::special(char *arg, const environment *env, char type)
557 moveto(env->hpos, env->vpos);
561 void dvi_printer::moveto(int h, int v)
564 out_signed(right1, h - cur_h);
570 out_signed(down1, v - cur_v);
578 void dvi_printer::draw(int code, int *p, int np, const environment *env)
582 int height = 0, width = 0;
584 if (line_thickness < 0)
585 thickness = env->size*RES_7227*linewidth/1000;
586 else if (line_thickness > 0)
587 thickness = line_thickness;
591 error("2 arguments required for line");
593 else if (p[0] == 0) {
596 x = env->hpos - thickness/2;
597 y = env->vpos + p[1] + thickness/2;
598 height = p[1] + thickness;
602 x = env->hpos - thickness/2;
603 y = env->vpos + thickness/2;
604 height = thickness - p[1];
608 else if (p[1] == 0) {
610 x = env->hpos - thickness/2;
611 y = env->vpos + thickness/2;
613 width = p[0] + thickness;
616 x = env->hpos - p[0] - thickness/2;
617 y = env->vpos + thickness/2;
619 width = thickness - p[0];
629 else if (code == 't') {
634 // troff gratuitously adds an extra 0
635 if (np != 1 && np != 2)
636 error("0 or 1 argument required for thickness");
638 line_thickness = p[0];
641 else if (code == 'R') {
643 error("2 arguments required for rule");
644 else if (p[0] != 0 || p[1] != 0) {
665 // XXX Will this overflow?
667 inline int milliinches(int n)
669 return (n*1000 + font::res/2)/font::res;
672 void draw_dvi_printer::set_line_thickness(const environment *env)
675 = milliinches(line_thickness < 0
676 // Will this overflow?
677 ? env->size*RES_7227*linewidth/1000
679 if (desired_pen_size != output_pen_size) {
681 sprintf(buf, "pn %d", desired_pen_size);
683 output_pen_size = desired_pen_size;
687 void draw_dvi_printer::fill_next(const environment *env)
690 if (env->fill->is_default())
693 // currently, only BW support
694 env->fill->get_gray(&g);
697 sprintf(buf, "sh %.3g", 1 - double(g)/color::MAX_COLOR_VAL);
701 void draw_dvi_printer::draw(int code, int *p, int np, const environment *env)
711 // troff adds an extra argument to C
712 if (np != 1 && !(code == 'C' && np == 2)) {
713 error("1 argument required for circle");
716 moveto(env->hpos+p[0]/2, env->vpos);
720 set_line_thickness(env);
722 rad = milliinches(p[0]/2);
723 sprintf(buf, "%s 0 0 %d %d 0 6.28319",
724 (fill_flag ? "ia" : "ar"),
732 error("2 arguments required for line");
735 moveto(env->hpos, env->vpos);
736 set_line_thickness(env);
737 do_special("pa 0 0");
738 sprintf(buf, "pa %d %d", milliinches(p[0]), milliinches(p[1]));
747 error("2 arguments required for ellipse");
750 moveto(env->hpos+p[0]/2, env->vpos);
754 set_line_thickness(env);
755 sprintf(buf, "%s 0 0 %d %d 0 6.28319",
756 (fill_flag ? "ia" : "ar"),
758 milliinches(p[1]/2));
767 error("even number of arguments required for polygon");
771 error("no arguments for polygon");
774 moveto(env->hpos, env->vpos);
778 set_line_thickness(env);
779 do_special("pa 0 0");
781 for (int i = 0; i < np; i += 2) {
784 sprintf(buf, "pa %d %d", milliinches(h), milliinches(v));
787 do_special("pa 0 0");
788 do_special(fill_flag ? "ip" : "fp");
794 error("even number of arguments required for spline");
798 error("no arguments for spline");
801 moveto(env->hpos, env->vpos);
802 set_line_thickness(env);
803 do_special("pa 0 0");
805 for (int i = 0; i < np; i += 2) {
808 sprintf(buf, "pa %d %d", milliinches(h), milliinches(v));
817 error("4 arguments required for arc");
820 set_line_thickness(env);
822 if (adjust_arc_center(p, c)) {
823 int rad = milliinches(int(sqrt(c[0]*c[0] + c[1]*c[1]) + .5));
824 moveto(env->hpos + int(c[0]), env->vpos + int(c[1]));
825 double start = atan2(p[1] + p[3] - c[1], p[0] + p[2] - c[0]);
826 double end = atan2(-c[1], -c[0]);
828 start -= 2 * 3.14159265358;
829 sprintf(buf, "ar 0 0 %d %d %f %f", rad, rad, start, end);
833 moveto(env->hpos, env->vpos);
834 do_special("pa 0 0");
837 milliinches(p[0] + p[2]),
838 milliinches(p[1] + p[3]));
850 // troff gratuitously adds an extra 0
851 if (np != 1 && np != 2) {
852 error("0 or 1 argument required for thickness");
855 line_thickness = p[0];
862 error("2 arguments required for rule");
888 error("unrecognised drawing command '%1'", char(code));
893 font *dvi_printer::make_font(const char *nm)
895 return dvi_font::load_dvi_font(nm);
898 printer *make_printer()
901 return new draw_dvi_printer;
903 return new dvi_printer;
906 static void usage(FILE *stream);
908 int main(int argc, char **argv)
910 setlocale(LC_NUMERIC, "C");
911 program_name = argv[0];
912 static char stderr_buf[BUFSIZ];
913 setbuf(stderr, stderr_buf);
915 static const struct option long_options[] = {
916 { "help", no_argument, 0, CHAR_MAX + 1 },
917 { "version", no_argument, 0, 'v' },
920 while ((c = getopt_long(argc, argv, "dF:I:lp:vw:", long_options, NULL))
930 font::command_line_font_dir(optarg);
933 // ignore include search path
936 if (!font::scan_papersize(optarg, 0,
937 &user_paper_length, &user_paper_width))
938 error("invalid custom paper size '%1' ignored", optarg);
942 printf("GNU grodvi (groff) version %s\n", Version_string);
947 if (sscanf(optarg, "%d", &linewidth) != 1
948 || linewidth < 0 || linewidth > 1000) {
949 error("bad line width");
950 linewidth = DEFAULT_LINEWIDTH;
953 case CHAR_MAX + 1: // --help
964 SET_BINARY(fileno(stdout));
968 for (int i = optind; i < argc; i++)
974 static void usage(FILE *stream)
976 fprintf(stream, "usage: %s [-dv] [-F dir] [-w n] [files ...]\n",