BasicShapeStyleValue.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2024, MacDue <macdue@dueutil.tech>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "BasicShapeStyleValue.h"
  7. namespace Web::CSS {
  8. Gfx::DeprecatedPath Polygon::to_path(CSSPixelRect reference_box, Layout::Node const& node) const
  9. {
  10. Gfx::DeprecatedPath path;
  11. bool first = true;
  12. for (auto const& point : points) {
  13. Gfx::FloatPoint resolved_point {
  14. static_cast<float>(point.x.to_px(node, reference_box.width())),
  15. static_cast<float>(point.y.to_px(node, reference_box.height()))
  16. };
  17. if (first)
  18. path.move_to(resolved_point);
  19. else
  20. path.line_to(resolved_point);
  21. first = false;
  22. }
  23. path.close();
  24. return path;
  25. }
  26. String Polygon::to_string() const
  27. {
  28. StringBuilder builder;
  29. builder.append("polygon("sv);
  30. bool first = true;
  31. for (auto const& point : points) {
  32. if (!first)
  33. builder.append(',');
  34. builder.appendff("{} {}", point.x, point.y);
  35. first = false;
  36. }
  37. builder.append(')');
  38. return MUST(builder.to_string());
  39. }
  40. BasicShapeStyleValue::~BasicShapeStyleValue() = default;
  41. Gfx::DeprecatedPath BasicShapeStyleValue::to_path(CSSPixelRect reference_box, Layout::Node const& node) const
  42. {
  43. return m_basic_shape.visit([&](auto const& shape) {
  44. return shape.to_path(reference_box, node);
  45. });
  46. }
  47. String BasicShapeStyleValue::to_string() const
  48. {
  49. return m_basic_shape.visit([](auto const& shape) {
  50. return shape.to_string();
  51. });
  52. }
  53. }