• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 @android.ravenwood.annotation.RavenwoodKeepWholeClass
31 public class SpanColors {
32     public static final @ColorInt int NO_COLOR_FOUND = Color.TRANSPARENT;
33 
34     private final SpanSet<CharacterStyle> mCharacterStyleSpanSet =
35             new SpanSet<>(CharacterStyle.class);
36     @Nullable private TextPaint mWorkPaint;
37 
SpanColors()38     public SpanColors() {}
39 
40     /**
41      * Init for the given text
42      *
43      * @param workPaint A temporary TextPaint object that will be used to calculate the colors. The
44      *                  paint properties will be mutated on calls to {@link #getColorAt(int)} so
45      *                  make sure to reset it before you use it for something else.
46      * @param spanned the text to examine
47      * @param start index to start at
48      * @param end index of the end
49      */
init(TextPaint workPaint, Spanned spanned, int start, int end)50     public void init(TextPaint workPaint, Spanned spanned, int start, int end) {
51         mWorkPaint = workPaint;
52         mCharacterStyleSpanSet.init(spanned, start, end);
53     }
54 
55     /**
56      * Removes all internal references to the spans to avoid memory leaks.
57      */
recycle()58     public void recycle() {
59         mWorkPaint = null;
60         mCharacterStyleSpanSet.recycle();
61     }
62 
63     /**
64      * Calculates the foreground color of the text at the given character index.
65      *
66      * <p>You must call {@link #init(TextPaint, Spanned, int, int)} before calling this
67      */
getColorAt(int index)68     public @ColorInt int getColorAt(int index) {
69         var finalColor = NO_COLOR_FOUND;
70         // Reset the paint so if we get a CharacterStyle that doesn't actually specify color,
71         // (like UnderlineSpan), we still return no color found.
72         mWorkPaint.setColor(finalColor);
73         for (int k = 0; k < mCharacterStyleSpanSet.numberOfSpans; k++) {
74             if ((index >= mCharacterStyleSpanSet.spanStarts[k])
75                     && (index <= mCharacterStyleSpanSet.spanEnds[k])) {
76                 final CharacterStyle span = mCharacterStyleSpanSet.spans[k];
77                 span.updateDrawState(mWorkPaint);
78 
79                 finalColor = calculateFinalColor(mWorkPaint);
80             }
81         }
82         return finalColor;
83     }
84 
calculateFinalColor(TextPaint workPaint)85     private @ColorInt int calculateFinalColor(TextPaint workPaint) {
86         // TODO: can we figure out what the getColorFilter() will do?
87         //  if so, we also need to reset colorFilter before the loop in getColorAt()
88         return workPaint.getColor();
89     }
90 }
91