• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
65     if (!spanDeclaration->HasSetFontFamily()) {
66         spanStyle.SetFontFamilies(textStyle.GetFontFamilies());
67     }
68     if (!spanDeclaration->HasSetAllowScale()) {
69         spanStyle.SetAllowScale(textStyle.IsAllowScale());
70     }
71     if (!spanDeclaration->HasSetFontFeatures()) {
72         spanStyle.SetFontFeatures(textStyle.GetFontFeatures());
73     }
74     if (!spanDeclaration->HasSetLetterSpacing()) {
75         spanStyle.SetLetterSpacing(textStyle.GetLetterSpacing());
76     }
77     if (!spanDeclaration->HasSetTextCase()) {
78         spanStyle.SetTextCase(textStyle.GetTextCase());
79     }
80     // Span don't support developer-set line-height.
81     spanStyle.SetLineHeight(textStyle.GetLineHeight(), textStyle.HasHeightOverride());
82     spanComponent->SetTextStyle(spanStyle);
83 }
84 
85 } // namespace OHOS::Ace
86