Add Atlas7x13, Clear now sets Dot field to Orig

Remove unused f2i function
This commit is contained in:
Peter Hellberg 2018-01-19 22:20:54 +01:00
parent 2dff742a8b
commit ad738cddf8
4 changed files with 54 additions and 4 deletions

View File

@ -9,9 +9,13 @@ import (
"github.com/faiface/pixel" "github.com/faiface/pixel"
"golang.org/x/image/font" "golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed" "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. // Glyph describes one glyph in an Atlas.
type Glyph struct { type Glyph struct {
Dot pixel.Vec Dot pixel.Vec
@ -171,6 +175,10 @@ func (a *Atlas) DrawRune(prevR, r rune, dot pixel.Vec) (rect, frame, bounds pixe
return rect, glyph.Frame, bounds, dot return rect, glyph.Frame, bounds, dot
} }
func new7x13Atlas(runeSets ...[]rune) *Atlas {
return NewAtlas(basicfont.Face7x13, runeSets...)
}
type fixedGlyph struct { type fixedGlyph struct {
dot fixed.Point26_6 dot fixed.Point26_6
frame fixed.Rectangle26_6 frame fixed.Rectangle26_6
@ -243,7 +251,3 @@ func makeMapping(face font.Face, runes []rune, padding, width fixed.Int26_6) (ma
func i2f(i fixed.Int26_6) float64 { func i2f(i fixed.Int26_6) float64 {
return float64(i) / (1 << 6) return float64(i) / (1 << 6)
} }
func f2i(f float64) fixed.Int26_6 {
return fixed.Int26_6(f * (1 << 6))
}

20
text/atlas_test.go Normal file
View File

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

View File

@ -17,6 +17,7 @@ func init() {
for i := range ASCII { for i := range ASCII {
ASCII[i] = rune(32 + i) ASCII[i] = rune(32 + i)
} }
Atlas7x13 = new7x13Atlas(ASCII)
} }
// RangeTable takes a *unicode.RangeTable and generates a set of runes contained within that // RangeTable takes a *unicode.RangeTable and generates a set of runes contained within that
@ -189,6 +190,7 @@ func (txt *Text) Clear() {
txt.bounds = pixel.Rect{} txt.bounds = pixel.Rect{}
txt.tris.SetLen(0) txt.tris.SetLen(0)
txt.dirty = true 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. // 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" "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) { func BenchmarkNewAtlas(b *testing.B) {
runeSets := []struct { runeSets := []struct {
name string 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)
}