Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.gfx; |
michael@0 | 7 | |
michael@0 | 8 | import org.mozilla.gecko.mozglue.DirectBufferAllocator; |
michael@0 | 9 | |
michael@0 | 10 | import android.graphics.Bitmap; |
michael@0 | 11 | import android.graphics.Canvas; |
michael@0 | 12 | import android.graphics.Color; |
michael@0 | 13 | import android.graphics.Paint; |
michael@0 | 14 | import android.graphics.Typeface; |
michael@0 | 15 | |
michael@0 | 16 | import java.nio.ByteBuffer; |
michael@0 | 17 | |
michael@0 | 18 | /** |
michael@0 | 19 | * Draws text on a layer. This is used for the frame rate meter. |
michael@0 | 20 | */ |
michael@0 | 21 | public class TextLayer extends SingleTileLayer { |
michael@0 | 22 | private final ByteBuffer mBuffer; // this buffer is owned by the BufferedCairoImage |
michael@0 | 23 | private final IntSize mSize; |
michael@0 | 24 | |
michael@0 | 25 | /* |
michael@0 | 26 | * This awkward pattern is necessary due to Java's restrictions on when one can call superclass |
michael@0 | 27 | * constructors. |
michael@0 | 28 | */ |
michael@0 | 29 | private TextLayer(ByteBuffer buffer, BufferedCairoImage image, IntSize size, String text) { |
michael@0 | 30 | super(false, image); |
michael@0 | 31 | mBuffer = buffer; |
michael@0 | 32 | mSize = size; |
michael@0 | 33 | renderText(text); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | public static TextLayer create(IntSize size, String text) { |
michael@0 | 37 | ByteBuffer buffer = DirectBufferAllocator.allocate(size.width * size.height * 4); |
michael@0 | 38 | BufferedCairoImage image = new BufferedCairoImage(buffer, size.width, size.height, |
michael@0 | 39 | CairoImage.FORMAT_ARGB32); |
michael@0 | 40 | return new TextLayer(buffer, image, size, text); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | public void setText(String text) { |
michael@0 | 44 | renderText(text); |
michael@0 | 45 | invalidate(); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | private void renderText(String text) { |
michael@0 | 49 | Bitmap bitmap = Bitmap.createBitmap(mSize.width, mSize.height, Bitmap.Config.ARGB_8888); |
michael@0 | 50 | Canvas canvas = new Canvas(bitmap); |
michael@0 | 51 | |
michael@0 | 52 | Paint textPaint = new Paint(); |
michael@0 | 53 | textPaint.setAntiAlias(true); |
michael@0 | 54 | textPaint.setColor(Color.WHITE); |
michael@0 | 55 | textPaint.setFakeBoldText(true); |
michael@0 | 56 | textPaint.setTextSize(18.0f); |
michael@0 | 57 | textPaint.setTypeface(Typeface.DEFAULT_BOLD); |
michael@0 | 58 | float width = textPaint.measureText(text) + 18.0f; |
michael@0 | 59 | |
michael@0 | 60 | Paint backgroundPaint = new Paint(); |
michael@0 | 61 | backgroundPaint.setColor(Color.argb(127, 0, 0, 0)); |
michael@0 | 62 | canvas.drawRect(0.0f, 0.0f, width, 18.0f + 6.0f, backgroundPaint); |
michael@0 | 63 | |
michael@0 | 64 | canvas.drawText(text, 6.0f, 18.0f, textPaint); |
michael@0 | 65 | |
michael@0 | 66 | bitmap.copyPixelsToBuffer(mBuffer.asIntBuffer()); |
michael@0 | 67 | } |
michael@0 | 68 | } |
michael@0 | 69 |