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     /**
29      * Returns the width of the span. Extending classes can set the height of the span by updating
30      * attributes of {@link android.graphics.Paint.FontMetricsInt}. If the span covers the whole
31      * text, and the height is not set,
32      * {@link #draw(Canvas, CharSequence, int, int, float, int, int, int, Paint)} will not be
33      * called for the span.
34      *
35      * @param paint Paint instance.
36      * @param text Current text.
37      * @param start Start character index for span.
38      * @param end End character index for span.
39      * @param fm Font metrics, can be null.
40      * @return Width of the span.
41      */
getSize(@onNull Paint paint, CharSequence text, @IntRange(from = 0) int start, @IntRange(from = 0) int end, @Nullable Paint.FontMetricsInt fm)42     public abstract int getSize(@NonNull Paint paint, CharSequence text,
43                         @IntRange(from = 0) int start, @IntRange(from = 0) int end,
44                         @Nullable Paint.FontMetricsInt fm);
45 
46     /**
47      * Draws the span into the canvas.
48      *
49      * @param canvas Canvas into which the span should be rendered.
50      * @param text Current text.
51      * @param start Start character index for span.
52      * @param end End character index for span.
53      * @param x Edge of the replacement closest to the leading margin.
54      * @param top Top of the line.
55      * @param y Baseline.
56      * @param bottom Bottom of the line.
57      * @param paint Paint instance.
58      */
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)59     public abstract void draw(@NonNull Canvas canvas, CharSequence text,
60                               @IntRange(from = 0) int start, @IntRange(from = 0) int end, float x,
61                               int top, int y, int bottom, @NonNull Paint paint);
62 
63     /**
64      * This method does nothing, since ReplacementSpans are measured
65      * explicitly instead of affecting Paint properties.
66      */
updateMeasureState(TextPaint p)67     public void updateMeasureState(TextPaint p) { }
68 
69     /**
70      * This method does nothing, since ReplacementSpans are drawn
71      * explicitly instead of affecting Paint properties.
72      */
updateDrawState(TextPaint ds)73     public void updateDrawState(TextPaint ds) { }
74 }
75