1 /*
2  * Copyright (c) 2021 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 "frameworks/core/components/svg/rosen_render_svg.h"
17 
18 #include "render_service_client/core/ui/rs_node.h"
19 
20 #include "frameworks/core/components/transform/rosen_render_transform.h"
21 
22 namespace OHOS::Ace {
23 
Paint(RenderContext & context,const Offset & offset)24 void RosenRenderSvg::Paint(RenderContext& context, const Offset& offset)
25 {
26     auto rsNode = static_cast<RosenRenderContext*>(&context)->GetRSNode();
27     if (rsNode == nullptr) {
28         return;
29     }
30     rsNode->SetAlpha(rootOpacity_ / 255.0);
31     UpdateTransformByGlobalOffset(rsNode);
32     RenderNode::Paint(context, offset);
33 }
34 
UpdateTransformByGlobalOffset(const std::shared_ptr<RSNode> & rsNode)35 void RosenRenderSvg::UpdateTransformByGlobalOffset(const std::shared_ptr<RSNode>& rsNode)
36 {
37     if (rsNode == nullptr) {
38         return;
39     }
40     if (GreatNotEqual(viewBox_.Width(), 0.0) && GreatNotEqual(viewBox_.Height(), 0.0)) {
41         double scale =
42             std::min(GetLayoutSize().Width() / viewBox_.Width(), GetLayoutSize().Height() / viewBox_.Height());
43         double tx = GetLayoutSize().Width() * 0.5 - (viewBox_.Width() * 0.5 + viewBox_.Left()) * scale;
44         double ty = GetLayoutSize().Height() * 0.5 - (viewBox_.Height() * 0.5 + viewBox_.Top()) * scale;
45         rsNode->SetScale(scale);
46         rsNode->SetTranslate({ tx, ty });
47 
48         double pivotX = ConvertDimensionToPx(transformOrigin_.first, LengthType::HORIZONTAL, true) / viewBox_.Width();
49         double pivotY = ConvertDimensionToPx(transformOrigin_.second, LengthType::VERTICAL, true) / viewBox_.Height();
50         rsNode->SetPivot(pivotX, pivotY);
51     }
52 }
53 
54 } // namespace OHOS::Ace
55