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; 18 19 import android.annotation.ColorInt; 20 import android.annotation.Px; 21 import android.compat.annotation.UnsupportedAppUsage; 22 import android.graphics.Paint; 23 24 /** 25 * TextPaint is an extension of Paint that leaves room for some extra 26 * data used during text measuring and drawing. 27 */ 28 @android.ravenwood.annotation.RavenwoodKeepWholeClass 29 public class TextPaint extends Paint { 30 31 // Special value 0 means no background paint 32 @ColorInt 33 public int bgColor; 34 public int baselineShift; 35 @ColorInt 36 public int linkColor; 37 public int[] drawableState; 38 public float density = 1.0f; 39 /** 40 * Special value 0 means no custom underline 41 */ 42 @ColorInt 43 public int underlineColor = 0; 44 45 /** 46 * Thickness of the underline, in pixels. 47 */ 48 public @Px float underlineThickness; 49 TextPaint()50 public TextPaint() { 51 super(); 52 } 53 TextPaint(int flags)54 public TextPaint(int flags) { 55 super(flags); 56 } 57 TextPaint(Paint p)58 public TextPaint(Paint p) { 59 super(p); 60 } 61 62 /** 63 * Copy the fields from tp into this TextPaint, including the 64 * fields inherited from Paint. 65 */ set(TextPaint tp)66 public void set(TextPaint tp) { 67 super.set(tp); 68 69 bgColor = tp.bgColor; 70 baselineShift = tp.baselineShift; 71 linkColor = tp.linkColor; 72 drawableState = tp.drawableState; 73 density = tp.density; 74 underlineColor = tp.underlineColor; 75 underlineThickness = tp.underlineThickness; 76 } 77 78 /** 79 * Defines a custom underline for this Paint. 80 * @param color underline solid color 81 * @param thickness underline thickness 82 * @hide 83 */ 84 @UnsupportedAppUsage setUnderlineText(int color, float thickness)85 public void setUnderlineText(int color, float thickness) { 86 underlineColor = color; 87 underlineThickness = thickness; 88 } 89 90 /** 91 * @hide 92 */ 93 @Override getUnderlineThickness()94 public float getUnderlineThickness() { 95 if (underlineColor != 0) { // Return custom thickness only if underline color is set. 96 return underlineThickness; 97 } else { 98 return super.getUnderlineThickness(); 99 } 100 } 101 } 102