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