LibreOffice Module android (master) 1
TextLayer.java
Go to the documentation of this file.
1/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6package org.mozilla.gecko.gfx;
7
8import android.graphics.Bitmap;
9import android.graphics.Canvas;
10import android.graphics.Color;
11import android.graphics.Paint;
12import android.graphics.Typeface;
13
15
16import java.nio.ByteBuffer;
17
21public class TextLayer extends SingleTileLayer {
22 private final ByteBuffer mBuffer; // this buffer is owned by the BufferedCairoImage
23 private final IntSize mSize;
24
25 /*
26 * This awkward pattern is necessary due to Java's restrictions on when one can call superclass
27 * constructors.
28 */
29 private TextLayer(ByteBuffer buffer, BufferedCairoImage image, IntSize size, String text) {
30 super(false, image);
31 mBuffer = buffer;
32 mSize = size;
34 }
35
36 public static TextLayer create(IntSize size, String text) {
37 ByteBuffer buffer = DirectBufferAllocator.allocate(size.width * size.height * 4);
38 BufferedCairoImage image = new BufferedCairoImage(buffer, size.width, size.height,
40 return new TextLayer(buffer, image, size, text);
41 }
42
43 public void setText(String text) {
45 invalidate();
46 }
47
48 private void renderText(String text) {
49 Bitmap bitmap = Bitmap.createBitmap(mSize.width, mSize.height, Bitmap.Config.ARGB_8888);
50 Canvas canvas = new Canvas(bitmap);
51
52 Paint textPaint = new Paint();
53 textPaint.setAntiAlias(true);
54 textPaint.setColor(Color.WHITE);
55 textPaint.setFakeBoldText(true);
56 textPaint.setTextSize(18.0f);
57 textPaint.setTypeface(Typeface.DEFAULT_BOLD);
58 float width = textPaint.measureText(text) + 18.0f;
59
60 Paint backgroundPaint = new Paint();
61 backgroundPaint.setColor(Color.argb(127, 0, 0, 0));
62 canvas.drawRect(0.0f, 0.0f, width, 18.0f + 6.0f, backgroundPaint);
63
64 canvas.drawText(text, 6.0f, 18.0f, textPaint);
65
66 bitmap.copyPixelsToBuffer(mBuffer.asIntBuffer());
67 }
68}
69
A Cairo image that simply saves a buffer of pixel data.
Encapsulates the logic needed to draw a single textured tile.
Draws text on a layer.
Definition: TextLayer.java:21
static TextLayer create(IntSize size, String text)
Definition: TextLayer.java:36
void renderText(String text)
Definition: TextLayer.java:48
TextLayer(ByteBuffer buffer, BufferedCairoImage image, IntSize size, String text)
Definition: TextLayer.java:29
void invalidate()
Invalidates the entire buffer so that it will be uploaded again.
Definition: TileLayer.java:76
def text(shape, orig_st)
size