| 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 |
package text |
| 38 |
|
| 39 |
import ( |
| 40 |
"io" |
| 41 |
"unicode/utf8" |
| 42 |
|
| 43 |
"golang.org/x/image/font" |
| 44 |
"golang.org/x/image/math/fixed" |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
type Direction bool |
| 49 |
|
| 50 |
const ( |
| 51 |
Forwards Direction = false |
| 52 |
Backwards Direction = true |
| 53 |
) |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
const ( |
| 59 |
SeekSet int = 0 |
| 60 |
SeekCur int = 1 |
| 61 |
SeekEnd int = 2 |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
const maxLen = 0x7fffffff |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
type Frame struct { |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
paragraphs []Paragraph |
| 96 |
lines []Line |
| 97 |
boxes []Box |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
cachedHeightPlus1 int32 |
| 103 |
cachedLineCountPlus1 int32 |
| 104 |
cachedParagraphCountPlus1 int32 |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
freeP, freeL, freeB int32 |
| 109 |
|
| 110 |
firstP int32 |
| 111 |
|
| 112 |
maxWidth fixed.Int26_6 |
| 113 |
|
| 114 |
faceHeight int32 |
| 115 |
face font.Face |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
len int |
| 126 |
text []byte |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
seqNum uint64 |
| 132 |
|
| 133 |
carets []*Caret |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
lineReaderData bAndK |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
func (f *Frame) SetFace(face font.Face) { |
| 144 |
if !f.initialized() { |
| 145 |
f.initialize() |
| 146 |
} |
| 147 |
f.face = face |
| 148 |
if face == nil { |
| 149 |
f.faceHeight = 0 |
| 150 |
} else { |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
m := face.Metrics() |
| 164 |
f.faceHeight = int32(m.Ascent.Ceil() + m.Descent.Ceil()) |
| 165 |
} |
| 166 |
if f.len != 0 { |
| 167 |
f.relayout() |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
func (f *Frame) SetMaxWidth(m fixed.Int26_6) { |
| 184 |
if !f.initialized() { |
| 185 |
f.initialize() |
| 186 |
} |
| 187 |
if f.maxWidth == m { |
| 188 |
return |
| 189 |
} |
| 190 |
f.maxWidth = m |
| 191 |
if f.len != 0 { |
| 192 |
f.relayout() |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
func (f *Frame) relayout() { |
| 197 |
for p := f.firstP; p != 0; p = f.paragraphs[p].next { |
| 198 |
l := f.mergeIntoOneLine(p) |
| 199 |
layout(f, l) |
| 200 |
} |
| 201 |
f.invalidateCaches() |
| 202 |
f.seqNum++ |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
func (f *Frame) mergeIntoOneLine(p int32) (l int32) { |
| 209 |
firstL := f.paragraphs[p].firstL |
| 210 |
ll := &f.lines[firstL] |
| 211 |
b0 := ll.firstB |
| 212 |
bb0 := &f.boxes[b0] |
| 213 |
for { |
| 214 |
if b1 := bb0.next; b1 != 0 { |
| 215 |
bb1 := &f.boxes[b1] |
| 216 |
if !f.joinBoxes(b0, b1, bb0, bb1) { |
| 217 |
b0, bb0 = b1, bb1 |
| 218 |
} |
| 219 |
continue |
| 220 |
} |
| 221 |
|
| 222 |
if ll.next == 0 { |
| 223 |
f.paragraphs[p].invalidateCaches() |
| 224 |
f.lines[firstL].invalidateCaches() |
| 225 |
return firstL |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
nextLL := &f.lines[ll.next] |
| 230 |
b1 := nextLL.firstB |
| 231 |
bb1 := &f.boxes[b1] |
| 232 |
bb0.next = b1 |
| 233 |
bb1.prev = b0 |
| 234 |
|
| 235 |
toFree := ll.next |
| 236 |
ll.next = nextLL.next |
| 237 |
|
| 238 |
|
| 239 |
f.freeLine(toFree) |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
func (f *Frame) joinBoxes(b0, b1 int32, bb0, bb1 *Box) bool { |
| 248 |
switch { |
| 249 |
case bb0.i == bb0.j: |
| 250 |
|
| 251 |
bb0.i, bb0.j = bb1.i, bb1.j |
| 252 |
case bb1.i == bb1.j: |
| 253 |
|
| 254 |
case bb0.j == bb1.i: |
| 255 |
|
| 256 |
bb0.j = bb1.j |
| 257 |
default: |
| 258 |
return false |
| 259 |
} |
| 260 |
bb0.next = bb1.next |
| 261 |
if bb0.next != 0 { |
| 262 |
f.boxes[bb0.next].prev = b0 |
| 263 |
} |
| 264 |
f.freeBox(b1) |
| 265 |
return true |
| 266 |
} |
| 267 |
|
| 268 |
func (f *Frame) initialized() bool { |
| 269 |
return len(f.paragraphs) > 0 |
| 270 |
} |
| 271 |
|
| 272 |
func (f *Frame) initialize() { |
| 273 |
|
| 274 |
|
| 275 |
f.paragraphs = make([]Paragraph, 2, 16) |
| 276 |
f.lines = make([]Line, 2, 16) |
| 277 |
f.boxes = make([]Box, 2, 16) |
| 278 |
|
| 279 |
f.firstP = 1 |
| 280 |
f.paragraphs[1].firstL = 1 |
| 281 |
f.lines[1].firstB = 1 |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
func (f *Frame) newParagraph() (p int32, realloc bool) { |
| 288 |
if f.freeP != 0 { |
| 289 |
p := f.freeP |
| 290 |
pp := &f.paragraphs[p] |
| 291 |
f.freeP = pp.next |
| 292 |
*pp = Paragraph{} |
| 293 |
return p, false |
| 294 |
} |
| 295 |
realloc = len(f.paragraphs) == cap(f.paragraphs) |
| 296 |
f.paragraphs = append(f.paragraphs, Paragraph{}) |
| 297 |
return int32(len(f.paragraphs) - 1), realloc |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
func (f *Frame) newLine() (l int32, realloc bool) { |
| 304 |
if f.freeL != 0 { |
| 305 |
l := f.freeL |
| 306 |
ll := &f.lines[l] |
| 307 |
f.freeL = ll.next |
| 308 |
*ll = Line{} |
| 309 |
return l, false |
| 310 |
} |
| 311 |
realloc = len(f.lines) == cap(f.lines) |
| 312 |
f.lines = append(f.lines, Line{}) |
| 313 |
return int32(len(f.lines) - 1), realloc |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
func (f *Frame) newBox() (b int32, realloc bool) { |
| 320 |
if f.freeB != 0 { |
| 321 |
b := f.freeB |
| 322 |
bb := &f.boxes[b] |
| 323 |
f.freeB = bb.next |
| 324 |
*bb = Box{} |
| 325 |
return b, false |
| 326 |
} |
| 327 |
realloc = len(f.boxes) == cap(f.boxes) |
| 328 |
f.boxes = append(f.boxes, Box{}) |
| 329 |
return int32(len(f.boxes) - 1), realloc |
| 330 |
} |
| 331 |
|
| 332 |
func (f *Frame) freeParagraph(p int32) { |
| 333 |
f.paragraphs[p] = Paragraph{next: f.freeP, prev: -1} |
| 334 |
f.freeP = p |
| 335 |
|
| 336 |
} |
| 337 |
|
| 338 |
func (f *Frame) freeLine(l int32) { |
| 339 |
f.lines[l] = Line{next: f.freeL, prev: -1} |
| 340 |
f.freeL = l |
| 341 |
|
| 342 |
} |
| 343 |
|
| 344 |
func (f *Frame) freeBox(b int32) { |
| 345 |
f.boxes[b] = Box{next: f.freeB, prev: -1} |
| 346 |
f.freeB = b |
| 347 |
|
| 348 |
} |
| 349 |
|
| 350 |
func (f *Frame) lastParagraph() int32 { |
| 351 |
for p := f.firstP; ; { |
| 352 |
if next := f.paragraphs[p].next; next != 0 { |
| 353 |
p = next |
| 354 |
continue |
| 355 |
} |
| 356 |
return p |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
func (f *Frame) FirstParagraph() *Paragraph { |
| 362 |
if !f.initialized() { |
| 363 |
f.initialize() |
| 364 |
} |
| 365 |
return &f.paragraphs[f.firstP] |
| 366 |
} |
| 367 |
|
| 368 |
func (f *Frame) invalidateCaches() { |
| 369 |
f.cachedHeightPlus1 = 0 |
| 370 |
f.cachedLineCountPlus1 = 0 |
| 371 |
f.cachedParagraphCountPlus1 = 0 |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
func (f *Frame) Height() int { |
| 376 |
if !f.initialized() { |
| 377 |
f.initialize() |
| 378 |
} |
| 379 |
if f.cachedHeightPlus1 <= 0 { |
| 380 |
h := 1 |
| 381 |
for p := f.firstP; p != 0; p = f.paragraphs[p].next { |
| 382 |
h += f.paragraphs[p].Height(f) |
| 383 |
} |
| 384 |
f.cachedHeightPlus1 = int32(h) |
| 385 |
} |
| 386 |
return int(f.cachedHeightPlus1 - 1) |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
func (f *Frame) LineCount() int { |
| 393 |
if !f.initialized() { |
| 394 |
f.initialize() |
| 395 |
} |
| 396 |
if f.cachedLineCountPlus1 <= 0 { |
| 397 |
n := 1 |
| 398 |
for p := f.firstP; p != 0; p = f.paragraphs[p].next { |
| 399 |
n += f.paragraphs[p].LineCount(f) |
| 400 |
} |
| 401 |
f.cachedLineCountPlus1 = int32(n) |
| 402 |
} |
| 403 |
return int(f.cachedLineCountPlus1 - 1) |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
func (f *Frame) ParagraphCount() int { |
| 410 |
if !f.initialized() { |
| 411 |
f.initialize() |
| 412 |
} |
| 413 |
if f.cachedParagraphCountPlus1 <= 0 { |
| 414 |
n := 1 |
| 415 |
for p := f.firstP; p != 0; p = f.paragraphs[p].next { |
| 416 |
n++ |
| 417 |
} |
| 418 |
f.cachedParagraphCountPlus1 = int32(n) |
| 419 |
} |
| 420 |
return int(f.cachedParagraphCountPlus1 - 1) |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
func (f *Frame) Len() int { |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
return f.len |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
func (f *Frame) deletedLen() int { |
| 433 |
return len(f.text) - f.len |
| 434 |
} |
| 435 |
|
| 436 |
func (f *Frame) compactText() { |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
newText := make([]byte, 0, f.len+f.deletedLen()/2) |
| 446 |
for p := f.firstP; p != 0; { |
| 447 |
pp := &f.paragraphs[p] |
| 448 |
for l := pp.firstL; l != 0; { |
| 449 |
ll := &f.lines[l] |
| 450 |
|
| 451 |
i := int32(len(newText)) |
| 452 |
for b := ll.firstB; b != 0; { |
| 453 |
bb := &f.boxes[b] |
| 454 |
newText = append(newText, f.text[bb.i:bb.j]...) |
| 455 |
nextB := bb.next |
| 456 |
f.freeBox(b) |
| 457 |
b = nextB |
| 458 |
} |
| 459 |
j := int32(len(newText)) |
| 460 |
ll.firstB, _ = f.newBox() |
| 461 |
bb := &f.boxes[ll.firstB] |
| 462 |
bb.i, bb.j = i, j |
| 463 |
|
| 464 |
l = ll.next |
| 465 |
} |
| 466 |
p = pp.next |
| 467 |
} |
| 468 |
f.text = newText |
| 469 |
if len(newText) != f.len { |
| 470 |
panic("text: invalid state") |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
func (f *Frame) NewCaret() *Caret { |
| 476 |
if !f.initialized() { |
| 477 |
f.initialize() |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
p := f.FirstParagraph() |
| 482 |
l := p.FirstLine(f) |
| 483 |
b := l.FirstBox(f) |
| 484 |
c := &Caret{ |
| 485 |
f: f, |
| 486 |
p: f.firstP, |
| 487 |
l: p.firstL, |
| 488 |
b: l.firstB, |
| 489 |
k: b.i, |
| 490 |
caretsIndex: len(f.carets), |
| 491 |
} |
| 492 |
f.carets = append(f.carets, c) |
| 493 |
return c |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
func (f *Frame) readRune(b, k int32) (r rune, size int, newB, newK int32) { |
| 504 |
bb := &f.boxes[b] |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
r, size = utf8.DecodeRune(f.text[k:bb.j]) |
| 509 |
if r < utf8.RuneSelf || size > 1 { |
| 510 |
return r, size, b, k + int32(size) |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
buf := [utf8.UTFMax]byte{} |
| 517 |
newBAndKs := [utf8.UTFMax + 1]bAndK{ |
| 518 |
0: bAndK{b, k}, |
| 519 |
} |
| 520 |
n := int32(0) |
| 521 |
for { |
| 522 |
if k < bb.j { |
| 523 |
nCopied := int32(copy(buf[n:], f.text[k:bb.j])) |
| 524 |
for i := int32(1); i <= nCopied; i++ { |
| 525 |
newBAndKs[n+i] = bAndK{b, k + i} |
| 526 |
} |
| 527 |
n += nCopied |
| 528 |
if n == utf8.UTFMax { |
| 529 |
break |
| 530 |
} |
| 531 |
} |
| 532 |
b = bb.next |
| 533 |
if b == 0 { |
| 534 |
break |
| 535 |
} |
| 536 |
bb = &f.boxes[b] |
| 537 |
k = bb.i |
| 538 |
} |
| 539 |
r, size = utf8.DecodeRune(buf[:n]) |
| 540 |
bk := newBAndKs[size] |
| 541 |
if bk.b == 0 { |
| 542 |
panic("text: invalid state") |
| 543 |
} |
| 544 |
return r, size, bk.b, bk.k |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
func (f *Frame) readLastRune(b, k int32) (r rune, size int, newB, newK int32) { |
| 550 |
bb := &f.boxes[b] |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
r, size = utf8.DecodeLastRune(f.text[bb.i:k]) |
| 555 |
if r < utf8.RuneSelf || size > 1 { |
| 556 |
return r, size, b, k - int32(size) |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
buf := [utf8.UTFMax]byte{} |
| 563 |
newBAndKs := [utf8.UTFMax + 1]bAndK{ |
| 564 |
utf8.UTFMax: bAndK{b, k}, |
| 565 |
} |
| 566 |
n := int32(utf8.UTFMax) |
| 567 |
for { |
| 568 |
if k < bb.j { |
| 569 |
nCopied := k - bb.i |
| 570 |
if nCopied > n { |
| 571 |
nCopied = n |
| 572 |
} |
| 573 |
copy(buf[n-nCopied:n], f.text[k-nCopied:k]) |
| 574 |
for i := int32(1); i <= nCopied; i++ { |
| 575 |
newBAndKs[n-i] = bAndK{b, k - i} |
| 576 |
} |
| 577 |
n -= nCopied |
| 578 |
if n == 0 { |
| 579 |
break |
| 580 |
} |
| 581 |
} |
| 582 |
b = bb.prev |
| 583 |
if b == 0 { |
| 584 |
break |
| 585 |
} |
| 586 |
bb = &f.boxes[b] |
| 587 |
k = bb.j |
| 588 |
} |
| 589 |
r, size = utf8.DecodeLastRune(buf[n:]) |
| 590 |
bk := newBAndKs[utf8.UTFMax-size] |
| 591 |
if bk.b == 0 { |
| 592 |
panic("text: invalid state") |
| 593 |
} |
| 594 |
return r, size, bk.b, bk.k |
| 595 |
} |
| 596 |
|
| 597 |
func (f *Frame) lineReader(b, k int32) lineReader { |
| 598 |
f.lineReaderData.b = b |
| 599 |
f.lineReaderData.k = k |
| 600 |
return lineReader{f} |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
type bAndK struct { |
| 607 |
b int32 |
| 608 |
k int32 |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
type lineReader struct{ f *Frame } |
| 618 |
|
| 619 |
func (z lineReader) bAndK() bAndK { |
| 620 |
return z.f.lineReaderData |
| 621 |
} |
| 622 |
|
| 623 |
func (z lineReader) ReadRune() (r rune, size int, err error) { |
| 624 |
d := &z.f.lineReaderData |
| 625 |
for d.b != 0 { |
| 626 |
bb := &z.f.boxes[d.b] |
| 627 |
if d.k < bb.j { |
| 628 |
r, size, d.b, d.k = z.f.readRune(d.b, d.k) |
| 629 |
return r, size, nil |
| 630 |
} |
| 631 |
d.b = bb.next |
| 632 |
d.k = z.f.boxes[d.b].i |
| 633 |
} |
| 634 |
return 0, 0, io.EOF |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
type Paragraph struct { |
| 639 |
firstL, next, prev int32 |
| 640 |
cachedHeightPlus1 int32 |
| 641 |
cachedLineCountPlus1 int32 |
| 642 |
} |
| 643 |
|
| 644 |
func (p *Paragraph) lastLine(f *Frame) int32 { |
| 645 |
for l := p.firstL; ; { |
| 646 |
if next := f.lines[l].next; next != 0 { |
| 647 |
l = next |
| 648 |
continue |
| 649 |
} |
| 650 |
return l |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
func (p *Paragraph) FirstLine(f *Frame) *Line { |
| 658 |
return &f.lines[p.firstL] |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
func (p *Paragraph) Next(f *Frame) *Paragraph { |
| 665 |
if p.next == 0 { |
| 666 |
return nil |
| 667 |
} |
| 668 |
return &f.paragraphs[p.next] |
| 669 |
} |
| 670 |
|
| 671 |
func (p *Paragraph) invalidateCaches() { |
| 672 |
p.cachedHeightPlus1 = 0 |
| 673 |
p.cachedLineCountPlus1 = 0 |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
func (p *Paragraph) Height(f *Frame) int { |
| 678 |
if p.cachedHeightPlus1 <= 0 { |
| 679 |
h := 1 |
| 680 |
for l := p.firstL; l != 0; l = f.lines[l].next { |
| 681 |
h += f.lines[l].Height(f) |
| 682 |
} |
| 683 |
p.cachedHeightPlus1 = int32(h) |
| 684 |
} |
| 685 |
return int(p.cachedHeightPlus1 - 1) |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
func (p *Paragraph) LineCount(f *Frame) int { |
| 692 |
if p.cachedLineCountPlus1 <= 0 { |
| 693 |
n := 1 |
| 694 |
for l := p.firstL; l != 0; l = f.lines[l].next { |
| 695 |
n++ |
| 696 |
} |
| 697 |
p.cachedLineCountPlus1 = int32(n) |
| 698 |
} |
| 699 |
return int(p.cachedLineCountPlus1 - 1) |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
type Line struct { |
| 704 |
firstB, next, prev int32 |
| 705 |
cachedHeightPlus1 int32 |
| 706 |
} |
| 707 |
|
| 708 |
func (l *Line) lastBox(f *Frame) int32 { |
| 709 |
for b := l.firstB; ; { |
| 710 |
if next := f.boxes[b].next; next != 0 { |
| 711 |
b = next |
| 712 |
continue |
| 713 |
} |
| 714 |
return b |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
func (l *Line) FirstBox(f *Frame) *Box { |
| 722 |
return &f.boxes[l.firstB] |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
func (l *Line) Next(f *Frame) *Line { |
| 729 |
if l.next == 0 { |
| 730 |
return nil |
| 731 |
} |
| 732 |
return &f.lines[l.next] |
| 733 |
} |
| 734 |
|
| 735 |
func (l *Line) invalidateCaches() { |
| 736 |
l.cachedHeightPlus1 = 0 |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
func (l *Line) Height(f *Frame) int { |
| 741 |
|
| 742 |
|
| 743 |
if f.face == nil { |
| 744 |
return 0 |
| 745 |
} |
| 746 |
if l.cachedHeightPlus1 <= 0 { |
| 747 |
l.cachedHeightPlus1 = f.faceHeight + 1 |
| 748 |
} |
| 749 |
return int(l.cachedHeightPlus1 - 1) |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
type Box struct { |
| 754 |
next, prev int32 |
| 755 |
|
| 756 |
i, j int32 |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
func (b *Box) Next(f *Frame) *Box { |
| 763 |
if b.next == 0 { |
| 764 |
return nil |
| 765 |
} |
| 766 |
return &f.boxes[b.next] |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
func (b *Box) Text(f *Frame) []byte { |
| 773 |
return f.text[b.i:b.j] |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
func (b *Box) TrimmedText(f *Frame) []byte { |
| 781 |
s := f.text[b.i:b.j] |
| 782 |
if b.next == 0 { |
| 783 |
for len(s) > 0 && s[len(s)-1] <= ' ' { |
| 784 |
s = s[:len(s)-1] |
| 785 |
} |
| 786 |
} |
| 787 |
return s |
| 788 |
} |