Path2D.cpp 3.9 KB

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