Skip to content

Commit

Permalink
vaev-base: Added an user defined literal for pixel values.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Nov 7, 2024
1 parent 31e0ac1 commit 32a7de1
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 79 deletions.
6 changes: 3 additions & 3 deletions src/vaev-base/borders.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ struct Border {
};

struct BorderProps {
static constexpr Length THIN = Px{1};
static constexpr Length MEDIUM = Px{3};
static constexpr Length THICK = Px{5};
static constexpr Length THIN = 1_px;
static constexpr Length MEDIUM = 3_px;
static constexpr Length THICK = 5_px;

Border top, start, bottom, end;
Math::Radii<PercentOr<Length>> radii;
Expand Down
4 changes: 4 additions & 0 deletions src/vaev-base/length.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ using PhysicalPixel = Distinct<i32, struct _PhysicalPixel>;
/// Represents a logical pixel in the CSS coordinate system.
using Px = Math::i24f8;

constexpr Px operator""_px(u64 val) {
return Px(val);
}

using RectPx = Math::Rect<Px>;

using Vec2Px = Math::Vec2<Px>;
Expand Down
2 changes: 1 addition & 1 deletion src/vaev-base/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct BorderSpacing {
struct TableProps {
TableLayout tableLayout = TableLayout::AUTO;
CaptionSide captionSide = CaptionSide::TOP;
BorderSpacing spacing = {Px{0}, Px{0}};
BorderSpacing spacing = {0_px, 0_px};
BorderCollapse collapse = BorderCollapse::SEPARATE;
};

Expand Down
4 changes: 2 additions & 2 deletions src/vaev-driver/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ RenderResult render(Markup::Document const &dom, Style::Media const &media, Vec2
{
.commit = Layout::Commit::YES,
.knownSize = {vp.small.width, NONE},
.availableSpace = {vp.small.width, Px{0}},
.availableSpace = {vp.small.width, 0_px},
.containingBlock = {vp.small.width, vp.small.height},
}
);
Expand Down Expand Up @@ -151,7 +151,7 @@ RenderResult render(Markup::Document &dom, Style::Media const &media, Print::Pap
{
.commit = Layout::Commit::YES,
.knownSize = {vp.small.width, NONE},
.availableSpace = {vp.small.width, Px{0}},
.availableSpace = {vp.small.width, 0_px},
.containingBlock = {vp.small.width, vp.small.height},
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/vaev-layout/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enum struct IntrinsicSize {
};

struct Viewport {
Px dpi = Px{96};
Px dpi = 96_px;
// https://drafts.csswg.org/css-values/#small-viewport-size
RectPx small;
// https://drafts.csswg.org/css-values/#large-viewport-size
Expand Down
8 changes: 4 additions & 4 deletions src/vaev-layout/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Vaev::Layout {
// https://www.w3.org/TR/CSS22/visuren.html#normal-flow
struct BlockFormatingContext {
Output run(Tree &tree, Box &box, Input input) {
Px blockSize = Px{0};
Px inlineSize = input.knownSize.width.unwrapOr(Px{0});
Px blockSize = 0_px;
Px inlineSize = input.knownSize.width.unwrapOr(0_px);

// NOTE: Our parent has no clue about our width but wants us to commit,
// we need to compute it first
Expand All @@ -24,8 +24,8 @@ struct BlockFormatingContext {
Input childInput = {
.commit = input.commit,
.intrinsic = input.intrinsic,
.availableSpace = {inlineSize, Px{0}},
.containingBlock = {inlineSize, input.knownSize.y.unwrapOr(Px{0})},
.availableSpace = {inlineSize, 0_px},
.containingBlock = {inlineSize, input.knownSize.y.unwrapOr(0_px)},
};

auto margin = computeMargins(tree, c, childInput);
Expand Down
54 changes: 27 additions & 27 deletions src/vaev-layout/flex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ struct FlexItem {
) {
// TODO: placehold value, check specs
Px aspectRatio{1};
auto crossSize = resolve(tree, *box, box->style->sizing->height.value, Px{0});
auto crossSize = resolve(tree, *box, box->style->sizing->height.value, 0_px);
flexBaseSize = (crossSize)*aspectRatio;
}

Expand Down Expand Up @@ -425,7 +425,7 @@ struct FlexItem {
fa.crossAxis(usedSize) -
getMargin(BOTH_CROSS)
) /
Px{2};
2_px;
fa.crossAxis(position) = startOfBlock + getMargin(START_CROSS);
}
}
Expand Down Expand Up @@ -516,7 +516,7 @@ struct FlexLine {
void alignMainSpaceAround(Px mainSize, Px occupiedSize) {
Px gapSize = (mainSize - occupiedSize) / Px{items.len()};

Px currPositionMainAxis{gapSize / Px{2}};
Px currPositionMainAxis{gapSize / 2_px};
for (auto &flexItem : items) {
fa.mainAxis(flexItem.position) =
currPositionMainAxis + flexItem.getMargin(FlexItem::START_MAIN);
Expand All @@ -538,7 +538,7 @@ struct FlexLine {
}

void alignMainCenter(Px mainSize, Px occupiedSize) {
Px currPositionMainAxis{(mainSize - occupiedSize) / Px{2}};
Px currPositionMainAxis{(mainSize - occupiedSize) / 2_px};
for (auto &flexItem : items) {
fa.mainAxis(flexItem.position) =
currPositionMainAxis + flexItem.getMargin(FlexItem::START_MAIN);
Expand Down Expand Up @@ -672,11 +672,11 @@ struct FlexFormatingContext {
flexItem.flexBaseSize -
flexItem.getMargin(FlexItem::BOTH_MAIN);

if (itemFlexFraction > Px{0})
itemFlexFraction = itemFlexFraction / max(Px{1}, Px{flexItem.flexItemProps.grow});
if (itemFlexFraction > 0_px)
itemFlexFraction = itemFlexFraction / max(1_px, Px{flexItem.flexItemProps.grow});

else if (itemFlexFraction < Px{0})
itemFlexFraction = itemFlexFraction / max(Px{1}, Px{flexItem.flexItemProps.shrink});
else if (itemFlexFraction < 0_px)
itemFlexFraction = itemFlexFraction / max(1_px, Px{flexItem.flexItemProps.shrink});

flexFraction.pushBack(itemFlexFraction);
}
Expand All @@ -700,7 +700,7 @@ struct FlexFormatingContext {
for (auto &flexItem : _items) {
Px product{flexItem.flexBaseSize};

if (largestFraction < Px{0}) {
if (largestFraction < 0_px) {
product += largestFraction / flexItem.getScaledFlexShrinkFactor();
} else {
product += largestFraction * Px{flexItem.flexItemProps.grow};
Expand All @@ -727,7 +727,7 @@ struct FlexFormatingContext {

// https://github.com/w3c/csswg-drafts/issues/8884
void _computeIntrinsicSizeRowNoWrap(Tree &t) {
_usedMainSize = Px{0};
_usedMainSize = 0_px;
for (auto &flexItem : _items) {
auto minContrib = flexItem.getMainSizeMinMaxContentContribution(
t,
Expand Down Expand Up @@ -766,8 +766,8 @@ struct FlexFormatingContext {
void _determineMainSize(Tree &t, Input input, Box &box) {
_usedMainSize =
_flex.isRowOriented()
? input.knownSize.x.unwrapOr(Px{0})
: input.knownSize.y.unwrapOr(Px{0});
? input.knownSize.x.unwrapOr(0_px)
: input.knownSize.y.unwrapOr(0_px);

if (input.intrinsic == IntrinsicSize::MAX_CONTENT) {
if (fa.isRowOriented) {
Expand Down Expand Up @@ -819,14 +819,14 @@ struct FlexFormatingContext {
while (si < _items.len()) {
usize ei = si;

Px currLineSize = Px{0};
Px currLineSize = 0_px;
while (ei < _items.len()) {

Px itemContribution = _items[ei].hypoMainSize;

itemContribution += _items[ei].getMargin(FlexItem::BOTH_MAIN);
// TODO: ignoring breaks for now
if (currLineSize + itemContribution <= _usedMainSize + Px{0.01} or currLineSize == Px{0}) {
if (currLineSize + itemContribution <= _usedMainSize + Px{0.01} or currLineSize == 0_px) {
currLineSize += itemContribution;
ei++;
} else
Expand Down Expand Up @@ -957,7 +957,7 @@ struct FlexFormatingContext {
);

// TODO: should consider padding and border so content size is not negative
auto minSizeFlooringContentSizeAt0 = Px{0};
auto minSizeFlooringContentSizeAt0 = 0_px;

return max(clampedSize, minSizeFlooringContentSizeAt0);
};
Expand All @@ -973,7 +973,7 @@ struct FlexFormatingContext {
totalViolation += clampedSize - fa.mainAxis(flexItem->usedSize);
}

if (totalViolation == Px{0}) {
if (totalViolation == 0_px) {
for (usize i = 0; i < unfrozenItems.len(); ++i) {
auto *flexItem = unfrozenItems[i];
Px clampedSize = clampAndFloorContentBox(flexItem);
Expand All @@ -985,7 +985,7 @@ struct FlexFormatingContext {
unfrozenItems.clear();
} else {
Vec<usize> indexesToFreeze;
if (totalViolation < Px{0}) {
if (totalViolation < 0_px) {
for (usize i = 0; i < unfrozenItems.len(); ++i) {
auto *flexItem = unfrozenItems[i];
Px clampedSize = clampAndFloorContentBox(flexItem);
Expand Down Expand Up @@ -1104,8 +1104,8 @@ struct FlexFormatingContext {
outer cross-end edge, and sum these two values.
TODO
*/
maxDistBaselineCrossStartEdge = max(maxDistBaselineCrossStartEdge, Px{0});
maxDistBaselineCrossEndEdge = max(maxDistBaselineCrossEndEdge, Px{0});
maxDistBaselineCrossStartEdge = max(maxDistBaselineCrossStartEdge, 0_px);
maxDistBaselineCrossEndEdge = max(maxDistBaselineCrossEndEdge, 0_px);
} else {
maxOuterHypCrossSize = max(
maxOuterHypCrossSize,
Expand Down Expand Up @@ -1248,9 +1248,9 @@ struct FlexFormatingContext {
if (not usedAutoMargins) {
for (auto &flexItem : flexLine.items) {
if (fa.startMainAxis(*flexItem.box->style->margin) == Width::AUTO)
fa.startMainAxis(flexItem.margin) = Px{0};
fa.startMainAxis(flexItem.margin) = 0_px;
if (fa.endMainAxis(*flexItem.box->style->margin) == Width::AUTO)
fa.endMainAxis(flexItem.margin) = Px{0};
fa.endMainAxis(flexItem.margin) = 0_px;
}
}

Expand Down Expand Up @@ -1299,12 +1299,12 @@ struct FlexFormatingContext {
} else {
Px freeCrossSpace = l.crossSize - fa.crossAxis(i.usedSize);
fa.endCrossAxis(i.margin) =
fa.startCrossAxis(i.margin) = freeCrossSpace / Px{2};
fa.startCrossAxis(i.margin) = freeCrossSpace / 2_px;
}
fa.crossAxis(i.position) = i.getMargin(FlexItem::START_CROSS);
} else {
if (i.box->style->margin->top == Width::Type::AUTO)
fa.startCrossAxis(i.margin) = Px{0};
fa.startCrossAxis(i.margin) = 0_px;

// FIXME: not sure if the following is what the specs want
if (l.crossSize > fa.crossAxis(i.usedSize))
Expand Down Expand Up @@ -1369,7 +1369,7 @@ struct FlexFormatingContext {
};

auto alignContentCenter = [&]() {
Px currPositionCross{availableCrossSpace / Px{2}};
Px currPositionCross{availableCrossSpace / 2_px};
for (auto &flexLine : _lines) {
fa.crossAxis(flexLine.position) = currPositionCross;
currPositionCross += flexLine.crossSize;
Expand All @@ -1392,12 +1392,12 @@ struct FlexFormatingContext {
}

case Style::Align::SPACE_AROUND: {
if (availableCrossSpace < Px{0}) {
if (availableCrossSpace < 0_px) {
alignContentCenter();
} else {
Px gapSize = availableCrossSpace / Px{_lines.len()};

Px currPositionCross{gapSize / Px{2}};
Px currPositionCross{gapSize / 2_px};
for (auto &flexLine : _lines) {
fa.crossAxis(flexLine.position) = currPositionCross;
currPositionCross += flexLine.crossSize + gapSize;
Expand All @@ -1407,7 +1407,7 @@ struct FlexFormatingContext {
}

case Style::Align::SPACE_BETWEEN: {
if (availableCrossSpace < Px{0} or _lines.len() == 1)
if (availableCrossSpace < 0_px or _lines.len() == 1)
alignContentFlexStart();
else {
Px gapSize = availableCrossSpace / Px{_lines.len() - 1};
Expand Down
2 changes: 1 addition & 1 deletion src/vaev-layout/inline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Output inlineLayout(Tree &, Box &box, Input input) {

auto inlineSize = input.knownSize.x.unwrapOrElse([&] {
if (input.intrinsic == IntrinsicSize::MIN_CONTENT) {
return Px{0};
return 0_px;
} else if (input.intrinsic == IntrinsicSize::MAX_CONTENT) {
return Limits<Px>::MAX;
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/vaev-layout/layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static Opt<Px> _computeSpecifiedSize(Tree &tree, Box &box, Size size, Vec2Px con
return resolve(tree, box, size.value, isWidth ? containingBlock.x : containingBlock.y);
} else {
logWarn("unknown specified size: {}", size);
return Px{0};
return 0_px;
}
}

Expand All @@ -153,7 +153,7 @@ Output layout(Tree &tree, Box &box, Input input) {
}

input.knownSize.width = input.knownSize.width.map([&](auto s) {
return max(Px{0}, s - padding.horizontal() - borders.horizontal());
return max(0_px, s - padding.horizontal() - borders.horizontal());
});

if (input.knownSize.height == NONE) {
Expand All @@ -162,7 +162,7 @@ Output layout(Tree &tree, Box &box, Input input) {
}

input.knownSize.height = input.knownSize.height.map([&](auto s) {
return max(Px{0}, s - padding.vertical() - borders.vertical());
return max(0_px, s - padding.vertical() - borders.vertical());
});

input.position = input.position + borders.topStart() + padding.topStart();
Expand Down
12 changes: 6 additions & 6 deletions src/vaev-layout/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ struct TableFormatingContext {

auto tableUsedWidth =
tableBox.style->sizing->width == Size::AUTO
? Px{0}
? 0_px
: resolve(tree, tableBox, tableBox.style->sizing->width.value, input.availableSpace.x);

auto [columnBorders, sumBorders] = getColumnBorders();
Expand Down Expand Up @@ -494,7 +494,7 @@ struct TableFormatingContext {

Vec<Px> finalColWidths{colWidth.len()};
for (usize i = 0; i < grid.size.x; ++i) {
auto finalColWidth = colWidth[i].unwrapOr(Px{0});
auto finalColWidth = colWidth[i].unwrapOr(0_px);
finalColWidths.pushBack(finalColWidth);
}
return {finalColWidths, tableUsedWidth};
Expand Down Expand Up @@ -602,7 +602,7 @@ struct TableFormatingContext {
if (width == Size::AUTO)
continue;

auto widthValue = resolve(tree, el, width.value, tableComputedWidth.unwrapOr(Px{0}));
auto widthValue = resolve(tree, el, width.value, tableComputedWidth.unwrapOr(0_px));

for (usize x = start; x <= end; ++x) {
minColWidth[x] = max(minColWidth[x], widthValue);
Expand Down Expand Up @@ -720,7 +720,7 @@ struct TableFormatingContext {
continue;

for (usize y = row.start; y <= row.end; ++y) {
rowHeight[y] = resolve(tree, row.el, height.value, Px{0});
rowHeight[y] = resolve(tree, row.el, height.value, 0_px);
}
}

Expand All @@ -740,7 +740,7 @@ struct TableFormatingContext {
tree,
*cell.box,
cell.box->style->sizing->height.value,
Px{0}
0_px
);

for (usize k = 0; k < rowSpan; k++) {
Expand Down Expand Up @@ -826,7 +826,7 @@ Output tableLayout(Tree &tree, Box &wrapper, Input input) {

auto queryPref = [](Vec<Px> const &pref, isize l, isize r) -> Px {
if (r < l)
return Px{0};
return 0_px;
if (l == 0)
return pref[r];
return pref[r] - pref[l - 1];
Expand Down
4 changes: 2 additions & 2 deletions src/vaev-layout/values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Resolver Resolver::inherit(Resolver const &resolver) {
Px Resolver::_resolveFontRelative(Length value) {
if (not boxFont or not rootFont) {
logWarn("missing font information");
return Px{0};
return 0_px;
}

switch (value.unit()) {
Expand Down Expand Up @@ -228,7 +228,7 @@ Px Resolver::resolve(PercentOr<Length> value, Px relative) {

Px Resolver::resolve(Width value, Px relative) {
if (value == Width::Type::AUTO)
return Px{0};
return 0_px;
return resolve(value.value, relative);
}

Expand Down
Loading

0 comments on commit 32a7de1

Please sign in to comment.