HTMLCanvasElement.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Base64.h>
  7. #include <AK/Checked.h>
  8. #include <LibGfx/Bitmap.h>
  9. #include <LibGfx/PNGWriter.h>
  10. #include <LibWeb/CSS/StyleResolver.h>
  11. #include <LibWeb/DOM/Document.h>
  12. #include <LibWeb/HTML/CanvasRenderingContext2D.h>
  13. #include <LibWeb/HTML/HTMLCanvasElement.h>
  14. #include <LibWeb/Layout/CanvasBox.h>
  15. namespace Web::HTML {
  16. static constexpr auto max_canvas_area = 16384 * 16384;
  17. HTMLCanvasElement::HTMLCanvasElement(DOM::Document& document, QualifiedName qualified_name)
  18. : HTMLElement(document, move(qualified_name))
  19. {
  20. }
  21. HTMLCanvasElement::~HTMLCanvasElement()
  22. {
  23. }
  24. unsigned HTMLCanvasElement::width() const
  25. {
  26. return attribute(HTML::AttributeNames::width).to_uint().value_or(300);
  27. }
  28. unsigned HTMLCanvasElement::height() const
  29. {
  30. return attribute(HTML::AttributeNames::height).to_uint().value_or(150);
  31. }
  32. RefPtr<Layout::Node> HTMLCanvasElement::create_layout_node()
  33. {
  34. auto style = document().style_resolver().resolve_style(*this);
  35. if (style->display() == CSS::Display::None)
  36. return nullptr;
  37. return adopt_ref(*new Layout::CanvasBox(document(), *this, move(style)));
  38. }
  39. CanvasRenderingContext2D* HTMLCanvasElement::get_context(String type)
  40. {
  41. VERIFY(type == "2d");
  42. if (!m_context)
  43. m_context = CanvasRenderingContext2D::create(*this);
  44. return m_context;
  45. }
  46. static Gfx::IntSize bitmap_size_for_canvas(const HTMLCanvasElement& canvas)
  47. {
  48. auto width = canvas.width();
  49. auto height = canvas.height();
  50. Checked<size_t> area = width;
  51. area *= height;
  52. if (area.has_overflow()) {
  53. dbgln("Refusing to create {}x{} canvas (overflow)", width, height);
  54. return {};
  55. }
  56. if (area.value() > max_canvas_area) {
  57. dbgln("Refusing to create {}x{} canvas (exceeds maximum size)", width, height);
  58. return {};
  59. }
  60. return Gfx::IntSize(width, height);
  61. }
  62. bool HTMLCanvasElement::create_bitmap()
  63. {
  64. auto size = bitmap_size_for_canvas(*this);
  65. if (size.is_empty()) {
  66. m_bitmap = nullptr;
  67. return false;
  68. }
  69. if (!m_bitmap || m_bitmap->size() != size)
  70. m_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, size);
  71. return m_bitmap;
  72. }
  73. String HTMLCanvasElement::to_data_url(const String& type, [[maybe_unused]] Optional<double> quality) const
  74. {
  75. if (!m_bitmap)
  76. return {};
  77. if (type != "image/png")
  78. return {};
  79. auto encoded_bitmap = Gfx::PNGWriter::encode(*m_bitmap);
  80. return URL::create_with_data(type, encode_base64(encoded_bitmap), true).to_string();
  81. }
  82. }