android/GlueIOIOPort: fix spurious errors after IOIO baud rate change
[xcsoar.git] / src / Android / TextUtil.cpp
blob91c700ff66a33f134e358ba1aafcf516cc07c136
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 #include "Android/TextUtil.hpp"
25 #include "Java/Class.hpp"
26 #include "Java/String.hpp"
27 #include "Java/Exception.hpp"
28 #include "Screen/Point.hpp"
30 JNIEnv *TextUtil::env(NULL);
31 static Java::TrivialClass cls;
32 jmethodID TextUtil::midTextUtil(NULL);
33 jmethodID TextUtil::midGetFontMetrics(NULL);
34 jmethodID TextUtil::midGetTextBounds(NULL);
35 jmethodID TextUtil::midGetTextTextureGL(NULL);
37 void
38 TextUtil::Initialise(JNIEnv *_env)
40 env = _env;
42 cls.Find(env, "org/xcsoar/TextUtil");
44 midTextUtil = env->GetMethodID(cls, "<init>", "(Ljava/lang/String;II)V");
45 midGetFontMetrics = env->GetMethodID(cls, "getFontMetrics", "([I)V");
46 midGetTextBounds = env->GetMethodID(cls, "getTextBounds",
47 "(Ljava/lang/String;)[I");
48 midGetTextTextureGL = env->GetMethodID(cls, "getTextTextureGL",
49 "(Ljava/lang/String;)[I");
52 void
53 TextUtil::Deinitialise(JNIEnv *env)
55 cls.Clear(env);
58 TextUtil::TextUtil(jobject _obj)
59 :Java::Object(env, _obj) {
60 // get height, ascent_height and capital_height
61 assert(midGetFontMetrics);
62 jintArray metricsArray = env->NewIntArray(5);
63 env->CallVoidMethod(Get(), midGetFontMetrics, metricsArray);
65 jint metrics[5];
66 env->GetIntArrayRegion(metricsArray, 0, 5, metrics);
67 height = metrics[0];
68 style = metrics[1];
69 ascent_height = metrics[2];
70 capital_height = metrics[3];
71 line_spacing = metrics[4];
73 // free local references
74 env->DeleteLocalRef(metricsArray);
77 TextUtil *
78 TextUtil::create(const char *facename, int height, bool bold, bool italic)
80 jobject localObject;
81 jint paramStyle, paramTextSize;
83 Java::String paramFamilyName(env, facename);
84 paramStyle = 0;
85 if (bold)
86 paramStyle |= 1;
87 if (italic)
88 paramStyle |= 2;
89 paramTextSize = height;
91 // construct org.xcsoar.TextUtil object
92 localObject = env->NewObject(cls, midTextUtil,
93 paramFamilyName.Get(),
94 paramStyle, paramTextSize);
95 if (!localObject)
96 return NULL;
98 TextUtil *tu = new TextUtil(localObject);
100 env->DeleteLocalRef(localObject);
102 return tu;
105 PixelSize
106 TextUtil::getTextBounds(const char *text) const
108 jint extent[2];
110 Java::String text2(env, text);
111 jintArray paramExtent = (jintArray)
112 env->CallObjectMethod(Get(), midGetTextBounds,
113 text2.Get());
114 if (!Java::DiscardException(env)) {
115 env->GetIntArrayRegion(paramExtent, 0, 2, extent);
116 env->DeleteLocalRef(paramExtent);
117 } else {
118 /* Java exception has occurred; return zeroes */
119 extent[0] = 0;
120 extent[1] = 0;
123 return { extent[0], extent[1] };
126 TextUtil::Texture
127 TextUtil::getTextTextureGL(const char *text) const
129 Java::String text2(env, text);
130 jintArray jresult = (jintArray)
131 env->CallObjectMethod(Get(), midGetTextTextureGL,
132 text2.Get());
133 jint result[3];
134 if (!Java::DiscardException(env) && jresult != nullptr) {
135 env->GetIntArrayRegion(jresult, 0, 3, result);
136 env->DeleteLocalRef(jresult);
137 } else {
138 result[0] = result[1] = result[2] = 0;
141 return Texture(result[0], result[1], result[2]);