SVGTextContentElement.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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/CSS/Parser/Parser.h>
  11. #include <LibWeb/DOM/Document.h>
  12. #include <LibWeb/Layout/SVGTextBox.h>
  13. #include <LibWeb/SVG/AttributeNames.h>
  14. #include <LibWeb/SVG/AttributeParser.h>
  15. #include <LibWeb/SVG/SVGGeometryElement.h>
  16. #include <LibWeb/SVG/SVGTextContentElement.h>
  17. namespace Web::SVG {
  18. SVGTextContentElement::SVGTextContentElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  19. : SVGGraphicsElement(document, move(qualified_name))
  20. {
  21. }
  22. JS::ThrowCompletionOr<void> SVGTextContentElement::initialize(JS::Realm& realm)
  23. {
  24. MUST_OR_THROW_OOM(Base::initialize(realm));
  25. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGTextContentElementPrototype>(realm, "SVGTextContentElement"));
  26. return {};
  27. }
  28. Optional<TextAnchor> SVGTextContentElement::text_anchor() const
  29. {
  30. if (!layout_node())
  31. return {};
  32. switch (layout_node()->computed_values().text_anchor()) {
  33. case CSS::TextAnchor::Start:
  34. return TextAnchor::Start;
  35. case CSS::TextAnchor::Middle:
  36. return TextAnchor::Middle;
  37. case CSS::TextAnchor::End:
  38. return TextAnchor::End;
  39. default:
  40. VERIFY_NOT_REACHED();
  41. }
  42. }
  43. // https://svgwg.org/svg2-draft/text.html#__svg__SVGTextContentElement__getNumberOfChars
  44. WebIDL::ExceptionOr<int> SVGTextContentElement::get_number_of_chars() const
  45. {
  46. auto chars = TRY_OR_THROW_OOM(vm(), utf8_to_utf16(child_text_content()));
  47. return static_cast<int>(chars.size());
  48. }
  49. }