kakemono 0.24.2

Convert PDF, Word, Excel, PowerPoint and EPUB documents and web pages to HTML, Markdown, EPUB, LaTeX and JSON in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//! A sheet as read from its part: the cells with their text, the merged
//! ranges, the hidden rows and columns, and what the file declares about
//! its tables. Rows and columns are zero-based.

use super::styles::Color;
use crate::model::Span;
use std::collections::{BTreeMap, HashMap, HashSet};

/// Rows and columns of a square of the sheet that [`MergeIndex`] files
/// ranges by.
const SQUARE_CELLS: u32 = 64;

/// The cells and declarations of one sheet.
#[derive(Debug, Clone, Default)]
pub(crate) struct Grid {
    /// The cells that have a value, by (row, column). An empty cell is kept
    /// only when it centres across the selection, since the text to its
    /// left spans it then.
    pub(crate) cells: BTreeMap<(u32, u32), GridCell>,
    /// The merged ranges, none overlapping another (of two that overlap,
    /// the first in the file is kept).
    pub(crate) merges: Vec<Merge>,
    /// The hidden rows.
    pub(crate) hidden_rows: HashSet<u32>,
    /// The hidden columns.
    pub(crate) hidden_cols: HashSet<u32>,
    /// What the file says about the tables of the sheet.
    pub(crate) declared: Declared,
    /// The rows with a value left out because they are past the limit of
    /// rows (`ExcelConfig::max_rows`).
    pub(crate) truncated_rows: u32,
    /// The columns with a value left out because they are past the limit
    /// of columns (`ExcelConfig::max_cols`), in the rows read.
    pub(crate) truncated_cols: u32,
    /// The widths of the columns and the heights of the rows, where the
    /// drawings of the sheet are placed.
    pub(crate) sizes: Sizes,
    /// The cells that show a picture of their own (Excel 365's "Place in
    /// cell"): their place and their value metadata (`vm`, from 1).
    pub(crate) cell_pictures: Vec<((u32, u32), u32)>,
    /// The paper the sheet is printed on (`pageSetup paperSize`): width
    /// and height in points; `None` when the file names none, or one of
    /// the sizes not known here.
    pub(crate) paper_pt: Option<(f32, f32)>,
    /// How the sheet looks, read only for the sheet view.
    pub(crate) looks: Looks,
    /// How the sheet is printed.
    pub(crate) print: PrintSetup,
    /// The embedded objects (a Word document, ...) shown by a picture.
    pub(crate) objects: Vec<EmbeddedObject>,
    /// The shapes (`shapeId`) of the embedded objects written as before
    /// Excel 2010, without a picture and anchor of their own: the sheet's
    /// VML drawing places their picture.
    pub(crate) legacy_objects: Vec<u32>,
}

/// An object embedded in a sheet (`oleObject`), which the sheet shows by
/// a picture of it (`objectPr`).
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct EmbeddedObject {
    /// The relationship (in the sheet part) of the picture.
    pub(crate) rel: String,
    /// The top left corner of its anchor.
    pub(crate) from: Marker,
    /// The bottom right corner of its anchor.
    pub(crate) to: Marker,
}

