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