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.drawable.Drawable; 20 import android.graphics.Paint; 21 import android.graphics.Canvas; 22 import android.text.Spanned; 23 import android.text.Layout; 24 25 public class DrawableMarginSpan 26 implements LeadingMarginSpan, LineHeightSpan 27 { DrawableMarginSpan(Drawable b)28 public DrawableMarginSpan(Drawable b) { 29 mDrawable = b; 30 } 31 DrawableMarginSpan(Drawable b, int pad)32 public DrawableMarginSpan(Drawable b, int pad) { 33 mDrawable = b; 34 mPad = pad; 35 } 36 getLeadingMargin(boolean first)37 public int getLeadingMargin(boolean first) { 38 return mDrawable.getIntrinsicWidth() + mPad; 39 } 40 drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout layout)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 ix = (int)x; 47 int itop = (int)layout.getLineTop(layout.getLineForOffset(st)); 48 49 int dw = mDrawable.getIntrinsicWidth(); 50 int dh = mDrawable.getIntrinsicHeight(); 51 52 // XXX What to do about Paint? 53 mDrawable.setBounds(ix, itop, ix+dw, itop+dh); 54 mDrawable.draw(c); 55 } 56 chooseHeight(CharSequence text, int start, int end, int istartv, int v, Paint.FontMetricsInt fm)57 public void chooseHeight(CharSequence text, int start, int end, 58 int istartv, int v, 59 Paint.FontMetricsInt fm) { 60 if (end == ((Spanned) text).getSpanEnd(this)) { 61 int ht = mDrawable.getIntrinsicHeight(); 62 63 int need = ht - (v + fm.descent - fm.ascent - istartv); 64 if (need > 0) 65 fm.descent += need; 66 67 need = ht - (v + fm.bottom - fm.top - istartv); 68 if (need > 0) 69 fm.bottom += need; 70 } 71 } 72 73 private Drawable mDrawable; 74 private int mPad; 75 } 76