MCPcopy Create free account
hub / github.com/cogentcore/core / FindWrapPosLR

Method FindWrapPosLR

paint/span.go:523–568  ·  view source on GitHub ↗

FindWrapPosLR finds a position to do word wrapping to fit within trgSize. RelPos positions must have already been set (e.g., SetRunePosLR)

(trgSize, curSize float32)

Source from the content-addressed store, hash-verified

521// FindWrapPosLR finds a position to do word wrapping to fit within trgSize.
522// RelPos positions must have already been set (e.g., SetRunePosLR)
523func (sr *Span) FindWrapPosLR(trgSize, curSize float32) int {
524 sz := len(sr.Text)
525 if sz == 0 {
526 return -1
527 }
528 idx := int(float32(sz) * (trgSize / curSize))
529 if idx >= sz {
530 idx = sz - 1
531 }
532 // find starting index that is just within size
533 csz := sr.RelPos.X + sr.Render[idx].RelPosAfterLR()
534 if csz > trgSize {
535 for idx > 0 {
536 csz = sr.RelPos.X + sr.Render[idx].RelPosAfterLR()
537 if csz <= trgSize {
538 break
539 }
540 idx--
541 }
542 } else {
543 for idx < sz-1 {
544 nsz := sr.RelPos.X + sr.Render[idx+1].RelPosAfterLR()
545 if nsz > trgSize {
546 break
547 }
548 idx++
549 }
550 }
551 fitIdx := idx
552 if unicode.IsSpace(sr.Text[idx]) {
553 idx++
554 for idx < sz && unicode.IsSpace(sr.Text[idx]) { // break at END of whitespace
555 idx++
556 }
557 return idx
558 }
559 // find earlier space
560 for idx > 0 && !unicode.IsSpace(sr.Text[idx-1]) {
561 idx--
562 }
563 if idx > 0 {
564 return idx
565 }
566 // no spaces within size: do a hard break at point
567 return fitIdx
568}
569
570// ZeroPos ensures that the positions start at 0, for LR direction
571func (sr *Span) ZeroPosLR() {

Callers 1

LayoutStdLRMethod · 0.80

Calls 1

RelPosAfterLRMethod · 0.80

Tested by

no test coverage detected