multi_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2023 The Ebitengine 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. "bytes"
  17. "testing"
  18. "github.com/hajimehoshi/bitmapfont/v3"
  19. "golang.org/x/image/font/gofont/goregular"
  20. "github.com/hajimehoshi/ebiten/v2"
  21. "github.com/hajimehoshi/ebiten/v2/text/v2"
  22. )
  23. func TestMultiFace(t *testing.T) {
  24. faces := []text.Face{text.NewGoXFace(bitmapfont.Face)}
  25. f, err := text.NewMultiFace(faces...)
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. img := ebiten.NewImage(30, 30)
  30. text.Draw(img, "Hello", f, nil)
  31. // Confirm that the given slice doesn't cause crash.
  32. faces[0] = nil
  33. text.Draw(img, "World", f, nil)
  34. }
  35. func TestMultiFaceFallback(t *testing.T) {
  36. enFaceSource, err := text.NewGoTextFaceSource(bytes.NewReader(goregular.TTF))
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. enFace := &text.GoTextFace{
  41. Source: enFaceSource,
  42. Size: 10,
  43. }
  44. multiFace, err := text.NewMultiFace(enFace)
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. // If all the faces in a MultiFace doesn't have a glyph, the last face should be used.
  49. str := "あ"
  50. got := text.AppendGlyphs(nil, str, multiFace, nil)
  51. want := text.AppendGlyphs(nil, str, enFace, nil)
  52. if len(got) != len(want) {
  53. t.Errorf("got: %d, want: %d", len(got), len(want))
  54. }
  55. }