Add RunesWithNull iterator

This commit is contained in:
Sasha Koshka 2024-09-19 21:08:14 -04:00
parent e38cac8e3b
commit 17385c4c9a

View File

@ -93,9 +93,9 @@ type TypeSetter struct {
func (this *TypeSetter) Runes () RuneIter {
this.needFlow()
return func (yield func (fixed.Point26_6, rune) bool) {
for _, token := range this.tokens {
for _, runl := range token.runes {
pos := token.position
for _, tok := range this.tokens {
for _, runl := range tok.runes {
pos := tok.position
pos.X += runl.x
if !yield(pos, runl.run) { return }
}
@ -103,6 +103,27 @@ func (this *TypeSetter) Runes () RuneIter {
}
}
// RunesWithNull returns an iterator for all runes in the TypeSetter, plus an
// additional null rune at the end. This is useful for calculating the positions
// of things.
func (this *TypeSetter) RunesWithNull () RuneIter {
this.needFlow()
return func (yield func (fixed.Point26_6, rune) bool) {
var tok token
for _, tok = range this.tokens {
for _, runl := range tok.runes {
pos := tok.position
pos.X += runl.x
if !yield(pos, runl.run) { return }
}
}
pos := tok.position
pos.X += tok.width
yield(pos, 0)
}
}
// Em returns the width of one emspace according to the typesetter's typeface,
// which is the width of the capital letter 'M'.
func (this *TypeSetter) Em () fixed.Int26_6 {
@ -138,7 +159,7 @@ func (this *TypeSetter) LayoutBoundsSpace () fixed.Rectangle26_6 {
func (this *TypeSetter) PositionAt (index int) fixed.Point26_6 {
idx := 0
var position fixed.Point26_6
this.Runes()(func (pos fixed.Point26_6, run rune) bool {
this.RunesWithNull()(func (pos fixed.Point26_6, run rune) bool {
if index == idx {
position = pos
return false