diff options
Diffstat (limited to 'src/font.cpp')
| -rw-r--r-- | src/font.cpp | 135 |
1 files changed, 93 insertions, 42 deletions
diff --git a/src/font.cpp b/src/font.cpp index 58be327..cce9cc4 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -92,7 +92,7 @@ uint32_t utf8_to_unicode(const char* str, uint32_t &out) { return i; } -float FontType_Inner_LookAheadWordLength(const FontType *const ft, const FontRender *const fr, uint32_t index, float font_glyph_spacing) { +float FontType_Inner_LookAheadWordLength(const FontType *const ft, const FontRender *const fr, uint32_t index, float font_glyph_spacing, uint32_t *char_count = nullptr) { uint32_t i; float ret = 0; FontGlyphChar *fgc; @@ -101,48 +101,93 @@ float FontType_Inner_LookAheadWordLength(const FontType *const ft, const FontRen if (PK_HAS_FLAG(fgc->flags, FONT_GLYPH_CHAR_FLAGS_WHITESPACE) == true) { break; } + if (PK_HAS_FLAG(fgc->flags, FONT_GLYPH_CHAR_FLAGS_NEW_LINE) == true) { + break; + } ret += fgc->advance * font_glyph_spacing; } + if (char_count != nullptr) { + *char_count = i - index; + } return ret; } -float FontType_Inner_LookAheadLineLength(const FontType *const ft, const FontRender *const fr, uint32_t index, float font_glyph_spacing) { - uint32_t i; - float ret = 0; - FontGlyphChar *fgc; +float FontType_Inner_LookAheadLineLength(const FontType *const ft, const FontRender *const fr, const uint32_t index, float font_glyph_spacing, uint32_t *char_count = nullptr) { + uint32_t i, ii; + float sz, ret = 0; + FontGlyphChar *fgc, *fgc2; for (i = index; i < fr->n_glyphs; ++i) { fgc = &ft->glyphs[fr->glyph_indices[i]]; if (PK_HAS_FLAG(fgc->flags, FONT_GLYPH_CHAR_FLAGS_NEW_LINE) == true) { + if (i != index) { + fgc2 = &ft->glyphs[fr->glyph_indices[i-1]]; + if (PK_HAS_FLAG(fgc2->flags, FONT_GLYPH_CHAR_FLAGS_WHITESPACE) == true) { + ret -= fgc2->advance * font_glyph_spacing; + } + } break; } - if (ret + (fgc->advance * font_glyph_spacing) > fr->settings.surface_area_size.x) { + // we want to burn white-space characters at the front and at the back + if (PK_HAS_FLAG(fgc->flags, FONT_GLYPH_CHAR_FLAGS_WHITESPACE) == true) { + ret += fgc->advance * font_glyph_spacing; + // burn the front + if (i == index) { + ret = 0; + } + continue; + } + sz = FontType_Inner_LookAheadWordLength(ft, fr, i, font_glyph_spacing, &ii); + if (ret + sz > fr->settings.surface_area_size.x) { + // burn the back + if (i != index) { + fgc2 = fgc-1; + if (PK_HAS_FLAG(fgc2->flags, FONT_GLYPH_CHAR_FLAGS_WHITESPACE) == true) { + ret -= fgc2->advance * font_glyph_spacing; + } + } break; } - ret += fgc->advance * font_glyph_spacing; + // -1 because the for loop is about to +1. + // On words that are not the last, not subtracting 1 burns the next character (bad) + // On the last word, not subtracting 1 causes the char_count to be off-by-one + i += ii-1; + ret += sz; + } + if (char_count != nullptr) { + *char_count = i - index; } return ret; } -float FontType_Inner_LookAheadLineCount(const FontType *const ft, const FontRender *const fr, const uint32_t index) { - uint32_t i, u; - float ret = 1; - FontGlyphChar *fgc; - for (i = index; i < fr->n_glyphs; ++i) { +float FontType_Inner_LookAheadLineCount(const FontType *const ft, const FontRender *const fr, const uint32_t index, float font_glyph_spacing) { + uint32_t ii = 0, i = index; + FontGlyphChar *fgc, *fgc2; + float ret = 0; + bool burn = false; + while (i < fr->n_glyphs) { fgc = &ft->glyphs[fr->glyph_indices[i]]; - if (PK_HAS_FLAG(fgc->flags, FONT_GLYPH_CHAR_FLAGS_NEW_LINE) == true) { + if (fgc->unicode == 10 || fgc->unicode == 13) { if (i+1 < fr->n_glyphs) { - if (fgc->unicode == 10) { - u = 11; - } else { - u = 10; - } - // handle \r\n - if (ft->glyphs[fr->glyph_indices[i+1]].unicode == u) { - i += 1; // burn + fgc2 = &ft->glyphs[fr->glyph_indices[i+1]]; + if (fgc->unicode != fgc2->unicode && (fgc2->unicode == 10 || fgc2->unicode == 13)) { + ++i; } } - ret += 1; + ++i; + if (burn == false) { + ret += 1; + } + burn = false; + continue; + } + FontType_Inner_LookAheadLineLength(ft, fr, i, font_glyph_spacing, &ii); + if (ii == 0) { + ++i; + continue; } + i += ii; + ret += 1; + burn = true; } return ret; } @@ -151,7 +196,7 @@ bool FontType_Inner_CalcTransforms(const FontType *ft, FontRender *fr, FontInsta assert(ft != nullptr); assert(fr != nullptr); assert(ptr_dst != nullptr); - FontGlyphChar *fgc; + FontGlyphChar *fgc, *fgc2; FontInstanceBufferItem *buf_item; bool new_word = true; uint32_t i, ii; @@ -206,32 +251,39 @@ bool FontType_Inner_CalcTransforms(const FontType *ft, FontRender *fr, FontInsta if (PK_HAS_FLAG(fgc->flags, FONT_GLYPH_CHAR_FLAGS_NEW_LINE) == true) { // 10 line feed '\n' // 13 carriage return '\r' - if ((fgc->unicode == 10 || fgc->unicode == 13) && i+1 < fr->n_glyphs) { - if ((ft->glyphs[fr->glyph_indices[i+1]].unicode == 10 || ft->glyphs[fr->glyph_indices[i+1]].unicode == 13) && ft->glyphs[fr->glyph_indices[i+1]].unicode != fgc->unicode) { + if (i+1 < fr->n_glyphs) { + fgc2 = &ft->glyphs[fr->glyph_indices[i+1]]; + if (fgc->unicode != fgc2->unicode && (fgc->unicode == 10 || fgc->unicode == 13) && (fgc2->unicode == 10 || fgc2->unicode == 13)) { i += 1;// burn /r/n and /n/r } } line_index += 1; + new_word = true; + cursor_head = 0; line_length = FontType_Inner_LookAheadLineLength(ft, fr, i+1, font_glyph_spacing); if (PK_HAS_FLAG(fr->settings.surface_area_type_flags, FONT_RENDER_SURFACE_AREA_TYPE_FLAGS_CENTER_HORIZONTAL)) { cursor_head = (fr->settings.surface_area_size.x - line_length) / 2.0; - } else { - cursor_head = 0; } continue; } if (PK_HAS_FLAG(fgc->flags, FONT_GLYPH_CHAR_FLAGS_WHITESPACE) == true) { - // x = 2 - // a = 4 - // 6 = 2 + 4 - cursor_head += fgc->advance * font_glyph_spacing; + // 2025-11-18 JCB TODO + // This is tricky. + // if we wrapped, we want to swallow any remaining spaces... Maybe? + // - YES a space between words + // - NO a tab? if (PK_HAS_FLAG(fgc->flags, FONT_GLYPH_CHAR_FLAGS_ALIGN_ADVANCE) == true) { // x2 = x - (a - (x % a)) // x2 = 6 - (4 - (6 & 4)) // x2 = 6 - (4 - 2) // x2 = 6 - (2) // x2 = 4 - cursor_head -= (ft->spacing.em_size * font_glyph_spacing) - fmod(cursor_head, (ft->spacing.em_size * font_glyph_spacing)); + cursor_head += (ft->spacing.em_size * font_glyph_spacing) - fmod(cursor_head, (ft->spacing.em_size * font_glyph_spacing)); + } else { + // x = 2 + // a = 4 + // 6 = 2 + 4 + cursor_head += fgc->advance * font_glyph_spacing; } new_word = true; continue; @@ -240,12 +292,11 @@ bool FontType_Inner_CalcTransforms(const FontType *ft, FontRender *fr, FontInsta float word_width = FontType_Inner_LookAheadWordLength(ft, fr, i, font_glyph_spacing); if (cursor_head + word_width > fr->settings.surface_area_size.x) { line_index += 1; - line_length = FontType_Inner_LookAheadLineLength(ft, fr, i+1, font_glyph_spacing); + cursor_head = 0; + line_length = FontType_Inner_LookAheadLineLength(ft, fr, i, font_glyph_spacing); if (PK_HAS_FLAG(fr->settings.surface_area_type_flags, FONT_RENDER_SURFACE_AREA_TYPE_FLAGS_CENTER_HORIZONTAL)) { cursor_head = (fr->settings.surface_area_size.x - line_length) / 2.0; - } else { - cursor_head = 0; } } new_word = false; @@ -421,6 +472,12 @@ void FontType_Tick(double delta) { pk_bkt_arr_iterate(&ft->renders, iter_fn.invoke, &iter_fn); ft->bindings.instance_counter = index; + /* early-out because vulkan is not initialized */ + if (window == NULL) { + b = pk_bkt_arr_iter_increment(&ftd.fonts, &ft); + continue; + } + // check recreate buffer // check against index because it's the real count // fibis.next includes whitespace & unhandled characters @@ -953,14 +1010,8 @@ FontTypeRender FontType_AddStringRender(FontTypeHandle font_type_handle, const p fr->settings = *settings; fr->text = str; - if (window == NULL) { - return ret; - } - // insert new characters into tmp buffer - pk_arr_t<uint32_t> glyph_indices{}; - glyph_indices.bkt = pkeSettings.mem_bkt.game_transient; - glyph_indices.next = 0; // hide garbage val warning + pk_arr_t<uint32_t> glyph_indices{pkeSettings.mem_bkt.game_transient}; pk_arr_reserve(&glyph_indices, str.length); FontType_cstr_to_unicode(*ft, glyph_indices, str); |
