1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:17:35 +00:00

Libraries: Move to Userland/Libraries/

This commit is contained in:
Andreas Kling 2021-01-12 12:17:30 +01:00
parent dc28c07fa5
commit 13d7c09125
1857 changed files with 266 additions and 274 deletions

View file

@ -0,0 +1,162 @@
/*
* 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.
*/
#include <LibGfx/Painter.h>
#include <LibWeb/Painting/BorderPainting.h>
#include <LibWeb/Painting/PaintContext.h>
namespace Web::Painting {
void paint_border(PaintContext& context, BorderEdge edge, const Gfx::FloatRect& rect, const CSS::ComputedValues& style)
{
const auto& border_data = [&] {
switch (edge) {
case BorderEdge::Top:
return style.border_top();
case BorderEdge::Right:
return style.border_right();
case BorderEdge::Bottom:
return style.border_bottom();
default: // BorderEdge::Left:
return style.border_left();
}
}();
float width = border_data.width;
if (width <= 0)
return;
auto color = border_data.color;
auto border_style = border_data.line_style;
int int_width = max((int)width, 1);
struct Points {
Gfx::FloatPoint p1;
Gfx::FloatPoint p2;
};
auto points_for_edge = [](BorderEdge edge, const Gfx::FloatRect& rect) -> Points {
switch (edge) {
case BorderEdge::Top:
return { rect.top_left(), rect.top_right() };
case BorderEdge::Right:
return { rect.top_right(), rect.bottom_right() };
case BorderEdge::Bottom:
return { rect.bottom_left(), rect.bottom_right() };
default: // Edge::Left
return { rect.top_left(), rect.bottom_left() };
}
};
auto [p1, p2] = points_for_edge(edge, rect);
if (border_style == CSS::LineStyle::Inset) {
auto top_left_color = Color::from_rgb(0x5a5a5a);
auto bottom_right_color = Color::from_rgb(0x888888);
color = (edge == BorderEdge::Left || edge == BorderEdge::Top) ? top_left_color : bottom_right_color;
} else if (border_style == CSS::LineStyle::Outset) {
auto top_left_color = Color::from_rgb(0x888888);
auto bottom_right_color = Color::from_rgb(0x5a5a5a);
color = (edge == BorderEdge::Left || edge == BorderEdge::Top) ? top_left_color : bottom_right_color;
}
auto gfx_line_style = Gfx::Painter::LineStyle::Solid;
if (border_style == CSS::LineStyle::Dotted)
gfx_line_style = Gfx::Painter::LineStyle::Dotted;
if (border_style == CSS::LineStyle::Dashed)
gfx_line_style = Gfx::Painter::LineStyle::Dashed;
if (gfx_line_style != Gfx::Painter::LineStyle::Solid) {
switch (edge) {
case BorderEdge::Top:
p1.move_by(int_width / 2, int_width / 2);
p2.move_by(-int_width / 2, int_width / 2);
break;
case BorderEdge::Right:
p1.move_by(-int_width / 2, int_width / 2);
p2.move_by(-int_width / 2, -int_width / 2);
break;
case BorderEdge::Bottom:
p1.move_by(int_width / 2, -int_width / 2);
p2.move_by(-int_width / 2, -int_width / 2);
break;
case BorderEdge::Left:
p1.move_by(int_width / 2, int_width / 2);
p2.move_by(int_width / 2, -int_width / 2);
break;
}
context.painter().draw_line({ (int)p1.x(), (int)p1.y() }, { (int)p2.x(), (int)p2.y() }, color, int_width, gfx_line_style);
return;
}
auto draw_line = [&](auto& p1, auto& p2) {
context.painter().draw_line({ (int)p1.x(), (int)p1.y() }, { (int)p2.x(), (int)p2.y() }, color, 1, gfx_line_style);
};
float p1_step = 0;
float p2_step = 0;
switch (edge) {
case BorderEdge::Top:
p1_step = style.border_left().width / (float)int_width;
p2_step = style.border_right().width / (float)int_width;
for (int i = 0; i < int_width; ++i) {
draw_line(p1, p2);
p1.move_by(p1_step, 1);
p2.move_by(-p2_step, 1);
}
break;
case BorderEdge::Right:
p1_step = style.border_top().width / (float)int_width;
p2_step = style.border_bottom().width / (float)int_width;
for (int i = int_width - 1; i >= 0; --i) {
draw_line(p1, p2);
p1.move_by(-1, p1_step);
p2.move_by(-1, -p2_step);
}
break;
case BorderEdge::Bottom:
p1_step = style.border_left().width / (float)int_width;
p2_step = style.border_right().width / (float)int_width;
for (int i = int_width - 1; i >= 0; --i) {
draw_line(p1, p2);
p1.move_by(p1_step, -1);
p2.move_by(-p2_step, -1);
}
break;
case BorderEdge::Left:
p1_step = style.border_top().width / (float)int_width;
p2_step = style.border_bottom().width / (float)int_width;
for (int i = 0; i < int_width; ++i) {
draw_line(p1, p2);
p1.move_by(1, p1_step);
p2.move_by(1, -p2_step);
}
break;
}
}
}

