9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

n6.c (4870B)


      1 #include "tdef.h"
      2 #include "ext.h"
      3 #include "fns.h"
      4 #include <ctype.h>
      5 
      6 /*
      7  * n6.c -- width functions, sizes and fonts
      8 */
      9 
     10 int
     11 n_width(Tchar j)
     12 {
     13 	int i, k;
     14 
     15 	if (iszbit(j))
     16 		return 0;
     17 	if (ismot(j)) {
     18 		if (isvmot(j))
     19 			return(0);
     20 		k = absmot(j);
     21 		if (isnmot(j))
     22 			k = -k;
     23 		return(k);
     24 	}
     25 	i = cbits(j);
     26 	if (i < ' ') {
     27 		if (i == '\b')
     28 			return(-widthp);
     29 		if (i == PRESC)
     30 			i = eschar;
     31 		else if (i == HX)
     32 			return(0);
     33 	}
     34 	if (i == ohc)
     35 		return(0);
     36 	i = trtab[i];
     37 	if (i < ' ')
     38 		return(0);
     39 	if (i >= t.tfont.nchars)	/* not on the font */
     40 		k = t.Char;		/* really ought to check properly */
     41 	else
     42 		k = t.tfont.wp[i].wid * t.Char;
     43 	widthp = k;
     44 	return(k);
     45 }
     46 
     47 
     48 Tchar n_setch(int c)
     49 {
     50 	return t_setch(c);
     51 }
     52 
     53 Tchar n_setabs(void)	/* set absolute char from \N'...' */
     54 {			/* for now, a no-op */
     55 	return t_setabs();
     56 }
     57 
     58 int n_findft(int i)
     59 {
     60 	int k;
     61 
     62 	if ((k = i - '0') >= 0 && k <= nfonts && k < smnt)
     63 		return(k);
     64 	for (k = 0; fontlab[k] != i; k++)
     65 		if (k > nfonts)
     66 			return(-1);
     67 	return(k);
     68 }
     69 
     70 
     71 
     72 void n_mchbits(void)
     73 {
     74 	chbits = 0;
     75 	setfbits(chbits, font);
     76 	sps = width(' ' | chbits);
     77 }
     78 
     79 
     80 void n_setps(void )
     81 {
     82 	int i, j;
     83 
     84 	i = cbits(getch());
     85 	if (isdigit(i)) {		/* \sd or \sdd */
     86 		i -= '0';
     87 		if (i == 0)		/* \s0 */
     88 			;
     89 		else if (i <= 3 && (ch=getch()) && isdigit(cbits(ch))) {	/* \sdd */
     90 			ch = 0;
     91 		}
     92 	} else if (i == '(') {		/* \s(dd */
     93 		getch();
     94 		getch();
     95 	} else if (i == '+' || i == '-') {	/* \s+, \s- */
     96 		j = cbits(getch());
     97 		if (isdigit(j)) {		/* \s+d, \s-d */
     98 			;
     99 		} else if (j == '(') {		/* \s+(dd, \s-(dd */
    100 			getch();
    101 			getch();
    102 		}
    103 	}
    104 }
    105 
    106 
    107 Tchar n_setht(void)		/* set character height from \H'...' */
    108 {
    109 
    110 	getch();
    111 	inumb(&apts);
    112 	getch();
    113 	return(0);
    114 }
    115 
    116 
    117 Tchar n_setslant(void)		/* set slant from \S'...' */
    118 {
    119 	int n;
    120 
    121 	getch();
    122 	n = 0;
    123 	n = inumb(&n);
    124 	getch();
    125 	return(0);
    126 }
    127 
    128 
    129 void n_caseft(void)
    130 {
    131 	skip();
    132 	setfont(1);
    133 }
    134 
    135 
    136 void n_setfont(int a)
    137 {
    138 	int i, j;
    139 
    140 	if (a)
    141 		i = getrq();
    142 	else 
    143 		i = getsn();
    144 	if (!i || i == 'P') {
    145 		j = font1;
    146 		goto s0;
    147 	}
    148 	if (i == 'S' || i == '0')
    149 		return;
    150 	if ((j = findft(i)) == -1)
    151 		return;
    152 s0:
    153 	font1 = font;
    154 	font = j;
    155 	mchbits();
    156 }
    157 
    158 
    159 void n_setwd(void)
    160 {
    161 	int base, wid;
    162 	Tchar i;
    163 	int	delim, emsz, k;
    164 	int	savhp, savapts, savapts1, savfont, savfont1, savpts, savpts1;
    165 
    166 	base = numtabp[ST].val = numtabp[ST].val = wid = numtabp[CT].val = 0;
    167 	if (ismot(i = getch()))
    168 		return;
    169 	delim = cbits(i);
    170 	savhp = numtabp[HP].val;
    171 	numtabp[HP].val = 0;
    172 	savapts = apts;
    173 	savapts1 = apts1;
    174 	savfont = font;
    175 	savfont1 = font1;
    176 	savpts = pts;
    177 	savpts1 = pts1;
    178 	setwdf++;
    179 	while (cbits(i = getch()) != delim && !nlflg) {
    180 		k = width(i);
    181 		wid += k;
    182 		numtabp[HP].val += k;
    183 		if (!ismot(i)) {
    184 			emsz = (INCH * pts + 36) / 72;
    185 		} else if (isvmot(i)) {
    186 			k = absmot(i);
    187 			if (isnmot(i))
    188 				k = -k;
    189 			base -= k;
    190 			emsz = 0;
    191 		} else 
    192 			continue;
    193 		if (base < numtabp[SB].val)
    194 			numtabp[SB].val = base;
    195 		if ((k = base + emsz) > numtabp[ST].val)
    196 			numtabp[ST].val = k;
    197 	}
    198 	setn1(wid, 0, (Tchar) 0);
    199 	numtabp[HP].val = savhp;
    200 	apts = savapts;
    201 	apts1 = savapts1;
    202 	font = savfont;
    203 	font1 = savfont1;
    204 	pts = savpts;
    205 	pts1 = savpts1;
    206 	mchbits();
    207 	setwdf = 0;
    208 }
    209 
    210 
    211 Tchar n_vmot(void)
    212 {
    213 	dfact = lss;
    214 	vflag++;
    215 	return n_mot();
    216 }
    217 
    218 
    219 Tchar n_hmot(void)
    220 {
    221 	dfact = EM;
    222 	return n_mot();
    223 }
    224 
    225 
    226 Tchar n_mot(void)
    227 {
    228 	int j, n;
    229 	Tchar i;
    230 
    231 	j = HOR;
    232 	getch(); /*eat delim*/
    233 	if (n = atoi0()) {
    234 		if (vflag)
    235 			j = VERT;
    236 		i = makem(quant(n, j));
    237 	} else
    238 		i = 0;
    239 	getch();
    240 	vflag = 0;
    241 	dfact = 1;
    242 	return(i);
    243 }
    244 
    245 
    246 Tchar n_sethl(int k)
    247 {
    248 	int j;
    249 	Tchar i;
    250 
    251 	j = t.Halfline;
    252 	if (k == 'u')
    253 		j = -j;
    254 	else if (k == 'r')
    255 		j = -2 * j;
    256 	vflag++;
    257 	i = makem(j);
    258 	vflag = 0;
    259 	return(i);
    260 }
    261 
    262 
    263 Tchar n_makem(int i)
    264 {
    265 	Tchar j;
    266 
    267 	if (i >= 0)
    268 		j = i;
    269 	else
    270 		j = -i;
    271 	j |= MOT;
    272 	if (i < 0)
    273 		j |= NMOT;
    274 	if (vflag)
    275 		j |= VMOT;
    276 	return(j);
    277 }
    278 
    279 
    280 void n_casefp(void)
    281 {
    282 	int i, j;
    283 
    284 	skip();
    285 	if ((i = cbits(getch()) - '0') < 0 || i > nfonts)
    286 		return;
    287 	if (skip() || !(j = getrq()))
    288 		return;
    289 	fontlab[i] = j;
    290 }
    291 
    292 
    293 
    294 void n_casebd(void)
    295 {
    296 	int i, j, k;
    297 
    298 	j = k = 0;
    299 bd0:
    300 	if (skip() || !(i = getrq()) || (j = findft(i)) == -1) {
    301 		if (k)
    302 			goto bd1;
    303 		else 
    304 			return;
    305 	}
    306 	if (j == smnt) {
    307 		k = smnt;
    308 		goto bd0;
    309 	}
    310 	if (k) {
    311 		sbold = j;
    312 		j = k;
    313 	}
    314 bd1:
    315 	skip();
    316 	noscale++;
    317 	bdtab[j] = atoi0();
    318 	noscale = 0;
    319 }
    320 
    321 
    322 void n_casevs(void)
    323 {
    324 	int i;
    325 
    326 	skip();
    327 	vflag++;
    328 	dfact = INCH; /*default scaling is points!*/
    329 	dfactd = 72;
    330 	res = VERT;
    331 	i = inumb(&lss);
    332 	if (nonumb)
    333 		i = lss1;
    334 	if (i < VERT)
    335 		i = VERT;	/* was VERT */
    336 	lss1 = lss;
    337 	lss = i;
    338 }
    339 
    340 
    341 
    342 
    343 Tchar n_xlss(void)
    344 {
    345 	/* stores \x'...' into
    346 	/* two successive Tchars.
    347 	/* the first contains HX, the second the value,
    348 	/* encoded as a vertical motion.
    349 	/* decoding is done in n2.c by pchar().
    350 	*/
    351 	int	i;
    352 
    353 	getch();
    354 	dfact = lss;
    355 	i = quant(atoi0(), VERT);
    356 	dfact = 1;
    357 	getch();
    358 	if (i >= 0)
    359 		*pbp++ = MOT | VMOT | i;
    360 	else
    361 		*pbp++ = MOT | VMOT | NMOT | -i;
    362 	return(HX);
    363 }