use super::styles::Color;
use crate::model::Span;
use std::collections::{BTreeMap, HashMap, HashSet};
const SQUARE_CELLS: u32 = 64;
#[derive(Debug, Clone, Default)]
pub(crate) struct Grid {
pub(crate) cells: BTreeMap<(u32, u32), GridCell>,
pub(crate) merges: Vec<Merge>,
pub(crate) hidden_rows: HashSet<u32>,
pub(crate) hidden_cols: HashSet<u32>,
pub(crate) declared: Declared,
pub(crate) truncated_rows: u32,
pub(crate) truncated_cols: u32,
pub(crate) sizes: Sizes,
pub(crate) cell_pictures: Vec<((u32, u32), u32)>,
pub(crate) paper_pt: Option<(f32, f32)>,
pub(crate) looks: Looks,
pub(crate) print: PrintSetup,
pub(crate) objects: Vec<EmbeddedObject>,
pub(crate) legacy_objects: Vec<u32>,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct EmbeddedObject {
pub(crate) rel: String,
pub(crate) from: Marker,
pub(crate) to: Marker,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct PrintSetup {
pub(crate) landscape: bool,
pub(crate) scale_percent: f32,
pub(crate) fit_to_page: bool,
pub(crate) fit_width: u32,
pub(crate) fit_height: u32,
pub(crate) margins_pt: [f32; 4],
pub(crate) h_centered: bool,
pub(crate) v_centered: bool,
pub(crate) grid_lines: bool,
pub(crate) headings: bool,
pub(crate) black_and_white: bool,
pub(crate) over_then_down: bool,
pub(crate) row_breaks: Vec<u32>,
pub(crate) col_breaks: Vec<u32>,
pub(crate) header_pt: f32,
pub(crate) footer_pt: f32,
pub(crate) header_footer: HeaderFooter,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct HeaderFooter {
pub(crate) odd_header: String,
pub(crate) odd_footer: String,
pub(crate) even_header: String,
pub(crate) even_footer: String,
pub(crate) first_header: String,
pub(crate) first_footer: String,
pub(crate) different_odd_even: bool,
pub(crate) different_first: bool,
pub(crate) scale_with_doc: bool,
pub(crate) align_with_margins: bool,
}
impl Default for HeaderFooter {
fn default() -> Self {
Self {
odd_header: String::new(),
odd_footer: String::new(),
even_header: String::new(),
even_footer: String::new(),
first_header: String::new(),
first_footer: String::new(),
different_odd_even: false,
different_first: false,
scale_with_doc: true,
align_with_margins: true,
}
}
}
impl HeaderFooter {
pub(crate) fn of_page(&self, number: usize) -> (&str, &str) {
if self.different_first && number == 1 {
(&self.first_header, &self.first_footer)
} else if self.different_odd_even && number.is_multiple_of(2) {
(&self.even_header, &self.even_footer)
} else {
(&self.odd_header, &self.odd_footer)
}
}
}
const PT_PER_INCH: f32 = 72.0;
impl Default for PrintSetup {
fn default() -> Self {
Self {
landscape: false,
scale_percent: 100.0,
fit_to_page: false,
fit_width: 1,
fit_height: 1,
margins_pt: [
0.7 * PT_PER_INCH,
0.7 * PT_PER_INCH,
0.75 * PT_PER_INCH,
0.75 * PT_PER_INCH,
],
h_centered: false,
v_centered: false,
grid_lines: false,
black_and_white: false,
headings: false,
over_then_down: false,
row_breaks: Vec::new(),
col_breaks: Vec::new(),
header_pt: 0.3 * PT_PER_INCH,
footer_pt: 0.3 * PT_PER_INCH,
header_footer: HeaderFooter::default(),
}
}
}
#[derive(Debug, Clone, Default)]
pub(crate) struct Looks {
pub(crate) cells: BTreeMap<(u32, u32), u32>,
pub(crate) cols: Vec<(u32, u32, u32)>,
pub(crate) rows: BTreeMap<u32, u32>,
pub(crate) no_grid_lines: bool,
pub(crate) custom_rows: HashSet<u32>,
pub(crate) conditional: BTreeMap<(u32, u32), Vec<u32>>,
pub(crate) bars: BTreeMap<(u32, u32), DataBar>,
pub(crate) fills: BTreeMap<(u32, u32), usize>,
pub(crate) number_colors: BTreeMap<(u32, u32), u8>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct DataBar {
pub(crate) length: f32,
pub(crate) color: Color,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub(crate) struct Sizes {
pub(crate) cols: Vec<(u32, u32, f32)>,
pub(crate) rows: BTreeMap<u32, f32>,
pub(crate) default_col_pt: Option<f32>,
pub(crate) default_row_pt: Option<f32>,
pub(crate) screen_digit_pt: f32,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct Marker {
pub(crate) row: u32,
pub(crate) row_off_pt: f32,
pub(crate) col: u32,
pub(crate) col_off_pt: f32,
}
pub(crate) const DEFAULT_COL_PT: f32 = 48.0;
const DEFAULT_ROW_PT: f32 = 15.0;
impl Sizes {
pub(crate) fn col_pt(&self, col: u32) -> f32 {
self.cols
.iter()
.rev()
.find(|(first, last, _)| (*first..=*last).contains(&col))
.map_or(self.default_col_pt.unwrap_or(DEFAULT_COL_PT), |(_, _, width)| *width)
}
pub(crate) fn span_pt(
&self,
from: Marker,
to: Marker,
hidden_rows: &HashSet<u32>,
hidden_cols: &HashSet<u32>,
) -> (f32, f32) {
let col_width = |col: u32| {
if hidden_cols.contains(&col) {
return 0.0;
}
self.col_pt(col)
};
let default_row = self.default_row_pt.unwrap_or(DEFAULT_ROW_PT);
let rows_between = |first: u32, last: u32| -> f32 {
if last <= first {
return 0.0;
}
let mut height = (last - first) as f32 * default_row;
for (row, row_height) in self.rows.range(first..last) {
height += if hidden_rows.contains(row) { 0.0 } else { *row_height } - default_row;
}
height -= hidden_rows
.iter()
.filter(|row| (first..last).contains(*row) && !self.rows.contains_key(row))
.count() as f32
* default_row;
height
};
let cols_between = |first: u32, last: u32| -> f32 {
(first..last.min(first.saturating_add(MAX_SHEET_COLS)))
.map(col_width)
.sum()
};
let row_height = |row: u32| rows_between(row, row.saturating_add(1));
let col_off = |marker: Marker| marker.col_off_pt.clamp(0.0, col_width(marker.col));
let row_off = |marker: Marker| marker.row_off_pt.clamp(0.0, row_height(marker.row));
let width = cols_between(from.col, to.col) - col_off(from) + col_off(to);
let height = rows_between(from.row, to.row) - row_off(from) + row_off(to);
(width.max(0.0), height.max(0.0))
}
}
const MAX_SHEET_COLS: u32 = 16_384;
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct GridCell {
pub(crate) spans: Vec<Span>,
pub(crate) kind: CellKind,
pub(crate) center_continuous: bool,
pub(crate) own_font: Vec<u8>,
pub(crate) overflow_pt: f32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub(crate) struct OwnFont {
pub(crate) style: bool,
pub(crate) color: bool,
pub(crate) underline: bool,
}
impl GridCell {
pub(crate) fn own_font(&self, index: usize) -> OwnFont {
let own = self.own_font.get(index).copied().unwrap_or(0);
OwnFont {
style: own & super::strings::OWN_STYLE != 0,
color: own & super::strings::OWN_COLOR != 0,
underline: own & super::strings::RUN_UNDERLINE != 0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum CellKind {
Number,
Date,
Text,
Bool,
Error,
}
pub(crate) type Table = (Merge, u32);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Merge {
pub(crate) top: u32,
pub(crate) left: u32,
pub(crate) bottom: u32,
pub(crate) right: u32,
}
impl Grid {
pub(crate) fn is_blank(&self, at: (u32, u32)) -> bool {
self.cells.get(&at).is_none_or(|cell| cell.spans.is_empty())
}
pub(crate) fn selection_width<'c>(
&self,
at: (u32, u32),
after: impl IntoIterator<Item = &'c u32>,
width: impl Fn(u32) -> f32,
) -> f32 {
if !self.cells.get(&at).is_some_and(|cell| cell.center_continuous) || self.merged(at) {
return 0.0;
}
after
.into_iter()
.take_while(|&&col| {
self.is_blank((at.0, col))
&& self.cells.get(&(at.0, col)).is_some_and(|cell| cell.center_continuous)
&& !self.merged((at.0, col))
})
.map(|&col| width(col))
.sum()
}
pub(crate) fn merged(&self, (row, col): (u32, u32)) -> bool {
self.merges
.iter()
.any(|merge| (merge.top..=merge.bottom).contains(&row) && (merge.left..=merge.right).contains(&col))
}
}
impl Merge {
pub(crate) fn overlaps(&self, other: &Self) -> bool {
self.top <= other.bottom && other.top <= self.bottom && self.left <= other.right && other.left <= self.right
}
}
#[derive(Default)]
pub(crate) struct MergeIndex {
ranges: Vec<Merge>,
squares: HashMap<(u32, u32), Vec<usize>>,
}
impl MergeIndex {
pub(crate) fn new(ranges: &[Merge]) -> Self {
let mut index = Self::default();
for range in ranges {
index.add(*range);
}
index
}
pub(crate) fn add(&mut self, range: Merge) {
for square in squares(&range) {
self.squares.entry(square).or_default().push(self.ranges.len());
}
self.ranges.push(range);
}
pub(crate) fn into_ranges(self) -> Vec<Merge> {
self.ranges
}
pub(crate) fn overlapping(&self, area: &Merge) -> Vec<Merge> {
let mut found: Vec<usize> = squares(area)
.filter_map(|square| self.squares.get(&square))
.flatten()
.copied()
.filter(|&index| self.ranges[index].overlaps(area))
.collect();
found.sort_unstable();
found.dedup();
found.into_iter().map(|index| self.ranges[index]).collect()
}
}
fn squares(range: &Merge) -> impl Iterator<Item = (u32, u32)> + use<> {
let rows = range.top / SQUARE_CELLS..=range.bottom / SQUARE_CELLS;
let cols = range.left / SQUARE_CELLS..=range.right / SQUARE_CELLS;
rows.flat_map(move |row| cols.clone().map(move |col| (row, col)))
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Declared {
pub(crate) tables: Vec<Table>,
pub(crate) auto_filter: Option<Merge>,
pub(crate) frozen_rows: u32,
pub(crate) print_title_rows: Option<(u32, u32)>,
pub(crate) print_title_cols: Option<(u32, u32)>,
}