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