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.graphics.Paint; 20 import android.graphics.Typeface; 21 import android.os.Parcel; 22 import android.text.ParcelableSpan; 23 import android.text.TextPaint; 24 import android.text.TextUtils; 25 26 /** 27 * 28 * Describes a style in a span. 29 * Note that styles are cumulative -- if both bold and italic are set in 30 * separate spans, or if the base style is bold and a span calls for italic, 31 * you get bold italic. You can't turn off a style from the base style. 32 * 33 */ 34 public class StyleSpan extends MetricAffectingSpan implements ParcelableSpan { 35 36 private final int mStyle; 37 38 /** 39 * 40 * @param style An integer constant describing the style for this span. Examples 41 * include bold, italic, and normal. Values are constants defined 42 * in {@link android.graphics.Typeface}. 43 */ StyleSpan(int style)44 public StyleSpan(int style) { 45 mStyle = style; 46 } 47 StyleSpan(Parcel src)48 public StyleSpan(Parcel src) { 49 mStyle = src.readInt(); 50 } 51 getSpanTypeId()52 public int getSpanTypeId() { 53 return getSpanTypeIdInternal(); 54 } 55 56 /** @hide */ getSpanTypeIdInternal()57 public int getSpanTypeIdInternal() { 58 return TextUtils.STYLE_SPAN; 59 } 60 describeContents()61 public int describeContents() { 62 return 0; 63 } 64 writeToParcel(Parcel dest, int flags)65 public void writeToParcel(Parcel dest, int flags) { 66 writeToParcelInternal(dest, flags); 67 } 68 69 /** @hide */ writeToParcelInternal(Parcel dest, int flags)70 public void writeToParcelInternal(Parcel dest, int flags) { 71 dest.writeInt(mStyle); 72 } 73 74 /** 75 * Returns the style constant defined in {@link android.graphics.Typeface}. 76 */ getStyle()77 public int getStyle() { 78 return mStyle; 79 } 80 81 @Override updateDrawState(TextPaint ds)82 public void updateDrawState(TextPaint ds) { 83 apply(ds, mStyle); 84 } 85 86 @Override updateMeasureState(TextPaint paint)87 public void updateMeasureState(TextPaint paint) { 88 apply(paint, mStyle); 89 } 90 apply(Paint paint, int style)91 private static void apply(Paint paint, int style) { 92 int oldStyle; 93 94 Typeface old = paint.getTypeface(); 95 if (old == null) { 96 oldStyle = 0; 97 } else { 98 oldStyle = old.getStyle(); 99 } 100 101 int want = oldStyle | style; 102 103 Typeface tf; 104 if (old == null) { 105 tf = Typeface.defaultFromStyle(want); 106 } else { 107 tf = Typeface.create(old, want); 108 } 109 110 int fake = want & ~tf.getStyle(); 111 112 if ((fake & Typeface.BOLD) != 0) { 113 paint.setFakeBoldText(true); 114 } 115 116 if ((fake & Typeface.ITALIC) != 0) { 117 paint.setTextSkewX(-0.25f); 118 } 119 120 paint.setTypeface(tf); 121 } 122 } 123