summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--editor/editor.cpp56
-rw-r--r--src/font.cpp27
-rw-r--r--src/font.hpp9
-rw-r--r--src/game.cpp2
4 files changed, 73 insertions, 21 deletions
diff --git a/editor/editor.cpp b/editor/editor.cpp
index e3f6b7c..28798d5 100644
--- a/editor/editor.cpp
+++ b/editor/editor.cpp
@@ -715,12 +715,39 @@ int SortFontGlyphChar(const void *a, const void *b) {
}
void GenerateMTSDF(FontTypeMSDFSettings *msdf_settings, const Asset *a) {
- static const uint64_t REQUIRED_UNICODE_CHAR_LENGTH = 3;
- static uint32_t required_unicode_codes[REQUIRED_UNICODE_CHAR_LENGTH] = {
- 9, // horizontal tab
- 10, // line feed
- 13, // carriage return
- };
+ static pk_arr *required_font_glyph_chars_arr;
+ required_font_glyph_chars_arr = pk_new<pk_arr>(pkeSettings.mem.bkt);
+ required_font_glyph_chars_arr->next = 0;
+ required_font_glyph_chars_arr->reserved = 0;
+ required_font_glyph_chars_arr->stride = sizeof(FontGlyphChar);
+ required_font_glyph_chars_arr->alignment = alignof(FontGlyphChar);
+ required_font_glyph_chars_arr->bkt = pkeSettings.mem.bkt;
+ required_font_glyph_chars_arr->data = nullptr;
+ pk_arr_reserve(required_font_glyph_chars_arr, 3);
+
+ FontGlyphChar fgc{};
+ fgc.advance = 0;
+ fgc.sprite_region_min = glm::vec2(0);
+ fgc.sprite_region_max = glm::vec2(0);
+ fgc.plane_bounds = glm::vec4(0);
+ fgc.unicode = 0;
+ fgc.flags = FONT_GLYPH_CHAR_FLAGS_NONE;
+ // 9 horizontal tab
+ fgc.unicode = 9;
+ fgc.advance = 2;
+ fgc.flags = (FONT_GLYPH_CHAR_FLAGS)(FONT_GLYPH_CHAR_FLAGS_WHITESPACE | FONT_GLYPH_CHAR_FLAGS_ALIGN_ADVANCE);
+ pk_arr_append(required_font_glyph_chars_arr, &fgc);
+ // 10 line feed
+ fgc.unicode = 10;
+ fgc.advance = 0;
+ fgc.flags = FONT_GLYPH_CHAR_FLAGS_NEW_LINE;
+ pk_arr_append(required_font_glyph_chars_arr, &fgc);
+ // 13 carriage return
+ fgc.unicode = 13;
+ fgc.advance = 0;
+ fgc.flags = FONT_GLYPH_CHAR_FLAGS_NEW_LINE;
+ pk_arr_append(required_font_glyph_chars_arr, &fgc);
+
uint64_t found_unicode_map = 0;
assert(PK_HAS_FLAG(a->type, PKE_ASSET_TYPE_FONT));
FontTypeSpacing ft_spacing{};
@@ -800,13 +827,17 @@ void GenerateMTSDF(FontTypeMSDFSettings *msdf_settings, const Asset *a) {
arr_glyphs.stride = sizeof(FontGlyphChar);
pk_arr_resize(&arr_glyphs, glyphs.size());
FontGlyphChar *arr = reinterpret_cast<FontGlyphChar *>(arr_glyphs.data);
+ FontGlyphChar *req_arr = reinterpret_cast<FontGlyphChar *>(required_font_glyph_chars_arr->data);
for (uint64_t i = 0; i < glyphs.size(); ++i) {
arr[i].advance = glyphs[i].getAdvance();
arr[i].unicode = glyphs[i].getCodepoint();
- arr[i].is_whitespace = glyphs[i].isWhitespace();
+ arr[i].flags = FONT_GLYPH_CHAR_FLAGS_NONE;
+ if (glyphs[i].isWhitespace()) {
+ arr[i].flags = FONT_GLYPH_CHAR_FLAGS_WHITESPACE;
+ }
- for (uint64_t k = 0; k < REQUIRED_UNICODE_CHAR_LENGTH; ++k) {
- if (arr[i].unicode == required_unicode_codes[k]) {
+ for (uint32_t k = 0; k < required_font_glyph_chars_arr->next; ++k) {
+ if (arr[i].unicode == req_arr[k].unicode) {
found_unicode_map |= (1ull << k);
break;
}
@@ -821,14 +852,11 @@ void GenerateMTSDF(FontTypeMSDFSettings *msdf_settings, const Asset *a) {
arr[i].sprite_region_max = glm::vec2(dbounds.z, height - dbounds.y);
}
- for (uint64_t k = 0; k < REQUIRED_UNICODE_CHAR_LENGTH; ++k) {
+ for (uint32_t k = 0; k < required_font_glyph_chars_arr->next; ++k) {
if ((found_unicode_map & (1ull << k)) != 0) {
continue;
}
- FontGlyphChar fgc{};
- memset(&fgc, 0, sizeof(FontGlyphChar));
- fgc.unicode = required_unicode_codes[k];
- pk_arr_append(&arr_glyphs, &fgc);
+ pk_arr_append(&arr_glyphs, &req_arr[k]);
}
qsort(arr_glyphs.data, arr_glyphs.next, arr_glyphs.stride, SortFontGlyphChar);
diff --git a/src/font.cpp b/src/font.cpp
index bf03d1a..30877f2 100644
--- a/src/font.cpp
+++ b/src/font.cpp
@@ -95,7 +95,7 @@ float FontType_Inner_LookAheadWordLength(const FontType *const ft, const FontRen
FontGlyphChar *fgc;
for (i = index; i < fr->n_glyphs; ++i) {
fgc = &ft->glyphs[fr->glyph_indices[i]];
- if (fgc->is_whitespace == true) {
+ if (PK_HAS_FLAG(fgc->flags, FONT_GLYPH_CHAR_FLAGS_WHITESPACE) == true) {
break;
}
ret += fgc->advance * font_glyph_spacing;
@@ -132,8 +132,31 @@ void FontType_Inner_CalcTransforms(const FontType *ft, FontRender *fr, FontInsta
scale = glm::vec3(1);
fgc = &ft->glyphs[fr->glyph_indices[i]];
- if (fgc->is_whitespace == true) {
+ 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) {
+ i += 1;// burn /r/n and /n/r
+ }
+ }
+ line_index += 1;
+ 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;
+ 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));
+ }
new_word = true;
continue;
}
diff --git a/src/font.hpp b/src/font.hpp
index 2d1e55a..09ed87a 100644
--- a/src/font.hpp
+++ b/src/font.hpp
@@ -11,10 +11,11 @@ TypeSafeInt_H(FontTypeIndex, uint16_t, 0xFFFF);
TypeSafeInt_H(FontRenderIndex, uint16_t, 0xFFFF);
enum FONT_GLYPH_CHAR_FLAGS : uint8_t {
- FONT_GLYPH_CHAR_FLAGS_NONE = (0 << 0),
- FONT_GLYPH_CHAR_FLAGS_CONTROL = (1 << 0),
- FONT_GLYPH_CHAR_FLAGS_WHITESPACE = (1 << 1),
- FONT_GLYPH_CHAR_FLAGS_NEW_LINE = (1 << 2),
+ FONT_GLYPH_CHAR_FLAGS_NONE = (0 << 0),
+ FONT_GLYPH_CHAR_FLAGS_CONTROL = (1 << 0),
+ FONT_GLYPH_CHAR_FLAGS_WHITESPACE = (1 << 1),
+ FONT_GLYPH_CHAR_FLAGS_ALIGN_ADVANCE = (1 << 2),
+ FONT_GLYPH_CHAR_FLAGS_NEW_LINE = (1 << 3),
};
enum FONT_RENDER_SURFACE_AREA_TYPE_FLAGS : uint8_t {
diff --git a/src/game.cpp b/src/game.cpp
index 85c2fd5..9a52e1b 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -609,7 +609,7 @@ void Game_Main(PKEWindowProperties windowProps, const char *executablePath) {
}
}
- pk_cstr test_text = cstring_to_pk_cstr("012\n3456789\tThe quick brown fox jumped over the lazy dog.");
+ pk_cstr test_text = cstring_to_pk_cstr("012\n3456789\tThe quick\r\nbrown fox jumped over the lazy dog.");
// pk_cstr test_text = cstring_to_pk_cstr("%+-*0123456789$");
// pk_cstr test_text = cstring_to_pk_cstr("$#");
// pk_cstr test_text = cstring_to_pk_cstr("$");