FontAtlas.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package imgui
  2. // #include "FontAtlasWrapper.h"
  3. import "C"
  4. import "unsafe"
  5. // Alpha8Image represents a imgui backed 8-bit alpha value image.
  6. type Alpha8Image struct {
  7. Width, Height int
  8. Pixels unsafe.Pointer
  9. }
  10. // RGBA32Image represents a imgui backed 32-bit RGBA (8 bits per channel) value image.
  11. type RGBA32Image struct {
  12. Width, Height int
  13. Pixels unsafe.Pointer
  14. }
  15. // FontAtlas contains runtime data for multiple fonts,
  16. // bake multiple fonts into a single texture, TTF/OTF font loader
  17. type FontAtlas uintptr
  18. func (atlas FontAtlas) handle() C.IggFontAtlas {
  19. return C.IggFontAtlas(atlas)
  20. }
  21. func GlyphRangesAll() GlyphRanges {
  22. return GlyphRanges(C.iggGetGlyphRangesAll())
  23. }
  24. // GlyphRangesDefault describes Basic Latin, Extended Latin.
  25. func (atlas FontAtlas) GlyphRangesDefault() GlyphRanges {
  26. return GlyphRanges(C.iggGetGlyphRangesDefault(atlas.handle()))
  27. }
  28. // GlyphRangesKorean describes Default + Korean characters.
  29. func (atlas FontAtlas) GlyphRangesKorean() GlyphRanges {
  30. return GlyphRanges(C.iggGetGlyphRangesKorean(atlas.handle()))
  31. }
  32. // GlyphRangesJapanese describes Default + Hiragana, Katakana, Half-Width, Selection of 1946 Ideographs.
  33. func (atlas FontAtlas) GlyphRangesJapanese() GlyphRanges {
  34. return GlyphRanges(C.iggGetGlyphRangesJapanese(atlas.handle()))
  35. }
  36. // GlyphRangesChineseFull describes Default + Half-Width + Japanese Hiragana/Katakana + full set of about 21000 CJK
  37. // Unified Ideographs.
  38. func (atlas FontAtlas) GlyphRangesChineseFull() GlyphRanges {
  39. return GlyphRanges(C.iggGetGlyphRangesChineseFull(atlas.handle()))
  40. }
  41. // GlyphRangesChineseSimplifiedCommon describes Default + Half-Width + Japanese Hiragana/Katakana + set of 2500 CJK
  42. // Unified Ideographs for common simplified Chinese.
  43. func (atlas FontAtlas) GlyphRangesChineseSimplifiedCommon() GlyphRanges {
  44. return GlyphRanges(C.iggGetGlyphRangesChineseSimplifiedCommon(atlas.handle()))
  45. }
  46. // GlyphRangesCyrillic describes Default + about 400 Cyrillic characters.
  47. func (atlas FontAtlas) GlyphRangesCyrillic() GlyphRanges {
  48. return GlyphRanges(C.iggGetGlyphRangesCyrillic(atlas.handle()))
  49. }
  50. // GlyphRangesThai describes Default + Thai characters.
  51. func (atlas FontAtlas) GlyphRangesThai() GlyphRanges {
  52. return GlyphRanges(C.iggGetGlyphRangesThai(atlas.handle()))
  53. }
  54. // AddFontDefault adds the default font to the atlas. This is done by default if you do not call any
  55. // of the AddFont* methods before retrieving the texture data.
  56. func (atlas FontAtlas) AddFontDefault() Font {
  57. fontHandle := C.iggAddFontDefault(atlas.handle())
  58. return Font(fontHandle)
  59. }
  60. // AddFontDefaultV adds the default font to the atlas using the specified FontConfig.
  61. func (atlas FontAtlas) AddFontDefaultV(cfg FontConfig) Font {
  62. fontHandle := C.iggAddFontDefaultV(atlas.handle(), cfg.handle())
  63. return Font(fontHandle)
  64. }
  65. // AddFontFromFileTTFV attempts to load a font from given TTF file.
  66. func (atlas FontAtlas) AddFontFromFileTTFV(filename string, sizePixels float32,
  67. config FontConfig, glyphRange GlyphRanges) Font {
  68. filenameArg, filenameFin := wrapString(filename)
  69. defer filenameFin()
  70. fontHandle := C.iggAddFontFromFileTTF(atlas.handle(), filenameArg, C.float(sizePixels),
  71. config.handle(), glyphRange.handle())
  72. return Font(fontHandle)
  73. }
  74. // AddFontFromFileTTF calls AddFontFromFileTTFV(filename, sizePixels, DefaultFontConfig, EmptyGlyphRanges).
  75. func (atlas FontAtlas) AddFontFromFileTTF(filename string, sizePixels float32) Font {
  76. return atlas.AddFontFromFileTTFV(filename, sizePixels, DefaultFontConfig, EmptyGlyphRanges)
  77. }
  78. // AddFontFromMemoryTTFV attempts to load a font from given TTF byte array.
  79. func (atlas FontAtlas) AddFontFromMemoryTTFV(
  80. fontData []byte, sizePixels float32,
  81. config FontConfig,
  82. glyphRange GlyphRanges,
  83. ) Font {
  84. // NOTE: We never free the fontDataC array because IMGUI's AddFontFromMemoryTTF takes ownership if
  85. // FontConfig.FontDataOwnedByAtlas == true (which it is by default). We do not expose this flag in Go
  86. // so we can assume in most cases it is true.
  87. if !config.getFontDataOwnedByAtlas() {
  88. panic("Only ImFontConfig.FontDataOwnedByAtlas == true is supported.")
  89. }
  90. fontDataC := C.malloc(C.size_t(len(fontData)))
  91. cBuf := (*[1 << 30]byte)(fontDataC)
  92. copy(cBuf[:], fontData)
  93. fontHandle := C.iggAddFontFromMemoryTTF(atlas.handle(), (*C.char)(fontDataC), C.int(len(fontData)), C.float(sizePixels),
  94. config.handle(), glyphRange.handle())
  95. return Font(fontHandle)
  96. }
  97. // AddFontFromMemoryTTF calls AddFontFromMemoryTTFV(fontData, sizePixels, DefaultFontConfig, EmptyGlyphRanges).
  98. func (atlas FontAtlas) AddFontFromMemoryTTF(fontData []byte, sizePixels float32) Font {
  99. return atlas.AddFontFromMemoryTTFV(fontData, sizePixels, DefaultFontConfig, EmptyGlyphRanges)
  100. }
  101. // SetTexDesiredWidth registers the width desired by user before building the image. Must be a power-of-two.
  102. // If have many glyphs your graphics API have texture size restrictions you may want to increase texture width to decrease height.
  103. // Set to 0 by default, causing auto-calculation.
  104. func (atlas FontAtlas) SetTexDesiredWidth(value int) {
  105. C.iggFontAtlasSetTexDesiredWidth(atlas.handle(), C.int(value))
  106. }
  107. // TextureDataAlpha8 returns the image in 8-bit alpha values for the font atlas.
  108. // The returned image is valid as long as the font atlas is.
  109. func (atlas FontAtlas) TextureDataAlpha8() *Alpha8Image {
  110. var pixels *C.uchar
  111. var width C.int
  112. var height C.int
  113. var bytesPerPixel C.int
  114. C.iggFontAtlasGetTexDataAsAlpha8(atlas.handle(), &pixels, &width, &height, &bytesPerPixel)
  115. return &Alpha8Image{
  116. Width: int(width),
  117. Height: int(height),
  118. Pixels: unsafe.Pointer(pixels), // nolint: gas
  119. }
  120. }
  121. // TextureDataRGBA32 returns the image in 32-bit RGBA values for the font atlas.
  122. // The returned image is valid as long as the font atlas is.
  123. func (atlas FontAtlas) TextureDataRGBA32() *RGBA32Image {
  124. var pixels *C.uchar
  125. var width C.int
  126. var height C.int
  127. var bytesPerPixel C.int
  128. C.iggFontAtlasGetTexDataAsRGBA32(atlas.handle(), &pixels, &width, &height, &bytesPerPixel)
  129. return &RGBA32Image{
  130. Width: int(width),
  131. Height: int(height),
  132. Pixels: unsafe.Pointer(pixels), // nolint: gas
  133. }
  134. }
  135. // SetTextureID sets user data to refer to the texture once it has been uploaded to user's graphic systems.
  136. // It is passed back to you during rendering via the DrawCommand.
  137. func (atlas FontAtlas) SetTextureID(id TextureID) {
  138. C.iggFontAtlasSetTextureID(atlas.handle(), id.handle())
  139. }
  140. func (atlas FontAtlas) Clear() {
  141. C.iggFontAtlasClear(atlas.handle())
  142. }
  143. func (atlas FontAtlas) Build() {
  144. C.iggFontAtlasBuild(atlas.handle())
  145. }
  146. // FontBuilderFlags returns shared flags (for all fonts) for custom font builder.
  147. func (atlas FontAtlas) FontBuilderFlags() uint {
  148. return uint(C.iggFontAtlasGetFontBuilderFlags(atlas.handle()))
  149. }
  150. // SetFontBuilderFlags sets shared flags (for all fonts) for custom font builder.
  151. // THIS IS BUILD IMPLEMENTATION DEPENDENT. Per-font override is also available in FontConfig.
  152. func (atlas FontAtlas) SetFontBuilderFlags(flags uint) {
  153. C.iggFontAtlasSetFontBuilderFlags(atlas.handle(), C.uint(flags))
  154. }
  155. // Returns fonts count currently available.
  156. func (atlas FontAtlas) GetFontCount() int {
  157. return int(C.iggFontAtlasFontCount(atlas.handle()))
  158. }