BasicShapeStyleValue.cpp 1.5 KB

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