summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/export/pdf.rs14
-rw-r--r--src/layout/flex.rs154
-rw-r--r--src/layout/mod.rs18
-rw-r--r--src/layout/stacked.rs135
-rw-r--r--src/layout/tree.rs53
-rw-r--r--src/macros.rs2
-rw-r--r--src/size.rs14
7 files changed, 247 insertions, 143 deletions
diff --git a/src/export/pdf.rs b/src/export/pdf.rs
index 3c718c2e..f029a37f 100644
--- a/src/export/pdf.rs
+++ b/src/export/pdf.rs
@@ -80,7 +80,7 @@ impl<'d, W: Write> ExportProcess<'d, W> {
) -> PdfResult<ExportProcess<'d, W>>
{
let (fonts, font_remap) = Self::subset_fonts(layouts, font_loader)?;
- let offsets = Self::calculate_offset(layouts.count(), fonts.len());
+ let offsets = Self::calculate_offsets(layouts.count(), fonts.len());
Ok(ExportProcess {
writer: PdfWriter::new(target),
@@ -155,7 +155,7 @@ impl<'d, W: Write> ExportProcess<'d, W> {
/// We need to know in advance which IDs to use for which objects to cross-reference them.
/// Therefore, we calculate them in the beginning.
- fn calculate_offset(layout_count: usize, font_count: usize) -> Offsets {
+ fn calculate_offsets(layout_count: usize, font_count: usize) -> Offsets {
let catalog = 1;
let page_tree = catalog + 1;
let pages = (page_tree + 1, page_tree + layout_count as Ref);
@@ -203,7 +203,11 @@ impl<'d, W: Write> ExportProcess<'d, W> {
)?;
// The page objects (non-root nodes in the page tree).
- for (id, page) in ids(self.offsets.pages).zip(self.layouts) {
+ let iter = ids(self.offsets.pages)
+ .zip(ids(self.offsets.contents))
+ .zip(self.layouts);
+
+ for ((page_id, content_id), page) in iter {
let rect = Rect::new(
0.0,
0.0,
@@ -212,10 +216,10 @@ impl<'d, W: Write> ExportProcess<'d, W> {
);
self.writer.write_obj(
- id,
+ page_id,
Page::new(self.offsets.page_tree)
.media_box(rect)
- .contents(ids(self.offsets.contents)),
+ .content(content_id),
)?;
}
diff --git a/src/layout/flex.rs b/src/layout/flex.rs
index a4b3ed6d..80cc2074 100644
--- a/src/layout/flex.rs
+++ b/src/layout/flex.rs
@@ -21,22 +21,19 @@ pub struct FlexLayouter {
ctx: FlexContext,
units: Vec<FlexUnit>,
- actions: LayoutActionList,
- usable: Size2D,
- dimensions: Size2D,
- cursor: Size2D,
-
+ stack: StackLayouter,
+ usable_width: Size,
run: FlexRun,
- next_glue: Option<Layout>,
+ cached_glue: Option<Layout>,
}
/// The context for flex layouting.
#[derive(Debug, Copy, Clone)]
pub struct FlexContext {
- /// The space to layout the boxes in.
pub space: LayoutSpace,
/// The spacing between two lines of boxes.
pub flex_spacing: Size,
+ pub extra_space: Option<LayoutSpace>,
}
enum FlexUnit {
@@ -49,7 +46,7 @@ enum FlexUnit {
}
struct FlexRun {
- content: Vec<(Size2D, Layout)>,
+ content: Vec<(Size, Layout)>,
size: Size2D,
}
@@ -60,17 +57,17 @@ impl FlexLayouter {
ctx,
units: vec![],
- actions: LayoutActionList::new(),
- usable: ctx.space.usable(),
- dimensions: match ctx.space.alignment {
- Alignment::Left => Size2D::zero(),
- Alignment::Right => Size2D::with_x(ctx.space.usable().x),
- },
-
- cursor: Size2D::new(ctx.space.padding.left, ctx.space.padding.top),
+ stack: StackLayouter::new(StackContext {
+ space: ctx.space,
+ extra_space: ctx.extra_space,
+ }),
- run: FlexRun::new(),
- next_glue: None,
+ usable_width: ctx.space.usable().x,
+ run: FlexRun {
+ content: vec![],
+ size: Size2D::zero()
+ },
+ cached_glue: None,
}
}
@@ -90,12 +87,14 @@ impl FlexLayouter {
}
/// Compute the justified layout.
- pub fn finish(mut self) -> LayoutResult<Layout> {
+ ///
+ /// The layouter is not consumed by this to prevent ownership problems
+ /// with borrowed layouters. The state of the layouter is not reset.
+ /// Therefore, it should not be further used after calling `finish`.
+ pub fn finish(&mut self) -> LayoutResult<MultiLayout> {
// Move the units out of the layout because otherwise, we run into
// ownership problems.
- let units = self.units;
- self.units = Vec::new();
-
+ let units = std::mem::replace(&mut self.units, vec![]);
for unit in units {
match unit {
FlexUnit::Boxed(boxed) => self.layout_box(boxed)?,
@@ -104,109 +103,96 @@ impl FlexLayouter {
}
// Finish the last flex run.
- self.finish_flex_run();
+ self.finish_run()?;
- Ok(Layout {
- dimensions: if self.ctx.space.shrink_to_fit {
- self.dimensions.padded(self.ctx.space.padding)
- } else {
- self.ctx.space.dimensions
- },
- actions: self.actions.into_vec(),
- debug_render: true,
- })
- }
-
- /// Whether this layouter contains any items.
- pub fn is_empty(&self) -> bool {
- self.units.is_empty()
+ self.stack.finish()
}
+ /// Layout a content box into the current flex run or start a new run if
+ /// it does not fit.
fn layout_box(&mut self, boxed: Layout) -> LayoutResult<()> {
- let next_glue_width = self
- .next_glue
+ let glue_width = self
+ .cached_glue
.as_ref()
- .map(|g| g.dimensions.x)
+ .map(|layout| layout.dimensions.x)
.unwrap_or(Size::zero());
- let new_line_width = self.run.size.x + next_glue_width + boxed.dimensions.x;
+ let new_line_width = self.run.size.x + glue_width + boxed.dimensions.x;
+
+ if self.overflows_line(new_line_width) {
+ self.cached_glue = None;
- if self.overflows(new_line_width) {
- // If the box does not even fit on its own line, then
- // we can't do anything.
- if self.overflows(boxed.dimensions.x) {
- return Err(LayoutError::NotEnoughSpace);
+ // If the box does not even fit on its own line, then we try
+ // it in the next space, or we have to give up if there is none.
+ if self.overflows_line(boxed.dimensions.x) {
+ if self.ctx.extra_space.is_some() {
+ self.stack.finish_layout(true)?;
+ return self.layout_box(boxed);
+ } else {
+ return Err(LayoutError::NotEnoughSpace("cannot fit box into flex run"));
+ }
}
- self.next_glue = None;
- self.finish_flex_run();
+ self.finish_run()?;
} else {
// Only add the glue if we did not move to a new line.
self.flush_glue();
}
- self.add_to_flex_run(boxed);
+ self.add_to_run(boxed);
Ok(())
}
fn layout_glue(&mut self, glue: Layout) {
self.flush_glue();
- self.next_glue = Some(glue);
+ self.cached_glue = Some(glue);
}
fn flush_glue(&mut self) {
- if let Some(glue) = self.next_glue.take() {
- self.add_to_flex_run(glue);
+ if let Some(glue) = self.cached_glue.take() {
+ let new_line_width = self.run.size.x + glue.dimensions.x;
+ if !self.overflows_line(new_line_width) {
+ self.add_to_run(glue);
+ }
}
}
- fn add_to_flex_run(&mut self, layout: Layout) {
- let position = self.cursor;
+ fn add_to_run(&mut self, layout: Layout) {
+ let x = self.run.size.x;
- self.cursor.x += layout.dimensions.x;
self.run.size.x += layout.dimensions.x;
self.run.size.y = crate::size::max(self.run.size.y, layout.dimensions.y);
- self.run.content.push((position, layout));
+ self.run.content.push((x, layout));
}
- fn finish_flex_run(&mut self) {
- // Add all layouts from the current flex run at the correct positions.
- match self.ctx.space.alignment {
- Alignment::Left => {
- for (position, layout) in self.run.content.drain(..) {
- self.actions.add_layout(position, layout);
- }
- }
+ fn finish_run(&mut self) -> LayoutResult<()> {
+ self.run.size.y += self.ctx.flex_spacing;
- Alignment::Right => {
- let extra_space = Size2D::with_x(self.usable.x - self.run.size.x);
- for (position, layout) in self.run.content.drain(..) {
- self.actions.add_layout(position + extra_space, layout);
- }
- }
+ let mut actions = LayoutActionList::new();
+ for (x, layout) in self.run.content.drain(..) {
+ let position = Size2D::with_x(x);
+ actions.add_layout(position, layout);
}
- self.dimensions.x = crate::size::max(self.dimensions.x, self.run.size.x);
- self.dimensions.y += self.ctx.flex_spacing;
- self.dimensions.y += self.run.size.y;
+ self.stack.add(Layout {
+ dimensions: self.run.size,
+ actions: actions.into_vec(),
+ debug_render: false,
+ })?;
- self.cursor.x = self.ctx.space.padding.left;
- self.cursor.y += self.run.size.y + self.ctx.flex_spacing;
self.run.size = Size2D::zero();
+
+ Ok(())
}
- fn overflows(&self, line: Size) -> bool {
- line > self.usable.x
+ /// Whether this layouter contains any items.
+ pub fn is_empty(&self) -> bool {
+ self.units.is_empty()
}
-}
-impl FlexRun {
- fn new() -> FlexRun {
- FlexRun {
- content: vec![],
- size: Size2D::zero()
- }
+ fn overflows_line(&self, line: Size) -> bool {
+ line > self.usable_width
}
}
diff --git a/src/layout/mod.rs b/src/layout/mod.rs
index 031226b9..b0fba4f2 100644
--- a/src/layout/mod.rs
+++ b/src/layout/mod.rs
@@ -48,6 +48,7 @@ impl Layout {
self.dimensions.x.to_pt(),
self.dimensions.y.to_pt()
)?;
+ writeln!(f, "{}", self.actions.len())?;
for action in &self.actions {
action.serialize(f)?;
writeln!(f)?;
@@ -93,6 +94,17 @@ impl MultiLayout {
}
}
+impl MultiLayout {
+ /// Serialize this collection of layouts into an output buffer.
+ pub fn serialize<W: Write>(&self, f: &mut W) -> io::Result<()> {
+ writeln!(f, "{}", self.count())?;
+ for layout in self {
+ layout.serialize(f)?;
+ }
+ Ok(())
+ }
+}
+
impl IntoIterator for MultiLayout {
type Item = Layout;
type IntoIter = std::vec::IntoIter<Layout>;
@@ -112,7 +124,7 @@ impl<'a> IntoIterator for &'a MultiLayout {
}
/// The general context for layouting.
-#[derive(Copy, Clone)]
+#[derive(Debug, Copy, Clone)]
pub struct LayoutContext<'a, 'p> {
pub loader: &'a SharedFontLoader<'p>,
pub style: &'a TextStyle,
@@ -154,7 +166,7 @@ pub enum Alignment {
/// The error type for layouting.
pub enum LayoutError {
/// There is not enough space to add an item.
- NotEnoughSpace,
+ NotEnoughSpace(&'static str),
/// There was no suitable font for the given character.
NoSuitableFont(char),
/// An error occured while gathering font data.
@@ -167,7 +179,7 @@ pub type LayoutResult<T> = Result<T, LayoutError>;
error_type! {
err: LayoutError,
show: f => match err {
- LayoutError::NotEnoughSpace => write!(f, "not enough space"),
+ LayoutError::NotEnoughSpace(desc) => write!(f, "not enough space: {}", desc),
LayoutError::NoSuitableFont(c) => write!(f, "no suitable font for '{}'", c),
LayoutError::Font(err) => write!(f, "font error: {}", err),
},
diff --git a/src/layout/stacked.rs b/src/layout/stacked.rs
index bfca4e3e..367a03d7 100644
--- a/src/layout/stacked.rs
+++ b/src/layout/stacked.rs
@@ -5,44 +5,38 @@ use super::*;
/// The boxes are arranged vertically, each layout gettings it's own "line".
pub struct StackLayouter {
ctx: StackContext,
+ layouts: MultiLayout,
actions: LayoutActionList,
+
+ space: LayoutSpace,
usable: Size2D,
dimensions: Size2D,
cursor: Size2D,
+ in_extra_space: bool,
+ started: bool,
}
/// The context for stack layouting.
#[derive(Debug, Copy, Clone)]
pub struct StackContext {
- /// The space to layout the boxes in.
pub space: LayoutSpace,
+ pub extra_space: Option<LayoutSpace>,
}
impl StackLayouter {
/// Create a new stack layouter.
pub fn new(ctx: StackContext) -> StackLayouter {
- let space = ctx.space;
-
StackLayouter {
ctx,
+ layouts: MultiLayout::new(),
actions: LayoutActionList::new(),
+ space: ctx.space,
usable: ctx.space.usable(),
- dimensions: match ctx.space.alignment {
- Alignment::Left => Size2D::zero(),
- Alignment::Right => Size2D::with_x(space.usable().x),
- },
-
- cursor: Size2D::new(
- // If left-align, the cursor points to the top-left corner of
- // each box. If we right-align, it points to the top-right
- // corner.
- match ctx.space.alignment {
- Alignment::Left => space.padding.left,
- Alignment::Right => space.dimensions.x - space.padding.right,
- },
- space.padding.top,
- ),
+ dimensions: start_dimensions(ctx.space),
+ cursor: start_cursor(ctx.space),
+ in_extra_space: false,
+ started: true,
}
}
@@ -53,19 +47,30 @@ impl StackLayouter {
/// Add a sublayout to the bottom.
pub fn add(&mut self, layout: Layout) -> LayoutResult<()> {
+ if !self.started {
+ self.start_new_space()?;
+ }
+
let new_dimensions = Size2D {
x: crate::size::max(self.dimensions.x, layout.dimensions.x),
y: self.dimensions.y + layout.dimensions.y,
};
if self.overflows(new_dimensions) {
- return Err(LayoutError::NotEnoughSpace);
+ if self.ctx.extra_space.is_some() &&
+ !(self.in_extra_space && self.overflows(layout.dimensions))
+ {
+ self.finish_layout(true)?;
+ return self.add(layout);
+ } else {
+ return Err(LayoutError::NotEnoughSpace("cannot fit box into stack"));
+ }
}
// Determine where to put the box. When we right-align it, we want the
// cursor to point to the top-right corner of the box. Therefore, the
// position has to be moved to the left by the width of the box.
- let position = match self.ctx.space.alignment {
+ let position = match self.space.alignment {
Alignment::Left => self.cursor,
Alignment::Right => self.cursor - Size2D::with_x(layout.dimensions.x),
};
@@ -88,26 +93,74 @@ impl StackLayouter {
/// Add vertical space after the last layout.
pub fn add_space(&mut self, space: Size) -> LayoutResult<()> {
- if self.overflows(self.dimensions + Size2D::with_y(space)) {
- return Err(LayoutError::NotEnoughSpace);
+ if !self.started {
+ self.start_new_space()?;
}
- self.cursor.y += space;
- self.dimensions.y += space;
+ let new_dimensions = self.dimensions + Size2D::with_y(space);
+
+ if self.overflows(new_dimensions) {
+ if self.ctx.extra_space.is_some() {
+ self.finish_layout(false)?;
+ } else {
+ return Err(LayoutError::NotEnoughSpace("cannot fit space into stack"));
+ }
+ } else {
+ self.cursor.y += space;
+ self.dimensions.y += space;
+ }
Ok(())
}
/// Finish the layouting.
- pub fn finish(self) -> Layout {
- Layout {
- dimensions: if self.ctx.space.shrink_to_fit {
- self.dimensions.padded(self.ctx.space.padding)
+ ///
+ /// The layouter is not consumed by this to prevent ownership problems.
+ /// It should not be used further.
+ pub fn finish(&mut self) -> LayoutResult<MultiLayout> {
+ if self.started {
+ self.finish_layout(false)?;
+ }
+ Ok(std::mem::replace(&mut self.layouts, MultiLayout::new()))
+ }
+
+ /// Finish the current layout and start a new one in an extra space
+ /// (if there is an extra space).
+ ///
+ /// If `start_new_empty` is true, a new empty layout will be started. Otherwise,
+ /// the new layout only emerges when new content is added.
+ pub fn finish_layout(&mut self, start_new_empty: bool) -> LayoutResult<()> {
+ let actions = std::mem::replace(&mut self.actions, LayoutActionList::new());
+ self.layouts.add(Layout {
+ dimensions: if self.space.shrink_to_fit {
+ self.dimensions.padded(self.space.padding)
} else {
- self.ctx.space.dimensions
+ self.space.dimensions
},
- actions: self.actions.into_vec(),
+ actions: actions.into_vec(),
debug_render: true,
+ });
+
+ self.started = false;
+
+ if start_new_empty {
+ self.start_new_space()?;
+ }
+
+ Ok(())
+ }
+
+ pub fn start_new_space(&mut self) -> LayoutResult<()> {
+ if let Some(space) = self.ctx.extra_space {
+ self.started = true;
+ self.space = space;
+ self.usable = space.usable();
+ self.dimensions = start_dimensions(space);
+ self.cursor = start_cursor(space);
+ self.in_extra_space = true;
+ Ok(())
+ } else {
+ Err(LayoutError::NotEnoughSpace("no extra space to start"))
}
}
@@ -121,10 +174,30 @@ impl StackLayouter {
/// Whether this layouter contains any items.
pub fn is_empty(&self) -> bool {
- self.actions.is_empty()
+ self.layouts.is_empty() && self.actions.is_empty()
}
fn overflows(&self, dimensions: Size2D) -> bool {
!self.usable.fits(dimensions)
}
}
+
+fn start_dimensions(space: LayoutSpace) -> Size2D {
+ match space.alignment {
+ Alignment::Left => Size2D::zero(),
+ Alignment::Right => Size2D::with_x(space.usable().x),
+ }
+}
+
+fn start_cursor(space: LayoutSpace) -> Size2D {
+ Size2D {
+ // If left-align, the cursor points to the top-left corner of
+ // each box. If we right-align, it points to the top-right
+ // corner.
+ x: match space.alignment {
+ Alignment::Left => space.padding.left,
+ Alignment::Right => space.dimensions.x - space.padding.right,
+ },
+ y: space.padding.top,
+ }
+}
diff --git a/src/layout/tree.rs b/src/layout/tree.rs
index 50616883..c8695e83 100644
--- a/src/layout/tree.rs
+++ b/src/layout/tree.rs
@@ -19,14 +19,13 @@ impl<'a, 'p> TreeLayouter<'a, 'p> {
fn new(ctx: LayoutContext<'a, 'p>) -> TreeLayouter<'a, 'p> {
TreeLayouter {
ctx,
- stack: StackLayouter::new(StackContext { space: ctx.space }),
+ stack: StackLayouter::new(StackContext {
+ space: ctx.space,
+ extra_space: ctx.extra_space
+ }),
flex: FlexLayouter::new(FlexContext {
- space: LayoutSpace {
- dimensions: ctx.space.usable(),
- padding: SizeBox::zero(),
- alignment: ctx.space.alignment,
- shrink_to_fit: true,
- },
+ space: flex_space(ctx.space),
+ extra_space: ctx.extra_space.map(|s| flex_space(s)),
flex_spacing: flex_spacing(&ctx.style),
}),
style: Cow::Borrowed(ctx.style),
@@ -48,10 +47,8 @@ impl<'a, 'p> TreeLayouter<'a, 'p> {
// Finish the current flex layouting process.
Node::Newline => {
- self.layout_flex()?;
-
let space = paragraph_spacing(&self.style);
- self.stack.add_space(space)?;
+ self.layout_flex(space)?;
}
// Toggle the text styles.
@@ -70,12 +67,10 @@ impl<'a, 'p> TreeLayouter<'a, 'p> {
fn finish(mut self) -> LayoutResult<MultiLayout> {
// If there are remainings, add them to the layout.
if !self.flex.is_empty() {
- self.layout_flex()?;
+ self.layout_flex(Size::zero())?;
}
- Ok(MultiLayout {
- layouts: vec![self.stack.finish()],
- })
+ self.stack.finish()
}
/// Add text to the flex layout. If `glue` is true, the text will be a glue
@@ -98,29 +93,38 @@ impl<'a, 'p> TreeLayouter<'a, 'p> {
}
/// Finish the current flex layout and add it the stack.
- fn layout_flex(&mut self) -> LayoutResult<()> {
+ fn layout_flex(&mut self, after_space: Size) -> LayoutResult<()> {
if self.flex.is_empty() {
return Ok(());
}
+ let layouts = self.flex.finish()?;
+ self.stack.add_many(layouts)?;
+ self.stack.add_space(after_space)?;
+
let mut ctx = self.flex.ctx();
ctx.space.dimensions = self.stack.remaining();
ctx.flex_spacing = flex_spacing(&self.style);
- let next = FlexLayouter::new(ctx);
- let flex = std::mem::replace(&mut self.flex, next);
- let boxed = flex.finish()?;
+ self.flex = FlexLayouter::new(ctx);
- self.stack.add(boxed)
+ Ok(())
}
/// Layout a function.
fn layout_func(&mut self, func: &FuncCall) -> LayoutResult<()> {
let mut ctx = self.ctx;
ctx.style = &self.style;
+
ctx.space.dimensions = self.stack.remaining();
ctx.space.padding = SizeBox::zero();
- ctx.space.shrink_to_fit = true;
+ ctx.space.shrink_to_fit = false;
+
+ if let Some(space) = ctx.extra_space.as_mut() {
+ space.dimensions = space.dimensions.unpadded(space.padding);
+ space.padding = SizeBox::zero();
+ space.shrink_to_fit = false;
+ }
let commands = func.body.layout(ctx)?;
@@ -137,6 +141,15 @@ impl<'a, 'p> TreeLayouter<'a, 'p> {
}
}
+fn flex_space(space: LayoutSpace) -> LayoutSpace {
+ LayoutSpace {
+ dimensions: space.usable(),
+ padding: SizeBox::zero(),
+ alignment: space.alignment,
+ shrink_to_fit: true,
+ }
+}
+
fn flex_spacing(style: &TextStyle) -> Size {
(style.line_spacing - 1.0) * Size::pt(style.font_size)
}
diff --git a/src/macros.rs b/src/macros.rs
index 831a37c8..a1c182fb 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -12,6 +12,7 @@ macro_rules! error_type {
impl std::fmt::Display for $err {
fn fmt(&self, $f: &mut std::fmt::Formatter) -> std::fmt::Result {
+ #[allow(unused)]
let $var = self;
$show
}
@@ -22,6 +23,7 @@ macro_rules! error_type {
impl std::error::Error for $err {
// The source method is only generated if an implementation was given.
$(fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ #[allow(unused)]
let $var = self;
$source
})*
diff --git a/src/size.rs b/src/size.rs
index c7439439..414fd855 100644
--- a/src/size.rs
+++ b/src/size.rs
@@ -131,6 +131,15 @@ impl Size2D {
}
}
+ /// Return a [`Size2D`] reduced by the paddings of the given box.
+ #[inline]
+ pub fn unpadded(&self, padding: SizeBox) -> Size2D {
+ Size2D {
+ x: self.x - padding.left - padding.right,
+ y: self.y - padding.top - padding.bottom,
+ }
+ }
+
/// Whether the given [`Size2D`] fits into this one, that is,
/// both coordinate values are smaller.
#[inline]
@@ -189,6 +198,11 @@ debug_display!(Size);
/// An error which can be returned when parsing a size.
pub struct ParseSizeError;
+error_type! {
+ err: ParseSizeError,
+ show: f => write!(f, "failed to parse size"),
+}
+
impl FromStr for Size {
type Err = ParseSizeError;