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/rich_editor/rich_editor_content_modifier.h"
17 #include "core/components_ng/pattern/rich_editor/rich_editor_pattern.h"
18
19 namespace OHOS::Ace::NG {
RichEditorContentModifier(const std::optional<TextStyle> & textStyle,const ParagraphManager * pManager,const WeakPtr<OHOS::Ace::NG::Pattern> & pattern)20 RichEditorContentModifier::RichEditorContentModifier(const std::optional<TextStyle>& textStyle,
21 const ParagraphManager* pManager, const WeakPtr<OHOS::Ace::NG::Pattern>& pattern)
22 : TextContentModifier(textStyle), pManager_(pManager), pattern_(pattern)
23 {
24 auto richEditorPattern = AceType::DynamicCast<RichEditorPattern>(pattern_.Upgrade());
25 CHECK_NULL_VOID(richEditorPattern);
26 richTextRectX_ = AceType::MakeRefPtr<PropertyFloat>(richEditorPattern->GetTextRect().GetX());
27 AttachProperty(richTextRectX_);
28 richTextRectY_ = AceType::MakeRefPtr<PropertyFloat>(richEditorPattern->GetTextRect().GetY());
29 AttachProperty(richTextRectY_);
30 clipOffset_ = AceType::MakeRefPtr<AnimatablePropertyOffsetF>(OffsetF());
31 AttachProperty(clipOffset_);
32 clipSize_ = AceType::MakeRefPtr<AnimatablePropertySizeF>(SizeF());
33 AttachProperty(clipSize_);
34 }
35
onDraw(DrawingContext & drawingContext)36 void RichEditorContentModifier::onDraw(DrawingContext& drawingContext)
37 {
38 CHECK_NULL_VOID(pManager_);
39 auto richEditorPattern = AceType::DynamicCast<RichEditorPattern>(pattern_.Upgrade());
40 CHECK_NULL_VOID(richEditorPattern);
41 auto& canvas = drawingContext.canvas;
42 canvas.Save();
43 auto contentRect = richEditorPattern->GetTextContentRect();
44 RSRect clipInnerRect = RSRect(contentRect.GetX(), contentRect.GetY(), contentRect.GetX() + contentRect.Width(),
45 contentRect.GetY() + contentRect.Height());
46 canvas.ClipRect(clipInnerRect, RSClipOp::INTERSECT);
47 auto&& paragraphs = pManager_->GetParagraphs();
48 auto offset = richEditorPattern->GetTextRect().GetOffset();
49 for (auto&& info : paragraphs) {
50 info.paragraph->Paint(drawingContext.canvas, offset.GetX(), offset.GetY());
51 offset.AddY(info.paragraph->GetHeight());
52 }
53 canvas.Restore();
54
55 auto clipOffset = clipOffset_->Get();
56 auto size = clipSize_->Get();
57 auto clipRect = RSRect(
58 clipOffset.GetX(), clipOffset.GetY(), clipOffset.GetX() + size.Width(), clipOffset.GetY() + size.Height());
59 drawingContext.canvas.ClipRect(clipRect, RSClipOp::INTERSECT);
60 }
61 } // namespace OHOS::Ace::NG
62