return Ok(());
};
- for y in 0..table.n.1 {
- for x in 0..table.n.0 {
+ for y in 0..table.n.y {
+ for x in 0..table.n.x {
if x > 0 {
write!(&mut self.file, "{}", self.options.delimiter)?;
}
- let coord = CellPos(x, y);
+ let coord = CellPos { x, y };
let content = table.get(coord);
if content.is_top_left() {
let display = content.inner().value.display(pivot_table);
writeln!(&mut self.writer, ">")?;
if let Some(title) = output.title {
- let cell = title.get(CellPos(0, 0));
+ let cell = title.get(CellPos::new(0, 0));
self.put_cell(
DrawCell::new(cell.inner(), &title),
CellRect::new(0..1, 0..1),
}
writeln!(&mut self.writer, "<tbody>")?;
- for y in 0..output.body.n.1 {
+ for y in 0..output.body.n.y {
writeln!(&mut self.writer, "<tr>")?;
for x in output.body.iter_x(y) {
- let cell = output.body.get(CellPos(x, y));
+ let cell = output.body.get(CellPos { x, y });
if cell.is_top_left() {
let is_header = x < output.body.h[Axis2::X] || y < output.body.h[Axis2::Y];
let tag = if is_header { "th" } else { "td" };
writeln!(&mut self.writer, "<tr>")?;
if let Some(caption) = output.caption {
self.put_cell(
- DrawCell::new(caption.get(CellPos(0, 0)).inner(), &caption),
+ DrawCell::new(caption.get(CellPos::new(0, 0)).inner(), &caption),
CellRect::new(0..output.body.n[Axis2::X], 0..1),
"td",
None,
if rect.x.end == table.n[Axis2::X] {
Self::put_border(
&mut style,
- table.get_rule(Axis2::X, CellPos(rect.x.end, rect.y.start)),
+ table.get_rule(Axis2::X, CellPos::new(rect.x.end, rect.y.start)),
"right",
);
}
if rect.y.end == table.n[Axis2::Y] {
Self::put_border(
&mut style,
- table.get_rule(Axis2::Y, CellPos(rect.x.start, rect.y.end)),
+ table.get_rule(Axis2::Y, CellPos::new(rect.x.start, rect.y.end)),
"bottom",
);
}
I: Iterator<Item = Box<Value>> + ExactSizeIterator,
{
let mut table = Table::new(
- CellPos(1, rows.len()),
- CellPos(0, 0),
+ CellPos::new(1, rows.len()),
+ CellPos::new(0, 0),
self.style.look.areas.clone(),
self.borders(false),
self.into_value_options(),
);
for (y, row) in rows.enumerate() {
- table.put(CellRect::for_cell(CellPos(0, y)), CellInner::new(area, row));
+ table.put(
+ CellRect::for_cell(CellPos::new(0, y)),
+ CellInner::new(area, row),
+ );
}
table
}
let data = CellPos::from_fn(|axis| headings[axis].width());
let mut stub = CellPos::from_fn(|axis| headings[!axis].height());
- if headings[Axis2::Y].row_label_position == LabelPosition::Corner && stub.1 == 0 {
+ if headings[Axis2::Y].row_label_position == LabelPosition::Corner && stub.y == 0 {
stub[Axis2::Y] = 1;
}
let mut body = Table::new(
// are not in the corner.
if self.metadata.corner_text.is_some()
&& self.style.look.row_label_position == LabelPosition::Nested
- && stub.0 > 0
- && stub.0 > 0
+ && stub.x > 0
+ && stub.y > 0
{
body.put(
- CellRect::new(0..stub.0, 0..stub.1),
+ CellRect::new(0..stub.x, 0..stub.y),
CellInner::new(
Area::Corner,
self.metadata.corner_text.clone().unwrap_or_default(),
);
}
- if body.n.0 > 0 && body.n.1 > 0 {
- body.h_line(Border::InnerFrame(BoxBorder::Top), 0..body.n.0, 0);
- body.h_line(Border::InnerFrame(BoxBorder::Bottom), 0..body.n.0, body.n.1);
- body.v_line(Border::InnerFrame(BoxBorder::Left), 0, 0..body.n.1);
- body.v_line(Border::InnerFrame(BoxBorder::Right), body.n.0, 0..body.n.1);
+ if body.n.x > 0 && body.n.y > 0 {
+ body.h_line(Border::InnerFrame(BoxBorder::Top), 0..body.n.x, 0);
+ body.h_line(Border::InnerFrame(BoxBorder::Bottom), 0..body.n.x, body.n.y);
+ body.v_line(Border::InnerFrame(BoxBorder::Left), 0, 0..body.n.y);
+ body.v_line(Border::InnerFrame(BoxBorder::Right), body.n.x, 0..body.n.y);
- body.h_line(Border::DataTop, 0..body.n.0, stub.1);
- body.v_line(Border::DataLeft, stub.0, 0..body.n.1);
+ body.h_line(Border::DataTop, 0..body.n.x, stub.y);
+ body.v_line(Border::DataLeft, stub.x, 0..body.n.y);
}
body
}
// Calculate minimum and maximum widths of cells that do not span
// multiple columns.
- let mut unspanned_columns = EnumMap::from_fn(|_| vec![0; n.0]);
+ let mut unspanned_columns = EnumMap::from_fn(|_| vec![0; n.x]);
for cell in table.cells().filter(|cell| cell.col_span() == 1) {
let mut w = device.measure_cell_width(&DrawCell::new(cell.inner(), &table));
if device.params().px_size.is_some() {
// In pathological cases, spans can cause the minimum width of a column
// to exceed the maximum width. This bollixes our interpolation
// algorithm later, so fix it up.
- for i in 0..n.0 {
+ for i in 0..n.x {
if columns[Min][i] > columns[Max][i] {
columns[Max][i] = columns[Min][i];
}
let w = joined_width(&cp_x, rect.x.clone());
let h = device.measure_cell_height(&DrawCell::new(cell.inner(), &table), w);
- let row = &mut unspanned_rows[cell.pos.1];
+ let row = &mut unspanned_rows[cell.pos.y];
if h > *row {
*row = h;
}
let mut x = cells.x.start;
while x < cells.x.end {
if !is_rule(x) && !is_rule(y) {
- let cell = self.get_cell(CellPos(x / 2, y / 2));
+ let cell = self.get_cell(CellPos::new(x / 2, y / 2));
self.draw_cell(device, ofs, &cell);
x = rule_ofs(cell.rect.x.end);
} else {
for y in cells.y.clone() {
for x in cells.x.clone() {
if is_rule(x) || is_rule(y) {
- self.draw_rule(device, ofs, CellPos(x, y));
+ self.draw_rule(device, ofs, CellPos { x, y });
}
}
}
if self.axis == Axis2::Y && device.params().can_adjust_break {
let mut x = 0;
while x < self.page.n[Axis2::X] {
- let cell = self.page.get_cell(CellPos(x, z));
+ let cell = self.page.get_cell(CellPos::new(x, z));
let better_pixel = device.adjust_break(
cell.content,
- Coord2::new(
- self.page
- .joined_width(Axis2::X, cell.rect.x.clone()),
- pixel,
- ),
+ Coord2::new(self.page.joined_width(Axis2::X, cell.rect.x.clone()), pixel),
);
x += cell.rect.x.len();
/// The `(x,y)` position of a cell in a [Table].
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
-pub struct CellPos(
+pub struct CellPos {
/// X
- pub usize,
+ pub x: usize,
/// Y
- pub usize,
-);
+ pub y: usize,
+}
impl CellPos {
+ pub fn new(x: usize, y: usize) -> Self {
+ Self { x, y }
+ }
pub fn for_axis((a, az): (Axis2, usize), bz: usize) -> Self {
match a {
- Axis2::X => Self(az, bz),
- Axis2::Y => Self(bz, az),
+ Axis2::X => Self::new(az, bz),
+ Axis2::Y => Self::new(bz, az),
}
}
where
F: FnMut(Axis2) -> usize,
{
- Self(f(Axis2::X), f(Axis2::Y))
+ Self::new(f(Axis2::X), f(Axis2::Y))
}
}
fn index(&self, index: Axis2) -> &Self::Output {
match index {
- Axis2::X => &self.0,
- Axis2::Y => &self.1,
+ Axis2::X => &self.x,
+ Axis2::Y => &self.y,
}
}
}
impl IndexMut<Axis2> for CellPos {
fn index_mut(&mut self, index: Axis2) -> &mut Self::Output {
match index {
- Axis2::X => &mut self.0,
- Axis2::Y => &mut self.1,
+ Axis2::X => &mut self.x,
+ Axis2::Y => &mut self.y,
}
}
}
/// A rectangular group of cells in a [Table].
#[derive(Clone, Debug, Default)]
pub struct CellRect {
+ /// X range.
pub x: Range<usize>,
+ /// Y range.
pub y: Range<usize>,
}
Self { x, y }
}
pub fn for_cell(cell: CellPos) -> Self {
- Self::new(cell.0..cell.0 + 1, cell.1..cell.1 + 1)
+ Self::new(cell.x..cell.x + 1, cell.y..cell.y + 1)
}
pub fn for_ranges((a_axis, a): (Axis2, Range<usize>), b: Range<usize>) -> Self {
match a_axis {
}
}
pub fn top_left(&self) -> CellPos {
- CellPos(self.x.start, self.y.start)
+ CellPos::new(self.x.start, self.y.start)
}
pub fn is_empty(&self) -> bool {
self.x.is_empty() || self.y.is_empty()
}
pub fn next_x(&self) -> usize {
- self.content.next_x(self.pos.0)
+ self.content.next_x(self.pos.x)
}
pub fn is_top_left(&self) -> bool {
Self {
n,
h: headers,
- contents: Array::default((n.0, n.1)),
+ contents: Array::default((n.x, n.y)),
areas,
borders,
rules: enum_map! {
- Axis2::X => Array::default((n.0 + 1, n.1)),
- Axis2::Y => Array::default((n.0, n.1 + 1)),
+ Axis2::X => Array::default((n.x + 1, n.y)),
+ Axis2::Y => Array::default((n.x, n.y + 1)),
},
value_options,
}
pub fn get(&self, coord: CellPos) -> CellRef<'_> {
CellRef {
pos: coord,
- content: &self.contents[[coord.0, coord.1]],
+ content: &self.contents[[coord.x, coord.y]],
}
}
pub fn get_rule(&self, axis: Axis2, pos: CellPos) -> BorderStyle {
- self.rules[axis][[pos.0, pos.1]].map_or(BorderStyle::none(), |b| self.borders[b])
+ self.rules[axis][[pos.x, pos.y]].map_or(BorderStyle::none(), |b| self.borders[b])
}
pub fn put(&mut self, region: CellRect, inner: CellInner) {
/// The heading region that `pos` is part of, if any.
pub fn heading_region(&self, pos: CellPos) -> Option<HeadingRegion> {
- if pos.0 < self.h.0 {
+ if pos.x < self.h.x {
Some(HeadingRegion::Rows)
- } else if pos.1 < self.h.1 {
+ } else if pos.y < self.h.y {
Some(HeadingRegion::Columns)
} else {
None
fn next(&mut self) -> Option<Self::Item> {
let next_x = self
.x
- .map_or(0, |x| self.table.get(CellPos(x, self.y)).next_x());
- if next_x >= self.table.n.0 {
+ .map_or(0, |x| self.table.get(CellPos::new(x, self.y)).next_x());
+ if next_x >= self.table.n.x {
None
} else {
self.x = Some(next_x);
next: if table.is_empty() {
None
} else {
- Some(table.get(CellPos(0, 0)))
+ Some(table.get(CellPos::new(0, 0)))
},
}
}
self.next = loop {
let next_x = next.next_x();
let coord = if next_x < self.table.n[X] {
- CellPos(next_x, next.pos.1)
- } else if next.pos.1 + 1 < self.table.n[Y] {
- CellPos(0, next.pos.1 + 1)
+ CellPos::new(next_x, next.pos.y)
+ } else if next.pos.y + 1 < self.table.n[Y] {
+ CellPos::new(0, next.pos.y + 1)
} else {
break None;
};