• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <cstdint>
17 
18 #include "ani_common.h"
19 #include "ani_drawing_converter.h"
20 #include "ani_text_utils.h"
21 
22 namespace OHOS::Text::ANI {
23 using namespace OHOS::Rosen;
24 
25 namespace {
GetColorValue(ani_env * env,ani_object colorObj,const char * name,int32_t & value)26 bool GetColorValue(ani_env* env, ani_object colorObj, const char* name, int32_t& value)
27 {
28     ani_double tempValue{0};
29     ani_status isAlphaOk =
30         env->Object_GetPropertyByName_Double(reinterpret_cast<ani_object>(colorObj), name, &tempValue);
31     if (isAlphaOk != ANI_OK) {
32         return false;
33     }
34     value = ConvertClampFromJsValue(tempValue, 0, Drawing::Color::RGB_MAX);
35     return true;
36 }
37 } // namespace
38 
ParseDrawingColorToNative(ani_env * env,ani_object obj,const std::string & str,Drawing::Color & colorSrc)39 void AniDrawingConverter::ParseDrawingColorToNative(
40     ani_env* env, ani_object obj, const std::string& str, Drawing::Color& colorSrc)
41 {
42     ani_ref colorRef = nullptr;
43     ani_status result = env->Object_GetPropertyByName_Ref(obj, str.c_str(), &colorRef);
44     if (result != ANI_OK || colorRef == nullptr) {
45         TEXT_LOGD("Failed to find param color, ret %{public}d", result);
46         return;
47     }
48 
49     int32_t alpha = 0;
50     if (!GetColorValue(env, reinterpret_cast<ani_object>(colorRef), "alpha", alpha)) {
51         return;
52     }
53     int32_t red = 0;
54     if (!GetColorValue(env, reinterpret_cast<ani_object>(colorRef), "red", red)) {
55         return;
56     }
57     int32_t green = 0;
58     if (!GetColorValue(env, reinterpret_cast<ani_object>(colorRef), "green", green)) {
59         return;
60     }
61     int32_t blue = 0;
62     if (!GetColorValue(env, reinterpret_cast<ani_object>(colorRef), "blue", blue)) {
63         return;
64     }
65     colorSrc = Drawing::Color(Drawing::Color::ColorQuadSetARGB(alpha, red, green, blue));
66 }
67 
ParseFontMetricsToAni(ani_env * env,const Drawing::FontMetrics & fontMetrics)68 ani_object AniDrawingConverter::ParseFontMetricsToAni(ani_env* env, const Drawing::FontMetrics& fontMetrics)
69 {
70     ani_object aniObj = AniTextUtils::CreateAniObject(env, ANI_CLASS_FONT_METRICS, ":V");
71     env->Object_SetPropertyByName_Double(aniObj, "flags", ani_int(fontMetrics.fFlags));
72     env->Object_SetPropertyByName_Double(aniObj, "top", ani_double(fontMetrics.fTop));
73     env->Object_SetPropertyByName_Double(aniObj, "ascent", ani_double(fontMetrics.fAscent));
74     env->Object_SetPropertyByName_Double(aniObj, "descent", ani_double(fontMetrics.fDescent));
75     env->Object_SetPropertyByName_Double(aniObj, "bottom", ani_double(fontMetrics.fBottom));
76     env->Object_SetPropertyByName_Double(aniObj, "leading", ani_double(fontMetrics.fLeading));
77     env->Object_SetPropertyByName_Double(aniObj, "avgCharWidth", ani_double(fontMetrics.fAvgCharWidth));
78     env->Object_SetPropertyByName_Double(aniObj, "maxCharWidth", ani_double(fontMetrics.fMaxCharWidth));
79     env->Object_SetPropertyByName_Double(aniObj, "xMin", ani_double(fontMetrics.fXMin));
80     env->Object_SetPropertyByName_Double(aniObj, "xMax", ani_double(fontMetrics.fXMax));
81     env->Object_SetPropertyByName_Double(aniObj, "xHeight", ani_double(fontMetrics.fXHeight));
82     env->Object_SetPropertyByName_Double(aniObj, "capHeight", ani_double(fontMetrics.fCapHeight));
83     env->Object_SetPropertyByName_Double(aniObj, "underlineThickness", ani_double(fontMetrics.fUnderlineThickness));
84     env->Object_SetPropertyByName_Double(aniObj, "underlinePosition", ani_double(fontMetrics.fUnderlinePosition));
85     env->Object_SetPropertyByName_Double(aniObj, "strikethroughThickness", ani_double(fontMetrics.fStrikeoutThickness));
86     env->Object_SetPropertyByName_Double(aniObj, "strikethroughPosition", ani_double(fontMetrics.fStrikeoutPosition));
87     return aniObj;
88 }
89 } // namespace OHOS::Text::ANI