SVGTextPositioningElement.cpp 2.3 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/Bindings/SVGTextPositioningElementPrototype.h>
  11. #include <LibWeb/CSS/Parser/Parser.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/SVG/AttributeNames.h>
  14. #include <LibWeb/SVG/AttributeParser.h>
  15. #include <LibWeb/SVG/SVGGeometryElement.h>
  16. #include <LibWeb/SVG/SVGTextPositioningElement.h>
  17. namespace Web::SVG {
  18. SVGTextPositioningElement::SVGTextPositioningElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  19. : SVGTextContentElement(document, move(qualified_name))
  20. {
  21. }
  22. void SVGTextPositioningElement::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGTextPositioningElement);
  26. }
  27. void SVGTextPositioningElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value)
  28. {
  29. SVGGraphicsElement::attribute_changed(name, old_value, value);
  30. if (name == SVG::AttributeNames::x) {
  31. m_x = AttributeParser::parse_number_percentage(value.value_or(String {}));
  32. } else if (name == SVG::AttributeNames::y) {
  33. m_y = AttributeParser::parse_number_percentage(value.value_or(String {}));
  34. } else if (name == SVG::AttributeNames::dx) {
  35. m_dx = AttributeParser::parse_number_percentage(value.value_or(String {}));
  36. } else if (name == SVG::AttributeNames::dy) {
  37. m_dy = AttributeParser::parse_number_percentage(value.value_or(String {}));
  38. }
  39. }
  40. Gfx::FloatPoint SVGTextPositioningElement::get_offset(CSSPixelSize const& viewport_size) const
  41. {
  42. auto const viewport_width = viewport_size.width().to_float();
  43. auto const viewport_height = viewport_size.height().to_float();
  44. float const x = m_x.value_or({ 0, false }).resolve_relative_to(viewport_width);
  45. float const y = m_y.value_or({ 0, false }).resolve_relative_to(viewport_height);
  46. float const dx = m_dx.value_or({ 0, false }).resolve_relative_to(viewport_width);
  47. float const dy = m_dy.value_or({ 0, false }).resolve_relative_to(viewport_height);
  48. return { x + dx, y + dy };
  49. }
  50. }