Path2D.cpp 4.0 KB

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