From c2339dc83b05618789a2bc3954c9093ad0c8a73b Mon Sep 17 00:00:00 2001 From: Benau Date: Fri, 14 Jun 2019 20:39:30 +0800 Subject: [PATCH] Make too long broken text draw as fit as possible --- src/font/font_with_face.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/font/font_with_face.cpp b/src/font/font_with_face.cpp index 19ecd0983..8d2eaff43 100644 --- a/src/font/font_with_face.cpp +++ b/src/font/font_with_face.cpp @@ -598,12 +598,22 @@ void FontWithFace::render(const std::vector& gl, if (width_per_line.empty()) return; + bool too_long_broken_text = false; + float next_line_height = m_font_max_height * scale; + if (width_per_line.size() > 1 && + width_per_line.size() * next_line_height > position.getHeight()) + { + // Make too long broken text draw as fit as possible + next_line_height = (float)position.getHeight() / width_per_line.size(); + too_long_broken_text = true; + } + // The offset must be round to integer when setting the offests // or * m_inverse_shaping, so the glyph is drawn without blurring effects if (hcenter || vcenter || clip) { text_dimension = gui::getGlyphLayoutsDimension( - gl, m_font_max_height * scale, m_inverse_shaping, scale); + gl, next_line_height, m_inverse_shaping, scale); if (hcenter) { @@ -612,8 +622,13 @@ void FontWithFace::render(const std::vector& gl, } if (vcenter) { - offset.Y += (s32)( - (position.getHeight() - text_dimension.Height) / 2.0f); + if (too_long_broken_text) + offset.Y -= (m_font_max_height - m_glyph_max_height) * scale; + else + { + offset.Y += (s32)( + (position.getHeight() - text_dimension.Height) / 2.0f); + } } if (clip) { @@ -645,7 +660,7 @@ void FontWithFace::render(const std::vector& gl, if ((glyph_layout.flags & gui::GLF_NEWLINE) != 0) { // Y doesn't matter because we don't use advance y in harfbuzz - offset.Y += m_font_max_height * scale; + offset.Y += next_line_height; cur_line++; line_changed = true; continue;