• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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/text/text_paint_method.h"
17 
18 #include "core/components_ng/pattern/text/text_pattern.h"
19 
20 namespace OHOS::Ace::NG {
TextPaintMethod(const WeakPtr<Pattern> & pattern,RefPtr<Paragraph> paragraph,float baselineOffset,RefPtr<TextContentModifier> textContentModifier,RefPtr<TextOverlayModifier> textOverlayModifier)21 TextPaintMethod::TextPaintMethod(const WeakPtr<Pattern>& pattern, RefPtr<Paragraph> paragraph, float baselineOffset,
22     RefPtr<TextContentModifier> textContentModifier, RefPtr<TextOverlayModifier> textOverlayModifier)
23     : pattern_(pattern), paragraph_(std::move(paragraph)), baselineOffset_(baselineOffset),
24       textContentModifier_(textContentModifier), textOverlayModifier_(textOverlayModifier)
25 {}
26 
GetContentModifier(PaintWrapper * paintWrapper)27 RefPtr<Modifier> TextPaintMethod::GetContentModifier(PaintWrapper* paintWrapper)
28 {
29     return textContentModifier_;
30 }
31 
UpdateContentModifier(PaintWrapper * paintWrapper)32 void TextPaintMethod::UpdateContentModifier(PaintWrapper* paintWrapper)
33 {
34     CHECK_NULL_VOID(paintWrapper);
35     CHECK_NULL_VOID(paragraph_);
36     CHECK_NULL_VOID(textContentModifier_);
37 
38     textContentModifier_->SetParagraph(paragraph_);
39 
40     SizeF contentSize = paintWrapper->GetContentSize();
41     textContentModifier_->SetContentSize(contentSize);
42     auto offset = paintWrapper->GetContentOffset();
43     textContentModifier_->SetContentOffset(offset);
44     auto paintOffset = offset - OffsetF(0.0, std::min(baselineOffset_, 0.0f));
45     textContentModifier_->SetPrintOffset(paintOffset);
46 
47     auto textPattern = pattern_.Upgrade();
48     CHECK_NULL_VOID(textPattern);
49     auto frameNode = textPattern->GetHost();
50     CHECK_NULL_VOID(frameNode);
51     auto layoutProperty = frameNode->GetLayoutProperty<TextLayoutProperty>();
52     CHECK_NULL_VOID(layoutProperty);
53     auto renderContext = frameNode->GetRenderContext();
54     CHECK_NULL_VOID(renderContext);
55     auto pattern = frameNode->GetPattern<TextPattern>();
56     CHECK_NULL_VOID(pattern);
57 
58     auto textOverflow = layoutProperty->GetTextOverflow();
59     if (textOverflow.has_value() && textOverflow.value() == TextOverflow::MARQUEE) {
60         if (paragraph_->GetTextWidth() > paintWrapper->GetContentSize().Width()) {
61             textContentModifier_->StartTextRace();
62         } else {
63             textContentModifier_->StopTextRace();
64         }
65     } else {
66         textContentModifier_->StopTextRace();
67     }
68 
69     textContentModifier_->ContentChange();
70 
71     auto reasons = renderContext->GetObscured().value_or(std::vector<ObscuredReasons>());
72     textContentModifier_->SetObscured(reasons);
73     auto spanItemChildren = pattern->GetSpanItemChildren();
74     textContentModifier_->SetIfHaveSpanItemChildren(!spanItemChildren.empty());
75     auto wideTextLength = pattern->GetDisplayWideTextLength();
76     std::vector<Rect> drawObscuredRects;
77     if (wideTextLength != 0) {
78         paragraph_->GetRectsForRange(0, wideTextLength, drawObscuredRects);
79     }
80     textContentModifier_->SetDrawObscuredRects(drawObscuredRects);
81     if (renderContext->GetClipEdge().has_value()) {
82         textContentModifier_->SetClip(renderContext->GetClipEdge().value());
83     }
84 
85     PropertyChangeFlag flag = 0;
86     if (textContentModifier_->NeedMeasureUpdate(flag)) {
87         frameNode->MarkDirtyNode(flag);
88     }
89 }
90 
GetOverlayModifier(PaintWrapper * paintWrapper)91 RefPtr<Modifier> TextPaintMethod::GetOverlayModifier(PaintWrapper* paintWrapper)
92 {
93     return textOverlayModifier_;
94 }
95 
UpdateOverlayModifier(PaintWrapper * paintWrapper)96 void TextPaintMethod::UpdateOverlayModifier(PaintWrapper* paintWrapper)
97 {
98     CHECK_NULL_VOID(paintWrapper);
99     CHECK_NULL_VOID(paragraph_);
100     CHECK_NULL_VOID(textOverlayModifier_);
101 
102     auto offset = paintWrapper->GetContentOffset();
103     auto paintOffset = offset - OffsetF(0.0, std::min(baselineOffset_, 0.0f));
104     textOverlayModifier_->SetPrintOffset(paintOffset);
105 
106     auto textPattern = DynamicCast<TextPattern>(pattern_.Upgrade());
107     CHECK_NULL_VOID(textPattern);
108     auto host = textPattern->GetHost();
109     CHECK_NULL_VOID(host);
110     auto context = host->GetRenderContext();
111     CHECK_NULL_VOID(context);
112     const auto& selection = textPattern->GetTextSelector();
113     std::vector<Rect> selectedRects;
114     if (selection.GetTextStart() != selection.GetTextEnd()) {
115         paragraph_->GetRectsForRange(selection.GetTextStart(), selection.GetTextEnd(), selectedRects);
116     }
117     auto contentRect = textPattern->GetTextContentRect();
118     textOverlayModifier_->SetContentRect(contentRect);
119     textOverlayModifier_->SetSelectedRects(selectedRects);
120     auto pipelineContext = PipelineContext::GetCurrentContext();
121     CHECK_NULL_VOID(pipelineContext);
122     auto themeManager = pipelineContext->GetThemeManager();
123     CHECK_NULL_VOID(themeManager);
124     auto theme = themeManager->GetTheme<TextTheme>();
125     auto selectedColor = theme->GetSelectedColor().GetValue();
126     textOverlayModifier_->SetSelectedColor(selectedColor);
127     if (context->GetClipEdge().has_value()) {
128         textOverlayModifier_->SetIsClip(context->GetClipEdge().value());
129     }
130 }
131 } // namespace OHOS::Ace::NG