summaryrefslogtreecommitdiff
path: root/Libraries/LibWeb/DOM
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-03-19 19:07:56 +0100
committerAndreas Kling <kling@serenityos.org>2020-03-19 19:07:56 +0100
commita37c29e35381bc2560515b384b442dab81a35fee (patch)
treee78428625be546f078e08132bebc2afbd98ada50 /Libraries/LibWeb/DOM
parent73d28a0551e75958270445efa67aca150621c8b9 (diff)
downloadserenity-a37c29e35381bc2560515b384b442dab81a35fee.zip
LibWeb: Add <canvas> element and start fleshing out CRC2D
This patch adds HTMLCanvasElement along with a LayoutCanvas object. The DOM and layout parts are very similar to <img> elements. The <canvas> element holds a Gfx::Bitmap which is sized according to the "width" and "height" attributes on the element. Calling .getContext("2d") on a <canvas> element gives you a context object that draws into the underlying Gfx::Bitmap of the <canvas>. The context weakly points to the <canvas> which allows it to outlive the canvas element if needed. This is really quite cool. :^)
Diffstat (limited to 'Libraries/LibWeb/DOM')
-rw-r--r--Libraries/LibWeb/DOM/CanvasRenderingContext2D.cpp44
-rw-r--r--Libraries/LibWeb/DOM/CanvasRenderingContext2D.h38
-rw-r--r--Libraries/LibWeb/DOM/ElementFactory.cpp3
-rw-r--r--Libraries/LibWeb/DOM/HTMLCanvasElement.cpp92
-rw-r--r--Libraries/LibWeb/DOM/HTMLCanvasElement.h66
5 files changed, 243 insertions, 0 deletions
diff --git a/Libraries/LibWeb/DOM/CanvasRenderingContext2D.cpp b/Libraries/LibWeb/DOM/CanvasRenderingContext2D.cpp
new file mode 100644
index 0000000000..4e2bc49d7b
--- /dev/null
+++ b/Libraries/LibWeb/DOM/CanvasRenderingContext2D.cpp
@@ -0,0 +1,44 @@
+#include <AK/OwnPtr.h>
+#include <LibGfx/Painter.h>
+#include <LibWeb/DOM/CanvasRenderingContext2D.h>
+#include <LibWeb/DOM/HTMLCanvasElement.h>
+
+namespace Web {
+
+CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement& element)
+ : m_element(element.make_weak_ptr())
+{
+}
+
+CanvasRenderingContext2D::~CanvasRenderingContext2D()
+{
+}
+
+void CanvasRenderingContext2D::set_fill_style(String style)
+{
+ m_fill_style = Gfx::Color::from_string(style).value_or(Color::Black);
+}
+
+String CanvasRenderingContext2D::fill_style() const
+{
+ return m_fill_style.to_string();
+}
+
+void CanvasRenderingContext2D::fill_rect(int x, int y, int width, int height)
+{
+ auto painter = this->painter();
+ if (!painter)
+ return;
+
+ painter->fill_rect({ x, y, width, height }, m_fill_style);
+}
+
+OwnPtr<Gfx::Painter> CanvasRenderingContext2D::painter()
+{
+ if (!m_element)
+ return nullptr;
+
+ return make<Gfx::Painter>(m_element->ensure_bitmap());
+}
+
+}
diff --git a/Libraries/LibWeb/DOM/CanvasRenderingContext2D.h b/Libraries/LibWeb/DOM/CanvasRenderingContext2D.h
new file mode 100644
index 0000000000..8a36742015
--- /dev/null
+++ b/Libraries/LibWeb/DOM/CanvasRenderingContext2D.h
@@ -0,0 +1,38 @@
+#pragma once
+
+#include <AK/RefCounted.h>
+#include <LibGfx/Color.h>
+#include <LibGfx/Forward.h>
+#include <LibWeb/Bindings/Wrappable.h>
+
+namespace Web {
+
+class CanvasRenderingContext2D
+ : public RefCounted<CanvasRenderingContext2D>
+ , public Bindings::Wrappable {
+
+ AK_MAKE_NONCOPYABLE(CanvasRenderingContext2D);
+ AK_MAKE_NONMOVABLE(CanvasRenderingContext2D);
+
+public:
+ using WrapperType = Bindings::CanvasRenderingContext2DWrapper;
+
+ static NonnullRefPtr<CanvasRenderingContext2D> create(HTMLCanvasElement& element) { return adopt(*new CanvasRenderingContext2D(element)); }
+ ~CanvasRenderingContext2D();
+
+ void set_fill_style(String);
+ String fill_style() const;
+
+ void fill_rect(int x, int y, int width, int height);
+
+private:
+ explicit CanvasRenderingContext2D(HTMLCanvasElement&);
+
+ OwnPtr<Gfx::Painter> painter();
+
+ WeakPtr<HTMLCanvasElement> m_element;
+
+ Gfx::Color m_fill_style;
+};
+
+}
diff --git a/Libraries/LibWeb/DOM/ElementFactory.cpp b/Libraries/LibWeb/DOM/ElementFactory.cpp
index 3eef44c953..6d0998d719 100644
--- a/Libraries/LibWeb/DOM/ElementFactory.cpp
+++ b/Libraries/LibWeb/DOM/ElementFactory.cpp
@@ -29,6 +29,7 @@
#include <LibWeb/DOM/HTMLBRElement.h>
#include <LibWeb/DOM/HTMLBlinkElement.h>
#include <LibWeb/DOM/HTMLBodyElement.h>
+#include <LibWeb/DOM/HTMLCanvasElement.h>
#include <LibWeb/DOM/HTMLFontElement.h>
#include <LibWeb/DOM/HTMLFormElement.h>
#include <LibWeb/DOM/HTMLHRElement.h>
@@ -85,6 +86,8 @@ NonnullRefPtr<Element> create_element(Document& document, const String& tag_name
}
if (lowercase_tag_name == "script")
return adopt(*new HTMLScriptElement(document, lowercase_tag_name));
+ if (lowercase_tag_name == "canvas")
+ return adopt(*new HTMLCanvasElement(document, lowercase_tag_name));
return adopt(*new Element(document, lowercase_tag_name));
}
diff --git a/Libraries/LibWeb/DOM/HTMLCanvasElement.cpp b/Libraries/LibWeb/DOM/HTMLCanvasElement.cpp
new file mode 100644
index 0000000000..106f17bdbf
--- /dev/null
+++ b/Libraries/LibWeb/DOM/HTMLCanvasElement.cpp
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <LibGfx/Bitmap.h>
+#include <LibWeb/CSS/StyleResolver.h>
+#include <LibWeb/DOM/CanvasRenderingContext2D.h>
+#include <LibWeb/DOM/Document.h>
+#include <LibWeb/DOM/HTMLCanvasElement.h>
+#include <LibWeb/Layout/LayoutCanvas.h>
+
+namespace Web {
+
+HTMLCanvasElement::HTMLCanvasElement(Document& document, const String& tag_name)
+ : HTMLElement(document, tag_name)
+{
+}
+
+HTMLCanvasElement::~HTMLCanvasElement()
+{
+}
+
+int HTMLCanvasElement::preferred_width() const
+{
+ bool ok = false;
+ int width = attribute("width").to_int(ok);
+ if (ok)
+ return width;
+
+ return 300;
+}
+
+int HTMLCanvasElement::preferred_height() const
+{
+ bool ok = false;
+ int height = attribute("height").to_int(ok);
+ if (ok)
+ return height;
+
+ return 300;
+}
+
+RefPtr<LayoutNode> HTMLCanvasElement::create_layout_node(const StyleProperties* parent_style) const
+{
+ auto style = document().style_resolver().resolve_style(*this, parent_style);
+ auto display = style->string_or_fallback(CSS::PropertyID::Display, "inline");
+ if (display == "none")
+ return nullptr;
+ return adopt(*new LayoutCanvas(*this, move(style)));
+}
+
+CanvasRenderingContext2D* HTMLCanvasElement::get_context(String type)
+{
+ ASSERT(type.to_lowercase() == "2d");
+ dbg() << "get_context with m_context=" << m_context.ptr();
+ if (!m_context)
+ m_context = CanvasRenderingContext2D::create(*this);
+ dbg() << "get_context returning m_context=" << m_context.ptr();
+ return m_context;
+}
+
+Gfx::Bitmap& HTMLCanvasElement::ensure_bitmap()
+{
+ if (!m_bitmap || m_bitmap->size() != Gfx::Size(preferred_width(), preferred_height())) {
+ m_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGBA32, { preferred_width(), preferred_height() });
+ }
+ return *m_bitmap;
+}
+
+}
diff --git a/Libraries/LibWeb/DOM/HTMLCanvasElement.h b/Libraries/LibWeb/DOM/HTMLCanvasElement.h
new file mode 100644
index 0000000000..7407d6d9b9
--- /dev/null
+++ b/Libraries/LibWeb/DOM/HTMLCanvasElement.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <AK/ByteBuffer.h>
+#include <LibGfx/Forward.h>
+#include <LibWeb/DOM/HTMLElement.h>
+
+namespace Web {
+
+class LayoutDocument;
+
+class HTMLCanvasElement : public HTMLElement {
+public:
+ using WrapperType = Bindings::HTMLCanvasElementWrapper;
+
+ HTMLCanvasElement(Document&, const String& tag_name);
+ virtual ~HTMLCanvasElement() override;
+
+ int preferred_width() const;
+ int preferred_height() const;
+
+ const Gfx::Bitmap* bitmap() const { return m_bitmap; }
+ Gfx::Bitmap& ensure_bitmap();
+
+ CanvasRenderingContext2D* get_context(String type);
+
+private:
+ virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override;
+
+ RefPtr<Gfx::Bitmap> m_bitmap;
+ RefPtr<CanvasRenderingContext2D> m_context;
+};
+
+template<>
+inline bool is<HTMLCanvasElement>(const Node& node)
+{
+ return is<Element>(node) && to<Element>(node).tag_name().to_lowercase() == "canvas";
+}
+
+
+}