home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / groff-1.09-src.lha / src / amiga / groff-1.09 / grops / ps.cc < prev    next >
C/C++ Source or Header  |  1994-02-13  |  36KB  |  1,517 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. #include "stringclass.h"
  23. #include "cset.h"
  24.  
  25. #include "ps.h"
  26. #include <time.h>
  27.  
  28. static int landscape_flag = 0;
  29. static int ncopies = 1;
  30. static int linewidth = -1;
  31. // Non-zero means generate PostScript code that guesses the paper
  32. // length using the imageable area.
  33. static int guess_flag = 0;
  34.  
  35. // Non-zero if -b was specified on the command line.
  36. static int bflag = 0;
  37. unsigned broken_flags = 0;
  38.  
  39. #define DEFAULT_LINEWIDTH 40    /* in ems/1000 */
  40. #define MAX_LINE_LENGTH 72
  41. #define FILL_MAX 1000
  42.  
  43. const char *const dict_name = "grops";
  44. const char *const defs_dict_name = "DEFS";
  45. const int DEFS_DICT_SPARE = 50;
  46.  
  47. double degrees(double r)
  48. {
  49.   return r*180.0/PI;
  50. }
  51.  
  52. double radians(double d)
  53. {
  54.   return d*PI/180.0;
  55. }
  56.  
  57. inline double transform_fill(int fill)
  58. {
  59.   return 1 - fill/double(FILL_MAX);
  60. }
  61.  
  62. // This is used for testing whether a character should be output in the
  63. // PostScript file using \nnn, so we really want the character to be
  64. // less than 0200.
  65.  
  66. inline int is_ascii(char c)
  67. {
  68.   return (unsigned char)c < 0200;
  69. }
  70.  
  71. ps_output::ps_output(FILE *f, int n)
  72. : fp(f), max_line_length(n), col(0), need_space(0), fixed_point(0)
  73. {
  74. }
  75.  
  76. ps_output &ps_output::set_file(FILE *f)
  77. {
  78.   fp = f;
  79.   col = 0;
  80.   return *this;
  81. }
  82.  
  83. ps_output &ps_output::copy_file(FILE *infp)
  84. {
  85.   int c;
  86.   while ((c = getc(infp)) != EOF)
  87.     putc(c, fp);
  88.   return *this;
  89. }
  90.  
  91. ps_output &ps_output::end_line()
  92. {
  93.   if (col != 0) {
  94.     putc('\n', fp);
  95.     col = 0;
  96.     need_space = 0;
  97.   }
  98.   return *this;
  99. }
  100.  
  101. ps_output &ps_output::special(const char *s)
  102. {
  103.   if (s == 0 || *s == '\0')
  104.     return *this;
  105.   if (col != 0) {
  106.     putc('\n', fp);
  107.     col = 0;
  108.   }
  109.   fputs(s, fp);
  110.   if (strchr(s, '\0')[-1] != '\n')
  111.     putc('\n', fp);
  112.   need_space = 0;
  113.   return *this;
  114. }
  115.  
  116. ps_output &ps_output::simple_comment(const char *s)
  117. {
  118.   if (col != 0)
  119.     putc('\n', fp);
  120.   putc('%', fp);
  121.   putc('%', fp);
  122.   fputs(s, fp);
  123.   putc('\n', fp);
  124.   col = 0;
  125.   need_space = 0;
  126.   return *this;
  127. }
  128.  
  129. ps_output &ps_output::begin_comment(const char *s)
  130. {
  131.   if (col != 0)
  132.     putc('\n', fp);
  133.   putc('%', fp);
  134.   putc('%', fp);
  135.   fputs(s, fp);
  136.   col = 2 + strlen(s);
  137.   return *this;
  138. }
  139.  
  140. ps_output &ps_output::end_comment()
  141. {
  142.   if (col != 0) {
  143.     putc('\n', fp);
  144.     col = 0;
  145.   }
  146.   need_space = 0;
  147.   return *this;
  148. }
  149.  
  150. ps_output &ps_output::comment_arg(const char *s)
  151. {
  152.   int len = strlen(s);
  153.   if (col + len + 1 > max_line_length) {
  154.     putc('\n', fp);
  155.     fputs("%%+", fp);
  156.     col = 3;
  157.   }
  158.   putc(' ',  fp);
  159.   fputs(s, fp);
  160.   col += len + 1;
  161.   return *this;
  162. }
  163.  
  164. ps_output &ps_output::set_fixed_point(int n)
  165. {
  166.   assert(n >= 0 && n <= 10);
  167.   fixed_point = n;
  168.   return *this;
  169. }
  170.  
  171. ps_output &ps_output::put_delimiter(char c)
  172. {
  173.   if (col + 1 > max_line_length) {
  174.     putc('\n', fp);
  175.     col = 0;
  176.   }
  177.   putc(c, fp);
  178.   col++;
  179.   need_space = 0;
  180.   return *this;
  181. }
  182.  
  183. ps_output &ps_output::put_string(const char *s, int n)
  184. {
  185.   int len = 0;
  186.   for (int i = 0; i < n; i++) {
  187.     char c = s[i];
  188.     if (is_ascii(c) && csprint(c)) {
  189.       if (c == '(' || c == ')' || c == '\\')
  190.     len += 2;
  191.       else
  192.     len += 1;
  193.     }
  194.     else
  195.       len += 4;
  196.   }
  197.   if (len > n*2) {
  198.     if (col + n*2 + 2 > max_line_length && n*2 + 2 <= max_line_length) {
  199.       putc('\n', fp);
  200.       col = 0;
  201.     }
  202.     if (col + 1 > max_line_length) {
  203.       putc('\n', fp);
  204.       col = 0;
  205.     }
  206.     putc('<', fp);
  207.     col++;
  208.     for (i = 0; i < n; i++) {
  209.       if (col + 2 > max_line_length) {
  210.     putc('\n', fp);
  211.     col = 0;
  212.       }
  213.       fprintf(fp, "%02x", s[i] & 0377);
  214.       col += 2;
  215.     }
  216.     putc('>', fp);
  217.     col++;
  218.   }
  219.   else {
  220.     if (col + len + 2 > max_line_length && len + 2 <= max_line_length) {
  221.       putc('\n', fp);
  222.       col = 0;
  223.     }
  224.     if (col + 2 > max_line_length) {
  225.       putc('\n', fp);
  226.       col = 0;
  227.     }
  228.     putc('(', fp);
  229.     col++;
  230.     for (i = 0; i < n; i++) {
  231.       char c = s[i];
  232.       if (is_ascii(c) && csprint(c)) {
  233.     if (c == '(' || c == ')' || c == '\\')
  234.       len = 2;
  235.     else
  236.       len = 1;
  237.       }
  238.       else
  239.     len = 4;
  240.       if (col + len + 1 > max_line_length) {
  241.     putc('\\', fp);
  242.     putc('\n', fp);
  243.     col = 0;
  244.       }
  245.       switch (len) {
  246.       case 1:
  247.     putc(c, fp);
  248.     break;
  249.       case 2:
  250.     putc('\\', fp);
  251.     putc(c, fp);
  252.     break;
  253.       case 4:
  254.     fprintf(fp, "\\%03o", c & 0377);
  255.     break;
  256.       default:
  257.     assert(0);
  258.       }
  259.       col += len;
  260.     }
  261.     putc(')', fp);
  262.     col++;
  263.   }
  264.   need_space = 0;
  265.   return *this;
  266. }
  267.  
  268. ps_output &ps_output::put_number(int n)
  269. {
  270.   char buf[1 + INT_DIGITS + 1];
  271.   sprintf(buf, "%d", n);
  272.   int len = strlen(buf);
  273.   if (col > 0 && col + len + need_space > max_line_length) {
  274.     putc('\n', fp);
  275.     col = 0;
  276.     need_space = 0;
  277.   }
  278.   if (need_space) {
  279.     putc(' ', fp);
  280.     col++;
  281.   }
  282.   fputs(buf, fp);
  283.   col += len;
  284.   need_space = 1;
  285.   return *this;
  286. }
  287.  
  288. ps_output &ps_output::put_fix_number(int i)
  289. {
  290.   const char *p = iftoa(i, fixed_point);
  291.   int len = strlen(p);
  292.   if (col > 0 && col + len + need_space > max_line_length) {
  293.     putc('\n', fp);
  294.     col = 0;
  295.     need_space = 0;
  296.   }
  297.   if (need_space) {
  298.     putc(' ', fp);
  299.     col++;
  300.   }
  301.   fputs(p, fp);
  302.   col += len;
  303.   need_space = 1;
  304.   return *this;
  305. }
  306.  
  307. ps_output &ps_output::put_float(double d)
  308. {
  309.   char buf[128];
  310.   sprintf(buf, "%.4f", d);
  311.   int len = strlen(buf);
  312.   if (col > 0 && col + len + need_space > max_line_length) {
  313.     putc('\n', fp);
  314.     col = 0;
  315.     need_space = 0;
  316.   }
  317.   if (need_space) {
  318.     putc(' ', fp);
  319.     col++;
  320.   }
  321.   fputs(buf, fp);
  322.   col += len;
  323.   need_space = 1;
  324.   return *this;
  325. }
  326.  
  327. ps_output &ps_output::put_symbol(const char *s)
  328. {
  329.   int len = strlen(s);
  330.   if (col > 0 && col + len + need_space > max_line_length) {
  331.     putc('\n', fp);
  332.     col = 0;
  333.     need_space = 0;
  334.   }
  335.   if (need_space) {
  336.     putc(' ', fp);
  337.     col++;
  338.   }
  339.   fputs(s, fp);
  340.   col += len;
  341.   need_space = 1;
  342.   return *this;
  343. }
  344.  
  345. ps_output &ps_output::put_literal_symbol(const char *s)
  346. {
  347.   int len = strlen(s);
  348.   if (col > 0 && col + len + 1 > max_line_length) {
  349.     putc('\n', fp);
  350.     col = 0;
  351.   }
  352.   putc('/', fp);
  353.   fputs(s, fp);
  354.   col += len + 1;
  355.   need_space = 1;
  356.   return *this;
  357. }
  358.  
  359. class ps_font : public font {
  360.   ps_font(const char *);
  361. public:
  362.   int encoding_index;
  363.   char *encoding;
  364.   char *reencoded_name;
  365.   ~ps_font();
  366.   void handle_unknown_font_command(const char *command, const char *arg,
  367.                    const char *filename, int lineno);
  368.   static ps_font *load_ps_font(const char *);
  369. };
  370.  
  371. ps_font *ps_font::load_ps_font(const char *s)
  372. {
  373.   ps_font *f = new ps_font(s);
  374.   if (!f->load()) {
  375.     delete f;
  376.     return 0;
  377.   }
  378.   return f;
  379. }
  380.  
  381. ps_font::ps_font(const char *nm)
  382. : font(nm), encoding(0), reencoded_name(0), encoding_index(-1)
  383. {
  384. }
  385.  
  386. ps_font::~ps_font()
  387. {
  388.   a_delete encoding;
  389.   a_delete reencoded_name;
  390. }
  391.  
  392. void ps_font::handle_unknown_font_command(const char *command, const char *arg,
  393.                       const char *filename, int lineno)
  394. {
  395.   if (strcmp(command, "encoding") == 0) {
  396.     if (arg == 0)
  397.       error_with_file_and_line(filename, lineno,
  398.                    "`encoding' command requires an argument");
  399.     else
  400.       encoding = strsave(arg);
  401.   }
  402. }
  403.  
  404. static void handle_unknown_desc_command(const char *command, const char *arg,
  405.                     const char *filename, int lineno)
  406. {
  407.   if (strcmp(command, "broken") == 0) {
  408.     if (arg == 0)
  409.       error_with_file_and_line(filename, lineno,
  410.                    "`broken' command requires an argument");
  411.     else if (!bflag)
  412.       broken_flags = atoi(arg);
  413.   }
  414. }
  415.  
  416. struct style {
  417.   font *f;
  418.   int point_size;
  419.   int height;
  420.   int slant;
  421.   style();
  422.   style(font *, int, int, int);
  423.   int operator==(const style &) const;
  424.   int operator!=(const style &) const;
  425. };
  426.  
  427. style::style() : f(0)
  428. {
  429. }
  430.  
  431. style::style(font *p, int sz, int h, int sl)
  432. : f(p), point_size(sz), height(h), slant(sl)
  433. {
  434. }
  435.  
  436. int style::operator==(const style &s) const
  437. {
  438.   return (f == s.f && point_size == s.point_size
  439.       && height == s.height && slant == s.slant);
  440. }
  441.  
  442. int style::operator!=(const style &s) const
  443. {
  444.   return !(*this == s);
  445. }
  446.  
  447. class ps_printer : public printer {
  448.   FILE *tempfp;
  449.   ps_output out;
  450.   int res;
  451.   int space_char_index;
  452.   int pages_output;
  453.   int paper_length;
  454.   int equalise_spaces;
  455.   enum { SBUF_SIZE = 256 };
  456.   char sbuf[SBUF_SIZE];
  457.   int sbuf_len;
  458.   int sbuf_start_hpos;
  459.   int sbuf_vpos;
  460.   int sbuf_end_hpos;
  461.   int sbuf_space_width;
  462.   int sbuf_space_count;
  463.   int sbuf_space_diff_count;
  464.   int sbuf_space_code;
  465.   int sbuf_kern;
  466.   style sbuf_style;
  467.   style output_style;
  468.   int output_hpos;
  469.   int output_vpos;
  470.   int output_draw_point_size;
  471.   int line_thickness;
  472.   int output_line_thickness;
  473.   int fill;
  474.   unsigned char output_space_code;
  475.   enum { MAX_DEFINED_STYLES = 50 };
  476.   style defined_styles[MAX_DEFINED_STYLES];
  477.   int ndefined_styles;
  478.   int next_encoding_index;
  479.   string defs;
  480.   int ndefs;
  481.   resource_manager rm;
  482.   int invis_count;
  483.  
  484.   void flush_sbuf();
  485.   void set_style(const style &);
  486.   void set_space_code(unsigned char c);
  487.   int set_encoding_index(ps_font *);
  488.   void do_exec(char *, const environment *);
  489.   void do_import(char *, const environment *);
  490.   void do_def(char *, const environment *);
  491.   void do_mdef(char *, const environment *);
  492.   void do_file(char *, const environment *);
  493.   void do_invis(char *, const environment *);
  494.   void do_endinvis(char *, const environment *);
  495.   void set_line_thickness(const environment *);
  496.   void fill_path();
  497.   void encode_fonts();
  498.   void define_encoding(const char *, int);
  499.   void reencode_font(ps_font *);
  500. public:
  501.   ps_printer();
  502.   ~ps_printer();
  503.   void set_char(int i, font *f, const environment *env, int w);
  504.   void draw(int code, int *p, int np, const environment *env);
  505.   void begin_page(int);
  506.   void end_page(int);
  507.   void special(char *arg, const environment *env);
  508.   font *make_font(const char *);
  509.   void end_of_line();
  510. };
  511.  
  512. ps_printer::ps_printer()
  513. : pages_output(0),
  514.   sbuf_len(0),
  515.   output_hpos(-1),
  516.   output_vpos(-1),
  517.   out(0, MAX_LINE_LENGTH),
  518.   ndefined_styles(0),
  519.   next_encoding_index(0),
  520.   line_thickness(-1),
  521.   fill(FILL_MAX + 1),
  522.   ndefs(0),
  523.   invis_count(0)
  524. {
  525.   tempfp = xtmpfile();
  526.   out.set_file(tempfp);
  527.   if (linewidth < 0)
  528.     linewidth = DEFAULT_LINEWIDTH;
  529.   if (font::hor != 1)
  530.     fatal("horizontal resolution must be 1");
  531.   if (font::vert != 1)
  532.     fatal("vertical resolution must be 1");
  533.   if (font::res % (font::sizescale*72) != 0)
  534.     fatal("res must be a multiple of 72*sizescale");
  535.   int r = font::res;
  536.   int point = 0;
  537.   while (r % 10 == 0) {
  538.     r /= 10;
  539.     point++;
  540.   }
  541.   res = r;
  542.   out.set_fixed_point(point);
  543.   space_char_index = font::name_to_index("space");
  544.   paper_length = font::paperlength;
  545.   if (paper_length == 0)
  546.     paper_length = 11*font::res;
  547.   equalise_spaces = font::res >= 72000;
  548. }
  549.  
  550. int ps_printer::set_encoding_index(ps_font *f)
  551. {
  552.   if (f->encoding_index >= 0)
  553.     return f->encoding_index;
  554.   for (font_pointer_list *p = font_list; p; p = p->next)
  555.     if (p->p != f) {
  556.       char *encoding = ((ps_font *)p->p)->encoding;
  557.       int encoding_index = ((ps_font *)p->p)->encoding_index;
  558.       if (encoding != 0 && encoding_index >= 0 
  559.       && strcmp(f->encoding, encoding) == 0) {
  560.     return f->encoding_index = encoding_index;
  561.       }
  562.     }
  563.   return f->encoding_index = next_encoding_index++;
  564. }
  565.  
  566. void ps_printer::set_char(int i, font *f, const environment *env, int w)
  567. {
  568.   if (i == space_char_index || invis_count > 0)
  569.     return;
  570.   unsigned char code = f->get_code(i);
  571.   style sty(f, env->size, env->height, env->slant);
  572.   if (sty.slant != 0) {
  573.     if (sty.slant > 80 || sty.slant < -80) {
  574.       error("silly slant `%1' degrees", sty.slant);
  575.       sty.slant = 0;
  576.     }
  577.   }
  578.   if (sbuf_len > 0) {
  579.     if (sbuf_len < SBUF_SIZE
  580.     && sty == sbuf_style
  581.     && sbuf_vpos == env->vpos) {
  582.       if (sbuf_end_hpos == env->hpos) {
  583.     sbuf[sbuf_len++] = code;
  584.     sbuf_end_hpos += w + sbuf_kern;
  585.     return;
  586.       }
  587.       if (sbuf_len == 1 && sbuf_kern == 0) {
  588.     sbuf_kern = env->hpos - sbuf_end_hpos;
  589.     sbuf_end_hpos = env->hpos + sbuf_kern + w;
  590.     sbuf[sbuf_len++] = code;
  591.     return;
  592.       }
  593.       /* If sbuf_end_hpos - sbuf_kern == env->hpos, we are better off
  594.      starting a new string. */
  595.       if (sbuf_len < SBUF_SIZE - 1 && env->hpos >= sbuf_end_hpos
  596.       && (sbuf_kern == 0 || sbuf_end_hpos - sbuf_kern != env->hpos)) {
  597.     if (sbuf_space_code < 0) {
  598.       if (f->contains(space_char_index)) {
  599.         sbuf_space_code = f->get_code(space_char_index);
  600.         sbuf_space_width = env->hpos - sbuf_end_hpos;
  601.         sbuf_end_hpos = env->hpos + w + sbuf_kern;
  602.         sbuf[sbuf_len++] = sbuf_space_code;
  603.         sbuf[sbuf_len++] = code;
  604.         sbuf_space_count++;
  605.         return;
  606.       }
  607.     }
  608.     else {
  609.       int diff = env->hpos - sbuf_end_hpos - sbuf_space_width;
  610.       if (diff == 0 || (equalise_spaces && (diff == 1 || diff == -1))) {
  611.         sbuf_end_hpos = env->hpos + w + sbuf_kern;
  612.         sbuf[sbuf_len++] = sbuf_space_code;
  613.         sbuf[sbuf_len++] = code;
  614.         sbuf_space_count++;
  615.         if (diff == 1)
  616.           sbuf_space_diff_count++;
  617.         else if (diff == -1)
  618.           sbuf_space_diff_count--;
  619.         return;
  620.       }
  621.     }
  622.       }
  623.     }
  624.     flush_sbuf();
  625.   }
  626.   sbuf_len = 1;
  627.   sbuf[0] = code;
  628.   sbuf_end_hpos = env->hpos + w;
  629.   sbuf_start_hpos = env->hpos;
  630.   sbuf_vpos = env->vpos;
  631.   sbuf_style = sty;
  632.   sbuf_space_code = -1;
  633.   sbuf_space_width = 0;
  634.   sbuf_space_count = sbuf_space_diff_count = 0;
  635.   sbuf_kern = 0;
  636. }
  637.  
  638. static char *make_encoding_name(int encoding_index)
  639. {
  640.   static char buf[3 + INT_DIGITS + 1];
  641.   sprintf(buf, "ENC%d", encoding_index);
  642.   return buf;
  643. }
  644.  
  645. const char *const WS = " \t\n\r";
  646.  
  647. void ps_printer::define_encoding(const char *encoding, int encoding_index)
  648. {
  649.   char *vec[256];
  650.   for (int i = 0; i < 256; i++)
  651.     vec[i] = 0;
  652.   char *path;
  653.   FILE *fp = font::open_file(encoding, &path);
  654.   if (fp == 0)
  655.     fatal("can't open encoding file `%1'", encoding);
  656.   int lineno = 1;
  657.   char buf[256];
  658.   while (fgets(buf, 512, fp) != 0) {
  659.     char *p = buf;
  660.     while (csspace(*p))
  661.       p++;
  662.     if (*p != '#' && *p != '\0' && (p = strtok(buf, WS)) != 0) {
  663.       char *q = strtok(0, WS);
  664.       int n;
  665.       if (q == 0 || sscanf(q, "%d", &n) != 1 || n < 0 || n >= 256)
  666.     fatal_with_file_and_line(path, lineno, "bad second field");
  667.       vec[n] = new char[strlen(p) + 1];
  668.       strcpy(vec[n], p);
  669.     }
  670.     lineno++;
  671.   }
  672.   a_delete path;
  673.   out.put_literal_symbol(make_encoding_name(encoding_index));
  674.   out.put_delimiter('[');
  675.   for (i = 0; i < 256; i++) {
  676.     if (vec[i] == 0)
  677.       out.put_literal_symbol(".notdef");
  678.     else {
  679.       out.put_literal_symbol(vec[i]);
  680.       a_delete vec[i];
  681.     }
  682.   }
  683.   out.put_delimiter(']').put_symbol("def");
  684. }
  685.  
  686. void ps_printer::reencode_font(ps_font *f)
  687. {
  688.   out.put_literal_symbol(f->reencoded_name)
  689.      .put_symbol(make_encoding_name(f->encoding_index))
  690.      .put_literal_symbol(f->get_internal_name())
  691.      .put_symbol("RE");
  692. }
  693.  
  694. void ps_printer::encode_fonts()
  695. {
  696.   if (next_encoding_index == 0)
  697.     return;
  698.   char *done_encoding = new char[next_encoding_index];
  699.   for (int i = 0; i < next_encoding_index; i++)
  700.     done_encoding[i] = 0;
  701.   for (font_pointer_list *f = font_list; f; f = f->next) {
  702.     int encoding_index = ((ps_font *)f->p)->encoding_index;
  703.     if (encoding_index >= 0) {
  704.       assert(encoding_index < next_encoding_index);
  705.       if (!done_encoding[encoding_index]) {
  706.     done_encoding[encoding_index] = 1;
  707.     define_encoding(((ps_font *)f->p)->encoding, encoding_index);
  708.       }
  709.       reencode_font((ps_font *)f->p);
  710.     }
  711.   }
  712.   a_delete done_encoding;
  713. }
  714.  
  715. void ps_printer::set_style(const style &sty)
  716. {
  717.   char buf[1 + INT_DIGITS + 1];
  718.   for (int i = 0; i < ndefined_styles; i++)
  719.     if (sty == defined_styles[i]) {
  720.       sprintf(buf, "F%d", i);
  721.       out.put_symbol(buf);
  722.       return;
  723.     }
  724.   if (ndefined_styles >= MAX_DEFINED_STYLES)
  725.     ndefined_styles = 0;
  726.   sprintf(buf, "F%d", ndefined_styles);
  727.   out.put_literal_symbol(buf);
  728.   const char *psname = sty.f->get_internal_name();
  729.   if (psname == 0)
  730.     fatal("no internalname specified for font `%1'", sty.f->get_name());
  731.   char *encoding = ((ps_font *)sty.f)->encoding;
  732.   if (encoding != 0) {
  733.     char *s = ((ps_font *)sty.f)->reencoded_name;
  734.     if (s == 0) {
  735.       int ei = set_encoding_index((ps_font *)sty.f);
  736.       char *tem = new char[strlen(psname) + 1 + INT_DIGITS + 1];
  737.       sprintf(tem, "%s@%d", psname, ei);
  738.       psname = tem;
  739.       ((ps_font *)sty.f)->reencoded_name = tem;
  740.     }
  741.     else
  742.       psname = s;
  743.   }
  744.   out.put_fix_number((font::res/(72*font::sizescale))*sty.point_size);
  745.   if (sty.height != 0 || sty.slant != 0) {
  746.     int h = sty.height == 0 ? sty.point_size : sty.height;
  747.     h *= font::res/(72*font::sizescale);
  748.     int c = int(h*tan(radians(sty.slant)) + .5);
  749.     out.put_fix_number(c).put_fix_number(h).put_literal_symbol(psname)
  750.        .put_symbol("MF");
  751.   }
  752.   else {
  753.     out.put_literal_symbol(psname).put_symbol("SF");
  754.   }
  755.   defined_styles[ndefined_styles++] = sty;
  756. }
  757.  
  758. void ps_printer::set_space_code(unsigned char c)
  759. {
  760.   out.put_literal_symbol("SC").put_number(c).put_symbol("def");
  761. }
  762.  
  763. void ps_printer::end_of_line()
  764. {
  765.   flush_sbuf();
  766.   // this ensures that we do an absolute motion to the beginning of a line
  767.   output_vpos = output_hpos = -1;
  768. }
  769.  
  770. void ps_printer::flush_sbuf()
  771. {
  772.   enum {
  773.     NONE,
  774.     RELATIVE_H,
  775.     RELATIVE_V,
  776.     RELATIVE_HV,
  777.     ABSOLUTE
  778.     } motion = NONE;
  779.   int space_flag = 0;
  780.   if (sbuf_len == 0)
  781.     return;
  782.   if (output_style != sbuf_style) {
  783.     set_style(sbuf_style);
  784.     output_style = sbuf_style;
  785.   }
  786.   int extra_space = 0;
  787.   if (output_hpos < 0 || output_vpos < 0)
  788.     motion = ABSOLUTE;
  789.   else {
  790.     if (output_hpos != sbuf_start_hpos)
  791.       motion = RELATIVE_H;
  792.     if (output_vpos != sbuf_vpos) {
  793.       if  (motion != NONE)
  794.     motion = RELATIVE_HV;
  795.       else
  796.     motion = RELATIVE_V;
  797.     }
  798.   }
  799.   if (sbuf_space_code >= 0) {
  800.     int w = sbuf_style.f->get_width(space_char_index, sbuf_style.point_size);
  801.     if (w + sbuf_kern != sbuf_space_width) {
  802.       if (sbuf_space_code != output_space_code) {
  803.     set_space_code(sbuf_space_code);
  804.     output_space_code = sbuf_space_code;
  805.       }
  806.       space_flag = 1;
  807.       extra_space = sbuf_space_width - w - sbuf_kern;
  808.       if (sbuf_space_diff_count > sbuf_space_count/2)
  809.     extra_space++;
  810.       else if (sbuf_space_diff_count < -(sbuf_space_count/2))
  811.     extra_space--;
  812.     }
  813.   }
  814.   if (space_flag)
  815.     out.put_fix_number(extra_space);
  816.   if (sbuf_kern != 0)
  817.     out.put_fix_number(sbuf_kern);
  818.   out.put_string(sbuf, sbuf_len);
  819.   char sym[2];
  820.   sym[0] = 'A' + motion*4 + space_flag + 2*(sbuf_kern != 0);
  821.   sym[1] = '\0';
  822.   switch (motion) {
  823.   case NONE:
  824.     break;
  825.   case ABSOLUTE:
  826.     out.put_fix_number(sbuf_start_hpos)
  827.        .put_fix_number(sbuf_vpos);
  828.     break;
  829.   case RELATIVE_H:
  830.     out.put_fix_number(sbuf_start_hpos - output_hpos);
  831.     break;
  832.   case RELATIVE_V:
  833.     out.put_fix_number(sbuf_vpos - output_vpos);
  834.     break;
  835.   case RELATIVE_HV:
  836.     out.put_fix_number(sbuf_start_hpos - output_hpos)
  837.        .put_fix_number(sbuf_vpos - output_vpos);
  838.     break;
  839.   default:
  840.     assert(0);
  841.   }
  842.   out.put_symbol(sym);
  843.   output_hpos = sbuf_end_hpos;
  844.   output_vpos = sbuf_vpos;
  845.   sbuf_len = 0;
  846. }
  847.  
  848.  
  849. void ps_printer::set_line_thickness(const environment *env)
  850. {
  851.   if (line_thickness < 0) {
  852.     if (output_draw_point_size != env->size) {
  853.       // we ought to check for overflow here
  854.       int lw = ((font::res/(72*font::sizescale))*linewidth*env->size)/1000;
  855.       out.put_fix_number(lw).put_symbol("LW");
  856.       output_draw_point_size = env->size;
  857.       output_line_thickness = -1;
  858.     }
  859.   }
  860.   else {
  861.     if (output_line_thickness != line_thickness) {
  862.       out.put_fix_number(line_thickness).put_symbol("LW");
  863.       output_line_thickness = line_thickness;
  864.       output_draw_point_size = -1;
  865.     }
  866.   }
  867. }
  868.  
  869. void ps_printer::fill_path()
  870. {
  871.   if (fill > FILL_MAX)
  872.     out.put_symbol("BL");
  873.   else
  874.     out.put_float(transform_fill(fill)).put_symbol("FL");
  875. }
  876.  
  877. void ps_printer::draw(int code, int *p, int np, const environment *env)
  878. {
  879.   if (invis_count > 0)
  880.     return;
  881.   int fill_flag = 0;
  882.   switch (code) {
  883.   case 'C':
  884.     fill_flag = 1;
  885.     // fall through
  886.   case 'c':
  887.     // troff adds an extra argument to C
  888.     if (np != 1 && !(code == 'C' && np == 2)) {
  889.       error("1 argument required for circle");
  890.       break;
  891.     }
  892.     out.put_fix_number(env->hpos + p[0]/2)
  893.        .put_fix_number(env->vpos)
  894.        .put_fix_number(p[0]/2)
  895.        .put_symbol("DC");
  896.     if (fill_flag) {
  897.       fill_path();
  898.     }
  899.     else {
  900.       set_line_thickness(env);
  901.       out.put_symbol("ST");
  902.     }
  903.     break;
  904.   case 'l':
  905.     if (np != 2) {
  906.       error("2 arguments required for line");
  907.       break;
  908.     }
  909.     set_line_thickness(env);
  910.     out.put_fix_number(p[0] + env->hpos)
  911.        .put_fix_number(p[1] + env->vpos)
  912.        .put_fix_number(env->hpos)
  913.        .put_fix_number(env->vpos)
  914.        .put_symbol("DL");
  915.     break;
  916.   case 'E':
  917.     fill_flag = 1;
  918.     // fall through
  919.   case 'e':
  920.     if (np != 2) {
  921.       error("2 arguments required for ellipse");
  922.       break;
  923.     }
  924.     out.put_fix_number(p[0])
  925.        .put_fix_number(p[1])
  926.        .put_fix_number(env->hpos + p[0]/2)
  927.        .put_fix_number(env->vpos)
  928.        .put_symbol("DE");
  929.     if (fill_flag) {
  930.       fill_path();
  931.     }
  932.     else {
  933.       set_line_thickness(env);
  934.       out.put_symbol("ST");
  935.     }
  936.     break;
  937.   case 'P':
  938.     fill_flag = 1;
  939.     // fall through
  940.   case 'p':
  941.     {
  942.       if (np & 1) {
  943.     error("even number of arguments required for polygon");
  944.     break;
  945.       }
  946.       if (np == 0) {
  947.     error("no arguments for polygon");
  948.     break;
  949.       }
  950.       out.put_fix_number(env->hpos)
  951.      .put_fix_number(env->vpos)
  952.      .put_symbol("MT");
  953.       for (int i = 0; i < np; i += 2)
  954.     out.put_fix_number(p[i])
  955.        .put_fix_number(p[i+1])
  956.        .put_symbol("RL");
  957.       out.put_symbol("CL");
  958.       if (fill_flag) {
  959.     fill_path();
  960.       }
  961.       else {
  962.     set_line_thickness(env);
  963.     out.put_symbol("ST");
  964.       }
  965.       break;
  966.     }
  967.   case '~':
  968.     {
  969.       if (np & 1) {
  970.     error("even number of arguments required for spline");
  971.     break;
  972.       }
  973.       if (np == 0) {
  974.     error("no arguments for spline");
  975.     break;
  976.       }
  977.       out.put_fix_number(env->hpos)
  978.      .put_fix_number(env->vpos)
  979.      .put_symbol("MT");
  980.       out.put_fix_number(p[0]/2)
  981.      .put_fix_number(p[1]/2)
  982.      .put_symbol("RL");
  983.       /* tnum/tden should be between 0 and 1; the closer it is to 1
  984.      the tighter the curve will be to the guiding lines; 2/3
  985.      is the standard value */
  986.       const int tnum = 2;
  987.       const int tden = 3;
  988.       for (int i = 0; i < np - 2; i += 2) {
  989.     out.put_fix_number((p[i]*tnum)/(2*tden))
  990.        .put_fix_number((p[i + 1]*tnum)/(2*tden))
  991.        .put_fix_number(p[i]/2 + (p[i + 2]*(tden - tnum))/(2*tden))
  992.        .put_fix_number(p[i + 1]/2 + (p[i + 3]*(tden - tnum))/(2*tden))
  993.        .put_fix_number((p[i] - p[i]/2) + p[i + 2]/2)
  994.        .put_fix_number((p[i + 1] - p[i + 1]/2) + p[i + 3]/2)
  995.        .put_symbol("RC");
  996.       }
  997.       out.put_fix_number(p[np - 2] - p[np - 2]/2)
  998.      .put_fix_number(p[np - 1] - p[np - 1]/2)
  999.      .put_symbol("RL");
  1000.       set_line_thickness(env);
  1001.       out.put_symbol("ST");
  1002.     }
  1003.     break;
  1004.   case 'a':
  1005.     {
  1006.       if (np != 4) {
  1007.     error("4 arguments required for arc");
  1008.     break;
  1009.       }
  1010.       set_line_thickness(env);
  1011.       double c[2];
  1012.       if (adjust_arc_center(p, c))
  1013.     out.put_fix_number(env->hpos + int(c[0]))
  1014.        .put_fix_number(env->vpos + int(c[1]))
  1015.        .put_fix_number(int(sqrt(c[0]*c[0] + c[1]*c[1])))
  1016.        .put_float(degrees(atan2(-c[1], -c[0])))
  1017.        .put_float(degrees(atan2(p[1] + p[3] - c[1], p[0] + p[2] - c[0])))
  1018.        .put_symbol("DA");
  1019.       else
  1020.     out.put_fix_number(p[0] + p[2] + env->hpos)
  1021.        .put_fix_number(p[1] + p[3] + env->vpos)
  1022.        .put_fix_number(env->hpos)
  1023.        .put_fix_number(env->vpos)
  1024.        .put_symbol("DL");
  1025.     }
  1026.     break;
  1027.   case 't':
  1028.     {
  1029.       if (np == 0) {
  1030.     line_thickness = -1;
  1031.       }
  1032.       else {
  1033.     // troff gratuitously adds an extra 0
  1034.     if (np != 1 && np != 2) {
  1035.       error("0 or 1 argument required for thickness");
  1036.       break;
  1037.     }
  1038.     line_thickness = p[0];
  1039.       }
  1040.       break;
  1041.     }
  1042.   case 'f':
  1043.     {
  1044.       if (np != 1 && np != 2) {
  1045.     error("1 argument required for fill");
  1046.     break;
  1047.       }
  1048.       fill = p[0];
  1049.       if (fill < 0 || fill > FILL_MAX) {
  1050.     // This means fill with the current color.
  1051.     fill = FILL_MAX + 1;
  1052.       }
  1053.       break;
  1054.     }      
  1055.   default:
  1056.     error("unrecognised drawing command `%1'", char(code));
  1057.     break;
  1058.   }
  1059.  
  1060.   output_hpos = output_vpos = -1;
  1061. }
  1062.  
  1063.  
  1064. void ps_printer::begin_page(int n)
  1065. {
  1066.   out.begin_comment("Page:").comment_arg(itoa(n));
  1067.   out.comment_arg(itoa(++pages_output)).end_comment();
  1068.   output_style.f = 0;
  1069.   output_space_code = 32;
  1070.   output_draw_point_size = -1;
  1071.   output_line_thickness = -1;
  1072.   output_hpos = output_vpos = -1;
  1073.   ndefined_styles = 0;
  1074.   out.simple_comment("BeginPageSetup");
  1075.   out.put_symbol("BP");
  1076.   out.simple_comment("EndPageSetup");
  1077. }
  1078.  
  1079. void ps_printer::end_page(int)
  1080. {
  1081.   flush_sbuf();
  1082.   out.put_symbol("EP");
  1083.   if (invis_count != 0) {
  1084.     error("missing `endinvis' command");
  1085.     invis_count = 0;
  1086.   }
  1087. }
  1088.  
  1089. font *ps_printer::make_font(const char *nm)
  1090. {
  1091.   return ps_font::load_ps_font(nm);
  1092. }
  1093.  
  1094. ps_printer::~ps_printer()
  1095. {
  1096.   out.simple_comment("Trailer");
  1097.   out.put_symbol("end");
  1098.   out.simple_comment("EOF");
  1099.   if (fseek(tempfp, 0L, 0) < 0)
  1100.     fatal("fseek on temporary file failed");
  1101.   fputs("%!PS-Adobe-", stdout);
  1102.   fputs((broken_flags & USE_PS_ADOBE_2_0) ? "2.0" : "3.0", stdout);
  1103.   putchar('\n');
  1104.   out.set_file(stdout);
  1105.   {
  1106.     extern const char *version_string;
  1107.     out.begin_comment("Creator:")
  1108.        .comment_arg("groff")
  1109.        .comment_arg("version")
  1110.        .comment_arg(version_string)
  1111.        .end_comment();
  1112.   }
  1113.   {
  1114.     fputs("%%CreationDate: ", out.get_file());
  1115. #ifdef LONG_FOR_TIME_T
  1116.     long
  1117. #else
  1118.     time_t
  1119. #endif
  1120.     t = time(0);
  1121.     fputs(ctime(&t), out.get_file());
  1122.   }
  1123.   for (font_pointer_list *f = font_list; f; f = f->next) {
  1124.     ps_font *psf = (ps_font *)(f->p);
  1125.     rm.need_font(psf->get_internal_name());
  1126.   }
  1127.   rm.print_header_comments(out);
  1128.   out.begin_comment("Pages:").comment_arg(itoa(pages_output)).end_comment();
  1129.   out.begin_comment("PageOrder:").comment_arg("Ascend").end_comment();
  1130. #if 0
  1131.   fprintf(out.get_file(), "%%%%DocumentMedia: () %g %g 0 () ()\n",
  1132.       font::paperwidth*72.0/font::res,
  1133.       paper_length*72.0/font::res);
  1134. #endif
  1135.   out.begin_comment("Orientation:")
  1136.      .comment_arg(landscape_flag ? "Landscape" : "Portrait")
  1137.      .end_comment(); 
  1138.   if (ncopies != 1) {
  1139.     out.end_line();
  1140.     fprintf(out.get_file(), "%%%%Requirements: numcopies(%d)\n", ncopies);
  1141.   }
  1142.   out.simple_comment("EndComments");
  1143.   out.simple_comment("BeginProlog");
  1144.   rm.output_prolog(out);
  1145.   if (!(broken_flags & NO_SETUP_SECTION)) {
  1146.     out.simple_comment("EndProlog");
  1147.     out.simple_comment("BeginSetup");
  1148.   }
  1149.   rm.document_setup(out);
  1150.   out.put_symbol(dict_name).put_symbol("begin");
  1151.   if (ndefs > 0)
  1152.     ndefs += DEFS_DICT_SPARE;
  1153.   out.put_literal_symbol(defs_dict_name)
  1154.      .put_number(ndefs + 1)
  1155.      .put_symbol("dict")
  1156.      .put_symbol("def");
  1157.   out.put_symbol(defs_dict_name)
  1158.      .put_symbol("begin");
  1159.   out.put_literal_symbol("u")
  1160.      .put_delimiter('{')
  1161.      .put_fix_number(1)
  1162.      .put_symbol("mul")
  1163.      .put_delimiter('}')
  1164.      .put_symbol("bind")
  1165.      .put_symbol("def");
  1166.   defs += '\0';
  1167.   out.special(defs.contents());
  1168.   out.put_symbol("end");
  1169.   if (ncopies != 1)
  1170.     out.put_literal_symbol("#copies").put_number(ncopies).put_symbol("def");
  1171.   out.put_literal_symbol("RES").put_number(res).put_symbol("def");
  1172.   out.put_literal_symbol("PL");
  1173.   if (guess_flag)
  1174.     out.put_symbol("PLG");
  1175.   else
  1176.     out.put_fix_number(paper_length);
  1177.   out.put_symbol("def");
  1178.   out.put_literal_symbol("LS")
  1179.      .put_symbol(landscape_flag ? "true" : "false")
  1180.      .put_symbol("def");
  1181.   encode_fonts();
  1182.   out.simple_comment((broken_flags & NO_SETUP_SECTION)
  1183.              ? "EndProlog"
  1184.              : "EndSetup");
  1185.   out.end_line();
  1186.   out.copy_file(tempfp);
  1187.   fclose(tempfp);
  1188. }
  1189.  
  1190. void ps_printer::special(char *arg, const environment *env)
  1191. {
  1192.   typedef void (ps_printer::*SPECIAL_PROCP)(char *, const environment *);
  1193.   static struct {
  1194.     const char *name;
  1195.     SPECIAL_PROCP proc;
  1196.   } proc_table[] = {
  1197.     { "exec", &ps_printer::do_exec },
  1198.     { "def", &ps_printer::do_def },
  1199.     { "mdef", &ps_printer::do_mdef },
  1200.     { "import", &ps_printer::do_import },
  1201.     { "file", &ps_printer::do_file },
  1202.     { "invis", &ps_printer::do_invis },
  1203.     { "endinvis", &ps_printer::do_endinvis },
  1204.   };
  1205.   for (char *p = arg; *p == ' ' || *p == '\n'; p++)
  1206.     ;
  1207.   char *tag = p;
  1208.   for (; *p != '\0' && *p != ':' && *p != ' ' && *p != '\n'; p++)
  1209.     ;
  1210.   if (*p == '\0' || strncmp(tag, "ps", p - tag) != 0) {
  1211.     error("X command without `ps:' tag ignored");
  1212.     return;
  1213.   }
  1214.   p++;
  1215.   for (; *p == ' ' || *p == '\n'; p++)
  1216.     ;
  1217.   char *command = p;
  1218.   for (; *p != '\0' && *p != ' ' && *p != '\n'; p++)
  1219.     ;
  1220.   if (*command == '\0') {
  1221.     error("X command without `ps:' tag ignored");
  1222.     return;
  1223.   }
  1224.   for (int i = 0; i < sizeof(proc_table)/sizeof(proc_table[0]); i++)
  1225.     if (strncmp(command, proc_table[i].name, p - command) == 0) {
  1226.       (this->*(proc_table[i].proc))(p, env);
  1227.       return;
  1228.     }
  1229.   error("X command `%1' not recognised", command);
  1230. }
  1231.  
  1232. // A conforming PostScript document must not have lines longer
  1233. // than 255 characters (excluding line termination characters).
  1234.  
  1235. static int check_line_lengths(const char *p)
  1236. {
  1237.   for (;;) {
  1238.     const char *end = strchr(p, '\n');
  1239.     if (end == 0)
  1240.       end = strchr(p, '\0');
  1241.     if (end - p > 255)
  1242.       return 0;
  1243.     if (*end == '\0')
  1244.       break;
  1245.     p = end + 1;
  1246.   }
  1247.   return 1;
  1248. }
  1249.  
  1250. void ps_printer::do_exec(char *arg, const environment *env)
  1251. {
  1252.   flush_sbuf();
  1253.   while (csspace(*arg))
  1254.     arg++;
  1255.   if (*arg == '\0') {
  1256.     error("missing argument to X exec command");
  1257.     return;
  1258.   }
  1259.   if (!check_line_lengths(arg)) {
  1260.     error("lines in X exec command must not be more than 255 characters long");
  1261.     return;
  1262.   }
  1263.   out.put_fix_number(env->hpos)
  1264.      .put_fix_number(env->vpos)
  1265.      .put_symbol("EBEGIN")
  1266.      .special(arg)
  1267.      .put_symbol("EEND");
  1268.   output_hpos = output_vpos = -1;
  1269.   output_style.f = 0;
  1270.   output_draw_point_size = -1;
  1271.   output_line_thickness = -1;
  1272.   ndefined_styles = 0;
  1273.   if (!ndefs)
  1274.     ndefs = 1;
  1275. }
  1276.  
  1277. void ps_printer::do_file(char *arg, const environment *env)
  1278. {
  1279.   flush_sbuf();
  1280.   while (csspace(*arg))
  1281.     arg++;
  1282.   if (*arg == '\0') {
  1283.     error("missing argument to X file command");
  1284.     return;
  1285.   }
  1286.   const char *filename = arg;
  1287.   do {
  1288.     ++arg;
  1289.   } while (*arg != '\0' && *arg != ' ' && *arg != '\n');
  1290.   out.put_fix_number(env->hpos)
  1291.      .put_fix_number(env->vpos)
  1292.      .put_symbol("EBEGIN");
  1293.   rm.import_file(filename, out);
  1294.   out.put_symbol("EEND");
  1295.   output_hpos = output_vpos = -1;
  1296.   output_style.f = 0;
  1297.   output_draw_point_size = -1;
  1298.   output_line_thickness = -1;
  1299.   ndefined_styles = 0;
  1300.   if (!ndefs)
  1301.     ndefs = 1;
  1302. }
  1303.  
  1304. void ps_printer::do_def(char *arg, const environment *)
  1305. {
  1306.   flush_sbuf();
  1307.   while (csspace(*arg))
  1308.     arg++;
  1309.   if (!check_line_lengths(arg)) {
  1310.     error("lines in X def command must not be more than 255 characters long");
  1311.     return;
  1312.   }
  1313.   defs += arg;
  1314.   if (*arg != '\0' && strchr(arg, '\0')[-1] != '\n')
  1315.     defs += '\n';
  1316.   ndefs++;
  1317. }
  1318.  
  1319. // Like def, but the first argument says how many definitions it contains.
  1320.  
  1321. void ps_printer::do_mdef(char *arg, const environment *)
  1322. {
  1323.   flush_sbuf();
  1324.   char *p;
  1325.   int n = (int)strtol(arg, &p, 10);
  1326.   if (n == 0 && p == arg) {
  1327.     error("first argument to X mdef must be an integer");
  1328.     return;
  1329.   }
  1330.   if (n < 0) {
  1331.     error("out of range argument `%1' to X mdef command", int(n));
  1332.     return;
  1333.   }
  1334.   arg = p;
  1335.   while (csspace(*arg))
  1336.     arg++;
  1337.   if (!check_line_lengths(arg)) {
  1338.     error("lines in X mdef command must not be more than 255 characters long");
  1339.     return;
  1340.   }
  1341.   defs += arg;
  1342.   if (*arg != '\0' && strchr(arg, '\0')[-1] != '\n')
  1343.     defs += '\n';
  1344.   ndefs += n;
  1345. }
  1346.  
  1347. void ps_printer::do_import(char *arg, const environment *env)
  1348. {
  1349.   flush_sbuf();
  1350.   while (*arg == ' ' || *arg == '\n')
  1351.     arg++;
  1352.   for (char *p = arg; *p != '\0' && *p != ' ' && *p != '\n'; p++)
  1353.     ;
  1354.   if (*p != '\0')
  1355.     *p++ = '\0';
  1356.   int parms[6];
  1357.   int nparms = 0;
  1358.   while (nparms < 6) {
  1359.     char *end;
  1360.     long n = strtol(p, &end, 10);
  1361.     if (n == 0 && end == p)
  1362.       break;
  1363.     parms[nparms++] = int(n);
  1364.     p = end;
  1365.   }
  1366.   if (csalpha(*p) && (p[1] == '\0' || p[1] == ' ' || p[1] == '\n')) {
  1367.     error("scaling indicators not allowed in arguments for X import command");
  1368.     return;
  1369.   }
  1370.   while (*p == ' ' || *p == '\n')
  1371.     p++;
  1372.   if (nparms < 5) {
  1373.     if (*p == '\0')
  1374.       error("too few arguments for X import command");
  1375.     else
  1376.       error("invalid argument `%1' for X import command", p);
  1377.     return;
  1378.   }
  1379.   if (*p != '\0') {
  1380.     error("superflous argument `%1' for X import command", p);
  1381.     return;
  1382.   }
  1383.   int llx = parms[0];
  1384.   int lly = parms[1];
  1385.   int urx = parms[2];
  1386.   int ury = parms[3];
  1387.   int desired_width = parms[4];
  1388.   int desired_height = parms[5];
  1389.   if (desired_width <= 0) {
  1390.     error("bad width argument `%1' for X import command: must be > 0",
  1391.       desired_width);
  1392.     return;
  1393.   }
  1394.   if (nparms == 6 && desired_height <= 0) {
  1395.     error("bad height argument `%1' for X import command: must be > 0",
  1396.       desired_height);
  1397.     return;
  1398.   }
  1399.   if (llx == urx) {
  1400.     error("llx and urx arguments for X import command must not be equal");
  1401.     return;
  1402.   }
  1403.   if (lly == ury) {
  1404.     error("lly and ury arguments for X import command must not be equal");
  1405.     return;
  1406.   }
  1407.   if (nparms == 5) {
  1408.     int old_wid = urx - llx;
  1409.     int old_ht = ury - lly;
  1410.     if (old_wid < 0)
  1411.       old_wid = -old_wid;
  1412.     if (old_ht < 0)
  1413.       old_ht = -old_ht;
  1414.     desired_height = int(desired_width*(double(old_ht)/double(old_wid)) + .5);
  1415.   }
  1416.   if (env->vpos - desired_height < 0)
  1417.     warning("top of imported graphic is above the top of the page");
  1418.   out.put_number(llx)
  1419.      .put_number(lly)
  1420.      .put_fix_number(desired_width)
  1421.      .put_number(urx - llx)
  1422.      .put_fix_number(-desired_height)
  1423.      .put_number(ury - lly)
  1424.      .put_fix_number(env->hpos)
  1425.      .put_fix_number(env->vpos)
  1426.      .put_symbol("PBEGIN");
  1427.   rm.import_file(arg, out);
  1428.   // do this here just in case application defines PEND
  1429.   out.put_symbol("end");
  1430.   out.put_symbol("PEND");
  1431. }
  1432.  
  1433. void ps_printer::do_invis(char *, const environment *)
  1434. {
  1435.   invis_count++;
  1436. }
  1437.  
  1438. void ps_printer::do_endinvis(char *, const environment *)
  1439. {
  1440.   if (invis_count == 0)
  1441.     error("unbalanced `endinvis' command");
  1442.   else
  1443.     --invis_count;
  1444. }
  1445.  
  1446. printer *make_printer()
  1447. {
  1448.   return new ps_printer;
  1449. }
  1450.  
  1451. static void usage();
  1452.  
  1453. int main(int argc, char **argv)
  1454. {
  1455.   program_name = argv[0];
  1456.   static char stderr_buf[BUFSIZ];
  1457.   setbuf(stderr, stderr_buf);
  1458.   int c;
  1459.   while ((c = getopt(argc, argv, "F:glc:w:vb:")) != EOF)
  1460.     switch(c) {
  1461.     case 'v':
  1462.       {
  1463.     extern const char *version_string;
  1464.     fprintf(stderr, "grops version %s\n", version_string);
  1465.     fflush(stderr);
  1466.     break;
  1467.       }
  1468.     case 'c':
  1469.       if (sscanf(optarg, "%d", &ncopies) != 1 || ncopies <= 0) {
  1470.     error("bad number of copies `%s'", optarg);
  1471.     ncopies = 1;
  1472.       }
  1473.       break;
  1474.     case 'g':
  1475.       guess_flag = 1;
  1476.       break;
  1477.     case 'l':
  1478.       landscape_flag = 1;
  1479.       break;
  1480.     case 'F':
  1481.       font::command_line_font_dir(optarg);
  1482.       break;
  1483.     case 'w':
  1484.       if (sscanf(optarg, "%d", &linewidth) != 1 || linewidth < 0) {
  1485.     error("bad linewidth `%s'", optarg);
  1486.     linewidth = -1;
  1487.       }
  1488.       break;
  1489.     case 'b':
  1490.       // XXX check this
  1491.       broken_flags = atoi(optarg);
  1492.       bflag = 1;
  1493.       break;
  1494.     case '?':
  1495.       usage();
  1496.       break;
  1497.     default:
  1498.       assert(0);
  1499.     }
  1500.   font::set_unknown_desc_command_handler(handle_unknown_desc_command);
  1501.   if (optind >= argc)
  1502.     do_file("-");
  1503.   else {
  1504.     for (int i = optind; i < argc; i++)
  1505.       do_file(argv[i]);
  1506.   }
  1507.   delete pr;
  1508.   return 0;
  1509. }
  1510.  
  1511. static void usage()
  1512. {
  1513.   fprintf(stderr, "usage: %s [-glv] [-b n] [-c n] [-w n] [-F dir] [files ...]\n",
  1514.       program_name);
  1515.   exit(1);
  1516. }
  1517.