2017-05-02 15:46:51 -05:00
|
|
|
package text
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image/color"
|
2017-05-04 15:30:18 -05:00
|
|
|
"math"
|
2017-05-02 15:46:51 -05:00
|
|
|
"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
|
|
|
|
|
|
|
lineHeight float64
|
|
|
|
tabWidth float64
|
2017-05-02 15:46:51 -05:00
|
|
|
|
2017-05-07 13:59:56 -05:00
|
|
|
buf []byte
|
2017-05-05 09:13:26 -05:00
|
|
|
prevR rune
|
|
|
|
bounds pixel.Rect
|
|
|
|
glyph pixel.TrianglesData
|
|
|
|
tris pixel.TrianglesData
|
|
|
|
|
|
|
|
mat pixel.Matrix
|
|
|
|
col pixel.RGBA
|
|
|
|
trans pixel.TrianglesData
|
|
|
|
transD pixel.Drawer
|
|
|
|
dirty bool
|
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,
|
2017-05-03 16:57:09 -05:00
|
|
|
lineHeight: atlas.LineHeight(),
|
|
|
|
tabWidth: atlas.Glyph(' ').Advance * 4,
|
2017-05-05 09:13:26 -05:00
|
|
|
mat: pixel.IM,
|
|
|
|
col: pixel.Alpha(1),
|
2017-05-02 15:46:51 -05:00
|
|
|
}
|
2017-05-04 15:30:18 -05:00
|
|
|
|
2017-05-02 17:13:39 -05:00
|
|
|
txt.glyph.SetLen(6)
|
2017-05-04 15:30:18 -05:00
|
|
|
for i := range txt.glyph {
|
|
|
|
txt.glyph[i].Color = pixel.Alpha(1)
|
|
|
|
txt.glyph[i].Intensity = 1
|
|
|
|
}
|
|
|
|
|
2017-05-05 09:13:26 -05:00
|
|
|
txt.transD.Picture = txt.atlas.pic
|
|
|
|
txt.transD.Triangles = &txt.trans
|
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) {
|
2017-05-05 09:13:26 -05:00
|
|
|
if txt.mat != m {
|
|
|
|
txt.mat = m
|
|
|
|
txt.dirty = true
|
|
|
|
}
|
2017-05-03 14:48:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (txt *Text) SetColorMask(c color.Color) {
|
2017-05-05 09:13:26 -05:00
|
|
|
rgba := pixel.ToRGBA(c)
|
|
|
|
if txt.col != rgba {
|
|
|
|
txt.col = rgba
|
|
|
|
txt.dirty = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (txt *Text) Bounds() pixel.Rect {
|
|
|
|
return txt.bounds
|
2017-05-03 14:48:05 -05:00
|
|
|
}
|
|
|
|
|
2017-05-07 14:00:19 -05:00
|
|
|
func (txt *Text) BoundsOf(s string) pixel.Rect {
|
|
|
|
dot := txt.Dot
|
|
|
|
prevR := txt.prevR
|
|
|
|
bounds := pixel.Rect{}
|
|
|
|
|
|
|
|
for _, r := range s {
|
2017-05-07 14:08:10 -05:00
|
|
|
var control bool
|
|
|
|
dot, control = txt.controlRune(r, dot)
|
|
|
|
if control {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-05-07 14:00:19 -05:00
|
|
|
var b pixel.Rect
|
|
|
|
_, _, b, dot = txt.Atlas().DrawRune(prevR, r, dot)
|
|
|
|
|
|
|
|
if bounds.W()*bounds.H() == 0 {
|
|
|
|
bounds = b
|
|
|
|
} else {
|
|
|
|
bounds = bounds.Union(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
prevR = r
|
|
|
|
}
|
|
|
|
|
|
|
|
return bounds
|
|
|
|
}
|
|
|
|
|
2017-05-02 15:46:51 -05:00
|
|
|
func (txt *Text) Color(c color.Color) {
|
2017-05-04 15:30:18 -05:00
|
|
|
rgba := pixel.ToRGBA(c)
|
|
|
|
for i := range txt.glyph {
|
|
|
|
txt.glyph[i].Color = rgba
|
|
|
|
}
|
2017-05-02 15:46:51 -05:00
|
|
|
}
|
|
|
|
|
2017-05-07 15:03:56 -05:00
|
|
|
func (txt *Text) LineHeight(height float64) {
|
|
|
|
txt.lineHeight = height
|
2017-05-03 14:04:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (txt *Text) TabWidth(width float64) {
|
|
|
|
txt.tabWidth = width
|
|
|
|
}
|
|
|
|
|
2017-05-02 15:46:51 -05:00
|
|
|
func (txt *Text) Clear() {
|
|
|
|
txt.prevR = -1
|
2017-05-05 09:13:26 -05:00
|
|
|
txt.bounds = pixel.Rect{}
|
2017-05-02 15:46:51 -05:00
|
|
|
txt.tris.SetLen(0)
|
2017-05-05 09:13:26 -05:00
|
|
|
txt.dirty = true
|
2017-05-02 15:46:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (txt *Text) Write(p []byte) (n int, err error) {
|
2017-05-07 13:59:56 -05:00
|
|
|
txt.buf = append(txt.buf, p...)
|
|
|
|
txt.drawBuf()
|
|
|
|
return len(p), nil
|
2017-05-03 16:54:24 -05:00
|
|
|
}
|
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) {
|
2017-05-07 13:59:56 -05:00
|
|
|
txt.buf = append(txt.buf, s...)
|
|
|
|
txt.drawBuf()
|
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 {
|
2017-05-07 13:59:56 -05:00
|
|
|
txt.buf = append(txt.buf, c)
|
|
|
|
txt.drawBuf()
|
|
|
|
return nil
|
2017-05-03 16:55:10 -05:00
|
|
|
}
|
|
|
|
|
2017-05-03 16:54:24 -05:00
|
|
|
func (txt *Text) WriteRune(r rune) (n int, err error) {
|
2017-05-07 13:59:56 -05:00
|
|
|
var b [4]byte
|
|
|
|
n = utf8.EncodeRune(b[:], r)
|
|
|
|
txt.buf = append(txt.buf, b[:n]...)
|
|
|
|
txt.drawBuf()
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2017-05-07 14:08:10 -05:00
|
|
|
// controlRune checks if r is a control rune (newline, tab, ...). If it is, a new dot position and
|
|
|
|
// true is returned. If r is not a control rune, the original dot and false is returned.
|
|
|
|
func (txt *Text) controlRune(r rune, dot pixel.Vec) (newDot pixel.Vec, control bool) {
|
|
|
|
switch r {
|
|
|
|
case '\n':
|
|
|
|
dot -= pixel.Y(txt.lineHeight)
|
|
|
|
dot = dot.WithX(txt.Orig.X())
|
|
|
|
case '\r':
|
|
|
|
dot = dot.WithX(txt.Orig.X())
|
|
|
|
case '\t':
|
|
|
|
rem := math.Mod(dot.X()-txt.Orig.X(), txt.tabWidth)
|
|
|
|
rem = math.Mod(rem, rem+txt.tabWidth)
|
|
|
|
if rem == 0 {
|
|
|
|
rem = txt.tabWidth
|
|
|
|
}
|
|
|
|
dot += pixel.X(rem)
|
|
|
|
default:
|
|
|
|
return dot, false
|
|
|
|
}
|
|
|
|
return dot, true
|
|
|
|
}
|
|
|
|
|
2017-05-07 13:59:56 -05:00
|
|
|
func (txt *Text) drawBuf() {
|
|
|
|
for utf8.FullRune(txt.buf) {
|
|
|
|
r, size := utf8.DecodeRune(txt.buf)
|
|
|
|
txt.buf = txt.buf[size:]
|
|
|
|
|
2017-05-07 14:08:10 -05:00
|
|
|
var control bool
|
|
|
|
txt.Dot, control = txt.controlRune(r, txt.Dot)
|
|
|
|
if control {
|
2017-05-07 13:59:56 -05:00
|
|
|
continue
|
2017-05-04 15:30:18 -05:00
|
|
|
}
|
2017-05-03 16:54:24 -05:00
|
|
|
|
2017-05-07 13:59:56 -05:00
|
|
|
var rect, frame, bounds pixel.Rect
|
|
|
|
rect, frame, bounds, txt.Dot = txt.Atlas().DrawRune(txt.prevR, r, txt.Dot)
|
2017-05-02 15:46:51 -05:00
|
|
|
|
2017-05-07 13:59:56 -05:00
|
|
|
txt.prevR = r
|
2017-05-03 16:54:24 -05:00
|
|
|
|
2017-05-07 13:59:56 -05:00
|
|
|
rv := [...]pixel.Vec{pixel.V(rect.Min.X(), rect.Min.Y()),
|
|
|
|
pixel.V(rect.Max.X(), rect.Min.Y()),
|
|
|
|
pixel.V(rect.Max.X(), rect.Max.Y()),
|
|
|
|
pixel.V(rect.Min.X(), rect.Max.Y()),
|
2017-05-05 09:42:40 -05:00
|
|
|
}
|
2017-05-02 15:46:51 -05:00
|
|
|
|
2017-05-07 13:59:56 -05:00
|
|
|
fv := [...]pixel.Vec{pixel.V(frame.Min.X(), frame.Min.Y()),
|
|
|
|
pixel.V(frame.Max.X(), frame.Min.Y()),
|
|
|
|
pixel.V(frame.Max.X(), frame.Max.Y()),
|
|
|
|
pixel.V(frame.Min.X(), frame.Max.Y()),
|
|
|
|
}
|
2017-05-03 16:54:24 -05:00
|
|
|
|
2017-05-07 13:59:56 -05:00
|
|
|
for i, j := range [...]int{0, 1, 2, 0, 2, 3} {
|
|
|
|
txt.glyph[i].Position = rv[j]
|
|
|
|
txt.glyph[i].Picture = fv[j]
|
|
|
|
}
|
2017-05-03 16:54:24 -05:00
|
|
|
|
2017-05-07 13:59:56 -05:00
|
|
|
txt.tris = append(txt.tris, txt.glyph...)
|
|
|
|
txt.dirty = true
|
2017-05-02 15:46:51 -05:00
|
|
|
|
2017-05-07 13:59:56 -05:00
|
|
|
if txt.bounds.W()*txt.bounds.H() == 0 {
|
|
|
|
txt.bounds = bounds
|
|
|
|
} else {
|
|
|
|
txt.bounds = txt.bounds.Union(bounds)
|
|
|
|
}
|
|
|
|
}
|
2017-05-02 15:46:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (txt *Text) Draw(t pixel.Target) {
|
2017-05-05 09:13:26 -05:00
|
|
|
if txt.dirty {
|
|
|
|
txt.trans.SetLen(txt.tris.Len())
|
|
|
|
txt.trans.Update(&txt.tris)
|
2017-05-07 14:12:48 -05:00
|
|
|
|
2017-05-05 09:13:26 -05:00
|
|
|
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)
|
|
|
|
}
|
2017-05-07 14:12:48 -05:00
|
|
|
|
2017-05-05 09:13:26 -05:00
|
|
|
txt.transD.Dirty()
|
|
|
|
txt.dirty = false
|
|
|
|
}
|
2017-05-07 14:12:48 -05:00
|
|
|
|
2017-05-05 09:13:26 -05:00
|
|
|
txt.transD.Draw(t)
|
2017-05-02 15:46:51 -05:00
|
|
|
}
|