HTMLCanvasElement.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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/Bitmap.h>
  27. #include <LibWeb/CSS/StyleResolver.h>
  28. #include <LibWeb/DOM/CanvasRenderingContext2D.h>
  29. #include <LibWeb/DOM/Document.h>
  30. #include <LibWeb/DOM/HTMLCanvasElement.h>
  31. #include <LibWeb/Layout/LayoutCanvas.h>
  32. namespace Web {
  33. static constexpr auto max_canvas_area = 16384 * 16384;
  34. HTMLCanvasElement::HTMLCanvasElement(Document& document, const FlyString& tag_name)
  35. : HTMLElement(document, tag_name)
  36. {
  37. }
  38. HTMLCanvasElement::~HTMLCanvasElement()
  39. {
  40. }
  41. int HTMLCanvasElement::requested_width() const
  42. {
  43. bool ok = false;
  44. unsigned width = attribute("width").to_int(ok);
  45. if (ok)
  46. return width;
  47. return 300;
  48. }
  49. int HTMLCanvasElement::requested_height() const
  50. {
  51. bool ok = false;
  52. unsigned height = attribute("height").to_int(ok);
  53. if (ok)
  54. return height;
  55. return 150;
  56. }
  57. RefPtr<LayoutNode> HTMLCanvasElement::create_layout_node(const StyleProperties* parent_style) const
  58. {
  59. auto style = document().style_resolver().resolve_style(*this, parent_style);
  60. auto display = style->string_or_fallback(CSS::PropertyID::Display, "inline");
  61. if (display == "none")
  62. return nullptr;
  63. return adopt(*new LayoutCanvas(*this, move(style)));
  64. }
  65. CanvasRenderingContext2D* HTMLCanvasElement::get_context(String type)
  66. {
  67. ASSERT(type.to_lowercase() == "2d");
  68. if (!m_context)
  69. m_context = CanvasRenderingContext2D::create(*this);
  70. return m_context;
  71. }
  72. static Gfx::Size bitmap_size_for_canvas(const HTMLCanvasElement& canvas)
  73. {
  74. int width = canvas.requested_width();
  75. int height = canvas.requested_height();
  76. if (width < 0 || height < 0) {
  77. dbg() << "Refusing to create canvas with negative size";
  78. return {};
  79. }
  80. int area = 0;
  81. if (__builtin_mul_overflow(width, height, &area)) {
  82. dbg() << "Refusing to create " << width << "x" << height << " canvas (overflow)";
  83. return {};
  84. }
  85. if (area > max_canvas_area) {
  86. dbg() << "Refusing to create " << width << "x" << height << " canvas (exceeds maximum size)";
  87. return {};
  88. }
  89. return { width, height };
  90. }
  91. bool HTMLCanvasElement::create_bitmap()
  92. {
  93. auto size = bitmap_size_for_canvas(*this);
  94. if (size.is_empty()) {
  95. m_bitmap = nullptr;
  96. return false;
  97. }
  98. if (!m_bitmap || m_bitmap->size() != size)
  99. m_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGBA32, size);
  100. return true;
  101. }
  102. }