// SPDX-FileCopyrightText: 2020 Robin Krahl // SPDX-License-Identifier: Apache-2.0 or MIT //! Elements of a PDF document. //! //! This module provides implementations of the [`Element`][] trait that can be used to render and //! arrange text and shapes. //! //! It includes the following elements: //! - Containers: //! - [`LinearLayout`][]: arranges its elements sequentially //! - [`TableLayout`][]: arranges its elements in columns and rows //! - [`OrderedList`][] and [`UnorderedList`][]: arrange their elements sequentially with bullet //! points //! - Text: //! - [`Text`][]: a single line of text //! - [`Paragraph`][]: a wrapped and aligned paragraph of text //! - Wrappers: //! - [`FramedElement`][]: draws a frame around the wrapped element //! - [`PaddedElement`][]: adds a padding to the wrapped element //! - [`StyledElement`][]: sets a default style for the wrapped element and its children //! - Other: //! - [`Image`][]: an image (requires the `images` feature) //! - [`Break`][]: adds forced line breaks as a spacer //! - [`PageBreak`][]: adds a forced page break //! //! You can create custom elements by implementing the [`Element`][] trait. //! //! [`Element`]: ../trait.Element.html //! [`LinearLayout`]: struct.LinearLayout.html //! [`TableLayout`]: struct.TableLayout.html //! [`OrderedList`]: struct.OrderedList.html //! [`UnorderedList`]: struct.UnorderedList.html //! [`Text`]: struct.Text.html //! [`Image`]: struct.Image.html //! [`Break`]: struct.Break.html //! [`PageBreak`]: struct.PageBreak.html //! [`Paragraph`]: struct.Paragraph.html //! [`FramedElement`]: struct.FramedElement.html //! [`PaddedElement`]: struct.PaddedElement.html //! [`StyledElement`]: struct.StyledElement.html #[cfg(feature = "images")] mod images; use std::collections; use std::iter; use std::mem; use crate::error::{Error, ErrorKind}; use crate::render; use crate::style::{Style, StyledString}; use crate::wrap; use crate::{Alignment, Context, Element, Margins, Mm, Position, RenderResult, Size}; #[cfg(feature = "images")] pub use images::Image; /// Arranges a list of elements sequentially. /// /// Currently, elements can only be arranged vertically. /// /// # Examples /// /// With setters: /// ``` /// use genpdf::elements; /// let mut layout = elements::LinearLayout::vertical(); /// layout.push(elements::Paragraph::new("Test1")); /// layout.push(elements::Paragraph::new("Test2")); /// ``` /// /// Chained: /// ``` /// use genpdf::elements; /// let layout = elements::LinearLayout::vertical() /// .element(elements::Paragraph::new("Test1")) /// .element(elements::Paragraph::new("Test2")); /// ``` /// pub struct LinearLayout { elements: Vec>, render_idx: usize, } impl LinearLayout { fn new() -> LinearLayout { LinearLayout { elements: Vec::new(), render_idx: 0, } } /// Creates a new linear layout that arranges its elements vertically. pub fn vertical() -> LinearLayout { LinearLayout::new() } /// Adds the given element to this layout. pub fn push(&mut self, element: E) { self.elements.push(Box::new(element)); } /// Adds the given element to this layout and it returns the layout. pub fn element(mut self, element: E) -> Self { self.push(element); self } fn render_vertical( &mut self, context: &Context, mut area: render::Area<'_>, style: Style, ) -> Result { let mut result = RenderResult::default(); while area.size().height > Mm(0.0) && self.render_idx < self.elements.len() { let element_result = self.elements[self.render_idx].render(context, area.clone(), style)?; area.add_offset(Position::new(0, element_result.size.height)); result.size = result.size.stack_vertical(element_result.size); if element_result.has_more { result.has_more = true; return Ok(result); } self.render_idx += 1; } result.has_more = self.render_idx < self.elements.len(); Ok(result) } } impl Element for LinearLayout { fn render( &mut self, context: &Context, area: render::Area<'_>, style: Style, ) -> Result { // TODO: add horizontal layout self.render_vertical(context, area, style) } } /// A single line of formatted text. /// /// This element renders a single styled string on a single line. It does not wrap it if the /// string is longer than the line. Therefore you should prefer [`Paragraph`][] over `Text` for /// most use cases. /// /// [`Paragraph`]: struct.Paragraph.html #[derive(Clone, Debug, Default)] pub struct Text { text: StyledString, } impl Text { /// Creates a new instance with the given styled string. pub fn new(text: impl Into) -> Text { Text { text: text.into() } } } impl Element for Text { fn render( &mut self, context: &Context, area: render::Area<'_>, mut style: Style, ) -> Result { let mut result = RenderResult::default(); style.merge(self.text.style); if area.print_str( &context.font_cache, Position::default(), style, &self.text.s, )? { result.size = Size::new( style.str_width(&context.font_cache, &self.text.s), style.line_height(&context.font_cache), ); } else { result.has_more = true; } Ok(result) } } /// A multi-line wrapped paragraph of formatted text. /// /// If the text of this paragraph is longer than the page width, the paragraph is wrapped at word /// borders (and additionally at string borders if it contains multiple strings). If a word in the /// paragraph is longer than the page width, the text is truncated. /// /// Use the [`push`][], [`string`][], [`push_styled`][] and [`string_styled`][] methods to add /// strings to this paragraph. Besides the styling of the text (see [`Style`][]), you can also set /// an [`Alignment`][] for the paragraph. /// /// Note that the line height and spacing is currently calculated based on the style of the entire /// paragraph. If the font family or font size is changed in the [`Style`][] settings for a /// string, the line height and spacing might be incorrect. /// /// # Examples /// /// With setters: /// ``` /// use genpdf::{elements, style}; /// let mut p = elements::Paragraph::default(); /// p.push("This is an "); /// p.push_styled("important", style::Color::Rgb(255, 0, 0)); /// p.push(" message!"); /// p.set_alignment(genpdf::Alignment::Center); /// ``` /// /// Chained: /// ``` /// use genpdf::{elements, style}; /// let p = elements::Paragraph::default() /// .string("This is an ") /// .styled_string("important", style::Color::Rgb(255, 0, 0)) /// .string(" message!") /// .aligned(genpdf::Alignment::Center); /// ``` /// /// [`Style`]: ../style/struct.Style.html /// [`Alignment`]: ../enum.Alignment.html /// [`push`]: #method.push /// [`push_styled`]: #method.push_styled /// [`string`]: #method.string /// [`string_styled`]: #method.string_styled #[derive(Clone, Debug, Default)] pub struct Paragraph { text: Vec, words: collections::VecDeque, style_applied: bool, alignment: Alignment, } impl Paragraph { /// Creates a new paragraph with the given content. pub fn new(text: impl Into) -> Paragraph { Paragraph { text: vec![text.into()], ..Default::default() } } /// Sets the alignment of this paragraph. pub fn set_alignment(&mut self, alignment: Alignment) { self.alignment = alignment; } /// Sets the alignment of this paragraph and returns the paragraph. pub fn aligned(mut self, alignment: Alignment) -> Self { self.set_alignment(alignment); self } /// Adds a string to the end of this paragraph. pub fn push(&mut self, s: impl Into) { self.text.push(s.into()); } /// Adds a string to the end of this paragraph and returns the paragraph. pub fn string(mut self, s: impl Into) -> Self { self.push(s); self } /// Adds a string with the given style to the end of this paragraph. pub fn push_styled(&mut self, s: impl Into, style: impl Into