Merge pull request #90 from peterhellberg/text-reset-and-default-atlas

Add Atlas7x13, Clear now sets Dot field to Orig
This commit is contained in:
Michal Štrba 2018-01-19 23:29:34 +01:00 committed by GitHub
commit 4498635b26
4 changed files with 56 additions and 9 deletions

View File

@ -12,6 +12,9 @@ import (
"golang.org/x/image/math/fixed"
)
// Atlas7x13 is an Atlas using basicfont.Face7x13 with the ASCII rune set
var Atlas7x13 *Atlas
// Glyph describes one glyph in an Atlas.
type Glyph struct {
Dot pixel.Vec
@ -243,7 +246,3 @@ func makeMapping(face font.Face, runes []rune, padding, width fixed.Int26_6) (ma
func i2f(i fixed.Int26_6) float64 {
return float64(i) / (1 << 6)
}
func f2i(f float64) fixed.Int26_6 {
return fixed.Int26_6(f * (1 << 6))
}

24
text/atlas_test.go Normal file
View File

@ -0,0 +1,24 @@
package text_test
import (
"testing"
"github.com/faiface/pixel/text"
)
func TestAtlas7x13(t *testing.T) {
if text.Atlas7x13 == nil {
t.Fatalf("Atlas7x13 is nil")
}
for _, tt := range []struct {
runes []rune
want bool
}{{text.ASCII, true}, {[]rune("ÅÄÖ"), false}} {
for _, r := range tt.runes {
if got := text.Atlas7x13.Contains(r); got != tt.want {
t.Fatalf("Atlas7x13.Contains('%s') = %v, want %v", string(r), got, tt.want)
}
}
}
}

View File

@ -7,6 +7,7 @@ import (
"unicode/utf8"
"github.com/faiface/pixel"
"golang.org/x/image/font/basicfont"
)
// ASCII is a set of all ASCII runes. These runes are codepoints from 32 to 127 inclusive.
@ -17,6 +18,7 @@ func init() {
for i := range ASCII {
ASCII[i] = rune(32 + i)
}
Atlas7x13 = NewAtlas(basicfont.Face7x13, ASCII)
}
// RangeTable takes a *unicode.RangeTable and generates a set of runes contained within that
@ -60,10 +62,7 @@ func RangeTable(table *unicode.RangeTable) []rune {
// 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
// always aligned to Orig when writing newlines.
//
// To reset the Dot to the Orig, just assign it:
// txt.Dot = txt.Orig
// always aligned to Orig when writing newlines. The Clear method resets the Dot to Orig.
type Text struct {
// Orig specifies the text origin, usually the top-left dot position. Dot is always aligned
// to Orig when writing newlines.
@ -183,12 +182,13 @@ func (txt *Text) BoundsOf(s string) pixel.Rect {
return bounds
}
// Clear removes all written text from the Text.
// Clear removes all written text from the Text. The Dot field is reset to Orig.
func (txt *Text) Clear() {
txt.prevR = -1
txt.bounds = pixel.Rect{}
txt.tris.SetLen(0)
txt.dirty = true
txt.Dot = txt.Orig
}
// Write writes a slice of bytes to the Text. This method never fails, always returns len(p), nil.

View File

@ -14,6 +14,26 @@ import (
"github.com/golang/freetype/truetype"
)
func TestClear(t *testing.T) {
txt := text.New(pixel.ZV, text.Atlas7x13)
if got, want := txt.Dot, pixel.ZV; !eqVectors(got, want) {
t.Fatalf("txt.Dot = %v, want %v", got, want)
}
fmt.Fprint(txt, "Test\nClear")
if got, want := txt.Dot, pixel.V(35, -13); !eqVectors(got, want) {
t.Fatalf("txt.Dot = %v, want %v", got, want)
}
txt.Clear()
if got, want := txt.Dot, pixel.ZV; !eqVectors(got, want) {
t.Fatalf("txt.Dot = %v, want %v", got, want)
}
}
func BenchmarkNewAtlas(b *testing.B) {
runeSets := []struct {
name string
@ -61,3 +81,7 @@ func BenchmarkTextWrite(b *testing.B) {
})
}
}
func eqVectors(a, b pixel.Vec) bool {
return (a.X == b.X && a.Y == b.Y)
}