diff options
| author | Aldrik Ramaekers <aldrik.ramaekers@protonmail.com> | 2020-02-03 18:55:17 +0100 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrik.ramaekers@protonmail.com> | 2020-02-03 18:55:17 +0100 |
| commit | 83cbdcc8c3d1c900417457f2ef44eafde123db93 (patch) | |
| tree | 2623f1056b2191c9e48f22811018042ee0a6f55b /src | |
| parent | 7e17e6bb7e7d7547bc01fa92090f59699e937a72 (diff) | |
fix text rendering
Diffstat (limited to 'src')
| -rw-r--r-- | src/assets.c | 1 | ||||
| -rw-r--r-- | src/assets.h | 2 | ||||
| -rw-r--r-- | src/localization.c | 3 | ||||
| -rw-r--r-- | src/mo_edit.c | 7 | ||||
| -rw-r--r-- | src/render.c | 85 | ||||
| -rw-r--r-- | src/save.c | 88 |
6 files changed, 140 insertions, 46 deletions
diff --git a/src/assets.c b/src/assets.c index 4b8bd3e..37334dd 100644 --- a/src/assets.c +++ b/src/assets.c @@ -131,7 +131,6 @@ bool assets_queue_worker_load_font(font *font) if (i == 'M') font->px_h = -yoff; if (i == ' ') new_glyph.xoff = font->size/3; - if (i == 'g') new_glyph.xoff += 1; // stupid font or stupid me? font->glyphs[i-TEXT_CHARSET_START] = new_glyph; } diff --git a/src/assets.h b/src/assets.h index 88b577e..4e614e9 100644 --- a/src/assets.h +++ b/src/assets.h @@ -16,7 +16,7 @@ #endif #ifndef ASSET_QUEUE_COUNT -#define ASSET_QUEUE_COUNT 10 +#define ASSET_QUEUE_COUNT 20 #endif // binary blobs diff --git a/src/localization.c b/src/localization.c index 41b1fcf..2ee39e3 100644 --- a/src/localization.c +++ b/src/localization.c @@ -24,8 +24,7 @@ mo_file load_localization_file(u8 *start_addr, u8 *end_addr, u8 *img_start, u8 * mo_entry *identifiers = (mo_entry*)(buffer + mo.header.identifier_table_offset); mo_entry *translations = (mo_entry*)(buffer + mo.header.translation_table_offset); - // skip first one because this is file information - for (s32 i = 1; i < mo.header.number_of_strings; i++) + for (s32 i = 0; i < mo.header.number_of_strings; i++) { mo_entry *entry = &identifiers[i]; mo_entry *trans = &translations[i]; diff --git a/src/mo_edit.c b/src/mo_edit.c index 87e184f..9b627fc 100644 --- a/src/mo_edit.c +++ b/src/mo_edit.c @@ -161,13 +161,13 @@ language* get_language_by_id(s32 language_id) return 0; } -void add_language_to_project(char *buffer) +s32 add_language_to_project(char *buffer) { if (string_equals(buffer, "")) { // TODO(Aldrik): localize platform_show_message(main_window, "Language name cannot be empty", "Invalid info"); - return; + return -1; } char *val = mem_alloc(MAX_INPUT_LENGTH); @@ -190,6 +190,8 @@ void add_language_to_project(char *buffer) array_push(&t->translations, &new_t); } + + return l.id; } void set_term_name(s32 index, char *name) @@ -324,6 +326,7 @@ void save_term_changes() void start_new_project() { + global_language_id = 1; current_project = mem_alloc(sizeof(translation_project)); current_project->terms = array_create(sizeof(term)); diff --git a/src/render.c b/src/render.c index e126133..666b2f9 100644 --- a/src/render.c +++ b/src/render.c @@ -88,19 +88,22 @@ s32 render_text_ellipsed(font *font, s32 x, s32 y, s32 maxw, char *text, color t s32 width = g.width; + int advance, lsb, kern; + stbtt_GetCodepointHMetrics(&font->info, ch, &advance, &lsb); + s32 y_ = y + font->px_h + g.yoff; + s32 x_to_render = x_ + (lsb*font->scale); - glTexCoord2i(0, 0); glVertex3i(x_,y_, render_depth); - glTexCoord2i(0, 1); glVertex3i(x_,y_+g.height, render_depth); - glTexCoord2i(1, 1); glVertex3i(x_+g.width,y_+g.height, render_depth); - glTexCoord2i(1, 0); glVertex3i(x_+g.width,y_, render_depth); + glTexCoord2i(0, 0); glVertex3i(x_to_render,y_, render_depth); + glTexCoord2i(0, 1); glVertex3i(x_to_render,y_+g.height, render_depth); + glTexCoord2i(1, 1); glVertex3i(x_to_render+g.width,y_+g.height, render_depth); + glTexCoord2i(1, 0); glVertex3i(x_to_render+g.width,y_, render_depth); glEnd(); /* add kerning */ - int kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next); - if (kern != 0) x_ += kern * font->scale; - x_ += g.width+g.xoff; + kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next); + x_ += (advance*font->scale)+(kern*font->scale); if (!in_ellipse && (x_-x) > maxw-(font->glyphs['.'].width*3)) { @@ -141,19 +144,22 @@ s32 render_text(font *font, s32 x, s32 y, char *text, color tint) s32 width = g.width; + int advance, lsb, kern; + stbtt_GetCodepointHMetrics(&font->info, ch, &advance, &lsb); + s32 y_ = y + font->px_h + g.yoff; + s32 x_to_render = x_ + (lsb*font->scale); - glTexCoord2i(0, 0); glVertex3i(x_,y_, render_depth); - glTexCoord2i(0, 1); glVertex3i(x_,y_+g.height, render_depth); - glTexCoord2i(1, 1); glVertex3i(x_+g.width,y_+g.height, render_depth); - glTexCoord2i(1, 0); glVertex3i(x_+g.width,y_, render_depth); + glTexCoord2i(0, 0); glVertex3i(x_to_render,y_, render_depth); + glTexCoord2i(0, 1); glVertex3i(x_to_render,y_+g.height, render_depth); + glTexCoord2i(1, 1); glVertex3i(x_to_render+g.width,y_+g.height, render_depth); + glTexCoord2i(1, 0); glVertex3i(x_to_render+g.width,y_, render_depth); glEnd(); /* add kerning */ - int kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next); - if (kern != 0) x_ += kern * font->scale; - x_ += g.width+g.xoff; + kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next); + x_ += (advance*font->scale)+(kern*font->scale); } glDisable(GL_TEXTURE_2D); @@ -209,19 +215,23 @@ s32 render_text_cutoff(font *font, s32 x, s32 y, char *text, color tint, u16 cut s32 width = g.width; + int advance, lsb, kern; + stbtt_GetCodepointHMetrics(&font->info, ch, &advance, &lsb); + s32 y__ = y_ + font->px_h + g.yoff; + s32 x_to_render = x_ + (lsb*font->scale); - glTexCoord2i(0, 0); glVertex3i(x_,y__, render_depth); - glTexCoord2i(0, 1); glVertex3i(x_,y__+g.height, render_depth); - glTexCoord2i(1, 1); glVertex3i(x_+g.width,y__+g.height, render_depth); - glTexCoord2i(1, 0); glVertex3i(x_+g.width,y__, render_depth); + glTexCoord2i(0, 0); glVertex3i(x_to_render,y__, render_depth); + glTexCoord2i(0, 1); glVertex3i(x_to_render,y__+g.height, render_depth); + glTexCoord2i(1, 1); glVertex3i(x_to_render+g.width,y__+g.height, render_depth); + glTexCoord2i(1, 0); glVertex3i(x_to_render+g.width,y__, render_depth); glEnd(); - int kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next); - if (kern != 0) x_ += kern * font->scale; + /* add kerning */ + kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next); + x_ += (advance*font->scale)+(kern*font->scale); - x_ += g.width+g.xoff; if (x_ > x+cutoff_width) { x_ = x; @@ -260,10 +270,13 @@ s32 calculate_cursor_position(font *font, char *text, s32 click_x) s32 width = g.width; s32 width_next = font->glyphs[ch_next].width; - int kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next); - if (kern != 0) x += kern * font->scale; - x += g.width+g.xoff; + int advance, lsb, kern; + stbtt_GetCodepointHMetrics(&font->info, ch, &advance, &lsb); + + /* add kerning */ + kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next); + x += (advance*font->scale)+(kern*font->scale); if (x - (width_next/5) > click_x) { @@ -301,10 +314,12 @@ s32 calculate_text_width_from_upto(font *font, char *text, s32 from, s32 index) if (i >= from) { - int kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next); - if (kern != 0) x += kern * font->scale; + int advance, lsb, kern; + stbtt_GetCodepointHMetrics(&font->info, ch, &advance, &lsb); - x += g.width+g.xoff; + /* add kerning */ + kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next); + x += (advance*font->scale)+(kern*font->scale); } i++; @@ -336,10 +351,12 @@ s32 calculate_text_width_upto(font *font, char *text, s32 index) glyph g = font->glyphs[ch]; s32 width = g.width; - int kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next); - if (kern != 0) x += kern * font->scale; + int advance, lsb, kern; + stbtt_GetCodepointHMetrics(&font->info, ch, &advance, &lsb); - x += g.width+g.xoff; + /* add kerning */ + kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next); + x += (advance*font->scale)+(kern*font->scale); i++; } @@ -367,10 +384,12 @@ s32 calculate_text_width(font *font, char *text) glyph g = font->glyphs[ch]; s32 width = g.width; - int kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next); - if (kern != 0) x += kern * font->scale; + int advance, lsb, kern; + stbtt_GetCodepointHMetrics(&font->info, ch, &advance, &lsb); - x += g.width+g.xoff; + /* add kerning */ + kern = stbtt_GetCodepointKernAdvance(&font->info, ch, ch_next); + x += (advance*font->scale)+(kern*font->scale); } return x; @@ -5,6 +5,7 @@ */ void start_new_project(); +s32 add_language_to_project(char *buffer); s32 write_mo_file(char *buffer, s32 buffer_size, s32 language_id) { @@ -143,8 +144,43 @@ void save_project() thread_detach(&thr); } -bool read_mo_file(char *buffer, s32 buffer_size) +bool read_mo_file(char *buffer, s32 buffer_size, s32 language_id) { + mo_header header = *((mo_header*)buffer); + + mo_entry *identifiers = (mo_entry*)(buffer + header.identifier_table_offset); + mo_entry *translations = (mo_entry*)(buffer + header.translation_table_offset); + + if (current_project->terms.length == 0) + { + for (s32 i = 0; i < header.number_of_strings; i++) + { + term t; + t.name = mem_alloc(MAX_TERM_NAME_LENGTH); + t.translations = array_create(sizeof(translation)); + array_reserve(&t.translations, current_project->languages.length); + + string_copyn(t.name, buffer+identifiers[i].offset, MAX_INPUT_LENGTH); + array_push(¤t_project->terms, &t); + } + } + else if (current_project->terms.length != header.number_of_strings) + { + // TODO(Aldrik): localize + platform_show_message(main_window, "Warning", "File is missing terms"); + } + + for (s32 i = 0; i < current_project->terms.length; i++) + { + term *t = array_at(¤t_project->terms, i); + + translation tr; + tr.value = mem_alloc(MAX_INPUT_LENGTH); + string_copyn(tr.value, buffer+translations[i].offset, MAX_INPUT_LENGTH); + tr.language_id = language_id; + array_push(&t->translations, &tr); + } + return true; } @@ -160,15 +196,53 @@ void load_project_from_folder(char *path_buf) start_new_project(); -#if 0 - // foreach file in path_buf - file_content content = platform_read_file_content(path_buf, "rb"); - if (!content.content || content.file_error) + array files = array_create(sizeof(found_file)); + array filters = get_filters("*.mo"); + + bool is_cancelled = false; + + char total_path[MAX_INPUT_LENGTH]; + total_path[0] = 0; + string_copyn(total_path, path_buf, MAX_INPUT_LENGTH); + +#ifdef OS_WIN + string_appendn(total_path, "\\", MAX_INPUT_LENGTH); +#endif +#ifdef OS_LINUX + string_appendn(total_path, "/", MAX_INPUT_LENGTH); +#endif + + platform_list_files_block(&files, total_path, filters, false, 0, false, &is_cancelled); + for (s32 i = 0; i < files.length; i++) { + found_file *file = array_at(&files, i); + + file_content content = platform_read_file_content(file->path, "rb"); + if (content.content && !content.file_error) + { + char name[MAX_INPUT_LENGTH]; + get_name_from_path(name, file->path); + name[strlen(name)-3] = 0; // remove .mo extension + + language l; + l.name = mem_alloc(MAX_INPUT_LENGTH); + string_copyn(l.name, name, MAX_INPUT_LENGTH); + l.id = global_language_id++; + array_push(¤t_project->languages, &l); + + read_mo_file(content.content, content.content_length, l.id); + } platform_destroy_file_content(&content); - return; } -#endif + array_destroy(&filters); + + for (s32 i = 0; i < files.length; i++) + { + found_file *match = array_at(&files, i); + mem_free(match->matched_filter); + mem_free(match->path); + } + array_destroy(&files); } static void* load_project_d(void *arg) |
