Skip to content

Commit 0fd089a

Browse files
committed
[fix] a glitch where unloaded face(s) was crashing the entire application (must be log)
1 parent ed786db commit 0fd089a

File tree

10 files changed

+103
-41
lines changed

10 files changed

+103
-41
lines changed

include/ekg/draw/font_renderer.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ namespace ekg::draw {
8989
bool flag_first_time {true};
9090
bool font_face_changed {};
9191
bool font_size_changed {};
92+
bool was_initialized {};
9293

9394
ekg::gpu::allocator *p_allocator {};
9495
std::unordered_map<char32_t, ekg::draw::glyph_char_t> mapped_glyph_char_data {};

include/ekg/draw/typography.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ namespace ekg::draw {
4646
public:
4747
FT_Face ft_face {};
4848
FT_GlyphSlot ft_glyph_slot {};
49-
std::string_view font_path {};
49+
std::string font_path {};
5050
bool font_face_changed {};
5151
bool font_face_loaded {};
52+
FT_Vector highest_glyph_size {};
5253
};
5354

5455
bool reload_font_face(

include/ekg/ekg.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ namespace ekg {
6464

6565
struct runtime_property {
6666
public:
67-
const char *p_font_path {};
67+
const char *p_font_path {""};
6868
const char *p_font_path_emoji {""};
6969
ekg::gpu::api *p_gpu_api {};
7070
ekg::os::platform *p_os_platform {};

include/ekg/util/io.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ namespace ekg {
179179

180180
~log() {
181181
ekg::log::buffer << '\n';
182+
#define EKG_LOG_DEBUG
183+
#ifdef EKG_LOG_DEBUG
184+
ekg::log::buffered = true;
185+
ekg::log::flush();
186+
ekg_log("");
187+
#endif
182188
}
183189

184190
template<typename t>

libekg.a

6.89 MB
Binary file not shown.

src/core/runtime.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ ekg::stack ekg::runtime::front {};
5252
ekg::current_hovered_state ekg::hovered {};
5353

5454
void ekg::runtime::update_size_changed() {
55+
ekg::log() << "Refreshing layout and font sizes!";
5556
ekg::layout::scale_calculate();
5657

5758
uint32_t font_size {
@@ -573,7 +574,7 @@ void ekg::runtime::prepare_ui_env() {
573574
this->f_renderer_small.bind_allocator(&this->gpu_allocator);
574575

575576
this->f_renderer_normal.sampler_texture.gl_protected_active_index = true;
576-
this->f_renderer_normal.set_size(0);
577+
this->f_renderer_normal.set_size(18);
577578
this->f_renderer_normal.bind_allocator(&this->gpu_allocator);
578579

579580
this->f_renderer_big.sampler_texture.gl_protected_active_index = true;

src/draw/font_renderer.cpp

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ ekg::gpu::sampler_t *ekg::draw::font_renderer::get_sampler_texture() {
3434
}
3535

3636
float ekg::draw::font_renderer::get_text_width(std::string_view text, int32_t &lines) {
37-
if (text.empty()) {
37+
if (text.empty() || (!this->font_face_text.font_face_loaded && !this->font_face_emoji.font_face_loaded)) {
3838
return 0.0f;
3939
}
4040

@@ -101,7 +101,7 @@ float ekg::draw::font_renderer::get_text_width(std::string_view text, int32_t &l
101101
}
102102

103103
float ekg::draw::font_renderer::get_text_width(std::string_view text) {
104-
if (text.empty()) {
104+
if (text.empty() || (!this->font_face_text.font_face_loaded && !this->font_face_emoji.font_face_loaded)) {
105105
return 0.0f;
106106
}
107107

@@ -168,15 +168,15 @@ float ekg::draw::font_renderer::get_text_height() {
168168
}
169169

170170
void ekg::draw::font_renderer::set_font(std::string_view path) {
171-
if (this->font_face_text.font_path != path) {
171+
if (!path.empty() && this->font_face_text.font_path != path) {
172172
this->font_face_text.font_path = path;
173173
this->font_face_text.font_face_changed = true;
174174
this->reload();
175175
}
176176
}
177177

178178
void ekg::draw::font_renderer::set_font_emoji(std::string_view path) {
179-
if (this->font_face_emoji.font_path != path) {
179+
if (!path.empty() && this->font_face_emoji.font_path != path) {
180180
this->font_face_emoji.font_path = path;
181181
this->font_face_emoji.font_face_changed = true;
182182
this->reload();
@@ -192,24 +192,35 @@ void ekg::draw::font_renderer::set_size(uint32_t size) {
192192
}
193193

194194
void ekg::draw::font_renderer::reload() {
195-
if (this->font_size == 0) {
195+
if (this->font_size == 0 || (this->font_face_text.font_path.empty() && this->font_face_emoji.font_path.empty())) {
196196
return;
197197
}
198198

199199
if (
200-
(!this->font_face_text.font_path.empty() && ekg::draw::reload_font_face(&this->font_face_text, this->font_size_changed, this->font_size)) ||
201-
(!this->font_face_emoji.font_path.empty() && ekg::draw::reload_font_face(&this->font_face_emoji, this->font_size_changed, this->font_size))
200+
(
201+
!this->font_face_text.font_path.empty()
202+
&&
203+
ekg::draw::reload_font_face(&this->font_face_text, this->font_size_changed, this->font_size)
204+
)
205+
||
206+
(
207+
!this->font_face_emoji.font_path.empty()
208+
&&
209+
ekg::draw::reload_font_face(&this->font_face_emoji, this->font_size_changed, this->font_size)
210+
)
202211
) {
203212
ekg::log() << "Failed to load font face, text: " << this->font_face_text.font_face_loaded << " , emoji: " << this->font_face_emoji.font_face_loaded;
204213
return;
205214
}
206215

207216
this->font_size_changed = false;
208217

209-
/* Phase of getting bitmap texture bounds. */
210218
this->full_width = 0;
211219
this->full_height = 0;
212220

221+
this->font_face_text.highest_glyph_size = FT_Vector {};
222+
this->font_face_emoji.highest_glyph_size = FT_Vector {};
223+
213224
this->ft_bool_kerning = FT_HAS_KERNING(this->font_face_text.ft_face);
214225
this->font_face_text.ft_glyph_slot = this->font_face_text.ft_face->glyph;
215226

@@ -219,22 +230,25 @@ void ekg::draw::font_renderer::reload() {
219230

220231
FT_GlyphSlot ft_glyph_slot {};
221232
FT_Face ft_face {};
222-
223233
ekg::flags flags {};
224234

235+
ekg::draw::font_face_t *p_font_face_picked {};
236+
225237
for (char32_t &char32 : this->loaded_sampler_generate_list) {
226238
switch (char32 < 256 || !this->font_face_emoji.font_face_loaded) {
227239
case true: {
228240
ft_face = this->font_face_text.ft_face;
229241
ft_glyph_slot = this->font_face_text.ft_face->glyph;
230242
flags = FT_LOAD_RENDER;
243+
p_font_face_picked = &this->font_face_text;
231244
break;
232245
}
233246

234247
default: {
235248
ft_face = this->font_face_emoji.ft_face;
236249
ft_glyph_slot = this->font_face_emoji.ft_face->glyph;
237250
flags = FT_LOAD_RENDER | FT_LOAD_COLOR;
251+
p_font_face_picked = &this->font_face_emoji;
238252
break;
239253
}
240254
}
@@ -253,7 +267,17 @@ void ekg::draw::font_renderer::reload() {
253267
char_data.wsize = static_cast<float>(static_cast<int32_t>(ft_glyph_slot->advance.x >> 6));
254268

255269
this->full_width += static_cast<int32_t>(char_data.w);
256-
this->full_height = std::max(this->full_height, static_cast<int32_t>(ft_glyph_slot->bitmap.rows));
270+
this->full_height = ekg_min(this->full_height, static_cast<int32_t>(char_data.h));
271+
272+
p_font_face_picked->highest_glyph_size.x = ekg_min(
273+
p_font_face_picked->highest_glyph_size.x,
274+
static_cast<int32_t>(char_data.w)
275+
);
276+
277+
p_font_face_picked->highest_glyph_size.y = ekg_min(
278+
p_font_face_picked->highest_glyph_size.y,
279+
static_cast<int32_t>(char_data.h)
280+
);
257281
}
258282

259283
this->text_height = static_cast<float>(this->font_size);
@@ -420,19 +444,27 @@ void ekg::draw::font_renderer::blit(std::string_view text, float x, float y, con
420444
void ekg::draw::font_renderer::flush() {
421445
uint64_t size {this->loaded_sampler_generate_list.size()};
422446
if (this->last_sampler_generate_list_size != size) {
447+
ekg::log() << "Sampler updated from-to: " << this->last_sampler_generate_list_size << " " << size;
448+
423449
this->reload();
424450
this->last_sampler_generate_list_size = size;
425451
ekg::ui::redraw = true;
426452
}
427453
}
428454

429455
void ekg::draw::font_renderer::init() {
456+
if (this->was_initialized) {
457+
return;
458+
}
459+
460+
this->was_initialized = true;
461+
430462
this->loaded_sampler_generate_list.resize(256);
431463
for (char32_t char32 {}; char32 < 256; char32++) {
432464
this->loaded_sampler_generate_list[char32] = char32;
433465
}
434466

435-
ekg::log() << "Initializing 256 chars rendereable";
467+
ekg::log() << "Initializing 256 default chars!";
436468
}
437469

438470
void ekg::draw::font_renderer::quit() {

src/os/ekg_opengl.cpp

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,43 @@ uint64_t ekg::os::opengl::generate_font_atlas(
329329
std::unordered_map<char32_t, ekg::draw::glyph_char_t> &mapped_glyph_char_data,
330330
float &non_swizzlable_range
331331
) {
332-
bool is_current_opengl_version_gl_es {
333-
this->opengl_version == ekg::os::opengl_version::es
332+
FT_Vector highest_glyph_size {
333+
(
334+
p_font_face_text->highest_glyph_size.x
335+
*
336+
p_font_face_text->highest_glyph_size.y
337+
)
338+
>
339+
(
340+
p_font_face_emoji->highest_glyph_size.x
341+
*
342+
p_font_face_emoji->highest_glyph_size.y
343+
)
344+
? (p_font_face_text->highest_glyph_size) : (p_font_face_emoji->highest_glyph_size)
334345
};
335346

336347
GLint sub_image_format {
337-
is_current_opengl_version_gl_es ? GL_RGBA : GL_RED
348+
GL_RED
349+
};
350+
351+
bool is_current_opengl_version_gl_es {
352+
this->opengl_version == ekg::os::opengl_version::es
338353
};
354+
355+
std::vector<unsigned char> r8_to_r8g8b8a8_swizzled_image {};
356+
unsigned char *p_current_image_buffer {};
357+
358+
if (is_current_opengl_version_gl_es) {
359+
sub_image_format = GL_RGBA;
360+
361+
r8_to_r8g8b8a8_swizzled_image.resize(
362+
highest_glyph_size.x
363+
*
364+
highest_glyph_size.y
365+
*
366+
4
367+
);
368+
}
339369

340370
if (!p_sampler->gl_id) {
341371
glGenTextures(1, &p_sampler->gl_id);
@@ -366,10 +396,6 @@ uint64_t ekg::os::opengl::generate_font_atlas(
366396
FT_Face ft_face {};
367397
FT_Vector char_size {};
368398

369-
std::vector<unsigned char> r8_to_r8g8b8a8_swizzled_image {};
370-
unsigned char *p_current_image_buffer {};
371-
size_t previous_size {1};
372-
373399
ekg::flags flags {};
374400
float offset {};
375401

@@ -413,18 +439,6 @@ uint64_t ekg::os::opengl::generate_font_atlas(
413439
char_size.x = char_data.w;
414440
char_size.y = char_data.h;
415441

416-
if (r8_to_r8g8b8a8_swizzled_image.size() != previous_size) {
417-
r8_to_r8g8b8a8_swizzled_image.resize(
418-
static_cast<size_t>(char_size.x)
419-
*
420-
static_cast<size_t>(char_size.y)
421-
*
422-
static_cast<size_t>(4)
423-
);
424-
425-
previous_size = r8_to_r8g8b8a8_swizzled_image.size();
426-
}
427-
428442
ekg::format_convert_result result {
429443
ekg::image_src_r8_convert_to_r8g8b8a8(
430444
char_size,

src/service/input.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include "ekg/ekg.hpp"
2929

3030
void ekg::service::input::init() {
31+
ekg::log() << "Initialising input-bind system!";
32+
3133
/**
3234
* Forced null termination at end due the necessary optmization,
3335
* when changing dynamically the special key state.

test/src/ekg_gui_showcase_test.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ int32_t showcase_useless_window() {
464464

465465
ekg::runtime_property ekg_runtime_property {
466466
.p_font_path = "comic-mono.ttf",
467-
.p_font_path_emoji = "twemoji.ttf",
467+
//.p_font_path_emoji = "twemoji.ttf",
468468
.p_gpu_api = new ekg::os::opengl("#version 300 es \nprecision highp float;"),
469469
//.p_gpu_api = new ekg::os::opengl("#version 450"),
470470
.p_os_platform = new ekg::os::sdl(app.p_sdl_win)
@@ -1044,9 +1044,9 @@ void test_out_of_context_uis() {
10441044
int32_t laboratory_testing() {
10451045
SDL_Init(SDL_INIT_VIDEO);
10461046

1047-
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
1048-
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
1049-
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);
1047+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
1048+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
1049+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
10501050
SDL_GL_SetSwapInterval((app.vsync = true));
10511051

10521052
app.p_sdl_win = {
@@ -1066,10 +1066,14 @@ int32_t laboratory_testing() {
10661066
glewExperimental = GL_TRUE;
10671067
glewInit();
10681068

1069+
ekg::ui::auto_scale = false;
1070+
ekg::ui::scale = {1280, 800};
1071+
10691072
ekg::runtime_property ekg_runtime_property {
1070-
.p_font_path = "whitneybook.otf",
1071-
.p_font_path_emoji = "twemoji.ttf",
1072-
.p_gpu_api = new ekg::os::opengl("#version 450"),
1073+
//.p_font_path = "comic-mono.ttf",
1074+
//.p_font_path_emoji = "twemoji.ttf",
1075+
.p_gpu_api = new ekg::os::opengl("#version 300 es \nprecision highp float;"),
1076+
//.p_gpu_api = new ekg::os::opengl("#version 450"),
10731077
.p_os_platform = new ekg::os::sdl(app.p_sdl_win)
10741078
};
10751079

@@ -1612,6 +1616,7 @@ int32_t laboratory_testing() {
16121616
};
16131617

16141618
ekg::label("meow", ekg::dock::fill | ekg::dock::next)->set_scaled_height(6);
1619+
ekg::textbox("oi", "idk", ekg::dock::fill | ekg::dock::next);
16151620

16161621
/*
16171622
ekg::gpu::sampler_t meow_sampler {};
@@ -1676,5 +1681,5 @@ int32_t laboratory_testing() {
16761681
}
16771682

16781683
int32_t main(int32_t, char**) {
1679-
return showcase_useless_window();
1684+
return laboratory_testing();
16801685
}

0 commit comments

Comments
 (0)