Path2D.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
  4. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/Bindings/Path2DPrototype.h>
  10. #include <LibWeb/Geometry/DOMMatrix.h>
  11. #include <LibWeb/HTML/Path2D.h>
  12. #include <LibWeb/SVG/AttributeParser.h>
  13. #include <LibWeb/SVG/SVGPathElement.h>
  14. namespace Web::HTML {
  15. GC_DEFINE_ALLOCATOR(Path2D);
  16. WebIDL::ExceptionOr<GC::Ref<Path2D>> Path2D::construct_impl(JS::Realm& realm, Optional<Variant<GC::Root<Path2D>, String>> const& path)
  17. {
  18. return realm.create<Path2D>(realm, path);
  19. }
  20. // https://html.spec.whatwg.org/multipage/canvas.html#dom-path2d
  21. Path2D::Path2D(JS::Realm& realm, Optional<Variant<GC::Root<Path2D>, String>> const& path)
  22. : PlatformObject(realm)
  23. , CanvasPath(static_cast<Bindings::PlatformObject&>(*this))
  24. {
  25. // 1. Let output be a new Path2D object.
  26. // 2. If path is not given, then return output.
  27. if (!path.has_value())
  28. return;
  29. // 3. If path is a Path2D object, then add all subpaths of path to output and return output.
  30. // (In other words, it returns a copy of the argument.)
  31. if (path->has<GC::Root<Path2D>>()) {
  32. this->path() = path->get<GC::Root<Path2D>>()->path();
  33. return;
  34. }
  35. // 4. Let svgPath be the result of parsing and interpreting path according to SVG 2's rules for path data. [SVG]
  36. auto path_instructions = SVG::AttributeParser::parse_path_data(path->get<String>());
  37. auto svg_path = SVG::path_from_path_instructions(path_instructions);
  38. if (!svg_path.is_empty()) {
  39. // 5. Let (x, y) be the last point in svgPath.
  40. auto xy = svg_path.last_point();
  41. // 6. Add all the subpaths, if any, from svgPath to output.
  42. this->path() = move(svg_path);
  43. // 7. Create a new subpath in output with (x, y) as the only point in the subpath.
  44. this->move_to(xy.x(), xy.y());
  45. }
  46. // 8. Return output.
  47. }
  48. Path2D::~Path2D() = default;
  49. void Path2D::initialize(JS::Realm& realm)
  50. {
  51. Base::initialize(realm);
  52. set_prototype(&Bindings::ensure_web_prototype<Bindings::Path2DPrototype>(realm, "Path2D"_fly_string));
  53. }
  54. // https://html.spec.whatwg.org/multipage/canvas.html#dom-path2d-addpath
  55. WebIDL::ExceptionOr<void> Path2D::add_path(GC::Ref<Path2D> path, Geometry::DOMMatrix2DInit& transform)
  56. {
  57. // The addPath(path, transform) method, when invoked on a Path2D object a, must run these steps:
  58. // 1. If the Path2D object path has no subpaths, then return.
  59. if (path->path().is_empty())
  60. return {};
  61. // 2. Let matrix be the result of creating a DOMMatrix from the 2D dictionary transform.
  62. auto matrix = TRY(Geometry::DOMMatrix::create_from_dom_matrix_2d_init(realm(), transform));
  63. // 3. If one or more of matrix's m11 element, m12 element, m21 element, m22 element, m41 element, or m42 element are infinite or NaN, then return.
  64. if (!isfinite(matrix->m11()) || !isfinite(matrix->m12()) || !isfinite(matrix->m21()) || !isfinite(matrix->m22()) || !isfinite(matrix->m41()) || !isfinite(matrix->m42()))
  65. return {};
  66. // 4. Create a copy of all the subpaths in path. Let this copy be known as c.
  67. // 5. Transform all the coordinates and lines in c by the transform matrix matrix.
  68. auto copy = path->path().copy_transformed(Gfx::AffineTransform { static_cast<float>(matrix->m11()), static_cast<float>(matrix->m12()), static_cast<float>(matrix->m21()), static_cast<float>(matrix->m22()), static_cast<float>(matrix->m41()), static_cast<float>(matrix->m42()) });
  69. // 6. Let (x, y) be the last point in the last subpath of c.
  70. auto xy = copy.last_point();
  71. // 7. Add all the subpaths in c to a.
  72. // FIXME: Is this correct?
  73. this->path().append_path(copy);
  74. // 8. Create a new subpath in a with (x, y) as the only point in the subpath.
  75. this->move_to(xy.x(), xy.y());
  76. return {};
  77. }
  78. }