SVGTextContentElement.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. void SVGTextContentElement::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGTextContentElement);
  26. }
  27. Optional<TextAnchor> SVGTextContentElement::text_anchor() const
  28. {
  29. if (!layout_node())
  30. return {};
  31. switch (layout_node()->computed_values().text_anchor()) {
  32. case CSS::TextAnchor::Start:
  33. return TextAnchor::Start;
  34. case CSS::TextAnchor::Middle:
  35. return TextAnchor::Middle;
  36. case CSS::TextAnchor::End:
  37. return TextAnchor::End;
  38. default:
  39. VERIFY_NOT_REACHED();
  40. }
  41. }
  42. ByteString SVGTextContentElement::text_contents() const
  43. {
  44. return child_text_content().to_byte_string().trim_whitespace();
  45. }
  46. // https://svgwg.org/svg2-draft/text.html#__svg__SVGTextContentElement__getNumberOfChars
  47. WebIDL::ExceptionOr<int> SVGTextContentElement::get_number_of_chars() const
  48. {
  49. auto chars = TRY_OR_THROW_OOM(vm(), utf8_to_utf16(text_contents()));
  50. return static_cast<int>(chars.size());
  51. }
  52. }