mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
* Add craft-wordmodel: WordCraft's document model without its editing code WordCraft's crates/doc (wordcraft-doc, at 07f97737) moved as it is, so the DOCX, RTF, ODT and other text-format crates can read into and write from one model. Module paths, type names and serde attributes are unchanged: an app that re-exports the crate keeps its native file format. Left in the app: range editing (edit.rs), IME text and equation editing. Not model, and already in craft-libs: code pages (craft-rtf), the ZIP entry-count check (craft-opc), the compound-file guard (craft-cfb). The paragraph walks moved to walk.rs and the unit helpers of wordcraft-geom to geom.rs. * craft-wordmodel: math_tree, the arguments of an equation node and a walk over its runs The DOCX reader and writer walk an equation's runs. The walk and the child accessors it uses were in WordCraft's equation editor (math_edit.rs), which stays in the app; they are structure, not editing. * craft-wordmodel: keep the document operations (edit.rs) with the model The DOCX and RTF readers use them: alternative-format chunks are merged into a document with adopt_blocks, and a reader formats ranges with format_range and inserts objects. They operate on the document, not on a view, so they belong beside it. The tests that drove them come back with them; the paragraph walks stay in walk.rs and edit re-exports them. base64 0.23, as WordCraft and craft-offcrypto use.
204 lines
7.7 KiB
Rust
204 lines
7.7 KiB
Rust
use super::block::{Block, Blocks};
|
||
use super::parts::PartKind;
|
||
use super::position::Path;
|
||
use super::{InlineObject, Numbering, numbering, para, section, styles};
|
||
|
||
/// Words in `s`, as Word counts them: tokens separated by whitespace that contain at least one
|
||
/// alphanumeric char, plus each Han ideograph and kana as a word of its own (Chinese and Japanese
|
||
/// are written without spaces, #4547; Korean is spaced, so Hangul counts by tokens).
|
||
pub fn count_words(s: &str) -> usize {
|
||
// The half-width voiced sound marks belong to the kana before them, not a word of their own.
|
||
let word_char = |c: char| c.is_alphanumeric() && !matches!(c, '\u{FF9E}' | '\u{FF9F}');
|
||
let tokens = s.split(|c: char| c.is_whitespace() || c == para::OBJ || is_cjk_word(c)).filter(|w| w.chars().any(word_char)).count();
|
||
tokens.saturating_add(s.chars().filter(|c| is_cjk_word(*c)).count())
|
||
}
|
||
|
||
/// Words in a generated list label: whitespace-separated tokens holding a letter or digit, so a
|
||
/// CJK label such as 第十二章 stays one word (how Word counts those is unverified).
|
||
fn count_label_words(s: &str) -> usize {
|
||
s.split(char::is_whitespace).filter(|w| w.chars().any(char::is_alphanumeric)).count()
|
||
}
|
||
|
||
/// A Han ideograph or kana letter (not CJK punctuation), which Word counts as one word.
|
||
fn is_cjk_word(c: char) -> bool {
|
||
matches!(c,
|
||
'\u{3005}'..='\u{3007}' // 々 〆 〇
|
||
| '\u{3031}'..='\u{3035}' | '\u{303B}' // vertical kana repeat marks, 〻
|
||
| '\u{3040}'..='\u{30FF}' // hiragana, katakana
|
||
| '\u{31F0}'..='\u{31FF}' // katakana phonetic extensions
|
||
| '\u{3400}'..='\u{4DBF}' // extension A
|
||
| '\u{4E00}'..='\u{9FFF}' // unified ideographs
|
||
| '\u{F900}'..='\u{FAFF}' // compatibility ideographs
|
||
| '\u{FF66}'..='\u{FF9D}' // half-width katakana
|
||
| '\u{1B000}'..='\u{1B16F}' // kana supplement and extended
|
||
| '\u{20000}'..='\u{3FFFF}' // extensions B and later
|
||
) && c.is_alphanumeric()
|
||
}
|
||
|
||
/// Characters in `s`, as the Word Count dialog counts them ("Characters (with spaces)"):
|
||
/// paragraph breaks aren't characters (#2797).
|
||
pub fn count_chars(s: &str) -> usize {
|
||
s.chars().filter(|c| *c != '\n' && *c != '\r').count()
|
||
}
|
||
|
||
/// Characters in `s` ignoring every whitespace character, as the Word Count dialog counts them
|
||
/// ("Characters (no spaces)"; #2797).
|
||
pub fn count_chars_no_spaces(s: &str) -> usize {
|
||
s.chars().filter(|c| !c.is_whitespace()).count()
|
||
}
|
||
|
||
/// How deep in nested tables [`Document::para_paths`] goes: cells below this are skipped.
|
||
pub const PARA_WALK_DEPTH: usize = 16;
|
||
|
||
/// The note and text box stories an object shows, for `Document::reachable`.
|
||
pub fn notes_and_boxes(o: &InlineObject) -> Vec<(u32, &'static [PartKind])> {
|
||
match o {
|
||
InlineObject::NoteRef { id, .. } => vec![(*id, &[PartKind::Footnote, PartKind::Endnote][..])],
|
||
o => o.text_boxes().into_iter().map(|id| (id, &[PartKind::TextBox][..])).collect(),
|
||
}
|
||
}
|
||
|
||
pub fn walk(bl: &Blocks, prefix: &mut Vec<u32>, out: &mut Vec<Path>, depth: usize) {
|
||
if depth > PARA_WALK_DEPTH {
|
||
return;
|
||
}
|
||
for (i, b) in bl.iter().enumerate() {
|
||
crate::work::steps(1);
|
||
prefix.push(i as u32);
|
||
match &**b {
|
||
Block::Para(_) => out.push(Path(prefix.clone())),
|
||
Block::Table(t) => {
|
||
for (r, row) in t.rows.iter().enumerate() {
|
||
for (c, cell) in row.cells.iter().enumerate() {
|
||
prefix.push(r as u32);
|
||
prefix.push(c as u32);
|
||
walk(&cell.blocks, prefix, out, depth + 1);
|
||
prefix.pop();
|
||
prefix.pop();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
prefix.pop();
|
||
}
|
||
}
|
||
|
||
/// [`walk`] for the paragraphs from path `a` to `b` inclusive, skipping the blocks outside.
|
||
pub fn walk_between(bl: &Blocks, prefix: &mut Vec<u32>, out: &mut Vec<Path>, depth: usize, (a, b): (&[u32], &[u32])) {
|
||
use std::cmp::Ordering::{Greater, Less};
|
||
if depth > PARA_WALK_DEPTH {
|
||
return;
|
||
}
|
||
// Where the paths starting with `q` lie against `x`: all before, all after, or either.
|
||
let side = |q: &[u32], x: &[u32]| {
|
||
let m = q.len().min(x.len());
|
||
q[..m].cmp(&x[..m])
|
||
};
|
||
for (i, blk) in bl.iter().enumerate() {
|
||
prefix.push(i as u32);
|
||
if side(prefix, b) == Greater {
|
||
prefix.pop();
|
||
break;
|
||
}
|
||
if side(prefix, a) != Less {
|
||
match &**blk {
|
||
Block::Para(_) => {
|
||
if prefix.as_slice() >= a && prefix.as_slice() <= b {
|
||
out.push(Path(prefix.clone()));
|
||
}
|
||
}
|
||
Block::Table(t) => {
|
||
for (r, row) in t.rows.iter().enumerate() {
|
||
for (c, cell) in row.cells.iter().enumerate() {
|
||
prefix.push(r as u32);
|
||
prefix.push(c as u32);
|
||
if side(prefix, a) != Less && side(prefix, b) != Greater {
|
||
walk_between(&cell.blocks, prefix, out, depth + 1, (a, b));
|
||
}
|
||
prefix.pop();
|
||
prefix.pop();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
prefix.pop();
|
||
}
|
||
}
|
||
|
||
/// Which text of a paragraph [`text_of`] collects.
|
||
#[derive(Clone, Copy)]
|
||
pub enum TextMode {
|
||
/// Every run, tracked deletions included ([`Paragraph::plain_text`]).
|
||
Plain,
|
||
/// As shown on screen ([`Paragraph::shown_text`]).
|
||
Shown,
|
||
/// Without tracked deletions ([`Paragraph::final_text`]).
|
||
Final,
|
||
}
|
||
|
||
pub fn text_of(bl: &Blocks, out: &mut String, depth: usize, mode: TextMode) {
|
||
if depth > 16 {
|
||
return;
|
||
}
|
||
for b in bl {
|
||
match &**b {
|
||
Block::Para(p) => {
|
||
out.push_str(&match mode {
|
||
TextMode::Plain => p.plain_text(),
|
||
TextMode::Shown => p.shown_text(),
|
||
TextMode::Final => p.final_text(),
|
||
});
|
||
out.push('\n');
|
||
}
|
||
Block::Table(t) => {
|
||
for row in &t.rows {
|
||
let mut cells = Vec::new();
|
||
for c in &row.cells {
|
||
let mut s = String::new();
|
||
text_of(&c.blocks, &mut s, depth + 1, mode);
|
||
cells.push(s.trim_end_matches('\n').replace('\n', " "));
|
||
}
|
||
out.push_str(&cells.join("\t"));
|
||
out.push('\n');
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
pub fn story_word_count_of(
|
||
bl: &Blocks,
|
||
numbering: &Numbering,
|
||
styles: &styles::StyleSheet,
|
||
counters: &mut numbering::Counters,
|
||
depth: usize,
|
||
) -> usize {
|
||
if depth > 16 {
|
||
return 0;
|
||
}
|
||
let mut total = 0usize;
|
||
for b in bl {
|
||
match &**b {
|
||
Block::Para(p) => {
|
||
let n = p.props.numbering.or_else(|| styles.resolve_para(&p.props).numbering).filter(|n| n.num != 0);
|
||
if let Some(n) = n
|
||
&& let Some((label, def)) = counters.next_label(numbering, n.num, n.level)
|
||
&& !matches!(def.format, section::NumFormat::Bullet | section::NumFormat::None)
|
||
{
|
||
total = total.saturating_add(count_label_words(&label));
|
||
}
|
||
total = total.saturating_add(count_words(&p.plain_text()));
|
||
}
|
||
Block::Table(t) => {
|
||
for row in &t.rows {
|
||
for c in &row.cells {
|
||
total = total.saturating_add(story_word_count_of(&c.blocks, numbering, styles, counters, depth + 1));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
total
|
||
}
|