Visual Computing Library
Loading...
Searching...
No Matches
font_manager.h
1/*
2 * Copyright 2013 Jeremie Roy. All rights reserved.
3 * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
4 */
5
6#ifndef VCL_BGFX_TEXT_FONT_FONT_MANAGER_H
7#define VCL_BGFX_TEXT_FONT_FONT_MANAGER_H
8
9#include <bx/handlealloc.h>
10#include <bx/string.h>
11#include <bgfx/bgfx.h>
12
13namespace bgfx {
14
15class Atlas;
16
17#define MAX_OPENED_FILES 64
18#define MAX_OPENED_FONT 64
19
20#define FONT_TYPE_ALPHA UINT32_C(0x00000100) // L8
21// #define FONT_TYPE_LCD UINT32_C(0x00000200) // BGRA8
22// #define FONT_TYPE_RGBA UINT32_C(0x00000300) // BGRA8
23#define FONT_TYPE_DISTANCE UINT32_C(0x00000400) // L8
24#define FONT_TYPE_DISTANCE_SUBPIXEL UINT32_C(0x00000500) // L8
25#define FONT_TYPE_DISTANCE_OUTLINE UINT32_C(0x00000600) // L8
26#define FONT_TYPE_DISTANCE_OUTLINE_IMAGE UINT32_C(0x00001600) // L8 + BGRA8
27#define FONT_TYPE_DISTANCE_DROP_SHADOW UINT32_C(0x00002700) // L8
28#define FONT_TYPE_DISTANCE_DROP_SHADOW_IMAGE UINT32_C(0x00003800) // L8 + BGRA8
29#define FONT_TYPE_DISTANCE_OUTLINE_DROP_SHADOW_IMAGE UINT32_C(0x00003900) // L8 + BGRA8
30#define FONT_TYPE_MASK_DISTANCE_IMAGE UINT32_C(0x00001000)
31#define FONT_TYPE_MASK_DISTANCE_DROP_SHADOW UINT32_C(0x00002000)
32
33struct FontInfo
34{
36 uint16_t pixelSize;
38 int16_t fontType;
39
41 float ascender;
43 float descender;
45 float lineGap;
47 float maxAdvanceWidth;
49 float underlineThickness;
51 float underlinePosition;
52
54 float scale;
55};
56
57// Glyph metrics:
58// --------------
59//
60// xmin xmax
61// | |
62// |<-------- width -------->|
63// | |
64// | +-------------------------+----------------- ymax
65// | | ggggggggg ggggg | ^ ^
66// | | g:::::::::ggg::::g | | |
67// | | g:::::::::::::::::g | | |
68// | | g::::::ggggg::::::gg | | |
69// | | g:::::g g:::::g | | |
70// offset_x -|-------->| g:::::g g:::::g | offset_y |
71// | | g:::::g g:::::g | | |
72// | | g::::::g g:::::g | | |
73// | | g:::::::ggggg:::::g | | |
74// | | g::::::::::::::::g | | height
75// | | gg::::::::::::::g | | |
76// baseline ---*---------|---- gggggggg::::::g-----*-------- |
77// / | | g:::::g | |
78// origin | | gggggg g:::::g | |
79// | | g:::::gg gg:::::g | |
80// | | g::::::ggg:::::::g | |
81// | | gg:::::::::::::g | |
82// | | ggg::::::ggg | |
83// | | gggggg | v
84// | +-------------------------+----------------- ymin
85// | |
86// |------------- advance_x ---------->|
87
89typedef int32_t CodePoint;
90
92struct GlyphInfo
93{
95 int32_t glyphIndex;
96
98 float width;
99
101 float height;
102
104 float offset_x;
105
110 float offset_y;
111
115 float advance_x;
116
120 float advance_y;
121
123 float bitmapScale;
124
126 uint16_t regionIndex;
127};
128
129BGFX_HANDLE(TrueTypeHandle)
130BGFX_HANDLE(FontHandle)
131
132class FontManager
133{
134public:
137 FontManager(Atlas* _atlas);
138
141 FontManager(uint16_t _textureSideWidth = 512);
142
143 ~FontManager();
144
146 const Atlas* getAtlas() const
147 {
148 return m_atlas;
149 }
150
155 TrueTypeHandle createTtf(const uint8_t* _buffer, uint32_t _size);
156
158 void destroyTtf(TrueTypeHandle _handle);
159
161 FontHandle createFontByPixelSize(TrueTypeHandle _handle, uint32_t _typefaceIndex, uint32_t _pixelSize, uint32_t _fontType = FONT_TYPE_ALPHA,
162 uint16_t _glyphWidthPadding = 6, uint16_t glyphHeightPadding = 6);
163
165 FontHandle createScaledFontToPixelSize(FontHandle _baseFontHandle, uint32_t _pixelSize);
166
168 void destroyFont(FontHandle _handle);
169
174 bool preloadGlyph(FontHandle _handle, const wchar_t* _string);
175
177 bool preloadGlyph(FontHandle _handle, CodePoint _character);
178
179 bool addGlyphBitmap(FontHandle _handle, CodePoint _character, uint16_t _width, uint16_t height, uint16_t _pitch, float extraScale, const uint8_t* _bitmapBuffer, float glyphOffsetX, float glyphOffsetY);
180
184 const FontInfo& getFontInfo(FontHandle _handle) const;
185
189 const GlyphInfo* getGlyphInfo(FontHandle _handle, CodePoint _codePoint);
190
191 float getKerning(FontHandle _handle, CodePoint _prevCodePoint, CodePoint _codePoint);
192
193 const GlyphInfo& getBlackGlyph() const
194 {
195 return m_blackGlyph;
196 }
197
198private:
199 struct CachedFont;
200 struct CachedFile
201 {
202 uint8_t* buffer;
203 uint32_t bufferSize;
204 };
205
206 void init();
207 bool addBitmap(GlyphInfo& _glyphInfo, const uint8_t* _data);
208
209 bool m_ownAtlas;
210 Atlas* m_atlas;
211
212 bx::HandleAllocT<MAX_OPENED_FONT> m_fontHandles;
213 CachedFont* m_cachedFonts;
214
215 bx::HandleAllocT<MAX_OPENED_FILES> m_filesHandles;
216 CachedFile* m_cachedFiles;
217
218 GlyphInfo m_blackGlyph;
219
220 //temporary buffer to raster glyph
221 uint8_t* m_buffer;
222};
223
224} // namespace bgfx
225
226#endif // VCL_BGFX_TEXT_FONT_FONT_MANAGER_H