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"
|
2018-01-19 16:08:06 -06:00
|
|
|
"golang.org/x/image/font/basicfont"
|
2017-05-02 15:46:51 -05:00
|
|
|
)
|
|
|
|
|
2017-05-09 07:20:34 -05:00
|
|
|
// ASCII is a set of all ASCII runes. These runes are codepoints from 32 to 127 inclusive.
|
2017-05-02 15:46:51 -05:00
|
|
|
var ASCII []rune
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
ASCII = make([]rune, unicode.MaxASCII-32)
|
|
|
|
for i := range ASCII {
|
|
|
|
ASCII[i] = rune(32 + i)
|
|
|
|
}
|
2018-01-19 16:08:06 -06:00
|
|
|
Atlas7x13 = NewAtlas(basicfont.Face7x13, ASCII)
|
2017-05-02 15:46:51 -05:00
|
|
|
}
|
|
|
|
|
2017-05-09 07:20:34 -05:00
|
|
|
// RangeTable takes a *unicode.RangeTable and generates a set of runes contained within that
|
|
|
|
// RangeTable.
|
2017-05-02 15:46:51 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-09 09:34:13 -05:00
|
|
|
// Text allows for effiecient and convenient text drawing.
|
2017-05-09 09:27:55 -05:00
|
|
|
//
|
|
|
|
// To create a Text object, use the New constructor:
|
2017-05-21 12:25:06 -05:00
|
|
|
// txt := text.New(pixel.ZV, text.NewAtlas(face, text.ASCII))
|
2017-05-09 09:27:55 -05:00
|
|
|
//
|
|
|
|
// As suggested by the constructor, a Text object is always associated with one font face and a
|
2017-05-09 09:34:54 -05:00
|
|
|
// fixed set of runes. For example, the Text we created above can draw text using the font face
|
2017-05-09 09:35:51 -05:00
|
|
|
// contained in the face variable and is capable of drawing ASCII characters.
|
2017-05-09 09:27:55 -05:00
|
|
|
//
|
|
|
|
// Here we create a Text object which can draw ASCII and Katakana characters:
|
2017-05-10 10:56:09 -05:00
|
|
|
// txt := text.New(0, text.NewAtlas(face, text.ASCII, text.RangeTable(unicode.Katakana)))
|
2017-05-09 09:27:55 -05:00
|
|
|
//
|
|
|
|
// Similarly to IMDraw, Text functions as a buffer. It implements io.Writer interface, so writing
|
|
|
|
// text to it is really simple:
|
|
|
|
// fmt.Print(txt, "Hello, world!")
|
|
|
|
//
|
2017-05-09 09:36:59 -05:00
|
|
|
// Newlines, tabs and carriage returns are supported.
|
|
|
|
//
|
2017-05-09 09:27:55 -05:00
|
|
|
// Finally, if we want the written text to show up on some other Target, we can draw it:
|
|
|
|
// txt.Draw(target)
|
|
|
|
//
|
|
|
|
// Text exports two important fields: Orig and Dot. Dot is the position where the next character
|
|
|
|
// will be written. Dot is automatically moved when writing to a Text object, but you can also
|
|
|
|
// manipulate it manually. Orig specifies the text origin, usually the top-left dot position. Dot is
|
2018-01-19 16:00:33 -06:00
|
|
|
// always aligned to Orig when writing newlines. The Clear method resets the Dot to Orig.
|
2017-05-02 15:46:51 -05:00
|
|
|
type Text struct {
|
2017-05-09 09:27:55 -05:00
|
|
|
// Orig specifies the text origin, usually the top-left dot position. Dot is always aligned
|
|
|
|
// to Orig when writing newlines.
|
2017-05-03 13:56:06 -05:00
|
|
|
Orig pixel.Vec
|
2017-05-09 09:27:55 -05:00
|
|
|
|
|
|
|
// Dot is the position where the next character will be written. Dot is automatically moved
|
|
|
|
// when writing to a Text object, but you can also manipulate it manually
|
|
|
|
Dot pixel.Vec
|
2017-05-02 15:46:51 -05:00
|
|
|
|
2017-05-18 16:59:42 -05:00
|
|
|
// Color is the color of the text that is to be written. Defaults to white.
|
|
|
|
Color color.Color
|
|
|
|
|
|
|
|
// LineHeight is the vertical distance between two lines of text.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// txt.LineHeight = 1.5 * txt.Atlas().LineHeight()
|
|
|
|
LineHeight float64
|
|
|
|
|
|
|
|
// TabWidth is the horizontal tab width. Tab characters will align to the multiples of this
|
|
|
|
// width.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// txt.TabWidth = 8 * txt.Atlas().Glyph(' ').Advance
|
|
|
|
TabWidth float64
|
2017-05-03 14:04:18 -05:00
|
|
|
|
2017-05-18 16:59:42 -05:00
|
|
|
atlas *Atlas
|
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
|
2020-08-11 15:16:16 -05:00
|
|
|
anchor pixel.Anchor
|
2017-05-02 15:46:51 -05:00
|
|
|
}
|
|
|
|
|
2017-05-10 10:56:09 -05:00
|
|
|
// New creates a new Text capable of drawing runes contained in the provided Atlas. Orig and Dot
|
|
|
|
// will be initially set to orig.
|
2017-05-09 09:27:55 -05:00
|
|
|
//
|
|
|
|
// Here we create a Text capable of drawing ASCII characters using the Go Regular font.
|
|
|
|
// ttf, err := truetype.Parse(goregular.TTF)
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// face := truetype.NewFace(ttf, &truetype.Options{
|
|
|
|
// Size: 14,
|
|
|
|
// })
|
2017-05-10 10:56:09 -05:00
|
|
|
// txt := text.New(orig, text.NewAtlas(face, text.ASCII))
|
|
|
|
func New(orig pixel.Vec, atlas *Atlas) *Text {
|
2017-05-02 15:46:51 -05:00
|
|
|
txt := &Text{
|
2017-05-10 10:56:09 -05:00
|
|
|
Orig: orig,
|
|
|
|
Dot: orig,
|
2017-05-18 16:59:42 -05:00
|
|
|
Color: pixel.Alpha(1),
|
|
|
|
LineHeight: atlas.LineHeight(),
|
|
|
|
TabWidth: atlas.Glyph(' ').Advance * 4,
|
2017-05-03 14:04:18 -05:00
|
|
|
atlas: atlas,
|
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
|
2021-10-01 13:31:18 -05:00
|
|
|
txt.transD.Cached = true
|
2017-05-02 15:46:51 -05:00
|
|
|
|
|
|
|
txt.Clear()
|
|
|
|
|
|
|
|
return txt
|
|
|
|
}
|
|
|
|
|
2017-05-09 09:27:55 -05:00
|
|
|
// Atlas returns the underlying Text's Atlas containing all of the pre-drawn glyphs. The Atlas is
|
|
|
|
// also useful for getting values such as the recommended line height.
|
2017-05-03 16:55:44 -05:00
|
|
|
func (txt *Text) Atlas() *Atlas {
|
|
|
|
return txt.atlas
|
|
|
|
}
|
|
|
|
|
2017-05-09 09:27:55 -05:00
|
|
|
// Bounds returns the bounding box of the text currently written to the Text excluding whitespace.
|
|
|
|
//
|
|
|
|
// If the Text is empty, a zero rectangle is returned.
|
2017-05-05 09:13:26 -05:00
|
|
|
func (txt *Text) Bounds() pixel.Rect {
|
|
|
|
return txt.bounds
|
2017-05-03 14:48:05 -05:00
|
|
|
}
|
|
|
|
|
2017-05-09 09:27:55 -05:00
|
|
|
// BoundsOf returns the bounding box of s if it was to be written to the Text right now.
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-11 15:16:16 -05:00
|
|
|
// AlignedTo returns the text moved by the given anchor.
|
|
|
|
func (txt *Text) AlignedTo(anchor pixel.Anchor) *Text {
|
|
|
|
txt.anchor = anchor
|
|
|
|
return txt
|
|
|
|
}
|
|
|
|
|
2018-01-19 16:00:33 -06:00
|
|
|
// Clear removes all written text from the Text. The Dot field is reset to Orig.
|
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
|
2018-01-19 15:20:54 -06:00
|
|
|
txt.Dot = txt.Orig
|
2017-05-02 15:46:51 -05:00
|
|
|
}
|
|
|
|
|
2017-05-09 09:27:55 -05:00
|
|
|
// Write writes a slice of bytes to the Text. This method never fails, always returns len(p), nil.
|
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-09 09:27:55 -05:00
|
|
|
// WriteString writes a string to the Text. This method never fails, always returns len(s), nil.
|
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-09 09:27:55 -05:00
|
|
|
// WriteByte writes a byte to the Text. This method never fails, always returns nil.
|
|
|
|
//
|
|
|
|
// Writing a multi-byte rune byte-by-byte is perfectly supported.
|
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-09 09:27:55 -05:00
|
|
|
// WriteRune writes a rune to the Text. This method never fails, always returns utf8.RuneLen(r), nil.
|
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-09 09:27:55 -05:00
|
|
|
// Draw draws all text written to the Text to the provided Target. The text is transformed by the
|
2017-05-21 11:23:51 -05:00
|
|
|
// provided Matrix.
|
|
|
|
//
|
|
|
|
// This method is equivalent to calling DrawColorMask with nil color mask.
|
|
|
|
//
|
|
|
|
// If there's a lot of text written to the Text, changing a matrix or a color mask often might hurt
|
|
|
|
// performance. Consider using your Target's SetMatrix or SetColorMask methods if available.
|
|
|
|
func (txt *Text) Draw(t pixel.Target, matrix pixel.Matrix) {
|
|
|
|
txt.DrawColorMask(t, matrix, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DrawColorMask draws all text written to the Text to the provided Target. The text is transformed
|
|
|
|
// by the provided Matrix and masked by the provided color mask.
|
|
|
|
//
|
|
|
|
// If there's a lot of text written to the Text, changing a matrix or a color mask often might hurt
|
|
|
|
// performance. Consider using your Target's SetMatrix or SetColorMask methods if available.
|
|
|
|
func (txt *Text) DrawColorMask(t pixel.Target, matrix pixel.Matrix, mask color.Color) {
|
|
|
|
if matrix != txt.mat {
|
|
|
|
txt.mat = matrix
|
|
|
|
txt.dirty = true
|
|
|
|
}
|
2020-08-11 15:16:16 -05:00
|
|
|
|
2020-08-22 08:31:07 -05:00
|
|
|
offset := txt.Orig.Sub(txt.Bounds().Max.Add(txt.Bounds().AnchorPos(txt.anchor.Opposite())))
|
2020-08-11 15:16:16 -05:00
|
|
|
txt.mat = pixel.IM.Moved(offset).Chained(txt.mat)
|
|
|
|
|
2017-05-21 11:23:51 -05:00
|
|
|
if mask == nil {
|
|
|
|
mask = pixel.Alpha(1)
|
|
|
|
}
|
|
|
|
rgba := pixel.ToRGBA(mask)
|
|
|
|
if rgba != txt.col {
|
|
|
|
txt.col = rgba
|
|
|
|
txt.dirty = true
|
|
|
|
}
|
|
|
|
|
2017-05-09 08:26:50 -05:00
|
|
|
if txt.dirty {
|
|
|
|
txt.trans.SetLen(txt.tris.Len())
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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':
|
2017-05-21 12:25:06 -05:00
|
|
|
dot.X = txt.Orig.X
|
|
|
|
dot.Y -= txt.LineHeight
|
2017-05-07 14:08:10 -05:00
|
|
|
case '\r':
|
2017-05-21 12:25:06 -05:00
|
|
|
dot.X = txt.Orig.X
|
2017-05-07 14:08:10 -05:00
|
|
|
case '\t':
|
2017-05-21 12:25:06 -05:00
|
|
|
rem := math.Mod(dot.X-txt.Orig.X, txt.TabWidth)
|
2017-05-18 16:59:42 -05:00
|
|
|
rem = math.Mod(rem, rem+txt.TabWidth)
|
2017-05-07 14:08:10 -05:00
|
|
|
if rem == 0 {
|
2017-05-18 16:59:42 -05:00
|
|
|
rem = txt.TabWidth
|
2017-05-07 14:08:10 -05:00
|
|
|
}
|
2017-05-21 12:25:06 -05:00
|
|
|
dot.X += rem
|
2017-05-07 14:08:10 -05:00
|
|
|
default:
|
|
|
|
return dot, false
|
|
|
|
}
|
|
|
|
return dot, true
|
|
|
|
}
|
|
|
|
|
2017-05-07 13:59:56 -05:00
|
|
|
func (txt *Text) drawBuf() {
|
2017-05-18 16:59:42 -05:00
|
|
|
if !utf8.FullRune(txt.buf) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rgba := pixel.ToRGBA(txt.Color)
|
|
|
|
for i := range txt.glyph {
|
|
|
|
txt.glyph[i].Color = rgba
|
|
|
|
}
|
|
|
|
|
2017-05-07 13:59:56 -05:00
|
|
|
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-21 12:25:06 -05:00
|
|
|
rv := [...]pixel.Vec{
|
|
|
|
{X: rect.Min.X, Y: rect.Min.Y},
|
|
|
|
{X: rect.Max.X, Y: rect.Min.Y},
|
|
|
|
{X: rect.Max.X, Y: rect.Max.Y},
|
|
|
|
{X: rect.Min.X, Y: rect.Max.Y},
|
2017-05-05 09:42:40 -05:00
|
|
|
}
|
2017-05-02 15:46:51 -05:00
|
|
|
|
2017-05-21 12:25:06 -05:00
|
|
|
fv := [...]pixel.Vec{
|
|
|
|
{X: frame.Min.X, Y: frame.Min.Y},
|
|
|
|
{X: frame.Max.X, Y: frame.Min.Y},
|
|
|
|
{X: frame.Max.X, Y: frame.Max.Y},
|
|
|
|
{X: frame.Min.X, Y: frame.Max.Y},
|
2017-05-07 13:59:56 -05:00
|
|
|
}
|
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
|
|
|
}
|