| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-19 23:47:29 +02:00
										 |  |  | #include <AK/Base64.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-15 16:55:36 +02:00
										 |  |  | #include <AK/Checked.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | #include <LibGfx/Bitmap.h>
 | 
					
						
							| 
									
										
										
										
											2021-04-19 23:47:29 +02:00
										 |  |  | #include <LibGfx/PNGWriter.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-24 13:49:57 +02:00
										 |  |  | #include <LibWeb/CSS/StyleComputer.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | #include <LibWeb/DOM/Document.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-26 15:08:16 +02:00
										 |  |  | #include <LibWeb/HTML/CanvasRenderingContext2D.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/HTMLCanvasElement.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | #include <LibWeb/Layout/CanvasBox.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 18:20:36 +02:00
										 |  |  | namespace Web::HTML { | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-15 12:12:19 +02:00
										 |  |  | static constexpr auto max_canvas_area = 16384 * 16384; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-18 21:00:52 +01:00
										 |  |  | HTMLCanvasElement::HTMLCanvasElement(DOM::Document& document, DOM::QualifiedName qualified_name) | 
					
						
							| 
									
										
										
										
											2021-02-07 11:20:15 +01:00
										 |  |  |     : HTMLElement(document, move(qualified_name)) | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-14 13:21:51 -06:00
										 |  |  | HTMLCanvasElement::~HTMLCanvasElement() = default; | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-21 15:37:13 +02:00
										 |  |  | unsigned HTMLCanvasElement::width() const | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-21 15:37:13 +02:00
										 |  |  |     return attribute(HTML::AttributeNames::width).to_uint().value_or(300); | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-21 15:37:13 +02:00
										 |  |  | unsigned HTMLCanvasElement::height() const | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-21 15:37:13 +02:00
										 |  |  |     return attribute(HTML::AttributeNames::height).to_uint().value_or(150); | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-13 00:54:21 +01:00
										 |  |  | void HTMLCanvasElement::set_width(unsigned value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     set_attribute(HTML::AttributeNames::width, String::number(value)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void HTMLCanvasElement::set_height(unsigned value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     set_attribute(HTML::AttributeNames::height, String::number(value)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 13:17:01 +01:00
										 |  |  | RefPtr<Layout::Node> HTMLCanvasElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style) | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-04-23 16:46:57 +02:00
										 |  |  |     return adopt_ref(*new Layout::CanvasBox(document(), *this, move(style))); | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CanvasRenderingContext2D* HTMLCanvasElement::get_context(String type) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-07-27 20:42:28 +02:00
										 |  |  |     if (type != "2d") | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  |     if (!m_context) | 
					
						
							|  |  |  |         m_context = CanvasRenderingContext2D::create(*this); | 
					
						
							|  |  |  |     return m_context; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 10:57:59 +02:00
										 |  |  | static Gfx::IntSize bitmap_size_for_canvas(const HTMLCanvasElement& canvas) | 
					
						
							| 
									
										
										
										
											2020-04-15 12:12:19 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-21 15:37:13 +02:00
										 |  |  |     auto width = canvas.width(); | 
					
						
							|  |  |  |     auto height = canvas.height(); | 
					
						
							| 
									
										
										
										
											2020-04-15 16:55:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Checked<size_t> area = width; | 
					
						
							|  |  |  |     area *= height; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (area.has_overflow()) { | 
					
						
							| 
									
										
										
										
											2021-01-09 14:02:45 +01:00
										 |  |  |         dbgln("Refusing to create {}x{} canvas (overflow)", width, height); | 
					
						
							| 
									
										
										
										
											2020-04-15 12:12:19 +02:00
										 |  |  |         return {}; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-15 16:55:36 +02:00
										 |  |  |     if (area.value() > max_canvas_area) { | 
					
						
							| 
									
										
										
										
											2021-01-09 14:02:45 +01:00
										 |  |  |         dbgln("Refusing to create {}x{} canvas (exceeds maximum size)", width, height); | 
					
						
							| 
									
										
										
										
											2020-04-15 12:12:19 +02:00
										 |  |  |         return {}; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-21 15:37:13 +02:00
										 |  |  |     return Gfx::IntSize(width, height); | 
					
						
							| 
									
										
										
										
											2020-04-15 12:12:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool HTMLCanvasElement::create_bitmap() | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-04-15 12:12:19 +02:00
										 |  |  |     auto size = bitmap_size_for_canvas(*this); | 
					
						
							|  |  |  |     if (size.is_empty()) { | 
					
						
							|  |  |  |         m_bitmap = nullptr; | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-06 19:30:59 +01:00
										 |  |  |     if (!m_bitmap || m_bitmap->size() != size) { | 
					
						
							|  |  |  |         auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, size); | 
					
						
							|  |  |  |         if (bitmap_or_error.is_error()) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         m_bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-15 16:55:36 +02:00
										 |  |  |     return m_bitmap; | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-19 23:47:29 +02:00
										 |  |  | String HTMLCanvasElement::to_data_url(const String& type, [[maybe_unused]] Optional<double> quality) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!m_bitmap) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     if (type != "image/png") | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     auto encoded_bitmap = Gfx::PNGWriter::encode(*m_bitmap); | 
					
						
							| 
									
										
										
										
											2021-09-13 00:33:23 +03:00
										 |  |  |     return AK::URL::create_with_data(type, encode_base64(encoded_bitmap), true).to_string(); | 
					
						
							| 
									
										
										
										
											2021-04-19 23:47:29 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 19:07:56 +01:00
										 |  |  | } |