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