Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1beb2830 authored by chenjean's avatar chenjean
Browse files

fix(HCT): text not being centered within the background

The root cause is that line breaks with trailing spaces create extra blank areas. We need a pre-emptive check for spaces; if a space is found, its background should not be rendered. Spaces in the middle of a line will have the background drawn along with the subsequent text, while spaces at the end of a line, the line will have a rectangle drawn using the onEnd method.

Bug: 394118810
Flag: com.android.graphics.hwui.flags.high_contrast_text_small_text_rect
Test: atest core/tests/coretests/src/android/text/LayoutTest.java
Change-Id: Ibf805b5e6681963f17a4df66853f83f8219c1302
parent 107fe807
Loading
Loading
Loading
Loading
+16 −6
Original line number Diff line number Diff line
@@ -1065,13 +1065,13 @@ public abstract class Layout {
                        var newBackground = determineContrastingBackgroundColor(index);
                        var hasBgColorChanged = newBackground != bgPaint.getColor();

                        if (lineNum != mLastLineNum || hasBgColorChanged) {
                        // Skip processing if the character is a space or a tap to avoid
                        // rendering an abrupt, empty rectangle.
                            if (Character.isWhitespace(mText.charAt(index))) {
                        if (TextLine.isLineEndSpace(mText.charAt(index))) {
                            return;
                        }

                        if (lineNum != mLastLineNum || hasBgColorChanged) {
                            // Draw what we have so far, then reset the rect and update its color
                            drawRect();
                            mLineBackground.set(left, top, right, bottom);
@@ -4620,6 +4620,16 @@ public abstract class Layout {
     * Callback for {@link #forEachCharacterBounds(int, int, int, int, CharacterBoundsListener)}
     */
    private interface CharacterBoundsListener {
        /**
         * Called for each character with its bounds.
         *
         * @param index the index of the character
         * @param lineNum the line number of the character
         * @param left the left edge of the character
         * @param top the top edge of the character
         * @param right the right edge of the character
         * @param bottom the bottom edge of the character
         */
        void onCharacterBounds(int index, int lineNum, float left, float top, float right,
                float bottom);