summaryrefslogtreecommitdiff
path: root/src/font.cpp
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2025-02-20 15:01:19 -0500
committerJonathan Bradley <jcb@pikum.xyz>2025-02-20 15:01:19 -0500
commit09989000cd5787578c0676b279d3ecbc9ca50524 (patch)
treefd9b4c26bf1a2d33698a9084111cef7d6d53622e /src/font.cpp
parent379c2063a639d57d7ce4c71762eae8259b989729 (diff)
pke: glyph word-wrap on whitespace
Diffstat (limited to 'src/font.cpp')
-rw-r--r--src/font.cpp26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/font.cpp b/src/font.cpp
index e905da3..6357a1d 100644
--- a/src/font.cpp
+++ b/src/font.cpp
@@ -89,12 +89,27 @@ 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) {
+ uint32_t i;
+ float ret = 0;
+ FontGlyphChar *fgc;
+ for (i = index; i < fr->n_glyphs; ++i) {
+ fgc = &ft->glyphs[fr->glyph_indices[i]];
+ if (fgc->is_whitespace == true) {
+ break;
+ }
+ ret += fgc->advance * font_glyph_spacing;
+ }
+ return ret;
+}
+
void FontType_Inner_CalcTransforms(const FontType *ft, FontRender *fr, FontInstanceBufferItem *ptr_dst) {
assert(ft != nullptr);
assert(fr != nullptr);
assert(ptr_dst != nullptr);
FontGlyphChar *fgc;
FontInstanceBufferItem *buf_item;
+ bool new_word = true;
uint32_t i;
float font_glyph_spacing;
float cursor_head, line_index, line_height;
@@ -119,8 +134,17 @@ void FontType_Inner_CalcTransforms(const FontType *ft, FontRender *fr, FontInsta
if (fgc->is_whitespace == true) {
cursor_head += fgc->advance * font_glyph_spacing;
+ new_word = true;
continue;
}
+ if (new_word == true) {
+ 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;
+ cursor_head = 0;
+ }
+ new_word = false;
+ }
// left, bottom, right, top
// x, y, z, w
@@ -151,7 +175,7 @@ void FontType_Inner_CalcTransforms(const FontType *ft, FontRender *fr, FontInsta
// box position
translate.y = fr->settings.surface_area_pos.y;
// baseline - current line (+1 to not draw above the box)
- translate.y -= ((line_index + 1) * line_height);
+ translate.y += ((line_index + 1) * line_height);
// places the top line of the glyph on the baseline
translate.y += (font_glyph_spacing / 2.0) + (glyph_size.y / 2.0);
// move glyph to the height relative to the baseline (cursor)