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 #ifndef ROSEN_MODULES_SPTEXT_PARAGRAPH_IMPL_H 16 #define ROSEN_MODULES_SPTEXT_PARAGRAPH_IMPL_H 17 18 #include <memory> 19 #include <mutex> 20 #include <optional> 21 #include <pthread.h> 22 #include <vector> 23 24 #include "modules/skparagraph/include/Paragraph.h" 25 #include "modules/skparagraph/include/DartTypes.h" 26 #include "symbol_engine/hm_symbol_run.h" 27 #include "txt/paint_record.h" 28 #include "txt/paragraph.h" 29 #include "txt/paragraph_style.h" 30 #include "txt/text_style.h" 31 32 #ifdef USE_M133_SKIA 33 #include "include/private/base/SkTArray.h" 34 #else 35 #include "include/private/SkTArray.h" 36 #endif 37 38 namespace OHOS { 39 namespace Rosen { 40 namespace SPText { 41 namespace skt = skia::textlayout; 42 class ParagraphImpl : public Paragraph { 43 public: 44 ParagraphImpl(std::unique_ptr<skt::Paragraph> paragraph, std::vector<PaintRecord>&& paints); 45 46 virtual ~ParagraphImpl() = default; 47 48 double GetMaxWidth() override; 49 50 double GetHeight() override; 51 52 double GetLongestLine() override; 53 54 double GetLongestLineWithIndent() override; 55 56 double GetMinIntrinsicWidth() override; 57 58 double GetMaxIntrinsicWidth() override; 59 60 double GetAlphabeticBaseline() override; 61 62 double GetIdeographicBaseline() override; 63 64 double GetGlyphsBoundsTop() override; 65 66 double GetGlyphsBoundsBottom() override; 67 68 double GetGlyphsBoundsLeft() override; 69 70 double GetGlyphsBoundsRight() override; 71 72 bool DidExceedMaxLines() override; 73 74 size_t GetLineCount() const override; 75 76 void SetIndents(const std::vector<float>& indents) override; 77 78 void MarkDirty() override; 79 80 int32_t GetUnresolvedGlyphsCount() override; 81 82 void UpdateFontSize(size_t from, size_t to, float fontSize) override; 83 84 float DetectIndents(size_t index) override; 85 86 void Layout(double width) override; 87 88 void Paint(SkCanvas* canvas, double x, double y) override; 89 90 void Paint(Drawing::Canvas* canvas, double x, double y) override; 91 92 void Paint(Drawing::Canvas* canvas, Drawing::Path* path, double hOffset, double vOffset) override; 93 94 std::vector<TextBox> GetRectsForRange(size_t start, size_t end, 95 RectHeightStyle rectHeightStyle, RectWidthStyle rectWidthStyle) override; 96 97 std::vector<TextBox> GetRectsForPlaceholders() override; 98 99 PositionWithAffinity GetGlyphPositionAtCoordinate(double dx, double dy) override; 100 101 Range<size_t> GetWordBoundary(size_t offset) override; 102 103 Range<size_t> GetActualTextRange(int lineNumber, bool includeSpaces) override; 104 105 Range<size_t> GetEllipsisTextRange() override; 106 107 std::vector<skt::LineMetrics> GetLineMetrics() override; 108 109 bool GetLineMetricsAt(int lineNumber, skt::LineMetrics* lineMetrics) const override; 110 SetAnimation(std::function<bool (const std::shared_ptr<TextEngine::SymbolAnimationConfig> &)> & animationFunc)111 void SetAnimation( 112 std::function<bool(const std::shared_ptr<TextEngine::SymbolAnimationConfig>&)>& animationFunc 113 ) override 114 { 115 if (animationFunc != nullptr) { 116 animationFunc_ = animationFunc; 117 } 118 } GetAnimation()119 std::function<bool(const std::shared_ptr<TextEngine::SymbolAnimationConfig>&)> GetAnimation() override 120 { 121 return animationFunc_; 122 } 123 SetParagraghId(uint32_t id)124 void SetParagraghId(uint32_t id) override 125 { 126 id_ = id; 127 } 128 129 Drawing::FontMetrics MeasureText() override; 130 131 Drawing::FontMetrics GetFontMetricsResult(const SPText::TextStyle& textStyle) override; 132 133 bool GetLineFontMetrics(const size_t lineNumber, size_t& charNumber, 134 std::vector<Drawing::FontMetrics>& fontMetrics) override; 135 std::vector<std::unique_ptr<SPText::TextLineBase>> GetTextLines() const override; 136 std::unique_ptr<Paragraph> CloneSelf() override; 137 TextStyle SkStyleToTextStyle(const skt::TextStyle& skStyle) override; 138 void UpdateColor(size_t from, size_t to, const RSColor& color, 139 skia::textlayout::UtfEncodeType encodeType) override; 140 Drawing::RectI GeneratePaintRegion(double x, double y) override; 141 void UpdateForegroundBrush(const TextStyle& spTextStyle) override; 142 143 void Relayout(double width, const ParagraphStyle& paragraphStyle, 144 const std::vector<OHOS::Rosen::SPText::TextStyle>& textStyles) override; 145 146 bool IsLayoutDone() override; 147 148 void SetLayoutState(size_t state) override; 149 150 void ApplyTextStyleChanges(const std::vector<OHOS::Rosen::SPText::TextStyle>& textStyles) override; 151 152 std::vector<TextBlobRecordInfo> GetTextBlobRecordInfo() const override; 153 154 bool HasSkipTextBlobDrawing() const override; 155 156 void SetSkipTextBlobDrawing(bool state) override; 157 isRunCombinated()158 bool isRunCombinated() { return paragraph_->isRunCombinated(); } 159 160 bool CanPaintAllText() const override; 161 162 std::string GetDumpInfo() const override; 163 164 private: 165 void ParagraphStyleUpdater(skt::Paragraph& skiaParagraph, const ParagraphStyle& spParagraphStyle, 166 skt::InternalState& state); 167 168 void TextStyleUpdater(skt::Block& skiaBlock, const TextStyle& spTextStyle, skt::InternalState& state); 169 170 void SymbolStyleUpdater(const HMSymbolTxt& symbolStyle, std::vector<std::shared_ptr<HMSymbolRun>>& hmSymbolRuns, 171 skt::InternalState& state); 172 173 void GetExtraTextStyleAttributes(const skt::TextStyle& skStyle, TextStyle& txt); 174 175 void ApplyParagraphStyleChanges(const ParagraphStyle& style); 176 #ifdef USE_M133_SKIA 177 void UpdateForegroundBrushWithValidData(skia_private::TArray<skt::Block, true>& skiaTextStyles, 178 const std::optional<RSBrush>& brush); 179 void UpdateForegroundBrushWithNullopt(skia_private::TArray<skt::Block, true>& skiaTextStyles); 180 #else 181 void UpdateForegroundBrushWithValidData(SkTArray<skt::Block, true>& skiaTextStyles, 182 const std::optional<RSBrush>& brush); 183 void UpdateForegroundBrushWithNullopt(SkTArray<skt::Block, true>& skiaTextStyles); 184 #endif 185 void UpdatePaintsBySkiaBlock(skt::Block& skiaBlock, const std::optional<RSBrush>& brush); 186 187 void RecordDifferentPthreadCall(const char* caller) const; 188 189 void InitSymbolRuns(); 190 191 void UpdateSymbolRun(const HMSymbolTxt& symbolStyle, std::shared_ptr<HMSymbolRun>& hmSymbolRun, 192 skt::InternalState& state, size_t index); 193 194 std::unique_ptr<skt::Paragraph> paragraph_; 195 std::vector<PaintRecord> paints_; 196 std::optional<std::vector<LineMetrics>> lineMetrics_; 197 std::vector<TextStyle> lineMetricsStyles_; 198 std::function<bool( 199 const std::shared_ptr<OHOS::Rosen::TextEngine::SymbolAnimationConfig>&)> animationFunc_ = nullptr; 200 uint32_t id_ = 0; 201 mutable pthread_t threadId_; 202 std::vector<std::shared_ptr<HMSymbolRun>> hmSymbols_; 203 std::once_flag initSymbolRunsFlag_; 204 }; 205 } // namespace SPText 206 } // namespace Rosen 207 } // namespace OHOS 208 209 #endif // ROSEN_MODULES_SPTEXT_PARAGRAPH_IMPL_H 210