View file

@ -0,0 +1,42 @@
/*
* 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 <LibGfx/Forward.h>
#include <LibWeb/CSS/ComputedValues.h>
namespace Web::Painting {
enum class BorderEdge {
Top,
Right,
Bottom,
Left,
};
void paint_border(PaintContext&, BorderEdge, const Gfx::FloatRect&, const CSS::ComputedValues&);
}

View file

@ -0,0 +1,73 @@
/*
* 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.
*/
#pragma once
#include <LibGfx/Forward.h>
#include <LibGfx/Palette.h>
#include <LibGfx/Rect.h>
#include <LibWeb/SVG/SVGContext.h>
namespace Web {
class PaintContext {
public:
explicit PaintContext(Gfx::Painter& painter, const Palette& palette, const Gfx::IntPoint& scroll_offset)
: m_painter(painter)
, m_palette(palette)
, m_scroll_offset(scroll_offset)
{
}
Gfx::Painter& painter() const { return m_painter; }
const Palette& palette() const { return m_palette; }
bool has_svg_context() const { return m_svg_context.has_value(); }
SVGContext& svg_context() { return m_svg_context.value(); }
void set_svg_context(SVGContext context) { m_svg_context = context; }
bool should_show_line_box_borders() const { return m_should_show_line_box_borders; }
void set_should_show_line_box_borders(bool value) { m_should_show_line_box_borders = value; }
Gfx::IntRect viewport_rect() const { return m_viewport_rect; }
void set_viewport_rect(const Gfx::IntRect& rect) { m_viewport_rect = rect; }
const Gfx::IntPoint& scroll_offset() const { return m_scroll_offset; }
bool has_focus() const { return m_focus; }
void set_has_focus(bool focus) { m_focus = focus; }
private:
Gfx::Painter& m_painter;
Palette m_palette;
Optional<SVGContext> m_svg_context;
Gfx::IntRect m_viewport_rect;
Gfx::IntPoint m_scroll_offset;
bool m_should_show_line_box_borders { false };
bool m_focus { false };
};
}

View file

@ -0,0 +1,95 @@
/*
* 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.
*/
#include <AK/QuickSort.h>
#include <AK/StringBuilder.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/Layout/Box.h>
#include <LibWeb/Layout/InitialContainingBlockBox.h>
#include <LibWeb/Painting/StackingContext.h>
namespace Web::Layout {
StackingContext::StackingContext(Box& box, StackingContext* parent)
: m_box(box)
, m_parent(parent)
{
ASSERT(m_parent != this);
if (m_parent) {
m_parent->m_children.append(this);
// FIXME: Don't sort on every append..
quick_sort(m_parent->m_children, [](auto& a, auto& b) {
return a->m_box.computed_values().z_index().value_or(0) < b->m_box.computed_values().z_index().value_or(0);
});
}
}
void StackingContext::paint(PaintContext& context, PaintPhase phase)
{
if (!is<InitialContainingBlockBox>(m_box)) {
m_box.paint(context, phase);
} else {
// NOTE: InitialContainingBlockBox::paint() merely calls StackingContext::paint()
// so we call its base class instead.
downcast<InitialContainingBlockBox>(m_box).BlockBox::paint(context, phase);
}
for (auto* child : m_children) {
child->paint(context, phase);
}
}
HitTestResult StackingContext::hit_test(const Gfx::IntPoint& position, HitTestType type) const
{
HitTestResult result;
if (!is<InitialContainingBlockBox>(m_box)) {
result = m_box.hit_test(position, type);
} else {
// NOTE: InitialContainingBlockBox::hit_test() merely calls StackingContext::hit_test()
// so we call its base class instead.
result = downcast<InitialContainingBlockBox>(m_box).BlockBox::hit_test(position, type);
}
for (auto* child : m_children) {
auto result_here = child->hit_test(position, type);
if (result_here.layout_node)
result = result_here;
}
return result;
}
void StackingContext::dump(int indent) const
{
StringBuilder builder;
for (int i = 0; i < indent; ++i)
builder.append(' ');
builder.appendff("SC for {}({}) {} [children: {}]", m_box.class_name(), m_box.dom_node() ? m_box.dom_node()->node_name().characters() : "(anonymous)", m_box.absolute_rect().to_string().characters(), m_children.size());
dbgln("{}", builder.string_view());
for (auto& child : m_children)
child->dump(indent + 1);
}
}

View file

@ -0,0 +1,52 @@
/*
* 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/Vector.h>
#include <LibWeb/Layout/Node.h>
namespace Web::Layout {
class StackingContext {
public:
StackingContext(Box&, StackingContext* parent);
StackingContext* parent() { return m_parent; }
const StackingContext* parent() const { return m_parent; }
void paint(PaintContext&, PaintPhase);
HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const;
void dump(int indent = 0) const;
private:
Box& m_box;
StackingContext* const m_parent { nullptr };
Vector<StackingContext*> m_children;
};
}