|
|
1.1 ! root 1: #include "frame.h" ! 2: ! 3: #define SLOP 25 ! 4: ! 5: addbox(f, bn, n) /* add n boxes after bn, shift the rest up, ! 6: * box[bn+n]==box[bn] */ ! 7: register Frame *f; ! 8: { ! 9: register i; ! 10: if(bn>f->nbox) ! 11: panic("addbox"); ! 12: if(f->nbox+n>f->nalloc) ! 13: growbox(f, n+SLOP); ! 14: for(i=f->nbox; --i>=bn; ) ! 15: f->box[i+n]=f->box[i]; ! 16: f->nbox+=n; ! 17: } ! 18: closebox(f, n0, n1) /* inclusive */ ! 19: register Frame *f; ! 20: { ! 21: register i; ! 22: if(n0>=f->nbox || n1>=f->nbox || n1<n0) ! 23: panic("closebox"); ! 24: n1++; ! 25: for(i=n1; i<f->nbox; i++) ! 26: f->box[i-(n1-n0)]=f->box[i]; ! 27: f->nbox-=n1-n0; ! 28: } ! 29: delbox(f, n0, n1) /* inclusive */ ! 30: register Frame *f; ! 31: { ! 32: if(n0>=f->nbox || n1>=f->nbox || n1<n0) ! 33: panic("delbox"); ! 34: freebox(f, n0, n1); ! 35: closebox(f, n0, n1); ! 36: } ! 37: freebox(f, n0, n1) /* inclusive */ ! 38: register Frame *f; ! 39: { ! 40: register i; ! 41: if(n1<n0) ! 42: return; ! 43: if(n0>=f->nbox || n1>=f->nbox) ! 44: panic("freebox"); ! 45: n1++; ! 46: for(i=n0; i<n1; i++) ! 47: if(f->box[i].len>=0) ! 48: freestr(f->box[i].ptr); ! 49: } ! 50: growbox(f, delta) ! 51: register Frame *f; ! 52: register delta; ! 53: { ! 54: gcrealloc((char **)&f->box, ! 55: f->nbox*sizeof(Box), (f->nalloc+delta)*sizeof(Box)); ! 56: f->nalloc+=delta; ! 57: } ! 58: gcrealloc(sp, old, new) ! 59: register char **sp; ! 60: { ! 61: register i; ! 62: register long *p, *q; ! 63: long *hold; ! 64: if(*sp==0){ ! 65: if(gcalloc(new, sp)==0) ! 66: goto Burma; ! 67: return; ! 68: } ! 69: if(new<=old) ! 70: return; ! 71: if(*sp==0 || gcalloc(old, &hold)==0) ! 72: Burma: ! 73: panic("gcrealloc"); ! 74: old=(old+sizeof(long)-1)/sizeof(long); ! 75: new=(new+sizeof(long)-1)/sizeof(long); ! 76: for(p=(long *)*sp,q=hold,i=0; i<old; i++) ! 77: *q++= *p++; ! 78: gcfree(*sp); ! 79: if(gcalloc(new*sizeof(long), sp)==0) ! 80: goto Burma; ! 81: for(p=(long *)*sp,q=hold,i=0; i<new; i++) ! 82: *p++= *q++; ! 83: gcfree(hold); ! 84: } ! 85: dupbox(f, bn) ! 86: register Frame *f; ! 87: { ! 88: if(f->box[bn].len<0) ! 89: panic("dupbox"); ! 90: addbox(f, bn, 1); ! 91: if(f->box[bn].len>=0){ ! 92: f->box[bn+1].ptr=allocstr(f->box[bn].len+1); ! 93: copystr(f->box[bn].ptr, f->box[bn+1].ptr); ! 94: } ! 95: } ! 96: truncatebox(f, b, n) /* drop last n chars; no allocation done */ ! 97: register Frame *f; ! 98: register Box *b; ! 99: { ! 100: if(b->len<0 || b->len<n) ! 101: panic("truncatebox"); ! 102: b->ptr[b->len-=n]=0; ! 103: b->wid=strwidth(f->font, b->ptr); ! 104: } ! 105: chopbox(f, b, n) /* drop first n chars; no allocation done */ ! 106: register Frame *f; ! 107: register Box *b; ! 108: { ! 109: if(b->len<0 || b->len<n) ! 110: panic("chopbox"); ! 111: copystr(b->ptr+n, b->ptr); ! 112: b->len-=n; ! 113: b->wid=strwidth(f->font, b->ptr); ! 114: } ! 115: splitbox(f, bn, n) ! 116: register Frame *f; ! 117: register bn, n; ! 118: { ! 119: dupbox(f, bn); ! 120: truncatebox(f, &f->box[bn], f->box[bn].len-n); ! 121: chopbox(f, &f->box[bn+1], n); ! 122: } ! 123: mergebox(f, bn) /* merge bn and bn+1 */ ! 124: register Frame *f; ! 125: { ! 126: register Box *b= &f->box[bn]; ! 127: insure(b, b[0].len+b[1].len+1); ! 128: copystr(b[1].ptr, b[0].ptr+b[0].len); ! 129: b[0].wid+=b[1].wid; ! 130: b[0].len+=b[1].len; ! 131: delbox(f, bn+1, bn+1); ! 132: } ! 133: findbox(f, bn, p, q) /* find box containing q and put q on a box boundary */ ! 134: register bn; ! 135: register Frame *f; ! 136: register Posn p, q; ! 137: { ! 138: register Box *b; ! 139: for(b= &f->box[bn]; bn<f->nbox && p+LEN(b)<=q; bn++, b++) ! 140: p+=LEN(b); ! 141: if(p!=q) ! 142: splitbox(f, bn++, q-p); ! 143: return bn; ! 144: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.