t6.c 7.1 KB

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