From ec8c16abfbd89dfca6241a2565dded76e285f8c8 Mon Sep 17 00:00:00 2001 From: Victoria Lease Date: Tue, 26 Feb 2013 11:18:42 -0800 Subject: [PATCH] round subpixels up to next integer in measureText A common source of layout bugs we're seeing these days involves the output of measureText() being fed into StaticLayout's constructor. measureText() returns subpixel-accurate values, but StaticLayout only takes integral bounds, resulting in the subpixel portion of the bounds being truncated. This leaves StaticLayout with insufficient space to layout the text that was just measured, causing all manner of unexpected line breaks. This could be causing issues elsewhere, as well. Until our text pipeline is fully subpixel-perfect, it's best that measureText guarantee that the value it returns will be sufficient to contain the text, even if cast to int. Bug: 8164205 Change-Id: Ib84947f0d0a1229287f5b19b99e7efd40f5317f7 --- graphics/java/android/graphics/Paint.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/graphics/java/android/graphics/Paint.java b/graphics/java/android/graphics/Paint.java index 7d99fece6f35e..8da20f2caae22 100644 --- a/graphics/java/android/graphics/Paint.java +++ b/graphics/java/android/graphics/Paint.java @@ -1197,14 +1197,14 @@ public class Paint { return 0f; } if (!mHasCompatScaling) { - return native_measureText(text, index, count); + return (float) Math.ceil(native_measureText(text, index, count)); } final float oldSize = getTextSize(); setTextSize(oldSize*mCompatScaling); float w = native_measureText(text, index, count); setTextSize(oldSize); - return w*mInvCompatScaling; + return (float) Math.ceil(w*mInvCompatScaling); } private native float native_measureText(char[] text, int index, int count); @@ -1229,14 +1229,14 @@ public class Paint { return 0f; } if (!mHasCompatScaling) { - return native_measureText(text, start, end); + return (float) Math.ceil(native_measureText(text, start, end)); } final float oldSize = getTextSize(); setTextSize(oldSize*mCompatScaling); float w = native_measureText(text, start, end); setTextSize(oldSize); - return w*mInvCompatScaling; + return (float) Math.ceil(w*mInvCompatScaling); } private native float native_measureText(String text, int start, int end); @@ -1256,12 +1256,14 @@ public class Paint { return 0f; } - if (!mHasCompatScaling) return native_measureText(text); + if (!mHasCompatScaling) { + return (float) Math.ceil(native_measureText(text)); + } final float oldSize = getTextSize(); setTextSize(oldSize*mCompatScaling); float w = native_measureText(text); setTextSize(oldSize); - return w*mInvCompatScaling; + return (float) Math.ceil(w*mInvCompatScaling); } private native float native_measureText(String text);