main.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 main
  15. import (
  16. "bytes"
  17. "log"
  18. "golang.org/x/image/font/gofont/goregular"
  19. "github.com/hajimehoshi/ebiten/v2"
  20. "github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
  21. "github.com/hajimehoshi/ebiten/v2/text/v2"
  22. )
  23. const (
  24. screenWidth = 640
  25. screenHeight = 480
  26. )
  27. var (
  28. goRegularFaceSource *text.GoTextFaceSource
  29. mplusFaceSource *text.GoTextFaceSource
  30. )
  31. func init() {
  32. s, err := text.NewGoTextFaceSource(bytes.NewReader(fonts.MPlus1pRegular_ttf))
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. mplusFaceSource = s
  37. }
  38. func init() {
  39. s, err := text.NewGoTextFaceSource(bytes.NewReader(goregular.TTF))
  40. if err != nil {
  41. log.Fatal(err)
  42. }
  43. goRegularFaceSource = s
  44. }
  45. type Game struct {
  46. face text.Face
  47. }
  48. func (g *Game) Update() error {
  49. if g.face == nil {
  50. // goregular.TTF is used primarily. If a glyph is not found in this font, the second font is used.
  51. // Use text.LimitedFace to limit the glyphs.
  52. en := text.NewLimitedFace(&text.GoTextFace{
  53. Source: goRegularFaceSource,
  54. Size: 24,
  55. })
  56. // Limit the glyphs for ASCII and Latin-1 characters for this face.
  57. // This means that, for example, '…' (U+2026) is not rendered by this face.
  58. en.AddUnicodeRange('\u0020', '\u00ff')
  59. // M+ Font is the second font.
  60. // Use a relatively big size to see different-sized faces are well mixed.
  61. ja := &text.GoTextFace{
  62. Source: mplusFaceSource,
  63. Size: 32,
  64. }
  65. f, err := text.NewMultiFace(en, ja)
  66. if err != nil {
  67. return err
  68. }
  69. g.face = f
  70. }
  71. return nil
  72. }
  73. func (g *Game) Draw(screen *ebiten.Image) {
  74. op := &text.DrawOptions{}
  75. op.GeoM.Translate(20, 20)
  76. op.LineSpacing = 48
  77. text.Draw(screen, "HelloこんにちはWorld世界\n日本語とEnglish…", g.face, op)
  78. }
  79. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  80. return screenWidth, screenHeight
  81. }
  82. func main() {
  83. ebiten.SetWindowSize(screenWidth, screenHeight)
  84. ebiten.SetWindowTitle("Mixed Font Faces (Ebitengine Demo)")
  85. if err := ebiten.RunGame(&Game{}); err != nil {
  86. log.Fatal(err)
  87. }
  88. }