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
);
38 TextUtil::Initialise(JNIEnv
*_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");
53 TextUtil::Deinitialise(JNIEnv
*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
);
66 env
->GetIntArrayRegion(metricsArray
, 0, 5, metrics
);
69 ascent_height
= metrics
[2];
70 capital_height
= metrics
[3];
71 line_spacing
= metrics
[4];
73 // free local references
74 env
->DeleteLocalRef(metricsArray
);
78 TextUtil::create(const char *facename
, int height
, bool bold
, bool italic
)
81 jint paramStyle
, paramTextSize
;
83 Java::String
paramFamilyName(env
, facename
);
89 paramTextSize
= height
;
91 // construct org.xcsoar.TextUtil object
92 localObject
= env
->NewObject(cls
, midTextUtil
,
93 paramFamilyName
.Get(),
94 paramStyle
, paramTextSize
);
98 TextUtil
*tu
= new TextUtil(localObject
);
100 env
->DeleteLocalRef(localObject
);
106 TextUtil::getTextBounds(const char *text
) const
110 Java::String
text2(env
, text
);
111 jintArray paramExtent
= (jintArray
)
112 env
->CallObjectMethod(Get(), midGetTextBounds
,
114 if (!Java::DiscardException(env
)) {
115 env
->GetIntArrayRegion(paramExtent
, 0, 2, extent
);
116 env
->DeleteLocalRef(paramExtent
);
118 /* Java exception has occurred; return zeroes */
123 return { extent
[0], extent
[1] };
127 TextUtil::getTextTextureGL(const char *text
) const
129 Java::String
text2(env
, text
);
130 jintArray jresult
= (jintArray
)
131 env
->CallObjectMethod(Get(), midGetTextTextureGL
,
134 if (!Java::DiscardException(env
) && jresult
!= nullptr) {
135 env
->GetIntArrayRegion(jresult
, 0, 3, result
);
136 env
->DeleteLocalRef(jresult
);
138 result
[0] = result
[1] = result
[2] = 0;
141 return Texture(result
[0], result
[1], result
[2]);