2017-05-02 15:46:51 -05:00
|
|
|
package text
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image/color"
|
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
|
|
|
"github.com/faiface/pixel"
|
|
|
|
"golang.org/x/image/font"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ASCII []rune
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
ASCII = make([]rune, unicode.MaxASCII-32)
|
|
|
|
for i := range ASCII {
|
|
|
|
ASCII[i] = rune(32 + i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RangeTable(table *unicode.RangeTable) []rune {
|
|
|
|
var runes []rune
|
|
|
|
for _, rng := range table.R16 {
|
|
|
|
for r := rng.Lo; r <= rng.Hi; r += rng.Stride {
|
|
|
|
runes = append(runes, rune(r))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, rng := range table.R32 {
|
|
|
|
for r := rng.Lo; r <= rng.Hi; r += rng.Stride {
|
|
|
|
runes = append(runes, rune(r))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return runes
|
|
|
|
}
|
|
|
|
|
|
|
|
type Text struct {
|
2017-05-03 13:56:06 -05:00
|
|
|
Orig pixel.Vec
|
|
|
|
Dot pixel.Vec
|
2017-05-02 15:46:51 -05:00
|
|
|
|
2017-05-03 16:54:24 -05:00
|
|
|
atlas *Atlas
|
2017-05-03 14:04:18 -05:00
|
|
|
|
|
|
|
color pixel.RGBA
|
|
|
|
lineHeight float64
|
|
|
|
tabWidth float64
|
2017-05-02 15:46:51 -05:00
|
|
|
|
|
|
|
prevR rune
|
2017-05-02 17:13:39 -05:00
|
|
|
glyph pixel.TrianglesData
|
2017-05-02 15:46:51 -05:00
|
|
|
tris pixel.TrianglesData
|
|
|
|
d pixel.Drawer
|
2017-05-03 14:48:05 -05:00
|
|
|
trans *pixel.Batch
|
2017-05-02 15:46:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(face font.Face, runeSets ...[]rune) *Text {
|
|
|
|
runes := []rune{unicode.ReplacementChar}
|
|
|
|
for _, set := range runeSets {
|
|
|
|
runes = append(runes, set...)
|
|
|
|
}
|
|
|
|
|
2017-05-03 16:54:24 -05:00
|
|
|
atlas := NewAtlas(face, runes)
|
2017-05-03 14:04:18 -05:00
|
|
|
|
2017-05-02 15:46:51 -05:00
|
|
|
txt := &Text{
|
2017-05-03 14:04:18 -05:00
|
|
|
atlas: atlas,
|
|
|
|
color: pixel.Alpha(1),
|
2017-05-03 16:57:09 -05:00
|
|
|
lineHeight: atlas.LineHeight(),
|
|
|
|
tabWidth: atlas.Glyph(' ').Advance * 4,
|
2017-05-02 15:46:51 -05:00
|
|
|
}
|
2017-05-02 17:13:39 -05:00
|
|
|
txt.glyph.SetLen(6)
|
2017-05-02 15:46:51 -05:00
|
|
|
txt.d.Picture = txt.atlas.pic
|
|
|
|
txt.d.Triangles = &txt.tris
|
2017-05-03 14:48:05 -05:00
|
|
|
txt.trans = pixel.NewBatch(&pixel.TrianglesData{}, atlas.pic)
|
2017-05-02 15:46:51 -05:00
|
|
|
|
|
|
|
txt.Clear()
|
|
|
|
|
|
|
|
return txt
|
|
|
|
}
|
|
|
|
|
2017-05-03 16:55:44 -05:00
|
|
|
func (txt *Text) Atlas() *Atlas {
|
|
|
|
return txt.atlas
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:48:05 -05:00
|
|
|
func (txt *Text) SetMatrix(m pixel.Matrix) {
|
|
|
|
txt.trans.SetMatrix(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (txt *Text) SetColorMask(c color.Color) {
|
|
|
|
txt.trans.SetColorMask(c)
|
|
|
|
}
|
|
|
|
|
2017-05-02 15:46:51 -05:00
|
|
|
func (txt *Text) Color(c color.Color) {
|
|
|
|
txt.color = pixel.ToRGBA(c)
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:04:18 -05:00
|
|
|
func (txt *Text) LineHeight(scale float64) {
|
|
|
|
txt.lineHeight = scale
|
|
|
|
}
|
|
|
|
|
|
|
|
func (txt *Text) TabWidth(width float64) {
|
|
|
|
txt.tabWidth = width
|
|
|
|
}
|
|
|
|
|
2017-05-02 15:46:51 -05:00
|
|
|
func (txt *Text) Clear() {
|
|
|
|
txt.prevR = -1
|
|
|
|
txt.tris.SetLen(0)
|
|
|
|
txt.d.Dirty()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (txt *Text) Write(p []byte) (n int, err error) {
|
2017-05-03 16:54:24 -05:00
|
|
|
n, err = len(p), nil // always returns this
|
|
|
|
|
2017-05-02 15:46:51 -05:00
|
|
|
if len(p) == 0 {
|
2017-05-03 16:54:24 -05:00
|
|
|
return
|
2017-05-02 15:46:51 -05:00
|
|
|
}
|
|
|
|
|
2017-05-02 17:13:39 -05:00
|
|
|
for i := range txt.glyph {
|
|
|
|
txt.glyph[i].Color = txt.color
|
|
|
|
txt.glyph[i].Intensity = 1
|
2017-05-02 15:46:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for len(p) > 0 {
|
|
|
|
r, size := utf8.DecodeRune(p)
|
|
|
|
p = p[size:]
|
2017-05-03 16:54:24 -05:00
|
|
|
txt.WriteRune(r)
|
|
|
|
}
|
2017-05-02 15:46:51 -05:00
|
|
|
|
2017-05-03 16:54:24 -05:00
|
|
|
return
|
|
|
|
}
|
2017-05-02 15:46:51 -05:00
|
|
|
|
2017-05-03 16:54:24 -05:00
|
|
|
func (txt *Text) WriteString(s string) (n int, err error) {
|
|
|
|
if len(s) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2017-05-02 15:46:51 -05:00
|
|
|
|
2017-05-03 16:54:24 -05:00
|
|
|
for i := range txt.glyph {
|
|
|
|
txt.glyph[i].Color = txt.color
|
|
|
|
txt.glyph[i].Intensity = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range s {
|
|
|
|
txt.WriteRune(r)
|
|
|
|
}
|
2017-05-02 15:46:51 -05:00
|
|
|
|
2017-05-03 16:54:24 -05:00
|
|
|
return len(s), nil
|
|
|
|
}
|
2017-05-02 15:46:51 -05:00
|
|
|
|
2017-05-03 16:55:10 -05:00
|
|
|
func (txt *Text) WriteByte(c byte) error {
|
|
|
|
_, err := txt.WriteRune(rune(c))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-03 16:54:24 -05:00
|
|
|
func (txt *Text) WriteRune(r rune) (n int, err error) {
|
|
|
|
n, err = utf8.RuneLen(r), nil // always returns this
|
|
|
|
|
|
|
|
switch r {
|
|
|
|
case '\n':
|
2017-05-03 16:57:09 -05:00
|
|
|
txt.Dot -= pixel.Y(txt.lineHeight)
|
2017-05-03 16:54:24 -05:00
|
|
|
txt.Dot = txt.Dot.WithX(txt.Orig.X())
|
|
|
|
return
|
|
|
|
case '\r':
|
|
|
|
txt.Dot = txt.Dot.WithX(txt.Orig.X())
|
|
|
|
return
|
|
|
|
case '\t':
|
|
|
|
//TODO: properly align tab
|
|
|
|
txt.Dot += pixel.X(txt.tabWidth)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !txt.atlas.Contains(r) {
|
|
|
|
r = unicode.ReplacementChar
|
|
|
|
}
|
|
|
|
if !txt.atlas.Contains(unicode.ReplacementChar) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
glyph := txt.atlas.Glyph(r)
|
2017-05-02 15:46:51 -05:00
|
|
|
|
2017-05-03 16:54:24 -05:00
|
|
|
if txt.prevR >= 0 {
|
|
|
|
txt.Dot += pixel.X(txt.atlas.Kern(txt.prevR, r))
|
|
|
|
}
|
|
|
|
|
|
|
|
a := pixel.V(glyph.Frame.Min.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())
|
|
|
|
d := pixel.V(glyph.Frame.Min.X(), glyph.Frame.Max.Y())
|
2017-05-02 15:46:51 -05:00
|
|
|
|
2017-05-03 16:54:24 -05:00
|
|
|
for i, v := range []pixel.Vec{a, b, c, a, c, d} {
|
|
|
|
txt.glyph[i].Position = v - glyph.Orig + txt.Dot
|
|
|
|
txt.glyph[i].Picture = v
|
2017-05-02 15:46:51 -05:00
|
|
|
}
|
|
|
|
|
2017-05-03 16:54:24 -05:00
|
|
|
txt.tris = append(txt.tris, txt.glyph...)
|
|
|
|
|
|
|
|
txt.Dot += pixel.X(glyph.Advance)
|
|
|
|
txt.prevR = r
|
|
|
|
|
2017-05-02 15:46:51 -05:00
|
|
|
txt.d.Dirty()
|
|
|
|
|
2017-05-03 16:54:24 -05:00
|
|
|
return
|
2017-05-02 15:46:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (txt *Text) Draw(t pixel.Target) {
|
2017-05-03 14:48:05 -05:00
|
|
|
txt.trans.Clear()
|
|
|
|
txt.d.Draw(txt.trans)
|
|
|
|
txt.trans.Draw(t)
|
2017-05-02 15:46:51 -05:00
|
|
|
}
|