1 /*-
2  * Copyright (c) 1979, 1980, 1981, 1986, 1988, 1990, 1991, 1992
3  *     The Regents of the University of California.
4  * Copyright (C) Caldera International Inc.  2001-2002.
5  * Copyright (c) 2003, 2004
6  *	Thorsten "mirabilos" Glaser <tg@mirbsd.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms,
10  * with or without modification, are permitted provided
11  * that the following conditions are met:
12  *
13  * Redistributions of source code and documentation must retain
14  * the above copyright notice, this list of conditions and the
15  * following disclaimer.  Redistributions in binary form must
16  * reproduce the above copyright notice, this list of conditions
17  * and the following disclaimer in the documentation and/or other
18  * materials provided with the distribution.
19  *
20  * All advertising materials mentioning features or use of this
21  * software must display the following acknowledgement:
22  *   This product includes software developed or owned by
23  *   Caldera International, Inc.
24  *
25  * Neither the name of Caldera International, Inc. nor the names
26  * of other contributors may be used to endorse or promote products
27  * derived from this software without specific prior written permission.
28  *
29  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
30  * INTERNATIONAL, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
31  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
34  * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
35  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
37  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
38  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
39  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
40  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41  */
42 
43 #include <sys/cdefs.h>
44 __SCCSID("@(#)shift.c	4.4 (Berkeley) 4/17/91");
45 __RCSID("$MirOS: src/usr.bin/oldroff/neqn/shift.c,v 1.3 2008/11/08 23:04:37 tg Exp $");
46 
47 # include "e.h"
48 #include "ey.h"
49 
bshiftb(p1,dir,p2)50 bshiftb(p1, dir, p2) int p1, dir, p2; {
51 	int shval, d1, h1, b1, h2, b2;
52 #ifndef NEQN
53 	int diffps, effps, effps2;
54 	char *sh1, *sh2;
55 #endif
56 
57 	yyval = p1;
58 	h1 = eht[p1];
59 	b1 = ebase[p1];
60 	h2 = eht[p2];
61 	b2 = ebase[p2];
62 #ifndef NEQN
63 	effps = EFFPS(ps);
64 	effps2 = EFFPS(ps+deltaps);
65 	diffps = deltaps;
66 	sh1 = sh2 = "";
67 #endif
68 	if( dir == SUB ) {	/* subscript */
69 #ifndef NEQN
70 		/* top 1/2m above bottom of main box */
71 		d1 = VERT( (effps2*6)/2 );
72 #else
73 		d1 = VERT(1);
74 #endif
75 		shval = - d1 + h2 - b2;
76 		if( d1+b1 > h2 ) /* move little sub down */
77 			shval = b1-b2;
78 		ebase[yyval] = b1 + max(0, h2-b1-d1);
79 		eht[yyval] = h1 + max(0, h2-b1-d1);
80 #ifndef NEQN
81 		if (rfont[p1] == ITAL && lfont[p2] == ROM)
82 			sh1 = "\\|";
83 		if (rfont[p2] == ITAL)
84 			sh2 = "\\|";
85 #endif
86 	} else {	/* superscript */
87 #ifndef NEQN
88 		/* 4/10 up main box */
89 		d1 = VERT( (effps*6*2)/10 );
90 #else
91 		d1 = VERT(1);
92 #endif
93 		ebase[yyval] = b1;
94 #ifndef NEQN
95 		shval = -VERT( (4 * (h1-b1)) / 10 ) - b2;
96 		if( VERT(4*(h1-b1)/10) + h2 < h1-b1 )	/* raise little super */
97 #else
98 		shval = -VERT(1) - b2;
99 		if( VERT(1) + h2 < h1-b1 )	/* raise little super */
100 #endif
101 			shval = -(h1-b1) + h2-b2 - d1;
102 #ifndef NEQN
103 		eht[yyval] = h1 + max(0, h2-VERT((6*(h1-b1))/10));
104 		if (rfont[p1] == ITAL)
105 			sh1 = "\\|";
106 		if (rfont[p2] == ITAL)
107 			sh2 = "\\|";
108 #else
109 		eht[yyval] = h1 + max(0, h2 - VERT(1));
110 #endif
111 	}
112 	if(dbg)printf(".\tb:b shift b: S%d <- S%d vert %d S%d vert %d; b=%d, h=%d\n",
113 		yyval, p1, shval, p2, -shval, ebase[yyval], eht[yyval]);
114 #ifndef NEQN
115 	printf(".as %d \\v'%du'\\s-%d%s\\*(%d\\s+%d%s\\v'%du'\n",
116 		yyval, shval, diffps, sh1, p2, diffps, sh2, -shval);
117 	ps += deltaps;
118 	if (rfont[p2] == ITAL)
119 		rfont[p1] = 0;
120 	else
121 		rfont[p1] = rfont[p2];
122 #else
123 	printf(".as %d \\v'%du'\\*(%d\\v'%du'\n",
124 		yyval, shval, p2, -shval);
125 #endif
126 	ofree(p2);
127 }
128 
shift(p1)129 shift(p1) int p1; {
130 	ps -= deltaps;
131 	yyval = p1;
132 	if(dbg)printf(".\tshift: %d;ps=%d\n", yyval, ps);
133 }
134 
shift2(p1,p2,p3)135 shift2(p1, p2, p3) int p1, p2, p3; {
136 	int effps, h1, h2, h3, b1, b2, b3, subsh, d1, d2, supsh, treg;
137 #ifndef NEQN
138 	int effps2;
139 #endif
140 
141 	treg = oalloc();
142 	yyval = p1;
143 	if(dbg)printf(".\tshift2 s%d <- %d %d %d\n", yyval, p1, p2, p3);
144 	effps = EFFPS(ps+deltaps);
145 #ifndef NEQN
146 	eht[p3] = h3 = VERT( (eht[p3] * effps) / EFFPS(ps) );
147 	ps += deltaps;
148 	effps2 = EFFPS(ps+deltaps);
149 #endif
150 	h1 = eht[p1]; b1 = ebase[p1];
151 	h2 = eht[p2]; b2 = ebase[p2];
152 #ifndef NEQN
153 	b3 = ebase[p3];
154 	d1 = VERT( (effps2*6)/2 );
155 #else
156 	h3 = eht[p3]; b3 = ebase[p3];
157 	d1 = VERT(1);
158 #endif
159 	subsh = -d1+h2-b2;
160 	if( d1+b1 > h2 ) /* move little sub down */
161 		subsh = b1-b2;
162 #ifndef NEQN
163 	supsh = -VERT( (4*(h1-b1))/10 ) - b3;
164 	d2 = VERT( (effps*6*2)/10 );
165 	if( VERT(4*(h1-b1)/10)+h3 < h1-b1 )
166 #else
167 	supsh = - VERT(1) - b3;
168 	d2 = VERT(1);
169 	if( VERT(1)+h3 < h1-b1 )
170 #endif
171 		supsh = -(h1-b1) + (h3-b3) - d2;
172 #ifndef NEQN
173 	eht[yyval] = h1 + max(0, h3-VERT( (6*(h1-b1))/10 )) + max(0, h2-b1-d1);
174 #else
175 	eht[yyval] = h1 + max(0, h3-VERT(1)) + max(0, h2-b1-d1);
176 #endif
177 	ebase[yyval] = b1+max(0, h2-b1-d1);
178 #ifndef NEQN
179 	if (rfont[p1] == ITAL && lfont[p2] == ROM)
180 		printf(".ds %d \\|\\*(%d\n", p2, p2);
181 	if (rfont[p2] == ITAL)
182 		printf(".as %d \\|\n", p2);
183 #endif
184 	nrwid(p2, effps, p2);
185 #ifndef NEQN
186 	if (rfont[p1] == ITAL && lfont[p3] == ROM)
187 		printf(".ds %d \\|\\|\\*(%d\n", p3, p3);
188 	else
189 		printf(".ds %d \\|\\*(%d\n", p3, p3);
190 #endif
191 	nrwid(p3, effps, p3);
192 	printf(".nr %d \\n(%d\n", treg, p3);
193 	printf(".if \\n(%d>\\n(%d .nr %d \\n(%d\n", p2, treg, treg, p2);
194 #ifndef NEQN
195 	printf(".as %d \\v'%du'\\s%d\\*(%d\\h'-\\n(%du'\\v'%du'\\\n",
196 		p1, subsh, effps, p2, p2, -subsh+supsh);
197 	printf("\\s%d\\*(%d\\h'-\\n(%du+\\n(%du'\\s%d\\v'%du'\n",
198 		effps, p3, p3, treg, effps2, -supsh);
199 #else
200 	printf(".as %d \\v'%du'\\*(%d\\h'-\\n(%du'\\v'%du'\\\n",
201 		p1, subsh, p2, p2, -subsh+supsh);
202 	printf("\\*(%d\\h'-\\n(%du+\\n(%du'\\v'%du'\n",
203 		p3, p3, treg, -supsh);
204 #endif
205 	ps += deltaps;
206 #ifndef NEQN
207 	if (rfont[p2] == ITAL)
208 		rfont[yyval] = 0;	/* lie */
209 #endif
210 	ofree(p2); ofree(p3); ofree(treg);
211 }
212