[android] Reuse Bitmap, Paint & Canvas instances in LocalGlyphRasterizer
- Make LocalGlyphRasterizer.drawGlyphBitmap() non-static - Store and reuse Bitmap, Paint & Canvas instances
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package com.mapbox.mapboxsdk.text;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.Typeface;
|
||||
import android.support.annotation.Keep;
|
||||
import android.support.annotation.WorkerThread;
|
||||
@@ -12,6 +14,25 @@ import android.support.annotation.WorkerThread;
|
||||
* by the portable local_glyph_rasterizer.hpp
|
||||
*/
|
||||
public class LocalGlyphRasterizer {
|
||||
private final Bitmap bitmap;
|
||||
private final Paint paint;
|
||||
private final Canvas canvas;
|
||||
|
||||
LocalGlyphRasterizer() {
|
||||
/*
|
||||
35x35px dimensions are hardwired to match local_glyph_rasterizer.cpp
|
||||
These dimensions are large enough to draw a 24 point character in the middle
|
||||
of the bitmap (y: 20) with some buffer around the edge
|
||||
*/
|
||||
bitmap = Bitmap.createBitmap(35, 35, Bitmap.Config.ARGB_8888);
|
||||
|
||||
paint = new Paint();
|
||||
paint.setAntiAlias(true);
|
||||
paint.setTextSize(24);
|
||||
|
||||
canvas = new Canvas();
|
||||
canvas.setBitmap(bitmap);
|
||||
}
|
||||
|
||||
/***
|
||||
* Uses Android-native drawing code to rasterize a single glyph
|
||||
@@ -26,23 +47,10 @@ public class LocalGlyphRasterizer {
|
||||
*/
|
||||
@WorkerThread
|
||||
@Keep
|
||||
protected static Bitmap drawGlyphBitmap(String fontFamily, boolean bold, char glyphID) {
|
||||
/*
|
||||
35x35px dimensions are hardwired to match local_glyph_rasterizer.cpp
|
||||
These dimensions are large enough to draw a 24 point character in the middle
|
||||
of the bitmap (y: 20) with some buffer around the edge
|
||||
*/
|
||||
Bitmap bitmap = Bitmap.createBitmap(35, 35, Bitmap.Config.ARGB_8888);
|
||||
|
||||
Paint paint = new Paint();
|
||||
paint.setAntiAlias(true);
|
||||
paint.setTextSize(24);
|
||||
protected Bitmap drawGlyphBitmap(String fontFamily, boolean bold, char glyphID) {
|
||||
paint.setTypeface(Typeface.create(fontFamily, bold ? Typeface.BOLD : Typeface.NORMAL));
|
||||
|
||||
Canvas canvas = new Canvas();
|
||||
canvas.setBitmap(bitmap);
|
||||
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
|
||||
canvas.drawText(String.valueOf(glyphID), 0, 20, paint);
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,19 +33,27 @@
|
||||
namespace mbgl {
|
||||
namespace android {
|
||||
|
||||
LocalGlyphRasterizer::LocalGlyphRasterizer() {
|
||||
UniqueEnv env { AttachEnv() };
|
||||
|
||||
static auto constructor = javaClass.GetConstructor(*env);
|
||||
|
||||
javaObject = javaClass.New(*env, constructor).NewGlobalRef(*env);
|
||||
}
|
||||
|
||||
PremultipliedImage LocalGlyphRasterizer::drawGlyphBitmap(const std::string& fontFamily, const bool bold, const GlyphID glyphID) {
|
||||
UniqueEnv env { AttachEnv() };
|
||||
|
||||
using Signature = jni::Object<Bitmap>(jni::String, jni::jboolean, jni::jchar);
|
||||
auto method = javaClass.GetStaticMethod<Signature>(*env, "drawGlyphBitmap");
|
||||
static auto method = javaClass.GetMethod<Signature>(*env, "drawGlyphBitmap");
|
||||
|
||||
jni::String jniFontFamily = jni::Make<jni::String>(*env, fontFamily);
|
||||
|
||||
auto javaBitmap = javaClass.Call(*env,
|
||||
method,
|
||||
jniFontFamily,
|
||||
static_cast<jni::jboolean>(bold),
|
||||
static_cast<jni::jchar>(glyphID));
|
||||
auto javaBitmap = javaObject->Call(*env,
|
||||
method,
|
||||
jniFontFamily,
|
||||
static_cast<jni::jboolean>(bold),
|
||||
static_cast<jni::jchar>(glyphID));
|
||||
jni::DeleteLocalRef(*env, jniFontFamily);
|
||||
|
||||
PremultipliedImage result = Bitmap::GetImage(*env, javaBitmap);
|
||||
@@ -77,13 +85,15 @@ public:
|
||||
std::string lowercaseFont = platform::lowercase(font);
|
||||
if (lowercaseFont.find("bold") != std::string::npos) {
|
||||
bold = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return android::LocalGlyphRasterizer::drawGlyphBitmap(*fontFamily, bold, glyphID);
|
||||
return androidLocalGlyphRasterizer.drawGlyphBitmap(*fontFamily, bold, glyphID);
|
||||
}
|
||||
|
||||
private:
|
||||
optional<std::string> fontFamily;
|
||||
android::LocalGlyphRasterizer androidLocalGlyphRasterizer;
|
||||
};
|
||||
|
||||
LocalGlyphRasterizer::LocalGlyphRasterizer(const optional<std::string> fontFamily)
|
||||
|
||||
@@ -17,14 +17,18 @@ namespace android {
|
||||
|
||||
class LocalGlyphRasterizer {
|
||||
public:
|
||||
static PremultipliedImage drawGlyphBitmap(const std::string& fontFamily, const bool bold, const char16_t glyphID);
|
||||
|
||||
static constexpr auto Name() { return "com/mapbox/mapboxsdk/text/LocalGlyphRasterizer"; };
|
||||
|
||||
static jni::Class<LocalGlyphRasterizer> javaClass;
|
||||
|
||||
static void registerNative(jni::JNIEnv&);
|
||||
|
||||
LocalGlyphRasterizer();
|
||||
|
||||
PremultipliedImage drawGlyphBitmap(const std::string& fontFamily, const bool bold, const char16_t glyphID);
|
||||
|
||||
private:
|
||||
jni::UniqueObject<LocalGlyphRasterizer> javaObject;
|
||||
};
|
||||
|
||||
} // namespace android
|
||||
|
||||
Reference in New Issue
Block a user