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

Commit fef3dab1 authored by Jean Chen's avatar Jean Chen Committed by Android (Google) Code Review
Browse files

Merge changes from topic "whiteSpace_emoji_background" into main

* changes:
  fix(HCT): HCT background is disruptive with when only Emoji
  fix(HCT): text not being centered within the background
parents 25fd117d ac2ef619
Loading
Loading
Loading
Loading
+40 −6
Original line number Diff line number Diff line
@@ -1071,13 +1071,27 @@ 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;
                        }

                        // To avoid highlighting emoji sequences, we use Extended_Pictgraphs as a
                        // heuristic. Highlighting is skipped based on code points, not glyph type
                        // (text vs. color), so emojis with default text presentation are
                        // intentionally not highlighted (numeric representation with emoji
                        // presentation are manually excluded). Although we process ZWJ and
                        // variation selectors within emoji sequences, they should not affect
                        // highlighting due to their zero-width nature.
                        var codePoint = Character.codePointAt(mText, index);
                        var isEmoji = Character.isEmojiComponent(codePoint)
                                || Character.isExtendedPictographic(codePoint);
                        if (isEmoji && !isStandardNumber(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);
@@ -1096,6 +1110,16 @@ public abstract class Layout {
                        drawRect();
                    }

                    private boolean isStandardNumber(int index) {
                        var codePoint = Character.codePointAt(mText, index);
                        var isNumberSignOrAsterisk = (codePoint >= '0' && codePoint <= '9')
                                || codePoint == '#' || codePoint == '*';
                        var isColoredGlyph = index + 1 < mText.length()
                                && Character.codePointAt(mText, index + 1) == 0xFE0F;

                        return isNumberSignOrAsterisk && !isColoredGlyph;
                    }

                    private void drawRect() {
                        if (!mLineBackground.isEmpty()) {
                            mLineBackground.inset(-padding, -padding);
@@ -4626,6 +4650,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);