• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_SPAN_SPAN_OBJECT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_SPAN_SPAN_OBJECT_H
18 
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 #include "base/memory/ace_type.h"
24 #include "base/memory/referenced.h"
25 #include "core/components/common/layout/constants.h"
26 #include "core/components_ng/pattern/text/span/tlv_util.h"
27 #include "core/components_ng/pattern/text/span_node.h"
28 #include "core/components_ng/pattern/text/text_model.h"
29 #include "core/components_ng/pattern/text/text_styles.h"
30 #include "core/components_ng/pattern/text_field/text_field_model.h"
31 #include "core/components_ng/render/paragraph.h"
32 
33 namespace OHOS::Ace {
34 
35 enum class SpanType {
36     Font = 0,
37     Decoration,
38     BaselineOffset,
39     LetterSpacing,
40     TextShadow = 4,
41     LineHeight = 5,
42     BackgroundColor = 6,
43     Url = 7,
44     HalfLeading = 8,
45     Gesture = 100,
46     ParagraphStyle = 200,
47     Image = 300,
48     CustomSpan = 400,
49     ExtSpan = 500
50 };
51 
52 struct SpanParagraphStyle {
53     std::optional<TextAlign> align;
54     std::optional<int32_t> maxLines;
55     std::optional<WordBreak> wordBreak;
56     std::optional<TextOverflow> textOverflow;
57     std::optional<NG::LeadingMargin> leadingMargin;
58     std::optional<Dimension> textIndent;
59     std::optional<Dimension> paragraphSpacing;
60 
EqualSpanParagraphStyle61     bool Equal(const SpanParagraphStyle& other) const
62     {
63         auto flag = align == other.align && maxLines == other.maxLines && wordBreak == other.wordBreak &&
64                     textOverflow == other.textOverflow && textIndent == other.textIndent &&
65                     paragraphSpacing == other.paragraphSpacing;
66         if (leadingMargin.has_value() && other.leadingMargin.has_value()) {
67             flag &= leadingMargin.value().CheckLeadingMargin(other.leadingMargin.value());
68         } else if (!leadingMargin.has_value() && !other.textOverflow.has_value()) {
69             flag &= true;
70         } else {
71             flag &= false;
72         }
73         return flag;
74     }
75 };
76 
77 enum class SpanOperation {
78     ADD = 0,
79     REMOVE,
80 };
81 
82 class SpanWatcher : public virtual AceType {
83     DECLARE_ACE_TYPE(SpanWatcher, AceType);
84 
85 public:
86     virtual void UpdateSpanItems(const std::list<RefPtr<NG::SpanItem>>& spanItems) = 0;
87 };
88 
89 struct GestureStyle {
90     std::optional<GestureEventFunc> onClick;
91     std::optional<GestureEventFunc> onLongPress;
92 
IsEqualGestureStyle93     bool IsEqual(const GestureStyle& other) const
94     {
95         return false;
96     }
97 };
98 
99 class SpanBase : public virtual AceType {
100     DECLARE_ACE_TYPE(SpanBase, AceType);
101 
102 public:
103     SpanBase() = default;
SpanBase(int32_t start,int32_t end)104     SpanBase(int32_t start, int32_t end) : start_(start), end_(end) {}
105     virtual bool IsAttributesEqual(const RefPtr<SpanBase>& other) const = 0;
106     virtual RefPtr<SpanBase> GetSubSpan(int32_t start, int32_t end) = 0;
107     virtual SpanType GetSpanType() const = 0;
108     virtual void ApplyToSpanItem(const RefPtr<NG::SpanItem>& spanItem, SpanOperation operation) const = 0;
109     virtual std::string ToString() const = 0;
110 
111     int32_t GetStartIndex() const;
112     int32_t GetEndIndex() const;
113     void UpdateStartIndex(int32_t startIndex);
114     void UpdateEndIndex(int32_t endIndex);
115     int32_t GetLength() const;
116     std::optional<std::pair<int32_t, int32_t>> GetIntersectionInterval(std::pair<int32_t, int32_t> interval) const;
117 
118 private:
119     int32_t start_ = 0;
120     int32_t end_ = 0; // The interval rules are closed on the left and open on the right
121 };
122 
123 class FontSpan : public SpanBase {
124     DECLARE_ACE_TYPE(FontSpan, SpanBase);
125 
126 public:
127     FontSpan() = default;
128     explicit FontSpan(Font font);
129     FontSpan(Font font, int32_t start, int32_t end);
130     Font GetFont() const;
131     RefPtr<SpanBase> GetSubSpan(int32_t start, int32_t end) override;
132     bool IsAttributesEqual(const RefPtr<SpanBase>& other) const override;
133     SpanType GetSpanType() const override;
134     std::string ToString() const override;
135     void ApplyToSpanItem(const RefPtr<NG::SpanItem>& spanItem, SpanOperation operation) const override;
136     static RefPtr<SpanBase> CreateDefaultSpan();
137 
138 private:
139     void AddSpanStyle(const RefPtr<NG::SpanItem>& spanItem) const;
140     static void RemoveSpanStyle(const RefPtr<NG::SpanItem>& spanItem);
141 
142     Font font_;
143 };
144 
145 class DecorationSpan : public SpanBase {
146     DECLARE_ACE_TYPE(DecorationSpan, SpanBase);
147 
148 public:
149     DecorationSpan() = default;
150     explicit DecorationSpan(TextDecoration type, std::optional<Color> color, std::optional<TextDecorationStyle> style);
151     DecorationSpan(TextDecoration type, std::optional<Color> color, std::optional<TextDecorationStyle> style,
152         int32_t start, int32_t end);
153     TextDecoration GetTextDecorationType() const;
154     std::optional<Color> GetColor() const;
155     std::optional<TextDecorationStyle> GetTextDecorationStyle() const;
156     RefPtr<SpanBase> GetSubSpan(int32_t start, int32_t end) override;
157     bool IsAttributesEqual(const RefPtr<SpanBase>& other) const override;
158     SpanType GetSpanType() const override;
159     std::string ToString() const override;
160     void ApplyToSpanItem(const RefPtr<NG::SpanItem>& spanItem, SpanOperation operation) const override;
161 
162 private:
163     void AddDecorationStyle(const RefPtr<NG::SpanItem>& spanItem) const;
164     static void RemoveDecorationStyle(const RefPtr<NG::SpanItem>& spanItem);
165 
166     TextDecoration type_;
167     std::optional<Color> color_;
168     std::optional<TextDecorationStyle> style_;
169 };
170 
171 class BaselineOffsetSpan : public SpanBase {
172     DECLARE_ACE_TYPE(BaselineOffsetSpan, SpanBase);
173 
174 public:
175     BaselineOffsetSpan() = default;
176     explicit BaselineOffsetSpan(Dimension baselineOffset);
177     BaselineOffsetSpan(Dimension baselineOffset, int32_t start, int32_t end);
178     Dimension GetBaselineOffset() const;
179     RefPtr<SpanBase> GetSubSpan(int32_t start, int32_t end) override;
180     bool IsAttributesEqual(const RefPtr<SpanBase>& other) const override;
181     SpanType GetSpanType() const override;
182     std::string ToString() const override;
183     void ApplyToSpanItem(const RefPtr<NG::SpanItem>& spanItem, SpanOperation operation) const override;
184 
185 private:
186     void AddBaselineOffsetStyle(const RefPtr<NG::SpanItem>& spanItem) const;
187     static void RemoveBaselineOffsetStyle(const RefPtr<NG::SpanItem>& spanItem);
188     Dimension baselineOffset_;
189 };
190 
191 class LetterSpacingSpan : public SpanBase {
192     DECLARE_ACE_TYPE(LetterSpacingSpan, SpanBase);
193 
194 public:
195     LetterSpacingSpan() = default;
196     explicit LetterSpacingSpan(Dimension letterSpacing);
197     LetterSpacingSpan(Dimension letterSpacing, int32_t start, int32_t end);
198     Dimension GetLetterSpacing() const;
199     RefPtr<SpanBase> GetSubSpan(int32_t start, int32_t end) override;
200     bool IsAttributesEqual(const RefPtr<SpanBase>& other) const override;
201     SpanType GetSpanType() const override;
202     std::string ToString() const override;
203     void ApplyToSpanItem(const RefPtr<NG::SpanItem>& spanItem, SpanOperation operation) const override;
204 
205 private:
206     void AddLetterSpacingStyle(const RefPtr<NG::SpanItem>& spanItem) const;
207     static void RemoveLetterSpacingStyle(const RefPtr<NG::SpanItem>& spanItem);
208 
209     Dimension letterSpacing_;
210 };
211 
212 class GestureSpan : public SpanBase {
213     DECLARE_ACE_TYPE(GestureSpan, SpanBase);
214 
215 public:
216     GestureSpan() = default;
217     explicit GestureSpan(GestureStyle gestureInfo);
218     ~GestureSpan() override = default;
219     GestureSpan(GestureStyle gestureInfo, int32_t start, int32_t end);
220     GestureStyle GetGestureStyle() const;
221     RefPtr<SpanBase> GetSubSpan(int32_t start, int32_t end) override;
222     bool IsAttributesEqual(const RefPtr<SpanBase>& other) const override;
223     SpanType GetSpanType() const override;
224     std::string ToString() const override;
225     void ApplyToSpanItem(const RefPtr<NG::SpanItem>& spanItem, SpanOperation operation) const override;
226 
GetGestureSpanId()227     int32_t GetGestureSpanId()
228     {
229         return gestureSpanId_;
230     }
231 
SetGestureSpanId(int32_t gestureSpanId)232     void SetGestureSpanId(int32_t gestureSpanId)
233     {
234         gestureSpanId_ = gestureSpanId;
235     }
236 
237 private:
238     void AddSpanStyle(const RefPtr<NG::SpanItem>& spanItem) const;
239     static void RemoveSpanStyle(const RefPtr<NG::SpanItem>& spanItem);
240 
241     GestureStyle gestureInfo_;
242     int32_t gestureSpanId_ = -1;
243 };
244 
245 class TextShadowSpan : public SpanBase {
246     DECLARE_ACE_TYPE(TextShadowSpan, SpanBase);
247 
248 public:
249     TextShadowSpan() = default;
250     explicit TextShadowSpan(std::vector<Shadow> font);
251     TextShadowSpan(std::vector<Shadow> font, int32_t start, int32_t end);
252     std::vector<Shadow> GetTextShadow() const;
253     RefPtr<SpanBase> GetSubSpan(int32_t start, int32_t end) override;
254     bool IsAttributesEqual(const RefPtr<SpanBase>& other) const override;
255     SpanType GetSpanType() const override;
256     std::string ToString() const override;
257     void ApplyToSpanItem(const RefPtr<NG::SpanItem>& spanItem, SpanOperation operation) const override;
258 
259 private:
260     void AddSpanStyle(const RefPtr<NG::SpanItem>& spanItem) const;
261     static void RemoveSpanStyle(const RefPtr<NG::SpanItem>& spanItem);
262 
263     std::optional<std::vector<Shadow>> textShadow_ = std::nullopt;
264 };
265 class BackgroundColorSpan : public SpanBase {
266     DECLARE_ACE_TYPE(BackgroundColorSpan, SpanBase);
267 
268 public:
269     BackgroundColorSpan() = default;
270     explicit BackgroundColorSpan(std::optional<TextBackgroundStyle> textBackgroundStyle_);
271     BackgroundColorSpan(std::optional<TextBackgroundStyle> textBackgroundStyle_, int32_t start, int32_t end);
272     TextBackgroundStyle GetBackgroundColor() const;
273     void SetBackgroundColorGroupId(int32_t groupId);
274     RefPtr<SpanBase> GetSubSpan(int32_t start, int32_t end) override;
275     bool IsAttributesEqual(const RefPtr<SpanBase>& other) const override;
276     SpanType GetSpanType() const override;
277     std::string ToString() const override;
278     void ApplyToSpanItem(const RefPtr<NG::SpanItem>& spanItem, SpanOperation operation) const override;
279 
280 private:
281     std::optional<TextBackgroundStyle> textBackgroundStyle_;
282     void AddSpanStyle(const RefPtr<NG::SpanItem>& spanItem) const;
283     static void RemoveSpanStyle(const RefPtr<NG::SpanItem>& spanItem);
284 };
285 
286 class ImageSpan : public SpanBase {
287     DECLARE_ACE_TYPE(ImageSpan, SpanBase);
288 
289 public:
290     explicit ImageSpan(const ImageSpanOptions& options);
291     ImageSpan(const ImageSpanOptions& options, int32_t position);
292     bool IsAttributesEqual(const RefPtr<SpanBase>& other) const override;
293     RefPtr<SpanBase> GetSubSpan(int32_t start, int32_t end) override;
294     SpanType GetSpanType() const override;
295     void ApplyToSpanItem(const RefPtr<NG::SpanItem>& spanItem, SpanOperation operation) const override;
296     std::string ToString() const override;
297 
298     const ImageSpanOptions& GetImageSpanOptions();
299     const std::optional<ImageSpanAttribute>& GetImageAttribute() const;
300 
301 private:
302     ImageSpanOptions imageOptions_;
303 };
304 
305 class CustomSpan : public SpanBase {
306     DECLARE_ACE_TYPE(CustomSpan, SpanBase);
307 
308 public:
309     CustomSpan();
310     explicit CustomSpan(std::optional<std::function<CustomSpanMetrics(CustomSpanMeasureInfo)>> onMeasure,
311         std::optional<std::function<void(NG::DrawingContext&, CustomSpanOptions)>> onDraw);
312 
313     explicit CustomSpan(std::optional<std::function<CustomSpanMetrics(CustomSpanMeasureInfo)>> onMeasure,
314         std::optional<std::function<void(NG::DrawingContext&, CustomSpanOptions)>> onDraw, int32_t start, int32_t end);
315     virtual ~CustomSpan() override = default;
316     RefPtr<SpanBase> GetSubSpan(int32_t start, int32_t end) override;
317     bool IsAttributesEqual(const RefPtr<SpanBase>& other) const override;
318     SpanType GetSpanType() const override;
319     void SetOnMeasure(std::function<CustomSpanMetrics(CustomSpanMeasureInfo)> onMeasure);
320     void SetOnDraw(std::function<void(NG::DrawingContext&, CustomSpanOptions)> onDraw);
321     std::optional<std::function<CustomSpanMetrics(CustomSpanMeasureInfo)>> GetOnMeasure();
322     std::optional<std::function<void(NG::DrawingContext&, CustomSpanOptions)>> GetOnDraw();
323     std::string ToString() const override;
324     void ApplyToSpanItem(const RefPtr<NG::SpanItem>& spanItem, SpanOperation operation) const override;
AddStyledString(const WeakPtr<SpanStringBase> & spanString)325     virtual void AddStyledString(const WeakPtr<SpanStringBase>& spanString) {}
RemoveStyledString(const WeakPtr<SpanStringBase> & spanString)326     virtual void RemoveStyledString(const WeakPtr<SpanStringBase>& spanString) {}
327 
328 private:
329     std::optional<std::function<CustomSpanMetrics(CustomSpanMeasureInfo)>> onMeasure_;
330     std::optional<std::function<void(NG::DrawingContext&, CustomSpanOptions)>> onDraw_;
331 };
332 
333 class ParagraphStyleSpan : public SpanBase {
334     DECLARE_ACE_TYPE(ParagraphStyleSpan, SpanBase);
335 
336 public:
337     ParagraphStyleSpan() = default;
338     explicit ParagraphStyleSpan(SpanParagraphStyle paragraphStyle);
339     ParagraphStyleSpan(SpanParagraphStyle paragraphStyle, int32_t start, int32_t end);
340     SpanParagraphStyle GetParagraphStyle() const;
341     RefPtr<SpanBase> GetSubSpan(int32_t start, int32_t end) override;
342     bool IsAttributesEqual(const RefPtr<SpanBase>& other) const override;
343     SpanType GetSpanType() const override;
344     std::string ToString() const override;
345     void ApplyToSpanItem(const RefPtr<NG::SpanItem>& spanItem, SpanOperation operation) const override;
346 
347 private:
348     void AddParagraphStyle(const RefPtr<NG::SpanItem>& spanItem) const;
349     void RemoveParagraphStyle(const RefPtr<NG::SpanItem>& spanItem) const;
350 
351     SpanParagraphStyle paragraphStyle_;
352 };
353 
354 class LineHeightSpan : public SpanBase {
355     DECLARE_ACE_TYPE(LineHeightSpan, SpanBase);
356 
357 public:
358     LineHeightSpan() = default;
359     explicit LineHeightSpan(Dimension lineHeight);
360     LineHeightSpan(Dimension lineHeight, int32_t start, int32_t end);
361     Dimension GetLineHeight() const;
362     RefPtr<SpanBase> GetSubSpan(int32_t start, int32_t end) override;
363     bool IsAttributesEqual(const RefPtr<SpanBase>& other) const override;
364     SpanType GetSpanType() const override;
365     std::string ToString() const override;
366     void ApplyToSpanItem(const RefPtr<NG::SpanItem>& spanItem, SpanOperation operation) const override;
367 
368 private:
369     void AddLineHeightStyle(const RefPtr<NG::SpanItem>& spanItem) const;
370     void RemoveLineHeightStyle(const RefPtr<NG::SpanItem>& spanItem) const;
371 
372     Dimension lineHeight_;
373 };
374 
375 class HalfLeadingSpan : public SpanBase {
376     DECLARE_ACE_TYPE(HalfLeadingSpan, SpanBase);
377 
378 public:
379     HalfLeadingSpan() = default;
380     explicit HalfLeadingSpan(bool halfLeading);
381     HalfLeadingSpan(bool halfLeading, int32_t start, int32_t end);
382     bool GetHalfLeading() const;
383     RefPtr<SpanBase> GetSubSpan(int32_t start, int32_t end) override;
384     bool IsAttributesEqual(const RefPtr<SpanBase>& other) const override;
385     SpanType GetSpanType() const override;
386     std::string ToString() const override;
387     void ApplyToSpanItem(const RefPtr<NG::SpanItem>& spanItem, SpanOperation operation) const override;
388 
389 private:
390     void AddHalfLeadingStyle(const RefPtr<NG::SpanItem>& spanItem) const;
391     void RemoveHalfLeadingStyle(const RefPtr<NG::SpanItem>& spanItem) const;
392 
393     bool halfLeading_;
394 };
395 
396 class ExtSpan : public SpanBase {
397     DECLARE_ACE_TYPE(ExtSpan, SpanBase);
398 
399 public:
400     ExtSpan() = default;
401     ExtSpan(int32_t start, int32_t end);
402     RefPtr<SpanBase> GetSubSpan(int32_t start, int32_t end) override;
403     bool IsAttributesEqual(const RefPtr<SpanBase>& other) const override;
404     SpanType GetSpanType() const override;
405     std::string ToString() const override;
ApplyToSpanItem(const RefPtr<NG::SpanItem> & spanItem,SpanOperation operation)406     void ApplyToSpanItem(const RefPtr<NG::SpanItem>& spanItem, SpanOperation operation) const override {}
407 };
408 
409 class UrlSpan : public SpanBase {
410     DECLARE_ACE_TYPE(UrlSpan, SpanBase);
411 
412 public:
413     UrlSpan() = default;
414     explicit UrlSpan(const std::string& urlAddress);
415     UrlSpan(const std::string& urlAddress, int32_t start, int32_t end);
416     std::string GetUrlSpanAddress() const;
417     RefPtr<SpanBase> GetSubSpan(int32_t start, int32_t end) override;
418     bool IsAttributesEqual(const RefPtr<SpanBase>& other) const override;
419     SpanType GetSpanType() const override;
420     std::string ToString() const override;
421     void ApplyToSpanItem(const RefPtr<NG::SpanItem>& spanItem,
422         SpanOperation operation) const override;
423 private:
424     void AddUrlStyle(const RefPtr<NG::SpanItem>& spanItem) const;
425     static void RemoveUrlStyle(const RefPtr<NG::SpanItem>& spanItem);
426     std::string urlAddress_;
427 };
428 } // namespace OHOS::Ace
429 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_SPAN_SPAN_OBJECT_H