home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / groff / libdriver / printer.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-30  |  3.9 KB  |  174 lines

  1. // -*- C++ -*-
  2. /* Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
  3.      Written by James Clark (jjc@jclark.uucp)
  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 1, 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 LICENSE.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include "driver.h"
  22.  
  23. printer *pr = 0;
  24.  
  25. font_pointer_list::font_pointer_list(font *f, font_pointer_list *fp)
  26. : p(f), next(fp)
  27. {
  28. }
  29.  
  30. printer::printer()
  31. : font_table(0), nfonts(0), font_list(0)
  32. {
  33. }
  34.  
  35. printer::~printer()
  36. {
  37.   delete font_table;
  38.   while (font_list) {
  39.     font_pointer_list *tem = font_list;
  40.     font_list = font_list->next;
  41.     delete tem->p;
  42.     delete tem;
  43.   }
  44.   if (ferror(stdout) || fflush(stdout) < 0)
  45.     fatal("output error");
  46. }
  47.  
  48. void printer::load_font(int n, const char *nm)
  49. {
  50.   assert(n >= 0);
  51.   if (n >= nfonts) {
  52.     if (nfonts == 0) {
  53.       nfonts = 10;
  54.       if (nfonts <= n)
  55.     nfonts = n + 1;
  56.       font_table = new font *[nfonts];
  57.       for (int i = 0; i < nfonts; i++)
  58.     font_table[i] = 0;
  59.     }
  60.     else {
  61.       font **old_font_table = font_table;
  62.       int old_nfonts = nfonts;
  63.       nfonts *= 2;
  64.       if (n >= nfonts)
  65.     nfonts = n + 1;
  66.       font_table = new font *[nfonts];
  67.       for (int i = 0; i < old_nfonts; i++)
  68.     font_table[i] = old_font_table[i];
  69.       for (i = old_nfonts; i < nfonts; i++)
  70.     font_table[i] = 0;
  71.     }
  72.   }
  73.   font *f = find_font(nm);
  74.   font_table[n] = f;
  75. }
  76.  
  77. font *printer::find_font(const char *nm)
  78. {
  79.   for (font_pointer_list *p = font_list; p; p = p->next)
  80.     if (strcmp(p->p->get_name(), nm) == 0)
  81.       return p->p;
  82.   font *f = make_font(nm);
  83.   if (!f)
  84.     fatal("sorry, I can't continue");
  85.   font_list = new font_pointer_list(f, font_list);
  86.   return f;
  87. }
  88.  
  89. font *printer::make_font(const char *nm)
  90. {
  91.   return font::load_font(nm);
  92. }
  93.  
  94. void printer::end_of_line()
  95. {
  96. }
  97.  
  98. void printer::special(char *, const environment *)
  99. {
  100. }
  101.  
  102. void printer::draw(int, int *, int, const environment *)
  103. {
  104. }
  105.  
  106. void printer::set_ascii_char(unsigned char c, const environment *env, 
  107.                  int *widthp)
  108. {
  109.   char buf[2];
  110.   buf[0] = c;
  111.   buf[1] = '\0';
  112.   set_special_char(buf, env, widthp);
  113. }
  114.  
  115. void printer::set_special_char(const char *nm, const environment *env,
  116.                   int *widthp)
  117. {
  118.   int i = font::name_to_index(nm);
  119.   int fn = env->fontno;
  120.   if (fn < 0 || fn >= nfonts) {
  121.     error("bad font position `%1'", fn);
  122.     return;
  123.   }
  124.   font *f = font_table[fn];
  125.   if (f == 0) {
  126.     error("no font mounted at `%1'", fn);
  127.     return;
  128.   }
  129.   if (!f->contains(i)) {
  130.     if (nm[0] != '\0' && nm[1] == '\0')
  131.       error("font `%1' does not contain ascii character `%2'",
  132.         f->get_name(),
  133.         nm[0]);
  134.     else
  135.       error("font `%1' does not contain special character `%2'",
  136.         f->get_name(),
  137.         nm);
  138.     return;
  139.   }
  140.   int w = f->get_width(i, env->size);
  141.   if (widthp)
  142.     *widthp = w;
  143.   set_char(i, f, env, w);
  144. }
  145.  
  146. void printer::set_numbered_char(int num, const environment *env, int *widthp)
  147. {
  148.   if (num < 0 || num >= 256) {
  149.     error("argument to N command not between 0 and 255");
  150.     return;
  151.   }
  152.   int i = font::number_to_index((unsigned char)num);
  153.   int fn = env->fontno;
  154.   if (fn < 0 || fn >= nfonts) {
  155.     error("bad font position `%1'", fn);
  156.     return;
  157.   }
  158.   font *f = font_table[fn];
  159.   if (f == 0) {
  160.     error("no font mounted at `%1'", fn);
  161.     return;
  162.   }
  163.   if (!f->contains(i)) {
  164.     error("font `%1' does not contain numbered character %2",
  165.       f->get_name(),
  166.       num);
  167.     return;
  168.   }
  169.   int w = f->get_width(i, env->size);
  170.   if (widthp)
  171.     *widthp = w;
  172.   set_char(i, f, env, w);
  173. }
  174.