!11207 Add some enumerations and interfaces

Merge pull request !11207 from changleipeng/0520
This commit is contained in:
openharmony_ci 2024-05-23 10:09:33 +00:00 committed by Gitee
commit af6d4283bc
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 204 additions and 18 deletions

View File

@ -123,6 +123,25 @@ static const std::vector<struct JsEnumInt> g_affinity = {
{ "DOWNSTREAM", static_cast<size_t>(Affinity::NEXT) },
};
static const std::vector<struct JsEnumInt> g_fontWidth = {
{ "ULTRA_CONDENSED", static_cast<size_t>(FontWidth::ULTRA_CONDENSED) },
{ "EXTRA_CONDENSED", static_cast<size_t>(FontWidth::EXTRA_CONDENSED) },
{ "CONDENSED", static_cast<size_t>(FontWidth::CONDENSED) },
{ "SEMI_CONDENSED", static_cast<size_t>(FontWidth::SEMI_CONDENSED) },
{ "NORMAL", static_cast<size_t>(FontWidth::NORMAL) },
{ "SEMI_EXPANDED", static_cast<size_t>(FontWidth::SEMI_EXPANDED) },
{ "EXPANDED", static_cast<size_t>(FontWidth::EXPANDED) },
{ "EXTRA_EXPANDED", static_cast<size_t>(FontWidth::EXTRA_EXPANDED) },
{ "ULTRA_EXPANDED", static_cast<size_t>(FontWidth::ULTRA_EXPANDED) },
};
static const std::vector<struct JsEnumInt> g_textHeightBehavior = {
{ "ALL", static_cast<size_t>(TextHeightBehavior::ALL) },
{ "DISABLE_FIRST_ASCENT", static_cast<size_t>(TextHeightBehavior::DISABLE_FIRST_ASCENT) },
{ "DISABLE_LAST_ASCENT", static_cast<size_t>(TextHeightBehavior::DISABLE_LAST_ASCENT) },
{ "DISABLE_ALL", static_cast<size_t>(TextHeightBehavior::DISABLE_ALL) },
};
static const std::map<std::string_view, const std::vector<struct JsEnumInt>&> g_intEnumClassMap = {
{ "TextAlign", g_textAlign },
{ "TextDecorationStyle", g_textDecorationStyle },
@ -135,9 +154,11 @@ static const std::map<std::string_view, const std::vector<struct JsEnumInt>&> g_
{ "EllipsisMode", g_ellipsisModal },
{ "TextDecorationType", g_textDecoration },
{ "PlaceholderAlignment", g_placeholderVerticalAlignment },
{ "RectWidthStyle", g_rectWidthStyle},
{ "RectHeightStyle", g_rectHeightStyle},
{ "Affinity", g_affinity},
{ "RectWidthStyle", g_rectWidthStyle },
{ "RectHeightStyle", g_rectHeightStyle },
{ "Affinity", g_affinity },
{ "FontWidth", g_fontWidth },
{ "TextHeightBehavior", g_textHeightBehavior },
};
napi_value JsEnum::JsEnumIntInit(napi_env env, napi_value exports)

View File

