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 typedef signed char schar;
27 implement_ptable(schar)
29 extern "C" const char *Version_string;
31 #define putstring(s) fputs(s, stdout)
34 #define SHRT_MIN (-32768)
38 #define SHRT_MAX 32767
43 // A character of the output device fits in a 32-bit word.
44 typedef unsigned int output_character;
46 static int horizontal_tab_flag = 0;
47 static int form_feed_flag = 0;
48 static int bold_flag_option = 1;
50 static int underline_flag_option = 1;
51 static int underline_flag;
52 static int overstrike_flag = 1;
53 static int draw_flag = 1;
54 static int italic_flag_option = 0;
55 static int italic_flag;
56 static int reverse_flag_option = 0;
57 static int reverse_flag;
58 static int old_drawing_scheme = 0;
60 static void update_options();
61 static void usage(FILE *stream);
63 static int hline_char = '-';
64 static int vline_char = '|';
67 UNDERLINE_MODE = 0x01,
77 // Mode to use for bold-underlining.
78 static unsigned char bold_underline_mode_option = BOLD_MODE|UNDERLINE_MODE;
79 static unsigned char bold_underline_mode;
81 #ifndef IS_EBCDIC_HOST
87 // SGR handling (ISO 6429)
88 #define SGR_BOLD CSI "1m"
89 #define SGR_NO_BOLD CSI "22m"
90 #define SGR_ITALIC CSI "3m"
91 #define SGR_NO_ITALIC CSI "23m"
92 #define SGR_UNDERLINE CSI "4m"
93 #define SGR_NO_UNDERLINE CSI "24m"
94 #define SGR_REVERSE CSI "7m"
95 #define SGR_NO_REVERSE CSI "27m"
96 // many terminals can't handle 'CSI 39 m' and 'CSI 49 m' to reset
97 // the foreground and background color, respectively; we thus use
98 // 'CSI 0 m' exclusively
99 #define SGR_DEFAULT CSI "0m"
101 #define DEFAULT_COLOR_IDX -1
103 class tty_font : public font {
104 tty_font(const char *);
108 unsigned char get_mode() { return mode; }
110 void handle_x_command(int argc, const char **argv);
112 static tty_font *load_tty_font(const char *);
115 tty_font *tty_font::load_tty_font(const char *s)
117 tty_font *f = new tty_font(s);
122 const char *num = f->get_internal_name();
124 if (num != 0 && (n = strtol(num, 0, 0)) != 0)
125 f->mode = (unsigned char)(n & (BOLD_MODE|UNDERLINE_MODE));
127 f->mode &= ~UNDERLINE_MODE;
129 f->mode &= ~BOLD_MODE;
130 if ((f->mode & (BOLD_MODE|UNDERLINE_MODE)) == (BOLD_MODE|UNDERLINE_MODE))
131 f->mode = (unsigned char)((f->mode & ~(BOLD_MODE|UNDERLINE_MODE))
132 | bold_underline_mode);
136 tty_font::tty_font(const char *nm)
141 tty_font::~tty_font()
146 void tty_font::handle_x_command(int argc, const char **argv)
148 if (argc >= 1 && strcmp(argv[0], "bold") == 0)
150 else if (argc >= 1 && strcmp(argv[0], "underline") == 0)
151 mode |= UNDERLINE_MODE;
162 schar back_color_idx;
163 schar fore_color_idx;
164 inline int draw_mode() { return mode & (VDRAW_MODE|HDRAW_MODE); }
166 return mode & (VDRAW_MODE|HDRAW_MODE|CU_MODE|COLOR_CHANGE); }
170 class tty_printer : public printer {
180 PTABLE(schar) tty_colors;
181 void make_underline(int);
182 void make_bold(output_character, int);
183 schar color_to_idx(color *);
184 void add_char(output_character, int, int, int, color *, color *,
186 char *make_rgb_string(unsigned int, unsigned int, unsigned int);
187 int tty_color(unsigned int, unsigned int, unsigned int, schar *,
188 schar = DEFAULT_COLOR_IDX);
189 void line(int, int, int, int, color *, color *);
190 void draw_line(int *, int, const environment *);
191 void draw_polygon(int *, int, const environment *);
195 void set_char(glyph *, font *, const environment *, int, const char *);
196 void draw(int, int *, int, const environment *);
197 void special(char *, const environment *, char);
198 void change_color(const environment * const);
199 void change_fill_color(const environment * const);
200 void put_char(output_character);
201 void put_color(schar, int);
202 void begin_page(int) { }
204 font *make_font(const char *);
207 char *tty_printer::make_rgb_string(unsigned int r,
211 char *s = new char[8];
213 s[1] = char(r & 0xff);
215 s[3] = char(g & 0xff);
217 s[5] = char(b & 0xff);
220 // avoid null-bytes in string
221 for (int i = 0; i < 6; i++)
229 int tty_printer::tty_color(unsigned int r,
231 unsigned int b, schar *idx, schar value)
233 int unknown_color = 0;
234 char *s = make_rgb_string(r, g, b);
235 schar *i = tty_colors.lookup(s);
240 tty_colors.define(s, i);
244 return unknown_color;
247 tty_printer::tty_printer() : cached_v(0)
249 if (font::is_unicode) {
255 (void)tty_color(0, 0, 0, &dummy, 0);
256 (void)tty_color(color::MAX_COLOR_VAL,
257 color::MAX_COLOR_VAL,
258 color::MAX_COLOR_VAL, &dummy, 7);
260 (void)tty_color(color::MAX_COLOR_VAL, 0, 0, &dummy, 1);
261 (void)tty_color(0, color::MAX_COLOR_VAL, 0, &dummy, 2);
262 (void)tty_color(0, 0, color::MAX_COLOR_VAL, &dummy, 4);
263 // yellow, magenta, cyan
264 (void)tty_color(color::MAX_COLOR_VAL, color::MAX_COLOR_VAL, 0, &dummy, 3);
265 (void)tty_color(color::MAX_COLOR_VAL, 0, color::MAX_COLOR_VAL, &dummy, 5);
266 (void)tty_color(0, color::MAX_COLOR_VAL, color::MAX_COLOR_VAL, &dummy, 6);
268 lines = new tty_glyph *[nlines];
269 for (int i = 0; i < nlines; i++)
274 tty_printer::~tty_printer()
279 void tty_printer::make_underline(int w)
281 if (old_drawing_scheme) {
283 warning("can't underline zero-width character");
292 putstring(SGR_ITALIC);
293 else if (reverse_flag)
294 putstring(SGR_REVERSE);
296 putstring(SGR_UNDERLINE);
302 void tty_printer::make_bold(output_character c, int w)
304 if (old_drawing_scheme) {
306 warning("can't print zero-width character in bold");
319 schar tty_printer::color_to_idx(color *col)
321 if (col->is_default())
322 return DEFAULT_COLOR_IDX;
323 unsigned int r, g, b;
324 col->get_rgb(&r, &g, &b);
326 if (tty_color(r, g, b, &idx)) {
327 char *s = col->print_color();
328 error("Unknown color (%1) mapped to default", s);
334 void tty_printer::set_char(glyph *g, font *f, const environment *env,
337 if (w % font::hor != 0)
338 fatal("width of character not a multiple of horizontal resolution");
339 add_char(f->get_code(g), w,
340 env->hpos, env->vpos,
342 ((tty_font *)f)->get_mode());
345 void tty_printer::add_char(output_character c, int w,
347 color *fore, color *back,
351 // This is too expensive.
352 if (h % font::hor != 0)
353 fatal("horizontal position not a multiple of horizontal resolution");
355 int hpos = h / font::hor;
356 if (hpos < SHRT_MIN || hpos > SHRT_MAX) {
357 error("character with ridiculous horizontal position discarded");
361 if (v == cached_v && cached_v != 0)
364 if (v % font::vert != 0)
365 fatal("vertical position not a multiple of vertical resolution");
366 vpos = v / font::vert;
368 tty_glyph **old_lines = lines;
369 lines = new tty_glyph *[vpos + 1];
370 memcpy(lines, old_lines, nlines * sizeof(tty_glyph *));
371 for (int i = nlines; i <= vpos; i++)
376 // Note that the first output line corresponds to groff
377 // position font::vert.
379 error("character above first line discarded");
385 tty_glyph *g = new tty_glyph;
389 g->fore_color_idx = color_to_idx(fore);
390 g->back_color_idx = color_to_idx(back);
393 // The list will be reversed later. After reversal, it must be in
394 // increasing order of hpos, with COLOR_CHANGE and CU specials before
395 // HDRAW characters before VDRAW characters before normal characters
396 // at each hpos, and otherwise in order of occurrence.
399 for (pp = lines + (vpos - 1); *pp; pp = &(*pp)->next)
400 if ((*pp)->hpos < hpos
401 || ((*pp)->hpos == hpos && (*pp)->order() >= g->order()))
407 void tty_printer::special(char *arg, const environment *env, char type)
410 add_char(*arg - '0', 0, env->hpos, env->vpos, env->col, env->fill,
417 for (p = arg; *p == ' ' || *p == '\n'; p++)
420 for (; *p != '\0' && *p != ':' && *p != ' ' && *p != '\n'; p++)
422 if (*p == '\0' || strncmp(tag, "tty", p - tag) != 0) {
423 error("X command without 'tty:' tag ignored");
427 for (; *p == ' ' || *p == '\n'; p++)
430 for (; *p != '\0' && *p != ' ' && *p != '\n'; p++)
432 if (*command == '\0') {
433 error("empty X command ignored");
436 if (strncmp(command, "sgr", p - command) == 0) {
437 for (; *p == ' ' || *p == '\n'; p++)
440 if (*p != '\0' && sscanf(p, "%d", &n) == 1 && n == 0)
441 old_drawing_scheme = 1;
443 old_drawing_scheme = 0;
448 void tty_printer::change_color(const environment * const env)
450 add_char(0, 0, env->hpos, env->vpos, env->col, env->fill, COLOR_CHANGE);
453 void tty_printer::change_fill_color(const environment * const env)
455 add_char(0, 0, env->hpos, env->vpos, env->col, env->fill, COLOR_CHANGE);
458 void tty_printer::draw(int code, int *p, int np, const environment *env)
463 draw_line(p, np, env);
465 draw_polygon(p, np, env);
468 void tty_printer::draw_polygon(int *p, int np, const environment *env)
471 error("even number of arguments required for polygon");
475 error("no arguments for polygon");
478 // We only draw polygons which consist entirely of horizontal and
482 for (int i = 0; i < np; i += 2) {
483 if (!(p[i] == 0 || p[i + 1] == 0))
488 if (!(hpos == 0 || vpos == 0))
490 int start_hpos = env->hpos;
491 int start_vpos = env->vpos;
494 for (int i = 0; i < np; i += 2) {
495 line(hpos, vpos, p[i], p[i + 1], env->col, env->fill);
499 line(hpos, vpos, start_hpos - hpos, start_vpos - vpos,
500 env->col, env->fill);
503 void tty_printer::draw_line(int *p, int np, const environment *env)
506 error("2 arguments required for line");
509 line(env->hpos, env->vpos, p[0], p[1], env->col, env->fill);
512 void tty_printer::line(int hpos, int vpos, int dx, int dy,
513 color *col, color *fill)
524 add_char(vline_char, font::hor, hpos, v, col, fill,
525 VDRAW_MODE|START_LINE|END_LINE);
527 add_char(vline_char, font::hor, hpos, v, col, fill,
528 VDRAW_MODE|START_LINE);
532 add_char(vline_char, font::hor, hpos, v, col, fill,
533 VDRAW_MODE|START_LINE|END_LINE);
537 add_char(vline_char, font::hor, hpos, v, col, fill,
538 VDRAW_MODE|END_LINE);
550 add_char(hline_char, font::hor, h, vpos, col, fill,
551 HDRAW_MODE|START_LINE|END_LINE);
553 add_char(hline_char, font::hor, h, vpos, col, fill,
554 HDRAW_MODE|START_LINE);
558 add_char(hline_char, font::hor, h, vpos, col, fill,
559 HDRAW_MODE|START_LINE|END_LINE);
563 add_char(hline_char, font::hor, h, vpos, col, fill,
564 HDRAW_MODE|END_LINE);
569 void tty_printer::put_char(output_character wc)
571 if (font::is_unicode && wc >= 0x80) {
576 count = 1, *p = (unsigned char)((wc >> 6) | 0xc0);
577 else if (wc < 0x10000)
578 count = 2, *p = (unsigned char)((wc >> 12) | 0xe0);
579 else if (wc < 0x200000)
580 count = 3, *p = (unsigned char)((wc >> 18) | 0xf0);
581 else if (wc < 0x4000000)
582 count = 4, *p = (unsigned char)((wc >> 24) | 0xf8);
583 else if (wc <= 0x7fffffff)
584 count = 5, *p = (unsigned char)((wc >> 30) | 0xfC);
587 do *++p = (unsigned char)(((wc >> (6 * --count)) & 0x3f) | 0x80);
596 void tty_printer::put_color(schar color_index, int back)
598 if (color_index == DEFAULT_COLOR_IDX) {
599 putstring(SGR_DEFAULT);
600 // set bold and underline again
605 putstring(SGR_ITALIC);
606 else if (reverse_flag)
607 putstring(SGR_REVERSE);
609 putstring(SGR_UNDERLINE);
611 // set other color again
613 color_index = back ? curr_back_idx : curr_fore_idx;
615 if (color_index != DEFAULT_COLOR_IDX) {
621 putchar(color_index + '0');
626 // The possible Unicode combinations for crossing characters.
628 // ' ' = 0, ' -' = 4, '- ' = 8, '--' = 12,
630 // ' ' = 0, ' ' = 1, '|' = 2, '|' = 3
633 static output_character crossings[4*4] = {
634 0x0000, 0x2577, 0x2575, 0x2502,
635 0x2576, 0x250C, 0x2514, 0x251C,
636 0x2574, 0x2510, 0x2518, 0x2524,
637 0x2500, 0x252C, 0x2534, 0x253C
640 void tty_printer::end_page(int page_length)
642 if (page_length % font::vert != 0)
643 error("vertical position at end of page not multiple of vertical resolution");
644 int lines_per_page = page_length / font::vert;
646 for (last_line = nlines; last_line > 0; last_line--)
647 if (lines[last_line - 1])
650 if (last_line > lines_per_page) {
651 error("characters past last line discarded");
654 while (lines[last_line]) {
655 tty_glyph *tem = lines[last_line];
656 lines[last_line] = tem->next;
659 } while (last_line > lines_per_page);
662 for (int i = 0; i < last_line; i++) {
663 tty_glyph *p = lines[i];
667 tty_glyph *tem = p->next;
674 curr_fore_idx = DEFAULT_COLOR_IDX;
675 curr_back_idx = DEFAULT_COLOR_IDX;
678 for (p = g; p; delete p, p = nextp) {
680 if (p->mode & CU_MODE) {
681 cu_flag = (p->code != 0);
684 if (nextp && p->hpos == nextp->hpos) {
685 if (p->draw_mode() == HDRAW_MODE &&
686 nextp->draw_mode() == VDRAW_MODE) {
687 if (font::is_unicode)
689 crossings[((p->mode & (START_LINE|END_LINE)) >> 4)
690 + ((nextp->mode & (START_LINE|END_LINE)) >> 6)];
695 if (p->draw_mode() != 0 && p->draw_mode() == nextp->draw_mode()) {
696 nextp->code = p->code;
699 if (!overstrike_flag)
702 if (hpos > p->hpos) {
706 } while (hpos > p->hpos);
709 if (horizontal_tab_flag) {
711 int next_tab_pos = ((hpos + TAB_WIDTH) / TAB_WIDTH) * TAB_WIDTH;
712 if (next_tab_pos > p->hpos)
715 make_underline(p->w);
716 else if (!old_drawing_scheme && is_underline) {
718 putstring(SGR_NO_ITALIC);
719 else if (reverse_flag)
720 putstring(SGR_NO_REVERSE);
722 putstring(SGR_NO_UNDERLINE);
729 for (; hpos < p->hpos; hpos++) {
731 make_underline(p->w);
732 else if (!old_drawing_scheme && is_underline) {
734 putstring(SGR_NO_ITALIC);
735 else if (reverse_flag)
736 putstring(SGR_NO_REVERSE);
738 putstring(SGR_NO_UNDERLINE);
744 assert(hpos == p->hpos);
745 if (p->mode & COLOR_CHANGE) {
746 if (!old_drawing_scheme) {
747 if (p->fore_color_idx != curr_fore_idx) {
748 put_color(p->fore_color_idx, 0);
749 curr_fore_idx = p->fore_color_idx;
751 if (p->back_color_idx != curr_back_idx) {
752 put_color(p->back_color_idx, 1);
753 curr_back_idx = p->back_color_idx;
758 if (p->mode & UNDERLINE_MODE)
759 make_underline(p->w);
760 else if (!old_drawing_scheme && is_underline) {
762 putstring(SGR_NO_ITALIC);
763 else if (reverse_flag)
764 putstring(SGR_NO_REVERSE);
766 putstring(SGR_NO_UNDERLINE);
769 if (p->mode & BOLD_MODE)
770 make_bold(p->code, p->w);
771 else if (!old_drawing_scheme && is_bold) {
772 putstring(SGR_NO_BOLD);
775 if (!old_drawing_scheme) {
776 if (p->fore_color_idx != curr_fore_idx) {
777 put_color(p->fore_color_idx, 0);
778 curr_fore_idx = p->fore_color_idx;
780 if (p->back_color_idx != curr_back_idx) {
781 put_color(p->back_color_idx, 1);
782 curr_back_idx = p->back_color_idx;
786 hpos += p->w / font::hor;
788 if (!old_drawing_scheme
789 && (is_bold || is_underline
790 || curr_fore_idx != DEFAULT_COLOR_IDX
791 || curr_back_idx != DEFAULT_COLOR_IDX))
792 putstring(SGR_DEFAULT);
795 if (form_feed_flag) {
796 if (last_line < lines_per_page)
800 for (; last_line < lines_per_page; last_line++)
805 font *tty_printer::make_font(const char *nm)
807 return tty_font::load_tty_font(nm);
810 printer *make_printer()
812 return new tty_printer();
815 static void update_options()
817 if (old_drawing_scheme) {
820 bold_underline_mode = bold_underline_mode_option;
821 bold_flag = bold_flag_option;
822 underline_flag = underline_flag_option;
825 italic_flag = italic_flag_option;
826 reverse_flag = reverse_flag_option;
827 bold_underline_mode = BOLD_MODE|UNDERLINE_MODE;
833 int main(int argc, char **argv)
835 program_name = argv[0];
836 static char stderr_buf[BUFSIZ];
837 if (getenv("GROFF_NO_SGR"))
838 old_drawing_scheme = 1;
839 setbuf(stderr, stderr_buf);
840 setlocale(LC_CTYPE, "");
842 static const struct option long_options[] = {
843 { "help", no_argument, 0, CHAR_MAX + 1 },
844 { "version", no_argument, 0, 'v' },
847 while ((c = getopt_long(argc, argv, "bBcdfF:hiI:oruUv", long_options, NULL))
851 printf("GNU grotty (groff) version %s\n", Version_string);
855 // Use italic font instead of underlining.
856 italic_flag_option = 1;
859 // ignore include search path
862 // Do not embolden by overstriking.
863 bold_flag_option = 0;
866 // Use old scheme for emboldening and underline.
867 old_drawing_scheme = 1;
871 underline_flag_option = 0;
874 // Do not overstrike (other than emboldening and underlining).
878 // Use reverse mode instead of underlining.
879 reverse_flag_option = 1;
882 // Do bold-underlining as bold.
883 bold_underline_mode_option = BOLD_MODE;
886 // Do bold-underlining as underlining.
887 bold_underline_mode_option = UNDERLINE_MODE;
890 // Use horizontal tabs.
891 horizontal_tab_flag = 1;
897 font::command_line_font_dir(optarg);
900 // Ignore \D commands.
903 case CHAR_MAX + 1: // --help
918 for (int i = optind; i < argc; i++)
924 static void usage(FILE *stream)
926 fprintf(stream, "usage: %s [-bBcdfhioruUv] [-F dir] [files ...]\n",