Home | History | Annotate | Download | only in text
      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.NonNull;
     21 import android.graphics.Paint;
     22 
     23 /**
     24  * TextPaint is an extension of Paint that leaves room for some extra
     25  * data used during text measuring and drawing.
     26  */
     27 public class TextPaint extends Paint {
     28 
     29     // Special value 0 means no background paint
     30     @ColorInt
     31     public int bgColor;
     32     public int baselineShift;
     33     @ColorInt
     34     public int linkColor;
     35     public int[] drawableState;
     36     public float density = 1.0f;
     37     /**
     38      * Special value 0 means no custom underline
     39      * @hide
     40      */
     41     @ColorInt
     42     public int underlineColor = 0;
     43     /**
     44      * Thickness of the underline, in pixels.
     45      * @hide
     46      */
     47     public float underlineThickness;
     48 
     49     public TextPaint() {
     50         super();
     51     }
     52 
     53     public TextPaint(int flags) {
     54         super(flags);
     55     }
     56 
     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      */
     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      * Returns true if all attributes, including the attributes inherited from Paint, are equal.
     79      *
     80      * The caller is expected to have checked the trivial cases, like the pointers being equal,
     81      * the objects having different classes, or the parameter being null.
     82      * @hide
     83      */
     84     public boolean hasEqualAttributes(@NonNull TextPaint other) {
     85         return bgColor == other.bgColor
     86                 && baselineShift == other.baselineShift
     87                 && linkColor == other.linkColor
     88                 && drawableState == other.drawableState
     89                 && density == other.density
     90                 && underlineColor == other.underlineColor
     91                 && underlineThickness == other.underlineThickness
     92                 && super.hasEqualAttributes((Paint) other);
     93     }
     94 
     95     /**
     96      * Defines a custom underline for this Paint.
     97      * @param color underline solid color
     98      * @param thickness underline thickness
     99      * @hide
    100      */
    101     public void setUnderlineText(int color, float thickness) {
    102         underlineColor = color;
    103         underlineThickness = thickness;
    104     }
    105 
    106     /**
    107      * @hide
    108      */
    109     @Override
    110     public float getUnderlineThickness() {
    111         if (underlineColor != 0) { // Return custom thickness only if underline color is set.
    112             return underlineThickness;
    113         } else {
    114             return super.getUnderlineThickness();
    115         }
    116     }
    117 }
    118