xref: /titanic_52/usr/src/cmd/eqn/paren.c (revision 779fc935796a940997d18b31d64a9fec9c6b40f6)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
16 
17 #include "e.h"
18 
19 extern int max();
20 
21 void brack(int, char *, char *, char *);
22 
23 void
24 paren(int leftc, int p1, int rightc)
25 {
26 	int n, m, h1, j, b1, v;
27 	h1 = eht[p1]; b1 = ebase[p1];
28 	yyval = p1;
29 #ifndef NEQN
30 	lfont[yyval] = rfont[yyval] = 0;
31 	n = (h1 + EM(1.0, EFFPS(ps)) - 1) / EM(1.0, EFFPS(ps));
32 #else	/* NEQN */
33 	n = max(b1+VERT(1), h1-b1-VERT(1)) / VERT(1);
34 #endif	/* NEQN */
35 	if (n < 2) n = 1;
36 	m = n-2;
37 	if (leftc == '{' || rightc == '}') {
38 		if ((n % 2) == 0) {
39 			n++;
40 		}
41 		if (n < 3) n = 3;
42 		m = n-3;
43 	}
44 #ifndef NEQN
45 	eht[yyval] = VERT(EM(n, ps));
46 	ebase[yyval] = b1 + (eht[yyval]-h1)/2;
47 	v = b1 - h1/2 + VERT(EM(0.4, ps));
48 #else	/* NEQN */
49 	eht[yyval] = VERT(2 * n);
50 	ebase[yyval] = (n)/2 * VERT(2);
51 	if (n%2 == 0)
52 		ebase[yyval] -= VERT(1);
53 	v = b1 - h1/2 + VERT(1);
54 #endif	/* NEQN */
55 	printf(".ds %d \\|\\v'%du'", yyval, v);
56 	switch (leftc) {
57 		case 'n':	/* nothing */
58 		case '\0':
59 			break;
60 		case 'f':	/* floor */
61 			if (n <= 1)
62 				printf("\\(lf");
63 			else
64 				brack(m, "\\(bv", "\\(bv", "\\(lf");
65 			break;
66 		case 'c':	/* ceiling */
67 			if (n <= 1)
68 				printf("\\(lc");
69 			else
70 				brack(m, "\\(lc", "\\(bv", "\\(bv");
71 			break;
72 		case '{':
73 			printf("\\b'\\(lt");
74 			for (j = 0; j < m; j += 2) printf("\\(bv");
75 			printf("\\(lk");
76 			for (j = 0; j < m; j += 2) printf("\\(bv");
77 			printf("\\(lb'");
78 			break;
79 		case '(':
80 			brack(m, "\\(lt", "\\(bv", "\\(lb");
81 			break;
82 		case '[':
83 			brack(m, "\\(lc", "\\(bv", "\\(lf");
84 			break;
85 		case '|':
86 			brack(m, "\\(bv", "\\(bv", "\\(bv");
87 			break;
88 		default:
89 			brack(m, (char *)&leftc, (char *)&leftc,
90 			    (char *)&leftc);
91 			break;
92 		}
93 	printf("\\v'%du'\\*(%d", -v, p1);
94 	if (rightc) {
95 		printf("\\|\\v'%du'", v);
96 		switch (rightc) {
97 			case 'f':	/* floor */
98 				if (n <= 1)
99 					printf("\\(rf");
100 				else
101 					brack(m, "\\(bv", "\\(bv", "\\(rf");
102 				break;
103 			case 'c':	/* ceiling */
104 				if (n <= 1)
105 					printf("\\(rc");
106 				else
107 					brack(m, "\\(rc", "\\(bv", "\\(bv");
108 				break;
109 			case '}':
110 				printf("\\b'\\(rt");
111 				for (j = 0; j < m; j += 2) printf("\\(bv");
112 				printf("\\(rk");
113 				for (j = 0; j < m; j += 2) printf("\\(bv");
114 				printf("\\(rb'");
115 				break;
116 			case ']':
117 				brack(m, "\\(rc", "\\(bv", "\\(rf");
118 				break;
119 			case ')':
120 				brack(m, "\\(rt", "\\(bv", "\\(rb");
121 				break;
122 			case '|':
123 				brack(m, "\\(bv", "\\(bv", "\\(bv");
124 				break;
125 			default:
126 				brack(m, (char *)&rightc, (char *)&rightc,
127 				    (char *)&rightc);
128 				break;
129 		}
130 		printf("\\v'%du'", -v);
131 	}
132 	printf("\n");
133 	if (dbg)
134 		printf(".\tcurly: h=%d b=%d n=%d v=%d l=%c, r=%c\n",
135 		    eht[yyval], ebase[yyval], n, v, leftc, rightc);
136 }
137 
138 void
139 brack(int m, char *t, char *c, char *b)
140 {
141 	int j;
142 	printf("\\b'%s", t);
143 	for (j = 0; j < m; j++)
144 		printf("%s", c);
145 	printf("%s'", b);
146 }
147