• 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_SVG_PARSE_SVG_ATTRIBUTES_PARSER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SVG_PARSE_SVG_ATTRIBUTES_PARSER_H
18 
19 #include <string>
20 
21 #include "base/memory/ace_type.h"
22 #include "base/utils/noncopyable.h"
23 #include "core/components/common/properties/color.h"
24 #include "core/components/common/properties/paint_state.h"
25 #include "core/components/common/properties/svg_paint_state.h"
26 #include "core/components_ng/svg/base/svg_length_scale_rule.h"
27 
28 namespace OHOS::Ace::NG {
29 struct TransformInfo;
30 
31 enum class SvgAlign {
32     ALIGN_XMIN_YMIN,
33     ALIGN_XMIN_YMID,
34     ALIGN_XMIN_YMAX,
35     ALIGN_XMID_YMIN,
36     ALIGN_XMID_YMID,
37     ALIGN_XMID_YMAX,
38     ALIGN_XMAX_YMIN,
39     ALIGN_XMAX_YMID,
40     ALIGN_XMAX_YMAX,
41     ALIGN_NONE,
42 };
43 
44 enum class SvgMeetOrSlice {
45     MEET,
46     SLICE,
47 };
48 
49 struct SvgPreserveAspectRatio {
50     SvgAlign svgAlign = SvgAlign::ALIGN_XMID_YMID;
51     SvgMeetOrSlice meetOrSlice = SvgMeetOrSlice::MEET;
52 };
53 
54 class SvgAttributesParser {
55 public:
56     static Color GetColor(const std::string& str);
57     static bool ParseColor(std::string value, Color& color);
58     static bool ParseRGBAMagicColor(const std::string& value, Color& color);
59     static std::optional<Color> GetSpecialColor(const std::string& value);
60     static LineCapStyle GetLineCapStyle(const std::string& val);
61     static LineJoinStyle GetLineJoinStyle(const std::string& val);
62     static Dimension ParseDimension(const std::string& value, bool useVp = false);
63     static double ParseDouble(const std::string& value);
64     static bool CheckColorAlpha(const std::string& colorStr, Color& result);
65     static std::pair<Dimension, Dimension> GetTransformOrigin(const std::string& transformOrigin);
66     static std::vector<NG::TransformInfo> GetTransformInfo(const std::string& transform);
67     static SvgAlign ParseSvgAlign(const std::string& value);
68     static SvgMeetOrSlice ParseSvgMeetOrSlice(const std::string& value);
69     static void ComputeTranslate(const Size& viewBox, const Size& viewPort, const float scaleX, const float scaleY,
70         const SvgAlign& svgAlign, float& translateX, float& translateY);
71     static void ComputeScale(const Size& viewBox, const Size& viewPort,
72         const SvgPreserveAspectRatio& preserveAspectRatio, float& scaleX, float& scaleY);
73     static Color GetColorFromHexString(const std::string& value);
74     static Color GetColorFrom4HexString(const std::string& value);
75     static void StringToDimensionWithUnitSvg(const std::string& value, Dimension& dimension);
76     static void ParseDimension(const std::string& value, Dimension& dimension, bool useVp = false);
77 };
78 enum class SvgFeColorMatrixType {
79     MATRIX,
80     SATURATE,
81     HUE_ROTATE,
82     LUMINACE_TO_ALPHA,
83 };
84 
85 enum class SvgColorInterpolationType {
86     LINEAR_RGB,
87     SRGB,
88     AUTO
89 };
90 
91 enum class SvgFeInType {
92     SOURCE_GRAPHIC,
93     SOURCE_ALPHA,
94     BACKGROUND_IMAGE,
95     BACKGROUND_ALPHA,
96     FILL_PAINT,
97     STROKE_PAINT,
98     PRIMITIVE
99 };
100 
101 enum class SvgFeEdgeMode {
102     EDGE_DUPLICATE,
103     EDGE_WRAP,
104     EDGE_NONE
105 };
106 
107 enum class SvgFeOperatorType {
108     FE_ARITHMETIC,
109     FE_ATOP,
110     FE_IN,
111     FE_LIGHTER,
112     FE_OUT,
113     FE_OVER,
114     FE_XOR
115 };
116 
117 enum class SvgFeBlendMode {
118     NORMAL,
119     MULTIPLY,
120     SCREEN,
121     DARKEN,
122     LIGHTEN
123 };
124 
125 struct SvgFeIn {
126     SvgFeInType in = SvgFeInType::PRIMITIVE;
127     std::string id;
128 };
129 
130 struct SvgAttributes {
131     Rect viewBox;
132     Dimension x;
133     Dimension y;
134     Dimension width = -1.0_px;
135     Dimension height = -1.0_px;
136     bool autoMirror = false;
137     SvgPreserveAspectRatio preserveAspectRatio;
138 };
139 
140 struct TransformInfo {
141     std::string funcType;
142     std::vector<std::string> paramVec;
143 };
144 
145 enum SvgRuleType {
146     SVG_RULE_NONEZERO = 0,
147     SVG_RULE_EVENODD
148 };
149 
150 constexpr float HALF_FLOAT = 0.5f;
151 class SvgClipAttribute {
152 public:
153     SvgClipAttribute() = default;
154     ~SvgClipAttribute() = default;
GetClipRule()155     const SvgRuleType& GetClipRule() const
156     {
157         return clipRule_;
158     }
159 
IsEvenodd()160     bool IsEvenodd() const
161     {
162         return clipRule_ == SvgRuleType::SVG_RULE_EVENODD;
163     }
164 
165     void SetClipRule(const SvgRuleType& clipRule, bool isSelf = true)
166     {
167         clipRule_ = clipRule;
168         hasClipRule_ = isSelf;
169     }
170 
GetHref()171     const std::string& GetHref() const
172     {
173         return href_;
174     }
175 
176     void SetHref(const std::string& href, bool isSelf = true)
177     {
178         href_ = href;
179         hasHref_ = isSelf;
180     }
181 
GetClipPathUnits()182     SvgLengthScaleUnit GetClipPathUnits() const
183     {
184         return clipPathUnits_;
185     }
186 
187     void SetClipPathUnits(SvgLengthScaleUnit clipPathUnits, bool isSelf = true)
188     {
189         clipPathUnits_ = clipPathUnits;
190         hasClipPathUnits_ = isSelf;
191     }
192 
Inherit(const SvgClipAttribute & svgClipAttribute)193     void Inherit(const SvgClipAttribute& svgClipAttribute)
194     {
195         if (!hasHref_) {
196             href_ = svgClipAttribute.GetHref();
197         }
198         if (!hasClipRule_) {
199             clipRule_ = svgClipAttribute.GetClipRule();
200         }
201         if (!hasClipPathUnits_) {
202             clipPathUnits_ = svgClipAttribute.GetClipPathUnits();
203         }
204     }
205 private:
206     std::string href_;
207     SvgRuleType clipRule_ = SvgRuleType::SVG_RULE_NONEZERO;
208     SvgLengthScaleUnit clipPathUnits_ = SvgLengthScaleUnit::USER_SPACE_ON_USE;
209     bool hasHref_ = false;
210     bool hasClipRule_ = false;
211     bool hasClipPathUnits_ = false;
212 };
213 
214 struct SvgBaseAttribute {
215     bool hasOpacity = false;
216     double opacity = 1.0;
217     FillState fillState;
218     StrokeState strokeState;
219     SvgTextStyle textStyle;
220     std::string transform;
221     std::vector<NG::TransformInfo> transformVec;
222     std::pair<Dimension, Dimension> transformOrigin;
223     std::string filterId;
224     std::string maskId;
225     std::string href;
226     std::string id;
227     SvgClipAttribute clipState;
228 
InheritFromUseSvgBaseAttribute229     void InheritFromUse(const SvgBaseAttribute& parent)
230     {
231         if (!hasOpacity) {
232             if (parent.hasOpacity) {
233                 fillState.SetOpacity(parent.opacity);
234                 opacity = parent.opacity;
235             } else {
236                 opacity = 1.0; // default opacity is 1.0
237             }
238         }
239         fillState.Inherit(parent.fillState);
240         strokeState.Inherit(parent.strokeState);
241         clipState.Inherit(parent.clipState);
242     }
243 
InheritSvgBaseAttribute244     void Inherit(const SvgBaseAttribute& parent)
245     {
246         if (!hasOpacity) {
247             if (parent.hasOpacity) {
248                 opacity = parent.opacity;
249             } else {
250                 opacity = 1.0; // default opacity is 1.0
251             }
252         }
253         fillState.Inherit(parent.fillState);
254         strokeState.Inherit(parent.strokeState);
255         clipState.Inherit(parent.clipState);
256     }
257 };
258 
259 
260 struct SvgAnimateAttribute {
261     std::string attributeName;
262     int32_t begin = 0;
263     int32_t dur = 0;
264     int32_t end = 0;
265     int32_t repeatCount = 1;
266     std::string fillMode;
267     std::string calcMode;
268     std::vector<std::string> values;
269     std::vector<double> keyTimes;
270     std::vector<std::string> keySplines;
271     std::string from;
272     std::string to;
273     std::vector<std::string> keyPoints;
274     std::string path;
275     std::string rotate;
276     std::string transformType;
277 };
278 
279 struct SvgStopAttribute {
280     GradientColor gradientColor;
281 };
282 
283 struct SvgRectAttribute {
284     Dimension x;
285     Dimension y;
286     Dimension rx = -1.0_px;
287     Dimension ry = -1.0_px;
288     Dimension width;
289     Dimension height;
290 };
291 
292 struct SvgMaskAttribute {
293     Dimension x = Dimension(-0.1, DimensionUnit::PERCENT); // x-axis default value
294     Dimension y = Dimension(-0.1, DimensionUnit::PERCENT); // y-axis default value
295     Dimension width = Dimension(1.2, DimensionUnit::PERCENT); // masking area width default value
296     Dimension height = Dimension(1.2, DimensionUnit::PERCENT); // masking area height default value
297     SvgLengthScaleUnit maskContentUnits = SvgLengthScaleUnit::USER_SPACE_ON_USE;
298     SvgLengthScaleUnit maskUnits = SvgLengthScaleUnit::OBJECT_BOUNDING_BOX;
299 };
300 
301 struct SvgCircleAttribute {
302     Dimension cx;
303     Dimension cy;
304     Dimension r;
305 };
306 
307 struct SvgPolygonAttribute {
308     std::string points;
309 };
310 
311 struct SvgEllipseAttribute {
312     Dimension cx;
313     Dimension cy;
314     Dimension rx = -1.0_px;
315     Dimension ry = -1.0_px;
316 };
317 
318 struct SvgLineAttribute {
319     Dimension x1;
320     Dimension y1;
321     Dimension x2;
322     Dimension y2;
323 };
324 
325 struct SvgPatternAttribute {
326     Dimension x; // x-axis default value
327     Dimension y; // y-axis default value
328     Dimension width; // pattern area width default value
329     Dimension height; // pattern area height default value
330     SvgLengthScaleUnit patternUnits = SvgLengthScaleUnit::OBJECT_BOUNDING_BOX;
331     SvgLengthScaleUnit patternContentUnits = SvgLengthScaleUnit::USER_SPACE_ON_USE;
332     std::string patternTransform;
333     Rect viewBox;
334 };
335 
336 struct SvgImageAttribute {
337     Dimension x = Dimension(0, DimensionUnit::PX); // x-axis default value
338     Dimension y = Dimension(0, DimensionUnit::PX); // y-axis default value
339     Dimension width = Dimension(0.0, DimensionUnit::PX); // image width default value
340     Dimension height = Dimension(0.0, DimensionUnit::PX); // image height default value
341     std::string href = "";
342 };
343 
344 struct SvgFilterAttribute {
345     Dimension x = Dimension(-0.1, DimensionUnit::PERCENT); // x-axis default value
346     Dimension y = Dimension(-0.1, DimensionUnit::PERCENT); // y-axis default value
347     Dimension width = Dimension(1.2, DimensionUnit::PERCENT); // masking area width default value
348     Dimension height = Dimension(1.2, DimensionUnit::PERCENT); // masking area height default value
349     SvgLengthScaleUnit filterUnits = SvgLengthScaleUnit::OBJECT_BOUNDING_BOX;
350     SvgLengthScaleUnit primitiveUnits = SvgLengthScaleUnit::USER_SPACE_ON_USE;
351 };
352 
353 struct SvgFeCommonAttribute {
354     Dimension x = Dimension(0.0, DimensionUnit::PERCENT);
355     Dimension y = Dimension(0.0, DimensionUnit::PERCENT);
356     Dimension height = Dimension(1.0, DimensionUnit::PERCENT);
357     Dimension width = Dimension(1.0, DimensionUnit::PERCENT);
358     std::string result;
359     SvgFeIn in;
360     SvgColorInterpolationType colorInterpolationType = SvgColorInterpolationType::SRGB;
361     std::optional<bool> isWidthValid;
362     std::optional<bool> isHeightValid;
363 };
364 
365 struct SvgFeFloodAttribute {
366     Color floodColor = Color::BLACK;
367     double floodOpacity = 1.0;
368 };
369 
370 struct SvgFeGaussianBlurAttribute {
371     float stdDeviationX = 0.0f;
372     float stdDeviationY = 0.0f;
373     SvgFeEdgeMode edgeMode = SvgFeEdgeMode::EDGE_DUPLICATE;
374 };
375 
376 struct SvgFeOffsetAttribute {
377     Dimension dx;
378     Dimension dy;
379 };
380 
381 struct SvgFeCompositeAttribute {
382     SvgFeIn in2;
383     SvgFeOperatorType operatorType = SvgFeOperatorType::FE_OVER;
384     float k1 = 0.0f;
385     float k2 = 0.0f;
386     float k3 = 0.0f;
387     float k4 = 0.0f;
388 };
389 
390 struct SvgFeBlendAttribute {
391     SvgFeIn in2;
392     SvgFeBlendMode blendMode = SvgFeBlendMode::NORMAL;
393 };
394 
395 struct SvgFeColorMatrixAttribute {
396     SvgFeColorMatrixType type = SvgFeColorMatrixType::MATRIX;
397     std::string values;
398 };
399 
400 enum class SvgSpreadMethod {
401     PAD = 0,
402     REPEAT,
403     REFLECT,
404 };
405 
406 struct SvgLinearGradientInfo {
407     float x1;
408     float x2;
409     float y1;
410     float y2;
411     int32_t spreadMethod;
412     std::string gradientTransform;
413     std::vector<GradientColor> colors;
414 };
415 
416 struct SvgLinearGradientAttribute {
417     Dimension x1 = Dimension(0.0, DimensionUnit::PERCENT);
418     Dimension y1 = Dimension(0.0, DimensionUnit::PERCENT);
419     Dimension x2 = Dimension(1.0, DimensionUnit::PERCENT);
420     Dimension y2 = Dimension(0.0, DimensionUnit::PERCENT);
421     std::string gradientTransform;
422     SvgLengthScaleUnit gradientUnits = SvgLengthScaleUnit::OBJECT_BOUNDING_BOX;
423     SvgSpreadMethod spreadMethod = SvgSpreadMethod::PAD;
424 };
425 
426 struct SvgRadialGradientInfo {
427     float cx;
428     float cy;
429     float r;
430     float fx;
431     float fy;
432     int32_t spreadMethod;
433     std::string gradientTransform;
434     std::vector<GradientColor> colors;
435 };
436 struct SvgRadialGradientAttribute {
437     Dimension cx = Dimension(HALF_FLOAT, DimensionUnit::PERCENT);
438     Dimension cy = Dimension(HALF_FLOAT, DimensionUnit::PERCENT);
439     Dimension r = Dimension(HALF_FLOAT, DimensionUnit::PERCENT);
440     std::optional<Dimension> fx;
441     std::optional<Dimension> fy;
442     std::string gradientTransform;
443     SvgLengthScaleUnit gradientUnits = SvgLengthScaleUnit::OBJECT_BOUNDING_BOX;
444     SvgSpreadMethod spreadMethod = SvgSpreadMethod::PAD;
445 };
446 
447 struct SvgGradientAttribute {
448     Gradient gradient = Gradient();
449 };
450 
451 } // namespace OHOS::Ace::NG
452 
453 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SVG_PARSE_SVG_ATTRIBUTES_PARSER_H