From 7761d2366b1622726ed666957085daf7573cff70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 23 Mar 2018 10:48:54 +0100 Subject: [PATCH] Android: Prevent a text bitmap size from becoming 0 on '\r\n'. Fixes #10764 (unless there are more platforms where it's broken...) --- .../src/org/ppsspp/ppsspp/TextRenderer.java | 22 ++++++++++++++----- ext/native/gfx_es2/draw_text_android.cpp | 9 ++++++-- ext/native/gfx_es2/draw_text_win.cpp | 3 ++- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/android/src/org/ppsspp/ppsspp/TextRenderer.java b/android/src/org/ppsspp/ppsspp/TextRenderer.java index 8c794a56a8c8..e2e4c37bef38 100644 --- a/android/src/org/ppsspp/ppsspp/TextRenderer.java +++ b/android/src/org/ppsspp/ppsspp/TextRenderer.java @@ -26,12 +26,17 @@ public static void init(Context ctx) { } } private static Point measureLine(String string, double textSize) { - p.setTextSize((float)textSize); - int w = (int)p.measureText(string); + int w; + if (string.length() > 0) { + p.setTextSize((float)textSize); + w = (int)p.measureText(string); + // Round width up to even already here to avoid annoyances from odd-width 16-bit textures which + // OpenGL does not like - each line must be 4-byte aligned + w = (w + 5) & ~1; + } else { + w = 1; + } int h = (int)(p.descent() - p.ascent() + 2.0f); - // Round width up to even already here to avoid annoyances from odd-width 16-bit textures which - // OpenGL does not like - each line must be 4-byte aligned - w = (w + 5) & ~1; Point p = new Point(); p.x = w; p.y = h; @@ -57,6 +62,10 @@ public static int[] renderText(String string, double textSize) { int w = s.x; int h = s.y; + if (w == 0) + w = 1; + if (h == 0) + h = 1; Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bmp); @@ -65,7 +74,8 @@ public static int[] renderText(String string, double textSize) { String lines[] = string.replaceAll("\\r", "").split("\n"); float y = 1.0f; for (String line : lines) { - canvas.drawText(line, 1, -p.ascent() + y, p); + if (line.length() > 0) + canvas.drawText(line, 1, -p.ascent() + y, p); y += p.descent() - p.ascent(); } diff --git a/ext/native/gfx_es2/draw_text_android.cpp b/ext/native/gfx_es2/draw_text_android.cpp index 1bc3bc6639a1..f80994edd26a 100644 --- a/ext/native/gfx_es2/draw_text_android.cpp +++ b/ext/native/gfx_es2/draw_text_android.cpp @@ -184,8 +184,13 @@ void TextDrawerAndroid::DrawString(DrawBuffer &target, const char *str, float x, jstring jstr = env_->NewStringUTF(text.c_str()); uint32_t textSize = env_->CallStaticIntMethod(cls_textRenderer, method_measureText, jstr, size); - int imageWidth = (textSize >> 16); - int imageHeight = (textSize & 0xFFFF); + int imageWidth = (short)(textSize >> 16); + int imageHeight = (short)(textSize & 0xFFFF); + if (imageWidth <= 0) + imageWidth = 1; + if (imageHeight <= 0) + imageHeight = 1; + jintArray imageData = (jintArray)env_->CallStaticObjectMethod(cls_textRenderer, method_renderText, jstr, size); env_->DeleteLocalRef(jstr); diff --git a/ext/native/gfx_es2/draw_text_win.cpp b/ext/native/gfx_es2/draw_text_win.cpp index 1cae07f79530..5bc066f29787 100644 --- a/ext/native/gfx_es2/draw_text_win.cpp +++ b/ext/native/gfx_es2/draw_text_win.cpp @@ -232,7 +232,8 @@ void TextDrawerWin32::DrawString(DrawBuffer &target, const char *str, float x, f if (size.cy > MAX_TEXT_HEIGHT) size.cy = MAX_TEXT_HEIGHT; // Prevent zero-sized textures, which can occur. Not worth to avoid - // creating the texture altogether in this case. + // creating the texture altogether in this case. One example is a string + // containing only '\r\n', see issue #10764. if (size.cx == 0) size.cx = 1; if (size.cy == 0)