Skip to content

Commit

Permalink
vaev: Update from upstream.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Nov 9, 2024
1 parent 60bda85 commit b4d34d2
Show file tree
Hide file tree
Showing 25 changed files with 1,000 additions and 526 deletions.
6 changes: 3 additions & 3 deletions src/web/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
18 changes: 14 additions & 4 deletions src/web/vaev-base/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ struct Display {
_LEN1,
};

bool isTableGroup() const {
return *this == Internal::TABLE_ROW_GROUP or
*this == Internal::TABLE_HEADER_GROUP or
*this == Internal::TABLE_FOOTER_GROUP;
bool isHeadBodyFootOrRow() {
return (
*this == TABLE_HEADER_GROUP or
*this == TABLE_ROW_GROUP or
*this == TABLE_FOOTER_GROUP or
*this == TABLE_ROW
);
}

bool isHeadBodyFootRowOrColGroup() {
return (
isHeadBodyFootOrRow() or
*this == TABLE_COLUMN_GROUP
);
}

using enum Internal;
Expand Down
3 changes: 3 additions & 0 deletions src/web/vaev-base/flex.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ struct FlexBasis {
};

struct FlexProps {
// FlexContainer
FlexDirection direction = FlexDirection::ROW;
FlexWrap wrap = FlexWrap::NOWRAP;

// FlexItem
FlexBasis basis = Width{Width::AUTO};
Number grow = 0;
Number shrink = 1;
Expand Down
4 changes: 4 additions & 0 deletions src/web/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(unsigned long long val) {
return Px(val);
}

using RectPx = Math::Rect<Px>;

using Vec2Px = Math::Vec2<Px>;
Expand Down
2 changes: 1 addition & 1 deletion src/web/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
15 changes: 15 additions & 0 deletions src/web/vaev-base/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,24 @@ enum struct TextTransform {
_LEN,
};

// MARK: White Space -----------------------------------------------------------
// https://drafts.csswg.org/css-text/#white-space-property

enum struct WhiteSpace {
NORMAL,
PRE,
NOWRAP,
PRE_WRAP,
BREAK_SPACES,
PRE_LINE,

_LEN,
};

struct TextProps {
TextAlign align = TextAlign::START;
TextTransform transform;
WhiteSpace whiteSpace = WhiteSpace::NORMAL;
};

} // namespace Vaev
4 changes: 2 additions & 2 deletions src/web/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
46 changes: 45 additions & 1 deletion src/web/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 All @@ -36,13 +36,57 @@ struct Input {
Vec2Px position = {};
Vec2Px availableSpace = {};
Vec2Px containingBlock = {};

Input withCommit(Commit c) const {
auto copy = *this;
copy.commit = c;
return copy;
}

Input withIntrinsic(IntrinsicSize i) const {
auto copy = *this;
copy.intrinsic = i;
return copy;
}

Input withKnownSize(Math::Vec2<Opt<Px>> size) const {
auto copy = *this;
copy.knownSize = size;
return copy;
}

Input withPosition(Vec2Px pos) const {
auto copy = *this;
copy.position = pos;
return copy;
}

Input withAvailableSpace(Vec2Px space) const {
auto copy = *this;
copy.availableSpace = space;
return copy;
}

Input withContainingBlock(Vec2Px block) const {
auto copy = *this;
copy.containingBlock = block;
return copy;
}
};

/// Output of the layout algorithm.

struct Output {
Vec2Px size;

Px width() const {
return size.x;
}

Px height() const {
return size.y;
}

static Output fromSize(Vec2Px size) {
return Output{size};
}
Expand Down
52 changes: 19 additions & 33 deletions src/web/vaev-layout/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,38 @@ namespace Vaev::Layout {

// https://www.w3.org/TR/CSS22/visuren.html#normal-flow
struct BlockFormatingContext {
static Px _determineWidth(Tree &tree, Box &box, Input input) {
Px width = Px{0};
for (auto &c : box.children()) {
auto ouput = layout(
tree,
c,
{
.commit = Commit::NO,
.intrinsic = input.intrinsic,
}
);

width = max(width, ouput.size.x);
}

return width;
}

Output run(Tree &tree, Box &box, Input input) {
Px blockSize = Px{0};
Px inlineSize = input.knownSize.width.unwrapOrElse([&] {
return _determineWidth(tree, box, input);
});
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
if (input.commit == Commit::YES and not input.knownSize.width)
inlineSize = run(tree, box, input.withCommit(Commit::NO)).width();

for (auto &c : box.children()) {
// TODO: Implement floating
// if (c.style->float_ != Float::NONE)
// continue;

Opt<Px> childInlineSize = NONE;
if (c.style->sizing->width == Size::AUTO and
c.style->display != Display::TABLE) {
childInlineSize = inlineSize;
}

Input childInput = {
.commit = input.commit,
.availableSpace = {inlineSize, Px{0}},
.containingBlock = {inlineSize, input.knownSize.y.unwrapOr(Px{0})},
.intrinsic = input.intrinsic,
.availableSpace = {inlineSize, 0_px},
.containingBlock = {inlineSize, input.knownSize.y.unwrapOr(0_px)},
};

auto margin = computeMargins(tree, c, childInput);

Opt<Px> childInlineSize = NONE;
if (c.style->sizing->width == Size::AUTO) {
childInlineSize = inlineSize - margin.horizontal();
}

if (c.style->position != Position::ABSOLUTE) {
blockSize += margin.top;
childInput.knownSize.width = childInlineSize;
if (input.commit == Commit::YES)
childInput.knownSize.width = childInlineSize;
}

childInput.position = input.position + Vec2Px{margin.start, blockSize};
Expand All @@ -66,9 +52,9 @@ struct BlockFormatingContext {
if (c.style->position != Position::ABSOLUTE) {
blockSize += ouput.size.y + margin.bottom;
}
}

// layoutFloat(t, f, input.containingBlock);
inlineSize = max(inlineSize, ouput.size.x + margin.start + margin.end);
}

return Output::fromSize({
inlineSize,
Expand Down
23 changes: 21 additions & 2 deletions src/web/vaev-layout/builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ static void _buildElement(Style::Computer &c, Markup::Element const &el, Box &pa
parent.add(std::move(box));
}

auto RE_SEGMENT_BREAK = Re::single('\n', '\r', '\f', '\v');

static void _buildRun(Style::Computer &, Markup::Text const &node, Box &parent) {
auto style = makeStrong<Style::Computed>(Style::Computed::initial());
style->inherit(*parent.style);
Expand Down Expand Up @@ -208,8 +210,25 @@ static void _buildRun(Style::Computer &, Markup::Text const &node, Box &parent)
break;
}

if (scan.eat(Re::space()))
prose->append(' ');
if (style->text->whiteSpace == WhiteSpace::PRE_LINE) {
bool hasBlank = false;
if (scan.eat(Re::blank())) {
hasBlank = true;
}

if (scan.eat(RE_SEGMENT_BREAK)) {
prose->append('\n');
scan.eat(Re::blank());
hasBlank = false;
}

if (hasBlank)
prose->append(' ');
} else {
// NORMAL
if (scan.eat(Re::space()))
prose->append(' ');
}
}

parent.add({style, fontFace, std::move(prose)});
Expand Down
Loading

0 comments on commit b4d34d2

Please sign in to comment.