1
0

gen.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2022 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. //go:build ignore
  15. package main
  16. import (
  17. "bufio"
  18. "fmt"
  19. "image"
  20. "image/color"
  21. "image/png"
  22. "os"
  23. "github.com/hajimehoshi/bitmapfont/v3"
  24. "golang.org/x/image/font"
  25. "golang.org/x/image/math/fixed"
  26. )
  27. func main() {
  28. if err := run(); err != nil {
  29. fmt.Fprintf(os.Stderr, "%v\n", err)
  30. }
  31. }
  32. func run() error {
  33. // These values are copied from an example in github.com/hajimehoshi/bitmapfont.
  34. const (
  35. charWidth = 6
  36. lineHeight = 16
  37. )
  38. var lines []string
  39. for j := 0; j < 8; j++ {
  40. var line string
  41. for i := 0; i < 32; i++ {
  42. line += string(rune(i + j*32))
  43. }
  44. lines = append(lines, line)
  45. }
  46. dst := image.NewRGBA(image.Rect(0, 0, charWidth*32, lineHeight*8))
  47. for i, clr := range []color.Color{color.RGBA{0, 0, 0, 0x80}, color.White} {
  48. var offsetX int
  49. var offsetY int
  50. if i == 0 {
  51. offsetX = 1
  52. offsetY = 1
  53. }
  54. d := font.Drawer{
  55. Dst: dst,
  56. Src: image.NewUniform(clr),
  57. Face: bitmapfont.Face,
  58. Dot: fixed.Point26_6{X: fixed.I(offsetX), Y: bitmapfont.Face.Metrics().Ascent + fixed.I(offsetY)},
  59. }
  60. for _, line := range lines {
  61. d.Dot.X = fixed.I(offsetX)
  62. d.DrawString(line)
  63. d.Dot.Y += fixed.I(lineHeight)
  64. }
  65. }
  66. f, err := os.Create("text.png")
  67. if err != nil {
  68. return err
  69. }
  70. defer f.Close()
  71. w := bufio.NewWriter(f)
  72. if err := png.Encode(w, dst); err != nil {
  73. return err
  74. }
  75. if err := w.Flush(); err != nil {
  76. return err
  77. }
  78. return nil
  79. }