craft-rtf: add OutlineWriter for PowerPoint-compatible Outline/RTF (refs #778) (#2494)

This commit is contained in:
Dylan M. Taylor
2026-11-19 23:15:41 -05:00
committed by GitHub
parent 057b0e3e01
commit d0193d7e94
3 changed files with 107 additions and 1 deletions
+1 -1
View File
@@ -46,4 +46,4 @@ pub use lex::{Token, Tokens, is_rtf};
pub use model::{Align, Block, COLUMN_BREAK, Cell, CharFmt, Document, LINE_BREAK, PAGE_BREAK, Para, Run, Table, VertAlign};
pub use read::{MAX_COLORS, MAX_FONT_NAME, MAX_FONTS, MAX_GROUPS, NotRtf, read};
pub use text::TextDecoder;
pub use write::{esc, esc_font, push_esc, write};
pub use write::{OutlineWriter, esc, esc_font, push_esc, write};
+75
View File
@@ -264,6 +264,81 @@ pub fn write(doc: &Document, default_font: &str) -> String {
o
}
/// Writes PowerPoint-compatible Outline/RTF (`\s1`–`\s9` heading styles and `\outlinelevel`).
///
/// Each slide starts with a level-0 title paragraph (`\s1\outlinelevel0\sb240\b\fs32 …\b0\par`),
/// followed by body paragraphs at indent level `0..=8` (`\s2`–`\s9`, `\outlinelevel1`–`9`,
/// `\li360`–`3240`, `\fs24`).
pub struct OutlineWriter {
out: String,
}
impl OutlineWriter {
/// Starts an Outline/RTF document with `default_font` as `\f0\fswiss` and the `\s1`–`\s9`
/// heading stylesheet.
pub fn new(default_font: &str) -> Self {
let mut out = format!("{{\\rtf1\\ansi\\ansicpg1252\\deff0\\uc1{{\\fonttbl{{\\f0\\fswiss {};}}}}\n{{\\stylesheet", esc_font(default_font));
for n in 1..=9 {
let _ = write!(out, "{{\\s{n}\\li{}\\fi-360\\level{n} heading {n};}}", 360 * n);
}
out.push_str("}\n");
Self { out }
}
/// Starts a slide title paragraph (`\pard\s1\outlinelevel0\sb240\b\fs32 `).
pub fn begin_title(&mut self) {
self.out.push_str("\\pard\\s1\\outlinelevel0\\sb240\\b\\fs32 ");
}
/// Ends a slide title paragraph (`\b0\par\n`).
pub fn end_title(&mut self) {
self.out.push_str("\\b0\\par\n");
}
/// Starts a body paragraph at outline indent `level` (`0..=8`, clamped).
pub fn begin_body(&mut self, level: u8) {
let lvl = u32::from(level.min(8));
let _ = write!(self.out, "\\pard\\s{}\\outlinelevel{}\\li{}\\fs24 ", (lvl + 2).min(9), lvl + 1, 360 * (lvl + 1));
}
/// Ends a body paragraph (`\par\n`).
pub fn end_body(&mut self) {
self.out.push_str("\\par\n");
}
/// Appends an inline line break (`\line `).
pub fn line_break(&mut self) {
self.out.push_str("\\line ");
}
/// Appends a text run, wrapping in `{\b\i\ul ...}` when any of those flags are set.
pub fn run(&mut self, text: &str, bold: bool, italic: bool, underline: bool) {
if !(bold || italic || underline) {
push_esc(&mut self.out, text);
return;
}
self.out.push('{');
if bold {
self.out.push_str("\\b");
}
if italic {
self.out.push_str("\\i");
}
if underline {
self.out.push_str("\\ul");
}
self.out.push(' ');
push_esc(&mut self.out, text);
self.out.push('}');
}
/// Closes the RTF document (`}\n`) and returns the output.
pub fn finish(mut self) -> String {
self.out.push_str("}\n");
self.out
}
}
#[cfg(test)]
#[path = "write_tests.rs"]
mod tests;
+31
View File
@@ -36,3 +36,34 @@ fn fonts_match_without_regard_to_ascii_case() {
// Both spellings of Arial are font 1; the default font's run names no font.
assert!(out.contains("{\\f1 x}{\\f1 x}x\\par"), "{out}");
}
#[test]
fn outline_writer_emits_powerpoint_headings_runs_and_unicode() {
let mut w = OutlineWriter::new("Liberation Sans");
w.begin_title();
w.run("First", true, false, false);
w.line_break();
w.run("Café 😀", false, true, true);
w.end_title();
w.begin_body(0);
w.run("Body", false, false, false);
w.end_body();
w.begin_body(12);
w.run("Deep", false, false, false);
w.end_body();
// Empty untitled slide title.
w.begin_title();
w.run("", false, false, false);
w.end_title();
let rtf = w.finish();
assert!(rtf.contains("{\\f0\\fswiss Liberation Sans;}"), "{rtf}");
assert!(rtf.contains("{\\s9\\li3240\\fi-360\\level9 heading 9;}"), "{rtf}");
assert!(rtf.contains("\\pard\\s1\\outlinelevel0\\sb240\\b\\fs32 {\\b First}\\line {\\i\\ul Caf\\u233? \\u-10179?\\u-8704?}\\b0\\par\n"), "{rtf}");
assert!(rtf.contains("\\pard\\s2\\outlinelevel1\\li360\\fs24 Body\\par\n"), "{rtf}");
assert!(rtf.contains("\\pard\\s9\\outlinelevel9\\li3240\\fs24 Deep\\par\n"), "{rtf}");
assert!(rtf.contains("\\pard\\s1\\outlinelevel0\\sb240\\b\\fs32 \\b0\\par\n"), "{rtf}");
let doc = crate::read(rtf.as_bytes()).unwrap();
assert_eq!(doc.blocks.len(), 4);
}