• 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_HTML_TO_SPAN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_SPAN_HTML_TO_SPAN_H
18 
19 #include <list>
20 #include <string>
21 #include <unordered_map>
22 #include <variant>
23 #include <vector>
24 
25 #include "libxml/HTMLparser.h"
26 #include "libxml/HTMLtree.h"
27 #include "libxml/parser.h"
28 
29 #include "base/geometry/dimension.h"
30 #include "base/memory/ace_type.h"
31 #include "base/memory/referenced.h"
32 #include "base/utils/string_utils.h"
33 #include "base/utils/utils.h"
34 #include "core/components/common/properties/color.h"
35 #include "core/components/common/properties/text_style.h"
36 #include "core/components/text/text_theme.h"
37 #include "core/components_ng/pattern/text/span/mutable_span_string.h"
38 #include "core/components_ng/pattern/text/span/span_object.h"
39 #include "core/components_ng/pattern/text/span/span_string.h"
40 #include "core/components_ng/pattern/text/span_node.h"
41 #include "core/components_ng/pattern/text/text_model.h"
42 #include "core/components_ng/pattern/text/text_pattern.h"
43 #include "core/components_ng/pattern/text/text_styles.h"
44 
45 namespace OHOS::Ace {
46 
47 class SpanStringBase;
48 
49 class HtmlToSpan {
50 public:
HtmlToSpan()51     explicit HtmlToSpan() {};
~HtmlToSpan()52     ~HtmlToSpan() {};
53     RefPtr<MutableSpanString> ToSpanString(const std::string& html, const bool isNeedLoadPixelMap = true);
54     using Styles = std::vector<std::pair<std::string, std::string>>;
55 
56     class DecorationSpanParam {
57     public:
58         std::vector<TextDecoration> decorationType;
59         Color color;
60         TextDecorationStyle decorationSytle;
61     };
62     class BaseLineSpanParam {
63     public:
64         Dimension dimension;
65     };
66     class LetterSpacingSpanParam {
67     public:
68         Dimension dimension;
69     };
70     class LineHeightSpanSparam {
71     public:
72         Dimension dimension;
73     };
74     using StyleValue = std::variant<std::monostate, Font, DecorationSpanParam, BaseLineSpanParam,
75         LetterSpacingSpanParam, LineHeightSpanSparam, std::vector<Shadow>, ImageSpanOptions, SpanParagraphStyle,
76         TextBackgroundStyle, std::string>;
77     enum class StyleIndex {
78         STYLE_NULL = 0,
79         STYLE_FONT,
80         STYLE_DECORATION,
81         STYLE_BASELINE,
82         STYLE_LETTERSPACE,
83         STYLE_LINEHEIGHT,
84         STYLE_SHADOWS,
85         STYLE_IMAGE,
86         STYLE_PARAGRAPH,
87         STYLE_BACKGROUND_COLOR,
88         STYLE_URL,
89         STYLE_MAX
90     };
91 
92 private:
93     enum class HtmlType {
94         PARAGRAPH = 0,
95         IMAGE,
96         TEXT,
97         ANCHOR,
98         DEFAULT,
99     };
100 
101     struct SpanInfo {
102         HtmlType type;
103         size_t start;
104         size_t end;
105         std::vector<StyleValue> values;
106     };
107     using StyleValues = std::map<std::string, StyleValue>;
108     template<class T>
109     T* Get(StyleValue* styleValue) const;
110     Styles ParseStyleAttr(const std::string& style);
111     bool IsParagraphAttr(const std::string& key);
112     void InitParagraph(const std::string& key, const std::string& value, const std::string& index, StyleValues& values);
113     void InitFont(const std::string& key, const std::string& value, const std::string& index, StyleValues& values);
114     bool IsFontAttr(const std::string& key);
115     void InitDecoration(
116         const std::string& key, const std::string& value, const std::string& index, StyleValues& values);
117     bool IsDecorationAttr(const std::string& key);
118     template<class T>
119     void InitDimension(const std::string& key, const std::string& value, const std::string& index, StyleValues& values);
120     bool IsLetterSpacingAttr(const std::string& key);
121     void InitTextShadow(
122         const std::string& key, const std::string& value, const std::string& index, StyleValues& values);
123     bool IsTextShadowAttr(const std::string& key);
124     std::pair<std::string, double> GetUnitAndSize(const std::string& str);
125     bool IsLength(const std::string& str);
126     void InitShadow(Shadow &textShadow, std::vector<std::string> &attribute);
127     void InitLineHeight(const std::string& key, const std::string& value, StyleValues& values);
128     Dimension FromString(const std::string& str);
129     TextAlign StringToTextAlign(const std::string& value);
130     TextVerticalAlign StringToTextVerticalAlign(const std::string& value);
131     WordBreak StringToWordBreak(const std::string& value);
132     TextOverflow StringToTextOverflow(const std::string& value);
133     bool IsTextIndentAttr(const std::string& key);
134     bool IsLineHeightAttr(const std::string& key);
135     bool IsPaddingAttr(const std::string& key);
136     bool IsMarginAttr(const std::string& key);
137     bool IsBorderAttr(const std::string& key);
138     bool IsDecorationLine(const std::string& key);
139     bool IsDecorationStyle(const std::string& key);
140     bool IsBackgroundColorAttr(const std::string& key) const;
141     void InitBackgroundColor(
142         const std::string& key, const std::string& value, const std::string& index, StyleValues& values);
143     bool IsForegroundColorAttr(const std::string& key) const;
144     void InitForegroundColor(
145         const std::string& key, const std::string& value, const std::string& index, StyleValues& values);
146     void SetPaddingOption(const std::string& key, const std::string& value, ImageSpanOptions& options);
147     void SetMarginOption(const std::string& key, const std::string& value, ImageSpanOptions& options);
148     void SetBorderOption(const std::string& key, const std::string& value, ImageSpanOptions& options);
149     template<class T>
150     std::pair<bool, HtmlToSpan::StyleValue*> GetStyleValue(
151         const std::string& key, std::map<std::string, StyleValue>& values);
152     void HandleImgSpanOption(const Styles& styleMap, ImageSpanOptions& options);
153     void HandleImagePixelMap(const std::string& src, ImageSpanOptions& option);
154     void HandleImageSize(const std::string& key, const std::string& value, ImageSpanOptions& options);
155     void MakeImageSpanOptions(const std::string& key, const std::string& value, ImageSpanOptions& imgOpt);
156 
157     Color ToSpanColor(const std::string& color);
158 
159     void ToDefalutSpan(xmlNodePtr node, size_t len, size_t& pos, std::vector<SpanInfo>& spanInfos);
160     std::map<std::string, HtmlToSpan::StyleValue> ToTextSpanStyle(xmlAttrPtr curNode);
161     void AddStyleSpan(const std::string& element, SpanInfo& info);
162     void ToTextSpan(const std::string& element, xmlNodePtr node, size_t len,
163         size_t& pos, std::vector<SpanInfo>& spanInfos);
164 
165     void ToImageOptions(const std::map<std::string, std::string>& styles, ImageSpanOptions& option);
166     void ToImage(xmlNodePtr node, size_t len, size_t& pos, std::vector<SpanInfo>& spanInfos,
167         bool isProcessImageOptions = true);
168 
169     void ToParagraphStyle(const Styles& styleMap, SpanParagraphStyle& style);
170     void ToParagraphSpan(xmlNodePtr node, size_t len, size_t& pos, std::vector<SpanInfo>& spanInfos);
171 
172     void ParseHtmlToSpanInfo(
173         xmlNodePtr node, size_t& pos, std::string& allContent, std::vector<SpanInfo>& spanInfos,
174         bool isNeedLoadPixelMap = true);
175     void ToSpan(xmlNodePtr curNode, size_t& pos, std::string& allContent, std::vector<SpanInfo>& spanInfos,
176         bool isNeedLoadPixelMap = true);
177     void PrintSpanInfos(const std::vector<SpanInfo>& spanInfos);
178     void AfterProcSpanInfos(std::vector<SpanInfo>& spanInfos);
179     bool IsValidNode(const std::string& name);
180 
181     RefPtr<SpanBase> CreateSpan(size_t index, const SpanInfo& info, StyleValue& value);
182     template<class T, class P>
183     RefPtr<SpanBase> MakeSpan(const SpanInfo& info, StyleValue& value);
184     template<class T, class P>
185     RefPtr<SpanBase> MakeDimensionSpan(const SpanInfo& info, StyleValue& value);
186     RefPtr<SpanBase> MakeDecorationSpan(const SpanInfo& info, StyleValue& value);
187     void AddImageSpans(const SpanInfo& info, RefPtr<MutableSpanString> mutableSpan);
188     void AddSpans(const SpanInfo& info, RefPtr<MutableSpanString> span);
189     void ToAnchorSpan(xmlNodePtr node, size_t len, size_t& pos, std::vector<SpanInfo>& spanInfos);
190     std::string CleanTextSpaces(const std::string& text);
191 
192     std::string GetHtmlContent(xmlNodePtr node);
193     RefPtr<MutableSpanString> GenerateSpans(const std::string& allContent, const std::vector<SpanInfo>& spanInfos);
194     std::vector<SpanInfo> spanInfos_;
195     static constexpr double PT_TO_PX = 1.3;
196     static constexpr double ROUND_TO_INT = 0.5;
197 };
198 } // namespace OHOS::Ace
199 
200 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_SPAN_HTML_TO_SPAN_H