mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 00:05:08 +00:00
Moves the import part of storytold/wordcraft#4964 into craft-flow, where WordCraft's RTF and ODT import now builds its tables (#778). Import raised every column under 6 pt to 6 pt, so a form with many narrow columns came out wider than its source. Columns over 1 pt now keep the width the file gives; one of a point or less, none or not a number still gives the table equal columns, so nothing below a point, negative or NaN reaches layout. craft-rtf merged grid columns under 6 pt that no row needed, because import would widen them. With import keeping them, that merge only moved cell edges, so it goes: grid columns merge only while there are over 63 or one is a point or less. Stacked on the craft-rtf column grid change.
570 lines
24 KiB
Rust
570 lines
24 KiB
Rust
use std::cmp::Reverse;
|
|
use std::collections::BinaryHeap;
|
|
|
|
use super::reader::Reader;
|
|
use super::state::{CommentData, Dest, HeaderFooter, HfPages, MARK_CLOSE, MARK_OPEN, MAX_PICT, NoteData, PageGeom};
|
|
use super::{Arc, Block, Blocks, Cell, Document, FBlock, Flow, HashMap, InlineObject, Para, Paragraph, model};
|
|
|
|
/// Parse RTF bytes into the flow model.
|
|
pub fn parse(bytes: &[u8]) -> Result<Flow, String> {
|
|
parse_with_page(bytes).map(|(f, _, _)| f)
|
|
}
|
|
|
|
/// The flow, the page, and the default font (`\deff`, font 0 when the file names none), which
|
|
/// is the font of all text that doesn't select one.
|
|
fn parse_with_page(bytes: &[u8]) -> Result<(Flow, PageGeom, Option<String>), String> {
|
|
parse_full(bytes, false).map(|r| (r.flow, r.page, r.default_font))
|
|
}
|
|
|
|
/// Everything an import takes from an RTF file.
|
|
struct Parsed {
|
|
flow: Flow,
|
|
page: PageGeom,
|
|
/// The last section's properties (the others are on the paragraphs that end them).
|
|
last_section: craft_wordmodel::SectionProps,
|
|
default_font: Option<String>,
|
|
/// Each section's headers and footers, in order, the last section's last.
|
|
headers_footers: Vec<Vec<(HeaderFooter, Vec<FBlock>)>>,
|
|
notes: Vec<NoteData>,
|
|
comments: Vec<CommentData>,
|
|
}
|
|
|
|
/// `marks`: also read footnotes, endnotes and comments (as place markers in the flow's text, for
|
|
/// [`import`] to turn into objects); without it they are skipped.
|
|
fn parse_full(bytes: &[u8], marks: bool) -> Result<Parsed, String> {
|
|
let start = bytes.iter().position(|b| !b.is_ascii_whitespace()).unwrap_or(0);
|
|
if !bytes.get(start..).is_some_and(|b| b.starts_with(b"{\\rtf")) {
|
|
return Err("not an RTF document (it doesn't start with {\\rtf)".into());
|
|
}
|
|
let mut r = Reader::default();
|
|
r.marks_on = marks;
|
|
let mut i = start;
|
|
let mut first_in_group = false;
|
|
let mut starred = false;
|
|
while let Some(&c) = bytes.get(i) {
|
|
match c {
|
|
b'{' => {
|
|
r.open();
|
|
first_in_group = true;
|
|
starred = false;
|
|
i += 1;
|
|
continue;
|
|
}
|
|
b'}' => {
|
|
r.close();
|
|
if r.info_depth.is_some_and(|d| r.depth() < d) {
|
|
r.info_depth = None;
|
|
}
|
|
first_in_group = false;
|
|
starred = false;
|
|
i += 1;
|
|
continue;
|
|
}
|
|
b'\\' => {
|
|
let Some(&n) = bytes.get(i + 1) else { break };
|
|
if n.is_ascii_alphabetic() {
|
|
let mut j = i + 1;
|
|
while bytes.get(j).is_some_and(u8::is_ascii_alphabetic) && j - i <= 32 {
|
|
j += 1;
|
|
}
|
|
let word = std::str::from_utf8(bytes.get(i + 1..j).unwrap_or(b"")).unwrap_or("");
|
|
let mut k = j;
|
|
let neg = bytes.get(k) == Some(&b'-');
|
|
if neg {
|
|
k += 1;
|
|
}
|
|
let d0 = k;
|
|
while bytes.get(k).is_some_and(u8::is_ascii_digit) && k - d0 < 10 {
|
|
k += 1;
|
|
}
|
|
let param = if k > d0 {
|
|
let v = std::str::from_utf8(bytes.get(d0..k).unwrap_or(b"0")).ok().and_then(|s| s.parse::<i64>().ok()).unwrap_or(0);
|
|
let v = if neg { -v } else { v };
|
|
// A date (DTTM) is a 32-bit pattern some writers put unsigned.
|
|
let v = if matches!(word, "revdttm" | "revdttmdel" | "crdate" | "prdate")
|
|
&& v > i64::from(i32::MAX)
|
|
&& v <= i64::from(u32::MAX)
|
|
{
|
|
v - (1i64 << 32)
|
|
} else {
|
|
v
|
|
};
|
|
Some(v.clamp(i64::from(i32::MIN), i64::from(i32::MAX)) as i32)
|
|
} else {
|
|
if neg {
|
|
k -= 1;
|
|
}
|
|
None
|
|
};
|
|
if bytes.get(k) == Some(&b' ') {
|
|
k += 1;
|
|
}
|
|
i = k;
|
|
if word == "bin" {
|
|
let n = param.unwrap_or(0).max(0) as usize;
|
|
let end = i.saturating_add(n).min(bytes.len());
|
|
if r.st.dest == Dest::Pict
|
|
&& let Some(data) = bytes.get(i..end)
|
|
&& r.pict_hex.len() + data.len() <= MAX_PICT
|
|
{
|
|
r.pict_hex.extend_from_slice(data);
|
|
}
|
|
i = end;
|
|
continue;
|
|
}
|
|
if r.skip_chars > 0
|
|
&& matches!(
|
|
r.st.dest,
|
|
Dest::Body | Dest::Info(_) | Dest::FldInst | Dest::Bookmark | Dest::FontTbl | Dest::RevTbl | Dest::Atn(_)
|
|
)
|
|
{
|
|
r.skip_chars -= 1;
|
|
first_in_group = false;
|
|
continue;
|
|
}
|
|
if word == "info" {
|
|
r.info_depth = Some(r.depth());
|
|
}
|
|
r.word(word, param, starred, first_in_group);
|
|
first_in_group = false;
|
|
starred = false;
|
|
continue;
|
|
}
|
|
if r.skip_chars > 0 && n != b'\'' {
|
|
r.skip_chars -= 1;
|
|
i += 2;
|
|
first_in_group = false;
|
|
continue;
|
|
}
|
|
match n {
|
|
b'\'' => {
|
|
// `from_str_radix` takes a leading `+`: a pair that isn't both hex digits
|
|
// is not an escape at all.
|
|
let h = bytes
|
|
.get(i + 2..i + 4)
|
|
.and_then(|h| std::str::from_utf8(h).ok())
|
|
.filter(|h| h.bytes().all(|b| b.is_ascii_hexdigit()))
|
|
.and_then(|h| u8::from_str_radix(h, 16).ok());
|
|
match h {
|
|
Some(v) => {
|
|
if r.st.dest == Dest::Pict {
|
|
// Hex digits, two at a time, without a String per picture byte.
|
|
const HEX: &[u8; 16] = b"0123456789abcdef";
|
|
let h = [HEX[usize::from(v >> 4)], HEX[usize::from(v & 0x0f)]];
|
|
if let Ok(s) = std::str::from_utf8(&h) {
|
|
r.text(s);
|
|
}
|
|
} else {
|
|
r.byte(v);
|
|
}
|
|
i += 4;
|
|
}
|
|
// Not two hex digits: the escape is malformed. Consume the escape and
|
|
// any hex digits of a truncated pair, and leave every other byte,
|
|
// `}` or a control word's `\`, to be read as the structure it is.
|
|
None => {
|
|
i += 2;
|
|
let mut n = 0;
|
|
while n < 2 && bytes.get(i).is_some_and(u8::is_ascii_hexdigit) {
|
|
i += 1;
|
|
n += 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
b'*' => {
|
|
starred = true;
|
|
i += 2;
|
|
continue;
|
|
}
|
|
b'~' | b' ' => {
|
|
r.ch('\u{00A0}');
|
|
i += 2;
|
|
}
|
|
b'-' => {
|
|
r.ch('\u{00AD}');
|
|
i += 2;
|
|
}
|
|
b'_' => {
|
|
r.ch('\u{2011}');
|
|
i += 2;
|
|
}
|
|
b'\n' | b'\r' => {
|
|
r.word("par", None, false, false);
|
|
i += 2;
|
|
}
|
|
b'\\' | b'{' | b'}' => {
|
|
r.ch(n as char);
|
|
i += 2;
|
|
}
|
|
_ => i += 2,
|
|
}
|
|
first_in_group = false;
|
|
}
|
|
b'\r' | b'\n' | 0 => i += 1,
|
|
_ => {
|
|
// A run of plain text.
|
|
let mut j = i;
|
|
while bytes.get(j).is_some_and(|b| !matches!(b, b'{' | b'}' | b'\\' | b'\r' | b'\n' | 0)) {
|
|
j += 1;
|
|
}
|
|
let chunk = bytes.get(i..j).unwrap_or(b"");
|
|
if r.st.dest == Dest::Pict && r.skip_chars == 0 && chunk.is_ascii() {
|
|
// Picture hex is not text: it needs no decoder, so neither a pending lead byte
|
|
// nor the code page's single-byte path applies to it.
|
|
if let Ok(s) = std::str::from_utf8(chunk) {
|
|
r.text(s);
|
|
}
|
|
} else if r.skip_chars == 0 && chunk.is_ascii() && r.mb_decoder.is_none() {
|
|
// Plain ASCII with no multibyte run open: append it as it is.
|
|
if let Ok(s) = std::str::from_utf8(chunk) {
|
|
r.text(s);
|
|
}
|
|
} else {
|
|
r.run(chunk);
|
|
}
|
|
i = j;
|
|
first_in_group = false;
|
|
}
|
|
}
|
|
}
|
|
Ok(finalize(r))
|
|
}
|
|
|
|
fn finalize(mut r: Reader) -> Parsed {
|
|
// A multibyte run ends here, and so does a note, comment, header or footer left open by a
|
|
// truncated file.
|
|
r.finish_multibyte();
|
|
r.end_sub();
|
|
r.end_header_footer();
|
|
if r.para_has || !r.para.inlines.is_empty() {
|
|
r.end_para(false);
|
|
}
|
|
r.close_table();
|
|
if let Some((y, mo, d, h, mi)) = r.created
|
|
&& y > 0
|
|
{
|
|
r.meta.created = format!("{y:04}-{:02}-{:02}T{:02}:{:02}:00Z", mo.clamp(1, 12), d.clamp(1, 31), h.min(23), mi.min(59));
|
|
}
|
|
let mut blocks = std::mem::take(&mut r.body);
|
|
// A file that ends with `\sect` ends its last section there: no empty one follows, and
|
|
// header or footer groups after that `\sect` (a section without text) are dropped.
|
|
let mut last_section = r.sec.props(&r.page);
|
|
let mut headers_footers = std::mem::take(&mut r.section_parts);
|
|
if let Some(FBlock::Para(p)) = blocks.last_mut()
|
|
&& let Some(s) = p.section.take()
|
|
{
|
|
last_section = *s;
|
|
} else {
|
|
headers_footers.push(std::mem::take(&mut r.hf_parts));
|
|
}
|
|
if blocks.is_empty() {
|
|
blocks.push(FBlock::Para(Para::default()));
|
|
}
|
|
let default_font = r.fonts.get(&r.deff).filter(|n| !model::is_mono(n)).map(|n| n.to_string());
|
|
Parsed { flow: Flow { blocks, meta: r.meta }, page: r.page, last_section, default_font, headers_footers, notes: r.notes, comments: r.comments }
|
|
}
|
|
|
|
/// Most grid columns a table gets (the document model's own cap).
|
|
const MAX_GRID_COLS: usize = 63;
|
|
/// Grid columns of this width or less, in twips (1 pt), always merge: the document model refuses
|
|
/// them, and would then give the table equal columns.
|
|
const NO_GRID_GAP: i64 = 20;
|
|
|
|
/// The column grid of a table from its rows' cell right edges: every distinct edge starts a grid
|
|
/// column, and each cell spans the grid columns between its left and right edges, as Word lays
|
|
/// out RTF rows whose cells don't line up. Returns the widths in points, or none (equal columns)
|
|
/// when no grid fits, which only a row of more cells than grid columns can cause.
|
|
///
|
|
/// One odd row never costs the whole table its grid (a form exported from a spreadsheet, with
|
|
/// many narrow columns, otherwise had its wide title squeezed into one of four equal columns):
|
|
/// - Over [`MAX_GRID_COLS`] edges, or edges [`NO_GRID_GAP`] or less apart, merge
|
|
/// ([`coarse_edges`]); each cell edge moves to the nearest grid edge. Narrow columns over a
|
|
/// point stay: import keeps them as narrow as the file gives them.
|
|
/// - A cell that ends at or before the previous one (a zero-width cell, or a cell with no
|
|
/// `\cellx` of its own) still gets a grid column of its own; cells pushed past the table's
|
|
/// right edge move back.
|
|
pub(super) fn column_grid(rows: &mut [Vec<Cell>], rights: &[Vec<i32>]) -> Vec<f32> {
|
|
if rights.len() != rows.len() {
|
|
return Vec::new();
|
|
}
|
|
let mut edges: Vec<i32> = rights.iter().flatten().copied().filter(|&x| x > 0).collect();
|
|
edges.sort_unstable();
|
|
edges.dedup();
|
|
if edges.is_empty() {
|
|
return Vec::new();
|
|
}
|
|
let grid = coarse_edges(&edges);
|
|
// Each cell's right edge as a grid edge number (1 = the first grid column's right edge).
|
|
let mut ends: Vec<Vec<usize>> = Vec::with_capacity(rows.len());
|
|
for (row, x) in rows.iter().zip(rights) {
|
|
if row.len() > grid.len() {
|
|
return Vec::new();
|
|
}
|
|
let mut at: Vec<usize> = Vec::with_capacity(row.len());
|
|
let mut prev = 0;
|
|
for i in 0..row.len() {
|
|
let end = x.get(i).map_or(0, |&r| nearest_edge(&grid, r)).max(prev + 1);
|
|
at.push(end);
|
|
prev = end;
|
|
}
|
|
// Cells pushed past the right edge move back, each keeping a column of its own.
|
|
let mut limit = grid.len();
|
|
for end in at.iter_mut().rev() {
|
|
*end = (*end).min(limit);
|
|
limit = end.saturating_sub(1);
|
|
}
|
|
ends.push(at);
|
|
}
|
|
for (row, at) in rows.iter_mut().zip(&ends) {
|
|
let mut left = 0;
|
|
for (c, &end) in row.iter_mut().zip(at) {
|
|
c.colspan = u32::try_from(end.saturating_sub(left)).unwrap_or(1).max(1);
|
|
left = end;
|
|
}
|
|
}
|
|
let mut prev = 0i64;
|
|
grid.iter()
|
|
.map(|&e| {
|
|
let w = (i64::from(e) - prev) as f32 / 20.0;
|
|
prev = i64::from(e);
|
|
w
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// The grid edges kept from the sorted, distinct, positive `edges`. The narrowest column merges
|
|
/// into the next, one at a time, so an edge moves only when it has to: while there are over
|
|
/// [`MAX_GRID_COLS`] columns, or columns of [`NO_GRID_GAP`] or less. The table's right edge is
|
|
/// always kept.
|
|
fn coarse_edges(edges: &[i32]) -> Vec<i32> {
|
|
let n = edges.len();
|
|
// Each edge's neighbours among the edges still kept.
|
|
let mut left: Vec<Option<usize>> = (0..n).map(|i| i.checked_sub(1)).collect();
|
|
let mut right: Vec<Option<usize>> = (1..=n).map(|i| (i < n).then_some(i)).collect();
|
|
let mut kept = vec![true; n];
|
|
let at = |i: Option<usize>| i.and_then(|i| edges.get(i)).map_or(0, |&e| i64::from(e));
|
|
let width = |i: usize, left: &[Option<usize>]| at(Some(i)) - at(left.get(i).copied().flatten());
|
|
let mut narrowest: BinaryHeap<Reverse<(i64, usize)>> = (0..n).map(|i| Reverse((width(i, &left), i))).collect();
|
|
let mut count = n;
|
|
while count > 1 {
|
|
let Some(Reverse((w, i))) = narrowest.pop() else {
|
|
break;
|
|
};
|
|
// A column that has since grown, or gone, has a fresher entry or none.
|
|
if !kept.get(i).copied().unwrap_or(false) || w != width(i, &left) {
|
|
continue;
|
|
}
|
|
if count <= MAX_GRID_COLS && w > NO_GRID_GAP {
|
|
break;
|
|
}
|
|
// The column's right edge goes, unless it is the table's: then its left edge does.
|
|
let last = right.get(i).copied().flatten().is_none();
|
|
let Some(gone) = (if last { left.get(i).copied().flatten() } else { Some(i) }) else {
|
|
break;
|
|
};
|
|
let before = left.get(gone).copied().flatten();
|
|
let after = right.get(gone).copied().flatten();
|
|
if let Some(slot) = before.and_then(|b| right.get_mut(b)) {
|
|
*slot = after;
|
|
}
|
|
if let Some(a) = after {
|
|
if let Some(slot) = left.get_mut(a) {
|
|
*slot = before;
|
|
}
|
|
narrowest.push(Reverse((width(a, &left), a)));
|
|
}
|
|
if let Some(slot) = kept.get_mut(gone) {
|
|
*slot = false;
|
|
}
|
|
count -= 1;
|
|
}
|
|
edges.iter().zip(&kept).filter(|&(_, &k)| k).map(|(&e, _)| e).collect()
|
|
}
|
|
|
|
/// The number of the grid edge nearest `x` (1-based; 0 is the table's left edge).
|
|
fn nearest_edge(grid: &[i32], x: i32) -> usize {
|
|
if x <= 0 {
|
|
return 0;
|
|
}
|
|
let p = grid.partition_point(|&e| e < x);
|
|
let below = if p == 0 { 0 } else { grid.get(p - 1).copied().unwrap_or(0) };
|
|
match grid.get(p) {
|
|
Some(&above) if i64::from(above) - i64::from(x) <= i64::from(x) - i64::from(below) => p + 1,
|
|
Some(_) => p,
|
|
None => grid.len(),
|
|
}
|
|
}
|
|
|
|
/// Parse RTF bytes into a document.
|
|
pub fn import(bytes: &[u8]) -> Result<Document, String> {
|
|
model::begin_import();
|
|
let Parsed { flow, last_section, default_font, headers_footers, notes, comments, .. } = parse_full(bytes, true)?;
|
|
let mut doc = model::to_doc_in(&flow, last_section);
|
|
// Each section's own headers and footers; those it leaves out stay linked to the previous.
|
|
let mut sets = Vec::with_capacity(headers_footers.len());
|
|
for parts in &headers_footers {
|
|
let (mut headers, mut footers) = (craft_wordmodel::section::HeaderSet::default(), craft_wordmodel::section::HeaderSet::default());
|
|
for (which, blocks) in parts {
|
|
let blocks = model::part_blocks(&mut doc, blocks);
|
|
let id = doc.add_part(if which.footer { craft_wordmodel::PartKind::Footer } else { craft_wordmodel::PartKind::Header }, blocks);
|
|
let set = if which.footer { &mut footers } else { &mut headers };
|
|
match which.pages {
|
|
HfPages::Default => set.default = Some(id),
|
|
HfPages::First => set.first = Some(id),
|
|
HfPages::Even => set.even = Some(id),
|
|
}
|
|
}
|
|
sets.push((headers, footers));
|
|
}
|
|
let mut sets = sets.into_iter();
|
|
for b in &mut doc.body {
|
|
if let craft_wordmodel::Block::Para(p) = &**b
|
|
&& p.section.is_some()
|
|
&& let craft_wordmodel::Block::Para(p) = std::sync::Arc::make_mut(b)
|
|
&& let Some(s) = p.section.as_deref_mut()
|
|
&& let Some((h, f)) = sets.next()
|
|
{
|
|
(s.headers, s.footers) = (h, f);
|
|
}
|
|
}
|
|
if let Some((h, f)) = sets.next() {
|
|
(doc.last_section.headers, doc.last_section.footers) = (h, f);
|
|
}
|
|
place_notes_and_comments(&mut doc, notes, comments);
|
|
// RTF's paragraph defaults are single spacing and no space around paragraphs; what a file
|
|
// wants beyond that it states on each paragraph.
|
|
let dp = &mut Arc::make_mut(&mut doc.styles).default_para;
|
|
dp.space_before = Some(0.0);
|
|
dp.space_after = Some(0.0);
|
|
dp.line_spacing = Some(craft_wordmodel::props::LineSpacing::Multiple(1.0));
|
|
if default_font.is_some() {
|
|
Arc::make_mut(&mut doc.styles).default_chr.font = default_font;
|
|
}
|
|
Ok(doc)
|
|
}
|
|
|
|
/// Turn the place markers the reader left in the text into note references and comment ranges,
|
|
/// and add the notes' and comments' stories to `doc`.
|
|
fn place_notes_and_comments(doc: &mut Document, notes: Vec<NoteData>, comments: Vec<CommentData>) {
|
|
use craft_wordmodel::para::NoteKind;
|
|
use craft_wordmodel::{Comment, PartKind};
|
|
if notes.is_empty() && comments.is_empty() {
|
|
return;
|
|
}
|
|
// (kind, part id) per note, by index.
|
|
let mut note_parts: Vec<(NoteKind, u32)> = Vec::new();
|
|
for n in notes {
|
|
let (pk, nk, style) =
|
|
if n.endnote { (PartKind::Endnote, NoteKind::Endnote, "EndnoteText") } else { (PartKind::Footnote, NoteKind::Footnote, "FootnoteText") };
|
|
let mut blocks = model::part_blocks(doc, &n.blocks);
|
|
for b in &mut blocks {
|
|
if let Block::Para(p) = Arc::make_mut(b)
|
|
&& p.props.style.is_none()
|
|
{
|
|
p.props.style = Some(style.to_string());
|
|
}
|
|
}
|
|
note_parts.push((nk, doc.add_part(pk, blocks)));
|
|
}
|
|
// RTF range id to the comment's id in the document.
|
|
let mut by_rtf: HashMap<i64, u32> = HashMap::new();
|
|
for (k, c) in comments.into_iter().enumerate() {
|
|
let id = k as u32;
|
|
let blocks = model::part_blocks(doc, &c.blocks);
|
|
let part = doc.add_part(PartKind::Comment, blocks);
|
|
doc.comments
|
|
.insert(id, Comment { author: c.author, initials: c.initials, date: c.date, parent: None, resolved: false, part, ..Default::default() });
|
|
by_rtf.insert(i64::from(c.rtf_id), id);
|
|
}
|
|
let ctx = Placer { note_parts, by_rtf };
|
|
let mut body = std::mem::take(&mut doc.body);
|
|
ctx.blocks(&mut body, None, 0);
|
|
doc.body = body;
|
|
let ids: Vec<u32> = doc.parts.keys().copied().collect();
|
|
for id in ids {
|
|
let Some(part) = doc.parts.get_mut(&id) else { continue };
|
|
let own = ctx.note_parts.iter().find(|(_, p)| *p == id).copied();
|
|
ctx.blocks(&mut part.blocks, own, 0);
|
|
// A note starts with its own number, as Word's do.
|
|
if let Some((kind, pid)) = own
|
|
&& let Some(first) = part.blocks.first_mut()
|
|
&& let Block::Para(p) = Arc::make_mut(first)
|
|
&& !p.objects.iter().any(|o| matches!(o, InlineObject::NoteRef { id, .. } if *id == pid))
|
|
{
|
|
let props = Placer::ref_props(kind);
|
|
if p.insert_object(0, InlineObject::NoteRef { kind, id: pid, custom: String::new() }, &props).is_err() {
|
|
// The paragraph can't take it; the note simply has no number mark.
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct Placer {
|
|
note_parts: Vec<(craft_wordmodel::para::NoteKind, u32)>,
|
|
by_rtf: HashMap<i64, u32>,
|
|
}
|
|
|
|
impl Placer {
|
|
fn ref_props(kind: craft_wordmodel::para::NoteKind) -> craft_wordmodel::CharProps {
|
|
let style = if kind == craft_wordmodel::para::NoteKind::Endnote { "EndnoteReference" } else { "FootnoteReference" };
|
|
craft_wordmodel::CharProps { style: Some(style.into()), ..Default::default() }
|
|
}
|
|
|
|
fn blocks(&self, bl: &mut Blocks, own: Option<(craft_wordmodel::para::NoteKind, u32)>, depth: usize) {
|
|
for b in bl.iter_mut() {
|
|
let needs = match &**b {
|
|
Block::Para(p) => p.text.contains(MARK_OPEN),
|
|
Block::Table(_) => depth < 8,
|
|
};
|
|
if !needs {
|
|
continue;
|
|
}
|
|
match Arc::make_mut(b) {
|
|
Block::Para(p) => self.para(p, own),
|
|
Block::Table(t) => {
|
|
for r in &mut t.rows {
|
|
for c in &mut r.cells {
|
|
self.blocks(&mut c.blocks, own, depth + 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn para(&self, p: &mut Paragraph, own: Option<(craft_wordmodel::para::NoteKind, u32)>) {
|
|
// Every pass removes one marker, so this ends; the bound is only a seat belt.
|
|
for _ in 0..100_000 {
|
|
let Some(a) = p.text.find(MARK_OPEN) else { return };
|
|
let Some(len) = p.text.get(a..).and_then(|t| t.find(MARK_CLOSE)) else { return };
|
|
let end = a + len + MARK_CLOSE.len_utf8();
|
|
let inner = p.text.get(a + MARK_OPEN.len_utf8()..a + len).unwrap_or("").to_string();
|
|
let mut chars = inner.chars();
|
|
let tag = chars.next().unwrap_or(' ');
|
|
let n = chars.as_str().parse::<i64>().unwrap_or(i64::MIN);
|
|
let (obj, props) = match tag {
|
|
'N' => match usize::try_from(n).ok().and_then(|i| self.note_parts.get(i)) {
|
|
Some(&(kind, id)) => (Some(InlineObject::NoteRef { kind, id, custom: String::new() }), Self::ref_props(kind)),
|
|
None => (None, Default::default()),
|
|
},
|
|
'S' => match own {
|
|
Some((kind, id)) => (Some(InlineObject::NoteRef { kind, id, custom: String::new() }), Self::ref_props(kind)),
|
|
None => (None, Default::default()),
|
|
},
|
|
'B' => (self.by_rtf.get(&n).map(|&id| InlineObject::CommentStart { id }), Default::default()),
|
|
'E' => (self.by_rtf.get(&n).map(|&id| InlineObject::CommentEnd { id }), Default::default()),
|
|
_ => (None, Default::default()),
|
|
};
|
|
if p.delete(a, end).is_err() {
|
|
return;
|
|
}
|
|
if let Some(o) = obj
|
|
&& p.insert_object(a, o, &props).is_err()
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|