summaryrefslogtreecommitdiff
path: root/src/font.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/font.cpp')
-rw-r--r--src/font.cpp50
1 files changed, 34 insertions, 16 deletions
diff --git a/src/font.cpp b/src/font.cpp
index cce9cc4..fd35019 100644
--- a/src/font.cpp
+++ b/src/font.cpp
@@ -129,22 +129,26 @@ float FontType_Inner_LookAheadLineLength(const FontType *const ft, const FontRen
}
// 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) {
+ sz = fgc->advance * font_glyph_spacing;
+ if (ret + sz > fr->settings.surface_area_size.x) {
+ break;
+ }
+ ret += sz;
+ // burn the front - but only if we wrapped
+ fgc2 = nullptr;
+ if (i != 0) {
+ fgc2 = &ft->glyphs[fr->glyph_indices[i-1]];
+ if (!PK_HAS_FLAG(fgc2->flags, FONT_GLYPH_CHAR_FLAGS_NEW_LINE)) {
+ fgc2 = nullptr;
+ }
+ }
+ if (i == index && fgc2 != nullptr) {
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;
}
// -1 because the for loop is about to +1.
@@ -153,6 +157,20 @@ float FontType_Inner_LookAheadLineLength(const FontType *const ft, const FontRen
i += ii-1;
ret += sz;
}
+ // If we are at the end of a line:
+ // Burn all white-space until we have a non-white-space
+ // subtract the extra width of `advance` (char spacing, don't need it)
+ ii = 1;
+ while (i != 0 && i - ii >= index && i - ii > 0) {
+ fgc = &ft->glyphs[fr->glyph_indices[i-ii]];
+ ret -= fgc->advance * font_glyph_spacing;
+ if (PK_HAS_FLAG(fgc->flags, FONT_GLYPH_CHAR_FLAGS_WHITESPACE)) {
+ ii += 1;
+ continue;
+ }
+ ret += (fgc->plane_bounds.z - fgc->plane_bounds.x) * font_glyph_spacing;
+ break;
+ }
if (char_count != nullptr) {
*char_count = i - index;
}
@@ -236,11 +254,8 @@ bool FontType_Inner_CalcTransforms(const FontType *ft, FontRender *fr, FontInsta
cursor_head = (fr->settings.surface_area_size.x - line_length) / 2.0;
}
if (PK_HAS_FLAG(fr->settings.surface_area_type_flags, FONT_RENDER_SURFACE_AREA_TYPE_FLAGS_CENTER_VERTICAL)) {
- float text_height = FontType_Inner_LookAheadLineCount(ft, fr, 0) * line_height;
- // TODO
- // This is wrong but I'm not sure how/why.
- // Mathematically it should be ` / 2.0`.
- line_offset += (fr->settings.surface_area_size.y - text_height) / 4.0;
+ float text_height = FontType_Inner_LookAheadLineCount(ft, fr, 0, font_glyph_spacing) * line_height;
+ line_offset += (fr->settings.surface_area_size.y - text_height) / 2.0;
}
for (i = 0, ii = 0; i < fr->n_glyphs; ++i) {
@@ -331,7 +346,10 @@ bool 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);
+ // - first line ignore line height, just use EM
+ translate.y += font_glyph_spacing;
+ translate.y += (line_index * line_height);
+ // - if vertically centered
translate.y += line_offset;
// places the top line of the glyph on the baseline
translate.y += (font_glyph_spacing / 2.0) + (glyph_size.y / 2.0);