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