t6.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* t6.c: compute tab stops */
  2. # define tx(a) (a>0 && a<128)
  3. # include "t.h"
  4. # define FN(i,c) font[c][stynum[i]]
  5. # define SZ(i,c) csize[c][stynum[i]]
  6. # define TMP1 S1
  7. # define TMP2 S2
  8. void
  9. maktab(void) /* define the tab stops of the table */
  10. {
  11. int icol, ilin, tsep, k, ik, vforml, il, text;
  12. char *s;
  13. for (icol = 0; icol < ncol; icol++) {
  14. doubled[icol] = acase[icol] = 0;
  15. Bprint(&tabout, ".nr %2s 0\n", reg(icol, CRIGHT));
  16. for (text = 0; text < 2; text++) {
  17. if (text)
  18. Bprint(&tabout, ".%2s\n.rm %2s\n", reg(icol, CRIGHT),
  19. reg(icol, CRIGHT));
  20. for (ilin = 0; ilin < nlin; ilin++) {
  21. if (instead[ilin] || fullbot[ilin])
  22. continue;
  23. vforml = ilin;
  24. for (il = prev(ilin); il >= 0 && vspen(table[il][icol].col); il = prev(il))
  25. vforml = il;
  26. if (fspan(vforml, icol))
  27. continue;
  28. if (filler(table[ilin][icol].col))
  29. continue;
  30. if ((flags[icol][stynum[ilin]] & ZEROW) != 0)
  31. continue;
  32. switch (ctype(vforml, icol)) {
  33. case 'a':
  34. acase[icol] = 1;
  35. s = table[ilin][icol].col;
  36. if ((int)s > 0 && (int)s < 128 && text) {
  37. if (doubled[icol] == 0)
  38. Bprint(&tabout, ".nr %d 0\n.nr %d 0\n",
  39. S1, S2);
  40. doubled[icol] = 1;
  41. Bprint(&tabout, ".if \\n(%c->\\n(%d .nr %d \\n(%c-\n",
  42. (int)s, S2, S2, (int)s);
  43. }
  44. case 'n':
  45. if (table[ilin][icol].rcol != 0) {
  46. if (doubled[icol] == 0 && text == 0)
  47. Bprint(&tabout, ".nr %d 0\n.nr %d 0\n",
  48. S1, S2);
  49. doubled[icol] = 1;
  50. if (real(s = table[ilin][icol].col) && !vspen(s)) {
  51. if (tx((int)s) != text)
  52. continue;
  53. Bprint(&tabout, ".nr %d ", TMP);
  54. wide(s, FN(vforml, icol), SZ(vforml, icol));
  55. Bprint(&tabout, "\n");
  56. Bprint(&tabout, ".if \\n(%d<\\n(%d .nr %d \\n(%d\n",
  57. S1, TMP, S1, TMP);
  58. }
  59. if (text == 0 && real(s = table[ilin][icol].rcol) && !vspen(s) && !barent(s)) {
  60. Bprint(&tabout, ".nr %d \\w%c%s%c\n",
  61. TMP, F1, s, F1);
  62. Bprint(&tabout, ".if \\n(%d<\\n(%d .nr %d \\n(%d\n", S2, TMP, S2,
  63. TMP);
  64. }
  65. continue;
  66. }
  67. case 'r':
  68. case 'c':
  69. case 'l':
  70. if (real(s = table[ilin][icol].col) && !vspen(s)) {
  71. if (tx((int)s) != text)
  72. continue;
  73. Bprint(&tabout, ".nr %d ", TMP);
  74. wide(s, FN(vforml, icol), SZ(vforml, icol));
  75. Bprint(&tabout, "\n");
  76. Bprint(&tabout, ".if \\n(%2s<\\n(%d .nr %2s \\n(%d\n",
  77. reg(icol, CRIGHT), TMP, reg(icol, CRIGHT), TMP);
  78. }
  79. }
  80. }
  81. }
  82. if (acase[icol]) {
  83. Bprint(&tabout, ".if \\n(%d>=\\n(%2s .nr %2s \\n(%du+2n\n",
  84. S2, reg(icol, CRIGHT), reg(icol, CRIGHT), S2);
  85. }
  86. if (doubled[icol]) {
  87. Bprint(&tabout, ".nr %2s \\n(%d\n", reg(icol, CMID), S1);
  88. Bprint(&tabout, ".nr %d \\n(%2s+\\n(%d\n", TMP, reg(icol, CMID), S2);
  89. Bprint(&tabout, ".if \\n(%d>\\n(%2s .nr %2s \\n(%d\n", TMP,
  90. reg(icol, CRIGHT), reg(icol, CRIGHT), TMP);
  91. Bprint(&tabout, ".if \\n(%d<\\n(%2s .nr %2s +(\\n(%2s-\\n(%d)/2\n",
  92. TMP, reg(icol, CRIGHT), reg(icol, CMID), reg(icol, CRIGHT), TMP);
  93. }
  94. if (cll[icol][0]) {
  95. Bprint(&tabout, ".nr %d %sn\n", TMP, cll[icol]);
  96. Bprint(&tabout, ".if \\n(%2s<\\n(%d .nr %2s \\n(%d\n",
  97. reg(icol, CRIGHT), TMP, reg(icol, CRIGHT), TMP);
  98. }
  99. for (ilin = 0; ilin < nlin; ilin++)
  100. if (k = lspan(ilin, icol)) {
  101. s = table[ilin][icol-k].col;
  102. if (!real(s) || barent(s) || vspen(s) )
  103. continue;
  104. Bprint(&tabout, ".nr %d ", TMP);
  105. wide(table[ilin][icol-k].col, FN(ilin, icol - k), SZ(ilin, icol - k));
  106. for (ik = k; ik >= 0; ik--) {
  107. Bprint(&tabout, "-\\n(%2s", reg(icol - ik, CRIGHT));
  108. if (!expflg && ik > 0)
  109. Bprint(&tabout, "-%dn", sep[icol-ik]);
  110. }
  111. Bprint(&tabout, "\n");
  112. Bprint(&tabout, ".if \\n(%d>0 .nr %d \\n(%d/%d\n", TMP,
  113. TMP, TMP, k);
  114. Bprint(&tabout, ".if \\n(%d<0 .nr %d 0\n", TMP, TMP);
  115. for (ik = 1; ik <= k; ik++) {
  116. if (doubled[icol-k+ik])
  117. Bprint(&tabout, ".nr %2s +\\n(%d/2\n",
  118. reg(icol - k + ik, CMID), TMP);
  119. Bprint(&tabout, ".nr %2s +\\n(%d\n",
  120. reg(icol - k + ik, CRIGHT), TMP);
  121. }
  122. }
  123. }
  124. if (textflg)
  125. untext();
  126. /* if even requested, make all columns widest width */
  127. if (evenflg) {
  128. Bprint(&tabout, ".nr %d 0\n", TMP);
  129. for (icol = 0; icol < ncol; icol++) {
  130. if (evenup[icol] == 0)
  131. continue;
  132. Bprint(&tabout, ".if \\n(%2s>\\n(%d .nr %d \\n(%2s\n",
  133. reg(icol, CRIGHT), TMP, TMP, reg(icol, CRIGHT));
  134. }
  135. for (icol = 0; icol < ncol; icol++) {
  136. if (evenup[icol] == 0)
  137. /* if column not evened just retain old interval */
  138. continue;
  139. if (doubled[icol])
  140. Bprint(&tabout, ".nr %2s (100*\\n(%2s/\\n(%2s)*\\n(%d/100\n",
  141. reg(icol, CMID), reg(icol, CMID), reg(icol, CRIGHT), TMP);
  142. /* that nonsense with the 100's and parens tries
  143. to avoid overflow while proportionally shifting
  144. the middle of the number */
  145. Bprint(&tabout, ".nr %2s \\n(%d\n", reg(icol, CRIGHT), TMP);
  146. }
  147. }
  148. /* now adjust for total table width */
  149. for (tsep = icol = 0; icol < ncol; icol++)
  150. tsep += sep[icol];
  151. if (expflg) {
  152. Bprint(&tabout, ".nr %d 0", TMP);
  153. for (icol = 0; icol < ncol; icol++)
  154. Bprint(&tabout, "+\\n(%2s", reg(icol, CRIGHT));
  155. Bprint(&tabout, "\n");
  156. Bprint(&tabout, ".nr %d \\n(.l-\\n(%d\n", TMP, TMP);
  157. if (boxflg || dboxflg || allflg)
  158. /* tsep += 1; */ {}
  159. else
  160. tsep -= sep[ncol-1];
  161. Bprint(&tabout, ".nr %d \\n(%d/%d\n", TMP, TMP, tsep);
  162. Bprint(&tabout, ".if \\n(%d<0 .nr %d 0\n", TMP, TMP);
  163. } else
  164. Bprint(&tabout, ".nr %d 1n\n", TMP);
  165. Bprint(&tabout, ".nr %2s 0\n", reg(-1, CRIGHT));
  166. tsep = (boxflg || allflg || dboxflg || left1flg) ? 2 : 0;
  167. if (sep[-1] >= 0)
  168. tsep = sep[-1];
  169. for (icol = 0; icol < ncol; icol++) {
  170. Bprint(&tabout, ".nr %2s \\n(%2s+((%d*\\n(%d)/2)\n", reg(icol, CLEFT),
  171. reg(icol - 1, CRIGHT), tsep, TMP);
  172. Bprint(&tabout, ".nr %2s +\\n(%2s\n", reg(icol, CRIGHT), reg(icol, CLEFT));
  173. if (doubled[icol]) {
  174. /* the next line is last-ditch effort to avoid zero field width */
  175. /*Bprint(&tabout, ".if \\n(%2s=0 .nr %2s 1\n",reg(icol,CMID), reg(icol,CMID));*/
  176. Bprint(&tabout, ".nr %2s +\\n(%2s\n", reg(icol, CMID),
  177. reg(icol, CLEFT));
  178. /* Bprint(&tabout, ".if n .if \\n(%s%%24>0 .nr %s +12u\n",reg(icol,CMID), reg(icol,CMID)); */
  179. }
  180. tsep = sep[icol] * 2;
  181. }
  182. if (rightl)
  183. Bprint(&tabout, ".nr %s (\\n(%s+\\n(%s)/2\n", reg(ncol - 1, CRIGHT),
  184. reg(ncol - 1, CLEFT), reg(ncol - 2, CRIGHT));
  185. Bprint(&tabout, ".nr TW \\n(%2s\n", reg(ncol - 1, CRIGHT));
  186. tsep = sep[ncol-1];
  187. if (boxflg || allflg || dboxflg)
  188. Bprint(&tabout, ".nr TW +((%d*\\n(%d)/2)\n", tsep, TMP);
  189. Bprint(&tabout,
  190. ".if t .if (\\n(TW+\\n(.o)>7.65i .tm Table at line %d file %s is too wide - \\n(TW units\n", iline - 1, ifile);
  191. return;
  192. }
  193. void
  194. wide(char *s, char *fn, char *size)
  195. {
  196. if (point(s)) {
  197. Bprint(&tabout, "\\w%c", F1);
  198. if (*fn > 0)
  199. putfont(fn);
  200. if (*size)
  201. putsize(size);
  202. Bprint(&tabout, "%s", s);
  203. if (*fn > 0)
  204. putfont("P");
  205. if (*size)
  206. putsize("0");
  207. Bprint(&tabout, "%c", F1);
  208. } else
  209. Bprint(&tabout, "\\n(%c-", (int)s);
  210. }
  211. int
  212. filler(char *s)
  213. {
  214. return (point(s) && s[0] == '\\' && s[1] == 'R');
  215. }