Serenity Operating System
at master 68 lines 2.3 kB view raw
1/* 2 * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org> 3 * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org> 4 * Copyright (c) 2023, MacDue <macdue@dueutil.tech> 5 * 6 * SPDX-License-Identifier: BSD-2-Clause 7 */ 8 9#include <LibWeb/HTML/Canvas/CanvasState.h> 10 11namespace Web::HTML { 12 13// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-save 14void CanvasState::save() 15{ 16 // The save() method steps are to push a copy of the current drawing state onto the drawing state stack. 17 m_drawing_state_stack.append(m_drawing_state); 18} 19 20// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-restore 21void CanvasState::restore() 22{ 23 // The restore() method steps are to pop the top entry in the drawing state stack, and reset the drawing state it describes. If there is no saved state, then the method must do nothing. 24 if (m_drawing_state_stack.is_empty()) 25 return; 26 m_drawing_state = m_drawing_state_stack.take_last(); 27} 28 29// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-reset 30void CanvasState::reset() 31{ 32 // The reset() method steps are to reset the rendering context to its default state. 33 reset_to_default_state(); 34} 35 36// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-iscontextlost 37bool CanvasState::is_context_lost() 38{ 39 // The isContextLost() method steps are to return this's context lost. 40 return m_context_lost; 41} 42 43NonnullRefPtr<Gfx::PaintStyle> CanvasState::FillOrStrokeStyle::to_gfx_paint_style() 44{ 45 return m_fill_or_stroke_style.visit( 46 [&](Gfx::Color color) -> NonnullRefPtr<Gfx::PaintStyle> { 47 if (!m_color_paint_style) 48 m_color_paint_style = Gfx::SolidColorPaintStyle::create(color).release_value_but_fixme_should_propagate_errors(); 49 return m_color_paint_style.release_nonnull(); 50 }, 51 [&](auto handle) { 52 return handle->to_gfx_paint_style(); 53 }); 54} 55 56Gfx::Color CanvasState::FillOrStrokeStyle::to_color_but_fixme_should_accept_any_paint_style() const 57{ 58 return as_color().value_or(Gfx::Color::Black); 59} 60 61Optional<Gfx::Color> CanvasState::FillOrStrokeStyle::as_color() const 62{ 63 if (auto* color = m_fill_or_stroke_style.get_pointer<Gfx::Color>()) 64 return *color; 65 return {}; 66} 67 68}