Home | History | Annotate | Download | only in style
      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.annotation.ColorInt;
     20 import android.graphics.Canvas;
     21 import android.graphics.Paint;
     22 import android.os.Parcel;
     23 import android.text.Layout;
     24 import android.text.ParcelableSpan;
     25 import android.text.TextUtils;
     26 
     27 public class QuoteSpan implements LeadingMarginSpan, ParcelableSpan {
     28     private static final int STRIPE_WIDTH = 2;
     29     private static final int GAP_WIDTH = 2;
     30 
     31     private final int mColor;
     32 
     33     public QuoteSpan() {
     34         super();
     35         mColor = 0xff0000ff;
     36     }
     37 
     38     public QuoteSpan(@ColorInt int color) {
     39         super();
     40         mColor = color;
     41     }
     42 
     43     public QuoteSpan(Parcel src) {
     44         mColor = src.readInt();
     45     }
     46 
     47     public int getSpanTypeId() {
     48         return getSpanTypeIdInternal();
     49     }
     50 
     51     /** @hide */
     52     public int getSpanTypeIdInternal() {
     53         return TextUtils.QUOTE_SPAN;
     54     }
     55 
     56     public int describeContents() {
     57         return 0;
     58     }
     59 
     60     public void writeToParcel(Parcel dest, int flags) {
     61         writeToParcelInternal(dest, flags);
     62     }
     63 
     64     /** @hide */
     65     public void writeToParcelInternal(Parcel dest, int flags) {
     66         dest.writeInt(mColor);
     67     }
     68 
     69     @ColorInt
     70     public int getColor() {
     71         return mColor;
     72     }
     73 
     74     public int getLeadingMargin(boolean first) {
     75         return STRIPE_WIDTH + GAP_WIDTH;
     76     }
     77 
     78     public void drawLeadingMargin(Canvas c, Paint p, int x, int dir,
     79                                   int top, int baseline, int bottom,
     80                                   CharSequence text, int start, int end,
     81                                   boolean first, Layout layout) {
     82         Paint.Style style = p.getStyle();
     83         int color = p.getColor();
     84 
     85         p.setStyle(Paint.Style.FILL);
     86         p.setColor(mColor);
     87 
     88         c.drawRect(x, top, x + dir * STRIPE_WIDTH, bottom, p);
     89 
     90         p.setStyle(style);
     91         p.setColor(color);
     92     }
     93 }
     94