text_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2018 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package text_test
  15. import (
  16. "image"
  17. "image/color"
  18. "testing"
  19. "github.com/hajimehoshi/bitmapfont/v3"
  20. "golang.org/x/image/font"
  21. "golang.org/x/image/math/fixed"
  22. "github.com/hajimehoshi/ebiten/v2"
  23. t "github.com/hajimehoshi/ebiten/v2/internal/testing"
  24. "github.com/hajimehoshi/ebiten/v2/text"
  25. )
  26. func TestMain(m *testing.M) {
  27. t.MainWithRunLoop(m)
  28. }
  29. func TestTextColor(t *testing.T) {
  30. clr := color.RGBA{R: 0x80, G: 0x80, B: 0x80, A: 0x80}
  31. img := ebiten.NewImage(30, 30)
  32. text.Draw(img, "Hello", bitmapfont.Face, 12, 12, clr)
  33. w, h := img.Bounds().Dx(), img.Bounds().Dy()
  34. allTransparent := true
  35. for j := 0; j < h; j++ {
  36. for i := 0; i < w; i++ {
  37. got := img.At(i, j)
  38. want1 := color.RGBA{R: 0x80, G: 0x80, B: 0x80, A: 0x80}
  39. want2 := color.RGBA{}
  40. if got != want1 && got != want2 {
  41. t.Errorf("img At(%d, %d): got %v; want %v or %v", i, j, got, want1, want2)
  42. }
  43. if got == want1 {
  44. allTransparent = false
  45. }
  46. }
  47. }
  48. if allTransparent {
  49. t.Fail()
  50. }
  51. }
  52. const testFaceSize = 6
  53. type testFace struct{}
  54. func (f *testFace) Glyph(dot fixed.Point26_6, r rune) (dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool) {
  55. dr = image.Rect(0, 0, testFaceSize, testFaceSize)
  56. a := image.NewAlpha(dr)
  57. switch r {
  58. case 'a':
  59. for j := dr.Min.Y; j < dr.Max.Y; j++ {
  60. for i := dr.Min.X; i < dr.Max.X; i++ {
  61. a.SetAlpha(i, j, color.Alpha{A: 0x80})
  62. }
  63. }
  64. case 'b':
  65. for j := dr.Min.Y; j < dr.Max.Y; j++ {
  66. for i := dr.Min.X; i < dr.Max.X; i++ {
  67. a.SetAlpha(i, j, color.Alpha{A: 0xff})
  68. }
  69. }
  70. }
  71. mask = a
  72. advance = fixed.I(testFaceSize)
  73. ok = true
  74. return
  75. }
  76. func (f *testFace) GlyphBounds(r rune) (bounds fixed.Rectangle26_6, advance fixed.Int26_6, ok bool) {
  77. bounds = fixed.R(0, 0, testFaceSize, testFaceSize)
  78. advance = fixed.I(testFaceSize)
  79. ok = true
  80. return
  81. }
  82. func (f *testFace) GlyphAdvance(r rune) (advance fixed.Int26_6, ok bool) {
  83. return fixed.I(testFaceSize), true
  84. }
  85. func (f *testFace) Kern(r0, r1 rune) fixed.Int26_6 {
  86. if r1 == 'b' {
  87. return fixed.I(-testFaceSize)
  88. }
  89. return 0
  90. }
  91. func (f *testFace) Close() error {
  92. return nil
  93. }
  94. func (f *testFace) Metrics() font.Metrics {
  95. return font.Metrics{
  96. Height: fixed.I(testFaceSize),
  97. Ascent: 0,
  98. Descent: fixed.I(testFaceSize),
  99. XHeight: 0,
  100. CapHeight: fixed.I(testFaceSize),
  101. CaretSlope: image.Pt(0, 1),
  102. }
  103. }
  104. // Issue #1378
  105. func TestNegativeKern(t *testing.T) {
  106. f := &testFace{}
  107. dst := ebiten.NewImage(testFaceSize*2, testFaceSize)
  108. // With testFace, 'b' is rendered at the previous position as 0xff.
  109. // 'a' is rendered at the current position as 0x80.
  110. text.Draw(dst, "ab", f, 0, 0, color.White)
  111. for j := 0; j < testFaceSize; j++ {
  112. for i := 0; i < testFaceSize; i++ {
  113. got := dst.At(i, j)
  114. want := color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
  115. if got != want {
  116. t.Errorf("At(%d, %d): got: %v, want: %v", i, j, got, want)
  117. }
  118. }
  119. }
  120. // The glyph 'a' should be treated correctly.
  121. text.Draw(dst, "a", f, testFaceSize, 0, color.White)
  122. for j := 0; j < testFaceSize; j++ {
  123. for i := testFaceSize; i < testFaceSize*2; i++ {
  124. got := dst.At(i, j)
  125. want := color.RGBA{R: 0x80, G: 0x80, B: 0x80, A: 0x80}
  126. if got != want {
  127. t.Errorf("At(%d, %d): got: %v, want: %v", i, j, got, want)
  128. }
  129. }
  130. }
  131. }