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