From ad738cddf8a0dd09fd19d266f2268e1c3437b0f2 Mon Sep 17 00:00:00 2001 From: Peter Hellberg Date: Fri, 19 Jan 2018 22:20:54 +0100 Subject: [PATCH 1/4] Add Atlas7x13, Clear now sets Dot field to Orig Remove unused f2i function --- text/atlas.go | 12 ++++++++---- text/atlas_test.go | 20 ++++++++++++++++++++ text/text.go | 2 ++ text/text_test.go | 24 ++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 text/atlas_test.go diff --git a/text/atlas.go b/text/atlas.go index cf9231c..67000cf 100644 --- a/text/atlas.go +++ b/text/atlas.go @@ -9,9 +9,13 @@ import ( "github.com/faiface/pixel" "golang.org/x/image/font" + "golang.org/x/image/font/basicfont" "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 @@ -171,6 +175,10 @@ func (a *Atlas) DrawRune(prevR, r rune, dot pixel.Vec) (rect, frame, bounds pixe return rect, glyph.Frame, bounds, dot } +func new7x13Atlas(runeSets ...[]rune) *Atlas { + return NewAtlas(basicfont.Face7x13, runeSets...) +} + type fixedGlyph struct { dot fixed.Point26_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 { return float64(i) / (1 << 6) } - -func f2i(f float64) fixed.Int26_6 { - return fixed.Int26_6(f * (1 << 6)) -} diff --git a/text/atlas_test.go b/text/atlas_test.go new file mode 100644 index 0000000..6d79409 --- /dev/null +++ b/text/atlas_test.go @@ -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) + } + } + } +} diff --git a/text/text.go b/text/text.go index ea6d194..fb66070 100644 --- a/text/text.go +++ b/text/text.go @@ -17,6 +17,7 @@ func init() { for i := range ASCII { ASCII[i] = rune(32 + i) } + Atlas7x13 = new7x13Atlas(ASCII) } // 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.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. diff --git a/text/text_test.go b/text/text_test.go index 095a53a..d3184a5 100644 --- a/text/text_test.go +++ b/text/text_test.go @@ -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) +} From 8d8d5cc9dbdde363b30872418c453060310101d8 Mon Sep 17 00:00:00 2001 From: Peter Hellberg Date: Fri, 19 Jan 2018 23:00:33 +0100 Subject: [PATCH 2/4] Document that Clear resets the Dot to Orig Remove note on how to reset Dot to the Orig --- text/text.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/text/text.go b/text/text.go index fb66070..04354a6 100644 --- a/text/text.go +++ b/text/text.go @@ -61,10 +61,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. @@ -184,7 +181,7 @@ 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{} From b157c890d69e7b9111af59fd7644a3411d7a8cd3 Mon Sep 17 00:00:00 2001 From: Peter Hellberg Date: Fri, 19 Jan 2018 23:08:06 +0100 Subject: [PATCH 3/4] Remove new7x13Atlas function --- text/atlas.go | 5 ----- text/text.go | 3 ++- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/text/atlas.go b/text/atlas.go index 67000cf..b7ddf43 100644 --- a/text/atlas.go +++ b/text/atlas.go @@ -9,7 +9,6 @@ import ( "github.com/faiface/pixel" "golang.org/x/image/font" - "golang.org/x/image/font/basicfont" "golang.org/x/image/math/fixed" ) @@ -175,10 +174,6 @@ func (a *Atlas) DrawRune(prevR, r rune, dot pixel.Vec) (rect, frame, bounds pixe return rect, glyph.Frame, bounds, dot } -func new7x13Atlas(runeSets ...[]rune) *Atlas { - return NewAtlas(basicfont.Face7x13, runeSets...) -} - type fixedGlyph struct { dot fixed.Point26_6 frame fixed.Rectangle26_6 diff --git a/text/text.go b/text/text.go index 04354a6..1247a8d 100644 --- a/text/text.go +++ b/text/text.go @@ -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,7 +18,7 @@ func init() { for i := range ASCII { ASCII[i] = rune(32 + i) } - Atlas7x13 = new7x13Atlas(ASCII) + Atlas7x13 = NewAtlas(basicfont.Face7x13, ASCII) } // RangeTable takes a *unicode.RangeTable and generates a set of runes contained within that From c871238e9f4bf98d00ac2fa189f4235900547d0a Mon Sep 17 00:00:00 2001 From: Peter Hellberg Date: Fri, 19 Jan 2018 23:09:48 +0100 Subject: [PATCH 4/4] Change package to text_test --- text/atlas_test.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/text/atlas_test.go b/text/atlas_test.go index 6d79409..2cb99bb 100644 --- a/text/atlas_test.go +++ b/text/atlas_test.go @@ -1,18 +1,22 @@ -package text +package text_test -import "testing" +import ( + "testing" + + "github.com/faiface/pixel/text" +) func TestAtlas7x13(t *testing.T) { - if Atlas7x13 == nil { + if text.Atlas7x13 == nil { t.Fatalf("Atlas7x13 is nil") } for _, tt := range []struct { runes []rune want bool - }{{ASCII, true}, {[]rune("ÅÄÖ"), false}} { + }{{text.ASCII, true}, {[]rune("ÅÄÖ"), false}} { for _, r := range tt.runes { - if got := Atlas7x13.Contains(r); got != tt.want { + if got := text.Atlas7x13.Contains(r); got != tt.want { t.Fatalf("Atlas7x13.Contains('%s') = %v, want %v", string(r), got, tt.want) } }