atlas.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package text
  5. import (
  6. "bufio"
  7. "fmt"
  8. "github.com/g3n/engine/math32"
  9. "image"
  10. "image/png"
  11. "os"
  12. "unicode/utf8"
  13. )
  14. type CharInfo struct {
  15. X int // Position X in pixels in the sheet image from left to right
  16. Y int // Position Y in pixels in the sheet image from top to bottom
  17. Width int // Char width in pixels
  18. Height int // Char heigh in pixels
  19. // Normalized position of char in the image
  20. OffsetX float32
  21. OffsetY float32
  22. RepeatX float32
  23. RepeatY float32
  24. }
  25. type Atlas struct {
  26. Chars []CharInfo
  27. Image *image.RGBA
  28. Height int // Recommended vertical space between two lines of text
  29. Ascent int // Distance from the top of a line to its base line
  30. Descent int // Distance from the bottom of a line to its baseline
  31. }
  32. func NewAtlas(font *Font, first, last rune) *Atlas {
  33. a := new(Atlas)
  34. a.Chars = make([]CharInfo, last+1)
  35. // Get font metrics
  36. metrics := font.Metrics()
  37. a.Height = int(metrics.Height >> 6)
  38. a.Ascent = int(metrics.Ascent >> 6)
  39. a.Descent = int(metrics.Descent >> 6)
  40. const cols = 16
  41. col := 0
  42. encoded := make([]byte, 4)
  43. line := []byte{}
  44. lines := ""
  45. maxWidth := 0
  46. lastX := 0
  47. lastY := a.Descent
  48. nlines := 0
  49. for code := first; code <= last; code++ {
  50. // Encodes rune into UTF8 and appends to current line
  51. count := utf8.EncodeRune(encoded, code)
  52. line = append(line, encoded[:count]...)
  53. // Measure current line
  54. width, _ := font.MeasureText(string(line))
  55. // Sets current code fields
  56. cinfo := &a.Chars[code]
  57. cinfo.X = lastX
  58. cinfo.Y = lastY
  59. cinfo.Width = width - lastX - 1
  60. cinfo.Height += a.Height
  61. lastX = width
  62. fmt.Printf("%c: cinfo:%+v\n", code, cinfo)
  63. // Checks end of the current line
  64. col++
  65. if col >= cols || code == last {
  66. nlines++
  67. lines += string(line) + "\n"
  68. line = []byte{}
  69. // Checks max width
  70. if width > maxWidth {
  71. maxWidth = width
  72. }
  73. if code == last {
  74. break
  75. }
  76. col = 0
  77. lastX = 0
  78. lastY += a.Height
  79. }
  80. }
  81. height := (nlines * a.Height) + a.Descent
  82. // Draw atlas image
  83. canvas := NewCanvas(maxWidth, height, &math32.Color4{1, 1, 1, 1})
  84. canvas.DrawText(0, 0, lines, font)
  85. a.Image = canvas.RGBA
  86. // Calculate normalized char positions in the image
  87. fWidth := float32(maxWidth)
  88. fHeight := float32(height)
  89. for i := 0; i < len(a.Chars); i++ {
  90. char := &a.Chars[i]
  91. char.OffsetX = float32(char.X) / fWidth
  92. char.OffsetY = float32(char.Y) / fHeight
  93. char.RepeatX = float32(char.Width) / fWidth
  94. char.RepeatY = float32(char.Height) / fHeight
  95. }
  96. a.SavePNG("atlas.png")
  97. return a
  98. }
  99. // SavePNG saves the current atlas image as a PNG image file
  100. func (a *Atlas) SavePNG(filename string) error {
  101. // Save that RGBA image to disk.
  102. outFile, err := os.Create(filename)
  103. if err != nil {
  104. return err
  105. }
  106. defer outFile.Close()
  107. b := bufio.NewWriter(outFile)
  108. err = png.Encode(b, a.Image)
  109. if err != nil {
  110. return err
  111. }
  112. err = b.Flush()
  113. if err != nil {
  114. return err
  115. }
  116. return nil
  117. }