HTMLCanvasElement.cpp 2.9 KB

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