2 /* Copyright (C) 1989-2000, 2001, 2002, 2003, 2004, 2005, 2006, 2009
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 3 of the License, or
11 (at your option) any later version.
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
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 typedef signed char schar;
28 implement_ptable(schar)
30 extern "C" const char *Version_string;
32 #define putstring(s) fputs(s, stdout)
35 #define SHRT_MIN (-32768)
39 #define SHRT_MAX 32767
44 // A character of the output device fits in a 32-bit word.
45 typedef unsigned int output_character;
47 static int horizontal_tab_flag = 0;
48 static int form_feed_flag = 0;
49 static int bold_flag_option = 1;
51 static int underline_flag_option = 1;
52 static int underline_flag;
53 static int overstrike_flag = 1;
54 static int draw_flag = 1;
55 static int italic_flag_option = 0;
56 static int italic_flag;
57 static int reverse_flag_option = 0;
58 static int reverse_flag;
59 static int old_drawing_scheme = 0;
61 static void update_options();
62 static void usage(FILE *stream);
64 static int hline_char = '-';
65 static int vline_char = '|';
68 UNDERLINE_MODE = 0x01,
78 // Mode to use for bold-underlining.
79 static unsigned char bold_underline_mode_option = BOLD_MODE|UNDERLINE_MODE;
80 static unsigned char bold_underline_mode;
82 #ifndef IS_EBCDIC_HOST
88 // SGR handling (ISO 6429)
89 #define SGR_BOLD CSI "1m"
90 #define SGR_NO_BOLD CSI "22m"
91 #define SGR_ITALIC CSI "3m"
92 #define SGR_NO_ITALIC CSI "23m"
93 #define SGR_UNDERLINE CSI "4m"
94 #define SGR_NO_UNDERLINE CSI "24m"
95 #define SGR_REVERSE CSI "7m"
96 #define SGR_NO_REVERSE CSI "27m"
97 // many terminals can't handle `CSI 39 m' and `CSI 49 m' to reset
98 // the foreground and background color, respectively; we thus use
99 // `CSI 0 m' exclusively
100 #define SGR_DEFAULT CSI "0m"
102 #define DEFAULT_COLOR_IDX -1
104 class tty_font : public font {
105 tty_font(const char *);
109 unsigned char get_mode() { return mode; }
111 void handle_x_command(int argc, const char **argv);
113 static tty_font *load_tty_font(const char *);
116 tty_font *tty_font::load_tty_font(const char *s)
118 tty_font *f = new tty_font(s);
123 const char *num = f->get_internal_name();
125 if (num != 0 && (n = strtol(num, 0, 0)) != 0)
126 f->mode = (unsigned char)(n & (BOLD_MODE|UNDERLINE_MODE));
128 f->mode &= ~UNDERLINE_MODE;
130 f->mode &= ~BOLD_MODE;
131 if ((f->mode & (BOLD_MODE|UNDERLINE_MODE)) == (BOLD_MODE|UNDERLINE_MODE))
132 f->mode = (unsigned char)((f->mode & ~(BOLD_MODE|UNDERLINE_MODE))
133 | bold_underline_mode);
137 tty_font::tty_font(const char *nm)
142 tty_font::~tty_font()
147 void tty_font::handle_x_command(int argc, const char **argv)
149 if (argc >= 1 && strcmp(argv[0], "bold") == 0)
151 else if (argc >= 1 && strcmp(argv[0], "underline") == 0)
152 mode |= UNDERLINE_MODE;
157 static tty_glyph *free_list;
164 schar back_color_idx;
165 schar fore_color_idx;
166 void *operator new(size_t);
167 void operator delete(void *);
168 inline int draw_mode() { return mode & (VDRAW_MODE|HDRAW_MODE); }
170 return mode & (VDRAW_MODE|HDRAW_MODE|CU_MODE|COLOR_CHANGE); }
173 tty_glyph *tty_glyph::free_list = 0;
175 void *tty_glyph::operator new(size_t)
178 const int BLOCK = 1024;
179 free_list = (tty_glyph *)new char[sizeof(tty_glyph) * BLOCK];
180 for (int i = 0; i < BLOCK - 1; i++)
181 free_list[i].next = free_list + i + 1;
182 free_list[BLOCK - 1].next = 0;
184 tty_glyph *p = free_list;
185 free_list = free_list->next;
190 void tty_glyph::operator delete(void *p)
193 ((tty_glyph *)p)->next = free_list;
194 free_list = (tty_glyph *)p;
198 class tty_printer : public printer {
209 PTABLE(schar) tty_colors;
210 void make_underline(int);
211 void make_bold(output_character, int);
212 schar color_to_idx(color *);
213 void add_char(output_character, int, int, int, color *, color *,
215 char *make_rgb_string(unsigned int, unsigned int, unsigned int);
216 int tty_color(unsigned int, unsigned int, unsigned int, schar *,
217 schar = DEFAULT_COLOR_IDX);
218 void line(int, int, int, int, color *, color *);
219 void draw_line(int *, int, const environment *);
220 void draw_polygon(int *, int, const environment *);
222 tty_printer(const char *);
224 void set_char(glyph *, font *, const environment *, int, const char *);
225 void draw(int, int *, int, const environment *);
226 void special(char *, const environment *, char);
227 void change_color(const environment * const);
228 void change_fill_color(const environment * const);
229 void put_char(output_character);
230 void put_color(schar, int);
231 void begin_page(int) { }
233 font *make_font(const char *);
236 char *tty_printer::make_rgb_string(unsigned int r,
240 char *s = new char[8];
242 s[1] = char(r & 0xff);
244 s[3] = char(g & 0xff);
246 s[5] = char(b & 0xff);
249 // avoid null-bytes in string
250 for (int i = 0; i < 6; i++)
258 int tty_printer::tty_color(unsigned int r,
260 unsigned int b, schar *idx, schar value)
262 int unknown_color = 0;
263 char *s = make_rgb_string(r, g, b);
264 schar *i = tty_colors.lookup(s);
269 tty_colors.define(s, i);
273 return unknown_color;
276 tty_printer::tty_printer(const char *dev) : cached_v(0)
278 is_utf8 = !strcmp(dev, "utf8");
285 (void)tty_color(0, 0, 0, &dummy, 0);
286 (void)tty_color(color::MAX_COLOR_VAL,
287 color::MAX_COLOR_VAL,
288 color::MAX_COLOR_VAL, &dummy, 7);
290 (void)tty_color(color::MAX_COLOR_VAL, 0, 0, &dummy, 1);
291 (void)tty_color(0, color::MAX_COLOR_VAL, 0, &dummy, 2);
292 (void)tty_color(0, 0, color::MAX_COLOR_VAL, &dummy, 4);
293 // yellow, magenta, cyan
294 (void)tty_color(color::MAX_COLOR_VAL, color::MAX_COLOR_VAL, 0, &dummy, 3);
295 (void)tty_color(color::MAX_COLOR_VAL, 0, color::MAX_COLOR_VAL, &dummy, 5);
296 (void)tty_color(0, color::MAX_COLOR_VAL, color::MAX_COLOR_VAL, &dummy, 6);
298 lines = new tty_glyph *[nlines];
299 for (int i = 0; i < nlines; i++)
304 tty_printer::~tty_printer()
309 void tty_printer::make_underline(int w)
311 if (old_drawing_scheme) {
313 warning("can't underline zero-width character");
315 int n = w / font::hor;
316 for (int i = 0; i < n; i++)
318 for (int j = 0; j < n; j++)
325 putstring(SGR_ITALIC);
326 else if (reverse_flag)
327 putstring(SGR_REVERSE);
329 putstring(SGR_UNDERLINE);
335 void tty_printer::make_bold(output_character c, int w)
337 if (old_drawing_scheme) {
339 warning("can't print zero-width character in bold");
341 int n = w / font::hor;
343 for (int i = 0; i < n; i++)
354 schar tty_printer::color_to_idx(color *col)
356 if (col->is_default())
357 return DEFAULT_COLOR_IDX;
358 unsigned int r, g, b;
359 col->get_rgb(&r, &g, &b);
361 if (tty_color(r, g, b, &idx)) {
362 char *s = col->print_color();
363 error("Unknown color (%1) mapped to default", s);
369 void tty_printer::set_char(glyph *g, font *f, const environment *env,
372 if (w % font::hor != 0)
373 fatal("width of character not a multiple of horizontal resolution");
374 add_char(f->get_code(g), w,
375 env->hpos, env->vpos,
377 ((tty_font *)f)->get_mode());
380 void tty_printer::add_char(output_character c, int w,
382 color *fore, color *back,
386 // This is too expensive.
387 if (h % font::hor != 0)
388 fatal("horizontal position not a multiple of horizontal resolution");
390 int hpos = h / font::hor;
391 if (hpos < SHRT_MIN || hpos > SHRT_MAX) {
392 error("character with ridiculous horizontal position discarded");
396 if (v == cached_v && cached_v != 0)
399 if (v % font::vert != 0)
400 fatal("vertical position not a multiple of vertical resolution");
401 vpos = v / font::vert;
403 tty_glyph **old_lines = lines;
404 lines = new tty_glyph *[vpos + 1];
405 memcpy(lines, old_lines, nlines * sizeof(tty_glyph *));
406 for (int i = nlines; i <= vpos; i++)
411 // Note that the first output line corresponds to groff
412 // position font::vert.
414 error("character above first line discarded");
420 tty_glyph *g = new tty_glyph;
424 g->fore_color_idx = color_to_idx(fore);
425 g->back_color_idx = color_to_idx(back);
428 // The list will be reversed later. After reversal, it must be in
429 // increasing order of hpos, with COLOR_CHANGE and CU specials before
430 // HDRAW characters before VDRAW characters before normal characters
431 // at each hpos, and otherwise in order of occurrence.
434 for (pp = lines + (vpos - 1); *pp; pp = &(*pp)->next)
435 if ((*pp)->hpos < hpos
436 || ((*pp)->hpos == hpos && (*pp)->order() >= g->order()))
442 void tty_printer::special(char *arg, const environment *env, char type)
445 add_char(*arg - '0', 0, env->hpos, env->vpos, env->col, env->fill,
452 for (p = arg; *p == ' ' || *p == '\n'; p++)
455 for (; *p != '\0' && *p != ':' && *p != ' ' && *p != '\n'; p++)
457 if (*p == '\0' || strncmp(tag, "tty", p - tag) != 0) {
458 error("X command without `tty:' tag ignored");
462 for (; *p == ' ' || *p == '\n'; p++)
465 for (; *p != '\0' && *p != ' ' && *p != '\n'; p++)
467 if (*command == '\0') {
468 error("empty X command ignored");
471 if (strncmp(command, "sgr", p - command) == 0) {
472 for (; *p == ' ' || *p == '\n'; p++)
475 if (*p != '\0' && sscanf(p, "%d", &n) == 1 && n == 0)
476 old_drawing_scheme = 1;
478 old_drawing_scheme = 0;
483 void tty_printer::change_color(const environment * const env)
485 add_char(0, 0, env->hpos, env->vpos, env->col, env->fill, COLOR_CHANGE);
488 void tty_printer::change_fill_color(const environment * const env)
490 add_char(0, 0, env->hpos, env->vpos, env->col, env->fill, COLOR_CHANGE);
493 void tty_printer::draw(int code, int *p, int np, const environment *env)
498 draw_line(p, np, env);
500 draw_polygon(p, np, env);
503 void tty_printer::draw_polygon(int *p, int np, const environment *env)
506 error("even number of arguments required for polygon");
510 error("no arguments for polygon");
513 // We only draw polygons which consist entirely of horizontal and
517 for (int i = 0; i < np; i += 2) {
518 if (!(p[i] == 0 || p[i + 1] == 0))
523 if (!(hpos == 0 || vpos == 0))
525 int start_hpos = env->hpos;
526 int start_vpos = env->vpos;
529 for (int i = 0; i < np; i += 2) {
530 line(hpos, vpos, p[i], p[i + 1], env->col, env->fill);
534 line(hpos, vpos, start_hpos - hpos, start_vpos - vpos,
535 env->col, env->fill);
538 void tty_printer::draw_line(int *p, int np, const environment *env)
541 error("2 arguments required for line");
544 line(env->hpos, env->vpos, p[0], p[1], env->col, env->fill);
547 void tty_printer::line(int hpos, int vpos, int dx, int dy,
548 color *col, color *fill)
559 add_char(vline_char, font::hor, hpos, v, col, fill,
560 VDRAW_MODE|START_LINE|END_LINE);
562 add_char(vline_char, font::hor, hpos, v, col, fill,
563 VDRAW_MODE|START_LINE);
567 add_char(vline_char, font::hor, hpos, v, col, fill,
568 VDRAW_MODE|START_LINE|END_LINE);
572 add_char(vline_char, font::hor, hpos, v, col, fill,
573 VDRAW_MODE|END_LINE);
585 add_char(hline_char, font::hor, h, vpos, col, fill,
586 HDRAW_MODE|START_LINE|END_LINE);
588 add_char(hline_char, font::hor, h, vpos, col, fill,
589 HDRAW_MODE|START_LINE);
593 add_char(hline_char, font::hor, h, vpos, col, fill,
594 HDRAW_MODE|START_LINE|END_LINE);
598 add_char(hline_char, font::hor, h, vpos, col, fill,
599 HDRAW_MODE|END_LINE);
604 void tty_printer::put_char(output_character wc)
606 if (is_utf8 && wc >= 0x80) {
611 count = 1, *p = (unsigned char)((wc >> 6) | 0xc0);
612 else if (wc < 0x10000)
613 count = 2, *p = (unsigned char)((wc >> 12) | 0xe0);
614 else if (wc < 0x200000)
615 count = 3, *p = (unsigned char)((wc >> 18) | 0xf0);
616 else if (wc < 0x4000000)
617 count = 4, *p = (unsigned char)((wc >> 24) | 0xf8);
618 else if (wc <= 0x7fffffff)
619 count = 5, *p = (unsigned char)((wc >> 30) | 0xfC);
622 do *++p = (unsigned char)(((wc >> (6 * --count)) & 0x3f) | 0x80);
631 void tty_printer::put_color(schar color_index, int back)
633 if (color_index == DEFAULT_COLOR_IDX) {
634 putstring(SGR_DEFAULT);
635 // set bold and underline again
640 putstring(SGR_ITALIC);
641 else if (reverse_flag)
642 putstring(SGR_REVERSE);
644 putstring(SGR_UNDERLINE);
646 // set other color again
648 color_index = back ? curr_back_idx : curr_fore_idx;
650 if (color_index != DEFAULT_COLOR_IDX) {
656 putchar(color_index + '0');
661 // The possible Unicode combinations for crossing characters.
663 // ` ' = 0, ` -' = 4, `- ' = 8, `--' = 12,
665 // ` ' = 0, ` ' = 1, `|' = 2, `|' = 3
668 static output_character crossings[4*4] = {
669 0x0000, 0x2577, 0x2575, 0x2502,
670 0x2576, 0x250C, 0x2514, 0x251C,
671 0x2574, 0x2510, 0x2518, 0x2524,
672 0x2500, 0x252C, 0x2534, 0x253C
675 void tty_printer::end_page(int page_length)
677 if (page_length % font::vert != 0)
678 error("vertical position at end of page not multiple of vertical resolution");
679 int lines_per_page = page_length / font::vert;
681 for (last_line = nlines; last_line > 0; last_line--)
682 if (lines[last_line - 1])
685 if (last_line > lines_per_page) {
686 error("characters past last line discarded");
689 while (lines[last_line]) {
690 tty_glyph *tem = lines[last_line];
691 lines[last_line] = tem->next;
694 } while (last_line > lines_per_page);
697 for (int i = 0; i < last_line; i++) {
698 tty_glyph *p = lines[i];
702 tty_glyph *tem = p->next;
709 curr_fore_idx = DEFAULT_COLOR_IDX;
710 curr_back_idx = DEFAULT_COLOR_IDX;
713 for (p = g; p; delete p, p = nextp) {
715 if (p->mode & CU_MODE) {
716 cu_flag = (p->code != 0);
719 if (nextp && p->hpos == nextp->hpos) {
720 if (p->draw_mode() == HDRAW_MODE &&
721 nextp->draw_mode() == VDRAW_MODE) {
724 crossings[((p->mode & (START_LINE|END_LINE)) >> 4)
725 + ((nextp->mode & (START_LINE|END_LINE)) >> 6)];
730 if (p->draw_mode() != 0 && p->draw_mode() == nextp->draw_mode()) {
731 nextp->code = p->code;
734 if (!overstrike_flag)
737 if (hpos > p->hpos) {
741 } while (hpos > p->hpos);
744 if (horizontal_tab_flag) {
746 int next_tab_pos = ((hpos + TAB_WIDTH) / TAB_WIDTH) * TAB_WIDTH;
747 if (next_tab_pos > p->hpos)
750 make_underline(p->w);
751 else if (!old_drawing_scheme && is_underline) {
753 putstring(SGR_NO_ITALIC);
754 else if (reverse_flag)
755 putstring(SGR_NO_REVERSE);
757 putstring(SGR_NO_UNDERLINE);
764 for (; hpos < p->hpos; hpos++) {
766 make_underline(p->w);
767 else if (!old_drawing_scheme && is_underline) {
769 putstring(SGR_NO_ITALIC);
770 else if (reverse_flag)
771 putstring(SGR_NO_REVERSE);
773 putstring(SGR_NO_UNDERLINE);
779 assert(hpos == p->hpos);
780 if (p->mode & COLOR_CHANGE) {
781 if (!old_drawing_scheme) {
782 if (p->fore_color_idx != curr_fore_idx) {
783 put_color(p->fore_color_idx, 0);
784 curr_fore_idx = p->fore_color_idx;
786 if (p->back_color_idx != curr_back_idx) {
787 put_color(p->back_color_idx, 1);
788 curr_back_idx = p->back_color_idx;
793 if (p->mode & UNDERLINE_MODE)
794 make_underline(p->w);
795 else if (!old_drawing_scheme && is_underline) {
797 putstring(SGR_NO_ITALIC);
798 else if (reverse_flag)
799 putstring(SGR_NO_REVERSE);
801 putstring(SGR_NO_UNDERLINE);
804 if (p->mode & BOLD_MODE)
805 make_bold(p->code, p->w);
806 else if (!old_drawing_scheme && is_bold) {
807 putstring(SGR_NO_BOLD);
810 if (!old_drawing_scheme) {
811 if (p->fore_color_idx != curr_fore_idx) {
812 put_color(p->fore_color_idx, 0);
813 curr_fore_idx = p->fore_color_idx;
815 if (p->back_color_idx != curr_back_idx) {
816 put_color(p->back_color_idx, 1);
817 curr_back_idx = p->back_color_idx;
821 hpos += p->w / font::hor;
823 if (!old_drawing_scheme
824 && (is_bold || is_underline
825 || curr_fore_idx != DEFAULT_COLOR_IDX
826 || curr_back_idx != DEFAULT_COLOR_IDX))
827 putstring(SGR_DEFAULT);
830 if (form_feed_flag) {
831 if (last_line < lines_per_page)
835 for (; last_line < lines_per_page; last_line++)
840 font *tty_printer::make_font(const char *nm)
842 return tty_font::load_tty_font(nm);
845 printer *make_printer()
847 return new tty_printer(device);
850 static void update_options()
852 if (old_drawing_scheme) {
855 bold_underline_mode = bold_underline_mode_option;
856 bold_flag = bold_flag_option;
857 underline_flag = underline_flag_option;
860 italic_flag = italic_flag_option;
861 reverse_flag = reverse_flag_option;
862 bold_underline_mode = BOLD_MODE|UNDERLINE_MODE;
868 int main(int argc, char **argv)
870 program_name = argv[0];
871 static char stderr_buf[BUFSIZ];
872 if (getenv("GROFF_NO_SGR"))
873 old_drawing_scheme = 1;
874 setbuf(stderr, stderr_buf);
876 static const struct option long_options[] = {
877 { "help", no_argument, 0, CHAR_MAX + 1 },
878 { "version", no_argument, 0, 'v' },
881 while ((c = getopt_long(argc, argv, "bBcdfF:hiI:oruUv", long_options, NULL))
885 printf("GNU grotty (groff) version %s\n", Version_string);
889 // Use italic font instead of underlining.
890 italic_flag_option = 1;
893 // ignore include search path
896 // Do not embolden by overstriking.
897 bold_flag_option = 0;
900 // Use old scheme for emboldening and underline.
901 old_drawing_scheme = 1;
905 underline_flag_option = 0;
908 // Do not overstrike (other than emboldening and underlining).
912 // Use reverse mode instead of underlining.
913 reverse_flag_option = 1;
916 // Do bold-underlining as bold.
917 bold_underline_mode_option = BOLD_MODE;
920 // Do bold-underlining as underlining.
921 bold_underline_mode_option = UNDERLINE_MODE;
924 // Use horizontal tabs.
925 horizontal_tab_flag = 1;
931 font::command_line_font_dir(optarg);
934 // Ignore \D commands.
937 case CHAR_MAX + 1: // --help
952 for (int i = optind; i < argc; i++)
958 static void usage(FILE *stream)
960 fprintf(stream, "usage: %s [-bBcdfhioruUv] [-F dir] [files ...]\n",