mirror of
https://github.com/RGBCube/serenity
synced 2025-05-25 18:35:09 +00:00
LibWeb: Add cm, in, mm and Q CSS units
This commit is contained in:
parent
279a49cdf4
commit
a9335eea1c
3 changed files with 45 additions and 3 deletions
|
@ -62,10 +62,18 @@ float Length::relative_length_to_px(const LayoutNode& layout_node) const
|
||||||
const char* Length::unit_name() const
|
const char* Length::unit_name() const
|
||||||
{
|
{
|
||||||
switch (m_type) {
|
switch (m_type) {
|
||||||
|
case Type::Cm:
|
||||||
|
return "cm";
|
||||||
|
case Type::In:
|
||||||
|
return "in";
|
||||||
case Type::Px:
|
case Type::Px:
|
||||||
return "px";
|
return "px";
|
||||||
case Type::Pt:
|
case Type::Pt:
|
||||||
return "pt";
|
return "pt";
|
||||||
|
case Type::Mm:
|
||||||
|
return "mm";
|
||||||
|
case Type::Q:
|
||||||
|
return "Q";
|
||||||
case Type::Ex:
|
case Type::Ex:
|
||||||
return "ex";
|
return "ex";
|
||||||
case Type::Em:
|
case Type::Em:
|
||||||
|
|
|
@ -37,6 +37,10 @@ public:
|
||||||
Undefined,
|
Undefined,
|
||||||
Percentage,
|
Percentage,
|
||||||
Auto,
|
Auto,
|
||||||
|
Cm,
|
||||||
|
In,
|
||||||
|
Mm,
|
||||||
|
Q,
|
||||||
Px,
|
Px,
|
||||||
Pt,
|
Pt,
|
||||||
Ex,
|
Ex,
|
||||||
|
@ -88,7 +92,17 @@ public:
|
||||||
bool is_undefined() const { return m_type == Type::Undefined; }
|
bool is_undefined() const { return m_type == Type::Undefined; }
|
||||||
bool is_percentage() const { return m_type == Type::Percentage; }
|
bool is_percentage() const { return m_type == Type::Percentage; }
|
||||||
bool is_auto() const { return m_type == Type::Auto; }
|
bool is_auto() const { return m_type == Type::Auto; }
|
||||||
bool is_absolute() const { return m_type == Type::Px || m_type == Type::Pt; }
|
|
||||||
|
bool is_absolute() const
|
||||||
|
{
|
||||||
|
return m_type == Type::Cm
|
||||||
|
|| m_type == Type::In
|
||||||
|
|| m_type == Type::Mm
|
||||||
|
|| m_type == Type::Px
|
||||||
|
|| m_type == Type::Pt
|
||||||
|
|| m_type == Type::Q;
|
||||||
|
}
|
||||||
|
|
||||||
bool is_relative() const
|
bool is_relative() const
|
||||||
{
|
{
|
||||||
return m_type == Type::Ex
|
return m_type == Type::Ex
|
||||||
|
@ -108,10 +122,18 @@ public:
|
||||||
switch (m_type) {
|
switch (m_type) {
|
||||||
case Type::Auto:
|
case Type::Auto:
|
||||||
return 0;
|
return 0;
|
||||||
|
case Type::Cm:
|
||||||
|
return m_value * (96.0f / 2.54f); // 1cm = 96px/2.54
|
||||||
|
case Type::In:
|
||||||
|
return m_value * 96.0f; // 1in = 2.54 cm = 96px
|
||||||
case Type::Px:
|
case Type::Px:
|
||||||
return m_value;
|
return m_value; // 1px = 1/96th of 1in
|
||||||
case Type::Pt:
|
case Type::Pt:
|
||||||
return m_value * 1.33333333f;
|
return m_value * 1.33333333f; // 1pt = 1/72th of 1in
|
||||||
|
case Type::Mm:
|
||||||
|
return m_value * (0.1f * (96.0f / 2.54f)); // 1mm = 1/10th of 1cm
|
||||||
|
case Type::Q:
|
||||||
|
return m_value * (0.025f * (96.0f / 2.54f)); // 1Q = 1/40th of 1cm
|
||||||
case Type::Undefined:
|
case Type::Undefined:
|
||||||
case Type::Percentage:
|
case Type::Percentage:
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -284,6 +284,9 @@ static CSS::Length parse_length(const CSS::ParsingContext& context, const String
|
||||||
} else if (view.ends_with("pt", CaseSensitivity::CaseInsensitive)) {
|
} else if (view.ends_with("pt", CaseSensitivity::CaseInsensitive)) {
|
||||||
type = CSS::Length::Type::Pt;
|
type = CSS::Length::Type::Pt;
|
||||||
value = try_parse_float(view.substring_view(0, view.length() - 2));
|
value = try_parse_float(view.substring_view(0, view.length() - 2));
|
||||||
|
} else if (view.ends_with("mm", CaseSensitivity::CaseInsensitive)) {
|
||||||
|
type = CSS::Length::Type::Mm;
|
||||||
|
value = try_parse_float(view.substring_view(0, view.length() - 2));
|
||||||
} else if (view.ends_with("rem", CaseSensitivity::CaseInsensitive)) {
|
} else if (view.ends_with("rem", CaseSensitivity::CaseInsensitive)) {
|
||||||
type = CSS::Length::Type::Rem;
|
type = CSS::Length::Type::Rem;
|
||||||
value = try_parse_float(view.substring_view(0, view.length() - 3));
|
value = try_parse_float(view.substring_view(0, view.length() - 3));
|
||||||
|
@ -305,6 +308,15 @@ static CSS::Length parse_length(const CSS::ParsingContext& context, const String
|
||||||
} else if (view.ends_with("vmin", CaseSensitivity::CaseInsensitive)) {
|
} else if (view.ends_with("vmin", CaseSensitivity::CaseInsensitive)) {
|
||||||
type = CSS::Length::Type::Vmin;
|
type = CSS::Length::Type::Vmin;
|
||||||
value = try_parse_float(view.substring_view(0, view.length() - 4));
|
value = try_parse_float(view.substring_view(0, view.length() - 4));
|
||||||
|
} else if (view.ends_with("cm", CaseSensitivity::CaseInsensitive)) {
|
||||||
|
type = CSS::Length::Type::Cm;
|
||||||
|
value = try_parse_float(view.substring_view(0, view.length() - 2));
|
||||||
|
} else if (view.ends_with("in", CaseSensitivity::CaseInsensitive)) {
|
||||||
|
type = CSS::Length::Type::In;
|
||||||
|
value = try_parse_float(view.substring_view(0, view.length() - 2));
|
||||||
|
} else if (view.ends_with("Q", CaseSensitivity::CaseInsensitive)) {
|
||||||
|
type = CSS::Length::Type::Q;
|
||||||
|
value = try_parse_float(view.substring_view(0, view.length() - 1));
|
||||||
} else if (view == "0") {
|
} else if (view == "0") {
|
||||||
type = CSS::Length::Type::Px;
|
type = CSS::Length::Type::Px;
|
||||||
value = 0;
|
value = 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue