Skip to content

Commit

Permalink
vave-style+layout: Implemented white-space property.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Nov 5, 2024
1 parent 3e5a840 commit 810026c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/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
23 changes: 21 additions & 2 deletions src/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
35 changes: 35 additions & 0 deletions src/vaev-style/styles.h
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,40 @@ struct TextTransformProp {
}
};

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

struct WhiteSpaceProp {
WhiteSpace value = initial();

static constexpr Str name() { return "white-space"; }

static WhiteSpace initial() { return WhiteSpace::NORMAL; }

void apply(Computed &c) const {
c.text.cow().whiteSpace = value;
}

Res<> parse(Cursor<Css::Sst> &c) {
if (c.skip(Css::Token::ident("normal"))) {
value = WhiteSpace::NORMAL;
} else if (c.skip(Css::Token::ident("nowrap"))) {
value = WhiteSpace::NOWRAP;
} else if (c.skip(Css::Token::ident("pre"))) {
value = WhiteSpace::PRE;
} else if (c.skip(Css::Token::ident("pre-wrap"))) {
value = WhiteSpace::PRE_WRAP;
} else if (c.skip(Css::Token::ident("pre-line"))) {
value = WhiteSpace::PRE_LINE;
} else if (c.skip(Css::Token::ident("break-spaces"))) {
value = WhiteSpace::BREAK_SPACES;
} else {
return Error::invalidData("expected white-space");
}

return Ok();
}
};

// https://drafts.csswg.org/css2/#z-index

struct ZIndexProp {
Expand Down Expand Up @@ -2254,6 +2288,7 @@ using _StyleProp = Union<
// Text
TextAlignProp,
TextTransformProp,
WhiteSpaceProp,

// ZIndex
ZIndexProp
Expand Down

0 comments on commit 810026c

Please sign in to comment.