remove Batch from Text and optimize it

This commit is contained in:
faiface 2017-05-05 16:13:26 +02:00
parent af0330d453
commit 75a25a0df6
1 changed files with 55 additions and 15 deletions

View File

@ -43,11 +43,16 @@ type Text struct {
lineHeight float64 lineHeight float64
tabWidth float64 tabWidth float64
prevR rune prevR rune
glyph pixel.TrianglesData bounds pixel.Rect
tris pixel.TrianglesData glyph pixel.TrianglesData
d pixel.Drawer tris pixel.TrianglesData
trans *pixel.Batch
mat pixel.Matrix
col pixel.RGBA
trans pixel.TrianglesData
transD pixel.Drawer
dirty bool
} }
func New(face font.Face, runeSets ...[]rune) *Text { func New(face font.Face, runeSets ...[]rune) *Text {
@ -62,6 +67,8 @@ func New(face font.Face, runeSets ...[]rune) *Text {
atlas: atlas, atlas: atlas,
lineHeight: atlas.LineHeight(), lineHeight: atlas.LineHeight(),
tabWidth: atlas.Glyph(' ').Advance * 4, tabWidth: atlas.Glyph(' ').Advance * 4,
mat: pixel.IM,
col: pixel.Alpha(1),
} }
txt.glyph.SetLen(6) txt.glyph.SetLen(6)
@ -70,9 +77,8 @@ func New(face font.Face, runeSets ...[]rune) *Text {
txt.glyph[i].Intensity = 1 txt.glyph[i].Intensity = 1
} }
txt.d.Picture = txt.atlas.pic txt.transD.Picture = txt.atlas.pic
txt.d.Triangles = &txt.tris txt.transD.Triangles = &txt.trans
txt.trans = pixel.NewBatch(&pixel.TrianglesData{}, atlas.pic)
txt.Clear() txt.Clear()
@ -84,11 +90,22 @@ func (txt *Text) Atlas() *Atlas {
} }
func (txt *Text) SetMatrix(m pixel.Matrix) { func (txt *Text) SetMatrix(m pixel.Matrix) {
txt.trans.SetMatrix(m) if txt.mat != m {
txt.mat = m
txt.dirty = true
}
} }
func (txt *Text) SetColorMask(c color.Color) { func (txt *Text) SetColorMask(c color.Color) {
txt.trans.SetColorMask(c) rgba := pixel.ToRGBA(c)
if txt.col != rgba {
txt.col = rgba
txt.dirty = true
}
}
func (txt *Text) Bounds() pixel.Rect {
return txt.bounds
} }
func (txt *Text) Color(c color.Color) { func (txt *Text) Color(c color.Color) {
@ -108,8 +125,9 @@ func (txt *Text) TabWidth(width float64) {
func (txt *Text) Clear() { func (txt *Text) Clear() {
txt.prevR = -1 txt.prevR = -1
txt.bounds = pixel.Rect{}
txt.tris.SetLen(0) txt.tris.SetLen(0)
txt.d.Dirty() txt.dirty = true
} }
func (txt *Text) Write(p []byte) (n int, err error) { func (txt *Text) Write(p []byte) (n int, err error) {
@ -141,6 +159,7 @@ func (txt *Text) WriteString(s string) (n int, err error) {
} }
func (txt *Text) WriteByte(c byte) error { func (txt *Text) WriteByte(c byte) error {
//FIXME: this is not correct, what if I want to write a 4-byte rune byte by byte?
_, err := txt.WriteRune(rune(c)) _, err := txt.WriteRune(rune(c))
return err return err
} }
@ -179,6 +198,19 @@ func (txt *Text) WriteRune(r rune) (n int, err error) {
txt.Dot += pixel.X(txt.atlas.Kern(txt.prevR, r)) txt.Dot += pixel.X(txt.atlas.Kern(txt.prevR, r))
} }
glyphBounds := glyph.Frame.Moved(txt.Dot - glyph.Orig)
if glyphBounds.H() > 0 {
glyphBounds = glyphBounds.Resized(txt.Dot, pixel.V(
glyphBounds.W(),
txt.atlas.Ascent()+txt.atlas.Descent(),
))
}
if txt.bounds.W()*txt.bounds.H() == 0 {
txt.bounds = glyphBounds
} else {
txt.bounds = txt.bounds.Union(glyphBounds)
}
a := pixel.V(glyph.Frame.Min.X(), glyph.Frame.Min.Y()) a := pixel.V(glyph.Frame.Min.X(), glyph.Frame.Min.Y())
b := pixel.V(glyph.Frame.Max.X(), glyph.Frame.Min.Y()) b := pixel.V(glyph.Frame.Max.X(), glyph.Frame.Min.Y())
c := pixel.V(glyph.Frame.Max.X(), glyph.Frame.Max.Y()) c := pixel.V(glyph.Frame.Max.X(), glyph.Frame.Max.Y())
@ -194,13 +226,21 @@ func (txt *Text) WriteRune(r rune) (n int, err error) {
txt.Dot += pixel.X(glyph.Advance) txt.Dot += pixel.X(glyph.Advance)
txt.prevR = r txt.prevR = r
txt.d.Dirty() txt.dirty = true
return return
} }
func (txt *Text) Draw(t pixel.Target) { func (txt *Text) Draw(t pixel.Target) {
txt.trans.Clear() if txt.dirty {
txt.d.Draw(txt.trans) txt.trans.SetLen(txt.tris.Len())
txt.trans.Draw(t) txt.trans.Update(&txt.tris)
for i := range txt.trans {
txt.trans[i].Position = txt.mat.Project(txt.trans[i].Position)
txt.trans[i].Color = txt.trans[i].Color.Mul(txt.col)
}
txt.transD.Dirty()
txt.dirty = false
}
txt.transD.Draw(t)
} }