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