HTMLCanvasElement.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, DOM::QualifiedName qualified_name)
  18. : HTMLElement(document, move(qualified_name))
  19. {
  20. }
  21. HTMLCanvasElement::~HTMLCanvasElement() = default;
  22. unsigned HTMLCanvasElement::width() const
  23. {
  24. return attribute(HTML::AttributeNames::width).to_uint().value_or(300);
  25. }
  26. unsigned HTMLCanvasElement::height() const
  27. {
  28. return attribute(HTML::AttributeNames::height).to_uint().value_or(150);
  29. }
  30. void HTMLCanvasElement::set_width(unsigned value)
  31. {
  32. set_attribute(HTML::AttributeNames::width, String::number(value));
  33. m_bitmap = nullptr;
  34. if (m_context)
  35. m_context->reset_to_default_state();
  36. }
  37. void HTMLCanvasElement::set_height(unsigned value)
  38. {
  39. set_attribute(HTML::AttributeNames::height, String::number(value));
  40. m_bitmap = nullptr;
  41. if (m_context)
  42. m_context->reset_to_default_state();
  43. }
  44. RefPtr<Layout::Node> HTMLCanvasElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  45. {
  46. return adopt_ref(*new Layout::CanvasBox(document(), *this, move(style)));
  47. }
  48. CanvasRenderingContext2D* HTMLCanvasElement::get_context(String type)
  49. {
  50. if (type != "2d")
  51. return nullptr;
  52. if (!m_context)
  53. m_context = CanvasRenderingContext2D::create(*this);
  54. return m_context;
  55. }
  56. static Gfx::IntSize bitmap_size_for_canvas(HTMLCanvasElement const& canvas)
  57. {
  58. auto width = canvas.width();
  59. auto height = canvas.height();
  60. Checked<size_t> area = width;
  61. area *= height;
  62. if (area.has_overflow()) {
  63. dbgln("Refusing to create {}x{} canvas (overflow)", width, height);
  64. return {};
  65. }
  66. if (area.value() > max_canvas_area) {
  67. dbgln("Refusing to create {}x{} canvas (exceeds maximum size)", width, height);
  68. return {};
  69. }
  70. return Gfx::IntSize(width, height);
  71. }
  72. bool HTMLCanvasElement::create_bitmap()
  73. {
  74. auto size = bitmap_size_for_canvas(*this);
  75. if (size.is_empty()) {
  76. m_bitmap = nullptr;
  77. return false;
  78. }
  79. if (!m_bitmap || m_bitmap->size() != size) {
  80. auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, size);
  81. if (bitmap_or_error.is_error())
  82. return false;
  83. m_bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  84. }
  85. return m_bitmap;
  86. }
  87. String HTMLCanvasElement::to_data_url(String const& type, [[maybe_unused]] Optional<double> quality) const
  88. {
  89. if (!m_bitmap)
  90. return {};
  91. if (type != "image/png")
  92. return {};
  93. auto encoded_bitmap = Gfx::PNGWriter::encode(*m_bitmap);
  94. return AK::URL::create_with_data(type, encode_base64(encoded_bitmap), true).to_string();
  95. }
  96. }