This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Juha Alanen
committed
Jun 27, 2019
1 parent
98a5c64
commit 50bdbea
Showing
25 changed files
with
533 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
|
||
#include <mbgl/style/expression/expression.hpp> | ||
#include <mbgl/style/expression/parsing_context.hpp> | ||
|
||
namespace mbgl { | ||
namespace style { | ||
namespace expression { | ||
|
||
class NumberFormat final : public Expression { | ||
public: | ||
NumberFormat(std::unique_ptr<Expression> number_, | ||
std::unique_ptr<Expression> locale_, | ||
std::unique_ptr<Expression> currency_, | ||
std::unique_ptr<Expression> minFractionDigits_, | ||
std::unique_ptr<Expression> maxFractionDigits_); | ||
|
||
~NumberFormat(); | ||
|
||
static ParseResult parse(const mbgl::style::conversion::Convertible& value, ParsingContext& ctx); | ||
|
||
EvaluationResult evaluate(const EvaluationContext& params) const override; | ||
void eachChild(const std::function<void(const Expression&)>& visit) const override; | ||
bool operator==(const Expression& e) const override; | ||
std::vector<optional<Value>> possibleOutputs() const override; | ||
|
||
mbgl::Value serialize() const override; | ||
std::string getOperator() const override { return "number-format"; } | ||
|
||
private: | ||
std::unique_ptr<Expression> number; | ||
std::unique_ptr<Expression> locale; | ||
std::unique_ptr<Expression> currency; | ||
std::unique_ptr<Expression> minFractionDigits; | ||
std::unique_ptr<Expression> maxFractionDigits; | ||
}; | ||
|
||
} // namespace expression | ||
} // namespace style | ||
} // namespace mbgl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include <mbgl/style/expression/collator.hpp> | ||
#include <mbgl/text/language_tag.hpp> | ||
#include <mbgl/util/platform.hpp> | ||
|
||
#include <jni/jni.hpp> | ||
|
||
#include "../attach_env.hpp" | ||
#include "format_number_jni.hpp" | ||
|
||
namespace mbgl { | ||
namespace android { | ||
|
||
void NumberFormat::registerNative(jni::JNIEnv& env) { | ||
jni::Class<NumberFormat>::Singleton(env); | ||
} | ||
|
||
jni::Local<jni::Object<NumberFormat>> NumberFormat::getInstance(jni::JNIEnv& env, const jni::Object<Locale>& locale) { | ||
static auto& javaClass = jni::Class<NumberFormat>::Singleton(env); | ||
static auto method = javaClass.GetStaticMethod<jni::Object<NumberFormat> (jni::Object<Locale>)>(env, "getInstance"); | ||
return javaClass.Call(env, method, locale); | ||
} | ||
|
||
jni::Local<jni::Object<NumberFormat>> NumberFormat::getCurrencyInstance(jni::JNIEnv& env, const jni::Object<Locale>& locale) { | ||
static auto& javaClass = jni::Class<NumberFormat>::Singleton(env); | ||
static auto method = javaClass.GetStaticMethod<jni::Object<NumberFormat> (jni::Object<Locale>)>(env, "getCurrencyInstance"); | ||
return javaClass.Call(env, method, locale); | ||
} | ||
|
||
jni::Local<jni::String> NumberFormat::format(jni::JNIEnv& env, const jni::Object<NumberFormat>& nf, jni::jdouble number) { | ||
static auto& javaClass = jni::Class<NumberFormat>::Singleton(env); | ||
static auto method = javaClass.GetMethod<jni::String (jni::jdouble)>(env, "format"); | ||
return nf.Call(env, method, number); | ||
} | ||
|
||
void NumberFormat::setMinimumFractionDigits(jni::JNIEnv& env, const jni::Object<NumberFormat>& nf, jni::jint value) { | ||
static auto& javaClass = jni::Class<NumberFormat>::Singleton(env); | ||
static auto method = javaClass.GetMethod<void (jni::jint)>(env, "setMinimumFractionDigits"); | ||
return nf.Call(env, method, value); | ||
} | ||
|
||
void NumberFormat::setMaximumFractionDigits(jni::JNIEnv& env, const jni::Object<NumberFormat>& nf, jni::jint value) { | ||
static auto& javaClass = jni::Class<NumberFormat>::Singleton(env); | ||
static auto method = javaClass.GetMethod<void (jni::jint)>(env, "setMaximumFractionDigits"); | ||
return nf.Call(env, method, value); | ||
} | ||
|
||
} // namespace android | ||
|
||
namespace platform { | ||
|
||
std::string formatNumber(double number, const std::string& localeId, const std::string& currency, | ||
uint8_t minFractionDigits, uint8_t maxFractionDigits) { | ||
|
||
auto env{ android::AttachEnv() }; | ||
|
||
jni::Global<jni::Object<android::Locale>> locale; | ||
LanguageTag languageTag = !localeId.empty() ? LanguageTag::fromBCP47(localeId) : LanguageTag(); | ||
if (!languageTag.language) { | ||
locale = jni::NewGlobal(*env, android::Locale::getDefault(*env)); | ||
} else if (!languageTag.region) { | ||
locale = jni::NewGlobal(*env, android::Locale::New(*env, jni::Make<jni::String>(*env, *languageTag.language))); | ||
} else { | ||
locale = jni::NewGlobal(*env, android::Locale::New(*env, jni::Make<jni::String>(*env, *languageTag.language), | ||
jni::Make<jni::String>(*env, *languageTag.region))); | ||
} | ||
|
||
jni::Global<jni::Object<android::NumberFormat>> formatter; | ||
if (currency.empty()) { | ||
formatter = jni::NewGlobal(*env, android::NumberFormat::getInstance(*env, locale)); | ||
android::NumberFormat::setMinimumFractionDigits(*env, formatter, static_cast<jni::jint>(minFractionDigits)); | ||
android::NumberFormat::setMaximumFractionDigits(*env, formatter, static_cast<jni::jint>(maxFractionDigits)); | ||
} else { | ||
formatter = jni::NewGlobal(*env, android::NumberFormat::getCurrencyInstance(*env, locale)); | ||
} | ||
|
||
auto result = android::NumberFormat::format(*env, formatter, static_cast<jni::jdouble>(number)); | ||
return jni::Make<std::string>(*env, result); | ||
} | ||
|
||
} // namespace platform | ||
} // namespace mbgl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#pragma once | ||
|
||
#include <jni/jni.hpp> | ||
|
||
#include "collator_jni.hpp" | ||
|
||
/* | ||
android::NumberFormat is the JNI wrapper | ||
of java/text/NumberFormat. | ||
*/ | ||
|
||
namespace mbgl { | ||
namespace android { | ||
|
||
class NumberFormat { | ||
public: | ||
static constexpr auto Name() { return "java/text/NumberFormat"; }; | ||
|
||
static jni::Local<jni::Object<NumberFormat>> getInstance(jni::JNIEnv&, const jni::Object<Locale>&); | ||
static jni::Local<jni::Object<NumberFormat>> getCurrencyInstance(jni::JNIEnv&, const jni::Object<Locale>&); | ||
static jni::Local<jni::String> format(jni::JNIEnv&, const jni::Object<NumberFormat>&, jni::jdouble); | ||
static void setMinimumFractionDigits(jni::JNIEnv&, const jni::Object<NumberFormat>&, jni::jint); | ||
static void setMaximumFractionDigits(jni::JNIEnv&, const jni::Object<NumberFormat>&, jni::jint); | ||
|
||
static void registerNative(jni::JNIEnv&); | ||
}; | ||
|
||
} // namespace android | ||
} // namespace mbgl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <mbgl/util/platform.hpp> | ||
|
||
#include <unicode/numberformatter.h> | ||
|
||
namespace mbgl { | ||
namespace platform { | ||
|
||
std::string formatNumber(double number, const std::string& localeId, const std::string& currency, | ||
uint8_t minFractionDigits, uint8_t maxFractionDigits) { | ||
|
||
UErrorCode status = U_ZERO_ERROR; | ||
icu::UnicodeString ustr; | ||
std::string formatted; | ||
|
||
icu::Locale locale = icu::Locale(localeId.c_str()); | ||
// Print the value as currency | ||
if (!currency.empty()) { | ||
icu::UnicodeString ucurrency = icu::UnicodeString::fromUTF8(currency); | ||
ustr = icu::number::NumberFormatter::with() | ||
.unit(icu::CurrencyUnit(ucurrency.getBuffer(), status)) | ||
.locale(locale) | ||
.formatDouble(number, status) | ||
.toString(); | ||
} else { | ||
ustr = icu::number::NumberFormatter::with() | ||
.precision(icu::number::Precision::minMaxFraction(minFractionDigits, maxFractionDigits)) | ||
.locale(locale) | ||
.formatDouble(number, status) | ||
.toString(); | ||
} | ||
return ustr.toUTF8String(formatted); | ||
} | ||
|
||
} // namespace platform | ||
} // namespace mbgl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <mbgl/util/platform.hpp> | ||
|
||
#include <QLocale> | ||
#include <QString> | ||
|
||
namespace mbgl { | ||
namespace platform { | ||
|
||
std::string formatNumber(double number, const std::string& localeId, const std::string& currency, | ||
uint8_t minFractionDigits, uint8_t maxFractionDigits) { | ||
|
||
QString formatted; | ||
// Qt Locale::toString() API takes only one precision argument | ||
(void)minFractionDigits; | ||
QLocale locale = QLocale(QString::fromStdString(localeId)); | ||
|
||
if (!currency.empty()) { | ||
formatted = locale.toCurrencyString(number); | ||
} else { | ||
formatted = locale.toString(number, 'f', maxFractionDigits); | ||
} | ||
return formatted.toStdString(); | ||
} | ||
|
||
} // namespace platform | ||
} // namespace mbgl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.