Path2D.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/HTML/Path2D.h>
  9. #include <LibWeb/SVG/AttributeParser.h>
  10. #include <LibWeb/SVG/SVGPathElement.h>
  11. namespace Web::HTML {
  12. JS::NonnullGCPtr<Path2D> Path2D::construct_impl(JS::Realm& realm, Optional<Variant<JS::Handle<Path2D>, DeprecatedString>> const& path)
  13. {
  14. return realm.heap().allocate<Path2D>(realm, realm, path).release_allocated_value_but_fixme_should_propagate_errors();
  15. }
  16. // https://html.spec.whatwg.org/multipage/canvas.html#dom-path2d
  17. Path2D::Path2D(JS::Realm& realm, Optional<Variant<JS::Handle<Path2D>, DeprecatedString>> const& path)
  18. : PlatformObject(realm)
  19. , CanvasPath(static_cast<Bindings::PlatformObject&>(*this))
  20. {
  21. // 1. Let output be a new Path2D object.
  22. // 2. If path is not given, then return output.
  23. if (!path.has_value())
  24. return;
  25. // 3. If path is a Path2D object, then add all subpaths of path to output and return output.
  26. // (In other words, it returns a copy of the argument.)
  27. if (path->has<JS::Handle<Path2D>>()) {
  28. this->path() = path->get<JS::Handle<Path2D>>()->path();
  29. return;
  30. }
  31. // 4. Let svgPath be the result of parsing and interpreting path according to SVG 2's rules for path data. [SVG]
  32. auto path_instructions = SVG::AttributeParser::parse_path_data(path->get<DeprecatedString>());
  33. auto svg_path = SVG::path_from_path_instructions(path_instructions);
  34. if (!svg_path.segments().is_empty()) {
  35. // 5. Let (x, y) be the last point in svgPath.
  36. auto xy = svg_path.segments().last().point();
  37. // 6. Add all the subpaths, if any, from svgPath to output.
  38. this->path() = move(svg_path);
  39. // 7. Create a new subpath in output with (x, y) as the only point in the subpath.
  40. this->move_to(xy.x(), xy.y());
  41. }
  42. // 8. Return output.
  43. }
  44. Path2D::~Path2D() = default;
  45. JS::ThrowCompletionOr<void> Path2D::initialize(JS::Realm& realm)
  46. {
  47. MUST_OR_THROW_OOM(Base::initialize(realm));
  48. set_prototype(&Bindings::ensure_web_prototype<Bindings::Path2DPrototype>(realm, "Path2D"));
  49. return {};
  50. }
  51. }