atlas.go 3.2 KB

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