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