home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / GROFFSRC / SRC / GROTTY / TTY.CC < prev   
Encoding:
C/C++ Source or Header  |  1993-04-01  |  9.9 KB  |  440 lines

  1. // -*- C++ -*-
  2. /* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  3.      Written by James Clark (jjc@jclark.com)
  4.  
  5. This file is part of groff.
  6.  
  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
  10. version.
  11.  
  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
  15. for more details.
  16.  
  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, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include "driver.h"
  22.  
  23. #ifndef USHRT_MAX
  24. #define USHRT_MAX 65535
  25. #endif
  26.  
  27. #define TAB_WIDTH 8
  28.  
  29. static int horizontal_tab_flag = 0;
  30. static int form_feed_flag = 0;
  31. static int bold_flag = 1;
  32. static int underline_flag = 1;
  33. static int overstrike_flag = 1;
  34. static int draw_flag = 1;
  35.  
  36. enum {
  37.   UNDERLINE_MODE = 01,
  38.   BOLD_MODE = 02,
  39.   VDRAW_MODE = 04,
  40.   HDRAW_MODE = 010
  41. };
  42.  
  43. // Mode to use for bold-underlining.
  44. static unsigned char bold_underline_mode = BOLD_MODE|UNDERLINE_MODE;
  45.  
  46. class tty_font : public font {
  47.   tty_font(const char *);
  48.   unsigned char mode;
  49. public:
  50.   ~tty_font();
  51.   unsigned char get_mode() { return mode; }
  52. #if 0
  53.   void handle_x_command(int argc, const char **argv);
  54. #endif
  55.   static tty_font *load_tty_font(const char *);
  56. };
  57.  
  58. tty_font *tty_font::load_tty_font(const char *s)
  59. {
  60.   tty_font *f = new tty_font(s);
  61.   if (!f->load()) {
  62.     delete f;
  63.     return 0;
  64.   }
  65.   const char *num = f->get_internal_name();
  66.   long n;
  67.   if (num != 0 && (n = strtol(num, 0, 0)) != 0)
  68.     f->mode = int(n & (BOLD_MODE|UNDERLINE_MODE));
  69.   if (!underline_flag)
  70.     f->mode &= ~UNDERLINE_MODE;
  71.   if (!bold_flag)
  72.     f->mode &= ~BOLD_MODE;
  73.   if ((f->mode & (BOLD_MODE|UNDERLINE_MODE)) == (BOLD_MODE|UNDERLINE_MODE))
  74.     f->mode = (f->mode & ~(BOLD_MODE|UNDERLINE_MODE)) | bold_underline_mode;
  75.   return f;
  76. }
  77.  
  78. tty_font::tty_font(const char *nm)
  79. : font(nm), mode(0)
  80. {
  81. }
  82.  
  83. tty_font::~tty_font()
  84. {
  85. }
  86.  
  87. #if 0
  88. void tty_font::handle_x_command(int argc, const char **argv)
  89. {
  90.   if (argc >= 1 && strcmp(argv[0], "bold") == 0)
  91.     mode |= BOLD_MODE;
  92.   else if (argc >= 1 && strcmp(argv[0], "underline") == 0)
  93.     mode |= UNDERLINE_MODE;
  94. }
  95. #endif
  96.  
  97. class glyph {
  98.   static glyph *free_list;
  99. public:
  100.   glyph *next;
  101.   unsigned short hpos;
  102.   unsigned char code;
  103.   unsigned char mode;
  104.   void *operator new(size_t);
  105.   void operator delete(void *);
  106.   inline int draw_mode() { return mode & (VDRAW_MODE|HDRAW_MODE); }
  107. };
  108.  
  109. glyph *glyph::free_list = 0;
  110.  
  111. void *glyph::operator new(size_t)
  112. {
  113.   if (!free_list) {
  114.     const int BLOCK = 1024;
  115.     free_list = (glyph *)new char[sizeof(glyph)*BLOCK];
  116.     for (int i = 0; i < BLOCK - 1; i++)
  117.       free_list[i].next = free_list + i + 1;
  118.     free_list[BLOCK - 1].next = 0;
  119.   }
  120.   glyph *p = free_list;
  121.   free_list = free_list->next;
  122.   p->next = 0;
  123.   return p;
  124. }
  125.  
  126. void glyph::operator delete(void *p)
  127. {
  128.   if (p) {
  129.     ((glyph *)p)->next = free_list;
  130.     free_list = (glyph *)p;
  131.   }
  132. }
  133.  
  134. class tty_printer : public printer {
  135.   glyph **lines;
  136.   int nlines;
  137.   int cached_v;
  138.   int cached_vpos;
  139.   void add_char(unsigned char, int, int, unsigned char);
  140. public:
  141.   tty_printer();
  142.   ~tty_printer();
  143.   void set_char(int, font *, const environment *, int);
  144.   void draw(int code, int *p, int np, const environment *env);
  145.   void begin_page(int) { }
  146.   void end_page(int page_length);
  147.   font *make_font(const char *);
  148. };
  149.  
  150. tty_printer::tty_printer() : cached_v(0)
  151. {
  152.   nlines = 66;
  153.   lines = new glyph *[nlines];
  154.   for (int i = 0; i < nlines; i++)
  155.     lines[i] = 0;
  156. }
  157.  
  158. tty_printer::~tty_printer()
  159. {
  160.   a_delete lines;
  161. }
  162.  
  163. void tty_printer::set_char(int i, font *f, const environment *env, int w)
  164. {
  165.   if (w != font::hor)
  166.     fatal("width of character not equal to horizontal resolution");
  167.   add_char(f->get_code(i), env->hpos, env->vpos, ((tty_font *)f)->get_mode());
  168. }
  169.  
  170. void tty_printer::add_char(unsigned char c, int h, int v, unsigned char mode)
  171. {
  172. #if 0
  173.   // This is too expensive.
  174.   if (h % font::hor != 0)
  175.     fatal("horizontal position not a multiple of horizontal resolution");
  176. #endif
  177.   int hpos = h / font::hor;
  178.   if (hpos < 0) {
  179.     error("character to the left of first column discarded");
  180.     return;
  181.   }
  182.   if (hpos > USHRT_MAX) {
  183.     error("character with ridiculously large horizontal position discarded");
  184.     return;
  185.   }
  186.   int vpos;
  187.   if (v == cached_v && cached_v != 0)
  188.     vpos = cached_vpos;
  189.   else {
  190.     if (v % font::vert != 0)
  191.       fatal("vertical position not a multiple of vertical resolution");
  192.     vpos = v / font::vert;
  193.     if (vpos > nlines) {
  194.       glyph **old_lines = lines;
  195.       lines = new glyph *[vpos + 1];
  196.       memcpy(lines, old_lines, nlines*sizeof(glyph *));
  197.       for (int i = nlines; i <= vpos; i++)
  198.     lines[i] = 0;
  199.       a_delete old_lines;
  200.       nlines = vpos + 1;
  201.     }
  202.     // Note that the first output line corresponds to groff
  203.     // position font::vert.
  204.     if (vpos <= 0) {
  205.       error("character above first line discarded");
  206.       return;
  207.     }
  208.     cached_v = v;
  209.     cached_vpos = vpos;
  210.   }
  211.   glyph *g = new glyph;
  212.   g->hpos = hpos;
  213.   g->code = c;
  214.   g->mode = mode;
  215.  
  216.   // The list will be reversed later.  After reversal, it must be in
  217.   // increasing order of hpos, with HDRAW characters before VDRAW
  218.   // characters before normal characters at each hpos, and otherwise
  219.   // in order of occurrence.
  220.  
  221.   for (glyph **pp = lines + (vpos - 1); *pp; pp = &(*pp)->next)
  222.     if (int((*pp)->hpos) < hpos
  223.     || ((*pp)->hpos == hpos && (*pp)->draw_mode() >= g->draw_mode()))
  224.       break;
  225.  
  226.   g->next = *pp;
  227.   *pp = g;
  228. }
  229.  
  230. void tty_printer::draw(int code, int *p, int np, const environment *env)
  231. {
  232.   if (code != 'l' || !draw_flag)
  233.     return;
  234.   if (np != 2) {
  235.     error("2 arguments required for line");
  236.     return;
  237.   }
  238.   if (p[0] == 0) {
  239.     // vertical line
  240.     int v = env->vpos;
  241.     int len = p[1];
  242.     if (len < 0) {
  243.       v += len;
  244.       len = -len;
  245.     }
  246.     while (len >= 0) {
  247.       add_char('|', env->hpos, v, VDRAW_MODE);
  248.       len -= font::vert;
  249.       v += font::vert;
  250.     }
  251.   }
  252.   if (p[1] == 0) {
  253.     // horizontal line
  254.     int h = env->hpos;
  255.     int len = p[0];
  256.     if (len < 0) {
  257.       h += len;
  258.       len = -len;
  259.     }
  260.     while (len >= 0) {
  261.       add_char('-', h, env->vpos, HDRAW_MODE);
  262.       len -= font::hor;
  263.       h += font::hor;
  264.     }
  265.   }
  266. }
  267.  
  268. void tty_printer::end_page(int page_length)
  269. {
  270.   if (page_length % font::vert != 0)
  271.     error("vertical position at end of page not multiple of vertical resolution");
  272.   int lines_per_page = page_length / font::vert;
  273.   for (int last_line = nlines; last_line > 0; last_line--)
  274.     if (lines[last_line - 1])
  275.       break;
  276. #if 0
  277.   if (last_line > lines_per_page) {
  278.     error("characters past last line discarded");
  279.     do {
  280.       --last_line;
  281.       while (lines[last_line]) {
  282.     glyph *tem = lines[last_line];
  283.     lines[last_line] = tem->next;
  284.     delete tem;
  285.       }
  286.     } while (last_line > lines_per_page);
  287.   }
  288. #endif
  289.   for (int i = 0; i < last_line; i++) {
  290.     glyph *p = lines[i];
  291.     lines[i] = 0;
  292.     glyph *g = 0;
  293.     while (p) {
  294.       glyph *tem = p->next;
  295.       p->next = g;
  296.       g = p;
  297.       p = tem;
  298.     }
  299.     int hpos = 0;
  300.     
  301.     glyph *nextp;
  302.     for (p = g; p; delete p, p = nextp) {
  303.       nextp = p->next;
  304.       if (nextp && p->hpos == nextp->hpos) {
  305.     if (p->draw_mode() == HDRAW_MODE && nextp->draw_mode() == VDRAW_MODE) {
  306.       nextp->code = '+';
  307.       continue;
  308.     }
  309.     if (p->draw_mode() != 0 && p->draw_mode() == nextp->draw_mode()) {
  310.       nextp->code = p->code;
  311.       continue;
  312.     }
  313.     if (!overstrike_flag)
  314.       continue;
  315.       }
  316.       if (hpos > int(p->hpos)) {
  317.     putchar('\b');
  318.     hpos--;
  319.       }
  320.       else {
  321.     if (horizontal_tab_flag) {
  322.       for (;;) {
  323.         int next_tab_pos = ((hpos + TAB_WIDTH) / TAB_WIDTH) * TAB_WIDTH;
  324.         if (next_tab_pos > int(p->hpos))
  325.           break;
  326.         putchar('\t');
  327.         hpos = next_tab_pos;
  328.       }
  329.     }
  330.     for (; hpos < int(p->hpos); hpos++)
  331.       putchar(' ');
  332.       }
  333.       assert(hpos == p->hpos);
  334.       if (p->mode & UNDERLINE_MODE) {
  335.     putchar('_');
  336.     putchar('\b');
  337.       }
  338.       if (p->mode & BOLD_MODE) {
  339.     putchar(p->code);
  340.     putchar('\b');
  341.       }
  342.       putchar(p->code);
  343.       hpos++;
  344.     }
  345.     putchar('\n');
  346.   }
  347.   if (form_feed_flag) {
  348.     if (last_line < lines_per_page)
  349.       putchar('\f');
  350.   }
  351.   else {
  352.     for (; last_line < lines_per_page; last_line++)
  353.       putchar('\n');
  354.   }
  355. }
  356.  
  357. font *tty_printer::make_font(const char *nm)
  358. {
  359.   return tty_font::load_tty_font(nm);
  360. }
  361.  
  362. printer *make_printer()
  363. {
  364.   return new tty_printer;
  365. }
  366.  
  367. static void usage();
  368.  
  369. int main(int argc, char **argv)
  370. {
  371.   program_name = argv[0];
  372.   static char stderr_buf[BUFSIZ];
  373.   setbuf(stderr, stderr_buf);
  374.   int c;
  375.   while ((c = getopt(argc, argv, "F:vhfbuoBUd")) != EOF)
  376.     switch(c) {
  377.     case 'v':
  378.       {
  379.     extern const char *version_string;
  380.     fprintf(stderr, "grotty version %s\n", version_string);
  381.     fflush(stderr);
  382.     break;
  383.       }
  384.     case 'b':
  385.       // Do not embolden by overstriking.
  386.       bold_flag = 0;
  387.       break;
  388.     case 'u':
  389.       // Do not underline.
  390.       underline_flag = 0;
  391.       break;
  392.     case 'o':
  393.       // Do not overstrike (other than emboldening and underlining).
  394.       overstrike_flag = 0;
  395.       break;
  396.     case 'B':
  397.       // Do bold-underlining as bold.
  398.       bold_underline_mode = BOLD_MODE;
  399.       break;
  400.     case 'U':
  401.       // Do bold-underlining as underlining.
  402.       bold_underline_mode = UNDERLINE_MODE;
  403.       break;
  404.     case 'h':
  405.       // Use horizontal tabs.
  406.       horizontal_tab_flag = 1;
  407.       break;
  408.     case 'f':
  409.       form_feed_flag = 1;
  410.       break;
  411.     case 'F':
  412.       font::command_line_font_dir(optarg);
  413.       break;
  414.     case 'd':
  415.       // Ignore \D commands.
  416.       draw_flag = 0;
  417.       break;
  418.     case '?':
  419.       usage();
  420.       break;
  421.     default:
  422.       assert(0);
  423.     }
  424.   if (optind >= argc)
  425.     do_file("-");
  426.   else {
  427.     for (int i = optind; i < argc; i++)
  428.       do_file(argv[i]);
  429.   }
  430.   delete pr;
  431.   exit(0);
  432. }
  433.  
  434. static void usage()
  435. {
  436.   fprintf(stderr, "usage: %s [-hfvbuodBU] [-F dir] [files ...]\n",
  437.       program_name);
  438.   exit(1);
  439. }
  440.