!9792 add js_text_utils interface

Merge pull request !9792 from changleipeng/liu_zhenghong
This commit is contained in:
openharmony_ci 2024-04-06 07:42:56 +00:00 committed by Gitee
commit 525ce904b9
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
6 changed files with 378 additions and 1 deletions

View File

@ -9,7 +9,7 @@
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.
import("//build/ohos.gni")
import("//foundation/graphic/graphic_2d/graphic_config.gni")
@ -52,6 +52,7 @@ ohos_shared_library("text_napi") {
ohos_shared_library("text_napi_impl") {
sources = [
"enum_napi/text_enum_napi.cpp",
"fontcollection_napi/js_fontcollection.cpp",
"js_text_init.cpp",
"js_text_utils.cpp",

View File

@ -0,0 +1,155 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <map>
#include <vector>
#include "utils/log.h"
#include "text_enum_napi.h"
namespace OHOS::Rosen {
struct JsEnumInt {
std::string_view enumName;
size_t enumInt;
};
static const std::vector<struct JsEnumInt> g_textAlign = {
{ "LEFT", static_cast<size_t>(TextAlign::LEFT) },
{ "RIGHT", static_cast<size_t>(TextAlign::RIGHT) },
{ "CENTER", static_cast<size_t>(TextAlign::CENTER) },
{ "JUSTIFY", static_cast<size_t>(TextAlign::JUSTIFY) },
{ "START", static_cast<size_t>(TextAlign::START) },
{ "END", static_cast<size_t>(TextAlign::END) },
};
static const std::vector<struct JsEnumInt> g_textDecorationStyle = {
{ "SOLID", static_cast<size_t>(TextDecorationStyle::SOLID) },
{ "DOUBLE", static_cast<size_t>(TextDecorationStyle::DOUBLE) },
{ "DOTTED", static_cast<size_t>(TextDecorationStyle::DOTTED) },
{ "DASHED", static_cast<size_t>(TextDecorationStyle::DASHED) },
{ "WAVY", static_cast<size_t>(TextDecorationStyle::WAVY) },
};
static const std::vector<struct JsEnumInt> g_fontWeight = {
{ "W100", static_cast<size_t>(FontWeight::W100) },
{ "W200", static_cast<size_t>(FontWeight::W200) },
{ "W300", static_cast<size_t>(FontWeight::W300) },
{ "W400", static_cast<size_t>(FontWeight::W400) },
{ "W500", static_cast<size_t>(FontWeight::W500) },
{ "W600", static_cast<size_t>(FontWeight::W600) },
{ "W700", static_cast<size_t>(FontWeight::W700) },
{ "W800", static_cast<size_t>(FontWeight::W800) },
{ "W900", static_cast<size_t>(FontWeight::W900) },
};
static const std::vector<struct JsEnumInt> g_fontStyle = {
{ "NORMAL", static_cast<size_t>(FontStyle::NORMAL) },
{ "ITALIC", static_cast<size_t>(FontStyle::ITALIC) },
};
static const std::vector<struct JsEnumInt> g_textBaseline = {
{ "ALPHABETIC", static_cast<size_t>(TextBaseline::ALPHABETIC) },
{ "IDEOGRAPHIC", static_cast<size_t>(TextBaseline::IDEOGRAPHIC) },
};
static const std::vector<struct JsEnumInt> g_textDirection = {
{ "RTL", static_cast<size_t>(TextDirection::RTL) },
{ "LTR", static_cast<size_t>(TextDirection::LTR) },
};
static const std::vector<struct JsEnumInt> g_wordBreakType = {
{ "NORMAL", static_cast<size_t>(WordBreakType::NORMAL) },
{ "BREAK_ALL", static_cast<size_t>(WordBreakType::BREAK_ALL) },
{ "BREAK_WORD", static_cast<size_t>(WordBreakType::BREAK_WORD) },
};
static const std::vector<struct JsEnumInt> g_breakStrategy = {
{ "GREEDY", static_cast<size_t>(BreakStrategy::GREEDY) },
{ "HIGH_QUALITY", static_cast<size_t>(BreakStrategy::HIGH_QUALITY) },
{ "BALANCED", static_cast<size_t>(BreakStrategy::BALANCED) },
};
static const std::vector<struct JsEnumInt> g_ellipsisModal = {
{ "HEAD", static_cast<size_t>(EllipsisModal::HEAD) },
{ "MIDDLE", static_cast<size_t>(EllipsisModal::MIDDLE) },
{ "TAIL", static_cast<size_t>(EllipsisModal::TAIL) },
};
static const std::vector<struct JsEnumInt> g_textDecoration = {
{ "NONE", static_cast<size_t>(TextDecoration::NONE) },
{ "UNDERLINE", static_cast<size_t>(TextDecoration::UNDERLINE) },
{ "OVERLINE", static_cast<size_t>(TextDecoration::OVERLINE) },
{ "LINE_THROUGH", static_cast<size_t>(TextDecoration::LINE_THROUGH) },
};
static const std::map<std::string_view, const std::vector<struct JsEnumInt>&> g_intEnumClassMap = {
{ "TextAlign", g_textAlign },
{ "TextDecorationStyle", g_textDecorationStyle },
{ "FontWeight", g_fontWeight },
{ "FontStyle", g_fontStyle },
{ "TextBaseline", g_textBaseline },
{ "TextDirection", g_textDirection },
{ "WordBreakType", g_wordBreakType },
{ "BreakStrategy", g_breakStrategy },
{ "EllipsisModal", g_ellipsisModal },
{ "TextDecoration", g_textDecoration },
};
napi_value JsEnum::JsEnumIntInit(napi_env env, napi_value exports)
{
for (auto it = g_intEnumClassMap.begin(); it != g_intEnumClassMap.end(); it++) {
auto &enumClassName = it->first;
auto &enumItemVec = it->second;
auto vecSize = enumItemVec.size();
std::vector<napi_value> value;
value.resize(vecSize);
for (size_t index = 0; index < vecSize; ++index) {
napi_create_uint32(env, enumItemVec[index].enumInt, &value[index]);
}
std::vector<napi_property_descriptor> property;
property.resize(vecSize);
for (size_t index = 0; index < vecSize; ++index) {
property[index] = napi_property_descriptor DECLARE_NAPI_STATIC_PROPERTY(
enumItemVec[index].enumName.data(), value[index]);
}
auto napiConstructor = [](napi_env env, napi_callback_info info) {
napi_value jsThis = nullptr;
napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
return jsThis;
};
napi_value result = nullptr;
napi_status napiStatus = napi_define_class(env, enumClassName.data(), NAPI_AUTO_LENGTH, napiConstructor,
nullptr, property.size(), property.data(), &result);
if (napiStatus != napi_ok) {
LOGE("napi_define_class falied");
return nullptr;
}
napiStatus = napi_set_named_property(env, exports, enumClassName.data(), result);
if (napiStatus != napi_ok) {
LOGE("napi_set_named_property falied");
return nullptr;
}
}
return exports;
}
napi_value JsEnum::Init(napi_env env, napi_value exports)
{
JsEnumIntInit(env, exports);
return exports;
}
} // namespace OHOS::Rosen

View File

@ -0,0 +1,35 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_ROSEN_TEXT_ENUM_NAPI_H
#define OHOS_ROSEN_TEXT_ENUM_NAPI_H
#include <memory>
#include <native_engine/native_engine.h>
#include <native_engine/native_value.h>
#include "typography_style.h"
namespace OHOS::Rosen {
class JsEnum {
public:
JsEnum() = default;
~JsEnum() = default;
static napi_value Init(napi_env env, napi_value exports);
private:
static napi_value JsEnumIntInit(napi_env env, napi_value exports);
};
} // namespace OHOS::Rosen
#endif // OHOS_ROSEN_JS_ENUM_NAPI_H

View File

@ -14,6 +14,7 @@
*/
#include "js_text_init.h"
#include "enum_napi/text_enum_napi.h"
#include "fontcollection_napi/js_fontcollection.h"
#include "paragraph_style_napi/js_paragraphstyle.h"
#include "utils/log.h"
@ -22,6 +23,7 @@ namespace OHOS::Rosen {
napi_value TextInit(napi_env env, napi_value exportObj)
{
JsFontCollection::Init(env, exportObj);
JsEnum::Init(env, exportObj);
return exportObj;
}
} // namespace OHOS::Rosen

View File

@ -41,4 +41,148 @@ napi_value NapiThrowError(napi_env env, DrawingErrorCode err, const std::string&
napi_throw(env, CreateJsError(env, static_cast<int32_t>(err), message));
return NapiGetUndefined(env);
}
bool OnMakeFontFamilies(napi_env& env, napi_value jsValue, std::vector<std::string> &fontFamilies)
{
if (jsValue == nullptr) {
return false;
}
uint32_t size = 0;
napi_get_array_length(env, jsValue, &size);
if (size == 0) {
return false;
}
for (uint32_t i = 0; i < size; i++) {
napi_value tempStr = nullptr;
napi_get_element(env, jsValue, i, &tempStr);
std::string text = "";
if (ConvertFromJsValue(env, tempStr, text)) {
fontFamilies.push_back(text);
}
}
return true;
}
bool SetTextStyleColor(napi_env env, napi_value argValue, const std::string& str, Drawing::Color& colorSrc)
{
napi_value tempValue = nullptr;
napi_value tempValueChild = nullptr;
napi_get_named_property(env, argValue, str.c_str(), &tempValue);
if (tempValue != nullptr) {
return false;
}
int32_t alpha = 0;
int32_t red = 0;
int32_t green = 0;
int32_t blue = 0;
napi_get_named_property(env, tempValue, "alpha", &tempValueChild);
bool isAlphaOk = ConvertClampFromJsValue(env, tempValueChild, alpha, 0, Drawing::Color::RGB_MAX);
napi_get_named_property(env, tempValue, "red", &tempValueChild);
bool isRedOk = ConvertClampFromJsValue(env, tempValueChild, red, 0, Drawing::Color::RGB_MAX);
napi_get_named_property(env, tempValue, "green", &tempValueChild);
bool isGreenOk = ConvertClampFromJsValue(env, tempValueChild, green, 0, Drawing::Color::RGB_MAX);
napi_get_named_property(env, tempValue, "blue", &tempValueChild);
bool isBlueOk = ConvertClampFromJsValue(env, tempValueChild, blue, 0, Drawing::Color::RGB_MAX);
if (isAlphaOk && isRedOk && isGreenOk && isBlueOk) {
Drawing::Color color(Drawing::Color::ColorQuadSetARGB(alpha, red, green, blue));
colorSrc = color;
return true;
}
return false;
}
bool GetTextStyleFromJS(napi_env env, napi_value argValue, TextStyle& textStyle)
{
if (argValue == nullptr) {
return false;
}
SetTextStyleColor(env, argValue, "color", textStyle.color);
napi_value tempValue = nullptr;
napi_get_named_property(env, argValue, "fontWeight", &tempValue);
uint32_t fontWeight = 0;
if (napi_get_value_uint32(env, tempValue, &fontWeight)== napi_ok) {
textStyle.fontWeight = FontWeight(fontWeight);
}
napi_get_named_property(env, argValue, "baseline", &tempValue);
uint32_t baseline = 0;
if (napi_get_value_uint32(env, tempValue, &baseline)== napi_ok) {
textStyle.baseline = TextBaseline(baseline);
}
SetTextStyleDoubleValueFromJS(env, argValue, "fontSize", textStyle.fontSize);
std::vector<std::string> fontFamilies;
napi_get_named_property(env, argValue, "fontFamilies", &tempValue);
if (OnMakeFontFamilies(env, tempValue, fontFamilies)== napi_ok) {
textStyle.fontFamilies = fontFamilies;
}
SetTextStyleDoubleValueFromJS(env, argValue, "letterSpacing", textStyle.letterSpacing);
SetTextStyleDoubleValueFromJS(env, argValue, "wordSpacing", textStyle.wordSpacing);
SetTextStyleDoubleValueFromJS(env, argValue, "heightScale", textStyle.heightScale);
SetTextStyleBooleValueFromJS(env, argValue, "halfLeading", textStyle.halfLeading);
SetTextStyleBooleValueFromJS(env, argValue, "heightOnly", textStyle.heightOnly);
napi_get_named_property(env, argValue, "ellipsis", &tempValue);
std::string text = "";
if (ConvertFromJsValue(env, tempValue, text)) {
textStyle.ellipsis = Str8ToStr16(text);
}
napi_get_named_property(env, argValue, "ellipsisModal", &tempValue);
uint32_t ellipsisModal = 0;
if (napi_get_value_uint32(env, tempValue, &ellipsisModal)== napi_ok) {
textStyle.ellipsisModal = EllipsisModal(ellipsisModal);
}
napi_get_named_property(env, argValue, "locale", &tempValue);
std::string textLocale = "";
if (ConvertFromJsValue(env, tempValue, textLocale)) {
textStyle.locale = textLocale;
}
return true;
}
bool GetParagraphStyleFromJS(napi_env env, napi_value argValue, TypographyStyle& pographyStyle)
{
if (argValue == nullptr) {
return false;
}
napi_value tempValue = nullptr;
napi_get_named_property(env, argValue, "textStyle", &tempValue);
TextStyle textStyle;
if (GetTextStyleFromJS(env, tempValue, textStyle)) {
pographyStyle.insideTextStyle = textStyle;
}
napi_get_named_property(env, argValue, "textDirection", &tempValue);
uint32_t textDirection = 0;
if (napi_get_value_uint32(env, tempValue, &textDirection)== napi_ok) {
pographyStyle.textDirection = TextDirection(textDirection);
}
napi_get_named_property(env, argValue, "align", &tempValue);
uint32_t align = 0;
if (napi_get_value_uint32(env, tempValue, &align)== napi_ok) {
pographyStyle.textAlign = TextAlign(align);
}
napi_get_named_property(env, argValue, "wordBreak", &tempValue);
uint32_t wordBreak = 0;
if (napi_get_value_uint32(env, tempValue, &wordBreak)== napi_ok) {
pographyStyle.wordBreakType = WordBreakType(wordBreak);
}
napi_get_named_property(env, argValue, "maxLines", &tempValue);
uint32_t maxLines = 0;
if (napi_get_value_uint32(env, tempValue, &maxLines)== napi_ok) {
pographyStyle.maxLines = maxLines;
}
napi_get_named_property(env, argValue, "breakStrategy", &tempValue);
uint32_t breakStrategy = 0;
if (napi_get_value_uint32(env, tempValue, &breakStrategy)== napi_ok) {
pographyStyle.breakStrategy = BreakStrategy(breakStrategy);
}
return true;
}
} // namespace OHOS::Rosen

View File

@ -20,6 +20,12 @@
#include "native_engine/native_engine.h"
#include "native_engine/native_value.h"
#include "utils/log.h"
#include "draw/color.h"
#include "text_style.h"
#include "typography_style.h"
#include <codecvt>
namespace OHOS::Rosen {
constexpr size_t ARGC_ONE = 1;
constexpr size_t ARGC_TWO = 2;
@ -222,5 +228,39 @@ void BindNativeFunction(napi_env env, napi_value object, const char* name, const
napi_value CreateJsError(napi_env env, int32_t errCode, const std::string& message);
napi_value NapiThrowError(napi_env env, DrawingErrorCode err, const std::string& message);
inline std::u16string Str8ToStr16(const std::string &str)
{
return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(str);
}
inline void SetTextStyleDoubleValueFromJS(napi_env env, napi_value argValue, const std::string str, double& cValue)
{
napi_value tempValue = nullptr;
napi_get_named_property(env, argValue, str.c_str(), &tempValue);
if (tempValue == nullptr) {
return;
}
ConvertFromJsValue(env, tempValue, cValue);
}
inline void SetTextStyleBooleValueFromJS(napi_env env, napi_value argValue, const std::string str, bool& cValue)
{
napi_value tempValue = nullptr;
napi_get_named_property(env, argValue, str.c_str(), &tempValue);
if (tempValue == nullptr) {
return;
}
ConvertFromJsValue(env, tempValue, cValue);
}
bool OnMakeFontFamilies(napi_env& env, napi_value jsValue, std::vector<std::string> &fontFamilies);
bool SetTextStyleColor(napi_env env, napi_value argValue, const std::string& str, Drawing::Color& colorSrc);
bool GetTextStyleFromJS(napi_env env, napi_value argValue, TextStyle& textStyle);
bool GetParagraphStyleFromJS(napi_env env, napi_value argValue, TypographyStyle& pographyStyle);
} // namespace OHOS::Rosen
#endif // OHOS_JS_TEXT_UTILS_H