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.NonNull; 20 import android.os.Parcel; 21 import android.text.ParcelableSpan; 22 import android.text.TextPaint; 23 import android.text.TextUtils; 24 25 /** 26 * The span that moves the position of the text baseline higher. 27 * <p> 28 * The span can be used like this: 29 * <pre>{@code 30 * SpannableString string = new SpannableString("1st example"); 31 *string.setSpan(new SuperscriptSpan(), 1, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}</pre> 32 * <img src="{@docRoot}reference/android/images/text/style/superscriptspan.png" /> 33 * <figcaption>Text with <code>SuperscriptSpan</code>.</figcaption> 34 * Note: Since the span affects the position of the text, if the text is on the first line of a 35 * TextView, it may appear cut. This can be avoided by decreasing the text size with an {@link 36 * AbsoluteSizeSpan} 37 */ 38 @android.ravenwood.annotation.RavenwoodKeepWholeClass 39 public class SuperscriptSpan extends MetricAffectingSpan implements ParcelableSpan { 40 /** 41 * Creates a {@link SuperscriptSpan}. 42 */ SuperscriptSpan()43 public SuperscriptSpan() { 44 } 45 46 /** 47 * Creates a {@link SuperscriptSpan} from a parcel. 48 */ SuperscriptSpan(@onNull Parcel src)49 public SuperscriptSpan(@NonNull Parcel src) { 50 } 51 52 @Override getSpanTypeId()53 public int getSpanTypeId() { 54 return getSpanTypeIdInternal(); 55 } 56 57 /** @hide */ 58 @Override getSpanTypeIdInternal()59 public int getSpanTypeIdInternal() { 60 return TextUtils.SUPERSCRIPT_SPAN; 61 } 62 63 @Override describeContents()64 public int describeContents() { 65 return 0; 66 } 67 68 @Override writeToParcel(@onNull Parcel dest, int flags)69 public void writeToParcel(@NonNull Parcel dest, int flags) { 70 writeToParcelInternal(dest, flags); 71 } 72 73 /** @hide */ 74 @Override writeToParcelInternal(@onNull Parcel dest, int flags)75 public void writeToParcelInternal(@NonNull Parcel dest, int flags) { 76 } 77 78 @Override updateDrawState(@onNull TextPaint textPaint)79 public void updateDrawState(@NonNull TextPaint textPaint) { 80 textPaint.baselineShift += (int) (textPaint.ascent() / 2); 81 } 82 83 @Override updateMeasureState(@onNull TextPaint textPaint)84 public void updateMeasureState(@NonNull TextPaint textPaint) { 85 textPaint.baselineShift += (int) (textPaint.ascent() / 2); 86 } 87 88 @Override toString()89 public String toString() { 90 return "SuperscriptSpan{}"; 91 } 92 } 93