widget: optimize processGlyph

processGlyph does not modify the value, so there's no reason to
return the struct.

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
This commit is contained in:
Egon Elbre
2023-10-23 22:04:52 +03:00
committed by Chris Waldon
parent df8a8789a3
commit 48bd5952b1
3 changed files with 13 additions and 11 deletions
+4 -4
View File
@@ -134,7 +134,7 @@ type textIterator struct {
// processGlyph checks whether the glyph is visible within the iterator's configured // processGlyph checks whether the glyph is visible within the iterator's configured
// viewport and (if so) updates the iterator's text dimensions to include the glyph. // viewport and (if so) updates the iterator's text dimensions to include the glyph.
func (it *textIterator) processGlyph(g text.Glyph, ok bool) (_ text.Glyph, visibleOrBefore bool) { func (it *textIterator) processGlyph(g text.Glyph, ok bool) (visibleOrBefore bool) {
if it.maxLines > 0 { if it.maxLines > 0 {
if g.Flags&text.FlagTruncator != 0 && g.Flags&text.FlagClusterBreak != 0 { if g.Flags&text.FlagTruncator != 0 && g.Flags&text.FlagClusterBreak != 0 {
// A glyph carrying both of these flags provides the count of truncated runes. // A glyph carrying both of these flags provides the count of truncated runes.
@@ -144,7 +144,7 @@ func (it *textIterator) processGlyph(g text.Glyph, ok bool) (_ text.Glyph, visib
it.linesSeen++ it.linesSeen++
} }
if it.linesSeen == it.maxLines && g.Flags&text.FlagParagraphBreak != 0 { if it.linesSeen == it.maxLines && g.Flags&text.FlagParagraphBreak != 0 {
return g, false return false
} }
} }
// Compute the maximum extent to which glyphs overhang on the horizontal // Compute the maximum extent to which glyphs overhang on the horizontal
@@ -191,7 +191,7 @@ func (it *textIterator) processGlyph(g text.Glyph, ok bool) (_ text.Glyph, visib
it.bounds.Max.X = max(it.bounds.Max.X, logicalBounds.Max.X) it.bounds.Max.X = max(it.bounds.Max.X, logicalBounds.Max.X)
it.bounds.Max.Y = max(it.bounds.Max.Y, logicalBounds.Max.Y) it.bounds.Max.Y = max(it.bounds.Max.Y, logicalBounds.Max.Y)
} }
return g, ok && !below return ok && !below
} }
func fixedToFloat(i fixed.Int26_6) float32 { func fixedToFloat(i fixed.Int26_6) float32 {
@@ -206,7 +206,7 @@ func fixedToFloat(i fixed.Int26_6) float32 {
// This design is awkward, but prevents the line slice from escaping // This design is awkward, but prevents the line slice from escaping
// to the heap. // to the heap.
func (it *textIterator) paintGlyph(gtx layout.Context, shaper *text.Shaper, glyph text.Glyph, line []text.Glyph) ([]text.Glyph, bool) { func (it *textIterator) paintGlyph(gtx layout.Context, shaper *text.Shaper, glyph text.Glyph, line []text.Glyph) ([]text.Glyph, bool) {
_, visibleOrBefore := it.processGlyph(glyph, true) visibleOrBefore := it.processGlyph(glyph, true)
if it.visible { if it.visible {
if len(line) == 0 { if len(line) == 0 {
it.lineOff = f32.Point{X: fixedToFloat(glyph.X), Y: float32(glyph.Y)}.Sub(layout.FPt(it.viewport.Min)) it.lineOff = f32.Point{X: fixedToFloat(glyph.X), Y: float32(glyph.Y)}.Sub(layout.FPt(it.viewport.Min))
+1 -4
View File
@@ -146,10 +146,7 @@ func TestGlyphIterator(t *testing.T) {
glyphs := getGlyphs(16, 0, maxWidth, text.Start, tc.str) glyphs := getGlyphs(16, 0, maxWidth, text.Start, tc.str)
it := textIterator{viewport: tc.viewport, maxLines: tc.maxLines} it := textIterator{viewport: tc.viewport, maxLines: tc.maxLines}
for i, g := range glyphs { for i, g := range glyphs {
gOut, ok := it.processGlyph(g, true) ok := it.processGlyph(g, true)
if gOut != g {
t.Errorf("textIterator modified glyphs[%d], original:\n%#+v, modified:\n%#+v", i, g, gOut)
}
if !ok && i != tc.stopAtGlyph { if !ok && i != tc.stopAtGlyph {
t.Errorf("expected iterator to stop at glyph %d, stopped at %d", tc.stopAtGlyph, i) t.Errorf("expected iterator to stop at glyph %d, stopped at %d", tc.stopAtGlyph, i)
} }
+8 -3
View File
@@ -492,14 +492,19 @@ func (e *textView) layoutText(lt *text.Shaper) {
it := textIterator{viewport: image.Rectangle{Max: image.Point{X: math.MaxInt, Y: math.MaxInt}}} it := textIterator{viewport: image.Rectangle{Max: image.Point{X: math.MaxInt, Y: math.MaxInt}}}
if lt != nil { if lt != nil {
lt.Layout(e.params, r) lt.Layout(e.params, r)
for glyph, ok := it.processGlyph(lt.NextGlyph()); ok; glyph, ok = it.processGlyph(lt.NextGlyph()) { for {
e.index.Glyph(glyph) g, ok := lt.NextGlyph()
if !it.processGlyph(g, ok) {
break
}
e.index.Glyph(g)
} }
} else { } else {
// Make a fake glyph for every rune in the reader. // Make a fake glyph for every rune in the reader.
b := bufio.NewReader(r) b := bufio.NewReader(r)
for _, _, err := b.ReadRune(); err != io.EOF; _, _, err = b.ReadRune() { for _, _, err := b.ReadRune(); err != io.EOF; _, _, err = b.ReadRune() {
g, _ := it.processGlyph(text.Glyph{Runes: 1, Flags: text.FlagClusterBreak}, true) g := text.Glyph{Runes: 1, Flags: text.FlagClusterBreak}
_ = it.processGlyph(g, true)
e.index.Glyph(g) e.index.Glyph(g)
} }
} }