1 /* 2 * Copyright (C) 2006 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.style; 18 19 import android.annotation.IntRange; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.graphics.Canvas; 23 import android.graphics.Paint; 24 import android.text.TextPaint; 25 26 @android.ravenwood.annotation.RavenwoodKeepWholeClass 27 public abstract class ReplacementSpan extends MetricAffectingSpan { 28 29 private CharSequence mContentDescription = null; 30 31 /** 32 * Returns the width of the span. Extending classes can set the height of the span by updating 33 * attributes of {@link android.graphics.Paint.FontMetricsInt}. If the span covers the whole 34 * text, and the height is not set, 35 * {@link #draw(Canvas, CharSequence, int, int, float, int, int, int, Paint)} will not be 36 * called for the span. 37 * 38 * @param paint Paint instance. 39 * @param text Current text. 40 * @param start Start character index for span. 41 * @param end End character index for span. 42 * @param fm Font metrics, can be null. 43 * @return Width of the span. 44 */ getSize(@onNull Paint paint, CharSequence text, @IntRange(from = 0) int start, @IntRange(from = 0) int end, @Nullable Paint.FontMetricsInt fm)45 public abstract int getSize(@NonNull Paint paint, CharSequence text, 46 @IntRange(from = 0) int start, @IntRange(from = 0) int end, 47 @Nullable Paint.FontMetricsInt fm); 48 49 /** 50 * Draws the span into the canvas. 51 * 52 * @param canvas Canvas into which the span should be rendered. 53 * @param text Current text. 54 * @param start Start character index for span. 55 * @param end End character index for span. 56 * @param x Edge of the replacement closest to the leading margin. 57 * @param top Top of the line. 58 * @param y Baseline. 59 * @param bottom Bottom of the line. 60 * @param paint Paint instance. 61 */ draw(@onNull Canvas canvas, CharSequence text, @IntRange(from = 0) int start, @IntRange(from = 0) int end, float x, int top, int y, int bottom, @NonNull Paint paint)62 public abstract void draw(@NonNull Canvas canvas, CharSequence text, 63 @IntRange(from = 0) int start, @IntRange(from = 0) int end, float x, 64 int top, int y, int bottom, @NonNull Paint paint); 65 66 /** 67 * Gets a brief description of this ReplacementSpan for use in accessibility support. 68 * 69 * @return The content description. 70 */ 71 @Nullable getContentDescription()72 public CharSequence getContentDescription() { 73 return mContentDescription; 74 } 75 76 /** 77 * Sets the specific content description into ReplacementSpan. 78 * ReplacementSpans are shared with accessibility services, 79 * but only the content description is available from them. 80 * 81 * @param contentDescription content description. The default value is null. 82 */ setContentDescription(@ullable CharSequence contentDescription)83 public void setContentDescription(@Nullable CharSequence contentDescription) { 84 mContentDescription = contentDescription; 85 } 86 87 /** 88 * This method does nothing, since ReplacementSpans are measured 89 * explicitly instead of affecting Paint properties. 90 */ updateMeasureState(TextPaint p)91 public void updateMeasureState(TextPaint p) { } 92 93 /** 94 * This method does nothing, since ReplacementSpans are drawn 95 * explicitly instead of affecting Paint properties. 96 */ updateDrawState(TextPaint ds)97 public void updateDrawState(TextPaint ds) { } 98 } 99