1 /*
2  * Copyright (c) 2024 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 #ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_LAYOUTMANAGER_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_LAYOUTMANAGER_H
18 
19 #include "frameworks/bridge/declarative_frontend/jsview/js_container_base.h"
20 #include "frameworks/core/components/common/properties/text_layout_info.h"
21 #include "frameworks/core/components_ng/pattern/text/layout_info_interface.h"
22 
23 namespace OHOS::Ace::Framework {
24 
25 constexpr int32_t DEFAULT_HEIGHT_STYLE = 5;
26 struct NapiMap {
27     napi_value instance;
28     napi_value setFunction;
29 };
30 
31 class JSLayoutManager : public Referenced {
32 public:
33     JSLayoutManager() = default;
34     ~JSLayoutManager() override = default;
35 
36     static void JSBind(BindingTarget globalObj);
37 
Constructor(const JSCallbackInfo & args)38     static void Constructor(const JSCallbackInfo& args)
39     {
40         auto jsLayoutManager = Referenced::MakeRefPtr<JSLayoutManager>();
41         jsLayoutManager->IncRefCount();
42         args.SetReturnValue(Referenced::RawPtr(jsLayoutManager));
43     }
44 
Destructor(JSLayoutManager * jsLayoutManager)45     static void Destructor(JSLayoutManager* jsLayoutManager)
46     {
47         if (jsLayoutManager) {
48             jsLayoutManager->DecRefCount();
49         }
50     }
51 
SetLayoutInfoInterface(const WeakPtr<OHOS::Ace::NG::LayoutInfoInterface> & layoutInfoInterface)52     void SetLayoutInfoInterface(const WeakPtr<OHOS::Ace::NG::LayoutInfoInterface>& layoutInfoInterface)
53     {
54         layoutInfoInterface_ = layoutInfoInterface;
55     }
56 
57     void GetLineCount(const JSCallbackInfo& args);
58 
59     void GetRectsForRange(const JSCallbackInfo& args);
60 
ParseRectWidthStyle(const JsiValue & widthStyleVal)61     RectWidthStyle ParseRectWidthStyle(const JsiValue& widthStyleVal)
62     {
63         if (widthStyleVal->IsNumber()) {
64             int widthStyleInt = widthStyleVal->ToNumber<int>();
65             if (widthStyleInt == 0 || widthStyleInt == 1) {
66                 return static_cast<RectWidthStyle>(widthStyleInt);
67             }
68         }
69         return RectWidthStyle::TIGHT;
70     }
71 
ParseRectHeightStyle(const JsiValue & heightStyleVal)72     RectHeightStyle ParseRectHeightStyle(const JsiValue& heightStyleVal)
73     {
74         if (heightStyleVal->IsNumber()) {
75             int heightStyleInt = heightStyleVal->ToNumber<int>();
76             if (heightStyleInt >= 0 && heightStyleInt <= DEFAULT_HEIGHT_STYLE) {
77                 return static_cast<RectHeightStyle>(heightStyleInt);
78             }
79         }
80         return RectHeightStyle::TIGHT;
81     }
82 
83     void GetGlyphPositionAtCoordinate(const JSCallbackInfo& args);
84 
85     void DidExceedMaxLines(const JSCallbackInfo& args);
86 
87     void GetLineMetrics(const JSCallbackInfo& info);
88 
89     Local<panda::ObjectRef> CreateJSRunMetrics(const std::map<size_t, RunMetrics>& mapRunMetrics,
90         const JSCallbackInfo& args);
91 
92     Local<panda::ObjectRef> CreateJSFontMetrics(const FontMetrics& fontMetrics, const JSCallbackInfo& args);
93 
94     Local<panda::ObjectRef> CreateJSTextStyleResult(const TextStyle& textStyle, const JSCallbackInfo& args);
95 
96 private:
97     WeakPtr<OHOS::Ace::NG::LayoutInfoInterface> layoutInfoInterface_;
98 };
99 } // namespace OHOS::Ace::Framework
100 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_LAYOUTMANAGER_H