t10.c 9.2 KB

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