1 /*
2 * Copyright (c) 2023 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 "core/components_ng/pattern/web/web_layout_algorithm.h"
17
18 #include "core/components_ng/base/frame_node.h"
19
20 #if !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM)
21 #include "core/components_ng/pattern/web/web_pattern.h"
22 #else
23 #include "core/components_ng/pattern/web/cross_platform/web_pattern.h"
24 #endif
25 #include "core/components_ng/property/measure_utils.h"
26 #include "core/pipeline_ng/pipeline_context.h"
27
28 constexpr int32_t MAX_TEXTURE_SIZE = 16000;
29 constexpr int32_t MAX_SURFACE_SIZE = 8000;
30
31 namespace OHOS::Ace::NG {
32 WebLayoutAlgorithm::WebLayoutAlgorithm() = default;
33
MeasureContent(const LayoutConstraintF & contentConstraint,LayoutWrapper * layoutWrapper)34 std::optional<SizeF> WebLayoutAlgorithm::MeasureContent(
35 const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper)
36 {
37 auto layoutSize = contentConstraint.selfIdealSize.IsValid() ? contentConstraint.selfIdealSize.ConvertToSizeT()
38 : contentConstraint.maxSize;
39 return layoutSize;
40 }
41
Measure(LayoutWrapper * layoutWrapper)42 void WebLayoutAlgorithm::Measure(LayoutWrapper* layoutWrapper)
43 {
44 CHECK_NULL_VOID(layoutWrapper);
45 auto host = layoutWrapper->GetHostNode();
46 CHECK_NULL_VOID(host);
47 auto pattern = DynamicCast<WebPattern>(host->GetPattern());
48 CHECK_NULL_VOID(pattern);
49 int rootLayerWidth = pattern->GetRootLayerWidth();
50 int rootLayerHeight = pattern->GetRootLayerHeight();
51 auto type = pattern->GetWebType();
52 if (pattern->GetLayoutMode() == WebLayoutMode::FIT_CONTENT && IsValidRootLayer(rootLayerWidth, type) &&
53 IsValidRootLayer(rootLayerHeight, type)) {
54 layoutWrapper->GetGeometryNode()->SetFrameSize(SizeF(rootLayerWidth, rootLayerHeight));
55 } else {
56 BoxLayoutAlgorithm::Measure(layoutWrapper);
57 }
58 }
59
IsValidRootLayer(int32_t x,WebType type)60 bool WebLayoutAlgorithm::IsValidRootLayer(int32_t x, WebType type)
61 {
62 int32_t maxSize = 0;
63 if (type == WebType::TEXTURE) {
64 maxSize = MAX_TEXTURE_SIZE;
65 } else {
66 maxSize = MAX_SURFACE_SIZE;
67 }
68 return x > 0 && x <= maxSize;
69 }
70 } // namespace OHOS::Ace::NG
71