1 /*
2 * Copyright (c) 2021 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 #include "core/components/common/properties/decoration.h"
17
18 namespace OHOS::Ace {
19
SetContextAndCallback(const WeakPtr<PipelineContext> & context,const RenderNodeAnimationCallback & callback)20 void Decoration::SetContextAndCallback(
21 const WeakPtr<PipelineContext>& context, const RenderNodeAnimationCallback& callback)
22 {
23 backgroundColor_.SetContextAndCallback(context, callback);
24 border_.SetContextAndCallback(context, callback);
25 blurRadius_.SetContextAndCallback(context, callback);
26 }
27
AddShadow(const Shadow & shadow)28 void Decoration::AddShadow(const Shadow& shadow)
29 {
30 shadows_.push_back(shadow);
31 }
32
ClearAllShadow()33 void Decoration::ClearAllShadow()
34 {
35 shadows_.clear();
36 }
37
GetOccupiedSize(double dipScale) const38 Size Decoration::GetOccupiedSize(double dipScale) const
39 {
40 return border_.GetLayoutSize(dipScale) + padding_.GetLayoutSizeInPx(dipScale);
41 }
42
GetOffset(double dipScale) const43 Offset Decoration::GetOffset(double dipScale) const
44 {
45 return border_.GetOffset(dipScale) + padding_.GetOffsetInPx(dipScale);
46 }
47
VerticalSpaceOccupied(double dipScale) const48 double Decoration::VerticalSpaceOccupied(double dipScale) const
49 {
50 return border_.VerticalWidth(dipScale) + padding_.VerticalInPx(dipScale);
51 }
52
HorizontalSpaceOccupied(double dipScale) const53 double Decoration::HorizontalSpaceOccupied(double dipScale) const
54 {
55 return border_.HorizontalWidth(dipScale) + padding_.HorizontalInPx(dipScale);
56 }
57
AddColor(const GradientColor & color)58 void Gradient::AddColor(const GradientColor& color)
59 {
60 colors_.push_back(color);
61 }
62
ClearColors()63 void Gradient::ClearColors()
64 {
65 colors_.clear();
66 }
67
SetSizeTypeX(BackgroundImageSizeType type)68 void BackgroundImageSize::SetSizeTypeX(BackgroundImageSizeType type)
69 {
70 typeX_ = type;
71 }
72
SetSizeTypeY(BackgroundImageSizeType type)73 void BackgroundImageSize::SetSizeTypeY(BackgroundImageSizeType type)
74 {
75 if (type == BackgroundImageSizeType::CONTAIN || type == BackgroundImageSizeType::COVER) {
76 return;
77 }
78 typeY_ = type;
79 }
80
SetSizeValueX(double value)81 void BackgroundImageSize::SetSizeValueX(double value)
82 {
83 if (value < -0.0) {
84 return;
85 }
86 valueX_ = value;
87 }
88
SetSizeValueY(double value)89 void BackgroundImageSize::SetSizeValueY(double value)
90 {
91 if (value < -0.0) {
92 return;
93 }
94 valueY_ = value;
95 }
96
IsValid() const97 bool BackgroundImageSize::IsValid() const
98 {
99 if (typeY_ == BackgroundImageSizeType::CONTAIN || typeY_ == BackgroundImageSizeType::COVER) {
100 return false;
101 }
102
103 if ((typeX_ == BackgroundImageSizeType::LENGTH || typeX_ == BackgroundImageSizeType::PERCENT) &&
104 LessOrEqual(valueX_, 0.0)) {
105 return false;
106 }
107
108 if ((typeY_ == BackgroundImageSizeType::LENGTH || typeY_ == BackgroundImageSizeType::PERCENT) &&
109 LessOrEqual(valueY_, 0.0)) {
110 return false;
111 }
112
113 return true;
114 }
115
GetSizeTypeX() const116 BackgroundImageSizeType BackgroundImageSize::GetSizeTypeX() const
117 {
118 return typeX_;
119 }
120
GetSizeTypeY() const121 BackgroundImageSizeType BackgroundImageSize::GetSizeTypeY() const
122 {
123 return typeY_;
124 }
125
GetSizeValueX() const126 double BackgroundImageSize::GetSizeValueX() const
127 {
128 return valueX_;
129 }
130
GetSizeValueY() const131 double BackgroundImageSize::GetSizeValueY() const
132 {
133 return valueY_;
134 }
135
operator +(const BackgroundImageSize & rhs) const136 BackgroundImageSize BackgroundImageSize::operator+(const BackgroundImageSize& rhs) const
137 {
138 if ((rhs.GetSizeTypeX() != GetSizeTypeX()) || (rhs.GetSizeTypeY() != GetSizeTypeY())) {
139 // error: unit not same, just return lhs value
140 return *this;
141 }
142 auto rhsX = rhs.GetSizeValueX();
143 auto rhsY = rhs.GetSizeValueY();
144 auto lhsX = GetSizeValueX();
145 auto lhsY = GetSizeValueY();
146 BackgroundImageSize size;
147 size.SetSizeValueX(rhsX + lhsX);
148 size.SetSizeTypeX(GetSizeTypeX());
149 size.SetSizeValueY(rhsY + lhsY);
150 size.SetSizeTypeY(GetSizeTypeY());
151 return size;
152 }
153
operator -(const BackgroundImageSize & rhs) const154 BackgroundImageSize BackgroundImageSize::operator-(const BackgroundImageSize& rhs) const
155 {
156 auto rhsX = rhs.GetSizeValueX();
157 auto rhsY = rhs.GetSizeValueY();
158 auto lhsX = GetSizeValueX();
159 auto lhsY = GetSizeValueY();
160 if ((rhs.GetSizeTypeX() != GetSizeTypeX()) || (rhs.GetSizeTypeY() != GetSizeTypeY())) {
161 // error: unit not same, just return lhs value
162 return *this;
163 }
164 BackgroundImageSize size;
165 size.SetSizeValueX(lhsX - rhsX);
166 size.SetSizeTypeX(GetSizeTypeX());
167 size.SetSizeValueY(lhsY - rhsY);
168 size.SetSizeTypeY(GetSizeTypeY());
169 return size;
170 }
171
operator *(double value) const172 BackgroundImageSize BackgroundImageSize::operator*(double value) const
173 {
174 BackgroundImageSize size;
175 size.SetSizeValueX(GetSizeValueX() * value);
176 size.SetSizeTypeX(GetSizeTypeX());
177 size.SetSizeValueY(GetSizeValueY() * value);
178 size.SetSizeTypeY(GetSizeTypeY());
179 return size;
180 }
181
operator ==(const BackgroundImageSize & size) const182 bool BackgroundImageSize::operator==(const BackgroundImageSize& size) const
183 {
184 return typeX_ == size.GetSizeTypeX() && NearEqual(valueX_, size.GetSizeValueX()) && typeY_ == size.GetSizeTypeY() &&
185 NearEqual(valueY_, size.GetSizeValueY());
186 }
187
operator !=(const BackgroundImageSize & size) const188 bool BackgroundImageSize::operator!=(const BackgroundImageSize& size) const
189 {
190 return !operator==(size);
191 }
192
ToString() const193 std::string BackgroundImageSize::ToString() const
194 {
195 auto widthType = GetSizeTypeX();
196 if (widthType == BackgroundImageSizeType::CONTAIN) {
197 return "ImageSize.Contain";
198 }
199 if (widthType == BackgroundImageSizeType::COVER) {
200 return "ImageSize.Cover";
201 }
202 if (widthType == BackgroundImageSizeType::AUTO) {
203 return "ImageSize.Auto";
204 }
205 auto jsonValue = JsonUtil::Create(true);
206 Dimension width = Dimension((GetSizeValueX()), DimensionUnit::PX);
207 Dimension height = Dimension((GetSizeValueY()), DimensionUnit::PX);
208 jsonValue->Put("width", width.ToString().c_str());
209 jsonValue->Put("height", height.ToString().c_str());
210 return jsonValue->ToString();
211 }
212
operator +(const BackgroundImagePosition & rhs) const213 BackgroundImagePosition BackgroundImagePosition::operator+(const BackgroundImagePosition& rhs) const
214 {
215 auto rhsX = rhs.GetSizeValueX();
216 auto rhsY = rhs.GetSizeValueY();
217 auto lhsX = GetSizeValueX();
218 auto lhsY = GetSizeValueY();
219 if ((rhs.GetSizeTypeX() != GetSizeTypeX()) || (rhs.GetSizeTypeY() != GetSizeTypeY())) {
220 // error: unit not same, just return lhs value
221 return *this;
222 }
223 BackgroundImagePosition position;
224 position.SetSizeValueX(lhsX + rhsX);
225 position.SetSizeTypeX(GetSizeTypeX());
226 position.SetSizeValueY(lhsY + rhsY);
227 position.SetSizeTypeY(GetSizeTypeY());
228 return position;
229 }
230
operator -(const BackgroundImagePosition & rhs) const231 BackgroundImagePosition BackgroundImagePosition::operator-(const BackgroundImagePosition& rhs) const
232 {
233 auto rhsX = rhs.GetSizeValueX();
234 auto rhsY = rhs.GetSizeValueY();
235 auto lhsX = GetSizeValueX();
236 auto lhsY = GetSizeValueY();
237 if ((rhs.GetSizeTypeX() != GetSizeTypeX()) || (rhs.GetSizeTypeY() != GetSizeTypeY())) {
238 // error: unit not same, just return lhs value
239 return *this;
240 }
241 BackgroundImagePosition position;
242 position.SetSizeValueX(lhsX - rhsX);
243 position.SetSizeTypeX(GetSizeTypeX());
244 position.SetSizeValueY(lhsY - rhsY);
245 position.SetSizeTypeY(GetSizeTypeY());
246 return position;
247 }
248
operator *(double value) const249 BackgroundImagePosition BackgroundImagePosition::operator*(double value) const
250 {
251 BackgroundImagePosition position;
252 position.SetSizeValueX(GetSizeValueX() * value);
253 position.SetSizeTypeX(GetSizeTypeX());
254 position.SetSizeValueY(GetSizeValueY() * value);
255 position.SetSizeTypeY(GetSizeTypeY());
256 return position;
257 }
258
operator ==(const BackgroundImagePosition & backgroundImagePosition) const259 bool BackgroundImagePosition::operator==(const BackgroundImagePosition& backgroundImagePosition) const
260 {
261 bool isXAxisEqual = (GetSizeX() == backgroundImagePosition.GetSizeX()) &&
262 GetSizeTypeX() == backgroundImagePosition.GetSizeTypeX();
263 bool isYAxisEqual = (GetSizeY() == backgroundImagePosition.GetSizeY()) &&
264 GetSizeTypeY() == backgroundImagePosition.GetSizeTypeY();
265 return isXAxisEqual && isYAxisEqual;
266 }
267
operator !=(const BackgroundImagePosition & backgroundImagePosition) const268 bool BackgroundImagePosition::operator!=(const BackgroundImagePosition& backgroundImagePosition) const
269 {
270 return !operator==(backgroundImagePosition);
271 }
272
GetAlignmentType(double width,double height)273 static std::string GetAlignmentType(double width, double height)
274 {
275 const double halfDimension = 50.0;
276 auto jsonValue = JsonUtil::Create(true);
277 if (NearZero(width)) {
278 if (NearZero(height)) {
279 return "Alignment.TopStart";
280 }
281 if (NearEqual(height, halfDimension)) { // Determine whether the vertical element is centered
282 return "Alignment.Start";
283 }
284 return "Alignment.BottomStart";
285 } else if (NearEqual(width, halfDimension)) { // Judge whether the horizontal element is centered
286 if (NearZero(height)) {
287 return "Alignment.Top";
288 }
289 if (NearEqual(height, halfDimension)) {
290 return "Alignment.Center";
291 }
292 return "Alignment.Bottom";
293 } else {
294 if (NearZero(height)) {
295 return "Alignment.TopEnd";
296 }
297 if (NearEqual(height, halfDimension)) {
298 return "Alignment.End";
299 }
300 return "Alignment.BottomEnd";
301 }
302 }
303
ToString() const304 std::string BackgroundImagePosition::ToString() const
305 {
306 if (GetSizeTypeX() == BackgroundImagePositionType::PX) {
307 auto width = GetSizeValueX();
308 auto height = GetSizeValueY();
309 auto jsonValue = JsonUtil::Create(true);
310 jsonValue->Put("x", width);
311 jsonValue->Put("y", height);
312 return jsonValue->ToString();
313 }
314 auto width = GetSizeValueX();
315 auto height = GetSizeValueY();
316 return GetAlignmentType(width, height);
317 }
318
CanvasPath2D(const std::string & cmds)319 CanvasPath2D::CanvasPath2D(const std::string& cmds)
320 {
321 PathArgs args;
322 args.cmds = cmds;
323 caches_.emplace_back(PathCmd::CMDS, args);
324 }
325
CanvasPath2D(const RefPtr<CanvasPath2D> & path)326 CanvasPath2D::CanvasPath2D(const RefPtr<CanvasPath2D>& path)
327 {
328 if (path != nullptr) {
329 auto caches = path->GetCaches();
330 caches_.swap(caches);
331 }
332 }
333
AddPath(const RefPtr<CanvasPath2D> & path)334 void CanvasPath2D::AddPath(const RefPtr<CanvasPath2D>& path)
335 {
336 if (path != nullptr) {
337 auto caches = path->GetCaches();
338 caches_.insert(caches_.end(), caches.begin(), caches.end());
339 }
340 }
341
SetTransform(double a,double b,double c,double d,double e,double f)342 void CanvasPath2D::SetTransform(double a, double b, double c, double d, double e, double f)
343 {
344 PathArgs args;
345 args.para1 = a;
346 args.para2 = b;
347 args.para3 = c;
348 args.para4 = d;
349 args.para5 = e;
350 args.para6 = f;
351 caches_.emplace_back(PathCmd::TRANSFORM, args);
352 }
353
MoveTo(double x,double y)354 void CanvasPath2D::MoveTo(double x, double y)
355 {
356 PathArgs args;
357 args.para1 = x;
358 args.para2 = y;
359 caches_.emplace_back(PathCmd::MOVE_TO, args);
360 }
361
LineTo(double x,double y)362 void CanvasPath2D::LineTo(double x, double y)
363 {
364 PathArgs args;
365 args.para1 = x;
366 args.para2 = y;
367 caches_.emplace_back(PathCmd::LINE_TO, args);
368 }
369
Arc(double x,double y,double radius,double startAngle,double endAngle,double ccw)370 void CanvasPath2D::Arc(double x, double y, double radius, double startAngle, double endAngle, double ccw)
371 {
372 PathArgs args;
373 args.para1 = x;
374 args.para2 = y;
375 args.para3 = radius;
376 args.para4 = startAngle;
377 args.para5 = endAngle;
378 args.para6 = ccw;
379 caches_.emplace_back(PathCmd::ARC, args);
380 }
381
ArcTo(double x1,double y1,double x2,double y2,double radius)382 void CanvasPath2D::ArcTo(double x1, double y1, double x2, double y2, double radius)
383 {
384 PathArgs args;
385 args.para1 = x1;
386 args.para2 = y1;
387 args.para3 = x2;
388 args.para4 = y2;
389 args.para5 = radius;
390 caches_.emplace_back(PathCmd::ARC_TO, args);
391 }
392
QuadraticCurveTo(double cpx,double cpy,double x,double y)393 void CanvasPath2D::QuadraticCurveTo(double cpx, double cpy, double x, double y)
394 {
395 PathArgs args;
396 args.para1 = cpx;
397 args.para2 = cpy;
398 args.para3 = x;
399 args.para4 = y;
400 caches_.emplace_back(PathCmd::QUADRATIC_CURVE_TO, args);
401 }
402
BezierCurveTo(double cp1x,double cp1y,double cp2x,double cp2y,double x,double y)403 void CanvasPath2D::BezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y)
404 {
405 PathArgs args;
406 args.para1 = cp1x;
407 args.para2 = cp1y;
408 args.para3 = cp2x;
409 args.para4 = cp2y;
410 args.para5 = x;
411 args.para6 = y;
412 caches_.emplace_back(PathCmd::BEZIER_CURVE_TO, args);
413 }
414
Ellipse(double x,double y,double radiusX,double radiusY,double rotation,double startAngle,double endAngle,double ccw)415 void CanvasPath2D::Ellipse(
416 double x, double y, double radiusX, double radiusY, double rotation, double startAngle, double endAngle, double ccw)
417 {
418 PathArgs args;
419 args.para1 = x;
420 args.para2 = y;
421 args.para3 = radiusX;
422 args.para4 = radiusY;
423 args.para5 = rotation;
424 args.para6 = startAngle;
425 args.para7 = endAngle;
426 args.para8 = ccw;
427 caches_.emplace_back(PathCmd::ELLIPSE, args);
428 }
429
Rect(double x,double y,double width,double height)430 void CanvasPath2D::Rect(double x, double y, double width, double height)
431 {
432 PathArgs args;
433 args.para1 = x;
434 args.para2 = y;
435 args.para3 = width;
436 args.para4 = height;
437 caches_.emplace_back(PathCmd::RECT, args);
438 }
439
ClosePath()440 void CanvasPath2D::ClosePath()
441 {
442 PathArgs args;
443 caches_.emplace_back(PathCmd::CLOSE_PATH, args);
444 }
445
GetCaches() const446 const std::vector<std::pair<PathCmd, PathArgs>>& CanvasPath2D::GetCaches() const
447 {
448 return caches_;
449 }
450
ToString() const451 std::string CanvasPath2D::ToString() const
452 {
453 std::string str;
454 for (const auto& cache : caches_) {
455 switch (cache.first) {
456 case PathCmd::CMDS: {
457 str.append("CMDS:");
458 str.append(cache.second.cmds).append(" ");
459 break;
460 }
461 case PathCmd::TRANSFORM: {
462 str.append("TRANSFORM:");
463 str.append(std::to_string(cache.second.para1)).append(",");
464 str.append(std::to_string(cache.second.para2)).append(",");
465 str.append(std::to_string(cache.second.para3)).append(",");
466 str.append(std::to_string(cache.second.para4)).append(",");
467 str.append(std::to_string(cache.second.para5)).append(",");
468 str.append(std::to_string(cache.second.para6)).append(" ");
469 break;
470 }
471 case PathCmd::MOVE_TO: {
472 str.append("MOVE_TO:");
473 str.append(std::to_string(cache.second.para1)).append(",");
474 str.append(std::to_string(cache.second.para2)).append(" ");
475 break;
476 }
477 case PathCmd::LINE_TO: {
478 str.append("LINE_TO:");
479 str.append(std::to_string(cache.second.para1)).append(",");
480 str.append(std::to_string(cache.second.para2)).append(" ");
481 break;
482 }
483 case PathCmd::ARC: {
484 str.append("ARC:");
485 str.append(std::to_string(cache.second.para1)).append(",");
486 str.append(std::to_string(cache.second.para2)).append(",");
487 str.append(std::to_string(cache.second.para3)).append(",");
488 str.append(std::to_string(cache.second.para4)).append(",");
489 str.append(std::to_string(cache.second.para5)).append(",");
490 str.append(std::to_string(cache.second.para6)).append(" ");
491 break;
492 }
493 case PathCmd::ARC_TO: {
494 str.append("ARC_TO:");
495 str.append(std::to_string(cache.second.para1)).append(",");
496 str.append(std::to_string(cache.second.para2)).append(",");
497 str.append(std::to_string(cache.second.para3)).append(",");
498 str.append(std::to_string(cache.second.para4)).append(",");
499 str.append(std::to_string(cache.second.para5)).append(" ");
500 break;
501 }
502 case PathCmd::QUADRATIC_CURVE_TO: {
503 str.append("QUADRATIC_CURVE_TO:");
504 str.append(std::to_string(cache.second.para1)).append(",");
505 str.append(std::to_string(cache.second.para2)).append(",");
506 str.append(std::to_string(cache.second.para3)).append(",");
507 str.append(std::to_string(cache.second.para4)).append(" ");
508 break;
509 }
510 case PathCmd::BEZIER_CURVE_TO: {
511 str.append("BEZIER_CURVE_TO:");
512 str.append(std::to_string(cache.second.para1)).append(",");
513 str.append(std::to_string(cache.second.para2)).append(",");
514 str.append(std::to_string(cache.second.para3)).append(",");
515 str.append(std::to_string(cache.second.para4)).append(",");
516 str.append(std::to_string(cache.second.para5)).append(",");
517 str.append(std::to_string(cache.second.para6)).append(" ");
518 break;
519 }
520 case PathCmd::ELLIPSE: {
521 str.append("ELLIPSE:");
522 str.append(std::to_string(cache.second.para1)).append(",");
523 str.append(std::to_string(cache.second.para2)).append(",");
524 str.append(std::to_string(cache.second.para3)).append(",");
525 str.append(std::to_string(cache.second.para4)).append(",");
526 str.append(std::to_string(cache.second.para5)).append(",");
527 str.append(std::to_string(cache.second.para6)).append(",");
528 str.append(std::to_string(cache.second.para7)).append(",");
529 str.append(std::to_string(cache.second.para8)).append(" ");
530 break;
531 }
532 case PathCmd::RECT: {
533 str.append("RECT:");
534 str.append(std::to_string(cache.second.para1)).append(",");
535 str.append(std::to_string(cache.second.para2)).append(",");
536 str.append(std::to_string(cache.second.para3)).append(",");
537 str.append(std::to_string(cache.second.para4)).append(" ");
538 break;
539 }
540 case PathCmd::CLOSE_PATH: {
541 str.append("CLOSE_PATH").append(" ");
542 break;
543 }
544 default: {
545 break;
546 }
547 }
548 }
549 return str;
550 }
551
552 } // namespace OHOS::Ace
553