twip 0.1.0-alpha.1

DOCX layout, editing and rendering engine.
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
//! Caret movement: logical units from the model, lines and pages from the geometry of the snapshot shown. Movement between cells and tables follows the containment structure and the table grid, never drawing order.

use std::rc::Rc;

use crate::docx::{Block, Document, Table};
use crate::layout::{ParaId, TableId, Unit};

use super::geometry::{PageGeometry, Pages};
use super::index::{Container, DocIndex};
use super::position::{Affinity, Place, Position, StoryId};
use super::step::para_len;
use super::table::Grid;
use super::text::UnitText;

/// What navigation reads: the document, its index and page geometry, and the page the caret was last on.
pub struct Nav<'a> {
    pub doc: &'a Document,
    pub idx: &'a DocIndex,
    pub pages: &'a mut Pages,
    pub near: usize,
}

/// A caret's line: page, line index on it, and the page's geometry.
pub struct LineAt {
    pub page: usize,
    pub line: usize,
    pub geo: Rc<PageGeometry>,
}

impl Nav<'_> {
    fn text(&self, para: ParaId) -> Option<UnitText> {
        self.idx.para(self.doc, para).map(UnitText::of)
    }

    fn story_of_container(&self, c: Container) -> StoryId {
        self.idx.story(c)
    }

    // ---------------- structure: first and last places ----------------

    /// The first caret place of a block: a paragraph's start, a table's first cell's first place.
    fn block_start(&self, story: StoryId, b: &Block) -> Option<Position> {
        match b {
            Block::Para(p) => Some(Position::text(story, ParaId(p.id), Unit(0))),
            Block::Table(t) => self.cell_first(story, t, 0, self.first_cell(t, 0)?),
        }
    }

    /// The last caret place of a block: a paragraph's end, a table's last row's end-of-row mark.
    fn block_end(&self, story: StoryId, b: &Block) -> Option<Position> {
        match b {
            Block::Para(p) => Some(Position::text(story, ParaId(p.id), para_len(p))),
            Block::Table(t) => Some(Position::row_end(story, TableId(t.id), t.rows.len().checked_sub(1)?)),
        }
    }

    fn first_cell(&self, t: &Table, row: usize) -> Option<usize> {
        let g = Grid::of(t);
        (0..t.rows.get(row)?.cells.len()).find(|&c| !g.continues(row, c))
    }

    fn cell_first(&self, story: StoryId, t: &Table, row: usize, cell: usize) -> Option<Position> {
        t.rows.get(row)?.cells.get(cell)?.blocks.first().and_then(|b| self.block_start(story, b))
    }

    fn cell_last(&self, story: StoryId, t: &Table, row: usize, cell: usize) -> Option<Position> {
        t.rows.get(row)?.cells.get(cell)?.blocks.last().and_then(|b| self.block_end(story, b))
    }

    /// The first place after block `block` of `container` in reading order: the next block, the next
    /// cell (merge continuations skipped), the row's end mark after its last cell.
    fn after(&self, container: Container, block: usize) -> Option<Position> {
        let story = self.story_of_container(container);
        let blocks = self.idx.blocks(self.doc, container)?;
        if let Some(b) = blocks.get(block + 1) {
            return self.block_start(story, b);
        }
        let Container::Cell { table, row, cell } = container else { return None };
        let t = self.idx.table(self.doc, table)?;
        let g = Grid::of(t);
        match (cell + 1..t.rows[row].cells.len()).find(|&c| !g.continues(row, c)) {
            Some(c) => self.cell_first(story, t, row, c),
            None => Some(Position::row_end(story, table, row)),
        }
    }

    /// The place after a row's end mark: the next row's first cell, or after the table.
    fn after_row(&self, story: StoryId, table: TableId, row: usize) -> Option<Position> {
        let t = self.idx.table(self.doc, table)?;
        if row + 1 < t.rows.len() {
            return match self.first_cell(t, row + 1) {
                Some(c) => self.cell_first(story, t, row + 1, c),
                None => Some(Position::row_end(story, table, row + 1)),
            };
        }
        let s = self.idx.table_slot(table)?;
        self.after(s.container, s.block)
    }

    /// The last place before block `block` of `container` (the mirror of [`Nav::after`]).
    fn before(&self, container: Container, block: usize) -> Option<Position> {
        let story = self.story_of_container(container);
        let blocks = self.idx.blocks(self.doc, container)?;
        if block > 0 {
            return self.block_end(story, &blocks[block - 1]);
        }
        let Container::Cell { table, row, cell } = container else { return None };
        let t = self.idx.table(self.doc, table)?;
        let g = Grid::of(t);
        match (0..cell).rev().find(|&c| !g.continues(row, c)) {
            Some(c) => self.cell_last(story, t, row, c),
            None if row > 0 => Some(Position::row_end(story, table, row - 1)),
            None => {
                let s = self.idx.table_slot(table)?;
                self.before(s.container, s.block)
            }
        }
    }

    /// The place before a row's end mark: its last cell's last place.
    fn before_row_end(&self, story: StoryId, table: TableId, row: usize) -> Option<Position> {
        let t = self.idx.table(self.doc, table)?;
        let g = Grid::of(t);
        let c = (0..t.rows.get(row)?.cells.len()).rev().find(|&c| !g.continues(row, c))?;
        self.cell_last(story, t, row, c)
    }

    // ---------------- logical movement ----------------

    /// Right: one grapheme, or with `word` to the next word start; at a paragraph's end, the next
    /// place in reading order.
    pub fn right(&self, pos: Position, word: bool) -> Position {
        match pos.place {
            Place::Text { para, offset, .. } => {
                let Some(t) = self.text(para) else { return pos };
                if offset < t.len() {
                    let u = if word { t.next_word(offset) } else { t.next_grapheme(offset) };
                    return Position::text(pos.story, para, u);
                }
                let Some(s) = self.idx.para_slot(para) else { return pos };
                self.after(s.container, s.block).unwrap_or(pos)
            }
            Place::RowEnd { table, row } => self.after_row(pos.story, table, row).unwrap_or(pos),
        }
    }

    /// Left: the mirror of [`Nav::right`].
    pub fn left(&self, pos: Position, word: bool) -> Position {
        match pos.place {
            Place::Text { para, offset, .. } => {
                let Some(t) = self.text(para) else { return pos };
                if offset > Unit(0) {
                    let u = if word { t.prev_word(offset) } else { t.prev_grapheme(offset) };
                    return Position::text(pos.story, para, u);
                }
                let Some(s) = self.idx.para_slot(para) else { return pos };
                self.before(s.container, s.block).unwrap_or(pos)
            }
            Place::RowEnd { table, row } => self.before_row_end(pos.story, table, row).unwrap_or(pos),
        }
    }

    /// Ctrl+Down: the start of the next paragraph in document order; a cell's end and a row's end
    /// mark count as paragraphs, so from a row's last cell it stops at the mark.
    pub fn next_para(&self, pos: Position) -> Position {
        self.right(self.para_end(pos), false)
    }

    /// Ctrl+Up: the start of this paragraph, or else the previous paragraph's start (or a row end mark before it).
    pub fn prev_para(&self, pos: Position) -> Position {
        match pos.place {
            Place::Text { para, offset, .. } if offset > Unit(0) => Position::text(pos.story, para, Unit(0)),
            _ => {
                let p = self.left(pos, false);
                match p.place {
                    Place::Text { para, .. } => Position::text(p.story, para, Unit(0)),
                    Place::RowEnd { .. } => p,
                }
            }
        }
    }

    fn para_end(&self, pos: Position) -> Position {
        match pos.place {
            Place::Text { para, .. } => match self.idx.para(self.doc, para) {
                Some(p) => Position::text(pos.story, para, para_len(p)),
                None => pos,
            },
            Place::RowEnd { .. } => pos,
        }
    }

    /// Ctrl+Home / Ctrl+End: the start or end of the story.
    pub fn story_edge(&self, story: StoryId, end: bool) -> Option<Position> {
        let roots: Vec<Container> = match story {
            StoryId::Body => (0..self.doc.sections.len()).map(Container::Section).collect(),
            s => vec![Container::Story(s)],
        };
        let pick = |c: &Container| -> Option<Position> {
            let blocks = self.idx.blocks(self.doc, *c)?;
            if end { blocks.last().and_then(|b| self.block_end(story, b)) } else { blocks.first().and_then(|b| self.block_start(story, b)) }
        };
        if end { roots.iter().rev().find_map(pick) } else { roots.iter().find_map(pick) }
    }

    /// Alt+Home/End (`row`) and Alt+Page Up/Down: the start of the first or last cell of the caret's
    /// row, or of its column, in its innermost table.
    pub fn table_edge(&self, pos: Position, row_wise: bool, end: bool) -> Option<Position> {
        let level = *self.idx.chain_of(&pos).last()?;
        let t = self.idx.table(self.doc, level.table)?;
        let g = Grid::of(t);
        let (r, c) = if row_wise {
            let mut cells = (0..t.rows.get(level.row)?.cells.len()).filter(|&c| !g.continues(level.row, c));
            (level.row, if end { cells.next_back()? } else { cells.min()? })
        } else {
            let cell = level.cell.min(t.rows.get(level.row)?.cells.len().checked_sub(1)?);
            let col = g.col_of(level.row, cell)?;
            let mut rows = (0..t.rows.len()).filter_map(|r| g.cell_at(r, col).map(|c| g.merge_start(r, c)));
            if end { rows.next_back()? } else { rows.min()? }
        };
        self.cell_first(pos.story, t, r, c)
    }

    // ---------------- geometry ----------------

    /// The line showing a position: on the page nearest the caret's last page.
    pub fn line_of(&mut self, pos: Position) -> Option<LineAt> {
        let (para, unit, affinity) = match pos.place {
            Place::Text { para, offset, affinity } => (para, offset, affinity),
            Place::RowEnd { .. } => return None,
        };
        let (page, geo, line) = self.pages.line_showing(para, unit, affinity, self.near, self.idx)?;
        Some(LineAt { page, line, geo })
    }

    /// Home / End: the start or end of the caret's line; End shows at the line's end (upstream).
    pub fn line_edge(&mut self, pos: Position, end: bool) -> Option<Position> {
        let at = self.line_of(pos)?;
        let l = &at.geo.lines[at.line];
        let stop = if end { l.stops.last()? } else { l.stops.first()? };
        let wraps = end && at.geo.lines.iter().any(|m| m.para == l.para && m.first() == stop.unit && m.top > l.top);
        Some(Position {
            story: pos.story,
            place: Place::Text { para: l.para, offset: stop.unit, affinity: if wraps { Affinity::Upstream } else { Affinity::Downstream } },
        })
    }

    /// The caret's x on its line.
    pub fn caret_x(&mut self, pos: Position) -> Option<f64> {
        let at = self.line_of(pos)?;
        let (_, unit) = pos.para_offset()?;
        at.geo.lines[at.line].x_of(unit)
    }

    /// The position on paragraph `para`'s first (or last) line nearest `x`.
    fn on_line(&mut self, story: StoryId, para: ParaId, x: f64, last: bool) -> Option<Position> {
        let pages = self.pages.pages_of(para, self.near, self.idx);
        let page = if last { *pages.last()? } else { *pages.first()? };
        let geo = self.pages.page(page, self.idx)?;
        let line = if last { geo.lines_of(para).last() } else { geo.lines_of(para).next() };
        let Some((_, line)) = line else {
            // A cell's closing paragraph after a nested table is drawn without lines; the caret still stops there.
            return geo.cells.iter().any(|c| c.closing.is_some_and(|(p, _)| p == para)).then(|| Position::text(story, para, Unit(0)));
        };
        let stop = line.nearest(x)?;
        let wraps = stop.end && geo.lines.iter().any(|m| m.para == para && m.first() == stop.unit && m.top > line.top);
        Some(Position {
            story,
            place: Place::Text { para, offset: stop.unit, affinity: if wraps { Affinity::Upstream } else { Affinity::Downstream } },
        })
    }

    /// Up / Down with goal x: the next line of the paragraph, else the
    /// first (last) line of the next (previous) content in the containment structure, entering tables at the cell
    /// under the goal x and leaving them to the cell below (above) by grid column.
    pub fn vertical(&mut self, pos: Position, x: f64, down: bool) -> Option<Position> {
        if let Some(at) = self.line_of(pos) {
            let l = &at.geo.lines[at.line];
            let para = l.para;
            // Another line of the same paragraph, on this page or the next.
            let same: Vec<usize> = at.geo.lines_of(para).map(|(i, _)| i).collect();
            let k = same.iter().position(|&i| i == at.line)?;
            let next = if down { same.get(k + 1) } else { k.checked_sub(1).and_then(|j| same.get(j)) };
            if let Some(&i) = next {
                let stop = at.geo.lines[i].nearest(x)?;
                return Some(Position::text(pos.story, para, stop.unit));
            }
            let page = if down { Some(at.page + 1) } else { at.page.checked_sub(1) };
            if let Some(g) = page.and_then(|page| self.pages.page(page, self.idx)) {
                let lines: Vec<usize> = g.lines_of(para).map(|(i, _)| i).collect();
                if let Some(&i) = if down { lines.first() } else { lines.last() } {
                    let stop = g.lines[i].nearest(x)?;
                    return Some(Position::text(pos.story, para, stop.unit));
                }
            }
        }
        // Past the paragraph (or a row end mark): the structural neighbour.
        let target = match pos.place {
            Place::Text { para, .. } => {
                let s = self.idx.para_slot(para)?;
                if down { self.below(s.container, s.block, x)? } else { self.above(s.container, s.block, x)? }
            }
            Place::RowEnd { table, row } => {
                let s = self.idx.table_slot(table)?;
                let t = self.idx.table(self.doc, table)?;
                if down {
                    if row + 1 < t.rows.len() { self.enter_row(pos.story, t, row + 1, x, true)? } else { self.below(s.container, s.block, x)? }
                } else if row > 0 {
                    self.enter_row(pos.story, t, row - 1, x, false)?
                } else {
                    self.above(s.container, s.block, x)?
                }
            }
        };
        Some(target)
    }

    /// The first line below block `block` of `container` at x.
    fn below(&mut self, container: Container, block: usize, x: f64) -> Option<Position> {
        let story = self.story_of_container(container);
        let blocks = self.idx.blocks(self.doc, container)?;
        if let Some(b) = blocks.get(block + 1) {
            return self.enter(story, b, x, true);
        }
        let Container::Cell { table, row, cell } = container else { return None };
        let t = self.idx.table(self.doc, table)?;
        let g = Grid::of(t);
        let col = self.column_at(t, row, cell, x, &g);
        match g.below(row, col) {
            Some((r, _)) => self.enter_row(story, t, r, x, true),
            None => {
                let s = self.idx.table_slot(table)?;
                self.below(s.container, s.block, x)
            }
        }
    }

    /// The last line above block `block` of `container` at x.
    fn above(&mut self, container: Container, block: usize, x: f64) -> Option<Position> {
        let story = self.story_of_container(container);
        let blocks = self.idx.blocks(self.doc, container)?;
        if block > 0 {
            return self.enter(story, &blocks[block - 1], x, false);
        }
        let Container::Cell { table, row, cell } = container else { return None };
        let t = self.idx.table(self.doc, table)?;
        let g = Grid::of(t);
        let col = self.column_at(t, row, cell, x, &g);
        match g.above(row, col) {
            Some((r, _)) => self.enter_row(story, t, r, x, false),
            None => {
                let s = self.idx.table_slot(table)?;
                self.above(s.container, s.block, x)
            }
        }
    }

    /// The grid column under x in a row (the cell's own column when x is outside the row's drawn cells).
    fn column_at(&mut self, t: &Table, row: usize, cell: usize, x: f64, g: &Grid) -> usize {
        let own = g.col_of(row, cell).unwrap_or(0);
        let Some(c) = self.cell_rect_cell(TableId(t.id), row, x) else { return own };
        g.col_of(row, c).unwrap_or(own)
    }

    /// The cell of a row whose drawn box spans x (on any page the row is drawn on).
    fn cell_rect_cell(&mut self, table: TableId, row: usize, x: f64) -> Option<usize> {
        for i in 0..self.pages.len() {
            let page = (self.near + i) % self.pages.len().max(1);
            let Some(geo) = self.pages.page(page, self.idx) else { continue };
            if !geo.shows_table(table) {
                continue;
            }
            let cells: Vec<(usize, [f64; 4])> = geo
                .cells
                .iter()
                .filter(|c| c.chain.last().is_some_and(|k| k.0 == table && k.1 == row))
                .map(|c| (c.chain.last().map_or(0, |k| k.2), c.rect))
                .collect();
            if cells.is_empty() {
                continue;
            }
            return cells
                .iter()
                .find(|(_, r)| x >= r[0] && x < r[2])
                .or_else(|| {
                    cells.iter().min_by(|a, b| (a.1[0] - x).abs().min((a.1[2] - x).abs()).total_cmp(&(b.1[0] - x).abs().min((b.1[2] - x).abs())))
                })
                .map(|c| c.0);
        }
        None
    }

    /// Into a block from above (`down`) or below: a paragraph's first (last) line, a table's first (last) row.
    fn enter(&mut self, story: StoryId, b: &Block, x: f64, down: bool) -> Option<Position> {
        match b {
            Block::Para(p) => self.on_line(story, ParaId(p.id), x, !down),
            Block::Table(t) => {
                let row = if down { 0 } else { t.rows.len().checked_sub(1)? };
                self.enter_row(story, t, row, x, down)
            }
        }
    }

    /// Into the cell of `row` under x, at its first (last) content; a merge continuation means its region's first
    /// cell.
    fn enter_row(&mut self, story: StoryId, t: &Table, row: usize, x: f64, down: bool) -> Option<Position> {
        let g = Grid::of(t);
        let cell = self.cell_rect_cell(TableId(t.id), row, x).or_else(|| self.first_cell(t, row))?;
        let (r, c) = g.merge_start(row, cell);
        let blocks = &t.rows.get(r)?.cells.get(c)?.blocks;
        let b = if down { blocks.first()? } else { blocks.last()? };
        self.enter(story, b, x, down)
    }
}