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 4, 2024
1 parent dfa60d9 commit 60bda85
Show file tree
Hide file tree
Showing 21 changed files with 177 additions and 69 deletions.
3 changes: 3 additions & 0 deletions src/libs/karm-base/cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ struct Cursor {
always_inline constexpr Cursor(Slice<T> slice)
: Cursor{begin(slice), end(slice)} {}

always_inline constexpr Cursor(MutSlice<T> slice)
: Cursor{begin(slice), end(slice)} {}

always_inline constexpr T const &operator[](usize i) const {
if (i >= len()) [[unlikely]]
panic("index out of bounds");
Expand Down
25 changes: 8 additions & 17 deletions src/libs/karm-scene/text.h
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
#pragma once

#include <karm-text/run.h>
#include <karm-text/prose.h>

#include "base.h"

namespace Karm::Scene {

struct Text : public Node {
Math::Vec2f baseline;
Karm::Text::Font font;
Karm::Text::Run run;
Gfx::Fill fill;
Math::Vec2f origin;
Strong<Karm::Text::Prose> prose;

Text(Math::Vec2f baseline, Karm::Text::Font font, Karm::Text::Run run, Gfx::Fill fill)
: baseline(baseline), font(font), run(run), fill(fill) {}
Text(Math::Vec2f origin, Strong<Karm::Text::Prose> prose)
: origin(origin), prose(prose) {}

void paint(Gfx::Canvas &g) override {
g.push();
g.fillStyle(fill);
g.fill(font, run, baseline);

// g.beginPath();
// g.line(Math::Edgef{baseline, baseline + Math::Vec2f{run->_width, 0}});
// g.stroke(Gfx::Stroke{
// .fill = Gfx::PINK,
// .width = 1,
// });
g.origin(origin);
prose->paint(g);
g.pop();
}

void repr(Io::Emit &e) const override {
e("(text z:{} {})", zIndex, baseline);
e("(text z:{} {})", zIndex, origin);
}
};

Expand Down
17 changes: 17 additions & 0 deletions src/web/vaev-base/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ namespace Vaev {
// CSS Text Module Level 4
// https://drafts.csswg.org/css-text-4

// MARK: Text Alignment --------------------------------------------------------
// https://drafts.csswg.org/css-text/#text-align-property

enum struct TextAlign {
START,
END,
LEFT,
RIGHT,
CENTER,
JUSTIFY,
MATCH_PARENT,
JUSTIFY_ALL,

_LEN,
};

// MARK: Text Transform --------------------------------------------------------
// https://drafts.csswg.org/css-text-4/#text-transform

Expand All @@ -18,6 +34,7 @@ enum struct TextTransform {
};

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

Expand Down
4 changes: 0 additions & 4 deletions src/web/vaev-js/_mod.h

This file was deleted.

5 changes: 3 additions & 2 deletions src/web/vaev-layout/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ struct BlockFormatingContext {
});

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

Opt<Px> childInlineSize = NONE;
if (c.style->sizing->width == Size::AUTO and
Expand Down
8 changes: 4 additions & 4 deletions src/web/vaev-layout/box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ namespace Vaev::Layout {

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

Box::Box(Strong<Style::Computed> style, Strong<Karm::Text::Fontface> font)
Box::Box(Strong<Style::Computed> style, Strong<Text::Fontface> font)
: style{std::move(style)}, fontFace{font} {}

Box::Box(Strong<Style::Computed> style, Strong<Karm::Text::Fontface> font, Content content)
Box::Box(Strong<Style::Computed> style, Strong<Text::Fontface> font, Content content)
: style{std::move(style)}, fontFace{font}, content{std::move(content)} {}

Karm::Slice<Box> Box::children() const {
Slice<Box> Box::children() const {
if (auto children = content.is<Vec<Box>>())
return *children;
return {};
}

Karm::MutSlice<Box> Box::children() {
MutSlice<Box> Box::children() {
if (auto children = content.is<Vec<Box>>()) {
return *children;
}
Expand Down
6 changes: 3 additions & 3 deletions src/web/vaev-layout/box.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <karm-image/picture.h>
#include <karm-text/run.h>
#include <karm-text/prose.h>
#include <vaev-markup/dom.h>
#include <vaev-style/computer.h>

Expand All @@ -14,7 +14,7 @@ namespace Vaev::Layout {
using Content = Union<
None,
Vec<Box>,
Karm::Text::Run,
Strong<Text::Prose>,
Image::Picture>;

struct Attrs {
Expand All @@ -29,7 +29,7 @@ struct Attrs {

struct Box : public Meta::NoCopy {
Strong<Style::Computed> style;
Strong<Karm::Text::Fontface> fontFace;
Strong<Text::Fontface> fontFace;
Content content = NONE;
Layout layout;
Attrs attrs;
Expand Down
52 changes: 43 additions & 9 deletions src/web/vaev-layout/builder.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <karm-text/loader.h>

#include "builder.h"
#include "values.h"

namespace Vaev::Layout {

Expand Down Expand Up @@ -148,37 +149,70 @@ static void _buildRun(Style::Computer &, Markup::Text const &node, Box &parent)
auto style = makeStrong<Style::Computed>(Style::Computed::initial());
style->inherit(*parent.style);

auto font = _lookupFontface(*style);
auto fontFace = _lookupFontface(*style);
Io::SScan scan{node.data};
scan.eat(Re::space());
if (scan.ended())
return;
Karm::Text::Run run;

// FIXME: We should pass this around from the top in order to properly resolve rems
Resolver resolver{
.rootFont = Text::Font{fontFace, 16},
.boxFont = Text::Font{fontFace, 16},
};
Text::ProseStyle proseStyle{
.font = {
fontFace,
resolver.resolve(style->font->size).cast<f64>(),
},
.multiline = true,
};

switch (style->text->align) {
case TextAlign::START:
case TextAlign::LEFT:
proseStyle.align = Text::TextAlign::LEFT;
break;

case TextAlign::END:
case TextAlign::RIGHT:
proseStyle.align = Text::TextAlign::RIGHT;
break;

case TextAlign::CENTER:
proseStyle.align = Text::TextAlign::CENTER;
break;

default:
// FIXME: Implement the rest
break;
}

auto prose = makeStrong<Text::Prose>(proseStyle);

while (not scan.ended()) {
switch (style->text->transform) {
case TextTransform::UPPERCASE:
run.append(toAsciiUpper(scan.next()));
prose->append(toAsciiUpper(scan.next()));
break;

case TextTransform::LOWERCASE:
run.append(toAsciiLower(scan.next()));
prose->append(toAsciiLower(scan.next()));
break;

case TextTransform::NONE:
run.append(scan.next());
prose->append(scan.next());
break;

default:
break;
}

if (scan.eat(Re::space())) {
run.append(' ');
}
if (scan.eat(Re::space()))
prose->append(' ');
}

parent.add({style, font, std::move(run)});
parent.add({style, fontFace, std::move(prose)});
}

void _buildNode(Style::Computer &c, Markup::Node const &node, Box &parent) {
Expand Down
26 changes: 26 additions & 0 deletions src/web/vaev-layout/inline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "inline.h"

#include "box.h"

namespace Vaev::Layout {

Output inlineLayout(Tree &, Box &box, Input input) {
// NOTE: We are not supposed to get there if the content is not a prose
auto &prose = *box.content.unwrap<Strong<Text::Prose>>("inlineLayout");

auto inlineSize = input.knownSize.x.unwrapOrElse([&] {
if (input.intrinsic == IntrinsicSize::MIN_CONTENT) {
return Px{0};
} else if (input.intrinsic == IntrinsicSize::MAX_CONTENT) {
return Limits<Px>::MAX;
} else {
return input.availableSpace.x;
}
});

auto res = prose.layout(inlineSize.cast<f64>());

return Output::fromSize(res.cast<Px>());
}

} // namespace Vaev::Layout
9 changes: 9 additions & 0 deletions src/web/vaev-layout/inline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "base.h"

namespace Vaev::Layout {

Output inlineLayout(Tree &tree, Box &box, Input input);

} // namespace Vaev::Layout
11 changes: 3 additions & 8 deletions src/web/vaev-layout/layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "box.h"
#include "flex.h"
#include "grid.h"
#include "inline.h"
#include "table.h"
#include "values.h"

Expand All @@ -12,14 +13,8 @@ namespace Vaev::Layout {
Output _contentLayout(Tree &tree, Box &box, Input input) {
auto display = box.style->display;

if (auto run = box.content.is<Karm::Text::Run>()) {
box.layout.fontSize = resolve(tree, box, box.style->font->size);
Karm::Text::Font font = {
box.fontFace,
box.layout.fontSize.cast<f64>(),
};
run->shape(font);
return Output::fromSize(run->size().cast<Px>());
if (auto run = box.content.is<Strong<Text::Prose>>()) {
return inlineLayout(tree, box, input);
} else if (
display == Display::FLOW or
display == Display::FLOW_ROOT or
Expand Down
10 changes: 5 additions & 5 deletions src/web/vaev-layout/paint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ static void _paintBox(Box &box, Scene::Stack &stack) {

_paintBox(box, currentColor, stack);

if (auto run = box.content.is<Karm::Text::Run>()) {
if (auto prose = box.content.is<Strong<Text::Prose>>()) {
(*prose)->_style.color = currentColor;

Karm::Text::Font font = {box.fontFace, box.layout.fontSize.cast<f64>()};
Math::Vec2f baseline = {0, font.metrics().ascend};
stack.add(makeStrong<Scene::Text>(
box.layout.borderBox().topStart().cast<f64>() + baseline,
font,
*run,
currentColor
box.layout.borderBox().topStart().cast<f64>(),
*prose
));
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/web/vaev-layout/values.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ struct Resolver {
f64 userFontSize = 16; /// Font size of the user agent
f64 parentFontSize = 16; /// Font size of the parent box

Opt<Text::Font> rootFont = NONE; /// Font of the root element
Opt<Text::Font> boxFont = NONE; /// Font of the current box
Viewport viewport; /// Viewport of the current box
Axis boxAxis = Axis::HORIZONTAL; /// Inline axis of the current box
Opt<Text::Font> rootFont = NONE; /// Font of the root element
Opt<Text::Font> boxFont = NONE; /// Font of the current box
Viewport viewport = {.small = {Px{800}, Px{600}}}; /// Viewport of the current box
Axis boxAxis = Axis::HORIZONTAL; /// Inline axis of the current box

static Resolver from(Tree const &tree, Box const &box);

Expand Down
4 changes: 2 additions & 2 deletions src/web/vaev-js/ast.h → src/web/vaev-script/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <karm-base/union.h>
#include <karm-base/vec.h>

namespace Vaev::Js {
namespace Vaev::Script {

struct Expr;

Expand Down Expand Up @@ -59,4 +59,4 @@ struct Stmt : public _Stmt {

struct Decl {};

} // namespace Vaev::Js
} // namespace Vaev::Script
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <karm-io/funcs.h>
#include <karm-sys/entry.h>
#include <karm-sys/file.h>
#include <vaev-js>
#include <vaev-script/lexer.h>

Async::Task<> entryPointAsync(Sys::Context &ctx) {
auto args = Sys::useArgs(ctx);
Expand All @@ -18,7 +18,7 @@ Async::Task<> entryPointAsync(Sys::Context &ctx) {
auto file = co_try$(Sys::File::open(url));
auto buf = co_try$(Io::readAllUtf8(file));
Io::SScan s{buf};
Vaev::Js::Lexer lex{s};
Vaev::Script::Lexer lex{s};
while (not lex.ended())
Sys::println("{}", lex.next());
co_return Ok();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"$schema": "https://schemas.cute.engineering/stable/cutekit.manifest.component.v1",
"id": "vaev-js.cli",
"id": "vaev-script.cli",
"type": "exe",
"description": "JavaScript engine testing tool",
"description": "ECMAScript engine testing tool",
"requires": [
"karm-sys",
"vaev-js"
"vaev-script"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "lexer.h"

namespace Vaev::Js {
namespace Vaev::Script {

// MARK: Token -----------------------------------------------------------------

Expand Down
Loading

0 comments on commit 60bda85

Please sign in to comment.