HTMLCanvasElement.cpp 2.9 KB

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