Serenity Operating System
at hosted 129 lines 4.1 kB view raw
1/* 2 * Copyright (c) 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 <AK/OwnPtr.h> 28#include <LibGfx/Painter.h> 29#include <LibWeb/DOM/CanvasRenderingContext2D.h> 30#include <LibWeb/DOM/HTMLCanvasElement.h> 31 32namespace Web { 33 34CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement& element) 35 : m_element(element.make_weak_ptr()) 36{ 37} 38 39CanvasRenderingContext2D::~CanvasRenderingContext2D() 40{ 41} 42 43void CanvasRenderingContext2D::set_fill_style(String style) 44{ 45 m_fill_style = Gfx::Color::from_string(style).value_or(Color::Black); 46} 47 48String CanvasRenderingContext2D::fill_style() const 49{ 50 return m_fill_style.to_string(); 51} 52 53void CanvasRenderingContext2D::fill_rect(float x, float y, float width, float height) 54{ 55 auto painter = this->painter(); 56 if (!painter) 57 return; 58 59 Gfx::FloatRect rect = compute_rect(x, y, width, height); 60 painter->fill_rect(enclosing_int_rect(rect), m_fill_style); 61 did_draw(rect); 62} 63 64void CanvasRenderingContext2D::set_stroke_style(String style) 65{ 66 m_stroke_style = Gfx::Color::from_string(style).value_or(Color::Black); 67} 68 69String CanvasRenderingContext2D::stroke_style() const 70{ 71 return m_fill_style.to_string(); 72} 73 74void CanvasRenderingContext2D::stroke_rect(float x, float y, float width, float height) 75{ 76 auto painter = this->painter(); 77 if (!painter) 78 return; 79 80 Gfx::FloatRect rect = compute_rect(x, y, width, height); 81 painter->draw_rect(enclosing_int_rect(rect), m_stroke_style); 82 did_draw(rect); 83} 84 85void CanvasRenderingContext2D::scale(float sx, float sy) 86{ 87 // FIXME: Actually do something with the scale factor! 88 dbg() << "CanvasRenderingContext2D::scale(): " << String::format("%f", sx) << ", " << String::format("%f", sy); 89 m_scale_x = sx; 90 m_scale_y = sy; 91} 92 93void CanvasRenderingContext2D::translate(float x, float y) 94{ 95 // FIXME: Actually do something with the translation! 96 dbg() << "CanvasRenderingContext2D::translate(): " << String::format("%f", x) << ", " << String::format("%f", y); 97 m_translate_x = x; 98 m_translate_y = y; 99} 100 101Gfx::FloatRect CanvasRenderingContext2D::compute_rect(float x, float y, float width, float height) 102{ 103 return { 104 (x + m_translate_x) * m_scale_x, 105 (y + m_translate_y) * m_scale_y, 106 width * m_scale_x, 107 height * m_scale_y 108 }; 109} 110 111void CanvasRenderingContext2D::did_draw(const Gfx::FloatRect&) 112{ 113 // FIXME: Make use of the rect to reduce the invalidated area when possible. 114 if (!m_element) 115 return; 116 if (!m_element->layout_node()) 117 return; 118 m_element->layout_node()->set_needs_display(); 119} 120 121OwnPtr<Gfx::Painter> CanvasRenderingContext2D::painter() 122{ 123 if (!m_element) 124 return nullptr; 125 126 return make<Gfx::Painter>(m_element->ensure_bitmap()); 127} 128 129}