Skip to content

Commit

Permalink
wip, a lot of workarounds to be fixed after idea is confirmed
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloamed committed Nov 5, 2024
1 parent ebdf0be commit f58621e
Show file tree
Hide file tree
Showing 24 changed files with 294 additions and 198 deletions.
67 changes: 49 additions & 18 deletions src/vaev-driver/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ RenderResult render(Markup::Document const &dom, Style::Media const &media, Vec2
{
.small = viewport,
},
Layout::FragmentationContext()
Layout::FragmentationContext({Px{900}, Px{100}})
};

elapsed = Sys::now() - start;
Expand All @@ -88,17 +88,45 @@ RenderResult render(Markup::Document const &dom, Style::Media const &media, Vec2

start = Sys::now();

Layout::layout(
tree,
tree.root,
{
.commit = Layout::Commit::YES,
.knownSize = {vp.small.width, NONE},
.availableSpace = {vp.small.width, Px{0}},
.containingBlock = {vp.small.width, vp.small.height},
}
);
Layout::layoutPositioned(tree, tree.root, {vp.small.width, vp.small.height});
auto root = Layout::Frag(&tree.root);

while (true) {
tree.fc.createNextFragmentainer();

auto outDiscovery = Layout::layoutFrag(
tree,
tree.root,
{
.knownSize = {vp.small.width, NONE},
.availableSpace = {vp.small.width, Px{0}},
.containingBlock = {vp.small.width, vp.small.height},
},
true
);

logDebug("OUT: {} {}", outDiscovery.unwrap().endChildren, outDiscovery.unwrap().size);

auto outReal = Layout::layoutFrag(
tree,
tree.root,
{
.fragment = &root,
.knownSize = {vp.small.width, NONE},
.availableSpace = {vp.small.width, Px{0}},
.containingBlock = {vp.small.width, vp.small.height},
},
false
);

tree.fc.getCurrentFragmentainer()->root = &root.children[root.children.len() - 1];

if (tree.fc.getStartPosition(tree.root) == 1)
break;
}

Layout::layoutPositioned(tree, root, {vp.small.width, vp.small.height});

tree.fc.preparePrint();

auto sceneRoot = makeStrong<Scene::Stack>();

Expand All @@ -107,7 +135,7 @@ RenderResult render(Markup::Document const &dom, Style::Media const &media, Vec2

auto paintStart = Sys::now();

Layout::paint(tree.root, *sceneRoot);
Layout::paint(root, *sceneRoot);
sceneRoot->prepare();

elapsed = Sys::now() - paintStart;
Expand All @@ -117,6 +145,7 @@ RenderResult render(Markup::Document const &dom, Style::Media const &media, Vec2
std::move(stylebook),
makeStrong<Layout::Box>(std::move(tree.root)),
sceneRoot,
makeStrong<Layout::Frag>(root)
};
}

Expand Down Expand Up @@ -144,15 +173,16 @@ RenderResult render(Markup::Document &dom, Style::Media const &media, Print::Pap
Layout::Tree tree = {
Layout::build(computer, dom),
vp,
Layout::FragmentationContext()
Layout::FragmentationContext({Px{800}, Px{100}})
};

auto root = Layout::Frag(&tree.root);

while (true) {
auto outDiscovery = Layout::layoutFrag(
tree,
tree.root,
{
.commit = Layout::Commit::YES,
.knownSize = {vp.small.width, NONE},
.availableSpace = {vp.small.width, Px{0}},
.containingBlock = {vp.small.width, vp.small.height},
Expand All @@ -164,7 +194,7 @@ RenderResult render(Markup::Document &dom, Style::Media const &media, Print::Pap
tree,
tree.root,
{
.commit = Layout::Commit::YES,
.fragment = &root,
.knownSize = {vp.small.width, NONE},
.availableSpace = {vp.small.width, Px{0}},
.containingBlock = {vp.small.width, vp.small.height},
Expand All @@ -177,14 +207,15 @@ RenderResult render(Markup::Document &dom, Style::Media const &media, Print::Pap
}

auto sceneRoot = makeStrong<Scene::Page>();
Layout::paint(tree.root, *sceneRoot);
Layout::paint(root, *sceneRoot);
sceneRoot->prepare();

return {
std::move(stylebook),
makeStrong<Layout::Box>(std::move(tree.root)),
sceneRoot,
makeStrong<Layout::Frag>(root)
};
}
} // namespace Vaev::Driver

} // namespace Vaev::Driver
1 change: 1 addition & 0 deletions src/vaev-driver/render.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <vaev-base/length.h>
#include <vaev-layout/box.h>
#include <vaev-layout/frag.h>
#include <vaev-layout/tree.h>
#include <vaev-markup/dom.h>
#include <vaev-style/media.h>

Expand Down
27 changes: 0 additions & 27 deletions src/vaev-layout/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,6 @@ struct Viewport {
RectPx dynamic = small;
};

struct Frag;

/// Input to the layout algorithm.
struct Input {
/// Parent fragment where the layout will be attached.
MutCursor<Frag> fragment;
IntrinsicSize intrinsic = IntrinsicSize::AUTO;
Math::Vec2<Opt<Px>> knownSize = {};
Vec2Px position = {};
Vec2Px availableSpace = {};
Vec2Px containingBlock = {};
};

/// Output of the layout algorithm.
struct Output {
Vec2Px size;

static Output fromSize(Vec2Px size) {
return Output{size};
}
};

struct OutputFrag {
Vec2Px size;
usize endChildren;
};

/// Computed layout values.

struct Box;
Expand Down
15 changes: 11 additions & 4 deletions src/vaev-layout/block.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "block.h"
#include "input_output.h"

#include "box.h"
#include "layout.h"
#include "tree.h"

namespace Vaev::Layout {

Expand Down Expand Up @@ -32,6 +32,13 @@ struct BlockFormatingContext {
return _determineWidth(tree, box, input);
});

if (not tree.fc.getCurrentFragmentainer()->acceptsFit(input.position, {
inlineSize,
blockSize,
})) {
return NONE;
}

usize endChildren = stopAt.unwrapOr(box.children().len());

for (usize i = startAt; i < endChildren; ++i) {
Expand Down Expand Up @@ -79,7 +86,7 @@ struct BlockFormatingContext {

currentBreakpoint = OutputFrag{
.size = Vec2Px{inlineSize, blockSize},
.endChildren = i,
.endChildren = i + 1,
};
}

Expand All @@ -90,7 +97,7 @@ struct BlockFormatingContext {
inlineSize,
blockSize,
},
.endChildren = endChildren
.endChildren = endChildren,
};
}
};
Expand Down
4 changes: 3 additions & 1 deletion src/vaev-layout/block.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include "base.h"
#include "input_output.h"

#include "tree.h"

namespace Vaev::Layout {

Expand Down
1 change: 1 addition & 0 deletions src/vaev-layout/box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <karm-text/loader.h>

#include "box.h"
#include "frag.h"

namespace Vaev::Layout {

Expand Down
63 changes: 2 additions & 61 deletions src/vaev-layout/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace Vaev::Layout {

// MARK: Box ------------------------------------------------------------------

struct Box;

using Content = Union<
None,
Vec<Box>,
Expand Down Expand Up @@ -46,65 +48,4 @@ struct Box : public Meta::NoCopy {
void repr(Io::Emit &e) const;
};

// TODO: if we have columns inside a page, who determines the position of the columns? the page or the column?

// https://www.w3.org/TR/css-break-3/#fragmentainer
struct Fragmentainer {
Vec2Px size;

bool acceptsFit(Vec2Px position, Vec2Px sizeBox) {
return position.x + sizeBox.x <= size.x and position.x + sizeBox.y <= size.y;
}
};

// https://www.w3.org/TR/css-break-3/#fragmentation-context
struct FragmentationContext {

Vec2Px defaultSize;
Map<Box *, usize> layoutUntilComitted, layoutUntilNotComitted;

FragmentationContext() : defaultSize({Px{800}, Px{600}}) {}

FragmentationContext(Vec2Px defaultSize) : defaultSize(defaultSize) {}

Vec<Strong<Fragmentainer>> fragmentainers;

void createNextFragmentainer() {
fragmentainers.pushBack(makeStrong<Fragmentainer>(Fragmentainer(defaultSize)));
}

Strong<Fragmentainer> getCurrentFragmentainer() {
if (fragmentainers.empty()) {
createNextFragmentainer();
}

return fragmentainers[fragmentainers.len() - 1];
}

usize getStartPosition(Box &box) {
if (not layoutUntilComitted.has(&box))
return 0;
else
return layoutUntilComitted.get(&box) + 1;
}

usize getDiscoveredEndPosition(Box &box) {
return layoutUntilNotComitted.get(&box);
}

void setDiscoveredEndPosition(Box &box, usize pos) {
layoutUntilNotComitted.put(&box, pos);
}

void setLaidOutEndPosition(Box &box, usize pos) {
layoutUntilComitted.put(&box, pos);
}
};

struct Tree {
Box root;
Viewport viewport;
FragmentationContext fc;
};

} // namespace Vaev::Layout
2 changes: 1 addition & 1 deletion src/vaev-layout/flex.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "layout.h"
#include "input_output.h"

namespace Vaev::Layout {

Expand Down
4 changes: 2 additions & 2 deletions src/vaev-layout/frag.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ struct Metrics {
};

struct Frag {
Cursor<Box> box;
MutCursor<Box> box;
Metrics metrics;
Vec<Frag> children;

Frag(Cursor<Box> box) : box{std::move(box)} {}
Frag(MutCursor<Box> box) : box{std::move(box)} {}

Style::Computed const &style() const {
return *box->style;
Expand Down
Loading

0 comments on commit f58621e

Please sign in to comment.