From 72663aeefb3bf8c794a1fe84f53ba6ff2488e0e8 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 25 Sep 2021 07:58:28 -0700 Subject: [PATCH 1/3] Android: Improve precision on text measure. We want this to come to the same values that measuring the first line would, so let's multiply in the same way. --- Common/Render/Text/draw_text_android.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Common/Render/Text/draw_text_android.cpp b/Common/Render/Text/draw_text_android.cpp index 7d0a1e2623..2f55694c5a 100644 --- a/Common/Render/Text/draw_text_android.cpp +++ b/Common/Render/Text/draw_text_android.cpp @@ -125,8 +125,8 @@ void TextDrawerAndroid::MeasureStringRect(const char *str, size_t len, const Bou auto env = getEnv(); std::vector lines; SplitString(toMeasure, '\n', lines); - float total_w = 0.0f; - float total_h = 0.0f; + int total_w = 0; + int total_h = 0; for (size_t i = 0; i < lines.size(); i++) { CacheKey key{ lines[i], fontHash_ }; @@ -148,13 +148,13 @@ void TextDrawerAndroid::MeasureStringRect(const char *str, size_t len, const Bou } entry->lastUsedFrame = frameCount_; - if (total_w < entry->width * fontScaleX_) { - total_w = entry->width * fontScaleX_; + if (total_w < entry->width) { + total_w = entry->width; } - total_h += entry->height * fontScaleY_; + total_h += entry->height; } - *w = total_w * dpiScale_; - *h = total_h * dpiScale_; + *w = total_w * fontScaleX_ * dpiScale_; + *h = total_h * fontScaleY_ * dpiScale_; } void TextDrawerAndroid::DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, int align) { From d9b39eed023c3cc1dd75cf4eb97ba5e18254f12b Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 25 Sep 2021 07:59:18 -0700 Subject: [PATCH 2/3] UI: Avoid math on calculated value width. If we add an integer and then subtract an integer, our result may change slightly, causing wrapping. --- Common/UI/UIScreen.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Common/UI/UIScreen.cpp b/Common/UI/UIScreen.cpp index 58a5b41b62..15215148af 100644 --- a/Common/UI/UIScreen.cpp +++ b/Common/UI/UIScreen.cpp @@ -830,14 +830,14 @@ void AbstractChoiceWithValueDisplay::Draw(UIContext &dc) { float availWidth = (bounds_.w - paddingX * 2) * 0.8f; float scale = CalculateValueScale(dc, valueText, availWidth); - float ignore; + float w, h; Bounds availBounds(0, 0, availWidth, bounds_.h); - dc.MeasureTextRect(dc.theme->uiFont, scale, scale, valueText.c_str(), (int)valueText.size(), availBounds, &textPadding_.right, &ignore, ALIGN_RIGHT | ALIGN_VCENTER | FLAG_WRAP_TEXT); - textPadding_.right += paddingX; + dc.MeasureTextRect(dc.theme->uiFont, scale, scale, valueText.c_str(), (int)valueText.size(), availBounds, &w, &h, ALIGN_RIGHT | ALIGN_VCENTER | FLAG_WRAP_TEXT); + textPadding_.right = w + paddingX; Choice::Draw(dc); dc.SetFontScale(scale, scale); - Bounds valueBounds(bounds_.x2() - textPadding_.right, bounds_.y, textPadding_.right - paddingX, bounds_.h); + Bounds valueBounds(bounds_.x2() - textPadding_.right, bounds_.y, w, bounds_.h); dc.DrawTextRect(valueText.c_str(), valueBounds, style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER | FLAG_WRAP_TEXT); dc.SetFontScale(1.0f, 1.0f); } From 799a4de23e0f104adbc51e9f407ca76cc15c6ffe Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 25 Sep 2021 08:02:58 -0700 Subject: [PATCH 3/3] Windows: Improve rect measurement precision. Not seeing issues here like Android, but let's make them match. --- Common/Render/Text/draw_text_uwp.cpp | 14 +++++++------- Common/Render/Text/draw_text_win.cpp | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Common/Render/Text/draw_text_uwp.cpp b/Common/Render/Text/draw_text_uwp.cpp index d12a41275f..5d68e77610 100644 --- a/Common/Render/Text/draw_text_uwp.cpp +++ b/Common/Render/Text/draw_text_uwp.cpp @@ -264,8 +264,8 @@ void TextDrawerUWP::MeasureStringRect(const char *str, size_t len, const Bounds std::vector lines; SplitString(toMeasure, '\n', lines); - float total_w = 0.0f; - float total_h = 0.0f; + int total_w = 0; + int total_h = 0; for (size_t i = 0; i < lines.size(); i++) { CacheKey key{ lines[i], fontHash_ }; @@ -304,13 +304,13 @@ void TextDrawerUWP::MeasureStringRect(const char *str, size_t len, const Bounds } entry->lastUsedFrame = frameCount_; - if (total_w < entry->width * fontScaleX_) { - total_w = entry->width * fontScaleX_; + if (total_w < entry->width) { + total_w = entry->width; } - total_h += entry->height * fontScaleY_; + total_h += entry->height; } - *w = total_w * dpiScale_; - *h = total_h * dpiScale_; + *w = total_w * fontScaleX_ * dpiScale_; + *h = total_h * fontScaleY_ * dpiScale_; } void TextDrawerUWP::DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, int align) { diff --git a/Common/Render/Text/draw_text_win.cpp b/Common/Render/Text/draw_text_win.cpp index 1a79f6a192..d5af5af762 100644 --- a/Common/Render/Text/draw_text_win.cpp +++ b/Common/Render/Text/draw_text_win.cpp @@ -166,8 +166,8 @@ void TextDrawerWin32::MeasureStringRect(const char *str, size_t len, const Bound std::vector lines; SplitString(toMeasure, '\n', lines); - float total_w = 0.0f; - float total_h = 0.0f; + int total_w = 0; + int total_h = 0; for (size_t i = 0; i < lines.size(); i++) { CacheKey key{ lines[i], fontHash_ }; @@ -187,15 +187,15 @@ void TextDrawerWin32::MeasureStringRect(const char *str, size_t len, const Bound } entry->lastUsedFrame = frameCount_; - if (total_w < entry->width * fontScaleX_) { - total_w = entry->width * fontScaleX_; + if (total_w < entry->width) { + total_w = entry->width; } int h = i == lines.size() - 1 ? entry->height : metrics.tmHeight + metrics.tmExternalLeading; - total_h += h * fontScaleY_; + total_h += h; } - *w = total_w * dpiScale_; - *h = total_h * dpiScale_; + *w = total_w * fontScaleX_ * dpiScale_; + *h = total_h * fontScaleY_ * dpiScale_; } void TextDrawerWin32::DrawStringBitmap(std::vector &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, int align) {