Unravel Engine C++ Reference
Loading...
Searching...
No Matches
font_manager.h
Go to the documentation of this file.
1/*
2 * Copyright 2013 Jeremie Roy. All rights reserved.
3 * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
4 */
5
6#ifndef FONT_MANAGER_H_HEADER_GUARD
7#define FONT_MANAGER_H_HEADER_GUARD
8
9#include <bgfx/bgfx.h>
10#include <bx/handlealloc.h>
11#include <bx/string.h>
12
13namespace gfx
14{
15class Atlas;
16
17#define MAX_OPENED_FILES 512
18#define MAX_OPENED_FONT 512
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
34{
36 uint16_t pixel_size{};
38 int16_t font_type{};
39
41 float ascender{};
43 float descender{};
45 float line_gap{};
47 float capline{};
49 float xline{};
57 float scale{};
58};
59
60// Glyph metrics:
61// --------------
62//
63// xmin xmax
64// | |
65// |<-------- width -------->|
66// | |
67// | +-------------------------+----------------- ymax
68// | | ggggggggg ggggg | ^ ^
69// | | g:::::::::ggg::::g | | |
70// | | g:::::::::::::::::g | | |
71// | | g::::::ggggg::::::gg | | |
72// | | g:::::g g:::::g | | |
73// offset_x -|-------->| g:::::g g:::::g | offset_y |
74// | | g:::::g g:::::g | | |
75// | | g::::::g g:::::g | | |
76// | | g:::::::ggggg:::::g | | |
77// | | g::::::::::::::::g | | height
78// | | gg::::::::::::::g | | |
79// baseline ---*---------|---- gggggggg::::::g-----*-------- |
80// / | | g:::::g | |
81// origin | | gggggg g:::::g | |
82// | | g:::::gg gg:::::g | |
83// | | g::::::ggg:::::::g | |
84// | | gg:::::::::::::g | |
85// | | ggg::::::ggg | |
86// | | gggggg | v
87// | +-------------------------+----------------- ymin
88// | |
89// |------------- advance_x ---------->|
90
92using code_point = uint32_t;
93
96{
98 int32_t glyph_index{};
99
101 float width{};
102
104 float height{};
105
107 float offset_x{};
108
113 float offset_y{};
114
118 float advance_x{};
119
123 float advance_y{};
124
127
129 uint16_t region_index{};
130};
131
132BGFX_HANDLE(true_type_handle)
133BGFX_HANDLE(font_handle)
134
136{
137public:
140 font_manager(uint16_t texture_side_width = 512);
141
143
145 auto get_atlas(font_handle handle) const -> Atlas*;
146
151 auto create_ttf(const uint8_t* buffer, uint32_t size) -> true_type_handle;
152
154 void destroy_ttf(true_type_handle handle);
155
157 auto create_font_by_pixel_size(true_type_handle handle,
158 uint32_t typeface_index,
159 uint32_t pixel_size,
160 uint32_t font_type = FONT_TYPE_ALPHA,
161 uint16_t glyph_width_padding = 8,
162 uint16_t glyph_height_padding = 8) -> font_handle;
163
165 auto create_scaled_font_to_pixel_size(font_handle base_font_handle, uint32_t pixel_size) -> font_handle;
166
168 void destroy_font(font_handle handle);
169
174 auto preload_glyph(font_handle handle, const wchar_t* string, const wchar_t* end = nullptr) -> bool;
175 auto preload_glyph(font_handle handle, const char* string, const char* end = nullptr) -> bool;
176
178 auto preload_glyph(font_handle handle, code_point character) -> bool;
179 auto preload_glyph_ranges(font_handle handle, const code_point* ranges) -> bool;
180
181 auto add_glyph_bitmap(font_handle handle,
182 code_point character,
183 uint16_t width,
184 uint16_t height,
185 uint16_t pitch,
186 float extra_scale,
187 const uint8_t* bitmap_buffer,
188 float glyph_offset_x,
189 float glyph_offset_y) -> bool;
190
194 auto get_font_info(font_handle handle) const -> const font_info&;
195
199 auto get_glyph_info(font_handle handle, code_point code_point) -> const glyph_info*;
200
201 auto get_kerning(font_handle handle, code_point prev_code_point, code_point code_point) -> float;
202
203 auto get_white_glyph(font_handle handle) const -> const glyph_info&;
204
205private:
206 struct cached_font;
207 struct cached_file
208 {
209 uint8_t* buffer;
210 uint32_t buffer_size;
211 };
212
213 void init();
214 auto add_bitmap(Atlas* atlas, glyph_info& glyph_info, const uint8_t* data) -> bool;
215 uint16_t atlas_size_;
216
217 bx::HandleAllocT<MAX_OPENED_FONT> font_handles_;
218 cached_font* cached_fonts_;
219
220 bx::HandleAllocT<MAX_OPENED_FILES> file_handles_;
221 cached_file* cached_files_;
222
223 // temporary buffer to raster glyph
224 uint8_t* buffer_;
225};
226} // namespace gfx
227#endif // FONT_MANAGER_H_HEADER_GUARD
#define FONT_TYPE_ALPHA
bool init(init_type init_data)
Definition graphics.cpp:177
uint32_t code_point
Unicode value of a character.
void end(encoder *_encoder)
Definition graphics.cpp:270
float line_gap
The spacing in pixels between one row's descent and the next row's ascent.
float xline
The extends above the baseline representing of the small letters.
float underline_thickness
The thickness of the under/hover/strike-trough line in pixels.
float underline_position
The position of the underline relatively to the baseline.
float ascender
The pixel extents above the baseline in pixels (typically positive).
float scale
Scale to apply to glyph data.
int16_t font_type
Rendering type used for the font.
float descender
The extents below the baseline in pixels (typically negative).
uint16_t pixel_size
The font height in pixel.
float max_advance_width
This field gives the maximum horizontal cursor advance for all glyphs in the font.
float capline
The extends above the baseline representing of the capital letters.
A structure that describe a glyph.
float bitmap_scale
Amount to scale a bitmap image glyph.
int32_t glyph_index
Index for faster retrieval.
float width
Glyph's width in pixels.
float offset_x
Glyph's left offset in pixels.
uint16_t region_index
Region index in the atlas storing textures.
float height
Glyph's height in pixels.
gfx::uniform_handle handle
Definition uniform.cpp:9