2020-04-06 11:09:01 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-03-19 19:07:56 +01:00
|
|
|
#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();
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:22:40 +02:00
|
|
|
void CanvasRenderingContext2D::fill_rect(float x, float y, float width, float height)
|
2020-03-19 19:07:56 +01:00
|
|
|
{
|
|
|
|
auto painter = this->painter();
|
|
|
|
if (!painter)
|
|
|
|
return;
|
|
|
|
|
2020-04-12 19:22:42 +02:00
|
|
|
auto rect = m_transform.map(Gfx::FloatRect(x, y, width, height));
|
2020-04-08 11:22:40 +02:00
|
|
|
painter->fill_rect(enclosing_int_rect(rect), m_fill_style);
|
2020-03-22 21:15:49 +01:00
|
|
|
did_draw(rect);
|
|
|
|
}
|
|
|
|
|
2020-04-07 01:09:17 -07:00
|
|
|
void CanvasRenderingContext2D::set_stroke_style(String style)
|
|
|
|
{
|
|
|
|
m_stroke_style = Gfx::Color::from_string(style).value_or(Color::Black);
|
|
|
|
}
|
|
|
|
|
|
|
|
String CanvasRenderingContext2D::stroke_style() const
|
|
|
|
{
|
|
|
|
return m_fill_style.to_string();
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:22:40 +02:00
|
|
|
void CanvasRenderingContext2D::stroke_rect(float x, float y, float width, float height)
|
2020-04-07 01:09:17 -07:00
|
|
|
{
|
|
|
|
auto painter = this->painter();
|
|
|
|
if (!painter)
|
|
|
|
return;
|
|
|
|
|
2020-04-12 19:22:42 +02:00
|
|
|
auto rect = m_transform.map(Gfx::FloatRect(x, y, width, height));
|
2020-04-08 11:22:40 +02:00
|
|
|
painter->draw_rect(enclosing_int_rect(rect), m_stroke_style);
|
2020-04-07 01:09:17 -07:00
|
|
|
did_draw(rect);
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:22:40 +02:00
|
|
|
void CanvasRenderingContext2D::scale(float sx, float sy)
|
2020-04-04 23:54:58 +02:00
|
|
|
{
|
2020-04-12 19:22:42 +02:00
|
|
|
dbg() << "CanvasRenderingContext2D::scale(): " << sx << ", " << sy;
|
|
|
|
m_transform.scale(sx, sy);
|
2020-04-04 23:54:58 +02:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:22:42 +02:00
|
|
|
void CanvasRenderingContext2D::translate(float tx, float ty)
|
2020-04-04 23:54:58 +02:00
|
|
|
{
|
2020-04-12 19:22:42 +02:00
|
|
|
dbg() << "CanvasRenderingContext2D::translate(): " << tx << ", " << ty;
|
|
|
|
m_transform.translate(tx, ty);
|
2020-04-07 01:09:17 -07:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:22:40 +02:00
|
|
|
void CanvasRenderingContext2D::did_draw(const Gfx::FloatRect&)
|
2020-03-22 21:15:49 +01:00
|
|
|
{
|
|
|
|
// FIXME: Make use of the rect to reduce the invalidated area when possible.
|
|
|
|
if (!m_element)
|
|
|
|
return;
|
|
|
|
if (!m_element->layout_node())
|
|
|
|
return;
|
|
|
|
m_element->layout_node()->set_needs_display();
|
2020-03-19 19:07:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
OwnPtr<Gfx::Painter> CanvasRenderingContext2D::painter()
|
|
|
|
{
|
|
|
|
if (!m_element)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return make<Gfx::Painter>(m_element->ensure_bitmap());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|