Path2D.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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);
  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. set_prototype(&Bindings::cached_web_prototype(realm, "Path2D"));
  22. // 1. Let output be a new Path2D object.
  23. // 2. If path is not given, then return output.
  24. if (!path.has_value())
  25. return;
  26. // 3. If path is a Path2D object, then add all subpaths of path to output and return output.
  27. // (In other words, it returns a copy of the argument.)
  28. if (path->has<JS::Handle<Path2D>>()) {
  29. this->path() = path->get<JS::Handle<Path2D>>()->path();
  30. return;
  31. }
  32. // 4. Let svgPath be the result of parsing and interpreting path according to SVG 2's rules for path data. [SVG]
  33. auto path_instructions = SVG::AttributeParser::parse_path_data(path->get<DeprecatedString>());
  34. auto svg_path = SVG::path_from_path_instructions(path_instructions);
  35. if (!svg_path.segments().is_empty()) {
  36. // 5. Let (x, y) be the last point in svgPath.
  37. auto xy = svg_path.segments().last().point();
  38. // 6. Add all the subpaths, if any, from svgPath to output.
  39. this->path() = move(svg_path);
  40. // 7. Create a new subpath in output with (x, y) as the only point in the subpath.
  41. this->move_to(xy.x(), xy.y());
  42. }
  43. // 8. Return output.
  44. }
  45. Path2D::~Path2D() = default;
  46. }