main.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. _ "embed"
  18. "fmt"
  19. "log"
  20. "github.com/hajimehoshi/ebiten/v2"
  21. "github.com/hajimehoshi/ebiten/v2/inpututil"
  22. "github.com/hajimehoshi/ebiten/v2/text/v2"
  23. )
  24. const (
  25. screenWidth = 640
  26. screenHeight = 480
  27. )
  28. //go:embed RobotoFlex.ttf
  29. var robotoFlexRegular []byte
  30. var robotoFlexFaceSource *text.GoTextFaceSource
  31. func init() {
  32. s, err := text.NewGoTextFaceSource(bytes.NewReader(robotoFlexRegular))
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. robotoFlexFaceSource = s
  37. }
  38. type Game struct {
  39. // wght represents 'Weight'.
  40. // https://learn.microsoft.com/en-us/typography/opentype/spec/dvaraxistag_wght
  41. wght float32
  42. // wdth represents 'Width'.
  43. // https://learn.microsoft.com/en-us/typography/opentype/spec/dvaraxistag_wdth
  44. wdth float32
  45. // slnt represents 'Slant'.
  46. // https://learn.microsoft.com/en-us/typography/opentype/spec/dvaraxistag_slnt
  47. slnt float32
  48. }
  49. func NewGame() *Game {
  50. return &Game{
  51. wght: 400,
  52. wdth: 100,
  53. slnt: 0,
  54. }
  55. }
  56. const (
  57. minWght = 100
  58. maxWght = 1000
  59. minWdth = 30
  60. maxWdth = 150
  61. minSlnt = -10
  62. maxSlnt = 0
  63. )
  64. func (g *Game) Update() error {
  65. if inpututil.IsKeyJustPressed(ebiten.KeyQ) {
  66. if g.wght > minWght {
  67. g.wght -= 100
  68. }
  69. }
  70. if inpututil.IsKeyJustPressed(ebiten.KeyW) {
  71. if g.wght < maxWght {
  72. g.wght += 100
  73. }
  74. }
  75. if inpututil.IsKeyJustPressed(ebiten.KeyA) {
  76. if g.wdth > minWdth {
  77. g.wdth -= 10
  78. }
  79. }
  80. if inpututil.IsKeyJustPressed(ebiten.KeyS) {
  81. if g.wdth < maxWdth {
  82. g.wdth += 10
  83. }
  84. }
  85. if inpututil.IsKeyJustPressed(ebiten.KeyZ) {
  86. if g.slnt > minSlnt {
  87. g.slnt -= 1
  88. }
  89. }
  90. if inpututil.IsKeyJustPressed(ebiten.KeyX) {
  91. if g.slnt < maxSlnt {
  92. g.slnt += 1
  93. }
  94. }
  95. return nil
  96. }
  97. func (g *Game) Draw(screen *ebiten.Image) {
  98. // Draw the instruction.
  99. inst := fmt.Sprintf(`Press keys to adjust font variations.
  100. [Q, W]: wght (Weight): %0.0f [%d-%d]
  101. [A, S]: wdth (Width): %0.0f [%d-%d]
  102. [Z, X]: slnt (Slant): %0.0f [%d-%d]`, g.wght, minWght, maxWght, g.wdth, minWdth, maxWdth, g.slnt, minSlnt, maxSlnt)
  103. op := &text.DrawOptions{}
  104. op.GeoM.Translate(20, 20)
  105. op.LineSpacing = 30
  106. text.Draw(screen, inst, &text.GoTextFace{
  107. Source: robotoFlexFaceSource,
  108. Size: 20,
  109. }, op)
  110. // Draw the sample text.
  111. const sampleText = `The quick brown fox jumps
  112. over the lazy dog.`
  113. op = &text.DrawOptions{}
  114. op.GeoM.Translate(20, screenHeight/2)
  115. op.LineSpacing = 50
  116. f := &text.GoTextFace{
  117. Source: robotoFlexFaceSource,
  118. Size: 40,
  119. }
  120. f.SetVariation(text.MustParseTag("wght"), g.wght)
  121. f.SetVariation(text.MustParseTag("wdth"), g.wdth)
  122. f.SetVariation(text.MustParseTag("slnt"), g.slnt)
  123. text.Draw(screen, sampleText, f, op)
  124. }
  125. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  126. return screenWidth, screenHeight
  127. }
  128. func main() {
  129. ebiten.SetWindowSize(screenWidth, screenHeight)
  130. ebiten.SetWindowTitle("Font Variation (Ebitengine Demo)")
  131. if err := ebiten.RunGame(NewGame()); err != nil {
  132. log.Fatal(err)
  133. }
  134. }