2022-11-08 17:37:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Concepts.h>
|
|
|
|
#include <AK/DistinctNumeric.h>
|
|
|
|
#include <AK/Traits.h>
|
|
|
|
#include <LibGfx/Forward.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
namespace Web {
|
|
|
|
|
|
|
|
/// DevicePixels: A position or length on the physical display.
|
|
|
|
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, DevicePixels, Arithmetic, CastToUnderlying, Comparison, Increment);
|
|
|
|
|
2022-12-14 10:52:29 +00:00
|
|
|
template<Integral T>
|
2022-11-08 17:37:49 +00:00
|
|
|
constexpr bool operator==(DevicePixels left, T right) { return left.value() == right; }
|
|
|
|
|
2022-12-14 10:52:29 +00:00
|
|
|
template<Integral T>
|
2022-11-08 17:37:49 +00:00
|
|
|
constexpr bool operator!=(DevicePixels left, T right) { return left.value() != right; }
|
|
|
|
|
2022-12-14 10:52:29 +00:00
|
|
|
template<Integral T>
|
2022-11-08 17:37:49 +00:00
|
|
|
constexpr bool operator>(DevicePixels left, T right) { return left.value() > right; }
|
|
|
|
|
2022-12-14 10:52:29 +00:00
|
|
|
template<Integral T>
|
2022-11-08 17:37:49 +00:00
|
|
|
constexpr bool operator<(DevicePixels left, T right) { return left.value() < right; }
|
|
|
|
|
2022-12-14 10:52:29 +00:00
|
|
|
template<Integral T>
|
2022-11-08 17:37:49 +00:00
|
|
|
constexpr bool operator>=(DevicePixels left, T right) { return left.value() >= right; }
|
|
|
|
|
2022-12-14 10:52:29 +00:00
|
|
|
template<Integral T>
|
2022-11-08 17:37:49 +00:00
|
|
|
constexpr bool operator<=(DevicePixels left, T right) { return left.value() <= right; }
|
|
|
|
|
2022-12-14 10:52:29 +00:00
|
|
|
template<Integral T>
|
2022-11-08 17:37:49 +00:00
|
|
|
constexpr DevicePixels operator*(DevicePixels left, T right) { return left.value() * right; }
|
2022-12-14 10:52:29 +00:00
|
|
|
template<Integral T>
|
2022-11-08 17:37:49 +00:00
|
|
|
constexpr DevicePixels operator*(T left, DevicePixels right) { return right * left; }
|
|
|
|
|
2022-12-14 10:52:29 +00:00
|
|
|
template<Integral T>
|
2022-11-08 17:37:49 +00:00
|
|
|
constexpr DevicePixels operator/(DevicePixels left, T right) { return left.value() / right; }
|
|
|
|
|
2022-12-14 10:52:29 +00:00
|
|
|
template<Integral T>
|
2022-11-08 17:37:49 +00:00
|
|
|
constexpr DevicePixels operator%(DevicePixels left, T right) { return left.value() % right; }
|
|
|
|
|
|
|
|
/// CSSPixels: A position or length in CSS "reference pixels", independent of zoom or screen DPI.
|
|
|
|
/// See https://www.w3.org/TR/css-values-3/#reference-pixel
|
2023-06-12 21:37:35 +03:00
|
|
|
class CSSPixels {
|
|
|
|
public:
|
|
|
|
constexpr CSSPixels() = default;
|
|
|
|
|
|
|
|
constexpr CSSPixels(double value)
|
|
|
|
: m_value { value }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr float to_float() const
|
|
|
|
{
|
|
|
|
return static_cast<float>(m_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr double to_double() const
|
|
|
|
{
|
|
|
|
return static_cast<double>(m_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr int to_int() const
|
|
|
|
{
|
|
|
|
return static_cast<int>(m_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool operator==(CSSPixels const& other) const
|
|
|
|
{
|
|
|
|
return this->m_value == other.m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr explicit operator double() const
|
|
|
|
{
|
|
|
|
return to_double();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr CSSPixels& operator++()
|
|
|
|
{
|
|
|
|
this->m_value += 1;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
constexpr CSSPixels operator++(int)
|
|
|
|
{
|
|
|
|
CSSPixels ret = this->m_value;
|
|
|
|
this->m_value += 1;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
constexpr CSSPixels& operator--()
|
|
|
|
{
|
|
|
|
this->m_value -= 1;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
constexpr CSSPixels operator--(int)
|
|
|
|
{
|
|
|
|
CSSPixels ret = this->m_value;
|
|
|
|
this->m_value -= 1;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr int operator<=>(CSSPixels const& other) const
|
|
|
|
{
|
|
|
|
return this->m_value > other.m_value ? 1 : this->m_value < other.m_value ? -1
|
|
|
|
: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr CSSPixels operator+(CSSPixels const& other) const
|
|
|
|
{
|
|
|
|
return this->m_value + other.m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr CSSPixels operator-(CSSPixels const& other) const
|
|
|
|
{
|
|
|
|
return this->m_value - other.m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr CSSPixels operator+() const
|
|
|
|
{
|
|
|
|
return +this->m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr CSSPixels operator-() const
|
|
|
|
{
|
|
|
|
return -this->m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr CSSPixels operator*(CSSPixels const& other) const
|
|
|
|
{
|
|
|
|
return this->m_value * other.m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr CSSPixels operator/(CSSPixels const& other) const
|
|
|
|
{
|
|
|
|
return this->m_value / other.m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr CSSPixels& operator+=(CSSPixels const& other)
|
|
|
|
{
|
|
|
|
this->m_value += other.m_value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr CSSPixels& operator-=(CSSPixels const& other)
|
|
|
|
{
|
|
|
|
this->m_value -= other.m_value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr CSSPixels& operator*=(CSSPixels const& other)
|
|
|
|
{
|
|
|
|
this->m_value *= other.m_value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr CSSPixels& operator/=(CSSPixels const& other)
|
|
|
|
{
|
|
|
|
this->m_value /= other.m_value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
double m_value {};
|
|
|
|
};
|
2022-11-08 17:37:49 +00:00
|
|
|
|
|
|
|
template<Arithmetic T>
|
2023-06-12 21:37:35 +03:00
|
|
|
constexpr bool operator==(CSSPixels left, T right) { return left.to_double() == right; }
|
2022-11-08 17:37:49 +00:00
|
|
|
|
|
|
|
template<Arithmetic T>
|
2023-06-12 21:37:35 +03:00
|
|
|
constexpr bool operator!=(CSSPixels left, T right) { return left.to_double() != right; }
|
2022-11-08 17:37:49 +00:00
|
|
|
|
|
|
|
template<Arithmetic T>
|
2023-06-12 21:37:35 +03:00
|
|
|
constexpr bool operator>(CSSPixels left, T right) { return left.to_double() > right; }
|
2022-11-08 17:37:49 +00:00
|
|
|
|
|
|
|
template<Arithmetic T>
|
2023-06-12 21:37:35 +03:00
|
|
|
constexpr bool operator<(CSSPixels left, T right) { return left.to_double() < right; }
|
2022-11-08 17:37:49 +00:00
|
|
|
|
|
|
|
template<Arithmetic T>
|
2023-06-12 21:37:35 +03:00
|
|
|
constexpr bool operator>=(CSSPixels left, T right) { return left.to_double() >= right; }
|
2022-11-08 17:37:49 +00:00
|
|
|
|
|
|
|
template<Arithmetic T>
|
2023-06-12 21:37:35 +03:00
|
|
|
constexpr bool operator<=(CSSPixels left, T right) { return left.to_double() <= right; }
|
2022-11-08 17:37:49 +00:00
|
|
|
|
|
|
|
template<Arithmetic T>
|
2023-06-12 21:37:35 +03:00
|
|
|
constexpr CSSPixels operator*(CSSPixels left, T right) { return left.to_double() * right; }
|
|
|
|
|
2022-11-08 17:37:49 +00:00
|
|
|
template<Arithmetic T>
|
|
|
|
constexpr CSSPixels operator*(T left, CSSPixels right) { return right * left; }
|
|
|
|
|
|
|
|
template<Arithmetic T>
|
2023-06-12 21:37:35 +03:00
|
|
|
constexpr CSSPixels operator/(CSSPixels left, T right) { return left.to_double() / right; }
|
2022-11-08 17:37:49 +00:00
|
|
|
|
|
|
|
template<Arithmetic T>
|
2023-06-12 21:37:35 +03:00
|
|
|
constexpr CSSPixels operator%(CSSPixels left, T right) { return left.to_double() % right; }
|
2022-11-08 17:37:49 +00:00
|
|
|
|
|
|
|
using CSSPixelLine = Gfx::Line<CSSPixels>;
|
|
|
|
using CSSPixelPoint = Gfx::Point<CSSPixels>;
|
|
|
|
using CSSPixelRect = Gfx::Rect<CSSPixels>;
|
|
|
|
using CSSPixelSize = Gfx::Size<CSSPixels>;
|
|
|
|
|
|
|
|
using DevicePixelLine = Gfx::Line<DevicePixels>;
|
|
|
|
using DevicePixelPoint = Gfx::Point<DevicePixels>;
|
|
|
|
using DevicePixelRect = Gfx::Rect<DevicePixels>;
|
|
|
|
using DevicePixelSize = Gfx::Size<DevicePixels>;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Web::CSSPixels floor(Web::CSSPixels const& value)
|
|
|
|
{
|
2023-06-12 21:37:35 +03:00
|
|
|
return ::floorf(value.to_float());
|
2022-11-08 17:37:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Web::CSSPixels ceil(Web::CSSPixels const& value)
|
|
|
|
{
|
2023-06-12 21:37:35 +03:00
|
|
|
return ::ceilf(value.to_float());
|
2022-11-08 17:37:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Web::CSSPixels round(Web::CSSPixels const& value)
|
|
|
|
{
|
2023-06-12 21:37:35 +03:00
|
|
|
return ::roundf(value.to_float());
|
2022-11-08 17:37:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Web::CSSPixels fmod(Web::CSSPixels const& x, Web::CSSPixels const& y)
|
|
|
|
{
|
2023-06-12 21:37:35 +03:00
|
|
|
return ::fmodf(x.to_float(), y.to_float());
|
2022-11-08 17:37:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Web::CSSPixels abs(Web::CSSPixels const& value)
|
|
|
|
{
|
2023-06-12 21:37:35 +03:00
|
|
|
return AK::abs(value.to_float());
|
2022-11-08 17:37:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Web::DevicePixels abs(Web::DevicePixels const& value)
|
|
|
|
{
|
|
|
|
return AK::abs(value.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Traits<Web::CSSPixels> : public GenericTraits<Web::CSSPixels> {
|
|
|
|
static unsigned hash(Web::CSSPixels const& key)
|
|
|
|
{
|
2023-06-12 21:37:35 +03:00
|
|
|
VERIFY(isfinite(key.to_double()));
|
|
|
|
return Traits<double>::hash(key.to_double());
|
2022-11-08 17:37:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool equals(Web::CSSPixels const& a, Web::CSSPixels const& b)
|
|
|
|
{
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Traits<Web::DevicePixels> : public GenericTraits<Web::DevicePixels> {
|
|
|
|
static unsigned hash(Web::DevicePixels const& key)
|
|
|
|
{
|
2023-01-20 15:23:17 -05:00
|
|
|
return Traits<Web::DevicePixels::Type>::hash(key.value());
|
2022-11-08 17:37:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool equals(Web::DevicePixels const& a, Web::DevicePixels const& b)
|
|
|
|
{
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2023-06-12 21:37:35 +03:00
|
|
|
struct Formatter<Web::CSSPixels> : Formatter<double> {
|
2022-11-08 17:37:49 +00:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, Web::CSSPixels const& value)
|
|
|
|
{
|
2023-06-12 21:37:35 +03:00
|
|
|
return Formatter<double>::format(builder, value.to_double());
|
2022-11-08 17:37:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2023-01-20 15:23:17 -05:00
|
|
|
struct Formatter<Web::DevicePixels> : Formatter<Web::DevicePixels::Type> {
|
2022-11-08 17:37:49 +00:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, Web::DevicePixels const& value)
|
|
|
|
{
|
2023-01-20 15:23:17 -05:00
|
|
|
return Formatter<Web::DevicePixels::Type>::format(builder, value.value());
|
2022-11-08 17:37:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|