@ -63,7 +63,7 @@ bool OnMakeFontFamilies(napi_env& env, napi_value jsValue, std::vector<std::stri
return true;
}
bool SetTextStyleColor(napi_env env, napi_value argValue, const std::string& str, Drawing::Color& colorSrc)
bool SetColorFromJS(napi_env env, napi_value argValue, const std::string& str, Drawing::Color& colorSrc)
{
napi_value tempValue = nullptr;
napi_value tempValueChild = nullptr;
@ -109,14 +109,14 @@ bool GetDecorationFromJS(napi_env env, napi_value argValue, const std::string& s
textStyle.decoration = TextDecoration(textDecoration);
}
SetTextStyleColor(env, tempValue, "color", textStyle.decorationColor);
SetColorFromJS(env, tempValue, "color", textStyle.decorationColor);
napi_get_named_property(env, tempValue, "decorationStyle", &tempValueChild);
uint32_t decorationStyle = 0;
if (tempValueChild != nullptr && napi_get_value_uint32(env, tempValueChild, &decorationStyle) == napi_ok) {
textStyle.decorationStyle = TextDecorationStyle(decorationStyle);
}
SetTextStyleDoubleValueFromJS(env, tempValue, "decorationThicknessScale", textStyle.decorationThicknessScale);
SetDoubleValueFromJS(env, tempValue, "decorationThicknessScale", textStyle.decorationThicknessScale);
return true;
}
@ -138,7 +138,7 @@ void ParsePartTextStyle(napi_env env, napi_value argValue, TextStyle& textStyle)
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &baseline) == napi_ok) {
textStyle.baseline = TextBaseline(baseline);
}
SetTextStyleDoubleValueFromJS(env, argValue, "fontSize", textStyle.fontSize);
SetDoubleValueFromJS(env, argValue, "fontSize", textStyle.fontSize);
std::vector<std::string> fontFamilies;
napi_get_named_property(env, argValue, "fontFamilies", &tempValue);
@ -146,11 +146,8 @@ void ParsePartTextStyle(napi_env env, napi_value argValue, TextStyle& textStyle)
textStyle.fontFamilies = fontFamilies;
}
GetDecorationFromJS(env, argValue, "decoration", textStyle);
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);
SetTextStyleBaseType(env, argValue, textStyle);
ReceiveFontFeature(env, argValue, textStyle);
napi_get_named_property(env, argValue, "ellipsis", &tempValue);
std::string text = "";
if (tempValue != nullptr && ConvertFromJsValue(env, tempValue, text)) {
@ -168,6 +165,66 @@ void ParsePartTextStyle(napi_env env, napi_value argValue, TextStyle& textStyle)
}
}
bool GetNamePropertyFromJS(napi_env env, napi_value argValue, const std::string& str, napi_value& propertyValue)
{
bool result = false;
if (napi_has_named_property(env, argValue, str.c_str(), &result) != napi_ok || (!result)) {
return false;
}
if (napi_get_named_property(env, argValue, str.c_str(), &propertyValue) != napi_ok) {
return false;
}
return true;
}
void ReceiveFontFeature(napi_env env, napi_value argValue, TextStyle& textStyle)
{
napi_value allFeatureValue = nullptr;
napi_get_named_property(env, argValue, "fontFeatures", &allFeatureValue);
uint32_t arrayLength = 0;
if (napi_get_array_length(env, allFeatureValue, &arrayLength) != napi_ok ||
!arrayLength) {
ROSEN_LOGE("The parameter of font features is unvaild");
return;
}
for (uint32_t further = 0; further < arrayLength; further++) {
napi_value singleElementValue;
if (napi_get_element(env, allFeatureValue, further, &singleElementValue) != napi_ok) {
ROSEN_LOGE("This parameter of the font features is unvaild");
break;
}
napi_value featureElement;
std::string name;
if (napi_get_named_property(env, singleElementValue, "name", &featureElement) != napi_ok ||
!ConvertFromJsValue(env, featureElement, name)) {
ROSEN_LOGE("This time that the name of parameter in font features is unvaild");
break;
}
int value = 0;
if (napi_get_named_property(env, singleElementValue, "value", &featureElement) != napi_ok ||
!ConvertFromJsValue(env, featureElement, value)) {
ROSEN_LOGE("This time that the value of parameter in font features is unvaild");
break;
}
textStyle.fontFeatures.SetFeature(name, value);
}
return;
}
void SetTextStyleBaseType(napi_env env, napi_value argValue, TextStyle& textStyle)
{
SetDoubleValueFromJS(env, argValue, "letterSpacing", textStyle.letterSpacing);
SetDoubleValueFromJS(env, argValue, "wordSpacing", textStyle.wordSpacing);
SetDoubleValueFromJS(env, argValue, "baselineShift", textStyle.baseLineShift);
SetDoubleValueFromJS(env, argValue, "heightScale", textStyle.heightScale);
SetBoolValueFromJS(env, argValue, "halfLeading", textStyle.halfLeading);
SetBoolValueFromJS(env, argValue, "heightOnly", textStyle.heightOnly);
}
void ScanShadowValue(napi_env env, napi_value allShadowValue, uint32_t arrayLength, TextStyle& textStyle)
{
textStyle.shadows.clear();
@ -180,7 +237,7 @@ void ScanShadowValue(napi_env env, napi_value allShadowValue, uint32_t arrayLeng
ROSEN_LOGE("The parameter of as private text-shadow is unvaild");
return;
}
SetTextStyleColor(env, element, "color", colorSrc);
SetColorFromJS(env, element, "color", colorSrc);
napi_value pointValue = nullptr;
if (napi_get_named_property(env, element, "point", &pointValue) != napi_ok) {
@ -201,7 +258,7 @@ void ScanShadowValue(napi_env env, napi_value allShadowValue, uint32_t arrayLeng
void SetTextShadowProperty(napi_env env, napi_value argValue, TextStyle& textStyle)
{
napi_value allShadowValue = nullptr;
if (napi_get_named_property(env, argValue, "textShadow", &allShadowValue) != napi_ok) {
if (!GetNamePropertyFromJS(env, argValue, "textShadows", allShadowValue)) {
return;
}
@ -219,9 +276,10 @@ bool GetTextStyleFromJS(napi_env env, napi_value argValue, TextStyle& textStyle)
if (argValue == nullptr) {
return false;
}
SetTextStyleColor(env, argValue, "color", textStyle.color);
SetColorFromJS(env, argValue, "color", textStyle.color);
ParsePartTextStyle(env, argValue, textStyle);
SetTextShadowProperty(env, argValue, textStyle);
SetRectStyleFromJS(env, argValue, textStyle.backgroundRect);
return true;
}
@ -266,6 +324,10 @@ bool GetParagraphStyleFromJS(napi_env env, napi_value argValue, TypographyStyle&
if (tempValue != nullptr && napi_get_value_uint32(env, tempValue, &breakStrategy) == napi_ok) {
pographyStyle.breakStrategy = BreakStrategy(breakStrategy);
}
SetStrutStyleFromJS(env, argValue, pographyStyle);
SetEnumValueFromJS(env, argValue, "textHeightBehavior", pographyStyle.textHeightBehavior);
return true;
}
@ -316,4 +378,57 @@ size_t GetParamLen(napi_env env, napi_value param)
}
return buffSize;
}
void SetStrutStyleFromJS(napi_env env, napi_value argValue, TypographyStyle& pographyStyle)
{
if (!argValue) {
return;
}
napi_value strutStyleValue = nullptr;
if (!GetNamePropertyFromJS(env, argValue, "strutStyle", strutStyleValue)) {
return;
}
napi_value tempValue = nullptr;
if (GetNamePropertyFromJS(env, strutStyleValue, "fontFamilies", tempValue)) {
std::vector<std::string> fontFamilies;
if (tempValue != nullptr && OnMakeFontFamilies(env, tempValue, fontFamilies)) {
pographyStyle.lineStyleFontFamilies = fontFamilies;
}
}
SetEnumValueFromJS(env, strutStyleValue, "fontStyle", pographyStyle.lineStyleFontStyle);
SetEnumValueFromJS(env, strutStyleValue, "fontWidth", pographyStyle.lineStyleFontWidth);
SetEnumValueFromJS(env, strutStyleValue, "fontWeight", pographyStyle.lineStyleFontWeight);
SetDoubleValueFromJS(env, strutStyleValue, "fontSize", pographyStyle.lineStyleFontSize);
SetDoubleValueFromJS(env, strutStyleValue, "height", pographyStyle.lineStyleHeightScale);
SetDoubleValueFromJS(env, strutStyleValue, "leading", pographyStyle.lineStyleSpacingScale);
SetBoolValueFromJS(env, strutStyleValue, "forceHeight", pographyStyle.lineStyleOnly);
SetBoolValueFromJS(env, strutStyleValue, "enabled", pographyStyle.useLineStyle);
SetBoolValueFromJS(env, strutStyleValue, "heightOverride", pographyStyle.lineStyleHeightOnly);
SetBoolValueFromJS(env, strutStyleValue, "halfLeading", pographyStyle.lineStyleHalfLeading);
}
void SetRectStyleFromJS(napi_env env, napi_value argValue, RectStyle& rectStyle)
{
if (!argValue) {
return;
}
napi_value tempValue = nullptr;
if (!GetNamePropertyFromJS(env, argValue, "backgroundRect", tempValue)) {
return;
}
Drawing::Color color;
SetColorFromJS(env, tempValue, "color", color);
rectStyle.color = color.CastToColorQuad();
SetDoubleValueFromJS(env, tempValue, "leftTopRadius", rectStyle.leftTopRadius);
SetDoubleValueFromJS(env, tempValue, "rightTopRadius", rectStyle.rightTopRadius);
SetDoubleValueFromJS(env, tempValue, "rightBottomRadius", rectStyle.rightBottomRadius);
SetDoubleValueFromJS(env, tempValue, "leftBottomRadius", rectStyle.leftBottomRadius);
}
} // namespace OHOS::Rosen

View File

@ -320,7 +320,7 @@ 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)
inline void SetDoubleValueFromJS(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);
@ -330,7 +330,7 @@ inline void SetTextStyleDoubleValueFromJS(napi_env env, napi_value argValue, con
ConvertFromJsValue(env, tempValue, cValue);
}
inline void SetTextStyleBooleValueFromJS(napi_env env, napi_value argValue, const std::string str, bool& cValue)
inline void SetBoolValueFromJS(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);
@ -389,7 +389,7 @@ inline napi_value CreateTextRectJsValue(napi_env env, TextRect textrect)
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 SetColorFromJS(napi_env env, napi_value argValue, const std::string& str, Drawing::Color& colorSrc);
bool GetDecorationFromJS(napi_env env, napi_value argValue, TextStyle& textStyle);
@ -401,10 +401,31 @@ bool GetPlaceholderSpanFromJS(napi_env env, napi_value argValue, PlaceholderSpan
void ParsePartTextStyle(napi_env env, napi_value argValue, TextStyle& textStyle);
void SetTextStyleBaseType(napi_env env, napi_value argValue, TextStyle& textStyle);
void ReceiveFontFeature(napi_env env, napi_value argValue, TextStyle& textStyle);
size_t GetParamLen(napi_env env, napi_value param);
bool GetNamePropertyFromJS(napi_env env, napi_value argValue, const std::string& str, napi_value& propertyValue);
template<class Type>
void SetEnumValueFromJS(napi_env env, napi_value argValue, const std::string str, Type& typeValue)
{
napi_value propertyValue = nullptr;
if (!GetNamePropertyFromJS(env, argValue, str, propertyValue)) {
return;
}
ConvertFromJsValue(env, propertyValue, typeValue);
}
void ScanShadowValue(napi_env env, napi_value allShadowValue, uint32_t arrayLength, TextStyle& textStyle);
void SetTextShadowProperty(napi_env env, napi_value argValue, TextStyle& textStyle);
void SetStrutStyleFromJS(napi_env env, napi_value argValue, TypographyStyle& pographyStyle);
void SetRectStyleFromJS(napi_env env, napi_value argValue, RectStyle& rectStyle);
} // namespace OHOS::Rosen
#endif // OHOS_JS_TEXT_UTILS_H

