1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28/*	  All Rights Reserved  	*/
29
30/*
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
33 * All Rights Reserved
34 *
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
38 */
39
40#pragma ident	"%Z%%M%	%I%	%E% SMI"
41
42#include "tdef.h"
43#include "tw.h"
44#include "ext.h"
45#include <ctype.h>
46
47/*
48 * n6.c -- width functions, sizes and fonts
49*/
50
51int	bdtab[NFONT+1] ={ 0, 0, 0, 3, 3, 0, };
52int	sbold = 0;
53int	fontlab[NFONT+1] = { 0, 'R', 'I', 'B', PAIR('B','I'), 'S', 0 };
54
55extern	int	nchtab;
56
57int
58width(j)
59tchar j;
60{
61	int	i, k;
62
63	if (j & (ZBIT|MOT)) {
64		if (iszbit(j))
65			return(0);
66		if (isvmot(j))
67			return(0);
68		k = absmot(j);
69		if (isnmot(j))
70			k = -k;
71		return(k);
72	}
73	i = cbits(j);
74	if (i < ' ') {
75		if (i == '\b')
76			return(-widthp);
77		if (i == PRESC)
78			i = eschar;
79		else if (iscontrol(i))
80			return(0);
81	}
82	if (i==ohc)
83		return(0);
84#ifdef EUC
85#ifdef NROFF
86	if (multi_locale) {
87		if ((j & MBMASK) || (j & CSMASK)) {
88			switch(j & MBMASK) {
89				case BYTE_CHR:
90				case LASTOFMB:
91					k = t.Char * csi_width[cs(j)];
92					break;
93				default:
94					k = 0;
95					break;
96			}
97			widthp = k;
98			return(k);
99		}
100	}
101	i &= 0x1ff;
102#endif /* NROFF */
103#endif /* EUC */
104	i = trtab[i];
105	if (i < 32)
106		return(0);
107	k = t.width[i] * t.Char;
108	widthp = k;
109	return(k);
110}
111
112
113tchar setch()
114{
115	int	j;
116	char	temp[10];
117	char	*s;
118
119	s = temp;
120	if ((*s++ = getach()) == 0 || (*s++ = getach()) == 0)
121		return(0);
122	*s = '\0';
123	if ((j = findch(temp)) > 0)
124		return j | chbits;
125	else
126		return 0;
127}
128
129tchar setabs()		/* set absolute char from \C'...' */
130{			/* for now, a no-op */
131	int i, n, nf;
132
133	getch();
134	n = 0;
135	n = inumb(&n);
136	getch();
137	if (nonumb)
138		return 0;
139	return n + nchtab + _SPECCHAR_ST;
140}
141
142int
143findft(i)
144int	i;
145{
146	int	k;
147
148	if ((k = i - '0') >= 0 && k <= nfonts && k < smnt)
149		return(k);
150	for (k = 0; fontlab[k] != i; k++)
151		if (k > nfonts)
152			return(-1);
153	return(k);
154}
155
156int
157caseps()
158{
159	return (0);
160}
161
162int
163mchbits()
164{
165	chbits = 0;
166	setfbits(chbits, font);
167	sps = width(' ' | chbits);
168
169	return (0);
170}
171
172
173int
174setps()
175{
176	int	i, j;
177
178	i = cbits(getch());
179	if (ischar(i) && isdigit(i)) {		/* \sd or \sdd */
180		i -= '0';
181		if (i == 0)		/* \s0 */
182			;
183		else if (i <= 3 && ischar(j = cbits(ch = getch())) &&
184		    isdigit(j)) {	/* \sdd */
185			ch = 0;
186		}
187	} else if (i == '(') {		/* \s(dd */
188		getch();
189		getch();
190	} else if (i == '+' || i == '-') {	/* \s+, \s- */
191		j = cbits(getch());
192		if (ischar(j) && isdigit(j)) {		/* \s+d, \s-d */
193			;
194		} else if (j == '(') {		/* \s+(dd, \s-(dd */
195			getch();
196			getch();
197		}
198	}
199
200	return (0);
201}
202
203
204tchar setht()		/* set character height from \H'...' */
205{
206	int	n;
207	tchar c;
208
209	getch();
210	n = inumb(&apts);
211	getch();
212	return(0);
213}
214
215
216tchar setslant()		/* set slant from \S'...' */
217{
218	int	n;
219	tchar c;
220
221	getch();
222	n = 0;
223	n = inumb(&n);
224	getch();
225	return(0);
226}
227
228
229int
230caseft()
231{
232	skip();
233	setfont(1);
234
235	return (0);
236}
237
238
239int
240setfont(a)
241int	a;
242{
243	int	i, j;
244
245	if (a)
246		i = getrq();
247	else
248		i = getsn();
249	if (!i || i == 'P') {
250		j = font1;
251		goto s0;
252	}
253	if (i == 'S' || i == '0')
254		return (0);
255	if ((j = findft(i, fontlab)) == -1)
256		return (0);
257s0:
258	font1 = font;
259	font = j;
260	mchbits();
261
262	return (0);
263}
264
265
266int
267setwd()
268{
269	int	base, wid;
270	tchar i;
271	int	delim, emsz, k;
272	int	savhp, savapts, savapts1, savfont, savfont1, savpts, savpts1;
273
274	base = numtab[ST].val = numtab[ST].val = wid = numtab[CT].val = 0;
275	if (ismot(i = getch()))
276		return (0);
277	delim = cbits(i);
278	savhp = numtab[HP].val;
279	numtab[HP].val = 0;
280	savapts = apts;
281	savapts1 = apts1;
282	savfont = font;
283	savfont1 = font1;
284	savpts = pts;
285	savpts1 = pts1;
286	setwdf++;
287	while (cbits(i = getch()) != delim && !nlflg) {
288		k = width(i);
289		wid += k;
290		numtab[HP].val += k;
291		if (!ismot(i)) {
292			emsz = (INCH * pts + 36) / 72;
293		} else if (isvmot(i)) {
294			k = absmot(i);
295			if (isnmot(i))
296				k = -k;
297			base -= k;
298			emsz = 0;
299		} else
300			continue;
301		if (base < numtab[SB].val)
302			numtab[SB].val = base;
303		if ((k = base + emsz) > numtab[ST].val)
304			numtab[ST].val = k;
305	}
306	setn1(wid, 0, (tchar) 0);
307	numtab[HP].val = savhp;
308	apts = savapts;
309	apts1 = savapts1;
310	font = savfont;
311	font1 = savfont1;
312	pts = savpts;
313	pts1 = savpts1;
314	mchbits();
315	setwdf = 0;
316
317	return (0);
318}
319
320
321tchar vmot()
322{
323	dfact = lss;
324	vflag++;
325	return(mot());
326}
327
328
329tchar hmot()
330{
331	dfact = EM;
332	return(mot());
333}
334
335
336tchar mot()
337{
338	int j, n;
339	tchar i;
340
341	j = HOR;
342	getch(); /*eat delim*/
343	if (n = atoi()) {
344		if (vflag)
345			j = VERT;
346		i = makem(quant(n, j));
347	} else
348		i = 0;
349	getch();
350	vflag = 0;
351	dfact = 1;
352	return(i);
353}
354
355
356tchar sethl(k)
357int	k;
358{
359	int	j;
360	tchar i;
361
362	j = t.Halfline;
363	if (k == 'u')
364		j = -j;
365	else if (k == 'r')
366		j = -2 * j;
367	vflag++;
368	i = makem(j);
369	vflag = 0;
370	return(i);
371}
372
373
374tchar makem(i)
375int	i;
376{
377	tchar j;
378
379	if ((j = i) < 0)
380		j = -j;
381	j |= MOT;
382	if (i < 0)
383		j |= NMOT;
384	if (vflag)
385		j |= VMOT;
386	return(j);
387}
388
389
390tchar getlg(i)
391tchar	i;
392{
393	return(i);
394}
395
396
397int
398caselg()
399{
400	return (0);
401}
402
403
404int
405casefp()
406{
407	int	i, j;
408
409	skip();
410	if ((i = cbits(getch()) - '0') < 0 || i > nfonts)
411		return (0);
412	if (skip() || !(j = getrq()))
413		return (0);
414	fontlab[i] = j;
415
416	return (0);
417}
418
419
420int
421casecs()
422{
423	return (0);
424}
425
426
427int
428casebd()
429{
430	int	i, j, k;
431
432	k = 0;
433bd0:
434	if (skip() || !(i = getrq()) || (j = findft(i)) == -1) {
435		if (k)
436			goto bd1;
437		else
438			return (0);
439	}
440	if (j == smnt) {
441		k = smnt;
442		goto bd0;
443	}
444	if (k) {
445		sbold = j;
446		j = k;
447	}
448bd1:
449	skip();
450	noscale++;
451	bdtab[j] = atoi();
452	noscale = 0;
453
454	return (0);
455}
456
457
458int
459casevs()
460{
461	int	i;
462
463	skip();
464	vflag++;
465	dfact = INCH; /*default scaling is points!*/
466	dfactd = 72;
467	res = VERT;
468	i = inumb(&lss);
469	if (nonumb)
470		i = lss1;
471	if (i < VERT)
472		i = VERT;	/* was VERT */
473	lss1 = lss;
474	lss = i;
475
476	return (0);
477}
478
479
480
481int
482casess()
483{
484	return (0);
485}
486
487
488tchar xlss()
489{
490	/* stores \x'...' into
491	 * two successive tchars.
492	 * the first contains HX, the second the value,
493	 * encoded as a vertical motion.
494	 * decoding is done in n2.c by pchar().
495	 */
496	int	i;
497
498	getch();
499	dfact = lss;
500	i = quant(atoi(), VERT);
501	dfact = 1;
502	getch();
503	if (i >= 0)
504		*pbp++ = MOT | VMOT | i;
505	else
506		*pbp++ = MOT | VMOT | NMOT | -i;
507	return(HX);
508}
509