mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:55:14 +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. * craft-docx: WordCraft's crates/docx as it is at wordcraft 07f97737 The source, unchanged, so the next commit shows only what the move changes. This commit does not build on its own (it still names wordcraft-doc and wordcraft-geom, and is not a workspace member yet). * craft-docx: on craft-wordmodel, craft-opc, craft-xml and craft-offcrypto - The package (zip reading, part names, relationships, content types, zip writing) is craft-opc's; the module keeps WordCraft's bookkeeping (passthrough keys, the embedded-part manifest) and its own Rel/Rels shape. - Parts are parsed with craft-xml's canonical names (the Strict drawing names and mc:Choice/@Requires through its hooks), then given the tree shape the reader was written against. - Encryption and Restrict Editing hashes are craft-offcrypto's; this crate maps its errors and the w:documentProtection attributes. - wordcraft-doc is craft-wordmodel; wordcraft-geom's helpers are its geom. Tests that covered what now lives in craft-opc and craft-offcrypto are left to those crates; the DIFAT regression is craft-cfb's. Test-only zip use follows zip 9's fallible names. The attribute-cap test times the read only.
67 lines
3.4 KiB
Rust
67 lines
3.4 KiB
Rust
//! The document's text watermark as a VML text shape in the headers (ECMA-376 Part 4, VML:
|
|
//! `shape`, `shapetype`, `path`, `textpath`, `fill`), which is how a WordprocessingML consumer
|
|
//! draws text behind every page. The reader (`read::story::vml_watermark`) maps it back.
|
|
|
|
use craft_wordmodel::{SectionProps, Watermark};
|
|
|
|
use crate::xml::esc;
|
|
|
|
/// The shape type the watermark shape uses: two straight subpaths, the box's top and bottom
|
|
/// edges, between which the `v:textpath` text is stretched to fit the shape (`fitshape`). A single
|
|
/// path gives the text no height, and Word draws nothing.
|
|
const SHAPETYPE_ID: &str = "_x0000_t136";
|
|
const SHAPETYPE_PATH: &str = "m0,0l21600,0m0,21600l21600,21600e";
|
|
|
|
/// The watermark text as written: what the reader accepts (non-blank, at most 200 characters).
|
|
pub(super) fn text(wm: &Watermark) -> Option<String> {
|
|
let t: String = wm.text.chars().take(200).collect();
|
|
(!t.trim().is_empty()).then_some(t)
|
|
}
|
|
|
|
/// A `w:r` holding the watermark shape (and its shape type), for the first paragraph of header
|
|
/// part number `n`, in its box ([`Watermark::frame`]: its own, or Word's default for the page).
|
|
pub(super) fn run(wm: &Watermark, n: usize, page: &SectionProps) -> Option<String> {
|
|
let text = text(wm)?;
|
|
let [width, height] = wm.frame(page.page_w, page.page_h);
|
|
// A font name can't break out of the textpath's CSS-like style or make it malformed (#3994):
|
|
// the characters a quoted CSS string can't carry raw (`\22 ` is `"`, `\3b ` `;`, `\3a ` `:`,
|
|
// `\5c ` `\`) are escaped and control characters are dropped (no font name carries one); the
|
|
// reader (`read::story::css_string`) maps the escapes back. An apostrophe is legal inside a
|
|
// double-quoted string, so it is kept.
|
|
let mut font = String::with_capacity(wm.font.len());
|
|
for c in wm.font.chars().filter(|c| !c.is_control()).take(100) {
|
|
match c {
|
|
'"' => font.push_str(r"\22 "),
|
|
';' => font.push_str(r"\3b "),
|
|
':' => font.push_str(r"\3a "),
|
|
'\\' => font.push_str(r"\5c "),
|
|
c => font.push(c),
|
|
}
|
|
}
|
|
let font = if font.trim().is_empty() { Watermark::default().font } else { font };
|
|
let rotation = if wm.diagonal { "rotation:315;" } else { "" };
|
|
let style = format!(
|
|
"position:absolute;margin-left:0;margin-top:0;width:{width:.1}pt;height:{height:.1}pt;{rotation}z-index:-251654144;\
|
|
mso-position-horizontal:center;mso-position-horizontal-relative:margin;\
|
|
mso-position-vertical:center;mso-position-vertical-relative:margin"
|
|
);
|
|
let fill = if wm.semitransparent { r#"<v:fill opacity=".5"/>"# } else { "" };
|
|
Some(format!(
|
|
concat!(
|
|
r#"<w:r><w:rPr><w:noProof/></w:rPr><w:pict>"#,
|
|
r#"<v:shapetype id="{st}" coordsize="21600,21600" o:spt="136" path="{path}">"#,
|
|
r#"<v:path textpathok="t"/><v:textpath on="t" fitshape="t"/></v:shapetype>"#,
|
|
r##"<v:shape id="PowerPlusWaterMarkObject{n}" type="#{st}" style="{style}" o:allowincell="f" fillcolor="#{color}" stroked="f">"##,
|
|
r#"{fill}<v:textpath style="{tstyle}" string="{text}"/></v:shape></w:pict></w:r>"#
|
|
),
|
|
st = SHAPETYPE_ID,
|
|
path = SHAPETYPE_PATH,
|
|
n = n,
|
|
style = esc(&style),
|
|
color = wm.color.hex(),
|
|
fill = fill,
|
|
tstyle = esc(&format!("font-family:\"{font}\";font-size:1pt")),
|
|
text = esc(&text),
|
|
))
|
|
}
|