HTMLCanvasElement.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/StyleComputer.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_computer().compute_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. if (type != "2d")
  42. return nullptr;
  43. if (!m_context)
  44. m_context = CanvasRenderingContext2D::create(*this);
  45. return m_context;
  46. }
  47. static Gfx::IntSize bitmap_size_for_canvas(const HTMLCanvasElement& canvas)
  48. {
  49. auto width = canvas.width();
  50. auto height = canvas.height();
  51. Checked<size_t> area = width;
  52. area *= height;
  53. if (area.has_overflow()) {
  54. dbgln("Refusing to create {}x{} canvas (overflow)", width, height);
  55. return {};
  56. }
  57. if (area.value() > max_canvas_area) {
  58. dbgln("Refusing to create {}x{} canvas (exceeds maximum size)", width, height);
  59. return {};
  60. }
  61. return Gfx::IntSize(width, height);
  62. }
  63. bool HTMLCanvasElement::create_bitmap()
  64. {
  65. auto size = bitmap_size_for_canvas(*this);
  66. if (size.is_empty()) {
  67. m_bitmap = nullptr;
  68. return false;
  69. }
  70. if (!m_bitmap || m_bitmap->size() != size)
  71. m_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, size);
  72. return m_bitmap;
  73. }
  74. String HTMLCanvasElement::to_data_url(const String& type, [[maybe_unused]] Optional<double> quality) const
  75. {
  76. if (!m_bitmap)
  77. return {};
  78. if (type != "image/png")
  79. return {};
  80. auto encoded_bitmap = Gfx::PNGWriter::encode(*m_bitmap);
  81. return AK::URL::create_with_data(type, encode_base64(encoded_bitmap), true).to_string();
  82. }
  83. }