pub struct TableLayout {
column_weights: Vec<usize>,
rows: Vec<Vec<Box<dyn Element>>>,
render_idx: usize,
cell_decorator: Option<Box<dyn CellDecorator>>,
}Expand description
Arranges elements in columns and rows.
This struct can be used to layout arbitrary elements in columns in rows, or to draw typical
tables. You can customize the cell style by providing a CellDecorator implementation.
If you want to print a typical table with borders around the cells, use the
FrameCellDecorator.
The column widths are determined by the weights that have been set in the constructor. The table always uses the full width of the provided area.
§Examples
With setters:
use genpdfi_extended::elements;
let mut table = elements::TableLayout::new(vec![1, 1]);
table.set_cell_decorator(elements::FrameCellDecorator::new(true, true, false));
let mut row = table.row();
row.push_element(elements::Paragraph::new("Cell 1"));
row.push_element(elements::Paragraph::new("Cell 2"));
row.push().expect("Invalid table row");Chained:
use genpdfi_extended::elements;
let table = elements::TableLayout::new(vec![1, 1])
.row()
.element(elements::Paragraph::new("Cell 1"))
.element(elements::Paragraph::new("Cell 2"))
.push()
.expect("Invalid table row");Example: render a simple table using an embedded font from the fonts/ directory
(this example is runnable in tests and CI because it uses the bundled fonts):
use genpdfi_extended::{elements, style, Document, fonts};
// Load a font family from the bundled `fonts/` directory and create a document
let family = fonts::from_files(concat!(env!("CARGO_MANIFEST_DIR"), "/fonts"), "NotoSans", None)
.expect("Failed to load font family");
let mut doc = Document::new(family);
// Construct a simple table and add it to the document
let mut table = elements::TableLayout::new(vec![1, 2]);
table.row()
.element(elements::Paragraph::new("Left cell with embedded font"))
.element(elements::Paragraph::new("Right cell with more width"))
.push().expect("push");
doc.push(table);
// Render to an in-memory buffer (no files written)
let mut buf = Vec::new();
let _render_results = doc.render(&mut buf).expect("render document");
assert!(!buf.is_empty());Example: render a table using a PDF builtin font (Helvetica) so viewers use device fonts:
use genpdfi_extended::{elements, style, Document, fonts};
use genpdfi_extended::fonts::Builtin;
// Load a font family and mark it as a builtin family (metrics are read from files but
// the PDF will reference the builtin font names so viewers can use device fonts).
let family = fonts::from_files(concat!(env!("CARGO_MANIFEST_DIR"), "/fonts"), "SpaceMono", Some(Builtin::Helvetica))
.expect("Failed to load builtin-like family");
let mut doc = Document::new(family);
let mut table = elements::TableLayout::new(vec![1, 1]);
table.row()
.element(elements::Paragraph::new("Left with builtin"))
.element(elements::Paragraph::new("Right with builtin"))
.push().expect("push");
doc.push(table);
let mut buf = Vec::new();
let _render_results = doc.render(&mut buf).expect("render document builtin");
assert!(!buf.is_empty());Fields§
§column_weights: Vec<usize>§rows: Vec<Vec<Box<dyn Element>>>§render_idx: usize§cell_decorator: Option<Box<dyn CellDecorator>>Implementations§
Source§impl TableLayout
impl TableLayout
Sourcepub fn new(column_weights: Vec<usize>) -> TableLayout
pub fn new(column_weights: Vec<usize>) -> TableLayout
Creates a new table layout with the given column weights.
The column weights are used to determine the relative width of the columns. The number of column weights determines the number of columns in the table.
Sourcepub fn set_cell_decorator(&mut self, decorator: impl CellDecorator + 'static)
pub fn set_cell_decorator(&mut self, decorator: impl CellDecorator + 'static)
Sets the cell decorator for this table.
Sourcepub fn row(&mut self) -> TableLayoutRow<'_>
pub fn row(&mut self) -> TableLayoutRow<'_>
Adds a row to this table using the TableLayoutRow helper struct.
Sourcepub fn push_row(&mut self, row: Vec<Box<dyn Element>>) -> Result<(), Error>
pub fn push_row(&mut self, row: Vec<Box<dyn Element>>) -> Result<(), Error>
Adds a row to this table.
The number of elements in the given vector must match the number of columns. Otherwise, an error is returned.
fn render_row( &mut self, context: &Context, area: Area<'_>, style: Style, ) -> Result<RenderResult, Error>
Trait Implementations§
Source§impl Element for TableLayout
impl Element for TableLayout
Source§fn render(
&mut self,
context: &Context,
area: Area<'_>,
style: Style,
) -> Result<RenderResult, Error>
fn render( &mut self, context: &Context, area: Area<'_>, style: Style, ) -> Result<RenderResult, Error>
Source§fn framed(self, line_style: impl Into<LineStyle>) -> FramedElement<Self>where
Self: Sized,
fn framed(self, line_style: impl Into<LineStyle>) -> FramedElement<Self>where
Self: Sized,
Auto Trait Implementations§
impl Freeze for TableLayout
impl !RefUnwindSafe for TableLayout
impl !Send for TableLayout
impl !Sync for TableLayout
impl Unpin for TableLayout
impl !UnwindSafe for TableLayout
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<E> IntoBoxedElement for Ewhere
E: Element + 'static,
impl<E> IntoBoxedElement for Ewhere
E: Element + 'static,
Source§fn into_boxed_element(self) -> Box<dyn Element>
fn into_boxed_element(self) -> Box<dyn Element>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for T
§impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
§fn to_owned_obj(&self, data: FontData<'_>) -> U
fn to_owned_obj(&self, data: FontData<'_>) -> U
T, using the provided data to resolve any offsets.