diff options
author | Simon Sapin <simon.sapin@exyr.org> | 2019-09-09 19:23:36 +0200 |
---|---|---|
committer | Anthony Ramine <nox@nox.paris> | 2019-09-11 10:06:35 +0200 |
commit | 86904757e66285412de162483f3ba2a6db35403e (patch) | |
tree | 8fa6e2682aaa3e63ff684a38750b44f1bec9bea5 /components/layout_2020/fragments.rs | |
parent | be0e84b30f5b3ffa708b81348e66748ec43dfc8a (diff) | |
download | servo-86904757e66285412de162483f3ba2a6db35403e.tar.gz servo-86904757e66285412de162483f3ba2a6db35403e.zip |
Import files from Victor
https://github.com/SimonSapin/victor/tree/fdb11f3e87f6d2d59170d10169fa6deb94e53b94
Diffstat (limited to 'components/layout_2020/fragments.rs')
-rw-r--r-- | components/layout_2020/fragments.rs | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/components/layout_2020/fragments.rs b/components/layout_2020/fragments.rs new file mode 100644 index 00000000000..f2a4dcd2986 --- /dev/null +++ b/components/layout_2020/fragments.rs @@ -0,0 +1,116 @@ +use super::*; +use crate::text::ShapedSegment; + +pub(crate) enum Fragment { + Box(BoxFragment), + Anonymous(AnonymousFragment), + Text(TextFragment), +} + +pub(crate) struct BoxFragment { + pub style: Arc<ComputedValues>, + pub children: Vec<Fragment>, + + /// From the containing block’s start corner…? + /// This might be broken when the containing block is in a different writing mode: + /// https://drafts.csswg.org/css-writing-modes/#orthogonal-flows + pub content_rect: Rect<Length>, + + pub padding: Sides<Length>, + pub border: Sides<Length>, + pub margin: Sides<Length>, + + pub block_margins_collapsed_with_children: CollapsedBlockMargins, +} + +pub(crate) struct CollapsedBlockMargins { + pub collapsed_through: bool, + pub start: CollapsedMargin, + pub end: CollapsedMargin, +} + +#[derive(Clone, Copy)] +pub(crate) struct CollapsedMargin { + max_positive: Length, + min_negative: Length, +} + +/// Can contain child fragments with relative coordinates, but does not contribute to painting itself. +pub(crate) struct AnonymousFragment { + pub rect: Rect<Length>, + pub children: Vec<Fragment>, + pub mode: (WritingMode, Direction), +} + +pub(crate) struct TextFragment { + pub parent_style: Arc<ComputedValues>, + pub content_rect: Rect<Length>, + pub text: ShapedSegment, +} + +impl AnonymousFragment { + pub fn no_op(mode: (WritingMode, Direction)) -> Self { + Self { + children: vec![], + rect: Rect::zero(), + mode, + } + } +} + +impl BoxFragment { + pub fn border_rect(&self) -> Rect<Length> { + self.content_rect + .inflate(&self.padding) + .inflate(&self.border) + } +} + +impl CollapsedBlockMargins { + pub fn from_margin(margin: &Sides<Length>) -> Self { + Self { + collapsed_through: false, + start: CollapsedMargin::new(margin.block_start), + end: CollapsedMargin::new(margin.block_end), + } + } + + pub fn zero() -> Self { + Self { + collapsed_through: false, + start: CollapsedMargin::zero(), + end: CollapsedMargin::zero(), + } + } +} + +impl CollapsedMargin { + pub fn zero() -> Self { + Self { + max_positive: Length::zero(), + min_negative: Length::zero(), + } + } + + pub fn new(margin: Length) -> Self { + Self { + max_positive: margin.max(Length::zero()), + min_negative: margin.min(Length::zero()), + } + } + + pub fn adjoin(&self, other: &Self) -> Self { + Self { + max_positive: self.max_positive.max(other.max_positive), + min_negative: self.min_negative.min(other.min_negative), + } + } + + pub fn adjoin_assign(&mut self, other: &Self) { + *self = self.adjoin(other); + } + + pub fn solve(&self) -> Length { + self.max_positive + self.min_negative + } +} |