Fix DynamicLayout last line spacing
StaticLayout did not work as expected when there is a line ending with
'\n'. For those cases it would calculate 2 lines, but would
discard line spacing for both of them. This CL fixed that issue. Also
DynamicLayout asks StaticLayout not to add line spacing for the last
line of the text.
Test: Manual/Visual test
Test: bit -t FrameworksCoreTests:android.text.StaticLayoutTest
Test: bit -t FrameworksCoreTests:android.text.StaticLayoutTextMeasuringTest
Test: bit -t FrameworksCoreTests:android.text.StaticLayoutLineBreakingTest
Test: bit -t FrameworksCoreTests:android.text.DynamicLayoutTest
Test: bit -t FrameworksCoreTests:android.text.DynamicLayoutBlocksTest
Test: bit -t FrameworksCoreTests:android.widget.TextViewTest
Test: bit -t FrameworksCoreTests:android.widget.EditorCursorTest
Test: bit -t CtsTextTestCases:android.text.cts.StaticLayoutTest
Test: bit -t CtsTextTestCases:android.text.cts.DynamicLayoutTest
Test: bit -t CtsWidgetTestCases:android.widget.cts.TextViewTest
Test: bit -t CtsWidgetTestCases:android.widget.cts.EditTextTest
Bug: 25194907
Change-Id: I0d901ecf768b25faea156b73097541da4b93d6d0
diff --git a/core/java/android/text/StaticLayout.java b/core/java/android/text/StaticLayout.java
index 4eb74a7..3474c2b 100644
--- a/core/java/android/text/StaticLayout.java
+++ b/core/java/android/text/StaticLayout.java
@@ -981,7 +981,20 @@
}
}
- boolean lastLine = mEllipsized || (end == bufEnd);
+ final boolean lastLine;
+ if (mEllipsized) {
+ lastLine = true;
+ } else {
+ final boolean lastCharIsNewLine = widthStart != bufEnd && bufEnd > 0
+ && text.charAt(bufEnd - 1) == CHAR_NEW_LINE;
+ if (end == bufEnd && !lastCharIsNewLine) {
+ lastLine = true;
+ } else if (start == bufEnd && lastCharIsNewLine) {
+ lastLine = true;
+ } else {
+ lastLine = false;
+ }
+ }
if (firstLine) {
if (trackPad) {