home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / gnu / groff-1.09-src.lha / src / amiga / groff-1.09 / tbl / table.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-28  |  68.6 KB  |  2,765 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 "table.h"
  22.  
  23. #define BAR_HEIGHT ".25m"
  24. #define DOUBLE_LINE_SEP "2p"
  25. #define HALF_DOUBLE_LINE_SEP "1p"
  26. #define LINE_SEP "2p"
  27. #define BODY_DEPTH ".25m"
  28.  
  29. const int DEFAULT_COLUMN_SEPARATION = 3;
  30.  
  31. #define DELIMITER_CHAR "\\[tbl]"
  32. #define PREFIX "3"
  33. #define SEPARATION_FACTOR_REG PREFIX "sep"
  34. #define BOTTOM_REG PREFIX "bot"
  35. #define RESET_MACRO_NAME PREFIX "init"
  36. #define LINESIZE_REG PREFIX "lps"
  37. #define TOP_REG PREFIX "top"
  38. #define CURRENT_ROW_REG PREFIX "crow"
  39. #define LAST_PASSED_ROW_REG PREFIX "passed"
  40. #define TRANSPARENT_STRING_NAME PREFIX "trans"
  41. #define QUOTE_STRING_NAME PREFIX "quote"
  42. #define SECTION_DIVERSION_NAME PREFIX "section"
  43. #define SECTION_DIVERSION_FLAG_REG PREFIX "sflag"
  44. #define SAVED_VERTICAL_POS_REG PREFIX "vert"
  45. #define NEED_BOTTOM_RULE_REG PREFIX "brule"
  46. #define KEEP_MACRO_NAME PREFIX "keep"
  47. #define RELEASE_MACRO_NAME PREFIX "release"
  48. #define SAVED_FONT_REG PREFIX "fnt"
  49. #define SAVED_SIZE_REG PREFIX "sz"
  50. #define SAVED_FILL_REG PREFIX "fll"
  51. #define SAVED_INDENT_REG PREFIX "ind"
  52. #define SAVED_CENTER_REG PREFIX "cent"
  53. #define TABLE_DIVERSION_NAME PREFIX "table"
  54. #define TABLE_DIVERSION_FLAG_REG PREFIX "tflag"
  55. #define TABLE_KEEP_MACRO_NAME PREFIX "tkeep"
  56. #define TABLE_RELEASE_MACRO_NAME PREFIX "trelease"
  57. #define NEEDED_REG PREFIX "needed"
  58. #define REPEATED_MARK_MACRO PREFIX "rmk"
  59. #define REPEATED_VPT_MACRO PREFIX "rvpt"
  60. #define SUPPRESS_BOTTOM_REG PREFIX "supbot"
  61. #define SAVED_DN_REG PREFIX "dn"
  62.  
  63. // this must be one character
  64. #define COMPATIBLE_REG PREFIX "c"
  65.  
  66. #define BLOCK_WIDTH_PREFIX PREFIX "tbw"
  67. #define BLOCK_DIVERSION_PREFIX PREFIX "tbd"
  68. #define BLOCK_HEIGHT_PREFIX PREFIX "tbh"
  69. #define SPAN_WIDTH_PREFIX PREFIX "w"
  70. #define SPAN_LEFT_NUMERIC_WIDTH_PREFIX PREFIX "lnw"
  71. #define SPAN_RIGHT_NUMERIC_WIDTH_PREFIX PREFIX "rnw"
  72. #define SPAN_ALPHABETIC_WIDTH_PREFIX PREFIX "aw"
  73. #define COLUMN_SEPARATION_PREFIX PREFIX "cs"
  74. #define ROW_START_PREFIX PREFIX "rs"
  75. #define COLUMN_START_PREFIX PREFIX "cl"
  76. #define COLUMN_END_PREFIX PREFIX "ce"
  77. #define COLUMN_DIVIDE_PREFIX PREFIX "cd"
  78. #define ROW_TOP_PREFIX PREFIX "rt"
  79.  
  80. string block_width_reg(int r, int c);
  81. string block_diversion_name(int r, int c);
  82. string block_height_reg(int r, int c);
  83. string span_width_reg(int start_col, int end_col);
  84. string span_left_numeric_width_reg(int start_col, int end_col);
  85. string span_right_numeric_width_reg(int start_col, int end_col);
  86. string span_alphabetic_width_reg(int start_col, int end_col);
  87. string column_separation_reg(int col);
  88. string row_start_reg(int r);
  89. string column_start_reg(int c);
  90. string column_end_reg(int c);
  91. string column_divide_reg(int c);
  92. string row_top_reg(int r);
  93.  
  94. void set_inline_modifier(const entry_modifier *);
  95. void restore_inline_modifier(const entry_modifier *m);
  96. void set_modifier(const entry_modifier *);
  97. int find_decimal_point(const char *s, char decimal_point_char,
  98.                const char *delim);
  99.  
  100. string an_empty_string;
  101. int location_force_filename = 0;
  102.  
  103. void printfs(const char *,
  104.          const string &arg1 = an_empty_string,
  105.          const string &arg2 = an_empty_string,
  106.          const string &arg3 = an_empty_string,
  107.          const string &arg4 = an_empty_string,
  108.          const string &arg5 = an_empty_string);
  109.  
  110. void prints(const string &);
  111.  
  112. inline void prints(char c)
  113. {
  114.   putchar(c);
  115. }
  116.  
  117. inline void prints(const char *s)
  118. {
  119.   fputs(s, stdout);
  120. }
  121.  
  122. void prints(const string &s)
  123. {
  124.   if (!s.empty())
  125.     fwrite(s.contents(), 1, s.length(), stdout);
  126. }
  127.  
  128. struct horizontal_span {
  129.   horizontal_span *next;
  130.   short start_col;
  131.   short end_col;
  132.   horizontal_span(int, int, horizontal_span *);
  133. };
  134.  
  135. struct single_line_entry;
  136. struct double_line_entry;
  137. struct simple_entry;
  138.  
  139. class table_entry {
  140. friend class table;
  141.   table_entry *next;
  142.   int input_lineno;
  143.   const char *input_filename;
  144. protected:
  145.   short start_row;
  146.   short start_col;
  147.   short end_row;
  148.   short end_col;
  149.   const entry_modifier *mod;
  150. public:
  151.   void set_location();
  152.   table_entry(const entry_modifier *);
  153.   virtual ~table_entry();
  154.   virtual int divert(int ncols, const string *mw, int *sep);
  155.   virtual void do_width();
  156.   virtual void do_depth();
  157.   virtual void print() = 0;
  158.   virtual void position_vertically() = 0;
  159.   virtual single_line_entry *to_single_line_entry();
  160.   virtual double_line_entry *to_double_line_entry();
  161.   virtual simple_entry *to_simple_entry();
  162.   virtual int line_type();
  163.   virtual void note_double_vrule_on_right(int);
  164.   virtual void note_double_vrule_on_left(int);
  165. };
  166.  
  167. class simple_entry : public table_entry {
  168. public:
  169.   simple_entry(const entry_modifier *);
  170.   void print();
  171.   void position_vertically();
  172.   simple_entry *to_simple_entry();
  173.   virtual void add_tab();
  174.   virtual void simple_print(int);
  175. };
  176.  
  177. class empty_entry : public simple_entry {
  178. public:
  179.   empty_entry(const entry_modifier *);
  180.   int line_type();
  181. };
  182.  
  183. class text_entry : public simple_entry {
  184. protected:
  185.   char *contents;
  186.   void print_contents();
  187. public:
  188.   text_entry(char *, const entry_modifier *);
  189.   ~text_entry();
  190. };
  191.  
  192. void text_entry::print_contents()
  193. {
  194.   set_inline_modifier(mod);
  195.   prints(contents);
  196.   restore_inline_modifier(mod);
  197. }
  198.  
  199. class repeated_char_entry : public text_entry {
  200. public:
  201.   repeated_char_entry(char *s, const entry_modifier *m);
  202.   void simple_print(int);
  203. };
  204.  
  205. class simple_text_entry : public text_entry {
  206. public:
  207.   simple_text_entry(char *s, const entry_modifier *m);
  208.   void do_width();
  209. };
  210.  
  211. class left_text_entry : public simple_text_entry {
  212. public:
  213.   left_text_entry(char *s, const entry_modifier *m);
  214.   void simple_print(int);
  215.   void add_tab();
  216. };
  217.  
  218. class right_text_entry : public simple_text_entry {
  219. public:
  220.   right_text_entry(char *s, const entry_modifier *m);
  221.   void simple_print(int);
  222.   void add_tab();
  223. };
  224.  
  225. class center_text_entry : public simple_text_entry {
  226. public:
  227.   center_text_entry(char *s, const entry_modifier *m);
  228.   void simple_print(int);
  229.   void add_tab();
  230. };
  231.  
  232. class numeric_text_entry : public text_entry {
  233.   int dot_pos;
  234. public:
  235.   numeric_text_entry(char *s, const entry_modifier *m, int pos);
  236.   void do_width();
  237.   void simple_print(int);
  238. };
  239.  
  240. class alphabetic_text_entry : public text_entry {
  241. public:
  242.   alphabetic_text_entry(char *s, const entry_modifier *m);
  243.   void do_width();
  244.   void simple_print(int);
  245.   void add_tab();
  246. };
  247.  
  248. class line_entry : public simple_entry {
  249. protected:
  250.   char double_vrule_on_right;
  251.   char double_vrule_on_left;
  252. public:
  253.   line_entry(const entry_modifier *);
  254.   void note_double_vrule_on_right(int);
  255.   void note_double_vrule_on_left(int);
  256.   void simple_print(int) = 0;
  257. };
  258.  
  259. class single_line_entry : public line_entry {
  260. public:
  261.   single_line_entry(const entry_modifier *m);
  262.   void simple_print(int);
  263.   single_line_entry *to_single_line_entry();
  264.   int line_type();
  265. };
  266.  
  267. class double_line_entry : public line_entry {
  268. public:
  269.   double_line_entry(const entry_modifier *m);
  270.   void simple_print(int);
  271.   double_line_entry *to_double_line_entry();
  272.   int line_type();
  273. };
  274.  
  275. class short_line_entry : public simple_entry {
  276. public:
  277.   short_line_entry(const entry_modifier *m);
  278.   void simple_print(int);
  279.   int line_type();
  280. };
  281.  
  282. class short_double_line_entry : public simple_entry {
  283. public:
  284.   short_double_line_entry(const entry_modifier *m);
  285.   void simple_print(int);
  286.   int line_type();
  287. };
  288.  
  289. class block_entry : public table_entry {
  290.   char *contents;
  291. protected:
  292.   void do_divert(int alphabetic, int ncols, const string *mw, int *sep);
  293. public:
  294.   block_entry(char *s, const entry_modifier *m);
  295.   ~block_entry();
  296.   int divert(int ncols, const string *mw, int *sep);
  297.   void do_width();
  298.   void do_depth();
  299.   void position_vertically();
  300.   void print() = 0;
  301. };
  302.  
  303. class left_block_entry : public block_entry {
  304. public:
  305.   left_block_entry(char *s, const entry_modifier *m);
  306.   void print();
  307. };
  308.  
  309. class right_block_entry : public block_entry {
  310. public:
  311.   right_block_entry(char *s, const entry_modifier *m);
  312.   void print();
  313. };
  314.  
  315. class center_block_entry : public block_entry {
  316. public:
  317.   center_block_entry(char *s, const entry_modifier *m);
  318.   void print();
  319. };
  320.  
  321. class alphabetic_block_entry : public block_entry {
  322. public:
  323.   alphabetic_block_entry(char *s, const entry_modifier *m);
  324.   void print();
  325.   int divert(int ncols, const string *mw, int *sep);
  326. };
  327.  
  328. table_entry::table_entry(const entry_modifier *m)
  329. : next(0), start_row(-1), end_row(-1), start_col(-1), end_col(-1), mod(m),
  330.   input_lineno(-1), input_filename(0)
  331. {
  332. }
  333.  
  334. table_entry::~table_entry()
  335. {
  336. }
  337.  
  338. int table_entry::divert(int, const string *, int *)
  339. {
  340.   return 0;
  341. }
  342.  
  343. void table_entry::do_width()
  344. {
  345. }
  346.  
  347. single_line_entry *table_entry::to_single_line_entry()
  348. {
  349.   return 0;
  350. }
  351.  
  352. double_line_entry *table_entry::to_double_line_entry()
  353. {
  354.   return 0;
  355. }
  356.  
  357. simple_entry *table_entry::to_simple_entry()
  358. {
  359.   return 0;
  360. }
  361.  
  362. void table_entry::do_depth()
  363. {
  364. }
  365.  
  366. void table_entry::set_location()
  367. {
  368.   set_troff_location(input_filename, input_lineno);
  369. }
  370.  
  371. int table_entry::line_type()
  372. {
  373.   return -1;
  374. }
  375.  
  376. void table_entry::note_double_vrule_on_right(int)
  377. {
  378. }
  379.  
  380. void table_entry::note_double_vrule_on_left(int)
  381. {
  382. }
  383.  
  384. simple_entry::simple_entry(const entry_modifier *m) : table_entry(m)
  385. {
  386. }
  387.  
  388. void simple_entry::add_tab()
  389. {
  390.   // do nothing
  391. }
  392.  
  393. void simple_entry::simple_print(int)
  394. {
  395.   // do nothing
  396. }
  397.  
  398. void simple_entry::position_vertically()
  399. {
  400.   if (start_row != end_row)
  401.     switch (mod->vertical_alignment) {
  402.     case entry_modifier::TOP:
  403.       printfs(".sp |\\n[%1]u\n", row_start_reg(start_row));
  404.       break;
  405.     case entry_modifier::CENTER:
  406.       // Peform the motion in two stages so that the center is rounded
  407.       // vertically upwards even if net vertical motion is upwards.
  408.       printfs(".sp |\\n[%1]u\n", row_start_reg(start_row));
  409.       printfs(".sp \\n[" BOTTOM_REG "]u-\\n[%1]u-1v/2u\n", 
  410.           row_start_reg(start_row));
  411.       break;
  412.     case entry_modifier::BOTTOM:
  413.       printfs(".sp |\\n[%1]u+\\n[" BOTTOM_REG "]u-\\n[%1]u-1v\n", 
  414.           row_start_reg(start_row));
  415.       break;
  416.     default:
  417.       assert(0);
  418.     }
  419. }
  420.  
  421. void simple_entry::print()
  422. {
  423.   prints(".ta");
  424.   add_tab();
  425.   prints('\n');
  426.   set_location();
  427.   prints("\\&");
  428.   simple_print(0);
  429.   prints('\n');
  430. }
  431.  
  432. simple_entry *simple_entry::to_simple_entry()
  433. {
  434.   return this;
  435. }
  436.  
  437. empty_entry::empty_entry(const entry_modifier *m)
  438. : simple_entry(m)
  439. {
  440. }
  441.  
  442. int empty_entry::line_type()
  443. {
  444.   return 0;
  445. }
  446.  
  447. text_entry::text_entry(char *s, const entry_modifier *m)
  448. : contents(s), simple_entry(m)
  449. {
  450. }
  451.  
  452. text_entry::~text_entry()
  453. {
  454.   a_delete contents;
  455. }
  456.  
  457.  
  458. repeated_char_entry::repeated_char_entry(char *s, const entry_modifier *m)
  459. : text_entry(s, m)
  460. {
  461. }
  462.  
  463. void repeated_char_entry::simple_print(int)
  464. {
  465.   printfs("\\h'|\\n[%1]u'", column_start_reg(start_col));
  466.   set_inline_modifier(mod);
  467.   printfs("\\l" DELIMITER_CHAR "\\n[%1]u\\&",
  468.       span_width_reg(start_col, end_col));
  469.   prints(contents);
  470.   prints(DELIMITER_CHAR);
  471.   restore_inline_modifier(mod);
  472. }
  473.  
  474. simple_text_entry::simple_text_entry(char *s, const entry_modifier *m)
  475. : text_entry(s, m)
  476. {
  477. }
  478.  
  479. void simple_text_entry::do_width()
  480. {
  481.   set_location();
  482.   printfs(".nr %1 \\n[%1]>?\\w" DELIMITER_CHAR,
  483.       span_width_reg(start_col, end_col));
  484.   print_contents();
  485.   prints(DELIMITER_CHAR "\n");
  486. }
  487.  
  488. left_text_entry::left_text_entry(char *s, const entry_modifier *m)
  489. : simple_text_entry(s, m)
  490. {
  491. }
  492.  
  493. void left_text_entry::simple_print(int)
  494. {
  495.   printfs("\\h'|\\n[%1]u'", column_start_reg(start_col));
  496.   print_contents();
  497. }
  498.  
  499. // The only point of this is to make `\a' ``work'' as in Unix tbl.  Grrr.
  500.  
  501. void left_text_entry::add_tab()
  502. {
  503.   printfs(" \\n[%1]u", column_end_reg(end_col));
  504. }
  505.  
  506. right_text_entry::right_text_entry(char *s, const entry_modifier *m)
  507. : simple_text_entry(s, m)
  508. {
  509. }
  510.  
  511. void right_text_entry::simple_print(int)
  512. {
  513.   printfs("\\h'|\\n[%1]u'", column_start_reg(start_col));
  514.   prints("\002\003");
  515.   print_contents();
  516.   prints("\002");
  517. }
  518.  
  519. void right_text_entry::add_tab()
  520. {
  521.   printfs(" \\n[%1]u", column_end_reg(end_col));
  522. }
  523.  
  524. center_text_entry::center_text_entry(char *s, const entry_modifier *m)
  525. : simple_text_entry(s, m)
  526. {
  527. }
  528.  
  529. void center_text_entry::simple_print(int)
  530. {
  531.   printfs("\\h'|\\n[%1]u'", column_start_reg(start_col));
  532.   prints("\002\003");
  533.   print_contents();
  534.   prints("\003\002");
  535. }
  536.  
  537. void center_text_entry::add_tab()
  538. {
  539.   printfs(" \\n[%1]u", column_end_reg(end_col));
  540. }
  541.  
  542. numeric_text_entry::numeric_text_entry(char *s, const entry_modifier *m, int pos)
  543. : text_entry(s, m), dot_pos(pos)
  544. {
  545. }
  546.  
  547. void numeric_text_entry::do_width()
  548. {
  549.   if (dot_pos != 0) {
  550.     set_location();
  551.     printfs(".nr %1 0\\w" DELIMITER_CHAR,
  552.         block_width_reg(start_row, start_col));
  553.     set_inline_modifier(mod);
  554.     for (int i = 0; i < dot_pos; i++)
  555.       prints(contents[i]);
  556.     restore_inline_modifier(mod);
  557.     prints(DELIMITER_CHAR "\n");
  558.     printfs(".nr %1 \\n[%1]>?\\n[%2]\n",
  559.         span_left_numeric_width_reg(start_col, end_col),
  560.         block_width_reg(start_row, start_col));
  561.   }
  562.   else
  563.     printfs(".nr %1 0\n", block_width_reg(start_row, start_col));
  564.   if (contents[dot_pos] != '\0') {
  565.     set_location();
  566.     printfs(".nr %1 \\n[%1]>?\\w" DELIMITER_CHAR,
  567.         span_right_numeric_width_reg(start_col, end_col));
  568.     set_inline_modifier(mod);
  569.     prints(contents + dot_pos);
  570.     restore_inline_modifier(mod);
  571.     prints(DELIMITER_CHAR "\n");
  572.   }
  573. }
  574.  
  575. void numeric_text_entry::simple_print(int)
  576. {
  577.   printfs("\\h'|(\\n[%1]u-\\n[%2]u-\\n[%3]u/2u+\\n[%2]u+\\n[%4]u-\\n[%5]u)'",
  578.       span_width_reg(start_col, end_col),
  579.       span_left_numeric_width_reg(start_col, end_col),
  580.       span_right_numeric_width_reg(start_col, end_col),
  581.       column_start_reg(start_col),
  582.       block_width_reg(start_row, start_col));
  583.   print_contents();
  584. }
  585.  
  586. alphabetic_text_entry::alphabetic_text_entry(char *s, const entry_modifier *m)
  587. : text_entry(s, m)
  588. {
  589. }
  590.  
  591. void alphabetic_text_entry::do_width()
  592. {
  593.   set_location();
  594.   printfs(".nr %1 \\n[%1]>?\\w" DELIMITER_CHAR,
  595.       span_alphabetic_width_reg(start_col, end_col));
  596.   print_contents();
  597.   prints(DELIMITER_CHAR "\n");
  598. }
  599.  
  600. void alphabetic_text_entry::simple_print(int)
  601. {
  602.   printfs("\\h'|\\n[%1]u'", column_start_reg(start_col));
  603.   printfs("\\h'\\n[%1]u-\\n[%2]u/2u'",
  604.       span_width_reg(start_col, end_col),
  605.       span_alphabetic_width_reg(start_col, end_col));
  606.   print_contents();
  607. }
  608.  
  609. // The only point of this is to make `\a' ``work'' as in Unix tbl.  Grrr.
  610.  
  611. void alphabetic_text_entry::add_tab()
  612. {
  613.   printfs(" \\n[%1]u", column_end_reg(end_col));
  614. }
  615.  
  616. block_entry::block_entry(char *s, const entry_modifier *m)
  617. : table_entry(m), contents(s)
  618. {
  619. }
  620.  
  621. block_entry::~block_entry()
  622. {
  623.   a_delete contents;
  624. }
  625.  
  626. void block_entry::position_vertically()
  627. {
  628.   if (start_row != end_row)
  629.     switch(mod->vertical_alignment) {
  630.     case entry_modifier::TOP:
  631.       printfs(".sp |\\n[%1]u\n", row_start_reg(start_row));
  632.       break;
  633.     case entry_modifier::CENTER:
  634.       // Peform the motion in two stages so that the center is rounded
  635.       // vertically upwards even if net vertical motion is upwards.
  636.       printfs(".sp |\\n[%1]u\n", row_start_reg(start_row));
  637.       printfs(".sp \\n[" BOTTOM_REG "]u-\\n[%1]u-\\n[%2]u/2u\n", 
  638.           row_start_reg(start_row),
  639.           block_height_reg(start_row, start_col));
  640.       break;
  641.     case entry_modifier::BOTTOM:
  642.       printfs(".sp |\\n[%1]u+\\n[" BOTTOM_REG "]u-\\n[%1]u-\\n[%2]u\n", 
  643.           row_start_reg(start_row),
  644.           block_height_reg(start_row, start_col));
  645.       break;
  646.     default:
  647.       assert(0);
  648.     }
  649.   if (mod->stagger)
  650.     prints(".sp -.5v\n");
  651. }
  652.  
  653. int block_entry::divert(int ncols, const string *mw, int *sep)
  654. {
  655.   do_divert(0, ncols, mw, sep);
  656.   return 1;
  657. }
  658.  
  659. void block_entry::do_divert(int alphabetic, int ncols, const string *mw,
  660.                 int *sep)
  661. {
  662.   printfs(".di %1\n", block_diversion_name(start_row, start_col));
  663.   prints(".if \\n[" SAVED_FILL_REG "] .fi\n"
  664.      ".in 0\n");
  665.   prints(".ll ");
  666.   for (int i = start_col; i <= end_col; i++)
  667.     if (mw[i].empty())
  668.       break;
  669.   if (i > end_col) {
  670.     // Every column spanned by this entry has a minimum width.
  671.     for (int i = start_col; i <= end_col; i++) {
  672.       if (i > start_col) {
  673.     if (sep)
  674.       printfs("+%1n", as_string(sep[i - 1]));
  675.     prints('+');
  676.       }
  677.       printfs("(n;%1)", mw[i]);
  678.     }
  679.     printfs(">?\\n[%1]u", span_width_reg(start_col, end_col));
  680.   }
  681.   else
  682.     printfs("(u;\\n[%1]>?(\\n[.l]*%2/%3))", 
  683.         span_width_reg(start_col, end_col), 
  684.         as_string(end_col - start_col + 1),
  685.         as_string(ncols + 1));
  686.   if (alphabetic)
  687.     prints("-2n");
  688.   prints("\n");
  689.   set_modifier(mod);
  690.   prints(".cp \\n(" COMPATIBLE_REG "\n");
  691.   set_location();
  692.   prints(contents);
  693.   prints(".br\n.di\n.cp 0\n");
  694.   if (!mod->zero_width) {
  695.     if (alphabetic) {
  696.       printfs(".nr %1 \\n[%1]>?(\\n[dl]+2n)\n",
  697.           span_width_reg(start_col, end_col));
  698.       printfs(".nr %1 \\n[%1]>?\\n[dl]\n",
  699.           span_alphabetic_width_reg(start_col, end_col));
  700.     }
  701.     else
  702.       printfs(".nr %1 \\n[%1]>?\\n[dl]\n", span_width_reg(start_col, end_col));
  703.   }
  704.   printfs(".nr %1 \\n[dn]\n", block_height_reg(start_row, start_col));
  705.   printfs(".nr %1 \\n[dl]\n", block_width_reg(start_row, start_col));
  706.   prints("." RESET_MACRO_NAME "\n"
  707.      ".in \\n[" SAVED_INDENT_REG "]u\n"
  708.      ".nf\n");
  709.   // the block might have contained .lf commands
  710.   location_force_filename = 1;
  711. }
  712.  
  713. void block_entry::do_width()
  714. {
  715.   // do nothing; the action happens in divert
  716. }
  717.  
  718. void block_entry::do_depth()
  719. {
  720.   printfs(".nr " BOTTOM_REG " \\n[" BOTTOM_REG "]>?(\\n[%1]+\\n[%2])\n",
  721.       row_start_reg(start_row),
  722.       block_height_reg(start_row, start_col));
  723. }
  724.  
  725. left_block_entry::left_block_entry(char *s, const entry_modifier *m)
  726. : block_entry(s, m)
  727. {
  728. }
  729.  
  730. void left_block_entry::print()
  731. {
  732.   printfs(".in +\\n[%1]u\n", column_start_reg(start_col));
  733.   printfs(".%1\n", block_diversion_name(start_row, start_col));
  734.   prints(".in\n");
  735. }
  736.  
  737.  
  738.  
  739. right_block_entry::right_block_entry(char *s, const entry_modifier *m)
  740. : block_entry(s, m)
  741. {
  742. }
  743.  
  744. void right_block_entry::print()
  745. {
  746.   printfs(".in +\\n[%1]u+\\n[%2]u-\\n[%3]u\n",
  747.       column_start_reg(start_col),
  748.       span_width_reg(start_col, end_col),
  749.       block_width_reg(start_row, start_col));
  750.   printfs(".%1\n", block_diversion_name(start_row, start_col));
  751.   prints(".in\n");
  752. }
  753.  
  754. center_block_entry::center_block_entry(char *s, const entry_modifier *m)
  755. : block_entry(s, m)
  756. {
  757. }
  758.  
  759. void center_block_entry::print()
  760. {
  761.   printfs(".in +\\n[%1]u+(\\n[%2]u-\\n[%3]u/2u)\n",
  762.       column_start_reg(start_col),
  763.       span_width_reg(start_col, end_col),
  764.       block_width_reg(start_row, start_col));
  765.   printfs(".%1\n", block_diversion_name(start_row, start_col));
  766.   prints(".in\n");
  767. }
  768.  
  769. alphabetic_block_entry::alphabetic_block_entry(char *s,
  770.                            const entry_modifier *m)
  771. : block_entry(s, m)
  772. {
  773. }
  774.  
  775. int alphabetic_block_entry::divert(int ncols, const string *mw, int *sep)
  776. {
  777.   do_divert(1, ncols, mw, sep);
  778.   return 1;
  779. }
  780.  
  781. void alphabetic_block_entry::print()
  782. {
  783.   printfs(".in +\\n[%1]u+(\\n[%2]u-\\n[%3]u/2u)\n",
  784.       column_start_reg(start_col),
  785.       span_width_reg(start_col, end_col),
  786.       span_alphabetic_width_reg(start_col, end_col));
  787.   printfs(".%1\n", block_diversion_name(start_row, start_col));
  788.   prints(".in\n");
  789. }
  790.  
  791. line_entry::line_entry(const entry_modifier *m)
  792. : simple_entry(m), double_vrule_on_right(0), double_vrule_on_left(0)
  793. {
  794. }
  795.  
  796. void line_entry::note_double_vrule_on_right(int is_corner)
  797. {
  798.   double_vrule_on_right = is_corner ? 1 : 2;
  799. }
  800.  
  801. void line_entry::note_double_vrule_on_left(int is_corner)
  802. {
  803.   double_vrule_on_left = is_corner ? 1 : 2;
  804. }
  805.  
  806.  
  807. single_line_entry::single_line_entry(const entry_modifier *m)
  808. : line_entry(m)
  809. {
  810. }
  811.  
  812. int single_line_entry::line_type()
  813. {
  814.   return 1;
  815. }
  816.  
  817. void single_line_entry::simple_print(int dont_move)
  818. {
  819.   printfs("\\h'|\\n[%1]u",
  820.       column_divide_reg(start_col));
  821.   if (double_vrule_on_left) {
  822.     prints(double_vrule_on_left == 1 ? "-" : "+");
  823.     prints(HALF_DOUBLE_LINE_SEP);
  824.   }
  825.   prints("'");
  826.   if (!dont_move)
  827.     prints("\\v'-" BAR_HEIGHT "'");
  828.   printfs("\\s[\\n[" LINESIZE_REG "]]" "\\D'l |\\n[%1]u",
  829.       column_divide_reg(end_col+1));
  830.   if (double_vrule_on_right) {
  831.     prints(double_vrule_on_left == 1 ? "+" : "-");
  832.     prints(HALF_DOUBLE_LINE_SEP);
  833.   }
  834.   prints("0'\\s0");
  835.   if (!dont_move)
  836.     prints("\\v'" BAR_HEIGHT "'");
  837. }
  838.   
  839. single_line_entry *single_line_entry::to_single_line_entry()
  840. {
  841.   return this;
  842. }
  843.  
  844. double_line_entry::double_line_entry(const entry_modifier *m)
  845. : line_entry(m)
  846. {
  847. }
  848.  
  849. int double_line_entry::line_type()
  850. {
  851.   return 2;
  852. }
  853.  
  854. void double_line_entry::simple_print(int dont_move)
  855. {
  856.   if (!dont_move)
  857.     prints("\\v'-" BAR_HEIGHT "'");
  858.   printfs("\\h'|\\n[%1]u",
  859.       column_divide_reg(start_col));
  860.   if (double_vrule_on_left) {
  861.     prints(double_vrule_on_left == 1 ? "-" : "+");
  862.     prints(HALF_DOUBLE_LINE_SEP);
  863.   }
  864.   prints("'");
  865.   printfs("\\v'-" HALF_DOUBLE_LINE_SEP "'"
  866.       "\\s[\\n[" LINESIZE_REG "]]"
  867.       "\\D'l |\\n[%1]u",
  868.       column_divide_reg(end_col+1));
  869.   if (double_vrule_on_right)
  870.     prints("-" HALF_DOUBLE_LINE_SEP);
  871.   prints(" 0'");
  872.   printfs("\\v'" DOUBLE_LINE_SEP "'"
  873.       "\\D'l |\\n[%1]u",
  874.       column_divide_reg(start_col));
  875.   if (double_vrule_on_right) {
  876.     prints(double_vrule_on_left == 1 ? "+" : "-");
  877.     prints(HALF_DOUBLE_LINE_SEP);
  878.   }
  879.   prints(" 0'");
  880.   prints("\\s0"
  881.      "\\v'-" HALF_DOUBLE_LINE_SEP "'");
  882.   if (!dont_move)
  883.     prints("\\v'" BAR_HEIGHT "'");
  884. }
  885.  
  886. double_line_entry *double_line_entry::to_double_line_entry()
  887. {
  888.   return this;
  889. }
  890.  
  891. short_line_entry::short_line_entry(const entry_modifier *m)
  892. : simple_entry(m)
  893. {
  894. }
  895.  
  896. int short_line_entry::line_type()
  897. {
  898.   return 1;
  899. }
  900.  
  901. void short_line_entry::simple_print(int dont_move)
  902. {
  903.   if (mod->stagger)
  904.     prints("\\v'-.5v'");
  905.   if (!dont_move)
  906.     prints("\\v'-" BAR_HEIGHT "'");
  907.   printfs("\\h'|\\n[%1]u'", column_start_reg(start_col));
  908.   printfs("\\s[\\n[" LINESIZE_REG "]]"
  909.       "\\D'l \\n[%1]u 0'"
  910.       "\\s0",
  911.       span_width_reg(start_col, end_col));
  912.   if (!dont_move)
  913.     prints("\\v'" BAR_HEIGHT "'");
  914.   if (mod->stagger)
  915.     prints("\\v'.5v'");
  916. }
  917.  
  918. short_double_line_entry::short_double_line_entry(const entry_modifier *m)
  919. : simple_entry(m)
  920. {
  921. }
  922.  
  923. int short_double_line_entry::line_type()
  924. {
  925.   return 2;
  926. }
  927.  
  928. void short_double_line_entry::simple_print(int dont_move)
  929. {
  930.   if (mod->stagger)
  931.     prints("\\v'-.5v'");
  932.   if (!dont_move)
  933.     prints("\\v'-" BAR_HEIGHT "'");
  934.   printfs("\\h'|\\n[%2]u'"
  935.       "\\v'-" HALF_DOUBLE_LINE_SEP "'"
  936.       "\\s[\\n[" LINESIZE_REG "]]"
  937.       "\\D'l \\n[%1]u 0'"
  938.       "\\v'" DOUBLE_LINE_SEP "'"
  939.       "\\D'l |\\n[%2]u 0'"
  940.       "\\s0"
  941.       "\\v'-" HALF_DOUBLE_LINE_SEP "'",
  942.       span_width_reg(start_col, end_col),
  943.       column_start_reg(start_col));
  944.   if (!dont_move)
  945.     prints("\\v'" BAR_HEIGHT "'");
  946.   if (mod->stagger)
  947.     prints("\\v'.5v'");
  948. }
  949.  
  950. void set_modifier(const entry_modifier *m)
  951. {
  952.   if (!m->font.empty())
  953.     printfs(".ft %1\n", m->font);
  954.   if (m->point_size.val != 0) {
  955.     prints(".ps ");
  956.     if (m->point_size.inc > 0)
  957.       prints('+');
  958.     else if (m->point_size.inc < 0)
  959.       prints('-');
  960.     printfs("%1\n", as_string(m->point_size.val));
  961.   }
  962.   if (m->vertical_spacing.val != 0) {
  963.     prints(".vs ");
  964.     if (m->vertical_spacing.inc > 0)
  965.       prints('+');
  966.     else if (m->vertical_spacing.inc < 0)
  967.       prints('-');
  968.     printfs("%1\n", as_string(m->vertical_spacing.val));
  969.   }
  970. }
  971.  
  972. void set_inline_modifier(const entry_modifier *m)
  973. {
  974.   if (!m->font.empty())
  975.     printfs("\\f[%1]", m->font);
  976.   if (m->point_size.val != 0) {
  977.     prints("\\s[");
  978.     if (m->point_size.inc > 0)
  979.       prints('+');
  980.     else if (m->point_size.inc < 0)
  981.       prints('-');
  982.     printfs("%1]", as_string(m->point_size.val));
  983.   }
  984.   if (m->stagger)
  985.     prints("\\v'-.5v'");
  986. }
  987.  
  988. void restore_inline_modifier(const entry_modifier *m)
  989. {
  990.   if (!m->font.empty())
  991.     prints("\\f[\\n[" SAVED_FONT_REG "]]");
  992.   if (m->point_size.val != 0)
  993.     prints("\\s[\\n[" SAVED_SIZE_REG "]]");
  994.   if (m->stagger)
  995.     prints("\\v'.5v'");
  996. }
  997.  
  998.  
  999. struct stuff {
  1000.   stuff *next;
  1001.   int row;            // occurs before row `row'
  1002.   char printed;            // has it been printed?
  1003.  
  1004.   stuff(int);
  1005.   virtual void print(table *) = 0;
  1006.   virtual ~stuff();
  1007.   virtual int is_single_line() { return 0; };
  1008.   virtual int is_double_line() { return 0; };
  1009. };
  1010.  
  1011. stuff::stuff(int r) : row(r), next(0), printed(0)
  1012. {
  1013. }
  1014.  
  1015. stuff::~stuff()
  1016. {
  1017. }
  1018.  
  1019. struct text_stuff : public stuff {
  1020.   string contents;
  1021.   const char *filename;
  1022.   int lineno;
  1023.  
  1024.   text_stuff(const string &, int r, const char *fn, int ln);
  1025.   ~text_stuff();
  1026.   void print(table *);
  1027. };
  1028.  
  1029.  
  1030. text_stuff::text_stuff(const string &s, int r, const char *fn, int ln)
  1031. : contents(s), stuff(r), filename(fn), lineno(ln)
  1032. {
  1033. }
  1034.  
  1035. text_stuff::~text_stuff()
  1036. {
  1037. }
  1038.  
  1039. void text_stuff::print(table *)
  1040. {
  1041.   printed = 1;
  1042.   prints(".cp \\n(" COMPATIBLE_REG "\n");
  1043.   set_troff_location(filename, lineno);
  1044.   prints(contents);
  1045.   prints(".cp 0\n");
  1046.   location_force_filename = 1;    // it might have been a .lf command
  1047. }
  1048.  
  1049. struct single_hline_stuff : public stuff {
  1050.   single_hline_stuff(int r);
  1051.   void print(table *);
  1052.   int is_single_line();
  1053. };
  1054.  
  1055. single_hline_stuff::single_hline_stuff(int r) : stuff(r)
  1056. {
  1057. }
  1058.  
  1059. void single_hline_stuff::print(table *tbl)
  1060. {
  1061.   printed = 1;
  1062.   tbl->print_single_hline(row);
  1063. }
  1064.  
  1065. int single_hline_stuff::is_single_line()
  1066. {
  1067.   return 1;
  1068. }
  1069.  
  1070. struct double_hline_stuff : stuff {
  1071.   double_hline_stuff(int r);
  1072.   void print(table *);
  1073.   int is_double_line();
  1074. };
  1075.  
  1076. double_hline_stuff::double_hline_stuff(int r) : stuff(r)
  1077. {
  1078. }
  1079.  
  1080. void double_hline_stuff::print(table *tbl)
  1081. {
  1082.   printed = 1;
  1083.   tbl->print_double_hline(row);
  1084. }
  1085.  
  1086. int double_hline_stuff::is_double_line()
  1087. {
  1088.   return 1;
  1089. }
  1090.  
  1091. struct vertical_rule {
  1092.   vertical_rule *next;
  1093.   short start_row;
  1094.   short end_row;
  1095.   short col;
  1096.   char is_double;
  1097.   string top_adjust;
  1098.   string bot_adjust;
  1099.  
  1100.   vertical_rule(int sr, int er, int c, int dbl, vertical_rule *);
  1101.   ~vertical_rule();
  1102.   void contribute_to_bottom_macro(table *);
  1103.   void print();
  1104. };
  1105.  
  1106. vertical_rule::vertical_rule(int sr, int er, int c, int dbl, vertical_rule *p)
  1107. : start_row(sr), end_row(er), col(c), is_double(dbl), next(p)
  1108. {
  1109. }
  1110.  
  1111. vertical_rule::~vertical_rule()
  1112. {
  1113. }
  1114.  
  1115. void vertical_rule::contribute_to_bottom_macro(table *tbl)
  1116. {
  1117.   printfs(".if \\n[" CURRENT_ROW_REG "]>=%1",
  1118.       as_string(start_row));
  1119.   if (end_row != tbl->get_nrows() - 1)
  1120.     printfs("&(\\n[" CURRENT_ROW_REG "]<%1)",
  1121.         as_string(end_row));
  1122.   prints(" \\{");
  1123.   printfs(".if %1<=\\n[" LAST_PASSED_ROW_REG "] .nr %2 \\n[#T]\n",
  1124.       as_string(start_row),
  1125.       row_top_reg(start_row));
  1126.   const char *offset_table[3];
  1127.   if (is_double) {
  1128.     offset_table[0] = "-" HALF_DOUBLE_LINE_SEP;
  1129.     offset_table[1] = "+" HALF_DOUBLE_LINE_SEP;
  1130.     offset_table[2] = 0;
  1131.   }
  1132.   else {
  1133.     offset_table[0] = "";
  1134.     offset_table[1] = 0;
  1135.   }
  1136.   for (const char **offsetp = offset_table; *offsetp; offsetp++) {
  1137.     prints(".sp -1\n"
  1138.        "\\v'" BODY_DEPTH);
  1139.     if (!bot_adjust.empty())
  1140.       printfs("+%1", bot_adjust);
  1141.     prints("'");
  1142.     printfs("\\h'\\n[%1]u%3'\\s[\\n[" LINESIZE_REG "]]\\D'l 0 |\\n[%2]u-1v",
  1143.         column_divide_reg(col),
  1144.         row_top_reg(start_row),
  1145.         *offsetp);
  1146.     if (!bot_adjust.empty())
  1147.       printfs("-(%1)", bot_adjust);
  1148.     // don't perform the top adjustment if the top is actually #T
  1149.     if (!top_adjust.empty())
  1150.       printfs("+((%1)*(%2>\\n[" LAST_PASSED_ROW_REG "]))",
  1151.           top_adjust,
  1152.           as_string(start_row));
  1153.     prints("'\\s0\n");
  1154.   }
  1155.   prints(".\\}\n");
  1156. }
  1157.  
  1158. void vertical_rule::print()
  1159. {
  1160.   printfs("\\*[" TRANSPARENT_STRING_NAME "]"
  1161.       ".if %1<=\\*[" QUOTE_STRING_NAME "]\\n[" LAST_PASSED_ROW_REG "] "
  1162.       ".nr %2 \\*[" QUOTE_STRING_NAME "]\\n[#T]\n",
  1163.       as_string(start_row),
  1164.       row_top_reg(start_row));
  1165.   const char *offset_table[3];
  1166.   if (is_double) {
  1167.     offset_table[0] = "-" HALF_DOUBLE_LINE_SEP;
  1168.     offset_table[1] = "+" HALF_DOUBLE_LINE_SEP;
  1169.     offset_table[2] = 0;
  1170.   }
  1171.   else {
  1172.     offset_table[0] = "";
  1173.     offset_table[1] = 0;
  1174.   }
  1175.   for (const char **offsetp = offset_table; *offsetp; offsetp++) {
  1176.     prints("\\*[" TRANSPARENT_STRING_NAME "].sp -1\n"
  1177.        "\\*[" TRANSPARENT_STRING_NAME "]\\v'" BODY_DEPTH);
  1178.     if (!bot_adjust.empty())
  1179.       printfs("+%1", bot_adjust);
  1180.     prints("'");
  1181.     printfs("\\h'\\n[%1]u%3'"
  1182.         "\\s[\\n[" LINESIZE_REG "]]"
  1183.         "\\D'l 0 |\\*[" QUOTE_STRING_NAME "]\\n[%2]u-1v",
  1184.         column_divide_reg(col),
  1185.         row_top_reg(start_row),
  1186.         *offsetp);
  1187.     if (!bot_adjust.empty())
  1188.       printfs("-(%1)", bot_adjust);
  1189.     // don't perform the top adjustment if the top is actually #T
  1190.     if (!top_adjust.empty())
  1191.       printfs("+((%1)*(%2>\\*[" QUOTE_STRING_NAME "]\\n["
  1192.           LAST_PASSED_ROW_REG "]))",
  1193.           top_adjust,
  1194.           as_string(start_row));
  1195.     prints("'"
  1196.        "\\s0\n");
  1197.   }
  1198. }
  1199.  
  1200. table::table(int nc, unsigned f, int ls, char dpc)
  1201. : ncolumns(nc), flags(f), linesize(ls), decimal_point_char(dpc),
  1202.   nrows(0), allocated_rows(0), entry(0), entry_list(0),
  1203.   entry_list_tailp(&entry_list),
  1204.   left_separation(0), right_separation(0), stuff_list(0), vline(0),
  1205.   vrule_list(0), row_is_all_lines(0), span_list(0)
  1206. {
  1207.   minimum_width = new string[ncolumns];
  1208.   column_separation = ncolumns > 1 ? new int[ncolumns - 1] : 0;
  1209.   equal = new char[ncolumns];
  1210.   int i;
  1211.   for (i = 0; i < ncolumns; i++)
  1212.     equal[i] = 0;
  1213.   for (i = 0; i < ncolumns-1; i++)
  1214.     column_separation[i] = DEFAULT_COLUMN_SEPARATION;
  1215.   delim[0] = delim[1] = '\0';
  1216. }
  1217.  
  1218. table::~table()
  1219. {
  1220.   for (int i = 0; i < nrows; i++) {
  1221.     a_delete entry[i];
  1222.     a_delete vline[i];
  1223.   }
  1224.   a_delete entry;
  1225.   a_delete vline;
  1226.   while (entry_list) {
  1227.     table_entry *tem = entry_list;
  1228.     entry_list = entry_list->next;
  1229.     delete tem;
  1230.   }
  1231.   ad_delete(ncolumns) minimum_width;
  1232.   a_delete column_separation;
  1233.   a_delete equal;
  1234.   while (stuff_list) {
  1235.     stuff *tem = stuff_list;
  1236.     stuff_list = stuff_list->next;
  1237.     delete tem;
  1238.   }
  1239.   while (vrule_list) {
  1240.     vertical_rule *tem = vrule_list;
  1241.     vrule_list = vrule_list->next;
  1242.     delete tem;
  1243.   }
  1244.   a_delete row_is_all_lines;
  1245.   while (span_list) {
  1246.     horizontal_span *tem = span_list;
  1247.     span_list = span_list->next;
  1248.     delete tem;
  1249.   }
  1250. }
  1251.  
  1252. void table::set_delim(char c1, char c2)
  1253. {
  1254.   delim[0] = c1;
  1255.   delim[1] = c2;
  1256. }
  1257.  
  1258. void table::set_minimum_width(int c, const string &w)
  1259. {
  1260.   assert(c >= 0 && c < ncolumns);
  1261.   minimum_width[c] = w;
  1262. }
  1263.  
  1264. void table::set_column_separation(int c, int n)
  1265. {
  1266.   assert(c >= 0 && c < ncolumns - 1);
  1267.   column_separation[c] = n;
  1268. }
  1269.  
  1270. void table::set_equal_column(int c)
  1271. {
  1272.   assert(c >= 0 && c < ncolumns);
  1273.   equal[c] = 1;
  1274. }
  1275.  
  1276. void table::add_stuff(stuff *p)
  1277. {
  1278.   for (stuff **pp = &stuff_list; *pp; pp = &(*pp)->next)
  1279.     ;
  1280.   *pp = p;
  1281. }
  1282.  
  1283. void table::add_text_line(int r, const string &s, const char *filename, int lineno)
  1284. {
  1285.   add_stuff(new text_stuff(s, r, filename, lineno));
  1286. }
  1287.  
  1288. void table::add_single_hline(int r)
  1289. {
  1290.   add_stuff(new single_hline_stuff(r));
  1291. }
  1292.  
  1293. void table::add_double_hline(int r)
  1294. {
  1295.   add_stuff(new double_hline_stuff(r));
  1296. }
  1297.  
  1298. void table::allocate(int r)
  1299. {
  1300.   if (r >= nrows) {
  1301.     typedef table_entry **PPtable_entry; // work around g++ 1.36.1 bug
  1302.     if (r >= allocated_rows) {
  1303.       if (allocated_rows == 0) {
  1304.     allocated_rows = 16;
  1305.     if (allocated_rows <= r)
  1306.       allocated_rows = r + 1;
  1307.     entry = new PPtable_entry[allocated_rows];
  1308.     vline = new char*[allocated_rows];
  1309.       }
  1310.       else {
  1311.     table_entry ***old_entry = entry;
  1312.     int old_allocated_rows = allocated_rows;
  1313.     allocated_rows *= 2;
  1314.     if (allocated_rows <= r)
  1315.       allocated_rows = r + 1;
  1316.     entry = new PPtable_entry[allocated_rows];
  1317.     memcpy(entry, old_entry, sizeof(table_entry**)*old_allocated_rows);
  1318.     a_delete old_entry;
  1319.     char **old_vline = vline;
  1320.     vline = new char*[allocated_rows];
  1321.     memcpy(vline, old_vline, sizeof(char*)*old_allocated_rows);
  1322.     a_delete old_vline;
  1323.       }
  1324.     }
  1325.     assert(allocated_rows > r);
  1326.     while (nrows <= r) {
  1327.       entry[nrows] = new table_entry*[ncolumns];
  1328.       for (int i = 0; i < ncolumns; i++)
  1329.     entry[nrows][i] = 0;
  1330.       vline[nrows] = new char[ncolumns+1];
  1331.       for (i = 0; i < ncolumns+1; i++)
  1332.     vline[nrows][i] = 0;
  1333.       nrows++;
  1334.     }
  1335.   }
  1336. }
  1337.  
  1338. void table::do_hspan(int r, int c)
  1339. {
  1340.   assert(r >= 0 && c >= 0 && r < nrows && c < ncolumns);
  1341.   if (c == 0) {
  1342.     error("first column cannot be horizontally spanned");
  1343.     return;
  1344.   }
  1345.   table_entry *e = entry[r][c];
  1346.   if (e) {
  1347.     assert(e->start_row <= r && r <= e->end_row
  1348.        && e->start_col <= c && c <= e->end_col
  1349.        && e->end_row - e->start_row > 0
  1350.        && e->end_col - e->start_col > 0);
  1351.     return;
  1352.   }
  1353.   e = entry[r][c-1];
  1354.   // e can be 0 if we had an empty entry or an error
  1355.   if (e == 0)
  1356.     return;
  1357.   if (e->start_row != r) {
  1358.     /*
  1359.       l l
  1360.       ^ s */
  1361.     error("impossible horizontal span at row %1, column %2", r + 1, c + 1);
  1362.   }
  1363.   else {
  1364.     e->end_col = c;
  1365.     entry[r][c] = e;
  1366.   }
  1367. }
  1368.  
  1369. void table::do_vspan(int r, int c)
  1370. {
  1371.   assert(r >= 0 && c >= 0 && r < nrows && c < ncolumns);
  1372.   if (r == 0) {
  1373.     error("first row cannot be vertically spanned");
  1374.     return;
  1375.   }
  1376.   table_entry *e = entry[r][c];
  1377.   if (e) {
  1378.     assert(e->start_row <= r && r <= e->end_row
  1379.        && e->start_col <= c && c <= e->end_col
  1380.        && e->end_row - e->start_row > 0
  1381.        && e->end_col - e->start_col > 0);
  1382.     return;
  1383.   }
  1384.   e = entry[r-1][c];
  1385.   // e can be 0 if we had an empty entry or an error
  1386.   if (e == 0)
  1387.     return;
  1388.   if (e->start_col != c) {
  1389.     /* l s
  1390.        l ^ */
  1391.     error("impossible vertical span at row %1, column %2", r + 1, c + 1);
  1392.   }
  1393.   else {
  1394.     for (int i = c; i <= e->end_col; i++) {
  1395.       assert(entry[r][i] == 0);
  1396.       entry[r][i] = e;
  1397.     }
  1398.     e->end_row = r;
  1399.   }
  1400. }
  1401.  
  1402. int find_decimal_point(const char *s, char decimal_point_char,
  1403.                const char *delim)
  1404. {
  1405.   if (s == 0 || *s == '\0')
  1406.     return -1;
  1407.   const char *p;
  1408.   int in_delim = 0;        // is p within eqn delimiters?
  1409.   // tbl recognises \& even within eqn delimiters; I don't
  1410.   for (p = s; *p; p++)
  1411.     if (in_delim) {
  1412.       if (*p == delim[1])
  1413.     in_delim = 0;
  1414.     }
  1415.     else if (*p == delim[0])
  1416.       in_delim = 1;
  1417.     else if (p[0] == '\\' && p[1] == '&')
  1418.       return p - s;
  1419.   int possible_pos = -1;
  1420.   in_delim = 0;
  1421.   for (p = s; *p; p++)
  1422.     if (in_delim) {
  1423.       if (*p == delim[1])
  1424.     in_delim = 0;
  1425.     }
  1426.     else if (*p == delim[0])
  1427.       in_delim = 1;
  1428.     else if (p[0] == decimal_point_char && csdigit(p[1]))
  1429.       possible_pos = p - s;
  1430.   if (possible_pos >= 0)
  1431.     return possible_pos;
  1432.   in_delim = 0;
  1433.   for (p = s; *p; p++)
  1434.     if (in_delim) {
  1435.       if (*p == delim[1])
  1436.     in_delim = 0;
  1437.     }
  1438.     else if (*p == delim[0])
  1439.       in_delim = 1;
  1440.     else if (csdigit(*p))
  1441.       possible_pos = p + 1 - s;
  1442.   return possible_pos;
  1443. }
  1444.  
  1445. void table::add_entry(int r, int c, const string &str, const entry_format *f,
  1446.               const char *fn, int ln)
  1447. {
  1448.   allocate(r);
  1449.   table_entry *e = 0;
  1450.   if (str == "\\_") {
  1451.     e = new short_line_entry(f);
  1452.   }
  1453.   else if (str == "\\=") {
  1454.     e = new short_double_line_entry(f);
  1455.   }
  1456.   else if (str == "_") {
  1457.     single_line_entry *lefte;
  1458.     if (c > 0 && entry[r][c-1] != 0 &&
  1459.     (lefte = entry[r][c-1]->to_single_line_entry()) != 0
  1460.     && lefte->start_row == r
  1461.     && lefte->mod->stagger == f->stagger) {
  1462.       lefte->end_col = c;
  1463.       entry[r][c] = lefte;
  1464.     }
  1465.     else
  1466.       e = new single_line_entry(f);
  1467.   }
  1468.   else if (str == "=") {
  1469.     double_line_entry *lefte;
  1470.     if (c > 0 && entry[r][c-1] != 0 &&
  1471.     (lefte = entry[r][c-1]->to_double_line_entry()) != 0
  1472.     && lefte->start_row == r
  1473.     && lefte->mod->stagger == f->stagger) {
  1474.       lefte->end_col = c;
  1475.       entry[r][c] = lefte;
  1476.     }
  1477.     else
  1478.       e = new double_line_entry(f);
  1479.   }
  1480.   else if (str == "\\^") {
  1481.     do_vspan(r, c);
  1482.   }
  1483.   else if (str.length() > 2 && str[0] == '\\' && str[1] == 'R') {
  1484.     if (str.search('\n') >= 0)
  1485.       error_with_file_and_line(fn, ln, "bad repeated character");
  1486.     else {
  1487.       char *s = str.substring(2, str.length() - 2).extract();
  1488.       e = new repeated_char_entry(s, f);
  1489.     }
  1490.   }
  1491.   else {
  1492.     int is_block = str.search('\n') >= 0;
  1493.     char *s;
  1494.     switch (f->type) {
  1495.     case FORMAT_SPAN:
  1496.       assert(str.empty());
  1497.       do_hspan(r, c);
  1498.       break;
  1499.     case FORMAT_LEFT:
  1500.       if (!str.empty()) {
  1501.     s = str.extract();
  1502.     if (is_block)
  1503.       e = new left_block_entry(s, f);
  1504.     else
  1505.       e = new left_text_entry(s, f);
  1506.       }
  1507.       else
  1508.     e = new empty_entry(f);
  1509.       break;
  1510.     case FORMAT_CENTER:
  1511.       if (!str.empty()) {
  1512.     s = str.extract();
  1513.     if (is_block)
  1514.       e = new center_block_entry(s, f);
  1515.     else
  1516.       e = new center_text_entry(s, f);
  1517.       }
  1518.       else
  1519.     e = new empty_entry(f);
  1520.       break;
  1521.     case FORMAT_RIGHT:
  1522.       if (!str.empty()) {
  1523.     s = str.extract();
  1524.     if (is_block)
  1525.       e = new right_block_entry(s, f);
  1526.     else
  1527.       e = new right_text_entry(s, f);
  1528.       }
  1529.       else
  1530.     e = new empty_entry(f);
  1531.       break;
  1532.     case FORMAT_NUMERIC:
  1533.       if (!str.empty()) {
  1534.     s = str.extract();
  1535.     if (is_block) {
  1536.       error_with_file_and_line(fn, ln, "can't have numeric text block");
  1537.       e = new left_block_entry(s, f);
  1538.     }
  1539.     else {
  1540.       int pos = find_decimal_point(s, decimal_point_char, delim);
  1541.       if (pos < 0)
  1542.         e = new center_text_entry(s, f);
  1543.       else
  1544.         e = new numeric_text_entry(s, f, pos);
  1545.     }
  1546.       }
  1547.       else
  1548.     e = new empty_entry(f);
  1549.       break;
  1550.     case FORMAT_ALPHABETIC:
  1551.       if (!str.empty()) {
  1552.     s = str.extract();
  1553.     if (is_block)
  1554.       e = new alphabetic_block_entry(s, f);
  1555.     else
  1556.       e = new alphabetic_text_entry(s, f);
  1557.       }
  1558.       else
  1559.     e = new empty_entry(f);
  1560.       break;
  1561.     case FORMAT_VSPAN:
  1562.       do_vspan(r, c);
  1563.       break;
  1564.     case FORMAT_HLINE:
  1565.       if (str.length() != 0)
  1566.     error_with_file_and_line(fn, ln,
  1567.                  "non-empty data entry for `_' format ignored");
  1568.       e = new single_line_entry(f);
  1569.       break;
  1570.     case FORMAT_DOUBLE_HLINE:
  1571.       if (str.length() != 0)
  1572.     error_with_file_and_line(fn, ln,
  1573.                  "non-empty data entry for `=' format ignored");
  1574.       e = new double_line_entry(f);
  1575.       break;
  1576.     default:
  1577.       assert(0);
  1578.     }
  1579.   }
  1580.   if (e) {
  1581.     table_entry *preve = entry[r][c];
  1582.     if (preve) {
  1583.       /* c s
  1584.          ^ l */
  1585.       error_with_file_and_line(fn, ln, "row %1, column %2 already spanned",
  1586.                    r + 1, c + 1);
  1587.       delete e;
  1588.     }
  1589.     else {
  1590.       e->input_lineno = ln;
  1591.       e->input_filename = fn;
  1592.       e->start_row = e->end_row = r;
  1593.       e->start_col = e->end_col = c;
  1594.       *entry_list_tailp = e;
  1595.       entry_list_tailp = &e->next;
  1596.       entry[r][c] = e;
  1597.     }
  1598.   }
  1599. }
  1600.  
  1601. // add vertical lines for row r
  1602.  
  1603. void table::add_vlines(int r, const char *v)
  1604. {
  1605.   allocate(r);
  1606.   for (int i = 0; i < ncolumns+1; i++)
  1607.     vline[r][i] = v[i];
  1608. }
  1609.  
  1610. void table::check()
  1611. {
  1612.   table_entry *p = entry_list;
  1613.   int i, j;
  1614.   while (p) {
  1615.     for (i = p->start_row; i <= p->end_row; i++)
  1616.       for (j = p->start_col; j <= p->end_col; j++)
  1617.     assert(entry[i][j] == p);
  1618.     p = p->next;
  1619.   }
  1620. }
  1621.  
  1622. void table::print()
  1623. {
  1624.   location_force_filename = 1;
  1625.   check();
  1626.   init_output();
  1627.   determine_row_type();
  1628.   compute_widths();
  1629.   if (!(flags & CENTER))
  1630.     prints(".if \\n[" SAVED_CENTER_REG "] \\{");
  1631.   prints(".in +(u;\\n[.l]-\\n[.i]-\\n[TW]/2)\n"
  1632.      ".nr " SAVED_INDENT_REG " \\n[.i]\n");
  1633.   if (!(flags & CENTER))
  1634.     prints(".\\}\n");
  1635.   build_vrule_list();
  1636.   define_bottom_macro();
  1637.   do_top();
  1638.   for (int i = 0; i < nrows; i++)
  1639.     do_row(i);
  1640.   do_bottom();
  1641. }
  1642.  
  1643. void table::determine_row_type()
  1644. {
  1645.   row_is_all_lines = new char[nrows];
  1646.   for (int i = 0; i < nrows; i++) {
  1647.     int had_single = 0;
  1648.     int had_double = 0;
  1649.     int had_non_line = 0;
  1650.     for (int c = 0; c < ncolumns; c++) {
  1651.       table_entry *e = entry[i][c];
  1652.       if (e != 0) {
  1653.     if (e->start_row == e->end_row) {
  1654.       int t = e->line_type();
  1655.       switch (t) {
  1656.       case -1:
  1657.         had_non_line = 1;
  1658.         break;
  1659.       case 0:
  1660.         // empty
  1661.         break;
  1662.       case 1:
  1663.         had_single = 1;
  1664.         break;
  1665.       case 2:
  1666.         had_double = 1;
  1667.         break;
  1668.       default:
  1669.         assert(0);
  1670.       }
  1671.       if (had_non_line)
  1672.         break;
  1673.     }
  1674.     c = e->end_col;
  1675.       }
  1676.     }
  1677.     if (had_non_line)
  1678.       row_is_all_lines[i] = 0;
  1679.     else if (had_double)
  1680.       row_is_all_lines[i] = 2;
  1681.     else if (had_single)
  1682.       row_is_all_lines[i] = 1;
  1683.     else
  1684.       row_is_all_lines[i] = 0;
  1685.   }
  1686. }
  1687.  
  1688.  
  1689. void table::init_output()
  1690. {
  1691.   prints(".nr " COMPATIBLE_REG " \\n(.C\n"
  1692.      ".cp 0\n");
  1693.   if (linesize > 0)
  1694.     printfs(".nr " LINESIZE_REG " %1\n", as_string(linesize));
  1695.   else
  1696.     prints(".nr " LINESIZE_REG " \\n[.s]\n");
  1697.   if (!(flags & CENTER))
  1698.     prints(".nr " SAVED_CENTER_REG " \\n[.ce]\n");
  1699.   prints(".de " RESET_MACRO_NAME "\n"
  1700.      ".ft \\n[.f]\n"
  1701.      ".ps \\n[.s]\n"
  1702.      ".vs \\n[.v]u\n"
  1703.      ".in \\n[.i]u\n"
  1704.      ".ll \\n[.l]u\n"
  1705.      ".ls \\n[.L]\n"
  1706.      ".ad \\n[.j]\n"
  1707.      ".ie \\n[.u] .fi\n"
  1708.      ".el .nf\n"
  1709.      ".ce \\n[.ce]\n"
  1710.      "..\n"
  1711.      ".nr " SAVED_INDENT_REG " \\n[.i]\n"
  1712.      ".nr " SAVED_FONT_REG " \\n[.f]\n"
  1713.      ".nr " SAVED_SIZE_REG " \\n[.s]\n"
  1714.      ".nr " SAVED_FILL_REG " \\n[.u]\n"
  1715.      ".nr T. 0\n"
  1716.      ".nr " CURRENT_ROW_REG " 0-1\n"
  1717.      ".nr " LAST_PASSED_ROW_REG " 0-1\n"
  1718.      ".nr " SECTION_DIVERSION_FLAG_REG " 0\n"
  1719.      ".ds " TRANSPARENT_STRING_NAME "\n"
  1720.      ".ds " QUOTE_STRING_NAME "\n"
  1721.      ".nr " NEED_BOTTOM_RULE_REG " 1\n"
  1722.      ".nr " SUPPRESS_BOTTOM_REG " 0\n"
  1723.      ".eo\n"
  1724.      ".de " REPEATED_MARK_MACRO "\n"
  1725.      ".mk \\$1\n"
  1726.      ".if !'\\n(.z'' \\!." REPEATED_MARK_MACRO " \"\\$1\"\n"
  1727.      "..\n"
  1728.      ".de " REPEATED_VPT_MACRO "\n"
  1729.      ".vpt \\$1\n"
  1730.      ".if !'\\n(.z'' \\!." REPEATED_VPT_MACRO " \"\\$1\"\n"
  1731.      "..\n");
  1732.   if (!(flags & NOKEEP))
  1733.     prints(".de " KEEP_MACRO_NAME "\n"
  1734.        ".if '\\n[.z]'' \\{.ds " QUOTE_STRING_NAME " \\\\\n"
  1735.        ".ds " TRANSPARENT_STRING_NAME " \\!\n"
  1736.        ".di " SECTION_DIVERSION_NAME "\n"
  1737.        ".nr " SECTION_DIVERSION_FLAG_REG " 1\n"
  1738.        ".in 0\n"
  1739.        ".\\}\n"
  1740.        "..\n"
  1741.        ".de " RELEASE_MACRO_NAME "\n"
  1742.        ".if \\n[" SECTION_DIVERSION_FLAG_REG "] \\{"
  1743.        ".di\n"
  1744.        ".in \\n[" SAVED_INDENT_REG "]u\n"
  1745.        ".nr " SAVED_DN_REG " \\n[dn]\n"
  1746.        ".ds " QUOTE_STRING_NAME "\n"
  1747.        ".ds " TRANSPARENT_STRING_NAME "\n"
  1748.        ".nr " SECTION_DIVERSION_FLAG_REG " 0\n"
  1749.        ".if \\n[.t]<=\\n[dn] \\{"
  1750.        ".nr T. 1\n"
  1751.        ".T#\n"
  1752.        ".nr " SUPPRESS_BOTTOM_REG " 1\n"
  1753.        ".sp \\n[.t]u\n"
  1754.        ".nr " SUPPRESS_BOTTOM_REG " 0\n"
  1755.        ".mk #T\n"
  1756.        ".\\}\n"
  1757.        ".if \\n[.t]<=\\n[" SAVED_DN_REG "] "
  1758.        /* Since we turn off traps, it won't get into an infinite loop
  1759.        when we try and print it; it will just go off the bottom of the
  1760.        page. */
  1761.        ".tm warning: page \\n%: table text block will not fit on one page\n"
  1762.        ".nf\n"
  1763.        ".ls 1\n"
  1764.        "." SECTION_DIVERSION_NAME "\n"
  1765.        ".ls\n"
  1766.        ".rm " SECTION_DIVERSION_NAME "\n"
  1767.        ".\\}\n"
  1768.        "..\n"
  1769.        ".nr " TABLE_DIVERSION_FLAG_REG " 0\n"
  1770.        ".de " TABLE_KEEP_MACRO_NAME "\n"
  1771.        ".if '\\n[.z]'' \\{"
  1772.        ".di " TABLE_DIVERSION_NAME "\n"
  1773.        ".nr " TABLE_DIVERSION_FLAG_REG " 1\n"
  1774.        ".\\}\n"
  1775.        "..\n"
  1776.        ".de " TABLE_RELEASE_MACRO_NAME "\n"
  1777.        ".if \\n[" TABLE_DIVERSION_FLAG_REG "] \\{.br\n"
  1778.        ".di\n"
  1779.        ".nr " SAVED_DN_REG " \\n[dn]\n"
  1780.        ".ne \\n[dn]u+\\n[.V]u\n"
  1781.        ".ie \\n[.t]<=\\n[" SAVED_DN_REG "] "
  1782.        ".tm error: page \\n%: table will not fit on one page; use .TS H/.TH with a supporting macro package\n"
  1783.        ".el \\{"
  1784.        ".in 0\n"
  1785.        ".ls 1\n"
  1786.        ".nf\n"
  1787.        "." TABLE_DIVERSION_NAME "\n"
  1788.        ".\\}\n"
  1789.        ".rm " TABLE_DIVERSION_NAME "\n"
  1790.        ".\\}\n"
  1791.        "..\n");
  1792.   prints(".ec\n"
  1793.      ".ce 0\n"
  1794.      ".nf\n");
  1795. }
  1796.  
  1797. string block_width_reg(int r, int c)
  1798. {
  1799.   static char name[sizeof(BLOCK_WIDTH_PREFIX)+INT_DIGITS+1+INT_DIGITS];
  1800.   sprintf(name, BLOCK_WIDTH_PREFIX "%d,%d", r, c);
  1801.   return string(name);
  1802. }
  1803.  
  1804. string block_diversion_name(int r, int c)
  1805. {
  1806.   static char name[sizeof(BLOCK_DIVERSION_PREFIX)+INT_DIGITS+1+INT_DIGITS];
  1807.   sprintf(name, BLOCK_DIVERSION_PREFIX "%d,%d", r, c);
  1808.   return string(name);
  1809. }
  1810.  
  1811. string block_height_reg(int r, int c)
  1812. {
  1813.   static char name[sizeof(BLOCK_HEIGHT_PREFIX)+INT_DIGITS+1+INT_DIGITS];
  1814.   sprintf(name, BLOCK_HEIGHT_PREFIX "%d,%d", r, c);
  1815.   return string(name);
  1816. }
  1817.  
  1818. string span_width_reg(int start_col, int end_col)
  1819. {
  1820.   static char name[sizeof(SPAN_WIDTH_PREFIX)+INT_DIGITS+1+INT_DIGITS];
  1821.   sprintf(name, SPAN_WIDTH_PREFIX "%d", start_col);
  1822.   if (end_col != start_col)
  1823.     sprintf(strchr(name, '\0'), ",%d", end_col);
  1824.   return string(name);
  1825. }
  1826.  
  1827. string span_left_numeric_width_reg(int start_col, int end_col)
  1828. {
  1829.   static char name[sizeof(SPAN_LEFT_NUMERIC_WIDTH_PREFIX)+INT_DIGITS+1+INT_DIGITS];
  1830.   sprintf(name, SPAN_LEFT_NUMERIC_WIDTH_PREFIX "%d", start_col);
  1831.   if (end_col != start_col)
  1832.     sprintf(strchr(name, '\0'), ",%d", end_col);
  1833.   return string(name);
  1834. }
  1835.  
  1836. string span_right_numeric_width_reg(int start_col, int end_col)
  1837. {
  1838.   static char name[sizeof(SPAN_RIGHT_NUMERIC_WIDTH_PREFIX)+INT_DIGITS+1+INT_DIGITS];
  1839.   sprintf(name, SPAN_RIGHT_NUMERIC_WIDTH_PREFIX "%d", start_col);
  1840.   if (end_col != start_col)
  1841.     sprintf(strchr(name, '\0'), ",%d", end_col);
  1842.   return string(name);
  1843. }
  1844.  
  1845. string span_alphabetic_width_reg(int start_col, int end_col)
  1846. {
  1847.   static char name[sizeof(SPAN_ALPHABETIC_WIDTH_PREFIX)+INT_DIGITS+1+INT_DIGITS];
  1848.   sprintf(name, SPAN_ALPHABETIC_WIDTH_PREFIX "%d", start_col);
  1849.   if (end_col != start_col)
  1850.     sprintf(strchr(name, '\0'), ",%d", end_col);
  1851.   return string(name);
  1852. }
  1853.  
  1854.  
  1855. string column_separation_reg(int col)
  1856. {
  1857.   static char name[sizeof(COLUMN_SEPARATION_PREFIX)+INT_DIGITS];
  1858.   sprintf(name, COLUMN_SEPARATION_PREFIX "%d", col);
  1859.   return string(name);
  1860. }
  1861.  
  1862. string row_start_reg(int row)
  1863. {
  1864.   static char name[sizeof(ROW_START_PREFIX)+INT_DIGITS];
  1865.   sprintf(name, ROW_START_PREFIX "%d", row);
  1866.   return string(name);
  1867. }  
  1868.  
  1869. string column_start_reg(int col)
  1870. {
  1871.   static char name[sizeof(COLUMN_START_PREFIX)+INT_DIGITS];
  1872.   sprintf(name, COLUMN_START_PREFIX "%d", col);
  1873.   return string(name);
  1874. }  
  1875.  
  1876. string column_end_reg(int col)
  1877. {
  1878.   static char name[sizeof(COLUMN_END_PREFIX)+INT_DIGITS];
  1879.   sprintf(name, COLUMN_END_PREFIX "%d", col);
  1880.   return string(name);
  1881. }
  1882.  
  1883. string column_divide_reg(int col)
  1884. {
  1885.   static char name[sizeof(COLUMN_DIVIDE_PREFIX)+INT_DIGITS];
  1886.   sprintf(name, COLUMN_DIVIDE_PREFIX "%d", col);
  1887.   return string(name);
  1888. }
  1889.  
  1890. string row_top_reg(int row)
  1891. {
  1892.   static char name[sizeof(ROW_TOP_PREFIX)+INT_DIGITS];
  1893.   sprintf(name, ROW_TOP_PREFIX "%d", row);
  1894.   return string(name);
  1895. }
  1896.  
  1897. void init_span_reg(int start_col, int end_col)
  1898. {
  1899.   printfs(".nr %1 \\n(.H\n.nr %2 0\n.nr %3 0\n.nr %4 0\n",
  1900.       span_width_reg(start_col, end_col),
  1901.       span_alphabetic_width_reg(start_col, end_col),
  1902.       span_left_numeric_width_reg(start_col, end_col),
  1903.       span_right_numeric_width_reg(start_col, end_col));
  1904. }
  1905.  
  1906. void compute_span_width(int start_col, int end_col)
  1907. {
  1908.   printfs(".nr %1 \\n[%1]>?(\\n[%2]+\\n[%3])\n"
  1909.       ".if \\n[%4] .nr %1 \\n[%1]>?(\\n[%4]+2n)\n", 
  1910.       span_width_reg(start_col, end_col),
  1911.       span_left_numeric_width_reg(start_col, end_col),
  1912.       span_right_numeric_width_reg(start_col, end_col),
  1913.       span_alphabetic_width_reg(start_col, end_col));
  1914.      
  1915. }
  1916.  
  1917. // Increase the widths of columns so that the width of any spanning entry
  1918. // is no greater than the sum of the widths of the columns that it spans.
  1919. // Ensure that the widths of columns remain equal.
  1920.  
  1921. void table::divide_span(int start_col, int end_col)
  1922. {
  1923.   assert(end_col > start_col);
  1924.   printfs(".nr " NEEDED_REG " \\n[%1]-(\\n[%2]", 
  1925.       span_width_reg(start_col, end_col),
  1926.       span_width_reg(start_col, start_col));
  1927.   for (int i = start_col + 1; i <= end_col; i++) {
  1928.     // The column separation may shrink with the expand option.
  1929.     if (!(flags & EXPAND))
  1930.       printfs("+%1n", as_string(column_separation[i - 1]));
  1931.     printfs("+\\n[%1]", span_width_reg(i, i));
  1932.   }
  1933.   prints(")\n");
  1934.   printfs(".nr " NEEDED_REG " \\n[" NEEDED_REG "]/%1\n",
  1935.       as_string(end_col - start_col + 1));
  1936.   prints(".if \\n[" NEEDED_REG "] \\{");
  1937.   for (i = start_col; i <= end_col; i++)
  1938.     printfs(".nr %1 +\\n[" NEEDED_REG "]\n", 
  1939.         span_width_reg(i, i));
  1940.   int equal_flag = 0;
  1941.   for (i = start_col; i <= end_col && !equal_flag; i++)
  1942.     if (equal[i])
  1943.       equal_flag = 1;
  1944.   if (equal_flag) {
  1945.     for (i = 0; i < ncolumns; i++)
  1946.       if (i < start_col || i > end_col)
  1947.     printfs(".nr %1 +\\n[" NEEDED_REG "]\n", 
  1948.         span_width_reg(i, i));
  1949.   }
  1950.   prints(".\\}\n");
  1951. }
  1952.  
  1953.  
  1954. void table::sum_columns(int start_col, int end_col)
  1955. {
  1956.   assert(end_col > start_col);
  1957.   printfs(".nr %1 \\n[%2]", 
  1958.       span_width_reg(start_col, end_col),
  1959.       span_width_reg(start_col, start_col));
  1960.   for (int i = start_col + 1; i <= end_col; i++)
  1961.     printfs("+(%1*\\n[" SEPARATION_FACTOR_REG "])+\\n[%2]",
  1962.         as_string(column_separation[i - 1]),
  1963.         span_width_reg(i, i));
  1964.   prints('\n');
  1965. }
  1966.  
  1967. horizontal_span::horizontal_span(int sc, int ec, horizontal_span *p)
  1968. : start_col(sc), end_col(ec), next(p)
  1969. {
  1970. }
  1971.  
  1972. void table::build_span_list()
  1973. {
  1974.   span_list = 0;
  1975.   table_entry *p = entry_list;
  1976.   while (p) {
  1977.     if (p->end_col != p->start_col) {
  1978.       for (horizontal_span *q = span_list; q; q = q->next)
  1979.     if (q->start_col == p->start_col
  1980.         && q->end_col == p->end_col)
  1981.       break;
  1982.       if (!q)
  1983.     span_list = new horizontal_span(p->start_col, p->end_col, span_list);
  1984.     }
  1985.     p = p->next;
  1986.   }
  1987.   // Now sort span_list primarily by order of end_row, and secondarily
  1988.   // by reverse order of start_row. This ensures that if we divide
  1989.   // spans using the order in span_list, we will get reasonable results.
  1990.   horizontal_span *unsorted = span_list;
  1991.   span_list = 0;
  1992.   while (unsorted) {
  1993.     for (horizontal_span **pp = &span_list; *pp; pp = &(*pp)->next)
  1994.       if (unsorted->end_col < (*pp)->end_col
  1995.       || (unsorted->end_col == (*pp)->end_col
  1996.           && (unsorted->start_col > (*pp)->start_col)))
  1997.     break;
  1998.     horizontal_span *tem = unsorted->next;
  1999.     unsorted->next = *pp;
  2000.     *pp = unsorted;
  2001.     unsorted = tem;
  2002.   }
  2003. }
  2004.  
  2005.  
  2006. void table::compute_separation_factor()
  2007. {
  2008.   if (flags & (ALLBOX|BOX|DOUBLEBOX))
  2009.     left_separation = right_separation = 1;
  2010.   else {
  2011.     for (int i = 0; i < nrows; i++) {
  2012.       if (vline[i][0] > 0)
  2013.     left_separation = 1;
  2014.       if (vline[i][ncolumns] > 0)
  2015.     right_separation = 1;
  2016.     }
  2017.   }
  2018.   if (flags & EXPAND) {
  2019.     int total_sep = left_separation + right_separation;
  2020.     for (int i = 0; i < ncolumns - 1; i++)
  2021.       total_sep += column_separation[i];
  2022.     if (total_sep != 0) {
  2023.       // Don't let the separation factor be negative.
  2024.       prints(".nr " SEPARATION_FACTOR_REG " \\n[.l]-\\n[.i]");
  2025.       for (i = 0; i < ncolumns; i++)
  2026.     printfs("-\\n[%1]", span_width_reg(i, i));
  2027.       printfs("/%1>?0\n", as_string(total_sep));
  2028.     }
  2029.   }
  2030. }
  2031.  
  2032. void table::compute_column_positions()
  2033. {
  2034.   printfs(".nr %1 0\n", column_divide_reg(0));
  2035.   printfs(".nr %1 %2*\\n[" SEPARATION_FACTOR_REG "]\n",
  2036.       column_start_reg(0),
  2037.       as_string(left_separation));
  2038.   for (int i = 1;; i++) {
  2039.     printfs(".nr %1 \\n[%2]+\\n[%3]\n",
  2040.         column_end_reg(i-1),
  2041.         column_start_reg(i-1),
  2042.         span_width_reg(i-1, i-1));
  2043.     if (i >= ncolumns)
  2044.       break;
  2045.     printfs(".nr %1 \\n[%2]+(%3*\\n[" SEPARATION_FACTOR_REG "])\n",
  2046.         column_start_reg(i),
  2047.         column_end_reg(i-1),
  2048.         as_string(column_separation[i-1]));
  2049.     printfs(".nr %1 \\n[%2]+\\n[%3]/2\n",
  2050.         column_divide_reg(i),
  2051.         column_end_reg(i-1),
  2052.         column_start_reg(i));
  2053.   }
  2054.   printfs(".nr %1 \\n[%2]+(%3*\\n[" SEPARATION_FACTOR_REG "])\n",
  2055.       column_divide_reg(ncolumns),
  2056.       column_end_reg(i-1),
  2057.       as_string(right_separation));
  2058.   printfs(".nr TW \\n[%1]\n",
  2059.       column_divide_reg(ncolumns));
  2060.   if (flags & DOUBLEBOX) {
  2061.     printfs(".nr %1 +" DOUBLE_LINE_SEP "\n", column_divide_reg(0));
  2062.     printfs(".nr %1 -" DOUBLE_LINE_SEP "\n", column_divide_reg(ncolumns));
  2063.   }
  2064. }
  2065.  
  2066. void table::make_columns_equal()
  2067. {
  2068.   int first = -1;        // index of first equal column
  2069.   for (int i = 0; i < ncolumns; i++)
  2070.     if (equal[i]) {
  2071.       if (first < 0) {
  2072.     printfs(".nr %1 \\n[%1]", span_width_reg(i, i));
  2073.     first = i;
  2074.       }
  2075.       else
  2076.     printfs(">?\\n[%1]", span_width_reg(i, i));
  2077.     }
  2078.   if (first >= 0) {
  2079.     prints('\n');
  2080.     for (i = first + 1; i < ncolumns; i++)
  2081.       if (equal[i])
  2082.     printfs(".nr %1 \\n[%2]\n", 
  2083.         span_width_reg(i, i),
  2084.         span_width_reg(first, first));
  2085.   }
  2086. }
  2087.  
  2088. void table::compute_widths()
  2089. {
  2090.   build_span_list();
  2091.   int i;
  2092.   horizontal_span *p;
  2093.   prints(".nr " SEPARATION_FACTOR_REG " 1n\n");
  2094.   for (i = 0; i < ncolumns; i++) {
  2095.     init_span_reg(i, i);
  2096.     if (!minimum_width[i].empty())
  2097.       printfs(".nr %1 %2\n", span_width_reg(i, i), minimum_width[i]);
  2098.   }
  2099.   for (p = span_list; p; p = p->next)
  2100.     init_span_reg(p->start_col, p->end_col);
  2101.   table_entry *q;
  2102.   for (q = entry_list; q; q = q->next)
  2103.     if (!q->mod->zero_width)
  2104.       q->do_width();
  2105.   for (i = 0; i < ncolumns; i++)
  2106.     compute_span_width(i, i);
  2107.   for (p = span_list; p; p = p->next)
  2108.     compute_span_width(p->start_col, p->end_col);
  2109.   make_columns_equal();
  2110.   // Note that divide_span keeps equal width columns equal.
  2111.   for (p = span_list; p; p = p->next)
  2112.     divide_span(p->start_col, p->end_col);
  2113.   for (p = span_list; p; p = p->next)
  2114.     sum_columns(p->start_col, p->end_col);
  2115.   int had_spanning_block = 0;
  2116.   int had_equal_block = 0;
  2117.   for (q = entry_list; q; q = q->next)
  2118.     if (q->divert(ncolumns, minimum_width,
  2119.           (flags & EXPAND) ? column_separation : 0)) {
  2120.       if (q->end_col > q->start_col)
  2121.     had_spanning_block = 1;
  2122.       for (i = q->start_col; i <= q->end_col && !had_equal_block; i++)
  2123.     if (equal[i])
  2124.       had_equal_block = 1;
  2125.     }
  2126.   if (had_equal_block)
  2127.     make_columns_equal();
  2128.   if (had_spanning_block)
  2129.     for (p = span_list; p; p = p->next)
  2130.       divide_span(p->start_col, p->end_col);
  2131.   compute_separation_factor();
  2132.   for (p = span_list; p; p = p->next)
  2133.     sum_columns(p->start_col, p->end_col);
  2134.   compute_column_positions();
  2135. }
  2136.  
  2137. void table::print_single_hline(int r)
  2138. {
  2139.   prints(".vs " LINE_SEP ">?\\n[.V]u\n"
  2140.      ".ls 1\n"
  2141.      "\\v'" BODY_DEPTH "'"
  2142.      "\\s[\\n[" LINESIZE_REG "]]");
  2143.   if (r > nrows - 1)
  2144.     prints("\\D'l |\\n[TW]u 0'");
  2145.   else {
  2146.     int start_col = 0;
  2147.     for (;;) {
  2148.       while (start_col < ncolumns 
  2149.          && entry[r][start_col] != 0
  2150.          && entry[r][start_col]->start_row != r)
  2151.     start_col++;
  2152.       for (int end_col = start_col;
  2153.        end_col < ncolumns
  2154.        && (entry[r][end_col] == 0
  2155.            || entry[r][end_col]->start_row == r);
  2156.        end_col++)
  2157.     ;
  2158.       if (end_col <= start_col)
  2159.     break;
  2160.       printfs("\\h'|\\n[%1]u",
  2161.           column_divide_reg(start_col));
  2162.       if ((r > 0 && vline[r-1][start_col] == 2)
  2163.       || (r < nrows && vline[r][start_col] == 2))
  2164.     prints("-" HALF_DOUBLE_LINE_SEP);
  2165.       prints("'");
  2166.       printfs("\\D'l |\\n[%1]u",
  2167.           column_divide_reg(end_col));
  2168.       if ((r > 0 && vline[r-1][end_col] == 2)
  2169.       || (r < nrows && vline[r][end_col] == 2))
  2170.     prints("+" HALF_DOUBLE_LINE_SEP);
  2171.       prints(" 0'");
  2172.       start_col = end_col;
  2173.     }
  2174.   }
  2175.   prints("\\s0\n");
  2176.   prints(".ls\n"
  2177.      ".vs\n");
  2178. }
  2179.  
  2180. void table::print_double_hline(int r)
  2181. {
  2182.   prints(".vs " LINE_SEP "+" DOUBLE_LINE_SEP
  2183.      ">?\\n[.V]u\n"
  2184.      ".ls 1\n"
  2185.      "\\v'" BODY_DEPTH "'"
  2186.      "\\s[\\n[" LINESIZE_REG "]]");
  2187.   if (r > nrows - 1)
  2188.     prints("\\v'-" DOUBLE_LINE_SEP "'"
  2189.        "\\D'l |\\n[TW]u 0'"
  2190.        "\\v'" DOUBLE_LINE_SEP "'"
  2191.        "\\h'|0'"
  2192.        "\\D'l |\\n[TW]u 0'");
  2193.   else {
  2194.     int start_col = 0;
  2195.     for (;;) {
  2196.       while (start_col < ncolumns 
  2197.          && entry[r][start_col] != 0
  2198.          && entry[r][start_col]->start_row != r)
  2199.     start_col++;
  2200.       for (int end_col = start_col;
  2201.        end_col < ncolumns
  2202.        && (entry[r][end_col] == 0
  2203.            || entry[r][end_col]->start_row == r);
  2204.        end_col++)
  2205.     ;
  2206.       if (end_col <= start_col)
  2207.     break;
  2208.       const char *left_adjust = 0;
  2209.       if ((r > 0 && vline[r-1][start_col] == 2)
  2210.       || (r < nrows && vline[r][start_col] == 2))
  2211.     left_adjust = "-" HALF_DOUBLE_LINE_SEP;
  2212.       const char *right_adjust = 0;
  2213.       if ((r > 0 && vline[r-1][end_col] == 2)
  2214.       || (r < nrows && vline[r][end_col] == 2))
  2215.     right_adjust = "+" HALF_DOUBLE_LINE_SEP;
  2216.       printfs("\\v'-" DOUBLE_LINE_SEP "'"
  2217.           "\\h'|\\n[%1]u",
  2218.           column_divide_reg(start_col));
  2219.       if (left_adjust)
  2220.     prints(left_adjust);
  2221.       prints("'");
  2222.       printfs("\\D'l |\\n[%1]u",
  2223.           column_divide_reg(end_col));
  2224.       if (right_adjust)
  2225.     prints(right_adjust);
  2226.       prints(" 0'");
  2227.       printfs("\\v'" DOUBLE_LINE_SEP "'"
  2228.           "\\h'|\\n[%1]u",
  2229.           column_divide_reg(start_col));
  2230.       if (left_adjust)
  2231.     prints(left_adjust);
  2232.       prints("'");
  2233.       printfs("\\D'l |\\n[%1]u",
  2234.           column_divide_reg(end_col));
  2235.       if (right_adjust)
  2236.     prints(right_adjust);
  2237.       prints(" 0'");
  2238.       start_col = end_col;
  2239.     }
  2240.   }
  2241.   prints("\\s0\n"
  2242.      ".ls\n"
  2243.      ".vs\n");
  2244. }
  2245.  
  2246. void table::compute_vrule_top_adjust(int start_row, int col, string &result)
  2247. {
  2248.   if (row_is_all_lines[start_row] && start_row < nrows - 1) {
  2249.     if (row_is_all_lines[start_row] == 2)
  2250.       result = LINE_SEP ">?\\n[.V]u" "+" DOUBLE_LINE_SEP;
  2251.     else
  2252.       result = LINE_SEP ">?\\n[.V]u";
  2253.     start_row++;
  2254.   }
  2255.   else {
  2256.     result = "";
  2257.     if (start_row == 0)
  2258.       return;
  2259.     for (stuff *p = stuff_list; p && p->row <= start_row; p = p->next)
  2260.       if (p->row == start_row 
  2261.       && (p->is_single_line() || p->is_double_line()))
  2262.     return;
  2263.   }
  2264.   int left = 0;
  2265.   if (col > 0) {
  2266.     table_entry *e = entry[start_row-1][col-1];
  2267.     if (e && e->start_row == e->end_row) {
  2268.       if (e->to_double_line_entry() != 0)
  2269.     left = 2;
  2270.       else if (e->to_single_line_entry() != 0)
  2271.     left = 1;
  2272.     }
  2273.   }
  2274.   int right = 0;
  2275.   if (col < ncolumns) {
  2276.     table_entry *e = entry[start_row-1][col];
  2277.     if (e && e->start_row == e->end_row) {
  2278.       if (e->to_double_line_entry() != 0)
  2279.     right = 2;
  2280.       else if (e->to_single_line_entry() != 0)
  2281.     right = 1;
  2282.     }
  2283.   }
  2284.   if (row_is_all_lines[start_row-1] == 0) {
  2285.     if (left > 0 || right > 0) {
  2286.       result += "-" BODY_DEPTH "-" BAR_HEIGHT;
  2287.       if ((left == 2 && right != 2) || (right == 2 && left != 2))
  2288.     result += "-" HALF_DOUBLE_LINE_SEP;
  2289.       else if (left == 2 && right == 2)
  2290.     result += "+" HALF_DOUBLE_LINE_SEP;
  2291.     }
  2292.   }
  2293.   else if (row_is_all_lines[start_row-1] == 2) {
  2294.     if ((left == 2 && right != 2) || (right == 2 && left != 2))
  2295.       result += "-" DOUBLE_LINE_SEP;
  2296.     else if (left == 1 || right == 1)
  2297.       result += "-" HALF_DOUBLE_LINE_SEP;
  2298.   }
  2299. }
  2300.  
  2301. void table::compute_vrule_bot_adjust(int end_row, int col, string &result)
  2302. {
  2303.   if (row_is_all_lines[end_row] && end_row > 0) {
  2304.     end_row--;
  2305.     result = "";
  2306.   }
  2307.   else {
  2308.     for (stuff *p = stuff_list; p && p->row < end_row + 1; p = p->next)
  2309.       ;
  2310.     if (p && p->row == end_row + 1 && p->is_double_line()) {
  2311.       result = "-" DOUBLE_LINE_SEP;
  2312.       return;
  2313.     }
  2314.     if ((p != 0 && p->row == end_row + 1)
  2315.     || end_row == nrows - 1) {
  2316.       result = "";
  2317.       return;
  2318.     }
  2319.     if (row_is_all_lines[end_row+1] == 1)
  2320.       result = LINE_SEP;
  2321.     else if (row_is_all_lines[end_row+1] == 2)
  2322.       result = LINE_SEP "+" DOUBLE_LINE_SEP;
  2323.     else
  2324.       result = "";
  2325.   }
  2326.   int left = 0;
  2327.   if (col > 0) {
  2328.     table_entry *e = entry[end_row+1][col-1];
  2329.     if (e && e->start_row == e->end_row) {
  2330.       if (e->to_double_line_entry() != 0)
  2331.     left = 2;
  2332.       else if (e->to_single_line_entry() != 0)
  2333.     left = 1;
  2334.     }
  2335.   }
  2336.   int right = 0;
  2337.   if (col < ncolumns) {
  2338.     table_entry *e = entry[end_row+1][col];
  2339.     if (e && e->start_row == e->end_row) {
  2340.       if (e->to_double_line_entry() != 0)
  2341.     right = 2;
  2342.       else if (e->to_single_line_entry() != 0)
  2343.     right = 1;
  2344.     }
  2345.   }
  2346.   if (row_is_all_lines[end_row+1] == 0) {
  2347.     if (left > 0 || right > 0) {
  2348.       result = "1v-" BODY_DEPTH "-" BAR_HEIGHT;
  2349.       if ((left == 2 && right != 2) || (right == 2 && left != 2))
  2350.     result += "+" HALF_DOUBLE_LINE_SEP;
  2351.       else if (left == 2 && right == 2)
  2352.     result += "-" HALF_DOUBLE_LINE_SEP;
  2353.     }
  2354.   }
  2355.   else if (row_is_all_lines[end_row+1] == 2) {
  2356.     if (left == 2 && right == 2)
  2357.       result += "-" DOUBLE_LINE_SEP;
  2358.     else if (left != 2 && right != 2 && (left == 1 || right == 1))
  2359.       result += "-" HALF_DOUBLE_LINE_SEP;
  2360.   }
  2361. }
  2362.  
  2363. void table::add_vertical_rule(int start_row, int end_row, int col, int is_double)
  2364. {
  2365.   vrule_list = new vertical_rule(start_row, end_row, col, is_double,
  2366.                  vrule_list);
  2367.   compute_vrule_top_adjust(start_row, col, vrule_list->top_adjust);
  2368.   compute_vrule_bot_adjust(end_row, col, vrule_list->bot_adjust);
  2369. }
  2370.  
  2371. void table::build_vrule_list()
  2372. {
  2373.   int col;
  2374.   if (flags & ALLBOX) {
  2375.     for (col = 1; col < ncolumns; col++) {
  2376.       int start_row = 0;
  2377.       for (;;) {
  2378.     while (start_row < nrows && vline_spanned(start_row, col))
  2379.       start_row++;
  2380.     if (start_row >= nrows)
  2381.       break;
  2382.     int end_row = start_row;
  2383.     while (end_row < nrows && !vline_spanned(end_row, col))
  2384.       end_row++;
  2385.     end_row--;
  2386.     add_vertical_rule(start_row, end_row, col, 0);
  2387.     start_row = end_row + 1;
  2388.       }
  2389.     }
  2390.   }
  2391.   if (flags & (BOX|ALLBOX|DOUBLEBOX)) {
  2392.     add_vertical_rule(0, nrows - 1, 0, 0);
  2393.     add_vertical_rule(0, nrows - 1, ncolumns, 0);
  2394.   }
  2395.   for (int end_row = 0; end_row < nrows; end_row++)
  2396.     for (col = 0; col < ncolumns+1; col++)
  2397.       if (vline[end_row][col] > 0
  2398.       && !vline_spanned(end_row, col)
  2399.       && (end_row == nrows - 1 
  2400.           || vline[end_row+1][col] != vline[end_row][col]
  2401.           || vline_spanned(end_row+1, col))) {
  2402.     for (int start_row = end_row - 1;
  2403.          start_row >= 0
  2404.          && vline[start_row][col] == vline[end_row][col]
  2405.          && !vline_spanned(start_row, col);
  2406.          start_row--)
  2407.       ;
  2408.     start_row++;
  2409.     add_vertical_rule(start_row, end_row, col, vline[end_row][col] > 1);
  2410.       }
  2411.   for (vertical_rule *p = vrule_list; p; p = p->next)
  2412.     if (p->is_double)
  2413.       for (int r = p->start_row; r <= p->end_row; r++) {
  2414.     if (p->col > 0 && entry[r][p->col-1] != 0
  2415.         && entry[r][p->col-1]->end_col == p->col-1) {
  2416.       int is_corner = r == p->start_row || r == p->end_row;
  2417.       entry[r][p->col-1]->note_double_vrule_on_right(is_corner);
  2418.     }
  2419.     if (p->col < ncolumns && entry[r][p->col] != 0
  2420.         && entry[r][p->col]->start_col == p->col) {
  2421.       int is_corner = r == p->start_row || r == p->end_row;
  2422.       entry[r][p->col]->note_double_vrule_on_left(is_corner);
  2423.     }
  2424.       }
  2425. }
  2426.  
  2427. void table::define_bottom_macro()
  2428. {
  2429.   prints(".eo\n"
  2430.      ".de T#\n"
  2431.      ".if !\\n[" SUPPRESS_BOTTOM_REG "] \\{"
  2432.      "." REPEATED_VPT_MACRO " 0\n"
  2433.      ".mk " SAVED_VERTICAL_POS_REG "\n");
  2434.   if (flags & (BOX|ALLBOX|DOUBLEBOX)) {
  2435.     prints(".if \\n[T.]&\\n[" NEED_BOTTOM_RULE_REG "] \\{");
  2436.     print_single_hline(0);
  2437.     prints(".\\}\n");
  2438.   }
  2439.   prints(".ls 1\n");
  2440.   for (vertical_rule *p = vrule_list; p; p = p->next)
  2441.     p->contribute_to_bottom_macro(this);
  2442.   if (flags & DOUBLEBOX)
  2443.     prints(".if \\n[T.] \\{.vs " DOUBLE_LINE_SEP ">?\\n[.V]u\n"
  2444.        "\\v'" BODY_DEPTH "'\\s[\\n[" LINESIZE_REG "]]"
  2445.        "\\D'l \\n[TW]u 0'\\s0\n"
  2446.        ".vs\n"
  2447.        ".\\}\n"
  2448.        ".if \\n[" LAST_PASSED_ROW_REG "]>=0 "
  2449.        ".nr " TOP_REG " \\n[#T]-" DOUBLE_LINE_SEP "\n"
  2450.        ".sp -1\n"
  2451.        "\\v'" BODY_DEPTH "'\\s[\\n[" LINESIZE_REG "]]"
  2452.        "\\D'l 0 |\\n[" TOP_REG "]u-1v'\\s0\n"
  2453.        ".sp -1\n"
  2454.        "\\v'" BODY_DEPTH "'\\h'|\\n[TW]u'\\s[\\n[" LINESIZE_REG "]]"
  2455.        "\\D'l 0 |\\n[" TOP_REG "]u-1v'\\s0\n");
  2456.   prints(".ls\n");
  2457.   prints(".nr " LAST_PASSED_ROW_REG " \\n[" CURRENT_ROW_REG "]\n"
  2458.      ".sp |\\n[" SAVED_VERTICAL_POS_REG "]u\n"
  2459.      "." REPEATED_VPT_MACRO " 1\n"
  2460.      ".\\}\n"
  2461.      "..\n"
  2462.      ".ec\n");
  2463. }
  2464.  
  2465.  
  2466. // is the vertical line before column c in row r horizontally spanned?
  2467.  
  2468. int table::vline_spanned(int r, int c)
  2469. {
  2470.   assert(r >= 0 && r < nrows && c >= 0 && c < ncolumns + 1);
  2471.   return (c != 0 && c != ncolumns && entry[r][c] != 0
  2472.       && entry[r][c]->start_col != c
  2473.       // horizontally spanning lines don't count
  2474.       && entry[r][c]->to_double_line_entry() == 0
  2475.       && entry[r][c]->to_single_line_entry() == 0);
  2476. }
  2477.  
  2478. int table::row_begins_section(int r)
  2479. {
  2480.   assert(r >= 0 && r < nrows);
  2481.   for (int i = 0; i < ncolumns; i++)
  2482.     if (entry[r][i] && entry[r][i]->start_row != r)
  2483.       return 0;
  2484.   return 1;
  2485. }
  2486.  
  2487. int table::row_ends_section(int r)
  2488. {
  2489.   assert(r >= 0 && r < nrows);
  2490.   for (int i = 0; i < ncolumns; i++)
  2491.     if (entry[r][i] && entry[r][i]->end_row != r)
  2492.       return 0;
  2493.   return 1;
  2494. }
  2495.  
  2496. void table::do_row(int r)
  2497. {
  2498.   if (!(flags & NOKEEP) && row_begins_section(r))
  2499.     prints("." KEEP_MACRO_NAME "\n");
  2500.   int had_line = 0;
  2501.   for (stuff *p = stuff_list; p && p->row < r; p = p->next)
  2502.     ;
  2503.   for (stuff *p1 = p; p1 && p1->row == r; p1 = p1->next)
  2504.     if (!p1->printed && (p1->is_single_line() || p1->is_double_line())) {
  2505.       had_line = 1;
  2506.       break;
  2507.     }
  2508.   if (!had_line && !row_is_all_lines[r])
  2509.     printfs("." REPEATED_MARK_MACRO " %1\n", row_top_reg(r));
  2510.   had_line = 0;
  2511.   for (; p && p->row == r; p = p->next)
  2512.     if (!p->printed) {
  2513.       p->print(this);
  2514.       if (!had_line && (p->is_single_line() || p->is_double_line())) {
  2515.     printfs("." REPEATED_MARK_MACRO " %1\n", row_top_reg(r));
  2516.     had_line = 1;
  2517.       }
  2518.     }
  2519.   // Change the row *after* printing the stuff list (which might contain .TH).
  2520.   printfs("\\*[" TRANSPARENT_STRING_NAME "].nr " CURRENT_ROW_REG " %1\n",
  2521.       as_string(r));
  2522.   if (!had_line && row_is_all_lines[r])
  2523.     printfs("." REPEATED_MARK_MACRO " %1\n", row_top_reg(r));
  2524.   // we might have had a .TH, for example,  since we last tried
  2525.   if (!(flags & NOKEEP) && row_begins_section(r))
  2526.     prints("." KEEP_MACRO_NAME "\n");
  2527.   printfs(".mk %1\n", row_start_reg(r));
  2528.   prints(".mk " BOTTOM_REG "\n"
  2529.      "." REPEATED_VPT_MACRO " 0\n");
  2530.   int c;
  2531.   int row_is_blank = 1;
  2532.   int first_start_row = r;
  2533.   for (c = 0; c < ncolumns; c++) {
  2534.     table_entry *e = entry[r][c];
  2535.     if (e) {
  2536.       if (e->end_row == r) {
  2537.     e->do_depth();
  2538.     if (e->start_row < first_start_row)
  2539.       first_start_row = e->start_row;
  2540.     row_is_blank = 0;
  2541.       }
  2542.       c = e->end_col;
  2543.     }
  2544.   }
  2545.   if (row_is_blank)
  2546.     prints(".nr " BOTTOM_REG " +1v\n");
  2547.   if (row_is_all_lines[r]) {
  2548.     prints(".vs " LINE_SEP);
  2549.     if (row_is_all_lines[r] == 2)
  2550.       prints("+" DOUBLE_LINE_SEP);
  2551.     prints(">?\\n[.V]u\n.ls 1\n");
  2552.     prints("\\&");
  2553.     prints("\\v'" BODY_DEPTH);
  2554.     if (row_is_all_lines[r] == 2)
  2555.       prints("-" HALF_DOUBLE_LINE_SEP);
  2556.     prints("'");
  2557.     for (c = 0; c < ncolumns; c++) {
  2558.       table_entry *e = entry[r][c];
  2559.       if (e) {
  2560.     if (e->end_row == e->start_row)
  2561.       e->to_simple_entry()->simple_print(1);
  2562.     c = e->end_col;
  2563.       }
  2564.     }
  2565.     prints("\n");
  2566.     prints(".ls\n"
  2567.        ".vs\n");
  2568.     prints(".nr " BOTTOM_REG " \\n[" BOTTOM_REG "]>?\\n[.d]\n");
  2569.     printfs(".sp |\\n[%1]u\n", row_start_reg(r));
  2570.   }
  2571.   for (int i = row_is_all_lines[r] ? r - 1 : r;
  2572.        i >= first_start_row;
  2573.        i--) {
  2574.     simple_entry *first = 0;
  2575.     for (c = 0; c < ncolumns; c++) {
  2576.       table_entry *e = entry[r][c];
  2577.       if (e) {
  2578.     if (e->end_row == r && e->start_row == i) {
  2579.       simple_entry *simple = e->to_simple_entry();
  2580.       if (simple) {
  2581.         if (!first) {
  2582.           prints(".ta");
  2583.           first = simple;
  2584.         }
  2585.         simple->add_tab();
  2586.       }
  2587.     }
  2588.     c = e->end_col;
  2589.       }
  2590.     }
  2591.     if (first) {
  2592.       prints('\n');
  2593.       first->position_vertically();
  2594.       first->set_location();
  2595.       prints("\\&");
  2596.       first->simple_print(0);
  2597.       for (c = first->end_col + 1; c < ncolumns; c++) {
  2598.     table_entry *e = entry[r][c];
  2599.     if (e) {
  2600.       if (e->end_row == r && e->start_row == i) {
  2601.         simple_entry *simple = e->to_simple_entry();
  2602.         if (simple)
  2603.           simple->simple_print(0);
  2604.       }
  2605.       c = e->end_col;
  2606.     }
  2607.       }
  2608.       prints('\n');
  2609.       prints(".nr " BOTTOM_REG " \\n[" BOTTOM_REG "]>?\\n[.d]\n");
  2610.       printfs(".sp |\\n[%1]u\n", row_start_reg(r));
  2611.     }
  2612.   }
  2613.   for (c = 0; c < ncolumns; c++) {
  2614.     table_entry *e = entry[r][c];
  2615.     if (e) {
  2616.       if (e->end_row == r && e->to_simple_entry() == 0) {
  2617.     e->position_vertically();
  2618.     e->print();
  2619.     prints(".nr " BOTTOM_REG " \\n[" BOTTOM_REG "]>?\\n[.d]\n");
  2620.     printfs(".sp |\\n[%1]u\n", row_start_reg(r));
  2621.       }
  2622.       c = e->end_col;
  2623.     }
  2624.   }
  2625.   prints("." REPEATED_VPT_MACRO " 1\n"
  2626.      ".sp |\\n[" BOTTOM_REG "]u\n"
  2627.      "\\*[" TRANSPARENT_STRING_NAME "].nr " NEED_BOTTOM_RULE_REG " 1\n");
  2628.   if (r != nrows - 1 && (flags & ALLBOX)) {
  2629.     print_single_hline(r + 1);
  2630.     prints("\\*[" TRANSPARENT_STRING_NAME "].nr " NEED_BOTTOM_RULE_REG " 0\n");
  2631.   }
  2632.   if (r != nrows - 1) {
  2633.     if (p && p->row == r + 1
  2634.     && (p->is_single_line() || p->is_double_line())) {
  2635.       p->print(this);
  2636.       prints("\\*[" TRANSPARENT_STRING_NAME "].nr " NEED_BOTTOM_RULE_REG
  2637.          " 0\n");
  2638.     }
  2639.     int printed_one = 0;
  2640.     for (vertical_rule *p = vrule_list; p; p = p->next)
  2641.       if (p->end_row == r) {
  2642.     if (!printed_one) {
  2643.       prints("." REPEATED_VPT_MACRO " 0\n");
  2644.       printed_one = 1;
  2645.     }
  2646.     p->print();
  2647.       }
  2648.     if (printed_one)
  2649.       prints("." REPEATED_VPT_MACRO " 1\n");
  2650.     if (!(flags & NOKEEP) && row_ends_section(r))
  2651.       prints("." RELEASE_MACRO_NAME "\n");
  2652.   }
  2653. }
  2654.  
  2655. void table::do_top()
  2656. {
  2657.   prints(".fc \002\003\n");
  2658.   if (!(flags & NOKEEP) && (flags & (BOX|DOUBLEBOX|ALLBOX)))
  2659.     prints("." TABLE_KEEP_MACRO_NAME "\n");
  2660.   if (flags & DOUBLEBOX) {
  2661.     prints(".ls 1\n"
  2662.        ".vs " LINE_SEP ">?\\n[.V]u\n"
  2663.        "\\v'" BODY_DEPTH "'\\s[\\n[" LINESIZE_REG "]]\\D'l \\n[TW]u 0'\\s0\n"
  2664.        ".vs\n"
  2665.        "." REPEATED_MARK_MACRO " " TOP_REG "\n"
  2666.        ".vs " DOUBLE_LINE_SEP ">?\\n[.V]u\n");
  2667.     printfs("\\v'" BODY_DEPTH "'"
  2668.         "\\s[\\n[" LINESIZE_REG "]]"
  2669.         "\\h'\\n[%1]u'"
  2670.         "\\D'l |\\n[%2]u 0'"
  2671.         "\\s0"
  2672.         "\n",
  2673.         column_divide_reg(0),
  2674.         column_divide_reg(ncolumns));
  2675.     prints(".ls\n"
  2676.        ".vs\n");
  2677.   }
  2678.   else if (flags & (ALLBOX|BOX)) {
  2679.     print_single_hline(0);
  2680.   }
  2681.   //printfs(".mk %1\n", row_top_reg(0));
  2682. }
  2683.  
  2684. void table::do_bottom()
  2685. {
  2686.   // print stuff after last row
  2687.   for (stuff *p = stuff_list; p; p = p->next)
  2688.     if (p->row > nrows - 1)
  2689.       p->print(this);
  2690.   if (!(flags & NOKEEP))
  2691.     prints("." RELEASE_MACRO_NAME "\n");
  2692.   printfs(".mk %1\n", row_top_reg(nrows));
  2693.   prints(".nr " NEED_BOTTOM_RULE_REG " 1\n"
  2694.      ".nr T. 1\n"
  2695.      ".T#\n");
  2696.   if (!(flags & NOKEEP) && (flags & (BOX|DOUBLEBOX|ALLBOX)))
  2697.     prints("." TABLE_RELEASE_MACRO_NAME "\n");
  2698.   if (flags & DOUBLEBOX)
  2699.     prints(".sp " DOUBLE_LINE_SEP "\n");
  2700.   prints("." RESET_MACRO_NAME "\n"
  2701.      ".fc\n"
  2702.      ".cp \\n(" COMPATIBLE_REG "\n");
  2703. }
  2704.  
  2705. int table::get_nrows()
  2706. {
  2707.   return nrows;
  2708. }
  2709.  
  2710. const char *last_filename = 0;
  2711.  
  2712. void set_troff_location(const char *fn, int ln)
  2713. {
  2714.   if (!location_force_filename && last_filename != 0
  2715.       && strcmp(fn, last_filename) == 0)
  2716.     printfs(".lf %1\n", as_string(ln));
  2717.   else {
  2718.     printfs(".lf %1 %2\n", as_string(ln), fn);
  2719.     last_filename = fn;
  2720.     location_force_filename = 0;
  2721.   }
  2722. }
  2723.  
  2724. void printfs(const char *s, const string &arg1, const string &arg2,
  2725.          const string &arg3, const string &arg4, const string &arg5)
  2726. {
  2727.   if (s) {
  2728.     char c;
  2729.     while ((c = *s++) != '\0') {
  2730.       if (c == '%') {
  2731.     switch (*s++) {
  2732.     case '1':
  2733.       prints(arg1);
  2734.       break;
  2735.     case '2':
  2736.       prints(arg2);
  2737.       break;
  2738.     case '3':
  2739.       prints(arg3);
  2740.       break;
  2741.     case '4':
  2742.       prints(arg4);
  2743.       break;
  2744.     case '5':
  2745.       prints(arg5);
  2746.       break;
  2747.     case '6':
  2748.     case '7':
  2749.     case '8':
  2750.     case '9':
  2751.       break;
  2752.     case '%':
  2753.       prints('%');
  2754.       break;
  2755.     default:
  2756.       assert(0);
  2757.     }
  2758.       }
  2759.       else
  2760.     prints(c);
  2761.     }
  2762.   }
  2763. }  
  2764.  
  2765.