Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] - add platform specific implementation of unaccent
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Sep 5, 2018
1 parent 3fd2f98 commit 64bd64c
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mapbox.mapboxsdk.utils;

import android.support.annotation.Keep;
import android.support.annotation.NonNull;

import java.text.Normalizer;

/**
* String utility class used by core from jni.
*/
@Keep
class StringUtils {

/**
* Normalises String input and strip diacritics from it.
*
* @return normalised String with stripped diacritics.
*/
@Keep
@NonNull
static String unaccent(@NonNull String value) {
return Normalizer.normalize(value, Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class RenderTestActivity extends AppCompatActivity {
add("overlapping,raster-masking");
add("missing,raster-loading");
add("pitchAndBearing,line-pitch");
add("overdraw,sparse-tileset");
}
};

Expand Down Expand Up @@ -292,7 +293,7 @@ public void setOnRenderTestCompletionListener(OnRenderTestCompletionListener lis
}

public void onLoadIgnoreList(List<String> ignoreList) {
Timber.e("We loaded %s amount of tests to be ignored", ignoreList.size());
Timber.e("We loaded %s of tests to be ignored", ignoreList.size());
EXCLUDED_TESTS.addAll(ignoreList);
new LoadRenderDefinitionTask(this).execute();
}
Expand Down
2 changes: 0 additions & 2 deletions platform/android/config.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
set(USE_GLES2 ON)

include(cmake/nunicode.cmake)
include(cmake/sqlite.cmake)
include(cmake/icu.cmake)

Expand Down Expand Up @@ -49,7 +48,6 @@ macro(mbgl_platform_core)
target_add_mason_package(mbgl-core PUBLIC rapidjson)

target_link_libraries(mbgl-core
PRIVATE nunicode
PRIVATE icu
PUBLIC expected
PUBLIC -llog
Expand Down
2 changes: 1 addition & 1 deletion platform/android/core-files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ platform/android/src/thread.cpp
platform/android/src/string_util.cpp
platform/default/bidi.cpp
platform/default/thread_local.cpp
platform/default/unaccent.cpp
platform/android/src/unaccent.cpp
platform/default/unaccent.hpp
platform/default/utf.cpp

Expand Down
2 changes: 1 addition & 1 deletion platform/android/scripts/run-render-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import shutil

catPath = os.getcwd() + "/platform/android/build/render-test/render/"
catPath = os.getcwd() + "/platform/android/build/render-test/"
failCounter = 0
testCounter = 0
for cat in os.listdir(catPath):
Expand Down
1 change: 1 addition & 0 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ void registerNatives(JavaVM *vm) {
LocalGlyphRasterizer::registerNative(env);
Locale::registerNative(env);
Collator::registerNative(env);
StringUtils::registerNative(env);

// Logger
Logger::registerNative(env);
Expand Down
14 changes: 14 additions & 0 deletions platform/android/src/text/collator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <unaccent.hpp>

#include <jni/jni.hpp>
#include "src/java/lang.hpp"

#include "../attach_env.hpp"
#include "collator_jni.hpp"
Expand Down Expand Up @@ -36,6 +37,19 @@ jni::jint Collator::compare(jni::JNIEnv& env, jni::Object<Collator> collator, jn
return collator.Call(env, method, lhs, rhs);
}


void StringUtils::registerNative(jni::JNIEnv& env) {
javaClass = *jni::Class<StringUtils>::Find(env).NewGlobalRef(env).release();
}

jni::Class<StringUtils> StringUtils::javaClass;

jni::String StringUtils::unaccent(jni::JNIEnv& env, jni::String value) {
using Signature = jni::String(jni::String);
auto static method = javaClass.GetStaticMethod<Signature>(env, "unaccent");
return javaClass.Call(env, method, value);
}

void Locale::registerNative(jni::JNIEnv& env) {
javaClass = *jni::Class<Locale>::Find(env).NewGlobalRef(env).release();
}
Expand Down
13 changes: 13 additions & 0 deletions platform/android/src/text/collator_jni.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,18 @@ class Collator {

};


class StringUtils {
public:
static constexpr auto Name() { return "com/mapbox/mapboxsdk/utils/StringUtils"; };

static jni::String unaccent(jni::JNIEnv&, jni::String);

static jni::Class<StringUtils> javaClass;

static void registerNative(jni::JNIEnv&);

};

} // namespace android
} // namespace mbgl
21 changes: 21 additions & 0 deletions platform/android/src/unaccent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <unaccent.hpp>
#include <string>
#include <src/java/lang.hpp>
#include "attach_env.hpp"
#include "text/collator_jni.hpp"

namespace mbgl {
namespace platform {

std::string unaccent(const std::string& str) {
android::UniqueEnv env = android::AttachEnv();
jni::String input = jni::Make<jni::String>(*env, str);
jni::String unaccented = android::StringUtils::unaccent(*env, input);
jni::DeleteLocalRef(*env, input);
std::string output = jni::Make<std::string>(*env, unaccented);
jni::DeleteLocalRef(*env, unaccented);
return output;
}

} // namespace platform
} // namespace mbgl

0 comments on commit 64bd64c

Please sign in to comment.