/// How a sheet is printed (`pageSetup`, `pageMargins`, `printOptions`,
/// `pageSetUpPr`, `rowBreaks`, `colBreaks`).
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct PrintSetup {
    /// The paper is turned: landscape (`orientation`).
    pub(crate) landscape: bool,
    /// The scale, in percent (`scale`).
    pub(crate) scale_percent: f32,
    /// The sheet is fitted to pages (`pageSetUpPr fitToPage`).
    pub(crate) fit_to_page: bool,
    /// The most pages wide it is fitted to; 0 for no limit.
    pub(crate) fit_width: u32,
    /// The most pages tall it is fitted to; 0 for no limit.
    pub(crate) fit_height: u32,
    /// The margins in points: left, right, top, bottom.
    pub(crate) margins_pt: [f32; 4],
    /// Centred across the page.
    pub(crate) h_centered: bool,
    /// Centred down the page.
    pub(crate) v_centered: bool,
    /// The gridlines are printed.
    pub(crate) grid_lines: bool,
    /// The row numbers and column letters are printed (`headings`).
    pub(crate) headings: bool,
    /// Printed in black and white (`blackAndWhite`): cells without their
    /// fills, their text and borders black.
    pub(crate) black_and_white: bool,
    /// Pages go across first, then down (`pageOrder="overThenDown"`).
    pub(crate) over_then_down: bool,
    /// The rows that start a page (manual breaks), 0-based.
    pub(crate) row_breaks: Vec<u32>,
    /// The columns that start a page (manual breaks), 0-based.
    pub(crate) col_breaks: Vec<u32>,
    /// The distance from the paper's top edge to the header, in points.
    pub(crate) header_pt: f32,
    /// The distance from the paper's bottom edge to the footer, in points.
    pub(crate) footer_pt: f32,
    /// The headers and footers printed on its pages.
    pub(crate) header_footer: HeaderFooter,
}

/// The headers and footers of a sheet's pages (`headerFooter`), in Excel's
/// codes (`&L`, `&C`, `&R` for the sections, `&P` for the page number,
/// ...); empty when a page has none.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct HeaderFooter {
    /// The header of every page (`oddHeader`), or of odd pages.
    pub(crate) odd_header: String,
    /// The footer of every page (`oddFooter`), or of odd pages.
    pub(crate) odd_footer: String,
    /// The header of even pages, when they have their own.
    pub(crate) even_header: String,
    /// The footer of even pages, when they have their own.
    pub(crate) even_footer: String,
    /// The header of the first page, when it has its own.
    pub(crate) first_header: String,
    /// The footer of the first page, when it has its own.
    pub(crate) first_footer: String,
    /// Even pages have their own (`differentOddEven`).
    pub(crate) different_odd_even: bool,
    /// The first page has its own (`differentFirst`).
    pub(crate) different_first: bool,
    /// They are scaled with the sheet (`scaleWithDoc`).
    pub(crate) scale_with_doc: bool,
    /// Their sides are at the page's margins (`alignWithMargins`).
    pub(crate) align_with_margins: bool,
}

impl Default for HeaderFooter {
    /// None, scaled with the sheet and at its margins, as Excel's.
    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 {
    /// The header and footer of page `number` (from 1).
    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)
        }
    }
}

/// Points in an inch: the unit of `pageMargins`.
const PT_PER_INCH: f32 = 72.0;

impl Default for PrintSetup {
    /// Excel's defaults: portrait at 100 %, its "normal" margins.
    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(),
        }
    }
}

/// The cell formats of a sheet's cells, rows and columns, and whether it
/// shows its gridlines: what the sheet view draws, read only for it.
#[derive(Debug, Clone, Default)]
pub(crate) struct Looks {
    /// The cell format (`s`, an index of the styles) of each cell that
    /// has one other than 0, with a value or not.
    pub(crate) cells: BTreeMap<(u32, u32), u32>,
    /// The cell format of the columns that have one (`col style`): first
    /// and last column, format.
    pub(crate) cols: Vec<(u32, u32, u32)>,
    /// The cell format of the rows that have one (`row s`, with
    /// `customFormat`).
    pub(crate) rows: BTreeMap<u32, u32>,
    /// The sheet hides its gridlines (`sheetView showGridLines="0"`).
    pub(crate) no_grid_lines: bool,
    /// The rows whose height is set by hand (`customHeight`); Excel fits
    /// the others to their text, so they grow when a font set in their
    /// place needs more lines.
    pub(crate) custom_rows: HashSet<u32>,
    /// The formats the conditional formats give each cell (indices of the
    /// styles' `dxfs`), the first rule's first.
    pub(crate) conditional: BTreeMap<(u32, u32), Vec<u32>>,
    /// The data bars of the cells that have one.
    pub(crate) bars: BTreeMap<(u32, u32), DataBar>,
    /// Where the fill of their number format (`*x`) widens the text of
    /// the cells that have one, as a byte offset into it: the text before
    /// it keeps to the cell's left edge, the text after to its right.
    pub(crate) fills: BTreeMap<(u32, u32), usize>,
    /// The colour the number format of the cells that have one sets
    /// their number in (`[Red]`), as an index of the legacy palette.
    pub(crate) number_colors: BTreeMap<(u32, u32), u8>,
}

