SVGTextContentElement.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.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/Bindings/SVGTextContentElementPrototype.h>
  11. #include <LibWeb/CSS/Parser/Parser.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/Layout/SVGTextBox.h>
  14. #include <LibWeb/SVG/AttributeNames.h>
  15. #include <LibWeb/SVG/AttributeParser.h>
  16. #include <LibWeb/SVG/SVGGeometryElement.h>
  17. #include <LibWeb/SVG/SVGTextContentElement.h>
  18. namespace Web::SVG {
  19. SVGTextContentElement::SVGTextContentElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  20. : SVGGraphicsElement(document, move(qualified_name))
  21. {
  22. }
  23. void SVGTextContentElement::initialize(JS::Realm& realm)
  24. {
  25. Base::initialize(realm);
  26. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGTextContentElement);
  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. ByteString SVGTextContentElement::text_contents() const
  44. {
  45. return child_text_content().to_byte_string().trim_whitespace();
  46. }
  47. // https://svgwg.org/svg2-draft/text.html#__svg__SVGTextContentElement__getNumberOfChars
  48. WebIDL::ExceptionOr<WebIDL::Long> SVGTextContentElement::get_number_of_chars() const
  49. {
  50. auto chars = TRY_OR_THROW_OOM(vm(), utf8_to_utf16(text_contents()));
  51. return static_cast<WebIDL::Long>(chars.size());
  52. }
  53. GC::Ref<Geometry::DOMPoint> SVGTextContentElement::get_start_position_of_char(WebIDL::UnsignedLong charnum)
  54. {
  55. dbgln("(STUBBED) SVGTextContentElement::get_start_position_of_char(charnum={}). Called on: {}", charnum, debug_description());
  56. return Geometry::DOMPoint::from_point(vm(), Geometry::DOMPointInit {});
  57. }
  58. }