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.graphics.Bitmap;
     20 import android.graphics.Canvas;
     21 import android.graphics.Paint;
     22 import android.text.Layout;
     23 import android.text.Spanned;
     24 
     25 public class IconMarginSpan
     26 implements LeadingMarginSpan, LineHeightSpan
     27 {
     28     public IconMarginSpan(Bitmap b) {
     29         mBitmap = b;
     30     }
     31 
     32     public IconMarginSpan(Bitmap b, int pad) {
     33         mBitmap = b;
     34         mPad = pad;
     35     }
     36 
     37     public int getLeadingMargin(boolean first) {
     38         return mBitmap.getWidth() + mPad;
     39     }
     40 
     41     public void drawLeadingMargin(Canvas c, Paint p, int x, int dir,
     42                                   int top, int baseline, int bottom,
     43                                   CharSequence text, int start, int end,
     44                                   boolean first, Layout layout) {
     45         int st = ((Spanned) text).getSpanStart(this);
     46         int itop = layout.getLineTop(layout.getLineForOffset(st));
     47 
     48         if (dir < 0)
     49             x -= mBitmap.getWidth();
     50 
     51         c.drawBitmap(mBitmap, x, itop, p);
     52     }
     53 
     54     public void chooseHeight(CharSequence text, int start, int end,
     55                              int istartv, int v,
     56                              Paint.FontMetricsInt fm) {
     57         if (end == ((Spanned) text).getSpanEnd(this)) {
     58             int ht = mBitmap.getHeight();
     59 
     60             int need = ht - (v + fm.descent - fm.ascent - istartv);
     61             if (need > 0)
     62                 fm.descent += need;
     63 
     64             need = ht - (v + fm.bottom - fm.top - istartv);
     65             if (need > 0)
     66                 fm.bottom += need;
     67         }
     68     }
     69 
     70     private Bitmap mBitmap;
     71     private int mPad;
     72 }
     73