1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
/* Copyright (c) 1992 AT&T - All rights reserved. */
#include <u.h>
#include <libc.h>
#include <libg.h>
#include <frame.h>
#define SLOP 25
void
_fraddbox(Frame *f, int bn, int n) /* add n boxes after bn, shift the rest up,
* box[bn+n]==box[bn] */
{
int i;
if(bn > f->nbox)
berror("_fraddbox");
if(f->nbox+n > f->nalloc)
_frgrowbox(f, n+SLOP);
for(i=f->nbox; --i>=bn; )
f->box[i+n] = f->box[i];
f->nbox+=n;
}
void
_frclosebox(Frame *f, int n0, int n1) /* inclusive */
{
int i;
if(n0>=f->nbox || n1>=f->nbox || n1<n0)
berror("_frclosebox");
n1++;
for(i=n1; i<f->nbox; i++)
f->box[i-(n1-n0)] = f->box[i];
f->nbox -= n1-n0;
}
void
_frdelbox(Frame *f, int n0, int n1) /* inclusive */
{
if(n0>=f->nbox || n1>=f->nbox || n1<n0)
berror("_frdelbox");
_frfreebox(f, n0, n1);
_frclosebox(f, n0, n1);
}
void
_frfreebox(Frame *f, int n0, int n1) /* inclusive */
{
int i;
if(n1<n0)
return;
if(n0>=f->nbox || n1>=f->nbox)
berror("_frfreebox");
n1++;
for(i=n0; i<n1; i++)
if(f->box[i].nrune >= 0)
free(f->box[i].a.ptr);
}
void
_frgrowbox(Frame *f, int delta)
{
f->nalloc += delta;
f->box = realloc(f->box, f->nalloc*sizeof(Frbox));
if(f->box == 0)
berror("_frgrowbox");
}
static
void
dupbox(Frame *f, int bn)
{
uchar *p;
if(f->box[bn].nrune < 0)
berror("dupbox");
_fraddbox(f, bn, 1);
if(f->box[bn].nrune >= 0){
p = _frallocstr(NBYTE(&f->box[bn])+1);
strcpy((char*)p, (char*)f->box[bn].a.ptr);
f->box[bn+1].a.ptr = p;
}
}
static
uchar*
runeindex(uchar *p, int n)
{
int i, w;
Rune rune;
for(i=0; i<n; i++,p+=w)
if(*p < Runeself)
w = 1;
else{
w = chartorune(&rune, (char*)p);
USED(rune);
}
return p;
}
static
void
truncatebox(Frame *f, Frbox *b, int n) /* drop last n chars; no allocation done */
{
if(b->nrune<0 || b->nrune<n)
berror("truncatebox");
b->nrune -= n;
runeindex(b->a.ptr, b->nrune)[0] = 0;
b->wid = strwidth(f->font, (char *)b->a.ptr);
}
static
void
chopbox(Frame *f, Frbox *b, int n) /* drop first n chars; no allocation done */
{
if(b->nrune<0 || b->nrune<n)
berror("chopbox");
strcpy((char*)b->a.ptr, (char*)runeindex(b->a.ptr, n));
b->nrune -= n;
b->wid = strwidth(f->font, (char *)b->a.ptr);
}
void
_frsplitbox(Frame *f, int bn, int n)
{
dupbox(f, bn);
truncatebox(f, &f->box[bn], f->box[bn].nrune-n);
chopbox(f, &f->box[bn+1], n);
}
void
_frmergebox(Frame *f, int bn) /* merge bn and bn+1 */
{
Frbox *b;
b = &f->box[bn];
_frinsure(f, bn, NBYTE(&b[0])+NBYTE(&b[1])+1);
strcpy((char*)runeindex(b[0].a.ptr, b[0].nrune), (char*)b[1].a.ptr);
b[0].wid += b[1].wid;
b[0].nrune += b[1].nrune;
_frdelbox(f, bn+1, bn+1);
}
int
_frfindbox(Frame *f, int bn, ulong p, ulong q) /* find box containing q and put q on a box boundary */
{
Frbox *b;
for(b = &f->box[bn]; bn<f->nbox && p+NRUNE(b)<=q; bn++, b++)
p += NRUNE(b);
if(p != q)
_frsplitbox(f, bn++, (int)(q-p));
return bn;
}
|