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

Commit 3d80511a authored by chenjean's avatar chenjean
Browse files

fix(HCT):Hide HCT background when the text is only whitespace

Bug: 385167023
Flag: com.android.graphics.hwui.flags.high_contrast_text_small_text_rect
Test: atest frameworks/base/core/tests/coretests/src/android/text/LayoutTest.java
Test: atest cts/tests/tests/uirendering/src/android/uirendering/cts/testclasses/TextViewHighContrastTextTests.kt
Change-Id: I3289699b5fa7d4e5fb678d52b7f36de436a0543c
parent b68bde55
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -1071,6 +1071,12 @@ public abstract class Layout {
                        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))) {
                                return;
                            }

                            // Draw what we have so far, then reset the rect and update its color
                            drawRect();
                            mLineBackground.set(left, top, right, bottom);
+49 −0
Original line number Diff line number Diff line
@@ -1024,6 +1024,55 @@ public class LayoutTest {
        expect.that(backgroundCommands.size()).isEqualTo(backgroundRectsDrawn);
    }

    @Test
    @RequiresFlagsEnabled(FLAG_HIGH_CONTRAST_TEXT_SMALL_TEXT_RECT)
    public void highContrastTextEnabled_testWhitespaceText_DrawsBackgroundsWithAdjacentLetters() {
        mTextPaint.setColor(Color.BLACK);
        SpannableString spannedText = new SpannableString("Test\tTap and Space");

        // Set the entire text to white initially
        spannedText.setSpan(
                new ForegroundColorSpan(Color.WHITE),
                /* start= */ 0,
                /* end= */ spannedText.length(),
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
        );

        // Find the whitespace character and set its color to black
        for (int i = 0; i < spannedText.length(); i++) {
            if (Character.isWhitespace(spannedText.charAt(i))) {
                spannedText.setSpan(
                        new ForegroundColorSpan(Color.BLACK),
                        i,
                        i + 1,
                        Spanned.SPAN_INCLUSIVE_EXCLUSIVE
                );
            }
        }

        Layout layout = new StaticLayout(spannedText, mTextPaint, mWidth,
                mAlign, mSpacingMult, mSpacingAdd, /* includePad= */ false);

        MockCanvas c = new MockCanvas(/* width= */ 256, /* height= */ 256);
        c.setHighContrastTextEnabled(true);
        layout.draw(
                c,
                /* highlightPaths= */ null,
                /* highlightPaints= */ null,
                /* selectionPath= */ null,
                /* selectionPaint= */ null,
                /* cursorOffsetVertical= */ 0
        );

        List<MockCanvas.DrawCommand> drawCommands = c.getDrawCommands();
        for (int i = 0; i < drawCommands.size(); i++) {
            MockCanvas.DrawCommand drawCommand = drawCommands.get(i);
            if (drawCommand.rect != null) {
                expect.that(removeAlpha(drawCommand.paint.getColor())).isEqualTo(Color.BLACK);
            }
        }
    }

    private int removeAlpha(int color) {
        return Color.rgb(
                Color.red(color),