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.text.TextPaint; 20 21 /** 22 * The classes that affect character-level text formatting extend this 23 * class. Most extend its subclass {@link MetricAffectingSpan}, but simple 24 * ones may just implement {@link UpdateAppearance}. 25 */ 26 @android.ravenwood.annotation.RavenwoodKeepWholeClass 27 public abstract class CharacterStyle { updateDrawState(TextPaint tp)28 public abstract void updateDrawState(TextPaint tp); 29 30 /** 31 * A given CharacterStyle can only applied to a single region of a given 32 * Spanned. If you need to attach the same CharacterStyle to multiple 33 * regions, you can use this method to wrap it with a new object that 34 * will have the same effect but be a distinct object so that it can 35 * also be attached without conflict. 36 */ wrap(CharacterStyle cs)37 public static CharacterStyle wrap(CharacterStyle cs) { 38 if (cs instanceof MetricAffectingSpan) { 39 return new MetricAffectingSpan.Passthrough((MetricAffectingSpan) cs); 40 } else { 41 return new Passthrough(cs); 42 } 43 } 44 45 /** 46 * Returns "this" for most CharacterStyles, but for CharacterStyles 47 * that were generated by {@link #wrap}, returns the underlying 48 * CharacterStyle. 49 */ getUnderlying()50 public CharacterStyle getUnderlying() { 51 return this; 52 } 53 54 /** 55 * A Passthrough CharacterStyle is one that 56 * passes {@link #updateDrawState} calls through to the 57 * specified CharacterStyle while still being a distinct object, 58 * and is therefore able to be attached to the same Spannable 59 * to which the specified CharacterStyle is already attached. 60 */ 61 private static class Passthrough extends CharacterStyle { 62 private CharacterStyle mStyle; 63 64 /** 65 * Creates a new Passthrough of the specfied CharacterStyle. 66 */ Passthrough(CharacterStyle cs)67 public Passthrough(CharacterStyle cs) { 68 mStyle = cs; 69 } 70 71 /** 72 * Passes updateDrawState through to the underlying CharacterStyle. 73 */ 74 @Override updateDrawState(TextPaint tp)75 public void updateDrawState(TextPaint tp) { 76 mStyle.updateDrawState(tp); 77 } 78 79 /** 80 * Returns the CharacterStyle underlying this one, or the one 81 * underlying it if it too is a Passthrough. 82 */ 83 @Override getUnderlying()84 public CharacterStyle getUnderlying() { 85 return mStyle.getUnderlying(); 86 } 87 } 88 } 89