1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.. All rights reserved.
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 #ifndef ROSEN_MODULES_SPTEXT_PARAGRAPH_IMPL_H
17 #define ROSEN_MODULES_SPTEXT_PARAGRAPH_IMPL_H
18 
19 #include <optional>
20 #include <pthread.h>
21 
22 #include "modules/skparagraph/include/Paragraph.h"
23 #include "txt/paint_record.h"
24 #include "txt/paragraph.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace SPText {
29 class ParagraphImpl : public Paragraph {
30 public:
31     ParagraphImpl(std::unique_ptr<skia::textlayout::Paragraph> paragraph,
32         std::vector<PaintRecord>&& paints);
33 
34     virtual ~ParagraphImpl() = default;
35 
36     double GetMaxWidth() override;
37 
38     double GetHeight() override;
39 
40     double GetLongestLine() override;
41 
42     double GetLongestLineWithIndent() override;
43 
44     double GetMinIntrinsicWidth() override;
45 
46     double GetMaxIntrinsicWidth() override;
47 
48     double GetAlphabeticBaseline() override;
49 
50     double GetIdeographicBaseline() override;
51 
52     double GetGlyphsBoundsTop() override;
53 
54     double GetGlyphsBoundsBottom() override;
55 
56     double GetGlyphsBoundsLeft() override;
57 
58     double GetGlyphsBoundsRight() override;
59 
60     bool DidExceedMaxLines() override;
61 
62     size_t GetLineCount() const override;
63 
64     void SetIndents(const std::vector<float>& indents) override;
65 
66     void MarkDirty() override;
67 
68     int32_t GetUnresolvedGlyphsCount() override;
69 
70     void UpdateFontSize(size_t from, size_t to, float fontSize) override;
71 
72     float DetectIndents(size_t index) override;
73 
74     void Layout(double width) override;
75 
76     void Paint(SkCanvas* canvas, double x, double y) override;
77 
78     void Paint(Drawing::Canvas* canvas, double x, double y) override;
79 
80     void Paint(Drawing::Canvas* canvas, Drawing::Path* path, double hOffset, double vOffset) override;
81 
82     std::vector<TextBox> GetRectsForRange(size_t start, size_t end,
83         RectHeightStyle rectHeightStyle, RectWidthStyle rectWidthStyle) override;
84 
85     std::vector<TextBox> GetRectsForPlaceholders() override;
86 
87     PositionWithAffinity GetGlyphPositionAtCoordinate(double dx, double dy) override;
88 
89     Range<size_t> GetWordBoundary(size_t offset) override;
90 
91     Range<size_t> GetActualTextRange(int lineNumber, bool includeSpaces) override;
92 
93     std::vector<skia::textlayout::LineMetrics> GetLineMetrics() override;
94 
95     bool GetLineMetricsAt(int lineNumber, skia::textlayout::LineMetrics* lineMetrics) const override;
96 
SetAnimation(std::function<bool (const std::shared_ptr<TextEngine::SymbolAnimationConfig> &)> & animationFunc)97     void SetAnimation(
98         std::function<bool(const std::shared_ptr<TextEngine::SymbolAnimationConfig>&)>& animationFunc
99     ) override
100     {
101         if (animationFunc != nullptr) {
102             animationFunc_ = animationFunc;
103         }
104     }
105 
SetParagraghId(uint32_t id)106     void SetParagraghId(uint32_t id) override
107     {
108         id_ = id;
109     }
110 
111     Drawing::FontMetrics MeasureText() override;
112 
113     Drawing::FontMetrics GetFontMetricsResult(const SPText::TextStyle& textStyle) override;
114 
115     bool GetLineFontMetrics(const size_t lineNumber, size_t& charNumber,
116         std::vector<Drawing::FontMetrics>& fontMetrics) override;
117     std::vector<std::unique_ptr<SPText::TextLineBase>> GetTextLines() const override;
118     std::unique_ptr<Paragraph> CloneSelf() override;
119     TextStyle SkStyleToTextStyle(const skia::textlayout::TextStyle& skStyle) override;
120     void UpdateColor(size_t from, size_t to, const RSColor& color) override;
121     Drawing::RectI GeneratePaintRegion(double x, double y) override;
122 
123 private:
124     void RecordDifferentPthreadCall(const char* caller) const;
125 
126     std::unique_ptr<skia::textlayout::Paragraph> paragraph_;
127     std::vector<PaintRecord> paints_;
128     std::optional<std::vector<LineMetrics>> lineMetrics_;
129     std::vector<TextStyle> lineMetricsStyles_;
130     std::function<bool(
131         const std::shared_ptr<OHOS::Rosen::TextEngine::SymbolAnimationConfig>&)> animationFunc_ = nullptr;
132     uint32_t id_ = 0;
133     mutable pthread_t threadId_;
134 };
135 } // namespace SPText
136 } // namespace Rosen
137 } // namespace OHOS
138 
139 #endif // ROSEN_MODULES_SPTEXT_PARAGRAPH_IMPL_H
140