1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.text; 18 19 import android.annotation.ColorInt; 20 import android.annotation.Nullable; 21 import android.graphics.Color; 22 import android.text.style.CharacterStyle; 23 24 /** 25 * Finds the foreground text color for the given Spanned text so you can iterate through each color 26 * change. 27 * 28 * @hide 29 */ 30 public class SpanColors { 31 public static final @ColorInt int NO_COLOR_FOUND = Color.TRANSPARENT; 32 33 private final SpanSet<CharacterStyle> mCharacterStyleSpanSet = 34 new SpanSet<>(CharacterStyle.class); 35 @Nullable private TextPaint mWorkPaint; 36 SpanColors()37 public SpanColors() {} 38 39 /** 40 * Init for the given text 41 * 42 * @param workPaint A temporary TextPaint object that will be used to calculate the colors. The 43 * paint properties will be mutated on calls to {@link #getColorAt(int)} so 44 * make sure to reset it before you use it for something else. 45 * @param spanned the text to examine 46 * @param start index to start at 47 * @param end index of the end 48 */ init(TextPaint workPaint, Spanned spanned, int start, int end)49 public void init(TextPaint workPaint, Spanned spanned, int start, int end) { 50 mWorkPaint = workPaint; 51 mCharacterStyleSpanSet.init(spanned, start, end); 52 } 53 54 /** 55 * Removes all internal references to the spans to avoid memory leaks. 56 */ recycle()57 public void recycle() { 58 mWorkPaint = null; 59 mCharacterStyleSpanSet.recycle(); 60 } 61 62 /** 63 * Calculates the foreground color of the text at the given character index. 64 * 65 * <p>You must call {@link #init(TextPaint, Spanned, int, int)} before calling this 66 */ getColorAt(int index)67 public @ColorInt int getColorAt(int index) { 68 var finalColor = NO_COLOR_FOUND; 69 // Reset the paint so if we get a CharacterStyle that doesn't actually specify color, 70 // (like UnderlineSpan), we still return no color found. 71 mWorkPaint.setColor(finalColor); 72 for (int k = 0; k < mCharacterStyleSpanSet.numberOfSpans; k++) { 73 if ((index >= mCharacterStyleSpanSet.spanStarts[k]) 74 && (index <= mCharacterStyleSpanSet.spanEnds[k])) { 75 final CharacterStyle span = mCharacterStyleSpanSet.spans[k]; 76 span.updateDrawState(mWorkPaint); 77 78 finalColor = calculateFinalColor(mWorkPaint); 79 } 80 } 81 return finalColor; 82 } 83 calculateFinalColor(TextPaint workPaint)84 private @ColorInt int calculateFinalColor(TextPaint workPaint) { 85 // TODO: can we figure out what the getColorFilter() will do? 86 // if so, we also need to reset colorFilter before the loop in getColorAt() 87 return workPaint.getColor(); 88 } 89 } 90