README: make the instructions for CM fonts more concise
[neateqn.git] / box.c
blob112fa3a8e9a17332cab7823c3359b8a12f0d972e
1 #include <stdarg.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "eqn.h"
7 struct box *box_alloc(int szreg, int pre, int style)
9 struct box *box = malloc(sizeof(*box));
10 memset(box, 0, sizeof(*box));
11 sbuf_init(&box->raw);
12 box->szreg = szreg;
13 box->atoms = 0;
14 box->style = style;
15 if (pre)
16 box->tcur = pre;
17 return box;
20 void box_free(struct box *box)
22 if (box->reg)
23 sregrm(box->reg);
24 if (box->szown)
25 nregrm(box->szreg);
26 sbuf_done(&box->raw);
27 free(box);
30 static void box_put(struct box *box, char *s)
32 sbuf_append(&box->raw, s);
33 if (box->reg)
34 printf(".as %s \"%s\n", sregname(box->reg), s);
37 void box_putf(struct box *box, char *s, ...)
39 char buf[LNLEN];
40 va_list ap;
41 va_start(ap, s);
42 vsprintf(buf, s, ap);
43 va_end(ap);
44 box_put(box, buf);
47 char *box_buf(struct box *box)
49 return sbuf_buf(&box->raw);
52 /* change box's point size; return the number register storing it */
53 int box_size(struct box *box, char *val)
55 int szreg = box->szreg;
56 if (!val || !*val)
57 return szreg;
58 if (!box->szown) {
59 box->szown = 1;
60 box->szreg = nregmk();
62 if (val[0] == '-' || val[0] == '+')
63 printf(".nr %s %s%s\n", nregname(box->szreg), nreg(szreg), val);
64 else
65 printf(".nr %s %s\n", nregname(box->szreg), val);
66 return box->szreg;
69 void box_move(struct box *box, int dy, int dx)
71 if (dy)
72 box_putf(box, "\\v'%du*%sp/100u'", dy, nreg(box->szreg));
73 if (dx)
74 box_putf(box, "\\h'%du*%sp/100u'", dx, nreg(box->szreg));
77 /* T_ORD, T_BIGOP, T_BINOP, T_RELOP, T_LEFT, T_RIGHT, T_PUNC, T_INNER */
78 static int spacing[][8] = {
79 {0, 1, 2, 3, 0, 0, 0, 1},
80 {1, 1, 0, 3, 0, 0, 0, 1},
81 {2, 2, 0, 0, 2, 0, 0, 2},
82 {3, 3, 0, 0, 3, 0, 0, 3},
83 {0, 0, 0, 0, 0, 0, 0, 0},
84 {0, 1, 2, 3, 0, 0, 0, 1},
85 {1, 1, 0, 1, 1, 1, 1, 1},
86 {1, 1, 2, 3, 1, 0, 1, 1},
89 /* return the amount of automatic spacing before adding the given token */
90 static int eqn_gaps(struct box *box, int cur)
92 int s = 0;
93 int a1 = T_ATOM(box->tcur); /* previous atom */
94 int a2 = T_ATOM(cur); /* current atom */
95 if (!TS_SZ(box->style) && a1 && a2)
96 s = spacing[T_ATOMIDX(a1)][T_ATOMIDX(a2)];
97 if (s == 3)
98 return S_S3;
99 if (s == 2)
100 return S_S2;
101 return s ? S_S1 : 0;
104 /* call just before inserting a non-italic character */
105 static void box_italiccorrection(struct box *box)
107 if (box->atoms && (box->tcur & T_ITALIC))
108 box_put(box, "\\/");
109 box->tcur &= ~T_ITALIC;
112 static int box_fixatom(int cur, int pre)
114 if (cur == T_BINOP && (!pre || pre == T_RELOP || pre == T_BIGOP ||
115 pre == T_LEFT || pre == T_PUNC))
116 return T_ORD;
117 if (cur == T_RELOP && (!pre || pre == T_LEFT))
118 return T_ORD;
119 return cur;
122 /* call before inserting a token with box_put() and box_putf() */
123 static void box_beforeput(struct box *box, int type, int breakable)
125 int autogaps; /* automatically inserted space before this token */
126 if (box->atoms) {
127 autogaps = eqn_gaps(box, T_ATOM(type));
128 if (!(type & T_ITALIC))
129 box_italiccorrection(box);
130 if (autogaps && type != T_GAP && box->tcur != T_GAP) {
131 box_italiccorrection(box);
132 if (breakable) { /* enlarge a space to match autogaps */
133 box_putf(box, "\\s[\\En(.s*%du*%sp/100u/\\w' 'u]\\j'%d' \\s0",
134 autogaps, nreg(box->szreg),
135 def_brcost(T_ATOM(box->tcur)));
136 } else {
137 box_putf(box, "\\h'%du*%sp/100u'",
138 autogaps, nreg(box->szreg));
142 if (box->tomark) {
143 printf(".nr %s 0\\w'%s'\n", box->tomark, box_toreg(box));
144 box->tomark = NULL;
148 /* call after inserting a token with box_put() and box_putf() */
149 static void box_afterput(struct box *box, int type)
151 box->atoms++;
152 box->tcur = T_FONT(type) | box_fixatom(T_TOK(type), T_TOK(box->tcur));
153 if (!box->tbeg)
154 box->tbeg = box->tcur;
157 /* insert s with the given type */
158 void box_puttext(struct box *box, int type, char *s, ...)
160 char buf[LNLEN];
161 va_list ap;
162 va_start(ap, s);
163 vsprintf(buf, s, ap);
164 va_end(ap);
165 box_beforeput(box, type, 0);
166 if (!(box->tcur & T_ITALIC) && (type & T_ITALIC))
167 box_put(box, "\\,");
168 box_put(box, buf);
169 box_afterput(box, type);
172 /* append sub to box */
173 void box_merge(struct box *box, struct box *sub, int breakable)
175 if (box_empty(sub))
176 return;
177 box_beforeput(box, sub->tbeg, breakable);
178 box_toreg(box);
179 box_put(box, box_toreg(sub));
180 if (!box->tbeg)
181 box->tbeg = sub->tbeg;
182 /* fix atom type only if merging a single atom */
183 if (sub->atoms == 1) {
184 box_afterput(box, sub->tcur);
185 } else {
186 box->tcur = sub->tcur;
187 box->atoms += sub->atoms;
191 /* put the maximum of number registers a and b into register dst */
192 static void roff_max(int dst, int a, int b)
194 printf(".ie %s>=%s .nr %s 0+%s\n",
195 nreg(a), nreg(b), nregname(dst), nreg(a));
196 printf(".el .nr %s 0+%s\n", nregname(dst), nreg(b));
199 /* return the width, height and depth of a string */
200 static void tok_dim(char *s, int wd, int ht, int dp)
202 printf(".nr %s 0\\w'%s'\n", nregname(wd), s);
203 if (ht)
204 printf(".nr %s 0-\\n[bbury]\n", nregname(ht));
205 if (dp)
206 printf(".nr %s 0\\n[bblly]\n", nregname(dp));
209 static int box_suprise(struct box *box)
211 if (TS_0(box->style))
212 return e_sup3;
213 return box->style == TS_D ? e_sup1 : e_sup2;
216 void box_sub(struct box *box, struct box *sub, struct box *sup)
218 int box_wd = nregmk();
219 int box_wdnoic = nregmk();
220 int box_dp = nregmk();
221 int box_ht = nregmk();
222 int sub_wd = nregmk();
223 int sup_wd = nregmk();
224 int all_wd = nregmk();
225 int sup_dp = nregmk();
226 int sub_ht = nregmk();
227 int sup_rise = nregmk();
228 int sub_fall = nregmk();
229 int tmp_18e = nregmk();
230 int sub_cor = nregmk();
231 if (sub)
232 box_italiccorrection(sub);
233 if (sup)
234 box_italiccorrection(sup);
235 if (sub)
236 tok_dim(box_toreg(box), box_wdnoic, 0, 0);
237 box_italiccorrection(box);
238 printf(".ps %s\n", nreg(box->szreg));
239 tok_dim(box_toreg(box), box_wd, box_ht, box_dp);
240 box_putf(box, "\\h'5m/100u'");
241 if (sup) {
242 tok_dim(box_toreg(sup), sup_wd, 0, sup_dp);
243 /* 18a */
244 printf(".nr %s 0%su-(%dm/100u)\n",
245 nregname(sup_rise), nreg(box_ht), e_supdrop);
246 /* 18c */
247 printf(".if %s<(%dm/100u) .nr %s (%dm/100u)\n",
248 nreg(sup_rise), box_suprise(box),
249 nregname(sup_rise), box_suprise(box));
250 printf(".if %s<(%s+(%dm/100u/4)) .nr %s 0%s+(%dm/100u/4)\n",
251 nreg(sup_rise), nreg(sup_dp), e_xheight,
252 nregname(sup_rise), nreg(sup_dp), e_xheight);
254 if (sub) {
255 tok_dim(box_toreg(sub), sub_wd, sub_ht, 0);
256 /* 18a */
257 printf(".nr %s 0%su+(%dm/100u)\n",
258 nregname(sub_fall), nreg(box_dp), e_subdrop);
260 if (sub && !sup) {
261 /* 18b */
262 printf(".if %s<(%dm/100u) .nr %s (%dm/100u)\n",
263 nreg(sub_fall), e_sub1,
264 nregname(sub_fall), e_sub1);
265 printf(".if %s<(%s-(%dm/100u*4/5)) .nr %s 0%s-(%dm/100u*4/5)\n",
266 nreg(sub_fall), nreg(sub_ht), e_xheight,
267 nregname(sub_fall), nreg(sub_ht), e_xheight);
269 if (sub && sup) {
270 /* 18d */
271 printf(".if %s<(%dm/100u) .nr %s (%dm/100u)\n",
272 nreg(sub_fall), e_sub2,
273 nregname(sub_fall), e_sub2);
274 /* 18e */
275 printf(".if (%s-%s)-(%s-%s)<(%dm/100u*4) \\{\\\n",
276 nreg(sup_rise), nreg(sup_dp),
277 nreg(sub_ht), nreg(sub_fall), e_rulethickness);
278 printf(".nr %s (%dm/100u*4)+%s-(%s-%s)\n",
279 nregname(sub_fall), e_rulethickness,
280 nreg(sub_ht), nreg(sup_rise), nreg(sup_dp));
281 printf(".nr %s (%dm/100u*4/5)-(%s-%s)\n",
282 nregname(tmp_18e), e_xheight,
283 nreg(sup_rise), nreg(sup_dp));
284 printf(".if %s>0 .nr %s +%s\n",
285 nreg(tmp_18e), nregname(sup_rise), nreg(tmp_18e));
286 printf(".if %s>0 .nr %s -%s \\}\n",
287 nreg(tmp_18e), nregname(sub_fall), nreg(tmp_18e));
289 /* writing the superscript */
290 if (sup) {
291 box_putf(box, "\\v'-%su'%s\\v'%su'",
292 nreg(sup_rise), box_toreg(sup), nreg(sup_rise));
293 if (sub)
294 box_putf(box, "\\h'-%su'", nreg(sup_wd));
296 /* writing the subscript */
297 if (sub) {
298 /* subscript correction */
299 printf(".nr %s (%s+%s)*(%s-%s)/%s\n", nregname(sub_cor),
300 nreg(box_ht), nreg(sub_fall),
301 nreg(box_wd), nreg(box_wdnoic), nreg(box_ht));
302 printf(".nr %s -%s\n", nregname(sub_wd), nreg(sub_cor));
303 box_putf(box, "\\h'-%su'", nreg(sub_cor));
304 box_putf(box, "\\v'%su'%s\\v'-%su'",
305 nreg(sub_fall), box_toreg(sub), nreg(sub_fall));
306 if (sup) {
307 box_putf(box, "\\h'-%su'", nreg(sub_wd));
308 roff_max(all_wd, sub_wd, sup_wd);
309 box_putf(box, "\\h'+%su'", nreg(all_wd));
312 box_putf(box, "\\h'%dm/100u'", e_scriptspace);
313 nregrm(box_wd);
314 nregrm(box_wdnoic);
315 nregrm(box_dp);
316 nregrm(box_ht);
317 nregrm(sub_wd);
318 nregrm(sup_wd);
319 nregrm(all_wd);
320 nregrm(sup_dp);
321 nregrm(sub_ht);
322 nregrm(sup_rise);
323 nregrm(sub_fall);
324 nregrm(tmp_18e);
325 nregrm(sub_cor);
328 void box_from(struct box *box, struct box *lim, struct box *llim, struct box *ulim)
330 int lim_wd = nregmk(); /* box's width */
331 int lim_ht = nregmk(); /* box's height */
332 int lim_dp = nregmk(); /* box's depth */
333 int llim_wd = nregmk(); /* llim's width */
334 int ulim_wd = nregmk(); /* ulim's width */
335 int ulim_dp = nregmk(); /* ulim's depth */
336 int llim_ht = nregmk(); /* llim's height */
337 int ulim_rise = nregmk(); /* the position of ulim */
338 int llim_fall = nregmk(); /* the position of llim */
339 int all_wd = nregmk(); /* the width of all */
340 box_italiccorrection(lim);
341 box_beforeput(box, T_BIGOP, 0);
342 tok_dim(box_toreg(lim), lim_wd, lim_ht, lim_dp);
343 printf(".ps %s\n", nreg(box->szreg));
344 if (ulim)
345 tok_dim(box_toreg(ulim), ulim_wd, 0, ulim_dp);
346 if (llim)
347 tok_dim(box_toreg(llim), llim_wd, llim_ht, 0);
348 if (ulim && llim)
349 roff_max(all_wd, llim_wd, ulim_wd);
350 else
351 printf(".nr %s %s\n", nregname(all_wd),
352 ulim ? nreg(ulim_wd) : nreg(llim_wd));
353 printf(".if %s>%s .nr %s 0%s\n",
354 nreg(lim_wd), nreg(all_wd),
355 nregname(all_wd), nreg(lim_wd));
356 box_putf(box, "\\h'%su-%su/2u'", nreg(all_wd), nreg(lim_wd));
357 box_merge(box, lim, 0);
358 box_putf(box, "\\h'-%su/2u'", nreg(lim_wd));
359 if (ulim) {
360 /* 13a */
361 printf(".nr %s (%dm/100u)-%s\n",
362 nregname(ulim_rise), e_bigopspacing3, nreg(ulim_dp));
363 printf(".if %s<(%dm/100u) .nr %s (%dm/100u)\n",
364 nreg(ulim_rise), e_bigopspacing1,
365 nregname(ulim_rise), e_bigopspacing1);
366 printf(".nr %s +%s+%s\n",
367 nregname(ulim_rise), nreg(lim_ht), nreg(ulim_dp));
368 box_putf(box, "\\h'-%su/2u'\\v'-%su'%s\\v'%su'\\h'-%su/2u'",
369 nreg(ulim_wd), nreg(ulim_rise), box_toreg(ulim),
370 nreg(ulim_rise), nreg(ulim_wd));
372 if (llim) {
373 /* 13a */
374 printf(".nr %s (%dm/100u)-%s\n",
375 nregname(llim_fall), e_bigopspacing4, nreg(llim_ht));
376 printf(".if %s<(%dm/100u) .nr %s (%dm/100u)\n",
377 nreg(llim_fall), e_bigopspacing2,
378 nregname(llim_fall), e_bigopspacing2);
379 printf(".nr %s +%s+%s\n",
380 nregname(llim_fall), nreg(lim_dp), nreg(llim_ht));
381 box_putf(box, "\\h'-%su/2u'\\v'%su'%s\\v'-%su'\\h'-%su/2u'",
382 nreg(llim_wd), nreg(llim_fall), box_toreg(llim),
383 nreg(llim_fall), nreg(llim_wd));
385 box_putf(box, "\\h'%su/2u'", nreg(all_wd));
386 box_afterput(box, T_BIGOP);
387 nregrm(lim_wd);
388 nregrm(lim_ht);
389 nregrm(lim_dp);
390 nregrm(llim_wd);
391 nregrm(ulim_wd);
392 nregrm(ulim_dp);
393 nregrm(llim_ht);
394 nregrm(ulim_rise);
395 nregrm(llim_fall);
396 nregrm(all_wd);
399 /* return the width of s; len is the height plus depth */
400 static void tok_len(char *s, int wd, int len, int ht, int dp)
402 printf(".nr %s 0\\w'%s'\n", nregname(wd), s);
403 if (len)
404 printf(".nr %s 0\\n[bblly]-\\n[bbury]-2\n", nregname(len));
405 if (dp)
406 printf(".nr %s 0\\n[bblly]-1\n", nregname(dp));
407 if (ht)
408 printf(".nr %s 0-\\n[bbury]-1\n", nregname(ht));
411 /* len[0]: width, len[1]: vertical length, len[2]: height, len[3]: depth */
412 static void blen_mk(char *s, int len[4])
414 int i;
415 for (i = 0; i < 4; i++)
416 len[i] = nregmk();
417 tok_len(s, len[0], len[1], len[2], len[3]);
420 /* free the registers allocated with blen_mk() */
421 static void blen_rm(int len[4])
423 int i;
424 for (i = 0; i < 4; i++)
425 nregrm(len[i]);
428 /* build a fraction; the correct font should be set up beforehand */
429 void box_over(struct box *box, struct box *num, struct box *den)
431 int num_wd = nregmk();
432 int num_dp = nregmk();
433 int den_wd = nregmk();
434 int den_ht = nregmk();
435 int all_wd = nregmk();
436 int num_rise = nregmk();
437 int den_fall = nregmk();
438 int bar_wd = nregmk();
439 int bar_dp = nregmk();
440 int bar_ht = nregmk();
441 int bar_fall = nregmk();
442 int tmp_15d = nregmk();
443 int bargap = (TS_DX(box->style) ? 7 : 3) * e_rulethickness / 2;
444 box_beforeput(box, T_INNER, 0);
445 box_italiccorrection(num);
446 box_italiccorrection(den);
447 tok_dim(box_toreg(num), num_wd, 0, num_dp);
448 tok_dim(box_toreg(den), den_wd, den_ht, 0);
449 roff_max(all_wd, num_wd, den_wd);
450 printf(".ps %s\n", nreg(box->szreg));
451 tok_len("\\(ru", bar_wd, 0, bar_ht, bar_dp);
452 /* 15b */
453 printf(".nr %s 0%dm/100u\n",
454 nregname(num_rise), TS_DX(box->style) ? e_num1 : e_num2);
455 printf(".nr %s 0%dm/100u\n",
456 nregname(den_fall), TS_DX(box->style) ? e_denom1 : e_denom2);
457 /* 15d */
458 printf(".nr %s (%s-%s)-((%dm/100u)+(%dm/100u/2))\n",
459 nregname(tmp_15d), nreg(num_rise), nreg(num_dp),
460 e_axisheight, e_rulethickness);
461 printf(".if %s<(%dm/100u) .nr %s +(%dm/100u)-%s\n",
462 nreg(tmp_15d), bargap, nregname(num_rise),
463 bargap, nreg(tmp_15d));
464 printf(".nr %s ((%dm/100u)-(%dm/100u/2))-(%s-%s)\n",
465 nregname(tmp_15d), e_axisheight, e_rulethickness,
466 nreg(den_ht), nreg(den_fall));
467 printf(".if %s<(%dm/100u) .nr %s +(%dm/100u)-%s\n",
468 nreg(tmp_15d), bargap, nregname(den_fall),
469 bargap, nreg(tmp_15d));
470 /* calculating the vertical position of the bar */
471 printf(".nr %s 0-%s+%s/2-(%dm/100u)\n",
472 nregname(bar_fall), nreg(bar_dp),
473 nreg(bar_ht), e_axisheight);
474 /* making the bar longer */
475 printf(".nr %s +2*(%dm/100u)\n",
476 nregname(all_wd), e_overhang);
477 /* null delimiter space */
478 box_putf(box, "\\h'%sp*%du/100u'",nreg(box->szreg), e_nulldelim);
479 /* drawing the bar */
480 box_putf(box, "\\v'%su'\\f[\\n(.f]\\s[%s]\\l'%su'\\v'-%su'\\h'-%su/2u'",
481 nreg(bar_fall), nreg(box->szreg), nreg(all_wd),
482 nreg(bar_fall), nreg(all_wd));
483 /* output the numerator */
484 box_putf(box, "\\h'-%su/2u'", nreg(num_wd));
485 box_putf(box, "\\v'-%su'%s\\v'%su'",
486 nreg(num_rise), box_toreg(num), nreg(num_rise));
487 box_putf(box, "\\h'-%su/2u'", nreg(num_wd));
488 /* output the denominator */
489 box_putf(box, "\\h'-%su/2u'", nreg(den_wd));
490 box_putf(box, "\\v'%su'%s\\v'-%su'",
491 nreg(den_fall), box_toreg(den), nreg(den_fall));
492 box_putf(box, "\\h'(-%su+%su)/2u'", nreg(den_wd), nreg(all_wd));
493 box_putf(box, "\\h'%sp*%du/100u'",nreg(box->szreg), e_nulldelim);
494 box_afterput(box, T_INNER);
495 box_toreg(box);
496 nregrm(num_wd);
497 nregrm(num_dp);
498 nregrm(den_wd);
499 nregrm(den_ht);
500 nregrm(all_wd);
501 nregrm(num_rise);
502 nregrm(den_fall);
503 nregrm(bar_wd);
504 nregrm(bar_dp);
505 nregrm(bar_ht);
506 nregrm(bar_fall);
507 nregrm(tmp_15d);
510 /* choose the smallest bracket among br[], large enough for \n(ht+\n(dp */
511 static void box_bracketsel(int dst, int ht, int dp, char **br, int any, int both)
513 int i;
514 for (i = 0; br[i]; i++) {
515 printf(".if '%s'' ", sreg(dst));
516 printf(".if \\w'%s' ", br[i]); /* is this bracket available? */
517 if (both) { /* check both the height and the depth */
518 printf(".if (%s-(%dm/100)*2)<=(-\\n[bbury]+\\n[bblly]+(%dm/100*2)) ",
519 nreg(ht), e_rulethickness, e_axisheight);
520 printf(".if (%s*2)<=(-\\n[bbury]+\\n[bblly]-(%dm/100*2)) ",
521 nreg(dp), e_axisheight);
522 } else {
523 printf(".if (%s+%s)<=(-\\n[bbury]+\\n[bblly]) ", nreg(ht), nreg(dp));
525 printf(".ds %s \"%s\n", sregname(dst), br[i]);
527 if (any) /* choose the largest bracket, if any is 1 */
528 while (--i >= 0)
529 printf(".if '%s'' .if \\w'%s' .ds %s \"%s\n",
530 sreg(dst), br[i], sregname(dst), br[i]);
533 /* build a bracket using the provided pieces */
534 static void box_bracketmk(int dst, int len,
535 char *top, char *mid, char *bot, char *cen)
537 int toplen[4];
538 int midlen[4];
539 int botlen[4];
540 int cenlen[4];
541 int mid_cnt = nregmk(); /* number of mid glyphs to insert */
542 int mid_cur = nregmk(); /* the number of mid glyphs inserted */
543 int cen_pos = nregmk();
544 int buildmacro = sregmk();
545 blen_mk(top, toplen);
546 blen_mk(mid, midlen);
547 blen_mk(bot, botlen);
548 if (cen)
549 blen_mk(cen, cenlen);
550 /* the number of mid tokens necessary to cover sub */
551 if (!cen) {
552 printf(".nr %s %s*2-%s-%s*11/10/%s\n",
553 nregname(mid_cnt), nreg(len),
554 nreg(toplen[1]), nreg(botlen[1]), nreg(midlen[1]));
555 printf(".if %s<0 .nr %s 0\n", nreg(mid_cnt), nregname(mid_cnt));
556 } else { /* for brackets with a center like { */
557 printf(".nr %s %s-(%s+%s+%s/2)*11/10/%s\n",
558 nregname(cen_pos), nreg(len), nreg(cenlen[1]),
559 nreg(toplen[1]), nreg(botlen[1]), nreg(midlen[1]));
560 printf(".if %s<0 .nr %s 0\n", nreg(cen_pos), nregname(cen_pos));
561 printf(".nr %s 0%s*2\n", nregname(mid_cnt), nreg(cen_pos));
563 /* the macro to create the bracket; escaping backslashes */
564 printf(".de %s\n", sregname(buildmacro));
565 if (cen) /* inserting cen */
566 printf(".if \\%s=\\%s .as %s \"\\v'-\\%su'%s\\h'-\\%su'\\v'-\\%su'\n",
567 nreg(mid_cur), nreg(cen_pos), sregname(dst),
568 nreg(cenlen[3]), cen, nreg(cenlen[0]), nreg(cenlen[2]));
569 printf(".if \\%s<\\%s .as %s \"\\v'-\\%su'%s\\h'-\\%su'\\v'-\\%su'\n",
570 nreg(mid_cur), nreg(mid_cnt),
571 sregname(dst), nreg(midlen[3]),
572 mid, nreg(midlen[0]), nreg(midlen[2]));
573 printf(".if \\\\n+%s<\\%s .%s\n",
574 escarg(nregname(mid_cur)), nreg(mid_cnt), sregname(buildmacro));
575 printf("..\n");
576 /* constructing the bracket */
577 printf(".ds %s \"\\v'-%su'%s\\h'-%su'\\v'-%su'\n",
578 sregname(dst), nreg(botlen[3]),
579 bot, nreg(botlen[0]), nreg(botlen[2]));
580 printf(".nr %s 0 1\n", nregname(mid_cur));
581 printf(".%s\n", sregname(buildmacro));
582 printf(".as %s \"\\v'-%su'%s\\h'-%su'\\v'-%su'\n",
583 sregname(dst), nreg(toplen[3]), top,
584 nreg(toplen[0]), nreg(toplen[2]));
585 /* moving back vertically */
586 printf(".as %s \"\\v'%su*%su+%su+%su+%su'\n",
587 sregname(dst), nreg(mid_cnt), nreg(midlen[1]), nreg(botlen[1]),
588 nreg(toplen[1]), cen ? nreg(cenlen[1]) : "0");
589 /* moving right */
590 printf(".as %s \"\\h'%su'\n",
591 sregname(dst), cen ? nreg(cenlen[0]) : nreg(midlen[0]));
592 blen_rm(toplen);
593 blen_rm(midlen);
594 blen_rm(botlen);
595 if (cen)
596 blen_rm(cenlen);
597 nregrm(mid_cnt);
598 nregrm(mid_cur);
599 nregrm(cen_pos);
600 sregrm(buildmacro);
603 static void box_bracket(struct box *box, char *brac, int ht, int dp)
605 char *sizes[NSIZES] = {NULL};
606 char *top = NULL, *mid = NULL, *bot = NULL, *cen = NULL;
607 int dst = sregmk();
608 int len = nregmk();
609 int fall = nregmk();
610 int parlen[4];
611 roff_max(len, ht, dp);
612 def_sizes(brac, sizes);
613 printf(".ds %s \"\n", sregname(dst));
614 def_pieces(brac, &top, &mid, &bot, &cen);
615 box_bracketsel(dst, ht, dp, sizes, !mid, 1);
616 if (mid) {
617 printf(".if '%s'' \\{\\\n", sreg(dst));
618 box_bracketmk(dst, len, top, mid, bot, cen);
619 printf(". \\}\n");
621 /* calculating the total vertical length of the bracket */
622 blen_mk(sreg(dst), parlen);
623 /* calculating the amount the bracket should be moved downwards */
624 printf(".nr %s 0-%s+%s/2-(%sp*%du/100u)\n", nregname(fall),
625 nreg(parlen[3]), nreg(parlen[2]), nreg(box->szreg), e_axisheight);
626 /* printing the output */
627 box_putf(box, "\\f[\\n(.f]\\s[\\n(.s]\\v'%su'%s\\v'-%su'",
628 nreg(fall), sreg(dst), nreg(fall));
629 box_toreg(box);
630 blen_rm(parlen);
631 sregrm(dst);
632 nregrm(len);
633 nregrm(fall);
636 static char *bracsign(char *brac, int left)
638 if (brac[0] == 'c' && !strncmp("ceiling", brac, strlen(brac)))
639 return left ? "\\(lc" : "\\(rc";
640 if (brac[0] == 'f' && !strncmp("floor", brac, strlen(brac)))
641 return left ? "\\(lf" : "\\(rf";
642 if (brac[0] == '<' && brac[1] == '\0')
643 return "\\(la";
644 if (brac[0] == '>' && brac[1] == '\0')
645 return "\\(ra";
646 return brac;
649 /* build large brackets; the correct font should be set up beforehand */
650 void box_wrap(struct box *box, struct box *sub, char *left, char *right)
652 int sublen[4];
653 blen_mk(box_toreg(sub), sublen);
654 printf(".ps %s\n", nreg(box->szreg));
655 if (left) {
656 box_beforeput(box, T_LEFT, 0);
657 box_bracket(box, bracsign(left, 1), sublen[2], sublen[3]);
658 box_afterput(box, T_LEFT);
660 box_merge(box, sub, 0);
661 if (right) {
662 box_beforeput(box, T_RIGHT, 0);
663 box_bracket(box, bracsign(right, 0), sublen[2], sublen[3]);
664 box_afterput(box, T_RIGHT);
666 blen_rm(sublen);
669 /* construct a radical with height at least len and width wd in dst register */
670 static void sqrt_rad(int dst, int len, int wd)
672 char *sizes[NSIZES] = {NULL};
673 int srlen[4];
674 int rnlen[4];
675 int sr_sz = nregmk();
676 int wd_diff = nregmk(); /* if wd is shorter than \(rn */
677 int sr_rx = nregmk(); /* the right-most horizontal position of \(sr */
678 int rn_dx = nregmk(); /* horizontal displacement necessary for \(rn */
679 int len2 = nregmk();
680 int rad = sregmk();
681 char *top = NULL, *mid = NULL, *bot = NULL, *cen;
682 printf(".nr %s 0%s/2*11/10\n", nregname(len2), nreg(len));
683 printf(".ds %s \"\n", sregname(rad));
684 /* selecting a radical of the appropriate size */
685 def_pieces("\\(sr", &top, &mid, &bot, &cen);
686 def_sizes("\\(sr", sizes);
687 box_bracketsel(rad, len2, len2, sizes, 0, 0);
688 /* constructing the bracket if needed */
689 if (mid) {
690 printf(".if \\w'%s' ", mid);
691 printf(".if '%s'' \\{\\\n", sreg(rad));
692 box_bracketmk(rad, len2, top, mid, bot, NULL);
693 printf(". \\}\n");
695 /* enlarging \(sr if no suitable glyph was found */
696 printf(".if '%s'' \\{\\\n", sreg(rad));
697 blen_mk("\\(sr", srlen);
698 printf(".ie %s<(%s+%s) .nr %s 0\\n(.s\n",
699 nreg(len), nreg(srlen[2]), nreg(srlen[3]), nregname(sr_sz));
700 printf(".el .nr %s 0%s*\\n(.s/(%s+%s-(%dm/100u))+1\n",
701 nregname(sr_sz), nreg(len),
702 nreg(srlen[2]), nreg(srlen[3]), e_rulethickness);
703 printf(".ps %s\n", nreg(sr_sz));
704 printf(".ds %s \"\\(sr\n", sregname(rad));
705 blen_rm(srlen);
706 printf(". \\}\n");
707 /* adding the handle */
708 blen_mk(sreg(rad), srlen);
709 printf(".nr %s \\n[bburx]\n", nregname(sr_rx));
710 blen_mk("\\(rn", rnlen);
711 printf(".nr %s 0%s-\\n[bbllx]-(%dm/100u)\n",
712 nregname(rn_dx), nreg(sr_rx), e_rulethickness);
713 printf(".nr %s 0\n", nregname(wd_diff));
714 printf(".if %s<%s .nr %s 0%s-%s\n",
715 nreg(wd), nreg(rnlen[0]),
716 nregname(wd_diff), nreg(rnlen[0]), nreg(wd));
717 /* output the radical; align the top of the radical to the baseline */
718 printf(".ds %s \"\\s[\\n(.s]\\f[\\n(.f]"
719 "\\v'%su'\\h'%su'\\l'%su+%su\\(rn'\\h'-%su'\\v'-%su'"
720 "\\h'-%su-%su'\\v'%su'%s\\v'-%su'\\h'%su+%su'\n",
721 nregname(dst),
722 nreg(rnlen[2]), nreg(rn_dx), nreg(wd), nreg(wd_diff),
723 nreg(rn_dx), nreg(rnlen[2]), nreg(wd), nreg(wd_diff),
724 nreg(srlen[2]), sreg(rad), nreg(srlen[2]), nreg(wd), nreg(wd_diff));
725 blen_rm(srlen);
726 blen_rm(rnlen);
727 nregrm(sr_sz);
728 nregrm(wd_diff);
729 nregrm(sr_rx);
730 nregrm(rn_dx);
731 sregrm(rad);
734 void box_sqrt(struct box *box, struct box *sub)
736 int sublen[4];
737 int radlen[4];
738 int rad = sregmk();
739 int rad_rise = nregmk();
740 int min_ht = nregmk();
741 box_italiccorrection(sub);
742 box_beforeput(box, T_ORD, 0);
743 blen_mk(box_toreg(sub), sublen);
744 printf(".ps %s\n", nreg(box->szreg));
745 /* 11 */
746 printf(".nr %s 0%s+%s+(2*%dm/100u)+(%dm/100u/4)\n",
747 nregname(min_ht), nreg(sublen[2]), nreg(sublen[3]),
748 e_rulethickness,
749 TS_DX(box->style) ? e_xheight : e_rulethickness);
750 sqrt_rad(rad, min_ht, sublen[0]);
751 blen_mk(sreg(rad), radlen);
752 printf(".nr %s 0(%dm/100u)+(%dm/100u/4)\n",
753 nregname(rad_rise), e_rulethickness,
754 TS_DX(box->style) ? e_xheight : e_rulethickness);
755 printf(".if %s>(%s+%s+%s) .nr %s (%s+%s-%s-%s)/2\n",
756 nreg(radlen[3]), nreg(sublen[2]), nreg(sublen[3]),
757 nreg(rad_rise), nregname(rad_rise),
758 nreg(rad_rise), nreg(radlen[3]), nreg(sublen[2]),
759 nreg(sublen[3]));
760 printf(".nr %s +%s\n", nregname(rad_rise), nreg(sublen[2]));
761 /* output the radical */
762 box_putf(box, "\\v'-%su'%s\\v'%su'\\h'-%su'%s",
763 nreg(rad_rise), sreg(rad), nreg(rad_rise),
764 nreg(sublen[0]), box_toreg(sub));
765 box_afterput(box, T_ORD);
766 box_toreg(box);
767 blen_rm(sublen);
768 blen_rm(radlen);
769 sregrm(rad);
770 nregrm(rad_rise);
771 nregrm(min_ht);
774 void box_bar(struct box *box)
776 int box_wd = nregmk();
777 int box_ht = nregmk();
778 int bar_wd = nregmk();
779 int bar_dp = nregmk();
780 int bar_rise = nregmk();
781 box_italiccorrection(box);
782 printf(".ps %s\n", nreg(box->szreg));
783 tok_len("\\(ru", bar_wd, 0, 0, bar_dp);
784 tok_dim(box_toreg(box), box_wd, box_ht, 0);
785 printf(".if %su<(%dm/100u) .nr %s 0%dm/100u\n",
786 nreg(box_ht), e_xheight, nregname(box_ht), e_xheight);
787 printf(".nr %s 0%su+%su+(3*%dm/100u)\n",
788 nregname(bar_rise), nreg(box_ht),
789 nreg(bar_dp), e_rulethickness);
790 box_putf(box, "\\v'-%su'\\s%s\\f[\\n(.f]\\l'-%su\\(ru'\\v'%su'",
791 nreg(bar_rise), escarg(nreg(box->szreg)),
792 nreg(box_wd), nreg(bar_rise));
793 nregrm(box_wd);
794 nregrm(box_ht);
795 nregrm(bar_wd);
796 nregrm(bar_dp);
797 nregrm(bar_rise);
800 void box_accent(struct box *box, char *c)
802 int box_wd = nregmk();
803 int box_ht = nregmk();
804 int ac_rise = nregmk();
805 int ac_wd = nregmk();
806 int ac_dp = nregmk();
807 box_italiccorrection(box);
808 printf(".ps %s\n", nreg(box->szreg));
809 tok_len(c, ac_wd, 0, 0, ac_dp);
810 tok_dim(box_toreg(box), box_wd, box_ht, 0);
811 printf(".if %su<(%dm/100u) .nr %s 0%dm/100u\n",
812 nreg(box_ht), e_xheight, nregname(box_ht), e_xheight);
813 printf(".nr %s 0%su+%su+(%sp*10u/100u)\n",
814 nregname(ac_rise), nreg(box_ht),
815 nreg(ac_dp), nreg(box->szreg));
816 box_putf(box, "\\v'-%su'\\h'-%su-%su/2u'\\s%s\\f[\\n(.f]%s\\h'%su-%su/2u'\\v'%su'",
817 nreg(ac_rise), nreg(box_wd), nreg(ac_wd),
818 escarg(nreg(box->szreg)), c, nreg(box_wd),
819 nreg(ac_wd), nreg(ac_rise));
820 nregrm(box_wd);
821 nregrm(box_ht);
822 nregrm(ac_rise);
823 nregrm(ac_wd);
824 nregrm(ac_dp);
827 void box_under(struct box *box)
829 int box_wd = nregmk();
830 int box_dp = nregmk();
831 int bar_wd = nregmk();
832 int bar_ht = nregmk();
833 int bar_fall = nregmk();
834 box_italiccorrection(box);
835 printf(".ps %s\n", nreg(box->szreg));
836 tok_len("\\(ul", bar_wd, 0, bar_ht, 0);
837 tok_dim(box_toreg(box), box_wd, 0, box_dp);
838 printf(".if %s<0 .nr %s 0\n", nreg(box_dp), nregname(box_dp));
839 printf(".nr %s 0%su+%su+(3*%dm/100u)\n",
840 nregname(bar_fall), nreg(box_dp),
841 nreg(bar_ht), e_rulethickness);
842 box_putf(box, "\\v'%su'\\s%s\\f[\\n(.f]\\l'-%su\\(ul'\\v'-%su'",
843 nreg(bar_fall), escarg(nreg(box->szreg)),
844 nreg(box_wd), nreg(bar_fall));
845 nregrm(box_wd);
846 nregrm(box_dp);
847 nregrm(bar_wd);
848 nregrm(bar_ht);
849 nregrm(bar_fall);
852 char *box_toreg(struct box *box)
854 if (!box->reg) {
855 box->reg = sregmk();
856 printf(".ds %s \"%s\n", sregname(box->reg), box_buf(box));
858 return sreg(box->reg);
861 int box_empty(struct box *box)
863 return !strlen(box_buf(box));
866 void box_vcenter(struct box *box, struct box *sub)
868 int wd = nregmk();
869 int ht = nregmk();
870 int dp = nregmk();
871 int fall = nregmk();
872 box_beforeput(box, sub->tbeg, 0);
873 tok_dim(box_toreg(sub), wd, ht, dp);
874 printf(".nr %s 0-%s+%s/2-(%sp*%du/100u)\n", nregname(fall),
875 nreg(dp), nreg(ht), nreg(box->szreg), e_axisheight);
876 box_putf(box, "\\v'%su'%s\\v'-%su'",
877 nreg(fall), box_toreg(sub), nreg(fall));
878 box_toreg(box);
879 box_afterput(box, sub->tcur);
880 nregrm(wd);
881 nregrm(ht);
882 nregrm(dp);
883 nregrm(fall);
886 /* include line-space requests */
887 void box_vertspace(struct box *box)
889 int box_wd = nregmk();
890 int htroom = nregmk();
891 int dproom = nregmk();
892 box_italiccorrection(box);
893 /* amount of room available before and after this line */
894 printf(".nr %s 0+\\n(.vu-%sp+(%sp*%du/100u)\n",
895 nregname(htroom), nreg(box->szreg),
896 nreg(box->szreg), e_bodyheight);
897 printf(".nr %s 0+\\n(.vu-%sp+(%sp*%du/100u)\n",
898 nregname(dproom), nreg(box->szreg),
899 nreg(box->szreg), e_bodydepth);
900 /* appending \x requests */
901 tok_dim(box_toreg(box), box_wd, 0, 0);
902 printf(".if -\\n[bbury]>%s .as %s \"\\x'\\n[bbury]u+%su'\n",
903 nreg(htroom), sregname(box->reg), nreg(htroom));
904 printf(".if \\n[bblly]>%s .as %s \"\\x'\\n[bblly]u-%su'\n",
905 nreg(dproom), sregname(box->reg), nreg(dproom));
906 nregrm(box_wd);
907 nregrm(htroom);
908 nregrm(dproom);
911 /* put the current width to the given number register */
912 void box_markpos(struct box *box, char *reg)
914 box->tomark = reg;
917 /* initialize the length of a pile or column of a matrix */
918 static void box_colinit(struct box **pile, int n,
919 int plen[][4], int wd, int ht)
921 int i;
922 for (i = 0; i < n; i++)
923 if (pile[i])
924 box_italiccorrection(pile[i]);
925 for (i = 0; i < n; i++)
926 blen_mk(pile[i] ? box_toreg(pile[i]) : "", plen[i]);
927 printf(".nr %s 0%s\n", nregname(wd), nreg(plen[0][0]));
928 printf(".nr %s 0%s\n", nregname(ht), nreg(plen[0][2]));
929 /* finding the maximum width */
930 for (i = 1; i < n; i++) {
931 printf(".if %s>%s .nr %s 0+%s\n",
932 nreg(plen[i][0]), nreg(wd),
933 nregname(wd), nreg(plen[i][0]));
935 /* finding the maximum height (vertical length) */
936 for (i = 1; i < n; i++) {
937 printf(".if %s+%s>%s .nr %s 0+%s+%s\n",
938 nreg(plen[i - 1][3]), nreg(plen[i][2]), nreg(ht),
939 nregname(ht), nreg(plen[i - 1][3]), nreg(plen[i][2]));
941 /* maximum height and the depth of the last row */
942 printf(".if %s>%s .nr %s 0+%s\n",
943 nreg(plen[n - 1][3]), nreg(ht),
944 nregname(ht), nreg(plen[n - 1][3]));
947 /* append the give pile to box */
948 static void box_colput(struct box **pile, int n, struct box *box,
949 int adj, int plen[][4], int wd, int ht)
951 int i;
952 box_putf(box, "\\v'-%du*%su/2u'", n - 1, nreg(ht));
953 /* adding the entries */
954 for (i = 0; i < n; i++) {
955 if (adj == 'c')
956 box_putf(box, "\\h'%su-%su/2u'",
957 nreg(wd), nreg(plen[i][0]));
958 if (adj == 'r')
959 box_putf(box, "\\h'%su-%su'",
960 nreg(wd), nreg(plen[i][0]));
961 box_putf(box, "\\v'%su'%s", i ? nreg(ht) : "0",
962 pile[i] ? box_toreg(pile[i]) : "");
963 if (adj == 'l')
964 box_putf(box, "\\h'-%su'", nreg(plen[i][0]));
965 if (adj == 'c')
966 box_putf(box, "\\h'-%su+(%su-%su/2u)'",
967 nreg(wd), nreg(wd), nreg(plen[i][0]));
968 if (adj == 'r')
969 box_putf(box, "\\h'-%su'", nreg(wd));
971 box_putf(box, "\\v'-%du*%su/2u'\\h'%su'", n - 1, nreg(ht), nreg(wd));
974 /* free the registers allocated for this pile */
975 static void box_coldone(struct box **pile, int n, int plen[][4])
977 int i;
978 for (i = 0; i < n; i++)
979 blen_rm(plen[i]);
982 /* calculate the number of entries in the given pile */
983 static int box_colnrows(struct box *cols[])
985 int n = 0;
986 while (n < NPILES && cols[n])
987 n++;
988 return n;
991 void box_pile(struct box *box, struct box **pile, int adj, int rowspace)
993 int plen[NPILES][4];
994 int max_wd = nregmk();
995 int max_ht = nregmk();
996 int n = box_colnrows(pile);
997 box_beforeput(box, T_INNER, 0);
998 box_colinit(pile, n, plen, max_wd, max_ht);
999 /* inserting spaces between entries */
1000 printf(".if %s<(%sp*%du/100u) .nr %s (%sp*%du/100u)\n",
1001 nreg(max_ht), nreg(box->szreg), e_baselinesep,
1002 nregname(max_ht), nreg(box->szreg), e_baselinesep);
1003 if (rowspace)
1004 printf(".nr %s +(%sp*%du/100u)\n",
1005 nregname(max_ht), nreg(box->szreg), rowspace);
1006 /* adding the entries */
1007 box_colput(pile, n, box, adj, plen, max_wd, max_ht);
1008 box_coldone(pile, n, plen);
1009 box_afterput(box, T_INNER);
1010 box_toreg(box);
1011 nregrm(max_wd);
1012 nregrm(max_ht);
1015 void box_matrix(struct box *box, int ncols, struct box *cols[][NPILES],
1016 int *adj, int colspace, int rowspace)
1018 int plen[NPILES][NPILES][4];
1019 int wd[NPILES];
1020 int ht[NPILES];
1021 int max_ht = nregmk();
1022 int max_wd = nregmk();
1023 int nrows = 0;
1024 int i;
1025 box_beforeput(box, T_INNER, 0);
1026 for (i = 0; i < ncols; i++)
1027 if (box_colnrows(cols[i]) > nrows)
1028 nrows = box_colnrows(cols[i]);
1029 for (i = 0; i < ncols; i++)
1030 wd[i] = nregmk();
1031 for (i = 0; i < ncols; i++)
1032 ht[i] = nregmk();
1033 /* initializing the columns */
1034 for (i = 0; i < ncols; i++)
1035 box_colinit(cols[i], nrows, plen[i], wd[i], ht[i]);
1036 /* finding the maximum width and height */
1037 printf(".nr %s 0%s\n", nregname(max_wd), nreg(wd[0]));
1038 printf(".nr %s 0%s\n", nregname(max_ht), nreg(ht[0]));
1039 for (i = 1; i < ncols; i++) {
1040 printf(".if %s>%s .nr %s 0+%s\n",
1041 nreg(wd[i]), nreg(max_wd),
1042 nregname(max_wd), nreg(wd[i]));
1044 for (i = 1; i < ncols; i++) {
1045 printf(".if %s>%s .nr %s 0+%s\n",
1046 nreg(ht[i]), nreg(max_ht),
1047 nregname(max_ht), nreg(ht[i]));
1049 /* inserting spaces between rows */
1050 printf(".if %s<(%sp*%du/100u) .nr %s (%sp*%du/100u)\n",
1051 nreg(max_ht), nreg(box->szreg), e_baselinesep,
1052 nregname(max_ht), nreg(box->szreg), e_baselinesep);
1053 if (rowspace)
1054 printf(".nr %s +(%sp*%du/100u)\n",
1055 nregname(max_ht), nreg(box->szreg), rowspace);
1056 /* printing the columns */
1057 for (i = 0; i < ncols; i++) {
1058 if (i) /* space between columns */
1059 box_putf(box, "\\h'%sp*%du/100u'",
1060 nreg(box->szreg), e_columnsep + colspace);
1061 box_colput(cols[i], nrows, box, adj[i],
1062 plen[i], max_wd, max_ht);
1064 box_afterput(box, T_INNER);
1065 box_toreg(box);
1066 for (i = 0; i < ncols; i++)
1067 box_coldone(cols[i], nrows, plen[i]);
1068 for (i = 0; i < ncols; i++)
1069 nregrm(ht[i]);
1070 for (i = 0; i < ncols; i++)
1071 nregrm(wd[i]);
1072 nregrm(max_wd);
1073 nregrm(max_ht);