mobile/android/base/gfx/TextLayer.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/gfx/TextLayer.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,69 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko.gfx;
    1.10 +
    1.11 +import org.mozilla.gecko.mozglue.DirectBufferAllocator;
    1.12 +
    1.13 +import android.graphics.Bitmap;
    1.14 +import android.graphics.Canvas;
    1.15 +import android.graphics.Color;
    1.16 +import android.graphics.Paint;
    1.17 +import android.graphics.Typeface;
    1.18 +
    1.19 +import java.nio.ByteBuffer;
    1.20 +
    1.21 +/**
    1.22 + * Draws text on a layer. This is used for the frame rate meter.
    1.23 + */
    1.24 +public class TextLayer extends SingleTileLayer {
    1.25 +    private final ByteBuffer mBuffer;   // this buffer is owned by the BufferedCairoImage
    1.26 +    private final IntSize mSize;
    1.27 +
    1.28 +    /*
    1.29 +     * This awkward pattern is necessary due to Java's restrictions on when one can call superclass
    1.30 +     * constructors.
    1.31 +     */
    1.32 +    private TextLayer(ByteBuffer buffer, BufferedCairoImage image, IntSize size, String text) {
    1.33 +        super(false, image);
    1.34 +        mBuffer = buffer;
    1.35 +        mSize = size;
    1.36 +        renderText(text);
    1.37 +    }
    1.38 +
    1.39 +    public static TextLayer create(IntSize size, String text) {
    1.40 +        ByteBuffer buffer = DirectBufferAllocator.allocate(size.width * size.height * 4);
    1.41 +        BufferedCairoImage image = new BufferedCairoImage(buffer, size.width, size.height,
    1.42 +                                                          CairoImage.FORMAT_ARGB32);
    1.43 +        return new TextLayer(buffer, image, size, text);
    1.44 +    }
    1.45 +
    1.46 +    public void setText(String text) {
    1.47 +        renderText(text);
    1.48 +        invalidate();
    1.49 +    }
    1.50 +
    1.51 +    private void renderText(String text) {
    1.52 +        Bitmap bitmap = Bitmap.createBitmap(mSize.width, mSize.height, Bitmap.Config.ARGB_8888);
    1.53 +        Canvas canvas = new Canvas(bitmap);
    1.54 +
    1.55 +        Paint textPaint = new Paint();
    1.56 +        textPaint.setAntiAlias(true);
    1.57 +        textPaint.setColor(Color.WHITE);
    1.58 +        textPaint.setFakeBoldText(true);
    1.59 +        textPaint.setTextSize(18.0f);
    1.60 +        textPaint.setTypeface(Typeface.DEFAULT_BOLD);
    1.61 +        float width = textPaint.measureText(text) + 18.0f;
    1.62 +
    1.63 +        Paint backgroundPaint = new Paint();
    1.64 +        backgroundPaint.setColor(Color.argb(127, 0, 0, 0));
    1.65 +        canvas.drawRect(0.0f, 0.0f, width, 18.0f + 6.0f, backgroundPaint);
    1.66 +
    1.67 +        canvas.drawText(text, 6.0f, 18.0f, textPaint);
    1.68 +
    1.69 +        bitmap.copyPixelsToBuffer(mBuffer.asIntBuffer());
    1.70 +    }
    1.71 +}
    1.72 +

mercurial