mirror of
https://github.com/storytold/deckcraft.git
synced 2026-11-21 20:39:56 +00:00
Outline RTF: write PowerPoint heading styles, keep empty and '- ' titles on open (#1446)
* Outline RTF: write PowerPoint's \s1-\s9 heading styles; keep empty titles and '- ' titles on open * Outline RTF: only a stylesheet 'heading N' style is a heading; an inherited title style keeps an explicitly ended empty title * Outline RTF: engine tests for empty and '- ' titles through file.open and file.openBytes * test(format): cover deep and Unicode RTF outlines * test: cover RTF outline edge cases
This commit is contained in:
@@ -172,9 +172,10 @@ fn open_other(name: &str, bytes: &[u8]) -> Result<(Presentation, Vec<String>, Ve
|
||||
let rtf = deckcraft_format::rtf::sniff_rtf(bytes);
|
||||
if rtf || lower.ends_with(".txt") || lower.ends_with(".md") {
|
||||
let text = String::from_utf8_lossy(bytes);
|
||||
let text = if rtf { std::borrow::Cow::Owned(deckcraft_format::rtf::rtf_to_outline(&text)) } else { text };
|
||||
let mut p = defaults::blank_presentation(defaults::WIDE, Default::default(), false);
|
||||
if lower.ends_with(".md") {
|
||||
if rtf {
|
||||
deckcraft_format::rtf::rtf_to_slides(&mut p, &text);
|
||||
} else if lower.ends_with(".md") {
|
||||
deckcraft_format::markdown_to_slides(&mut p, &text);
|
||||
} else {
|
||||
deckcraft_format::outline_to_slides(&mut p, &text);
|
||||
|
||||
@@ -2643,6 +2643,24 @@ fn outline_rtf_exports_and_opens_back() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// PowerPoint's outline RTF keeps an empty title and a title starting `- `, through both openers.
|
||||
#[test]
|
||||
fn outline_rtf_keeps_empty_and_dash_titles_through_file_open_and_open_bytes() {
|
||||
let rtf = r"{\rtf1\ansi{\stylesheet{\s1\li360 heading 1;}{\s2\li1080 heading 2;}}\pard\plain\s1 Roadmap\par\pard\plain\s2 Ship\par\pard\plain\s1 {\f3 \par }\pard\plain\s1 - dash\par}";
|
||||
let titles = |s: &Session| s.doc().unwrap().doc.slides.iter().map(|sl| sl.title()).collect::<Vec<_>>();
|
||||
let dir = std::env::temp_dir().join(format!("deckcraft-rtf-titles-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("deck.rtf");
|
||||
std::fs::write(&path, rtf).unwrap();
|
||||
let mut s = session();
|
||||
s.execute("file.open", &json!({"path": path.to_string_lossy()})).unwrap();
|
||||
assert_eq!(titles(&s), ["Roadmap", "", "- dash"]);
|
||||
let mut s = session();
|
||||
s.execute("file.openBytes", &json!({"name": "deck.rtf", "data": crate::cmd::base64_encode(rtf.as_bytes())})).unwrap();
|
||||
assert_eq!(titles(&s), ["Roadmap", "", "- dash"]);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// #725: a layout with a small body placeholder ahead of its content placeholder (as Keynote
|
||||
/// exports "Title & Bullets") still gets the body in the content placeholder (`idx="1"`).
|
||||
#[test]
|
||||
@@ -2747,6 +2765,13 @@ fn plain_outlines_are_unchanged() {
|
||||
assert_eq!(p.slides.first().map(|s| s.title()).as_deref(), Some("# Intro"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rtf_without_heading_paragraphs_opens_as_one_blank_slide() {
|
||||
let p = crate::cmd::file::open_presentation("empty.rtf", br"{\rtf1\ansi}").unwrap();
|
||||
assert_eq!(p.slides.len(), 1);
|
||||
assert_eq!(p.slides[0].title(), "");
|
||||
}
|
||||
|
||||
fn text_box_xfrm(s: &Session, id: u64) -> deckcraft_geom::Xfrm {
|
||||
let st = s.doc().unwrap();
|
||||
cmd::xfrm_of(&st.doc, &st.selection, st.shape(deckcraft_model::ShapeId(id as u32)).unwrap())
|
||||
|
||||
+169
-20
@@ -1,9 +1,11 @@
|
||||
//! Outline / RTF: PowerPoint's "Outline/RTF" save type, and reading such a file back as slides.
|
||||
//!
|
||||
//! The export writes one paragraph per slide title (outline level 0, bold, larger) and one per
|
||||
//! body paragraph (outline level = indent + 1, indented), keeping bold, italic and underline.
|
||||
//! The import reads paragraphs and their `\outlinelevel` (or, failing that, `\li` indent) into
|
||||
//! the tab-indented outline [`crate::outline_to_slides`] understands.
|
||||
//! body paragraph (outline level = indent + 1, indented), keeping bold, italic and underline. Each
|
||||
//! carries PowerPoint's `\s1`-`\s9` heading style, which is how PowerPoint finds the outline levels.
|
||||
//! The import reads paragraphs and their `\outlinelevel` (or, failing that, the `\s1`-`\s9` style,
|
||||
//! or the `\li` indent) into the tab-indented outline [`crate::outline_to_slides`] understands, or,
|
||||
//! with [`rtf_to_slides`], straight into slides that keep empty titles and titles starting `- `.
|
||||
|
||||
use deckcraft_model::Presentation;
|
||||
use deckcraft_model::text::{Paragraph, RunKind};
|
||||
@@ -53,10 +55,14 @@ fn runs(out: &mut String, p: &Paragraph) {
|
||||
|
||||
/// The presentation's outline (slide titles and body text) as RTF.
|
||||
pub fn slides_to_rtf(p: &Presentation) -> String {
|
||||
let mut out = String::from("{\\rtf1\\ansi\\ansicpg1252\\deff0\\uc1{\\fonttbl{\\f0\\fswiss Liberation Sans;}}\n");
|
||||
let mut out = String::from("{\\rtf1\\ansi\\ansicpg1252\\deff0\\uc1{\\fonttbl{\\f0\\fswiss Liberation Sans;}}\n{\\stylesheet");
|
||||
for n in 1..=9 {
|
||||
out.push_str(&format!("{{\\s{n}\\li{}\\fi-360\\level{n} heading {n};}}", 360 * n));
|
||||
}
|
||||
out.push_str("}\n");
|
||||
for s in &p.slides {
|
||||
let title = s.shapes.iter().find(|sh| sh.ph_type().is_some_and(|k| k.is_title())).and_then(|sh| sh.text.as_ref());
|
||||
out.push_str("\\pard\\outlinelevel0\\sb240\\b\\fs32 ");
|
||||
out.push_str("\\pard\\s1\\outlinelevel0\\sb240\\b\\fs32 ");
|
||||
// The title stays one outline paragraph (a `\par` would start another slide on re-import),
|
||||
// so its paragraphs are joined with line breaks.
|
||||
let mut tps = title.into_iter().flat_map(|t| &t.paragraphs).filter(|p| !p.is_empty()).peekable();
|
||||
@@ -78,7 +84,7 @@ pub fn slides_to_rtf(p: &Presentation) -> String {
|
||||
let Some(t) = &sh.text else { continue };
|
||||
for para in t.paragraphs.iter().filter(|p| !p.is_empty()) {
|
||||
let lvl = para.level.min(8) as u32;
|
||||
out.push_str(&format!("\\pard\\outlinelevel{}\\li{}\\fs24 ", lvl + 1, 360 * (lvl + 1)));
|
||||
out.push_str(&format!("\\pard\\s{}\\outlinelevel{}\\li{}\\fs24 ", (lvl + 2).min(9), lvl + 1, 360 * (lvl + 1)));
|
||||
runs(&mut out, para);
|
||||
out.push_str("\\par\n");
|
||||
}
|
||||
@@ -142,15 +148,97 @@ struct Outline<'a> {
|
||||
stack: Vec<St>,
|
||||
st: St,
|
||||
level: Option<u32>,
|
||||
/// The level a `\s1`-`\s9` heading style gives, used when no `\outlinelevel` is set.
|
||||
style_level: Option<u32>,
|
||||
indent: i64,
|
||||
/// Which `\s1`-`\s9` the stylesheet names "heading N"; any other `\sN` is a body style.
|
||||
headings: [bool; 10],
|
||||
pending_skip: usize,
|
||||
line: String,
|
||||
out: String,
|
||||
/// Paragraphs so far: outline level and text (empty only for an empty level-0 title).
|
||||
out: Vec<(usize, String)>,
|
||||
}
|
||||
|
||||
/// Read an RTF outline into tab-indented outline text: a paragraph's outline level (or its left
|
||||
/// indent in 0.25" steps) becomes its number of tabs.
|
||||
pub fn rtf_to_outline(rtf: &str) -> String {
|
||||
let mut out = String::new();
|
||||
for (n, text) in paragraphs(rtf).into_iter().filter(|(_, t)| !t.is_empty()) {
|
||||
out.push_str(&"\t".repeat(n));
|
||||
out.push_str(&text);
|
||||
out.push('\n');
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Append a slide per level-0 paragraph of an RTF outline, its deeper paragraphs the body; unlike
|
||||
/// [`rtf_to_outline`] then [`crate::outline_to_slides`], an empty title makes an (untitled) slide
|
||||
/// and a title starting `- ` keeps it, as PowerPoint's own outline export has both.
|
||||
pub fn rtf_to_slides(p: &mut Presentation, rtf: &str) -> usize {
|
||||
let mut slides: crate::OutlineSlides = Vec::new();
|
||||
for (n, text) in paragraphs(rtf) {
|
||||
if n == 0 {
|
||||
slides.push((text, Vec::new()));
|
||||
} else if let Some((_, body)) = slides.last_mut() {
|
||||
body.push(((n - 1).min(8) as u8, text));
|
||||
}
|
||||
}
|
||||
crate::add_outline_slides(p, slides)
|
||||
}
|
||||
|
||||
/// The `\sN` styles the stylesheet calls "heading N": `{\s1\li360 heading 1;}`.
|
||||
fn heading_styles(rtf: &str) -> [bool; 10] {
|
||||
let mut out = [false; 10];
|
||||
let Some(at) = rtf.find("\\stylesheet") else { return out };
|
||||
let b = rtf.as_bytes();
|
||||
let (mut i, mut depth) = (at + "\\stylesheet".len(), 1);
|
||||
let (mut style, mut name): (Option<usize>, String) = (None, String::new());
|
||||
while i < b.len() && depth > 0 {
|
||||
match b[i] {
|
||||
b'{' => {
|
||||
depth += 1;
|
||||
(style, name) = (None, String::new());
|
||||
}
|
||||
b'}' => {
|
||||
depth -= 1;
|
||||
if depth == 1
|
||||
&& let Some(n) = style.filter(|n| (1..=9).contains(n))
|
||||
{
|
||||
out[n] = name.trim().to_ascii_lowercase().starts_with("heading");
|
||||
}
|
||||
}
|
||||
b'\\' => {
|
||||
let w = i + 1;
|
||||
let mut j = w;
|
||||
while j < b.len() && b[j].is_ascii_alphabetic() {
|
||||
j += 1;
|
||||
}
|
||||
let k = j;
|
||||
while j < b.len() && (b[j].is_ascii_digit() || j == k && b[j] == b'-') {
|
||||
j += 1;
|
||||
}
|
||||
if depth == 2 && &rtf[w..k] == "s" {
|
||||
style = rtf[k..j].parse().ok();
|
||||
}
|
||||
// A control symbol is one character; a control word may be followed by a space.
|
||||
i = if k == w {
|
||||
w + 1
|
||||
} else if b.get(j) == Some(&b' ') {
|
||||
j + 1
|
||||
} else {
|
||||
j
|
||||
};
|
||||
continue;
|
||||
}
|
||||
c if depth == 2 && !matches!(c, b'\r' | b'\n') => name.push(c as char),
|
||||
_ => {}
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn paragraphs(rtf: &str) -> Vec<(usize, String)> {
|
||||
let mut p = Outline {
|
||||
rtf,
|
||||
b: rtf.as_bytes(),
|
||||
@@ -158,14 +246,16 @@ pub fn rtf_to_outline(rtf: &str) -> String {
|
||||
stack: vec![],
|
||||
st: St { skip: false, uc: 1 },
|
||||
level: None,
|
||||
style_level: None,
|
||||
indent: 0,
|
||||
headings: heading_styles(rtf),
|
||||
pending_skip: 0,
|
||||
line: String::new(),
|
||||
out: String::new(),
|
||||
out: vec![],
|
||||
};
|
||||
p.run();
|
||||
p.flush();
|
||||
finish_surrogates(p.out)
|
||||
p.flush(false);
|
||||
p.out.into_iter().map(|(n, t)| (n, finish_surrogates(t))).collect()
|
||||
}
|
||||
|
||||
impl Outline<'_> {
|
||||
@@ -246,12 +336,18 @@ impl Outline<'_> {
|
||||
|
||||
fn apply(&mut self, word: &str, arg: Option<i64>) {
|
||||
match word {
|
||||
"par" => self.flush(),
|
||||
"par" => self.flush(true),
|
||||
"pard" => {
|
||||
self.level = None;
|
||||
self.style_level = None;
|
||||
self.indent = 0;
|
||||
}
|
||||
"outlinelevel" => self.level = arg.map(|a| a.clamp(0, 9) as u32),
|
||||
"outlinelevel" => {
|
||||
self.level = arg.map(|a| a.clamp(0, 9) as u32);
|
||||
}
|
||||
"s" => {
|
||||
self.style_level = arg.filter(|a| (1..=9).contains(a) && self.headings[*a as usize]).map(|a| a as u32 - 1);
|
||||
}
|
||||
"li" => self.indent = arg.unwrap_or(0),
|
||||
"tab" | "line" => self.line.push(' '),
|
||||
"uc" => self.st.uc = arg.unwrap_or(1).clamp(0, 8) as usize,
|
||||
@@ -312,7 +408,7 @@ impl Outline<'_> {
|
||||
b'\\' | b'{' | b'}' => self.line.push(n as char),
|
||||
b'~' => self.line.push('\u{a0}'),
|
||||
b'_' => self.line.push('\u{2011}'),
|
||||
b'\n' | b'\r' => self.flush(),
|
||||
b'\n' | b'\r' => self.flush(true),
|
||||
_ if !n.is_ascii() => self.line.extend(c),
|
||||
_ => {}
|
||||
}
|
||||
@@ -331,14 +427,15 @@ impl Outline<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Ends the current line: a non-empty line becomes one outline entry, indented by its level.
|
||||
fn flush(&mut self) {
|
||||
/// Ends the current line: a non-empty line becomes one entry at its level, and so does an empty
|
||||
/// one that a `\par` ends while its style is a title (`\s1` or `\outlinelevel0`, inherited or
|
||||
/// not): PowerPoint writes an untitled slide so. The flush at the end of the input is no `\par`.
|
||||
fn flush(&mut self, par: bool) {
|
||||
let text = self.line.trim();
|
||||
if !text.is_empty() {
|
||||
let n = self.level.map(|l| l as i64).unwrap_or(self.indent.saturating_add(180) / 360).clamp(0, 9) as usize;
|
||||
self.out.push_str(&"\t".repeat(n));
|
||||
self.out.push_str(text);
|
||||
self.out.push('\n');
|
||||
let explicit = self.level.or(self.style_level);
|
||||
if !text.is_empty() || par && explicit == Some(0) {
|
||||
let n = explicit.map(|l| l as i64).unwrap_or(self.indent.saturating_add(180) / 360).clamp(0, 9) as usize;
|
||||
self.out.push((n, text.to_string()));
|
||||
}
|
||||
self.line.clear();
|
||||
}
|
||||
@@ -537,6 +634,58 @@ mod tests {
|
||||
assert_eq!(rtf_to_outline(&rtf), crate::slides_to_outline(&p));
|
||||
}
|
||||
|
||||
/// PowerPoint writes `\\s1`-`\\s9` (no `\\outlinelevel`), an untitled slide as an empty `\\s1`
|
||||
/// paragraph, and a title may start `- `; all three must survive reading.
|
||||
#[test]
|
||||
fn reads_powerpoint_outline_with_styles_and_empty_titles() {
|
||||
let rtf = r"{\rtf1\ansi{\stylesheet{\s1\li360 heading 1;}{\s2\li1080 heading 2;}{\s3\li1800 heading 3;}}\pard\plain\s1\li0 {\f1 Title one}{\f1 \par }\pard\plain\s2\li360 {\pntext\pard\plain\f3 \'95\tab}{\f2 Body a\par }\pard\plain\s3\li1080 Body b\par\pard\plain\s1 {\f3 \par }\pard\plain\s1 - dash\par\pard\plain\s2 x\par}";
|
||||
assert_eq!(rtf_to_outline(rtf), "Title one\n\tBody a\n\t\tBody b\n- dash\n\tx\n");
|
||||
let mut p = deckcraft_model::defaults::blank_presentation(deckcraft_model::defaults::WIDE, Default::default(), false);
|
||||
assert_eq!(rtf_to_slides(&mut p, rtf), 3);
|
||||
assert_eq!(crate::slides_to_outline(&p), "Title one\n\tBody a\n\t\tBody b\n\n- dash\n\tx\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_style_the_stylesheet_does_not_call_a_heading_is_a_body_paragraph() {
|
||||
let rtf = r"{\rtf1\ansi{\stylesheet{\s1\li720 Indented body;}{\s2\li360 heading 2;}}\pard\s1\li720 Point\par\pard\s2\li360 Sub\par}";
|
||||
assert_eq!(rtf_to_outline(rtf), "\t\tPoint\n\tSub\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_title_after_an_inherited_heading_style_is_kept() {
|
||||
let rtf = r"{\rtf1\ansi{\stylesheet{\s1\li360 heading 1;}}\pard\s1 A\par\par B\par}";
|
||||
let mut p = deckcraft_model::defaults::blank_presentation(deckcraft_model::defaults::WIDE, Default::default(), false);
|
||||
assert_eq!(rtf_to_slides(&mut p, rtf), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn outline_rtf_without_heading_styles_keeps_unicode_and_caps_deep_levels() {
|
||||
let rtf = r"{\rtf1\ansi\pard\li0 Caf\'e9 \uc1\u233?\par\pard\li3600 Deep\par}";
|
||||
let mut p = deckcraft_model::defaults::blank_presentation(deckcraft_model::defaults::WIDE, Default::default(), false);
|
||||
assert_eq!(rtf_to_slides(&mut p, rtf), 1);
|
||||
assert_eq!(crate::slides_to_outline(&p), "Café é\n\t\t\t\t\t\t\t\t\tDeep\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escaped_unicode_titles_and_levels_beyond_nine_survive_slide_import() {
|
||||
let rtf = r"{\rtf1\ansi\pard\outlinelevel0 Caf\'e9 \uc1\u233?\par\pard\outlinelevel10 Deep\par}";
|
||||
let mut p = deckcraft_model::defaults::blank_presentation(deckcraft_model::defaults::WIDE, Default::default(), false);
|
||||
assert_eq!(rtf_to_slides(&mut p, rtf), 1);
|
||||
let expected = "Café é\n\t\t\t\t\t\t\t\t\tDeep\n";
|
||||
assert_eq!(crate::slides_to_outline(&p), expected);
|
||||
let mut reopened = deckcraft_model::defaults::blank_presentation(deckcraft_model::defaults::WIDE, Default::default(), false);
|
||||
rtf_to_slides(&mut reopened, &slides_to_rtf(&p));
|
||||
assert_eq!(crate::slides_to_outline(&reopened), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_writes_heading_styles() {
|
||||
let mut p = deckcraft_model::defaults::blank_presentation(deckcraft_model::defaults::WIDE, Default::default(), false);
|
||||
crate::outline_to_slides(&mut p, "A\n\tB\n\t\tC\n");
|
||||
let rtf = slides_to_rtf(&p);
|
||||
assert!(rtf.contains("{\\s9\\li3240") && rtf.contains("\\pard\\s1\\outlinelevel0") && rtf.contains("\\pard\\s3\\outlinelevel2"), "{rtf}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_word_style_rtf_by_indent_and_hex_escapes() {
|
||||
let rtf = r"{\rtf1\ansi{\fonttbl{\f0 Arial;}}{\*\generator X;}\pard\b Title one\b0\par\pard\li720 Point \'93a\'94\par\pard\li1440 Sub\par}";
|
||||
|
||||
Reference in New Issue
Block a user