Skip to content

Commit

Permalink
karm-math: Replaced toFloat, toInt, and toUint by cast<U>().
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Aug 1, 2024
1 parent b63c3ad commit 004584c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 66 deletions.
36 changes: 10 additions & 26 deletions src/libs/karm-math/fixed.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,34 +82,18 @@ struct _Fixed {
return _val;
}

template <Meta::SignedIntegral I>
constexpr I toInt() const {
return static_cast<I>(_val >> FRAC);
}

template <Meta::UnsignedIntegral U>
constexpr U toUint() const {
return static_cast<U>(_val >> FRAC);
}

template <Meta::Float F>
constexpr F toFloat() const {
return static_cast<F>(_val) / DENO;
template <typename U>
constexpr U cast() const {
if constexpr (Meta::Integral<U>)
return static_cast<U>(_val >> FRAC);
else
return static_cast<U>(_val) / DENO;
}

template <Meta::SignedIntegral I>
explicit constexpr operator I() const {
return toInt<I>();
}

template <Meta::UnsignedIntegral U>
template <typename U>
requires Meta::Float<U> or Meta::Integral<U>
explicit constexpr operator U() const {
return toUint<U>();
}

template <Meta::Float F>
explicit constexpr operator F() const {
return toFloat<F>();
return cast<U>();
}

constexpr _Fixed &operator++() {
Expand Down Expand Up @@ -242,7 +226,7 @@ struct Karm::Limits<Math::_Fixed<T, F>> {
template <Meta::SignedIntegral T, usize F>
struct Karm::Io::Formatter<Math::_Fixed<T, F>> {
Res<usize> format(Io::TextWriter &writer, Math::_Fixed<T, F> const &val) {
return Io::format(writer, "{}", val.template toFloat<f64>());
return Io::format(writer, "{}", val.template cast<f64>());
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/web/vaev-base/length.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct Length {
: _val(val), _unit(unit) {}

constexpr Length(Px val)
: _val(val) {}
: _val(val.cast<f64>()) {}

constexpr bool isAbsolute() const {
switch (_unit) {
Expand Down
74 changes: 37 additions & 37 deletions src/web/vaev-layout/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,40 +58,40 @@ Px Context::resolve(Length l) {
// Font-relative

case Length::Unit::EM:
return Px::fromFloatNearest(l.val() * fontSize().toFloat<f64>());
return Px::fromFloatNearest(l.val() * fontSize().cast<f64>());

case Length::Unit::REM:
return Px::fromFloatNearest(l.val() * fontSize().toFloat<f64>());
return Px::fromFloatNearest(l.val() * fontSize().cast<f64>());

case Length::Unit::EX:
return Px::fromFloatNearest(l.val() * xHeight().toFloat<f64>());
return Px::fromFloatNearest(l.val() * xHeight().cast<f64>());

case Length::Unit::REX:
return Px::fromFloatNearest(l.val() * xHeight().toFloat<f64>());
return Px::fromFloatNearest(l.val() * xHeight().cast<f64>());

case Length::Unit::CAP:
return Px::fromFloatNearest(l.val() * capHeight().toFloat<f64>());
return Px::fromFloatNearest(l.val() * capHeight().cast<f64>());

case Length::Unit::RCAP:
return Px::fromFloatNearest(l.val() * capHeight().toFloat<f64>());
return Px::fromFloatNearest(l.val() * capHeight().cast<f64>());

case Length::Unit::CH:
return Px::fromFloatNearest(l.val() * zeroAdvance().toFloat<f64>());
return Px::fromFloatNearest(l.val() * zeroAdvance().cast<f64>());

case Length::Unit::RCH:
return Px::fromFloatNearest(l.val() * zeroAdvance().toFloat<f64>());
return Px::fromFloatNearest(l.val() * zeroAdvance().cast<f64>());

case Length::Unit::IC:
return Px::fromFloatNearest(l.val() * zeroAdvance().toFloat<f64>());
return Px::fromFloatNearest(l.val() * zeroAdvance().cast<f64>());

case Length::Unit::RIC:
return Px::fromFloatNearest(l.val() * zeroAdvance().toFloat<f64>());
return Px::fromFloatNearest(l.val() * zeroAdvance().cast<f64>());

case Length::Unit::LH:
return Px::fromFloatNearest(l.val() * lineHeight().toFloat<f64>());
return Px::fromFloatNearest(l.val() * lineHeight().cast<f64>());

case Length::Unit::RLH:
return Px::fromFloatNearest(l.val() * lineHeight().toFloat<f64>());
return Px::fromFloatNearest(l.val() * lineHeight().cast<f64>());

// Viewport-relative

Expand All @@ -100,72 +100,72 @@ Px Context::resolve(Length l) {
// Equal to 1% of the width of current viewport.
case Length::Unit::VW:
case Length::Unit::LVW:
return Px::fromFloatNearest(l.val() * viewport.large.width.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.large.width.cast<f64>() / 100);

case Length::Unit::SVW:
return Px::fromFloatNearest(l.val() * viewport.small.width.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.small.width.cast<f64>() / 100);

case Length::Unit::DVW:
return Px::fromFloatNearest(l.val() * viewport.dynamic.width.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.dynamic.width.cast<f64>() / 100);

// https://drafts.csswg.org/css-values/#vh
// Equal to 1% of the height of current viewport.
case Length::Unit::VH:
case Length::Unit::LVH:
return Px::fromFloatNearest(l.val() * viewport.large.height.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.large.height.cast<f64>() / 100);

case Length::Unit::SVH:
return Px::fromFloatNearest(l.val() * viewport.small.height.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.small.height.cast<f64>() / 100);

case Length::Unit::DVH:
return Px::fromFloatNearest(l.val() * viewport.dynamic.height.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.dynamic.height.cast<f64>() / 100);

// https://drafts.csswg.org/css-values/#vi
// Equal to 1% of the size of the viewport in the box’s inline axis.
case Length::Unit::VI:
case Length::Unit::LVI:
if (mainAxis() == Axis::HORIZONTAL) {
return Px::fromFloatNearest(l.val() * viewport.large.width.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.large.width.cast<f64>() / 100);
} else {
return Px::fromFloatNearest(l.val() * viewport.large.height.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.large.height.cast<f64>() / 100);
}

case Length::Unit::SVI:
if (mainAxis() == Axis::HORIZONTAL) {
return Px::fromFloatNearest(l.val() * viewport.small.width.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.small.width.cast<f64>() / 100);
} else {
return Px::fromFloatNearest(l.val() * viewport.small.height.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.small.height.cast<f64>() / 100);
}

case Length::Unit::DVI:
if (mainAxis() == Axis::HORIZONTAL) {
return Px::fromFloatNearest(l.val() * viewport.dynamic.width.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.dynamic.width.cast<f64>() / 100);
} else {
return Px::fromFloatNearest(l.val() * viewport.dynamic.height.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.dynamic.height.cast<f64>() / 100);
}

// https://drafts.csswg.org/css-values/#vb
// Equal to 1% of the size of the viewport in the box’s block axis.
case Length::Unit::VB:
case Length::Unit::LVB:
if (crossAxis() == Axis::HORIZONTAL) {
return Px::fromFloatNearest(l.val() * viewport.large.width.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.large.width.cast<f64>() / 100);
} else {
return Px::fromFloatNearest(l.val() * viewport.large.height.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.large.height.cast<f64>() / 100);
}

case Length::Unit::SVB:
if (crossAxis() == Axis::HORIZONTAL) {
return Px::fromFloatNearest(l.val() * viewport.small.width.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.small.width.cast<f64>() / 100);
} else {
return Px::fromFloatNearest(l.val() * viewport.small.height.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.small.height.cast<f64>() / 100);
}

case Length::Unit::DVB:
if (crossAxis() == Axis::HORIZONTAL) {
return Px::fromFloatNearest(l.val() * viewport.dynamic.width.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.dynamic.width.cast<f64>() / 100);
} else {
return Px::fromFloatNearest(l.val() * viewport.dynamic.height.toFloat<f64>() / 100);
return Px::fromFloatNearest(l.val() * viewport.dynamic.height.cast<f64>() / 100);
}

// https://drafts.csswg.org/css-values/#vmin
Expand Down Expand Up @@ -213,22 +213,22 @@ Px Context::resolve(Length l) {
// Absolute
// https://drafts.csswg.org/css-values/#absolute-lengths
case Length::Unit::CM:
return Px::fromFloatNearest(l.val() * viewport.dpi.toFloat<f64>() / 2.54);
return Px::fromFloatNearest(l.val() * viewport.dpi.cast<f64>() / 2.54);

case Length::Unit::MM:
return Px::fromFloatNearest(l.val() * viewport.dpi.toFloat<f64>() / 25.4);
return Px::fromFloatNearest(l.val() * viewport.dpi.cast<f64>() / 25.4);

case Length::Unit::Q:
return Px::fromFloatNearest(l.val() * viewport.dpi.toFloat<f64>() / 101.6);
return Px::fromFloatNearest(l.val() * viewport.dpi.cast<f64>() / 101.6);

case Length::Unit::IN:
return Px::fromFloatNearest(l.val() * viewport.dpi.toFloat<f64>());
return Px::fromFloatNearest(l.val() * viewport.dpi.cast<f64>());

case Length::Unit::PT:
return Px::fromFloatNearest(l.val() * viewport.dpi.toFloat<f64>() / 72.0);
return Px::fromFloatNearest(l.val() * viewport.dpi.cast<f64>() / 72.0);

case Length::Unit::PC:
return Px::fromFloatNearest(l.val() * viewport.dpi.toFloat<f64>() / 6.0);
return Px::fromFloatNearest(l.val() * viewport.dpi.cast<f64>() / 6.0);

case Length::Unit::PX:
return Px::fromFloatNearest(l.val());
Expand All @@ -241,7 +241,7 @@ Px Context::resolve(Length l) {
Px Context::resolve(PercentOr<Length> p, Px relative) {
if (p.resolved())
return resolve(p.value());
return Px{relative.toFloat<f64>() * (p.percent().value() / 100.)};
return Px{relative.cast<f64>() * (p.percent().value() / 100.)};
}

Px Context::resolve(Width w, Px relative) {
Expand Down
4 changes: 2 additions & 2 deletions src/web/vaev-view/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ struct View : public Ui::View<View> {
auto [layout, _] = Driver::render(*_dom, media, size.cast<Px>());

return {
layout->_box.borderBox.width.toInt<isize>(),
layout->_box.borderBox.height.toInt<isize>(),
layout->_box.borderBox.width.cast<isize>(),
layout->_box.borderBox.height.cast<isize>(),
};
}
};
Expand Down

0 comments on commit 004584c

Please sign in to comment.