android/GlueIOIOPort: fix spurious errors after IOIO baud rate change
[xcsoar.git] / android / src / TextUtil.java
blob2de91e83bc125e1f786687bf92537ea71b9cb642
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 /* TextUtil.java - Android text handling to be used by C++ Code via jni.
27 package org.xcsoar;
29 import android.graphics.Rect;
30 import android.graphics.Paint;
31 import android.graphics.Typeface;
32 import android.graphics.Canvas;
33 import android.graphics.Bitmap;
34 import android.graphics.Color;
36 public class TextUtil {
37 private Paint paint;
38 private Paint.FontMetricsInt metrics;
39 private int[] extent = new int[2];
40 private int[] id = new int[3];
42 public TextUtil(String family_name, int style, int textSize) {
43 Typeface tf = Typeface.create(family_name, style);
44 paint = new Paint(Paint.ANTI_ALIAS_FLAG);
45 paint.setTypeface(tf);
46 paint.setTextSize(textSize);
47 if ((style & Typeface.ITALIC) != 0 && !tf.isItalic())
48 paint.setTextSkewX((float) -0.2);
50 metrics = paint.getFontMetricsInt();
53 public void getFontMetrics(int[] metrics) {
54 Rect bounds = new Rect();
55 char[] m = new char[1];
56 m[0] = 'M';
57 paint.getTextBounds(m, 0, 1, bounds);
59 metrics[0] = Math.round(paint.descent() - paint.ascent());
60 metrics[1] = paint.getTypeface().getStyle();
61 metrics[2] = Math.round(-paint.ascent());
62 metrics[3] = bounds.height();
63 metrics[4] = Math.round(paint.getFontSpacing());
66 public int[] getTextBounds(String text) {
67 /* we cannot simply use getTextBounds() here, because xcsoar will not
68 * know where the baseline of the text is inside the texture
70 extent[0] = Math.round(paint.measureText(text, 0, text.length()));
71 extent[1] = metrics.descent - metrics.ascent;
72 return extent;
75 public int[] getTextTextureGL(String text) {
76 getTextBounds(text);
78 // draw text into a bitmap
79 Bitmap bmp = Bitmap.createBitmap(extent[0], extent[1],
80 Bitmap.Config.ALPHA_8);
81 bmp.eraseColor(Color.TRANSPARENT);
82 paint.setColor(Color.WHITE);
83 Canvas canvas = new Canvas(bmp);
84 canvas.drawText(text, 0, -paint.getFontMetricsInt().ascent, paint);
86 // create OpenGL texture
87 if (!BitmapUtil.bitmapToOpenGL(bmp, id))
88 return null;
90 return id;