widget: make glyphIndex reusable

This commit allows the glyph index type to be reset and reused, preventing the
reallocation of numerous buffers when indexing glyphs.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
This commit is contained in:
Chris Waldon
2023-03-16 12:13:41 -04:00
committed by Elias Naur
parent 25171df66a
commit 36e768e716
3 changed files with 39 additions and 13 deletions
+15 -1
View File
@@ -20,6 +20,7 @@ type lineInfo struct {
}
type glyphIndex struct {
// glyphs holds the glyphs processed.
glyphs []text.Glyph
// positions contain all possible caret positions, sorted by rune index.
positions []combinedPos
@@ -46,6 +47,20 @@ type glyphIndex struct {
skipPrior bool
}
// reset prepares the index for reuse.
func (g *glyphIndex) reset() {
g.glyphs = g.glyphs[:0]
g.positions = g.positions[:0]
g.lines = g.lines[:0]
g.currentLineMin = 0
g.currentLineMax = 0
g.currentLineGlyphs = 0
g.pos = combinedPos{}
g.prog = 0
g.clusterAdvance = 0
g.skipPrior = false
}
// screenPos represents a character position in text line and column numbers,
// not pixels.
type screenPos struct {
@@ -90,7 +105,6 @@ func (g *glyphIndex) incrementPosition(pos combinedPos) (next combinedPos, eof b
return g.positions[index+1], false
}
return candidate, true
}
// Glyph indexes the provided glyph, generating text cursor positions for it.