t10.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. #include "tdef.h"
  2. #include "fns.h"
  3. #include "ext.h"
  4. /*
  5. * troff10.c
  6. *
  7. * typesetter interface
  8. */
  9. int vpos = 0; /* absolute vertical position on page */
  10. int hpos = 0; /* ditto horizontal */
  11. extern Font fonts[MAXFONTS+1];
  12. int Inch;
  13. int Hor;
  14. int Vert;
  15. int Unitwidth;
  16. int nfonts;
  17. void t_ptinit(void)
  18. {
  19. int i;
  20. char buf[100], *p;
  21. hmot = t_hmot;
  22. makem = t_makem;
  23. setabs = t_setabs;
  24. setch = t_setch;
  25. sethl = t_sethl;
  26. setht = t_setht;
  27. setslant = t_setslant;
  28. vmot = t_vmot;
  29. xlss = t_xlss;
  30. findft = t_findft;
  31. width = t_width;
  32. mchbits = t_mchbits;
  33. ptlead = t_ptlead;
  34. ptout = t_ptout;
  35. ptpause = t_ptpause;
  36. setfont = t_setfont;
  37. setps = t_setps;
  38. setwd = t_setwd;
  39. /* open table for device, */
  40. /* read in resolution, size info, font info, etc., set params */
  41. if ((p = getenv("TYPESETTER")) != 0){
  42. strncpy(devname, p, sizeof devname);
  43. devname[sizeof devname-1] = 0;
  44. }
  45. if (termtab[0] == 0)
  46. strcpy(termtab, DWBfontdir);
  47. if (fontdir[0] == 0)
  48. strcpy(fontdir, DWBfontdir);
  49. if (devname[0] == 0)
  50. strcpy(devname, TDEVNAME);
  51. hyf = 1;
  52. lg = 1;
  53. snprintf(buf, sizeof buf, "/dev%s/DESC", devname);
  54. strcat(termtab, buf);
  55. if (getdesc(termtab) < 0) {
  56. ERROR "can't open DESC file %s", termtab WARN;
  57. done3(1);
  58. }
  59. if (!ascii) {
  60. OUT "x T %s\n", devname PUT;
  61. OUT "x res %d %d %d\n", Inch, Hor, Vert PUT;
  62. OUT "x init\n" PUT;
  63. }
  64. for (i = 1; i <= nfonts; i++)
  65. setfp(i, fontlab[i], (char *) 0, 0);
  66. sps = EM/3; /* space size */
  67. ics = EM; /* insertion character space */
  68. for (i = 0; i < (NTAB - 1) && DTAB * (i + 1) < TABMASK; i++)
  69. tabtab[i] = DTAB * (i + 1);
  70. tabtab[NTAB] = 0;
  71. pl = 11 * INCH; /* paper length */
  72. po = PO; /* page offset */
  73. spacesz = SS;
  74. lss = lss1 = VS;
  75. ll = ll1 = lt = lt1 = LL;
  76. t_specnames(); /* install names like "hyphen", etc. */
  77. }
  78. void t_specnames(void)
  79. {
  80. int i;
  81. for (i = 0; spnames[i].n; i++)
  82. *spnames[i].n = chadd(spnames[i].v, Troffchar, Install);
  83. }
  84. void t_ptout(Tchar i)
  85. {
  86. int dv;
  87. Tchar *k;
  88. int temp, a, b;
  89. int diff;
  90. if (cbits(i) != '\n') {
  91. if (olinep >= oline + olnsize) {
  92. diff = olinep - oline;
  93. olnsize += OLNSIZE;
  94. if ((oline = (Tchar *)realloc((char *)oline, olnsize * sizeof(Tchar))) != NULL) {
  95. if (diff && olinep)
  96. olinep = oline + diff;
  97. } else {
  98. ERROR "Output line overflow." WARN;
  99. done(2);
  100. }
  101. }
  102. *olinep++ = i;
  103. return;
  104. }
  105. if (olinep == oline) {
  106. lead += lss;
  107. return;
  108. }
  109. hpos = po; /* ??? */
  110. esc = 0; /* ??? */
  111. ptesc(); /* the problem is to get back to the left end of the line */
  112. dv = 0;
  113. for (k = oline; k < olinep; k++) {
  114. if (ismot(*k) && isvmot(*k)) {
  115. temp = absmot(*k);
  116. if (isnmot(*k))
  117. temp = -temp;
  118. dv += temp;
  119. }
  120. }
  121. if (dv) {
  122. vflag++;
  123. *olinep++ = makem(-dv);
  124. vflag = 0;
  125. }
  126. b = dip->blss + lss;
  127. lead += dip->blss + lss;
  128. dip->blss = 0;
  129. for (k = oline; k < olinep; )
  130. k += ptout0(k); /* now passing a pointer! */
  131. olinep = oline;
  132. lead += dip->alss;
  133. a = dip->alss;
  134. dip->alss = 0;
  135. /*
  136. OUT "x xxx end of line: hpos=%d, vpos=%d\n", hpos, vpos PUT;
  137. */
  138. OUT "n%d %d\n", b, a PUT; /* be nice to chuck */
  139. }
  140. int ptout0(Tchar *pi)
  141. {
  142. int j, k, w;
  143. int z, dx, dy, dx2, dy2, n;
  144. Tchar i;
  145. int outsize; /* size of object being printed */
  146. outsize = 1; /* default */
  147. i = *pi;
  148. k = cbits(i);
  149. if (ismot(i)) {
  150. j = absmot(i);
  151. if (isnmot(i))
  152. j = -j;
  153. if (isvmot(i))
  154. lead += j;
  155. else
  156. esc += j;
  157. return(outsize);
  158. }
  159. if (k == CHARHT) {
  160. xpts = fbits(i); /* sneaky, font bits as size bits */
  161. if (xpts != mpts)
  162. ptps();
  163. OUT "x H %d\n", sbits(i) PUT;
  164. return(outsize);
  165. }
  166. if (k == SLANT) {
  167. OUT "x S %d\n", sfbits(i)-180 PUT;
  168. return(outsize);
  169. }
  170. if (k == WORDSP) {
  171. oput('w');
  172. return(outsize);
  173. }
  174. if (sfbits(i) == oldbits) {
  175. xfont = pfont;
  176. xpts = ppts;
  177. } else
  178. xbits(i, 2);
  179. if (k == XON) {
  180. extern int xon;
  181. ptflush(); /* guarantee that everything is out */
  182. if (esc)
  183. ptesc();
  184. if (xfont != mfont)
  185. ptfont();
  186. if (xpts != mpts)
  187. ptps();
  188. if (lead)
  189. ptlead();
  190. OUT "x X " PUT;
  191. xon++;
  192. for (j = 1; cbits(pi[j]) != XOFF; j++)
  193. outascii(pi[j]);
  194. oput('\n');
  195. xon--;
  196. return j+1;
  197. }
  198. if (k < 040 && k != DRAWFCN)
  199. return(outsize);
  200. j = z = 0;
  201. if (k != DRAWFCN) {
  202. if (widcache[k].fontpts == (xfont<<8) + xpts && !setwdf) {
  203. w = widcache[k].width;
  204. bd = 0;
  205. cs = 0;
  206. } else
  207. w = getcw(k);
  208. if (cs) {
  209. if (bd)
  210. w += (bd - 1) * HOR;
  211. j = (cs - w) / 2;
  212. w = cs - j;
  213. if (bd)
  214. w -= (bd - 1) * HOR;
  215. }
  216. if (iszbit(i)) {
  217. if (cs)
  218. w = -j;
  219. else
  220. w = 0;
  221. z = 1;
  222. }
  223. }
  224. esc += j;
  225. if (xfont != mfont)
  226. ptfont();
  227. if (xpts != mpts)
  228. ptps();
  229. if (lead)
  230. ptlead();
  231. /* put out the real character here */
  232. if (k == DRAWFCN) {
  233. if (esc)
  234. ptesc();
  235. w = 0;
  236. dx = absmot(pi[3]);
  237. if (isnmot(pi[3]))
  238. dx = -dx;
  239. dy = absmot(pi[4]);
  240. if (isnmot(pi[4]))
  241. dy = -dy;
  242. switch (cbits(pi[1])) {
  243. case DRAWCIRCLE: /* circle */
  244. OUT "D%c %d\n", DRAWCIRCLE, dx PUT; /* dx is diameter */
  245. hpos += dx;
  246. break;
  247. case DRAWELLIPSE:
  248. OUT "D%c %d %d\n", DRAWELLIPSE, dx, dy PUT;
  249. hpos += dx;
  250. break;
  251. case DRAWBUILD:
  252. k = cbits(pi[2]);
  253. OUT "D%c %d ", DRAWBUILD, dx PUT;
  254. if (k < ALPHABET)
  255. OUT "%c\n", k PUT;
  256. else
  257. ptchname(k);
  258. hpos += dx;
  259. break;
  260. case DRAWLINE: /* line */
  261. k = cbits(pi[2]);
  262. OUT "D%c %d %d ", DRAWLINE, dx, dy PUT;
  263. if (k < ALPHABET)
  264. OUT "%c\n", k PUT;
  265. else
  266. ptchname(k);
  267. hpos += dx;
  268. vpos += dy;
  269. break;
  270. case DRAWARC: /* arc */
  271. dx2 = absmot(pi[5]);
  272. if (isnmot(pi[5]))
  273. dx2 = -dx2;
  274. dy2 = absmot(pi[6]);
  275. if (isnmot(pi[6]))
  276. dy2 = -dy2;
  277. OUT "D%c %d %d %d %d\n", DRAWARC,
  278. dx, dy, dx2, dy2 PUT;
  279. hpos += dx + dx2;
  280. vpos += dy + dy2;
  281. break;
  282. case 's': /* using 's' internally to avoid .tr ~ */
  283. pi[1] = '~';
  284. case DRAWSPLINE: /* spline */
  285. default: /* something else; copy it like spline */
  286. OUT "D%c %d %d", cbits(pi[1]), dx, dy PUT;
  287. hpos += dx;
  288. vpos += dy;
  289. if (cbits(pi[3]) == DRAWFCN || cbits(pi[4]) == DRAWFCN) {
  290. /* it was somehow defective */
  291. OUT "\n" PUT;
  292. break;
  293. }
  294. for (n = 5; cbits(pi[n]) != DRAWFCN; n += 2) {
  295. dx = absmot(pi[n]);
  296. if (isnmot(pi[n]))
  297. dx = -dx;
  298. dy = absmot(pi[n+1]);
  299. if (isnmot(pi[n+1]))
  300. dy = -dy;
  301. OUT " %d %d", dx, dy PUT;
  302. hpos += dx;
  303. vpos += dy;
  304. }
  305. OUT "\n" PUT;
  306. break;
  307. }
  308. for (n = 3; cbits(pi[n]) != DRAWFCN; n++)
  309. ;
  310. outsize = n + 1;
  311. } else if (k < ALPHABET) {
  312. /* try to go faster and compress output */
  313. /* by printing nnc for small positive motion followed by c */
  314. /* kludgery; have to make sure set all the vars too */
  315. if (esc > 0 && esc < 100) {
  316. oput(esc / 10 + '0');
  317. oput(esc % 10 + '0');
  318. oput(k);
  319. hpos += esc;
  320. esc = 0;
  321. } else {
  322. if (esc)
  323. ptesc();
  324. oput('c');
  325. oput(k);
  326. oput('\n');
  327. }
  328. } else {
  329. if (esc)
  330. ptesc();
  331. ptchname(k);
  332. }
  333. if (bd) {
  334. bd -= HOR;
  335. if (esc += bd)
  336. ptesc();
  337. if (k < ALPHABET)
  338. OUT "c%c\n", k PUT;
  339. else
  340. ptchname(k);
  341. if (z)
  342. esc -= bd;
  343. }
  344. esc += w;
  345. return(outsize);
  346. }
  347. void ptchname(int k)
  348. {
  349. char *chn = chname(k);
  350. switch (chn[0]) {
  351. case MBchar:
  352. OUT "c%s\n", chn+1 PUT; /* \n not needed? */
  353. break;
  354. case Number:
  355. OUT "N%s\n", chn+1 PUT;
  356. break;
  357. case Troffchar:
  358. OUT "C%s\n", chn+1 PUT;
  359. break;
  360. default:
  361. ERROR "illegal char type %s", chn WARN;
  362. break;
  363. }
  364. }
  365. void ptflush(void) /* get us to a clean output state */
  366. {
  367. if (TROFF) {
  368. /* ptesc(); but always H, no h */
  369. hpos += esc;
  370. OUT "\nH%d\n", hpos PUT;
  371. esc = 0;
  372. ptps();
  373. ptfont();
  374. ptlead();
  375. }
  376. }
  377. void ptps(void)
  378. {
  379. int i, j, k;
  380. i = xpts;
  381. for (j = 0; i > (k = pstab[j]); j++)
  382. if (!k) {
  383. k = pstab[--j];
  384. break;
  385. }
  386. if (!ascii)
  387. OUT "s%d\n", k PUT; /* really should put out string rep of size */
  388. mpts = i;
  389. }
  390. void ptfont(void)
  391. {
  392. mfont = xfont;
  393. if (ascii)
  394. return;
  395. if (xfont > nfonts) {
  396. ptfpcmd(0, fonts[xfont].longname, 0); /* Put the desired font in the
  397. * fontcache of the filter */
  398. OUT "f0\n" PUT; /* make sure that it gets noticed */
  399. } else
  400. OUT "f%d\n", xfont PUT;
  401. }
  402. void ptfpcmd(int f, char *s, char *longname)
  403. {
  404. if (f > nfonts) /* a bit risky? */
  405. f = 0;
  406. if (longname) {
  407. OUT "x font %d %s %s\n", f, s, longname PUT;
  408. } else {
  409. OUT "x font %d %s\n", f, s PUT;
  410. }
  411. /* OUT "f%d\n", xfont PUT; /* need this for buggy version of adobe transcript */
  412. /* which apparently believes that x font means */
  413. /* to set the font, not just the position. */
  414. }
  415. void t_ptlead(void)
  416. {
  417. vpos += lead;
  418. if (!ascii)
  419. OUT "V%d\n", vpos PUT;
  420. lead = 0;
  421. }
  422. void ptesc(void)
  423. {
  424. hpos += esc;
  425. if (!ascii)
  426. if (esc > 0) {
  427. oput('h');
  428. if (esc>=10 && esc<100) {
  429. oput(esc/10 + '0');
  430. oput(esc%10 + '0');
  431. } else
  432. OUT "%d", esc PUT;
  433. } else
  434. OUT "H%d\n", hpos PUT;
  435. esc = 0;
  436. }
  437. void ptpage(int n) /* called at end of each output page, we hope */
  438. {
  439. int i;
  440. if (NROFF)
  441. return;
  442. ptlead();
  443. vpos = 0;
  444. if (ascii)
  445. return;
  446. OUT "p%d\n", n PUT; /* new page */
  447. for (i = 0; i <= nfonts; i++)
  448. if (fontlab[i]) {
  449. if (fonts[i].truename)
  450. OUT "x font %d %s %s\n", i, fonts[i].longname, fonts[i].truename PUT;
  451. else
  452. OUT "x font %d %s\n", i, fonts[i].longname PUT;
  453. }
  454. ptps();
  455. ptfont();
  456. }
  457. void pttrailer(void)
  458. {
  459. if (TROFF)
  460. OUT "x trailer\n" PUT;
  461. }
  462. void ptstop(void)
  463. {
  464. if (TROFF)
  465. OUT "x stop\n" PUT;
  466. }
  467. void t_ptpause(void)
  468. {
  469. if (ascii)
  470. return;
  471. ptlead();
  472. vpos = 0;
  473. pttrailer();
  474. ptlead();
  475. OUT "x pause\n" PUT;
  476. flusho();
  477. mpts = mfont = 0;
  478. ptesc();
  479. esc = po;
  480. hpos = vpos = 0; /* probably in wrong place */
  481. }