xref: /plan9/sys/src/cmd/eqn/shift.c (revision 3e12c5d1bb89fc02707907988834ef147769ddaf)
1 #include "e.h"
2 #include "y.tab.h"
3 
subsup(int p1,int p2,int p3)4 void subsup(int p1, int p2, int p3)
5 {
6 	if (p2 != 0 && p3 != 0)
7 		shift2(p1, p2, p3);
8 	else if (p2 != 0)
9 		bshiftb(p1, SUB, p2);
10 	else if (p3 != 0)
11 		bshiftb(p1, SUP, p3);
12 }
13 
14 extern double Subbase, Supshift;
15 extern char *Sub1space, *Sup1space, *Sub2space;
16 extern char *SS1space, *SS2space;
17 
bshiftb(int p1,int dir,int p2)18 void bshiftb(int p1, int dir, int p2)
19 {
20 	int subps, n;
21 	double shval, d1, h1, b1, h2, b2;
22 	char *sh1, *sh2;
23 
24 	yyval = p1;
25 	h1 = eht[p1];
26 	b1 = ebase[p1];
27 	h2 = eht[p2];
28 	b2 = ebase[p2];
29 	subps = ps;
30 	ps += deltaps;
31 	if (dir == SUB) {
32 		/* base .2m below bottom of main box */
33 		shval = b1 + EM(Subbase, ps);
34 		ebase[yyval] = shval + b2;
35 		eht[yyval] = max(h1-b1+shval+b2, h2);
36 		if (rfont[p1] == ITAL && lfont[p2] == ROM)
37 			n = 2;		/* Sub1space */
38 		else
39 			n = max(2, class[rclass[p1]][lclass[p2]]);
40 		sh1 = pad(n);
41 		rclass[p1] = OTHER;	/* OTHER leaves too much after sup */
42 	} else {	/* superscript */
43 		/* 4/10 up main box */
44 		d1 = EM(Subbase, subps);
45 		ebase[yyval] = b1;
46 		shval = -(Supshift * (h1-b1)) - b2;
47 		if (Supshift*(h1-b1) + h2 < h1-b1)	/* raise little super */
48 			shval = -(h1-b1) + h2-b2 - d1;
49 		eht[yyval] = h1 + max(0, h2 - (1-Supshift)*(h1-b1));
50 		if (rclass[p1] == ILETF)
51 			n = 4;
52 		else if (rfont[p1] == ITAL)
53 			n = 2;		/* Sup1space */
54 		else
55 			n = max(1, class[rclass[p1]][lclass[p2]]);
56 		sh1 = pad(n);
57 		rclass[p1] = rclass[p2];	/* OTHER leaves too much after sup */
58 	}
59 	dprintf(".\tS%d <- %d shift %g %d; b=%g, h=%g, ps=%d, subps=%d\n",
60 		yyval, p1, shval, p2, ebase[yyval], eht[yyval], ps, subps);
61 	sh2 = Sub2space;	/* was Sub2space; */
62 	printf(".as %d \\v'%gm'%s%s\\*(%d%s%s\\v'%gm'\n",
63 		yyval, REL(shval,ps), DPS(ps,subps), sh1, p2,
64 		DPS(subps,ps), sh2, REL(-shval,ps));
65 	rfont[p1] = 0;
66 	sfree(p2);
67 }
68 
shift2(int p1,int p2,int p3)69 void shift2(int p1, int p2, int p3)
70 {
71 	int subps;
72 	double h1, h2, h3, b1, b2, b3, subsh, d2, supsh;
73 	int treg;
74 	char *sh2;
75 
76 	treg = salloc();
77 	yyval = p1;
78 	subps = ps;	/* sub and sup at this size */
79 	ps += deltaps;	/* outer size */
80 	h1 = eht[p1]; b1 = ebase[p1];
81 	h2 = eht[p2]; b2 = ebase[p2];
82 	h3 = eht[p3]; b3 = ebase[p3];
83 	subsh = EM(Subbase, ps);
84 	if (b1 > b2 + subsh) /* move little sub down */
85 		subsh += b1;
86 	eht[yyval] = max(subsh+b2-b1+h1, h2);
87 	supsh = -Supshift*(h1-b1) - b3;
88 	d2 = EM(Subbase, subps);
89 	if (h3 < (1-Supshift)*(h1-b1))
90 		supsh = -(h1-b1) + (h3-b3) - d2;
91 	ebase[yyval] = subsh + b2 - b1;
92 	eht[yyval] = h1 + subsh+b2-b1 + max(0, h3-(1-Supshift)*(h1-b1));
93 	dprintf(".\tS%d <- %d sub %d sup %d, ps=%d, subps=%d, h=%g, b=%g\n",
94 		yyval, p1, p2, p3, ps, subps, eht[yyval], ebase[yyval]);
95 	if (rclass[p1] == ILETF)
96 		sh2 = "\\|\\|";
97 	else
98 		sh2 = SS2space;
99 	/*n = max(class[rclass[p1]][lclass[p2]], class[rclass[p1]][lclass[p3]]);
100 	/*sh2 = pad(max(2, n));
101 	*/
102 	printf(".ds %d %s\\*(%d\n", p2, SS1space, p2);
103 	nrwid(p2, subps, p2);
104 	printf(".ds %d %s\\*(%d\n", p3, sh2, p3);
105 	nrwid(p3, subps, p3);
106 	printf(".nr %d \\n(%d\n", treg, p3);
107 	printf(".if \\n(%d>\\n(%d .nr %d \\n(%d\n", p2, treg, treg, p2);
108 	printf(".as %d %s\\v'%gm'\\*(%d\\v'%gm'\\h'-\\n(%du'\\\n",
109 		p1, DPS(ps,subps), REL(subsh,subps), p2, REL(-subsh,subps), p2);
110 	printf("\\v'%gm'\\*(%d\\v'%gm'\\h'-\\n(%du+\\n(%du'%s%s\n",
111 		REL(supsh,subps), p3, REL(-supsh,subps), p3, treg, DPS(subps,ps), Sub2space);
112 	if (rfont[p2] == ITAL)
113 		rfont[yyval] = 0;	/* lie */
114 	rclass[yyval] = rclass[p3];	/* was OTHER */
115 	sfree(p2); sfree(p3); sfree(treg);
116 }
117