summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2025-11-23 16:03:51 -0500
committerJonathan Bradley <jcb@pikum.xyz>2025-11-23 16:03:51 -0500
commit64db50cf0657c83740276c3869e4cb6304020cf6 (patch)
tree28e83ae4ec6abe239da4ff84e918963d7d5e7cf0
parent057734d30afad9d5facf3d7697c2bd5873f23fca (diff)
pke: chkpt: more font positioning work, add tests
-rw-r--r--src/font.cpp2
-rw-r--r--tests/pke-test-font.cpp66
2 files changed, 58 insertions, 10 deletions
diff --git a/src/font.cpp b/src/font.cpp
index fd35019..9b38d69 100644
--- a/src/font.cpp
+++ b/src/font.cpp
@@ -1034,7 +1034,6 @@ FontTypeRender FontType_AddStringRender(FontTypeHandle font_type_handle, const p
FontType_cstr_to_unicode(*ft, glyph_indices, str);
- // TODO specific bucket
count = glyph_indices.next;
fr->n_glyphs = count;
fr->glyph_indices = pk_new_arr<uint32_t>(count, ftd.bkt);
@@ -1084,7 +1083,6 @@ void FontType_UpdateStringRenderText(FontTypeRender ftr, pk_cstr &&cstr) {
FontType_cstr_to_unicode(*ft, glyph_indices, cstr);
- // TODO specific bucket
count = glyph_indices.next;
fr.n_glyphs = count;
fr.glyph_indices = pk_new_arr<uint32_t>(count, ftd.bkt);
diff --git a/tests/pke-test-font.cpp b/tests/pke-test-font.cpp
index dd078ab..7db06d4 100644
--- a/tests/pke-test-font.cpp
+++ b/tests/pke-test-font.cpp
@@ -6,16 +6,33 @@
#include "font.hpp"
#include "pk.h"
#include "pke-test-stubs.h"
+#include "static-plane.hpp"
#include "thread-pool.hpp"
+#include "window.hpp"
static pk_membucket *bkt;
+struct FontInstanceBufferItem {
+ glm::mat4 pos_scale;
+ glm::vec4 color_foreground;
+ glm::vec4 color_background;
+ glm::vec2 sprite_region_min;
+ glm::vec2 sprite_region_max;
+ glm::vec2 bounding_region_min;
+ glm::vec2 bounding_region_max;
+ float width;
+ float padding[3];
+};
+
float FontType_Inner_LookAheadWordLength(const FontType *const ft, const FontRender *const fr, uint32_t index, float font_glyph_spacing, uint32_t *char_count);
float FontType_Inner_LookAheadLineLength(const FontType *const ft, const FontRender *const fr, const uint32_t index, float font_glyph_spacing, uint32_t *char_count);
float FontType_Inner_LookAheadLineCount(const FontType *const ft, const FontRender *const fr, const uint32_t index, float font_glyph_spacing);
+bool FontType_Inner_CalcTransforms(const FontType *ft, FontRender *fr, FontInstanceBufferItem *ptr_dst, uint32_t *count);
void pke_test_font_setup() {
pke_test_stub_init_vulkan();
+ Extent.width = 1920;
+ Extent.height = 1080;
bkt = pk_mem_bucket_create("pke_test_font", PK_MEM_DEFAULT_BUCKET_SIZE, PK_MEMBUCKET_FLAG_NONE);
pk_mem_bucket_set_client_mem_bucket(bkt);
pk_ev_init(bkt);
@@ -407,17 +424,21 @@ int pke_test_font_007() {
FontType *ft;
FontRender *fr;
FontRenderSettings frs{};
- uint32_t u;
- float font_glyph_spacing;
+ uint32_t u = 0;
+ float font_glyph_spacing, tmp;
+ glm::vec4 coord;
pke_test_font_inner_func_params params[3];
+ pk_arr_t<FontInstanceBufferItem> fibis{};
params[0].index = 0;
params[1].index = 13;
params[2].index = 14;
const char *str = "cowabungalong\ttest";
+ pk_arr_resize(&fibis, strlen(str) + 2);
+ frs.surface_area_type_flags |= FONT_RENDER_SURFACE_AREA_TYPE_FLAGS_CENTER_BOTH;
frs.char_scale = 10;
- frs.surface_area_size = glm::vec2(75, 300);
- 1 = 0; // TODO
+ frs.surface_area_size = glm::vec2(80, 300);
+ // 1 = 0; // TODO
// there's something wrong with wrapping.
// The issue is that the first line doesn't re-center when the line wraps,
// but only for like one pixel.
@@ -425,8 +446,6 @@ int pke_test_font_007() {
// the logic that writes the fibis.
// The fact it's a 1-pixel error makes me think it's a `<` vs `<=` thing, but
// it isn't jumping out at me. - Perhaps a different "off-by-one" scenario.
- // I may need to refactor the font logic that builds fibis to happen in a
- // separate function so that I can add it to the asserts here in the tests.
// I should also consider refactoring the "inner" funcs to just write the
// fibis as we go (sans centering). I would need to track:
// - index of where new lines start
@@ -437,12 +456,14 @@ int pke_test_font_007() {
handle_001 = FontType_AddStringRender(fti, std::move(cstring_to_pk_cstr(str)), &frs);
- FontType_Tick(0.f);
-
ft = FontType_Get(fti);
fr = FontType_GetFontRender(handle_001);
+ // FontType_Tick(0.f);
+ FontType_Inner_CalcTransforms(ft, fr, &fibis[0], &u);
+
PK_TEST_ASSERT_EQ_RET(18, fr->n_glyphs);
+ PK_TEST_ASSERT_EQ_RET(17, u);
font_glyph_spacing = ft->spacing.em_size * fr->settings.char_scale;
@@ -485,6 +506,35 @@ int pke_test_font_007() {
PK_TEST_ASSERT_EQ_RET (1.f, params[2].val);
}
+ // render & fibi stuff
+ {
+ /*
+ {-1, -1},
+ { 1, -1},
+ { 1, 1},
+ {-1, 1},
+ */
+ // top left
+ // ~6 (padding) == (80 (box size) - 74.02)
+ // ~3 padding on each side to center
+ coord = fibis[0].pos_scale * glm::vec4(pkeIntrinsicsPlane.vert[0], 0, 1);
+ coord += glm::vec4(1.f, 1.f, 0.f, 0.f);
+ coord *= glm::vec4(1920.f/2.f, 1080.f/2.f, 0.f, 1.f);
+ PK_TEST_ASSERT_GTE_RET(2, coord.x);
+ PK_TEST_ASSERT_LTE_RET(3, coord.x);
+ tmp = coord.x;
+
+ // bottom right
+ coord = fibis[12].pos_scale * glm::vec4(pkeIntrinsicsPlane.vert[2], 0, 1);
+ coord += glm::vec4(1.f, 1.f, 0.f, 0.f);
+ coord *= glm::vec4(1920.f/2.f, 1080.f/2.f, 0.f, 1.f);
+ PK_TEST_ASSERT_GTE_RET(tmp-0.1, frs.surface_area_size.x-coord.x);
+ PK_TEST_ASSERT_LTE_RET(tmp+0.1, frs.surface_area_size.x-coord.x);
+ PK_TEST_ASSERT_GTE_RET(frs.surface_area_size.x-3, coord.x);
+ PK_TEST_ASSERT_LTE_RET(frs.surface_area_size.x-2, coord.x);
+ }
+
+
return 0;
}