Fix spacing bugs 🚧

This commit is contained in:
Laurenz 2019-11-21 17:23:44 +01:00
parent 131e81baaf
commit ebff8eb038
8 changed files with 49 additions and 36 deletions

View File

@ -41,8 +41,7 @@ struct PartialLine {
usable: Size, usable: Size,
content: Vec<(Size, Layout)>, content: Vec<(Size, Layout)>,
dimensions: Size2D, dimensions: Size2D,
space: Option<Size>, space: SpaceState,
last_was_space: bool,
} }
impl PartialLine { impl PartialLine {
@ -51,8 +50,7 @@ impl PartialLine {
usable, usable,
content: vec![], content: vec![],
dimensions: Size2D::zero(), dimensions: Size2D::zero(),
space: None, space: SpaceState::Forbidden,
last_was_space: false,
} }
} }
} }
@ -198,7 +196,7 @@ impl FlexLayouter {
let remaining = self.axes.specialize(Size2D { let remaining = self.axes.specialize(Size2D {
x: self.part.usable x: self.part.usable
- self.part.dimensions.x - self.part.dimensions.x
- self.part.space.unwrap_or(Size::zero()), - self.part.space.soft_or_zero(),
y: self.line.combined_dimensions.y, y: self.line.combined_dimensions.y,
}); });
@ -235,9 +233,9 @@ impl FlexLayouter {
fn layout_box(&mut self, boxed: Layout) -> LayoutResult<()> { fn layout_box(&mut self, boxed: Layout) -> LayoutResult<()> {
let size = self.axes.generalize(boxed.dimensions); let size = self.axes.generalize(boxed.dimensions);
let new_dimension = self.part.dimensions.x let new_dimension = self.part.dimensions.x
+ self.part.space.unwrap_or(Size::zero()); + size.x
+ self.part.space.soft_or_zero();
if new_dimension > self.part.usable { if new_dimension > self.part.usable {
self.finish_line()?; self.finish_line()?;
@ -251,7 +249,7 @@ impl FlexLayouter {
} }
} }
if let Some(space) = self.part.space.take() { if let SpaceState::Soft(space) = self.part.space {
self.layout_space(space, false); self.layout_space(space, false);
} }
@ -260,15 +258,15 @@ impl FlexLayouter {
self.part.dimensions.x += size.x; self.part.dimensions.x += size.x;
self.part.dimensions.y.max_eq(size.y); self.part.dimensions.y.max_eq(size.y);
self.part.last_was_space = false; self.part.space = SpaceState::Allowed;
Ok(()) Ok(())
} }
fn layout_space(&mut self, space: Size, soft: bool) { fn layout_space(&mut self, space: Size, soft: bool) {
if soft { if soft {
if !self.part.last_was_space { if self.part.space != SpaceState::Forbidden {
self.part.space = Some(space); self.part.space = SpaceState::Soft(space);
} }
} else { } else {
if self.part.dimensions.x + space > self.part.usable { if self.part.dimensions.x + space > self.part.usable {
@ -276,7 +274,8 @@ impl FlexLayouter {
} else { } else {
self.part.dimensions.x += space; self.part.dimensions.x += space;
} }
self.part.last_was_space = true;
self.part.space = SpaceState::Forbidden;
} }
} }

View File

@ -307,6 +307,19 @@ pub enum Alignment {
End, End,
} }
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum SpaceState {
Soft(Size),
Forbidden,
Allowed,
}
impl SpaceState {
fn soft_or_zero(&self) -> Size {
if let SpaceState::Soft(space) = self { *space } else { Size::zero() }
}
}
/// The error type for layouting. /// The error type for layouting.
pub enum LayoutError { pub enum LayoutError {
/// An action is unallowed in the active context. /// An action is unallowed in the active context.

View File

@ -36,8 +36,7 @@ struct Subspace {
anchor: Size2D, anchor: Size2D,
factor: i32, factor: i32,
dimensions: Size2D, dimensions: Size2D,
space: Option<Size>, space: SpaceState,
last_was_space: bool,
} }
impl Subspace { impl Subspace {
@ -48,8 +47,7 @@ impl Subspace {
anchor: axes.anchor(usable), anchor: axes.anchor(usable),
factor: axes.secondary.axis.factor(), factor: axes.secondary.axis.factor(),
dimensions: Size2D::zero(), dimensions: Size2D::zero(),
space: None, space: SpaceState::Forbidden,
last_was_space: false,
} }
} }
} }
@ -79,7 +77,7 @@ impl StackLayouter {
} }
pub fn add(&mut self, layout: Layout) -> LayoutResult<()> { pub fn add(&mut self, layout: Layout) -> LayoutResult<()> {
if let Some(space) = self.sub.space.take() { if let SpaceState::Soft(space) = self.sub.space {
self.add_space(space, false); self.add_space(space, false);
} }
@ -92,6 +90,9 @@ impl StackLayouter {
while !self.sub.usable.fits(new_dimensions) { while !self.sub.usable.fits(new_dimensions) {
if self.space_is_last() && self.space_is_empty() { if self.space_is_last() && self.space_is_empty() {
println!("usable: {}", self.sub.usable);
println!("dims: {}", new_dimensions);
println!("size: {}", size);
Err(LayoutError::NotEnoughSpace("failed to add box to stack"))?; Err(LayoutError::NotEnoughSpace("failed to add box to stack"))?;
} }
@ -109,7 +110,7 @@ impl StackLayouter {
self.space.actions.add_layout(pos, layout); self.space.actions.add_layout(pos, layout);
self.sub.dimensions = new_dimensions; self.sub.dimensions = new_dimensions;
self.sub.last_was_space = false; self.sub.space = SpaceState::Allowed;
Ok(()) Ok(())
} }
@ -123,17 +124,17 @@ impl StackLayouter {
pub fn add_space(&mut self, space: Size, soft: bool) { pub fn add_space(&mut self, space: Size, soft: bool) {
if soft { if soft {
if !self.sub.last_was_space { if self.sub.space != SpaceState::Forbidden {
self.sub.space = Some(space); self.sub.space = SpaceState::Soft(space);
} }
} else { } else {
if self.sub.dimensions.y + space > self.sub.usable.y { if self.sub.dimensions.y + space > self.sub.usable.y {
self.sub.dimensions.y = self.sub.usable.y; self.sub.dimensions.y = self.sub.usable.y;
self.finish_space(false);
} else { } else {
self.sub.dimensions.y += space; self.sub.dimensions.y += space;
} }
self.sub.last_was_space = true;
self.sub.space = SpaceState::Forbidden;
} }
} }
@ -197,8 +198,8 @@ impl StackLayouter {
self.layouts.add(Layout { self.layouts.add(Layout {
dimensions: match self.ctx.expand { dimensions: match self.ctx.expand {
true => self.space.combined_dimensions.padded(space.padding), true => space.dimensions,
false => space.dimensions, false => self.space.combined_dimensions.padded(space.padding),
}, },
actions: self.space.actions.to_vec(), actions: self.space.actions.to_vec(),
debug_render: true, debug_render: true,

View File

@ -58,16 +58,11 @@ impl<'a, 'p> TreeLayouter<'a, 'p> {
} }
fn layout_space(&mut self) { fn layout_space(&mut self) {
if !self.flex.run_is_empty() { self.flex.add_primary_space(word_spacing(&self.style), true);
self.flex.add_primary_space(word_spacing(&self.style), true);
}
} }
fn layout_paragraph(&mut self) -> LayoutResult<()> { fn layout_paragraph(&mut self) -> LayoutResult<()> {
if !self.flex.run_is_empty() { self.flex.add_secondary_space(paragraph_spacing(&self.style), true)
self.flex.add_secondary_space(paragraph_spacing(&self.style), true)?;
}
Ok(())
} }
fn layout_func(&mut self, func: &FuncCall) -> LayoutResult<()> { fn layout_func(&mut self, func: &FuncCall) -> LayoutResult<()> {

View File

@ -31,6 +31,11 @@ function! {
let mut axes = ctx.axes; let mut axes = ctx.axes;
axes.primary.alignment = this.alignment; axes.primary.alignment = this.alignment;
if ctx.axes.primary.alignment == Alignment::End
&& this.alignment == Alignment::Origin {
axes.primary.expand = true;
}
Ok(match &this.body { Ok(match &this.body {
Some(body) => commands![AddMultiple( Some(body) => commands![AddMultiple(
layout_tree(body, LayoutContext { layout_tree(body, LayoutContext {

View File

@ -20,7 +20,7 @@
Context Right: {lorem:10} Context Right: {lorem:10}
[align: left][In-between Left: {lorem:10}] [align: left][In-between Left]
Right Again: {lorem:10} Right Again: {lorem:10}

View File

@ -10,11 +10,11 @@
] ]
[align: right][*WiSe 2019/2020* [n] Week 1] [align: right][*WiSe 2019/2020* [n] Week 1]
[v: 3mm] [v: 6mm]
[align: center][ [align: center][
*3. Ubungsblatt Computerorientierte Mathematik II* [n] *3. Ubungsblatt Computerorientierte Mathematik II* [v: 0.3mm]
*Abgabe: 03.05.2019* (bis 10:10 Uhr in MA 001) [n] *Abgabe: 03.05.2019* (bis 10:10 Uhr in MA 001) [v: 0.3mm]
*Alle Antworten sind zu beweisen.* *Alle Antworten sind zu beweisen.*
] ]

View File

@ -43,7 +43,7 @@ class MultiboxRenderer:
images = [] images = []
layout_count = int(self.content[0]) layout_count = int(self.content[0])
horizontal = math.ceil(math.sqrt(layout_count)) horizontal = math.floor(math.sqrt(layout_count))
start = 1 start = 1
for _ in range(layout_count): for _ in range(layout_count):