Files
craft-libs/crates/rtf/src/doc.rs
T
mmazzieriandMauro Mazzieri 5bd7eb9e88 craft-rtf: read text boxes and drawing shapes from \shpinst (#2059)
Ported from storytold/wordcraft#6242 (issue storytold/wordcraft#6212), written before the RTF reader
moved here. Rectangles, ellipses, triangles, diamonds, block arrows and text boxes are read from
the shape's properties (position, size, wrap, fill, line, text); the \defshp picture Word writes
after a shape it read, also inside \shppict, is not imported as a second copy.

Co-authored-by: Mauro Mazzieri <mauro.mazzieri@talete.net>
2026-11-18 15:31:52 -05:00

74 lines
3.6 KiB
Rust

//! Rich Text Format, read into and written from the word model (`craft_wordmodel::Document`)
//! through the text formats' flow (`craft_flow`). Feature `doc`.
//!
//! WordCraft's RTF reader and writer.
//!
//! Export: font and colour tables, a small stylesheet (headings with outline levels, Title,
//! Quote, Code), list tables (`\listtable` / `\listoverridetable` with `\listtext` labels for
//! older readers), document info, page setup, paragraph formatting (`\pard`, alignment, page
//! break before), character formatting (`\b \i \ul \strike \super \sub \f \fs \cf \chcbpat`),
//! hyperlinks as `HYPERLINK` fields, bookmarks, pictures (`\pngblip` / `\jpegblip`), tables
//! (`\trowd … \cellx … \cell … \row`, header rows, vertical merges, shading), footnotes and
//! endnotes (`{\super\chftn}{\footnote[\ftnalt] …}`) and comments (`{\*\atrfstart n}` …
//! `{\*\atrfend n}` around the commented text, then `{\*\atnid initials}{\*\atnauthor name}`
//! `\chatn{\*\annotation{\*\atnref n}{\*\atndate …}` text `}`).
//!
//! Import: a tolerant reader for groups and destinations (unknown `\*` destinations and objects
//! are skipped; headers, footers, notes and comments are read into stories of their own),
//! `\par \line \tab \page`, `\uN` with `\ucN`
//! fallback skipping, `\'hh` and 8-bit text in the document's code page (`\ansicpg`, `\mac`, `\pc`, `\pca`) or
//! the current font's (`\fcharset`, `\cpg`: Cyrillic, Central European, Greek, Mac OS Roman, the
//! East Asian multibyte pages 932/936/949/950…; Windows-1252 when none is named). A multibyte
//! code page's bytes stream through one decoder, so a lead byte and its trail byte decode
//! together and a run that looks like a UTF-8 BOM is not mistaken for one; the formatting words
//! above, stylesheet-based
//! headings and `\outlinelevel`, lists through `\ls`/`\ilvl`, fields (in `import`, a page field
//! such as `PAGE` or `NUMPAGES` whose result is plain text in one paragraph stays a field,
//! `\fldlock` locking it, and `\chpgn` is a `PAGE` field; otherwise a field is its result's text),
//! bookmarks, pictures and
//! tables, footnotes, endnotes (`\footnote` with `\ftnalt`) and comments (author, initials, date,
//! text, the commented range), drawing shapes and text boxes (`\shp`: rectangles, ellipses,
//! triangles, diamonds, block arrows and text boxes from `\shpinst`, with their place, wrapping,
//! fill, outline and text; their `\shprslt` copy and the `\defshp` picture after them are
//! skipped). Group nesting, picture sizes and the number of notes and comments are capped. Not
//! kept: custom note marks, comment replies (`\atnparent`), notes inside headers, footers, text
//! boxes and other notes, tables inside note and comment texts (flattened). Pictures, lines and
//! shapes in a group (`\shpgrp`) are read from their `\shprslt` copy.
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use craft_wordmodel::props::VAlign;
use craft_wordmodel::{Align, Block, Blocks, Document, InlineObject, Paragraph, Rgb};
use craft_flow::{self as model, Cell, FBlock, FTable, Flow, Fmt, Inline, Kind, ListInfo, Meta, Para, RevMark, make_img};
use crate::date;
mod export;
mod field;
mod parse;
mod reader;
mod shape;
mod state;
mod styles;
mod text;
mod word;
pub use date::{dttm_to_iso, iso_to_dttm};
pub use export::{GENERATOR, esc, export, export_with_generator};
#[cfg(test)]
use export::{Tables, Writer};
pub use parse::{import, parse};
use reader::Reader;
#[cfg(test)]
use state::MAX_SECTIONS;
#[cfg(test)]
use word::hyperlink;
#[cfg(test)]
mod tests;
#[cfg(test)]
mod tests_fields;
#[cfg(test)]
mod tests_shapes;