xref: /illumos-gate/usr/src/cmd/eqn/shift.c (revision 82079dec87a9b623b65337b7422dc7c27a04f0d9)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1980 Regents of the University of California.
11  * All rights reserved. The Berkeley software License Agreement
12  * specifies the terms and conditions for redistribution.
13  */
14 
15 #include "e.h"
16 #include "e.def"
17 
18 void
19 bshiftb(int p1, int dir, int p2)
20 {
21 	int shval, d1, h1, b1, h2, b2;
22 #ifndef NEQN
23 	int diffps, effps, effps2;
24 	char *sh1, *sh2;
25 #endif	/* NEQN */
26 
27 	yyval = p1;
28 	h1 = eht[p1];
29 	b1 = ebase[p1];
30 	h2 = eht[p2];
31 	b2 = ebase[p2];
32 #ifndef NEQN
33 	effps = EFFPS(ps);
34 	effps2 = EFFPS(ps+deltaps);
35 	diffps = deltaps;
36 	sh1 = sh2 = "";
37 #endif	/* NEQN */
38 	if (dir == SUB) {	/* subscript */
39 #ifndef NEQN
40 		/* top 1/2m above bottom of main box */
41 		d1 = VERT(EM(0.5, effps2));
42 #else	/* NEQN */
43 		d1 = VERT(1);
44 #endif	/* NEQN */
45 		shval = - d1 + h2 - b2;
46 		if (d1+b1 > h2) /* move little sub down */
47 			shval = b1-b2;
48 		ebase[yyval] = b1 + max(0, h2-b1-d1);
49 		eht[yyval] = h1 + max(0, h2-b1-d1);
50 #ifndef NEQN
51 		if (rfont[p1] == ITAL && lfont[p2] == ROM)
52 			sh1 = "\\|";
53 		if (rfont[p2] == ITAL)
54 			sh2 = "\\|";
55 #endif	/* NEQN */
56 	} else {	/* superscript */
57 #ifndef NEQN
58 		/* 4/10 up main box */
59 		d1 = VERT(EM(0.2, effps));
60 #else	/* NEQN */
61 		d1 = VERT(1);
62 #endif	/* NEQN */
63 		ebase[yyval] = b1;
64 #ifndef NEQN
65 		shval = -VERT((4 * (h1-b1)) / 10) - b2;
66 		if (VERT(4*(h1-b1)/10) + h2 < h1-b1)	/* raise little super */
67 #else	/* NEQN */
68 		shval = -VERT(1) - b2;
69 		if (VERT(1) + h2 < h1-b1)	/* raise little super */
70 #endif	/* NEQN */
71 			shval = -(h1-b1) + h2-b2 - d1;
72 #ifndef NEQN
73 		eht[yyval] = h1 + max(0, h2-VERT((6*(h1-b1))/10));
74 		if (rfont[p1] == ITAL)
75 			sh1 = "\\|";
76 		if (rfont[p2] == ITAL)
77 			sh2 = "\\|";
78 #else	/* NEQN */
79 		eht[yyval] = h1 + max(0, h2 - VERT(1));
80 #endif	/* NEQN */
81 	}
82 	if (dbg)
83 		printf(".\tb:b shift b: S%d <- S%d vert %d S%d vert %d; "
84 		    "b=%d, h=%d\n", yyval, p1, shval, p2, -shval,
85 		    ebase[yyval], eht[yyval]);
86 #ifndef NEQN
87 	printf(".as %d \\v'%du'\\s-%d%s\\*(%d\\s+%d%s\\v'%du'\n",
88 	    yyval, shval, diffps, sh1, p2, diffps, sh2, -shval);
89 	ps += deltaps;
90 	if (rfont[p2] == ITAL)
91 		rfont[p1] = 0;
92 	else
93 		rfont[p1] = rfont[p2];
94 #else	/* NEQN */
95 	printf(".as %d \\v'%du'\\*(%d\\v'%du'\n",
96 	    yyval, shval, p2, -shval);
97 #endif	/* NEQN */
98 	ofree(p2);
99 }
100 
101 void
102 shift(int p1)
103 {
104 	ps -= deltaps;
105 	yyval = p1;
106 	if (dbg) printf(".\tshift: %d;ps=%d\n", yyval, ps);
107 }
108 
109 void
110 shift2(int p1, int p2, int p3)
111 {
112 	int effps, h1, h2, h3, b1, b2, b3, subsh, d1, d2, supsh, treg;
113 #ifndef NEQN
114 	int effps2;
115 #endif	/* NEQN */
116 
117 	treg = oalloc();
118 	yyval = p1;
119 	if (dbg) printf(".\tshift2 s%d <- %d %d %d\n", yyval, p1, p2, p3);
120 	effps = EFFPS(ps+deltaps);
121 #ifndef NEQN
122 	eht[p3] = h3 = VERT((eht[p3] * effps) / EFFPS(ps));
123 	ps += deltaps;
124 	effps2 = EFFPS(ps+deltaps);
125 #endif	/* NEQN */
126 	h1 = eht[p1]; b1 = ebase[p1];
127 	h2 = eht[p2]; b2 = ebase[p2];
128 #ifndef NEQN
129 	b3 = ebase[p3];
130 	d1 = VERT(EM(0.5, effps2));
131 #else	/* NEQN */
132 	h3 = eht[p3]; b3 = ebase[p3];
133 	d1 = VERT(1);
134 #endif	/* NEQN */
135 	subsh = -d1+h2-b2;
136 	if (d1+b1 > h2) /* move little sub down */
137 		subsh = b1-b2;
138 #ifndef NEQN
139 	supsh = -VERT((4*(h1-b1))/10) - b3;
140 	d2 = VERT(EM(0.2, effps));
141 	if (VERT(4*(h1-b1)/10)+h3 < h1-b1)
142 #else	/* NEQN */
143 	supsh = - VERT(1) - b3;
144 	d2 = VERT(1);
145 	if (VERT(1)+h3 < h1-b1)
146 #endif	/* NEQN */
147 		supsh = -(h1-b1) + (h3-b3) - d2;
148 #ifndef NEQN
149 	eht[yyval] = h1 + max(0, h3-VERT((6*(h1-b1))/10)) + max(0, h2-b1-d1);
150 #else	/* NEQN */
151 	eht[yyval] = h1 + max(0, h3-VERT(1)) + max(0, h2-b1-d1);
152 #endif	/* NEQN */
153 	ebase[yyval] = b1+max(0, h2-b1-d1);
154 #ifndef NEQN
155 	if (rfont[p1] == ITAL && lfont[p2] == ROM)
156 		printf(".ds %d \\|\\*(%d\n", p2, p2);
157 	if (rfont[p2] == ITAL)
158 		printf(".as %d \\|\n", p2);
159 #endif	/* NEQN */
160 	nrwid(p2, effps, p2);
161 #ifndef NEQN
162 	if (rfont[p1] == ITAL && lfont[p3] == ROM)
163 		printf(".ds %d \\|\\|\\*(%d\n", p3, p3);
164 	else
165 		printf(".ds %d \\|\\*(%d\n", p3, p3);
166 #endif	/* NEQN */
167 	nrwid(p3, effps, p3);
168 	printf(".nr %d \\n(%d\n", treg, p3);
169 	printf(".if \\n(%d>\\n(%d .nr %d \\n(%d\n", p2, treg, treg, p2);
170 #ifndef NEQN
171 	printf(".as %d \\v'%du'\\s%d\\*(%d\\h'-\\n(%du'\\v'%du'\\\n",
172 	    p1, subsh, effps, p2, p2, -subsh+supsh);
173 	printf("\\s%d\\*(%d\\h'-\\n(%du+\\n(%du'\\s%d\\v'%du'\n",
174 	    effps, p3, p3, treg, effps2, -supsh);
175 #else	/* NEQN */
176 	printf(".as %d \\v'%du'\\*(%d\\h'-\\n(%du'\\v'%du'\\\n",
177 	    p1, subsh, p2, p2, -subsh+supsh);
178 	printf("\\*(%d\\h'-\\n(%du+\\n(%du'\\v'%du'\n",
179 	    p3, p3, treg, -supsh);
180 #endif	/* NEQN */
181 	ps += deltaps;
182 #ifndef NEQN
183 	if (rfont[p2] == ITAL)
184 		rfont[yyval] = 0;	/* lie */
185 #endif	/* NEQN */
186 	ofree(p2); ofree(p3); ofree(treg);
187 }
188