1/*
2 * Copyright (C) 2009 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 com.cooliris.media;
18
19import android.graphics.Bitmap;
20import android.graphics.Canvas;
21import android.graphics.Color;
22import android.graphics.Paint;
23import android.graphics.Rect;
24import android.graphics.Typeface;
25
26public final class SimpleStringTexture extends Texture {
27
28    private final String mString;
29    private final StringTexture.Config mConfig;
30    private float mBaselineHeight = 0.0f;
31
32    SimpleStringTexture(String string, StringTexture.Config config) {
33        mString = string;
34        mConfig = config;
35    }
36
37    public float getBaselineHeight() {
38        return mBaselineHeight;
39    }
40
41    @Override
42    public boolean isCached() {
43        return true;
44    }
45
46    @Override
47    protected Bitmap load(RenderView view) {
48        // Configure paint.
49        StringTexture.Config config = mConfig;
50        Paint paint = new Paint();
51        paint.setAntiAlias(true);
52        paint.setColor(Shared.argb(config.a, config.r, config.g, config.b));
53        paint.setShadowLayer(config.shadowRadius, 0f, 0f, Color.BLACK);
54        paint.setTypeface(config.bold ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
55        paint.setTextSize(config.fontSize);
56        paint.setUnderlineText(config.underline);
57        paint.setStrikeThruText(config.strikeThrough);
58        if (config.italic) {
59            paint.setTextSkewX(-0.25f);
60        }
61
62        // Measure string.
63        String string = mString;
64        Rect bounds = new Rect();
65        paint.getTextBounds(string, 0, string.length(), bounds);
66
67        // Get font metrics.
68        Paint.FontMetricsInt metrics = paint.getFontMetricsInt();
69        int height = metrics.bottom - metrics.top;
70
71        // Draw string into bitmap with a 1px margin for anti-aliasing.
72        // Ensure baseline alignment with other strings of the same size.
73        int padding = 1 + config.shadowRadius;
74        Bitmap bitmap = Bitmap
75                .createBitmap(bounds.width() + padding + padding, height + padding + padding, Bitmap.Config.ARGB_8888);
76        Canvas canvas = new Canvas(bitmap);
77        canvas.translate(padding, padding - metrics.ascent);
78        canvas.drawText(string, 0, 0, paint);
79
80        mBaselineHeight = padding + metrics.bottom;
81
82        return bitmap;
83    }
84
85}
86