Skip to content

Commit

Permalink
Android: Prevent a text bitmap size from becoming 0 on '\r\n'. Fixes #…
Browse files Browse the repository at this point in the history
…10764 (unless there are more platforms where it's broken...)
  • Loading branch information
hrydgard committed Mar 23, 2018
1 parent fb798cf commit 7761d23
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
22 changes: 16 additions & 6 deletions android/src/org/ppsspp/ppsspp/TextRenderer.java
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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();
}

Expand Down
9 changes: 7 additions & 2 deletions ext/native/gfx_es2/draw_text_android.cpp
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion ext/native/gfx_es2/draw_text_win.cpp
Expand Up @@ -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)
Expand Down

0 comments on commit 7761d23

Please sign in to comment.