2020-06-07 12:55:46 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-07 12:55:46 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibWeb/CSS/Length.h>
|
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
2020-07-26 10:08:16 -03:00
|
|
|
#include <LibWeb/HTML/HTMLHtmlElement.h>
|
2021-05-30 07:36:53 -03:00
|
|
|
#include <LibWeb/Page/BrowsingContext.h>
|
2020-06-07 12:55:46 -03:00
|
|
|
|
2020-07-26 15:01:35 -03:00
|
|
|
namespace Web::CSS {
|
2020-06-07 12:55:46 -03:00
|
|
|
|
2020-11-22 11:53:01 -03:00
|
|
|
float Length::relative_length_to_px(const Layout::Node& layout_node) const
|
2020-06-07 12:55:46 -03:00
|
|
|
{
|
|
|
|
|
switch (m_type) {
|
2020-08-07 15:30:27 -03:00
|
|
|
case Type::Ex:
|
2021-01-06 07:05:23 -03:00
|
|
|
return m_value * layout_node.font().x_height();
|
2020-06-07 12:55:46 -03:00
|
|
|
case Type::Em:
|
|
|
|
|
return m_value * layout_node.font_size();
|
|
|
|
|
case Type::Rem:
|
|
|
|
|
return m_value * layout_node.document().document_element()->layout_node()->font_size();
|
2020-09-08 15:39:09 -03:00
|
|
|
case Type::Vw:
|
2021-05-30 07:36:53 -03:00
|
|
|
return layout_node.document().browsing_context()->viewport_rect().width() * (m_value / 100);
|
2020-09-08 15:39:09 -03:00
|
|
|
case Type::Vh:
|
2021-05-30 07:36:53 -03:00
|
|
|
return layout_node.document().browsing_context()->viewport_rect().height() * (m_value / 100);
|
2020-09-08 15:39:09 -03:00
|
|
|
case Type::Vmin: {
|
2021-05-30 07:36:53 -03:00
|
|
|
auto viewport = layout_node.document().browsing_context()->viewport_rect();
|
2020-09-08 15:39:09 -03:00
|
|
|
|
|
|
|
|
return min(viewport.width(), viewport.height()) * (m_value / 100);
|
|
|
|
|
}
|
|
|
|
|
case Type::Vmax: {
|
2021-05-30 07:36:53 -03:00
|
|
|
auto viewport = layout_node.document().browsing_context()->viewport_rect();
|
2020-09-08 15:39:09 -03:00
|
|
|
|
|
|
|
|
return max(viewport.width(), viewport.height()) * (m_value / 100);
|
|
|
|
|
}
|
2020-06-07 12:55:46 -03:00
|
|
|
default:
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-07 12:55:46 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* Length::unit_name() const
|
|
|
|
|
{
|
|
|
|
|
switch (m_type) {
|
2020-09-29 15:06:58 -03:00
|
|
|
case Type::Cm:
|
|
|
|
|
return "cm";
|
|
|
|
|
case Type::In:
|
|
|
|
|
return "in";
|
2020-06-07 12:55:46 -03:00
|
|
|
case Type::Px:
|
|
|
|
|
return "px";
|
2020-06-28 10:25:32 -03:00
|
|
|
case Type::Pt:
|
|
|
|
|
return "pt";
|
2020-09-29 15:06:58 -03:00
|
|
|
case Type::Mm:
|
|
|
|
|
return "mm";
|
|
|
|
|
case Type::Q:
|
|
|
|
|
return "Q";
|
2020-10-05 12:18:07 -03:00
|
|
|
case Type::Pc:
|
|
|
|
|
return "pc";
|
2020-08-07 15:30:27 -03:00
|
|
|
case Type::Ex:
|
|
|
|
|
return "ex";
|
2020-06-07 12:55:46 -03:00
|
|
|
case Type::Em:
|
|
|
|
|
return "em";
|
|
|
|
|
case Type::Rem:
|
|
|
|
|
return "rem";
|
|
|
|
|
case Type::Auto:
|
|
|
|
|
return "auto";
|
2020-06-24 10:38:21 -03:00
|
|
|
case Type::Percentage:
|
2020-06-25 10:52:54 -03:00
|
|
|
return "%";
|
2020-06-24 06:24:00 -03:00
|
|
|
case Type::Undefined:
|
|
|
|
|
return "undefined";
|
2020-09-08 15:39:09 -03:00
|
|
|
case Type::Vh:
|
|
|
|
|
return "vh";
|
|
|
|
|
case Type::Vw:
|
|
|
|
|
return "vw";
|
|
|
|
|
case Type::Vmax:
|
|
|
|
|
return "vmax";
|
|
|
|
|
case Type::Vmin:
|
|
|
|
|
return "vmin";
|
2020-06-07 12:55:46 -03:00
|
|
|
}
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-07 12:55:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|