View File

@ -83,6 +83,7 @@ napi_value JsParagraphBuilder::Init(napi_env env, napi_value exportObj)
DECLARE_NAPI_FUNCTION("popStyle", JsParagraphBuilder::PopStyle),
DECLARE_NAPI_FUNCTION("addPlaceholder", JsParagraphBuilder::AddPlaceholder),
DECLARE_NAPI_FUNCTION("build", JsParagraphBuilder::Build),
DECLARE_NAPI_FUNCTION("addSymbol", JsParagraphBuilder::AppendSymbol),
};
napi_value constructor = nullptr;
@ -224,6 +225,7 @@ napi_value JsParagraphBuilder::Build(napi_env env, napi_callback_info info)
JsParagraphBuilder* me = CheckParamsAndGetThis<JsParagraphBuilder>(env, info);
return (me != nullptr) ? me->OnBuild(env, info) : nullptr;
}
napi_value JsParagraphBuilder::OnBuild(napi_env env, napi_callback_info info)
{
if (typographyCreate_ == nullptr) {
@ -235,4 +237,29 @@ napi_value JsParagraphBuilder::OnBuild(napi_env env, napi_callback_info info)
return JsParagraph::CreateJsTypography(env, std::move(typography));
}
napi_value JsParagraphBuilder::AppendSymbol(napi_env env, napi_callback_info info)
{
JsParagraphBuilder* me = CheckParamsAndGetThis<JsParagraphBuilder>(env, info);
return (me != nullptr) ? me->OnAppendSymbol(env, info) : nullptr;
}
napi_value JsParagraphBuilder::OnAppendSymbol(napi_env env, napi_callback_info info)
{
if (typographyCreate_ == nullptr) {
return nullptr;
}
size_t argc = ARGC_ONE;
napi_value argv[ARGC_ONE] = {nullptr};
if (napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr) != napi_ok ||
argc < ARGC_ONE) {
return nullptr;
}
uint32_t symbolId = 0;
if (!ConvertFromJsNumber(env, argv[0], symbolId)) {
return nullptr;
}
typographyCreate_->AppendSymbol(symbolId);
return NapiGetUndefined(env);
}
} // namespace OHOS::Rosen

View File

@ -36,6 +36,7 @@ public:
static napi_value AddText(napi_env env, napi_callback_info info);
static napi_value PopStyle(napi_env env, napi_callback_info info);
static napi_value AddPlaceholder(napi_env env, napi_callback_info info);
static napi_value AppendSymbol(napi_env env, napi_callback_info info);
static napi_value Build(napi_env env, napi_callback_info info);
private:
@ -44,6 +45,7 @@ private:
napi_value OnPopStyle(napi_env env, napi_callback_info info);
napi_value OnAddPlaceholder(napi_env env, napi_callback_info info);
napi_value OnBuild(napi_env env, napi_callback_info info);
napi_value OnAppendSymbol(napi_env env, napi_callback_info info);
static thread_local napi_ref constructor_;
std::unique_ptr<TypographyCreate> typographyCreate_ = nullptr;
std::shared_ptr<FontCollection> fontCollection_ = nullptr;