/// The data bar a conditional format draws in a cell.
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct DataBar {
    /// Its length, as a share of the cell's width.
    pub(crate) length: f32,
    /// Its colour.
    pub(crate) color: Color,
}

/// The widths of the columns and the heights of the rows of a sheet, as
/// far as the file gives them, in points.
#[derive(Debug, Clone, Default, PartialEq)]
pub(crate) struct Sizes {
    /// The width of the columns the file sizes (`col`): first and last
    /// column, width in points.
    pub(crate) cols: Vec<(u32, u32, f32)>,
    /// The height of the rows the file sizes (`row ht`), in points.
    pub(crate) rows: BTreeMap<u32, f32>,
    /// The width of the other columns (`sheetFormatPr`); `None` for
    /// Excel's default.
    pub(crate) default_col_pt: Option<f32>,
    /// The height of the other rows (`sheetFormatPr`); `None` for Excel's
    /// default.
    pub(crate) default_row_pt: Option<f32>,
    /// The width of a digit of the default font on the screen the sizes
    /// were read for, in points (whole pixels); 0 when unknown.
    pub(crate) screen_digit_pt: f32,
}

/// A corner of a drawing on the sheet: a cell (row, column, both
/// 0-based) and the offset from its top left corner, in points.
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct Marker {
    /// The row.
    pub(crate) row: u32,
    /// The offset down from the row's top.
    pub(crate) row_off_pt: f32,
    /// The column.
    pub(crate) col: u32,
    /// The offset right from the column's left edge.
    pub(crate) col_off_pt: f32,
}

/// Excel's default column width: 8.43 characters of Calibri 11, 64
/// pixels.
pub(crate) const DEFAULT_COL_PT: f32 = 48.0;
/// Excel's default row height, in points.
const DEFAULT_ROW_PT: f32 = 15.0;

impl Sizes {
    /// The width of the column `col` in points (hidden or not).
    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)
    }

    /// The width and height in points of the box from `from` to `to`;
    /// hidden rows and columns (`hidden_rows`, `hidden_cols`) take no room,
    /// and an offset larger than its cell ends at the cell's edge.
    /// A box whose `to` comes before its `from` has no size on that side.
    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);
        // The rows: the default height for all, corrected by the rows
        // the file sizes or hides (a sheet may have a million rows).
        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
        };
        // The columns are at most 16 384.
        let cols_between = |first: u32, last: u32| -> f32 {
            (first..last.min(first.saturating_add(MAX_SHEET_COLS)))
                .map(col_width)
                .sum()
        };
        // An offset past its cell ends at the cell's edge, as in Excel.
        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))
    }
}

/// The most columns of a sheet (XFD).
const MAX_SHEET_COLS: u32 = 16_384;

/// One cell: its text as Excel shows it, and what kind of value it holds.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct GridCell {
    /// The text, with the style of the cell's font (or of its rich-text
    /// runs) and its hyperlink.
    pub(crate) spans: Vec<Span>,
    /// The kind of value.
    pub(crate) kind: CellKind,
    /// The cell's text is centred across the selection
    /// (`centerContinuous`).
    pub(crate) center_continuous: bool,
    /// For each span, what it sets of its own font rather than take the
    /// cell's (a rich run's bold and italic, and colour when it gives one:
    /// [`OWN_STYLE`](super::strings::OWN_STYLE),
    /// [`OWN_COLOR`](super::strings::OWN_COLOR)); empty when none does.
    pub(crate) own_font: Vec<u8>,
    /// How wide its text is, about, in points, when it runs over the empty
    /// cells to its right (a text not wrapped, aligned left or as numbers
    /// and text are by default); 0 when it does not.
    pub(crate) overflow_pt: f32,
}

