SVGTextContentElement.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Utf16View.h>
  8. #include <LibJS/Runtime/Completion.h>
  9. #include <LibJS/Runtime/Utf16String.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/Layout/SVGTextBox.h>
  12. #include <LibWeb/SVG/AttributeNames.h>
  13. #include <LibWeb/SVG/AttributeParser.h>
  14. #include <LibWeb/SVG/SVGGeometryElement.h>
  15. #include <LibWeb/SVG/SVGTextContentElement.h>
  16. namespace Web::SVG {
  17. SVGTextContentElement::SVGTextContentElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  18. : SVGGraphicsElement(document, move(qualified_name))
  19. {
  20. }
  21. JS::ThrowCompletionOr<void> SVGTextContentElement::initialize(JS::Realm& realm)
  22. {
  23. MUST_OR_THROW_OOM(Base::initialize(realm));
  24. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGTextContentElementPrototype>(realm, "SVGTextContentElement"));
  25. return {};
  26. }
  27. void SVGTextContentElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
  28. {
  29. SVGGraphicsElement::parse_attribute(name, value);
  30. if (name == SVG::AttributeNames::x) {
  31. m_x = AttributeParser::parse_coordinate(value).value();
  32. } else if (name == SVG::AttributeNames::y) {
  33. m_y = AttributeParser::parse_coordinate(value).value();
  34. } else if (name == SVG::AttributeNames::dx) {
  35. m_dx = AttributeParser::parse_coordinate(value).value();
  36. } else if (name == SVG::AttributeNames::dy) {
  37. m_dy = AttributeParser::parse_coordinate(value).value();
  38. }
  39. }
  40. JS::GCPtr<Layout::Node> SVGTextContentElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  41. {
  42. return heap().allocate_without_realm<Layout::SVGTextBox>(document(), *this, move(style));
  43. }
  44. // https://svgwg.org/svg2-draft/text.html#__svg__SVGTextContentElement__getNumberOfChars
  45. WebIDL::ExceptionOr<int> SVGTextContentElement::get_number_of_chars() const
  46. {
  47. auto chars = TRY_OR_THROW_OOM(vm(), utf8_to_utf16(child_text_content()));
  48. return static_cast<int>(chars.size());
  49. }
  50. Gfx::FloatPoint SVGTextContentElement::get_offset() const
  51. {
  52. return { m_x + m_dx, m_y + m_dy };
  53. }
  54. }