The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.text.Spanned; |
| 23 | import android.text.Layout; |
| 24 | |
| 25 | public class DrawableMarginSpan |
| 26 | implements LeadingMarginSpan, LineHeightSpan |
| 27 | { |
| 28 | public DrawableMarginSpan(Drawable b) { |
| 29 | mDrawable = b; |
| 30 | } |
| 31 | |
| 32 | public DrawableMarginSpan(Drawable b, int pad) { |
| 33 | mDrawable = b; |
| 34 | mPad = pad; |
| 35 | } |
| 36 | |
| 37 | public int getLeadingMargin(boolean first) { |
| 38 | return mDrawable.getIntrinsicWidth() + 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 ix = (int)x; |
| 47 | int itop = (int)layout.getLineTop(layout.getLineForOffset(st)); |
| 48 | |
| 49 | int dw = mDrawable.getIntrinsicWidth(); |
| 50 | int dh = mDrawable.getIntrinsicHeight(); |
| 51 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | // XXX What to do about Paint? |
| 53 | mDrawable.setBounds(ix, itop, ix+dw, itop+dh); |
| 54 | mDrawable.draw(c); |
| 55 | } |
| 56 | |
| 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 | } |