HTMLSvgElement.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGfx/Path.h>
  27. #include <LibWeb/CSS/StyleResolver.h>
  28. #include <LibWeb/DOM/Document.h>
  29. #include <LibWeb/DOM/Event.h>
  30. #include <LibWeb/DOM/HTMLPathElement.h>
  31. #include <LibWeb/DOM/HTMLSvgElement.h>
  32. #include <LibWeb/Layout/LayoutSvg.h>
  33. #include <ctype.h>
  34. namespace Web {
  35. static constexpr auto max_svg_area = 16384 * 16384;
  36. HTMLSvgElement::HTMLSvgElement(Document& document, const FlyString& tag_name)
  37. : HTMLElement(document, tag_name)
  38. {
  39. }
  40. void HTMLSvgElement::parse_attribute(const FlyString& name, const String& value)
  41. {
  42. HTMLElement::parse_attribute(name, value);
  43. if (name == "stroke") {
  44. m_stroke_color = Gfx::Color::from_string(value);
  45. } else if (name == "stroke-width") {
  46. auto result = value.to_int();
  47. if (result.has_value())
  48. m_stroke_width = result.value();
  49. } else if (name == "fill") {
  50. m_fill_color = Gfx::Color::from_string(value);
  51. }
  52. }
  53. RefPtr<LayoutNode> HTMLSvgElement::create_layout_node(const StyleProperties* parent_style)
  54. {
  55. auto style = document().style_resolver().resolve_style(*this, parent_style);
  56. if (style->display() == CSS::Display::None)
  57. return nullptr;
  58. return adopt(*new LayoutSvg(document(), *this, move(style)));
  59. }
  60. static Gfx::IntSize bitmap_size_for_canvas(const HTMLSvgElement& canvas)
  61. {
  62. auto width = canvas.width();
  63. auto height = canvas.height();
  64. Checked<size_t> area = width;
  65. area *= height;
  66. if (area.has_overflow()) {
  67. dbg() << "Refusing to create " << width << "x" << height << " svg (overflow)";
  68. return {};
  69. }
  70. if (area.value() > max_svg_area) {
  71. dbg() << "Refusing to create " << width << "x" << height << " svg (exceeds maximum size)";
  72. return {};
  73. }
  74. return Gfx::IntSize(width, height);
  75. }
  76. bool HTMLSvgElement::create_bitmap()
  77. {
  78. auto size = bitmap_size_for_canvas(*this);
  79. if (size.is_empty()) {
  80. m_bitmap = nullptr;
  81. return false;
  82. }
  83. if (!m_bitmap || m_bitmap->size() != size)
  84. m_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGBA32, size);
  85. Gfx::Painter painter(*m_bitmap);
  86. paint(painter);
  87. return m_bitmap;
  88. }
  89. SvgPaintingContext HTMLSvgElement::make_context() const
  90. {
  91. SvgPaintingContext context;
  92. if (m_stroke_width.has_value())
  93. context.stroke_width = m_stroke_width.value();
  94. if (m_stroke_color.has_value())
  95. context.stroke_color = m_stroke_color.value();
  96. if (m_fill_color.has_value())
  97. context.fill_color = m_fill_color.value();
  98. return context;
  99. }
  100. unsigned HTMLSvgElement::width() const
  101. {
  102. return attribute(HTML::AttributeNames::width).to_uint().value_or(300);
  103. }
  104. unsigned HTMLSvgElement::height() const
  105. {
  106. return attribute(HTML::AttributeNames::height).to_uint().value_or(150);
  107. }
  108. static bool is_svg_graphic_element(const HTMLElement& element)
  109. {
  110. return is<HTMLPathElement>(element);
  111. }
  112. static SvgGraphicElement& as_svg_graphic_element(HTMLElement& element)
  113. {
  114. if (is<HTMLPathElement>(element))
  115. return to<HTMLPathElement>(element);
  116. ASSERT_NOT_REACHED();
  117. }
  118. void HTMLSvgElement::paint(Gfx::Painter& painter)
  119. {
  120. for_each_child([&](Node& child) {
  121. if (is<HTMLElement>(child)) {
  122. auto& element = to<HTMLElement>(child);
  123. if (is_svg_graphic_element(element)) {
  124. as_svg_graphic_element(element).paint(make_context(), painter);
  125. }
  126. }
  127. });
  128. }
  129. }