/// What a rich run of a cell sets of its own font rather than take the
/// cell's.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub(crate) struct OwnFont {
    /// Its bold, italic and underline (it is plain in a bold cell unless
    /// it is bold itself).
    pub(crate) style: bool,
    /// Its colour, black too.
    pub(crate) color: bool,
    /// It is underlined.
    pub(crate) underline: bool,
}

impl GridCell {
    /// What its span of index `index` sets of its own font.
    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,
        }
    }
}

/// The kind of value of a cell.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum CellKind {
    /// A number (not a date).
    Number,
    /// A number shown as a date or time, or a date written ISO (`t="d"`).
    Date,
    /// Text: a shared, inline or formula string.
    Text,
    /// `TRUE` or `FALSE`.
    Bool,
    /// An error value (`#N/A`, `#DIV/0!`).
    Error,
}

/// An Excel Table: its range and its number of header rows
/// (`headerRowCount`).
pub(crate) type Table = (Merge, u32);

/// A range of cells, inclusive: a merge, a table, an autofilter.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Merge {
    /// The first row.
    pub(crate) top: u32,
    /// The first column.
    pub(crate) left: u32,
    /// The last row.
    pub(crate) bottom: u32,
    /// The last column.
    pub(crate) right: u32,
}

impl Grid {
    /// Whether the cell at `at` shows no text (it may be kept for its
    /// centring).
    pub(crate) fn is_blank(&self, at: (u32, u32)) -> bool {
        self.cells.get(&at).is_none_or(|cell| cell.spans.is_empty())
    }

    /// The width of the columns that the text of the cell at `at` is
    /// centred across, past its own (`centerContinuous`): of `after`, the
    /// columns that follow it in order, the blank cells right after it
    /// that are centred so too, outside any merge, as Excel centres such
    /// a text. `width` gives a column's width. Zero for another cell.
    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()
    }

    /// Whether the cell at `at` is in a merged range.
    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 {
    /// Whether the two ranges have a cell in common.
    pub(crate) fn overlaps(&self, other: &Self) -> bool {
        self.top <= other.bottom && other.top <= self.bottom && self.left <= other.right && other.left <= self.right
    }
}

/// Ranges filed by the squares of the sheet ([`SQUARE_CELLS`]) they cover,
/// so that those overlapping a range are found among the ranges of its
/// squares, not among all of them: a sheet's merges, which do not overlap
/// each other, are few in a square.
#[derive(Default)]
pub(crate) struct MergeIndex {
    /// The ranges, in the order they were added.
    ranges: Vec<Merge>,
    /// The ranges of each square, by (row, column) of the square.
    squares: HashMap<(u32, u32), Vec<usize>>,
}

impl MergeIndex {
    /// The ranges `ranges`, filed.
    pub(crate) fn new(ranges: &[Merge]) -> Self {
        let mut index = Self::default();
        for range in ranges {
            index.add(*range);
        }
        index
    }

    /// Files `range`.
    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);
    }

    /// The ranges filed, in the order they were added.
    pub(crate) fn into_ranges(self) -> Vec<Merge> {
        self.ranges
    }

    /// The ranges that overlap `area`, each once, in the order they were
    /// added.
    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()
    }
}

/// The squares of the sheet that `range` covers.
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)))
}

/// What the file declares about the tables of a sheet, from which their
/// header rows are found.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Declared {
    /// The Excel Tables (`xl/tables/`) of the sheet: their range and their
    /// number of header rows (`headerRowCount`).
    pub(crate) tables: Vec<Table>,
    /// The range of the sheet's autofilter: its first row is a header.
    pub(crate) auto_filter: Option<Merge>,
    /// The rows frozen at the top (`pane ySplit`, `state="frozen"`).
    pub(crate) frozen_rows: u32,
    /// The rows printed at the top of every page (`_xlnm.Print_Titles`):
    /// first and last, inclusive.
    pub(crate) print_title_rows: Option<(u32, u32)>,
    /// The columns printed at the left of every page (`_xlnm.Print_Titles`):
    /// first and last, inclusive.
    pub(crate) print_title_cols: Option<(u32, u32)>,
}