Serenity Operating System
at hosted 90 lines 3.0 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <LibGfx/Bitmap.h> 28#include <LibWeb/CSS/StyleResolver.h> 29#include <LibWeb/DOM/CanvasRenderingContext2D.h> 30#include <LibWeb/DOM/Document.h> 31#include <LibWeb/DOM/HTMLCanvasElement.h> 32#include <LibWeb/Layout/LayoutCanvas.h> 33 34namespace Web { 35 36HTMLCanvasElement::HTMLCanvasElement(Document& document, const FlyString& tag_name) 37 : HTMLElement(document, tag_name) 38{ 39} 40 41HTMLCanvasElement::~HTMLCanvasElement() 42{ 43} 44 45int HTMLCanvasElement::preferred_width() const 46{ 47 bool ok = false; 48 int width = attribute("width").to_int(ok); 49 if (ok) 50 return width; 51 52 return 300; 53} 54 55int HTMLCanvasElement::preferred_height() const 56{ 57 bool ok = false; 58 int height = attribute("height").to_int(ok); 59 if (ok) 60 return height; 61 62 return 150; 63} 64 65RefPtr<LayoutNode> HTMLCanvasElement::create_layout_node(const StyleProperties* parent_style) const 66{ 67 auto style = document().style_resolver().resolve_style(*this, parent_style); 68 auto display = style->string_or_fallback(CSS::PropertyID::Display, "inline"); 69 if (display == "none") 70 return nullptr; 71 return adopt(*new LayoutCanvas(*this, move(style))); 72} 73 74CanvasRenderingContext2D* HTMLCanvasElement::get_context(String type) 75{ 76 ASSERT(type.to_lowercase() == "2d"); 77 if (!m_context) 78 m_context = CanvasRenderingContext2D::create(*this); 79 return m_context; 80} 81 82Gfx::Bitmap& HTMLCanvasElement::ensure_bitmap() 83{ 84 if (!m_bitmap || m_bitmap->size() != Gfx::Size(preferred_width(), preferred_height())) { 85 m_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGBA32, { preferred_width(), preferred_height() }); 86 } 87 return *m_bitmap; 88} 89 90}