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 "core/components/text/text_component_v2.h"
17 
18 #include "core/components/declaration/span/span_declaration.h"
19 #include "core/components/text_span/text_span_component.h"
20 
21 namespace OHOS::Ace {
22 
OnChildInserted(const RefPtr<Component> & child,int32_t position)23 void TextComponentV2::OnChildInserted(const RefPtr<Component>& child, int32_t position)
24 {
25     CheckAndSetChildStyle(child);
26 }
27 
OnChildAppended(const RefPtr<Component> & child)28 void TextComponentV2::OnChildAppended(const RefPtr<Component>& child)
29 {
30     CheckAndSetChildStyle(child);
31 }
32 
CheckAndSetChildStyle(const RefPtr<Component> & child)33 void TextComponentV2::CheckAndSetChildStyle(const RefPtr<Component>& child)
34 {
35     // If span component has no developer-set styles, then set text styles to span
36     auto spanComponent = AceType::DynamicCast<TextSpanComponent>(child);
37     if (!spanComponent) {
38         LOGW("child of text is not valid.");
39         return;
40     }
41     auto spanDeclaration = AceType::DynamicCast<SpanDeclaration>(spanComponent->GetDeclaration());
42     if (!spanDeclaration) {
43         LOGW("declaration of span is not valid.");
44         return;
45     }
46     auto spanStyle = spanComponent->GetTextStyle();
47     auto textStyle = GetTextStyle();
48 
49     if (!spanDeclaration->HasSetFontSize()) {
50         spanStyle.SetFontSize(textStyle.GetFontSize());
51     }
52     if (!spanDeclaration->HasSetFontStyle()) {
53         spanStyle.SetFontStyle(textStyle.GetFontStyle());
54     }
55     if (!spanDeclaration->HasSetFontColor()) {
56         spanStyle.SetTextColor(textStyle.GetTextColor());
57     }
58     if (!spanDeclaration->HasSetFontWeight()) {
59         spanStyle.SetFontWeight(textStyle.GetFontWeight());
60     }
61     if (!spanDeclaration->HasSetTextDecoration()) {
62         spanStyle.SetTextDecoration(textStyle.GetTextDecoration());
63         spanStyle.SetTextDecorationColor(textStyle.GetTextDecorationColor());
64         spanStyle.SetTextDecorationStyle(textStyle.GetTextDecorationStyle());
65     }
66 
67     if (!spanDeclaration->HasSetFontFamily()) {
68         spanStyle.SetFontFamilies(textStyle.GetFontFamilies());
69     }
70     if (!spanDeclaration->HasSetAllowScale()) {
71         spanStyle.SetAllowScale(textStyle.IsAllowScale());
72     }
73     if (!spanDeclaration->HasSetFontFeatures()) {
74         spanStyle.SetFontFeatures(textStyle.GetFontFeatures());
75     }
76     if (!spanDeclaration->HasSetLetterSpacing()) {
77         spanStyle.SetLetterSpacing(textStyle.GetLetterSpacing());
78     }
79     if (!spanDeclaration->HasSetTextCase()) {
80         spanStyle.SetTextCase(textStyle.GetTextCase());
81     }
82     // Span don't support developer-set line-height.
83     spanStyle.SetLineHeight(textStyle.GetLineHeight(), textStyle.HasHeightOverride());
84     spanComponent->SetTextStyle(spanStyle);
85 }
86 
87 } // namespace OHOS::Ace
88