| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.text.style; |
| 18 |
|
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.content.Context; |
| 21 |
import android.content.res.ColorStateList; |
| 22 |
import android.content.res.TypedArray; |
| 23 |
import android.graphics.LeakyTypefaceStorage; |
| 24 |
import android.graphics.Typeface; |
| 25 |
import android.graphics.fonts.FontStyle; |
| 26 |
import android.os.LocaleList; |
| 27 |
import android.os.Parcel; |
| 28 |
import android.text.ParcelableSpan; |
| 29 |
import android.text.TextPaint; |
| 30 |
import android.text.TextUtils; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public class TextAppearanceSpan extends MetricAffectingSpan implements ParcelableSpan { |
| 62 |
private final String mFamilyName; |
| 63 |
private final int mStyle; |
| 64 |
private final int mTextSize; |
| 65 |
private final ColorStateList mTextColor; |
| 66 |
private final ColorStateList mTextColorLink; |
| 67 |
private final Typeface mTypeface; |
| 68 |
|
| 69 |
private final int mTextFontWeight; |
| 70 |
private final LocaleList mTextLocales; |
| 71 |
|
| 72 |
private final float mShadowRadius; |
| 73 |
private final float mShadowDx; |
| 74 |
private final float mShadowDy; |
| 75 |
private final int mShadowColor; |
| 76 |
|
| 77 |
private final boolean mHasElegantTextHeight; |
| 78 |
private final boolean mElegantTextHeight; |
| 79 |
private final boolean mHasLetterSpacing; |
| 80 |
private final float mLetterSpacing; |
| 81 |
|
| 82 |
private final String mFontFeatureSettings; |
| 83 |
private final String mFontVariationSettings; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public TextAppearanceSpan(Context context, int appearance) { |
| 91 |
this(context, appearance, -1); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public TextAppearanceSpan(Context context, int appearance, int colorList) { |
| 103 |
ColorStateList textColor; |
| 104 |
|
| 105 |
TypedArray a = |
| 106 |
context.obtainStyledAttributes(appearance, |
| 107 |
com.android.internal.R.styleable.TextAppearance); |
| 108 |
|
| 109 |
textColor = a.getColorStateList(com.android.internal.R.styleable. |
| 110 |
TextAppearance_textColor); |
| 111 |
mTextColorLink = a.getColorStateList(com.android.internal.R.styleable. |
| 112 |
TextAppearance_textColorLink); |
| 113 |
mTextSize = a.getDimensionPixelSize(com.android.internal.R.styleable. |
| 114 |
TextAppearance_textSize, -1); |
| 115 |
|
| 116 |
mStyle = a.getInt(com.android.internal.R.styleable.TextAppearance_textStyle, 0); |
| 117 |
if (!context.isRestricted() && context.canLoadUnsafeResources()) { |
| 118 |
mTypeface = a.getFont(com.android.internal.R.styleable.TextAppearance_fontFamily); |
| 119 |
} else { |
| 120 |
mTypeface = null; |
| 121 |
} |
| 122 |
if (mTypeface != null) { |
| 123 |
mFamilyName = null; |
| 124 |
} else { |
| 125 |
String family = a.getString(com.android.internal.R.styleable.TextAppearance_fontFamily); |
| 126 |
if (family != null) { |
| 127 |
mFamilyName = family; |
| 128 |
} else { |
| 129 |
int tf = a.getInt(com.android.internal.R.styleable.TextAppearance_typeface, 0); |
| 130 |
|
| 131 |
switch (tf) { |
| 132 |
case 1: |
| 133 |
mFamilyName = "sans"; |
| 134 |
break; |
| 135 |
|
| 136 |
case 2: |
| 137 |
mFamilyName = "serif"; |
| 138 |
break; |
| 139 |
|
| 140 |
case 3: |
| 141 |
mFamilyName = "monospace"; |
| 142 |
break; |
| 143 |
|
| 144 |
default: |
| 145 |
mFamilyName = null; |
| 146 |
break; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
mTextFontWeight = a.getInt(com.android.internal.R.styleable |
| 152 |
.TextAppearance_textFontWeight, -1); |
| 153 |
|
| 154 |
final String localeString = a.getString(com.android.internal.R.styleable |
| 155 |
.TextAppearance_textLocale); |
| 156 |
if (localeString != null) { |
| 157 |
LocaleList localeList = LocaleList.forLanguageTags(localeString); |
| 158 |
if (!localeList.isEmpty()) { |
| 159 |
mTextLocales = localeList; |
| 160 |
} else { |
| 161 |
mTextLocales = null; |
| 162 |
} |
| 163 |
} else { |
| 164 |
mTextLocales = null; |
| 165 |
} |
| 166 |
|
| 167 |
mShadowRadius = a.getFloat(com.android.internal.R.styleable |
| 168 |
.TextAppearance_shadowRadius, 0.0f); |
| 169 |
mShadowDx = a.getFloat(com.android.internal.R.styleable |
| 170 |
.TextAppearance_shadowDx, 0.0f); |
| 171 |
mShadowDy = a.getFloat(com.android.internal.R.styleable |
| 172 |
.TextAppearance_shadowDy, 0.0f); |
| 173 |
mShadowColor = a.getInt(com.android.internal.R.styleable |
| 174 |
.TextAppearance_shadowColor, 0); |
| 175 |
|
| 176 |
mHasElegantTextHeight = a.hasValue(com.android.internal.R.styleable |
| 177 |
.TextAppearance_elegantTextHeight); |
| 178 |
mElegantTextHeight = a.getBoolean(com.android.internal.R.styleable |
| 179 |
.TextAppearance_elegantTextHeight, false); |
| 180 |
|
| 181 |
mHasLetterSpacing = a.hasValue(com.android.internal.R.styleable |
| 182 |
.TextAppearance_letterSpacing); |
| 183 |
mLetterSpacing = a.getFloat(com.android.internal.R.styleable |
| 184 |
.TextAppearance_letterSpacing, 0.0f); |
| 185 |
|
| 186 |
mFontFeatureSettings = a.getString(com.android.internal.R.styleable |
| 187 |
.TextAppearance_fontFeatureSettings); |
| 188 |
|
| 189 |
mFontVariationSettings = a.getString(com.android.internal.R.styleable |
| 190 |
.TextAppearance_fontVariationSettings); |
| 191 |
|
| 192 |
a.recycle(); |
| 193 |
|
| 194 |
if (colorList >= 0) { |
| 195 |
a = context.obtainStyledAttributes(com.android.internal.R.style.Theme, |
| 196 |
com.android.internal.R.styleable.Theme); |
| 197 |
|
| 198 |
textColor = a.getColorStateList(colorList); |
| 199 |
a.recycle(); |
| 200 |
} |
| 201 |
|
| 202 |
mTextColor = textColor; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public TextAppearanceSpan(String family, int style, int size, |
| 210 |
ColorStateList color, ColorStateList linkColor) { |
| 211 |
mFamilyName = family; |
| 212 |
mStyle = style; |
| 213 |
mTextSize = size; |
| 214 |
mTextColor = color; |
| 215 |
mTextColorLink = linkColor; |
| 216 |
mTypeface = null; |
| 217 |
|
| 218 |
mTextFontWeight = -1; |
| 219 |
mTextLocales = null; |
| 220 |
|
| 221 |
mShadowRadius = 0.0f; |
| 222 |
mShadowDx = 0.0f; |
| 223 |
mShadowDy = 0.0f; |
| 224 |
mShadowColor = 0; |
| 225 |
|
| 226 |
mHasElegantTextHeight = false; |
| 227 |
mElegantTextHeight = false; |
| 228 |
mHasLetterSpacing = false; |
| 229 |
mLetterSpacing = 0.0f; |
| 230 |
|
| 231 |
mFontFeatureSettings = null; |
| 232 |
mFontVariationSettings = null; |
| 233 |
} |
| 234 |
|
| 235 |
public TextAppearanceSpan(Parcel src) { |
| 236 |
mFamilyName = src.readString(); |
| 237 |
mStyle = src.readInt(); |
| 238 |
mTextSize = src.readInt(); |
| 239 |
if (src.readInt() != 0) { |
| 240 |
mTextColor = ColorStateList.CREATOR.createFromParcel(src); |
| 241 |
} else { |
| 242 |
mTextColor = null; |
| 243 |
} |
| 244 |
if (src.readInt() != 0) { |
| 245 |
mTextColorLink = ColorStateList.CREATOR.createFromParcel(src); |
| 246 |
} else { |
| 247 |
mTextColorLink = null; |
| 248 |
} |
| 249 |
mTypeface = LeakyTypefaceStorage.readTypefaceFromParcel(src); |
| 250 |
|
| 251 |
mTextFontWeight = src.readInt(); |
| 252 |
mTextLocales = src.readParcelable(LocaleList.class.getClassLoader(), android.os.LocaleList.class); |
| 253 |
|
| 254 |
mShadowRadius = src.readFloat(); |
| 255 |
mShadowDx = src.readFloat(); |
| 256 |
mShadowDy = src.readFloat(); |
| 257 |
mShadowColor = src.readInt(); |
| 258 |
|
| 259 |
mHasElegantTextHeight = src.readBoolean(); |
| 260 |
mElegantTextHeight = src.readBoolean(); |
| 261 |
mHasLetterSpacing = src.readBoolean(); |
| 262 |
mLetterSpacing = src.readFloat(); |
| 263 |
|
| 264 |
mFontFeatureSettings = src.readString(); |
| 265 |
mFontVariationSettings = src.readString(); |
| 266 |
} |
| 267 |
|
| 268 |
public int getSpanTypeId() { |
| 269 |
return getSpanTypeIdInternal(); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
public int getSpanTypeIdInternal() { |
| 274 |
return TextUtils.TEXT_APPEARANCE_SPAN; |
| 275 |
} |
| 276 |
|
| 277 |
public int describeContents() { |
| 278 |
return 0; |
| 279 |
} |
| 280 |
|
| 281 |
public void writeToParcel(Parcel dest, int flags) { |
| 282 |
writeToParcelInternal(dest, flags); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
public void writeToParcelInternal(Parcel dest, int flags) { |
| 287 |
dest.writeString(mFamilyName); |
| 288 |
dest.writeInt(mStyle); |
| 289 |
dest.writeInt(mTextSize); |
| 290 |
if (mTextColor != null) { |
| 291 |
dest.writeInt(1); |
| 292 |
mTextColor.writeToParcel(dest, flags); |
| 293 |
} else { |
| 294 |
dest.writeInt(0); |
| 295 |
} |
| 296 |
if (mTextColorLink != null) { |
| 297 |
dest.writeInt(1); |
| 298 |
mTextColorLink.writeToParcel(dest, flags); |
| 299 |
} else { |
| 300 |
dest.writeInt(0); |
| 301 |
} |
| 302 |
LeakyTypefaceStorage.writeTypefaceToParcel(mTypeface, dest); |
| 303 |
|
| 304 |
dest.writeInt(mTextFontWeight); |
| 305 |
dest.writeParcelable(mTextLocales, flags); |
| 306 |
|
| 307 |
dest.writeFloat(mShadowRadius); |
| 308 |
dest.writeFloat(mShadowDx); |
| 309 |
dest.writeFloat(mShadowDy); |
| 310 |
dest.writeInt(mShadowColor); |
| 311 |
|
| 312 |
dest.writeBoolean(mHasElegantTextHeight); |
| 313 |
dest.writeBoolean(mElegantTextHeight); |
| 314 |
dest.writeBoolean(mHasLetterSpacing); |
| 315 |
dest.writeFloat(mLetterSpacing); |
| 316 |
|
| 317 |
dest.writeString(mFontFeatureSettings); |
| 318 |
dest.writeString(mFontVariationSettings); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
public String getFamily() { |
| 326 |
return mFamilyName; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public ColorStateList getTextColor() { |
| 334 |
return mTextColor; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
public ColorStateList getLinkTextColor() { |
| 342 |
return mTextColorLink; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
public int getTextSize() { |
| 350 |
return mTextSize; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
public int getTextStyle() { |
| 358 |
return mStyle; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
public int getTextFontWeight() { |
| 366 |
return mTextFontWeight; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
@Nullable |
| 374 |
public LocaleList getTextLocales() { |
| 375 |
return mTextLocales; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
@Nullable |
| 383 |
public Typeface getTypeface() { |
| 384 |
return mTypeface; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
public int getShadowColor() { |
| 392 |
return mShadowColor; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
public float getShadowDx() { |
| 400 |
return mShadowDx; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public float getShadowDy() { |
| 408 |
return mShadowDy; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
public float getShadowRadius() { |
| 416 |
return mShadowRadius; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
@Nullable |
| 424 |
public String getFontFeatureSettings() { |
| 425 |
return mFontFeatureSettings; |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
@Nullable |
| 433 |
public String getFontVariationSettings() { |
| 434 |
return mFontVariationSettings; |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
public boolean isElegantTextHeight() { |
| 442 |
return mElegantTextHeight; |
| 443 |
} |
| 444 |
|
| 445 |
@Override |
| 446 |
public void updateDrawState(TextPaint ds) { |
| 447 |
updateMeasureState(ds); |
| 448 |
|
| 449 |
if (mTextColor != null) { |
| 450 |
ds.setColor(mTextColor.getColorForState(ds.drawableState, 0)); |
| 451 |
} |
| 452 |
|
| 453 |
if (mTextColorLink != null) { |
| 454 |
ds.linkColor = mTextColorLink.getColorForState(ds.drawableState, 0); |
| 455 |
} |
| 456 |
|
| 457 |
if (mShadowColor != 0) { |
| 458 |
ds.setShadowLayer(mShadowRadius, mShadowDx, mShadowDy, mShadowColor); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
@Override |
| 463 |
public void updateMeasureState(TextPaint ds) { |
| 464 |
final Typeface styledTypeface; |
| 465 |
int style = 0; |
| 466 |
|
| 467 |
if (mTypeface != null) { |
| 468 |
style = mStyle; |
| 469 |
styledTypeface = Typeface.create(mTypeface, style); |
| 470 |
} else if (mFamilyName != null || mStyle != 0) { |
| 471 |
Typeface tf = ds.getTypeface(); |
| 472 |
|
| 473 |
if (tf != null) { |
| 474 |
style = tf.getStyle(); |
| 475 |
} |
| 476 |
|
| 477 |
style |= mStyle; |
| 478 |
|
| 479 |
if (mFamilyName != null) { |
| 480 |
styledTypeface = Typeface.create(mFamilyName, style); |
| 481 |
} else if (tf == null) { |
| 482 |
styledTypeface = Typeface.defaultFromStyle(style); |
| 483 |
} else { |
| 484 |
styledTypeface = Typeface.create(tf, style); |
| 485 |
} |
| 486 |
} else { |
| 487 |
styledTypeface = null; |
| 488 |
} |
| 489 |
|
| 490 |
if (styledTypeface != null) { |
| 491 |
final Typeface readyTypeface; |
| 492 |
if (mTextFontWeight >= 0) { |
| 493 |
final int weight = Math.min(FontStyle.FONT_WEIGHT_MAX, mTextFontWeight); |
| 494 |
final boolean italic = (style & Typeface.ITALIC) != 0; |
| 495 |
readyTypeface = ds.setTypeface(Typeface.create(styledTypeface, weight, italic)); |
| 496 |
} else { |
| 497 |
readyTypeface = styledTypeface; |
| 498 |
} |
| 499 |
|
| 500 |
int fake = style & ~readyTypeface.getStyle(); |
| 501 |
|
| 502 |
if ((fake & Typeface.BOLD) != 0) { |
| 503 |
ds.setFakeBoldText(true); |
| 504 |
} |
| 505 |
|
| 506 |
if ((fake & Typeface.ITALIC) != 0) { |
| 507 |
ds.setTextSkewX(-0.25f); |
| 508 |
} |
| 509 |
|
| 510 |
ds.setTypeface(readyTypeface); |
| 511 |
} |
| 512 |
|
| 513 |
if (mTextSize > 0) { |
| 514 |
ds.setTextSize(mTextSize); |
| 515 |
} |
| 516 |
|
| 517 |
if (mTextLocales != null) { |
| 518 |
ds.setTextLocales(mTextLocales); |
| 519 |
} |
| 520 |
|
| 521 |
if (mHasElegantTextHeight) { |
| 522 |
ds.setElegantTextHeight(mElegantTextHeight); |
| 523 |
} |
| 524 |
|
| 525 |
if (mHasLetterSpacing) { |
| 526 |
ds.setLetterSpacing(mLetterSpacing); |
| 527 |
} |
| 528 |
|
| 529 |
if (mFontFeatureSettings != null) { |
| 530 |
ds.setFontFeatureSettings(mFontFeatureSettings); |
| 531 |
} |
| 532 |
|
| 533 |
if (mFontVariationSettings != null) { |
| 534 |
ds.setFontVariationSettings(mFontVariationSettings); |
| 535 |
} |
| 536 |
} |
| 537 |
} |