Registry / RepositoryTypeScriptRustPython
Rollpie
ReadmeFiles
Versions
Info
Download
0.1.199.9 KB2026-09-230.1.098.9 KB2026-09-14
Version
0.1.1
Copyright
Rollpie, Irohabook
Publisher
math
Published
2026-09-23
Size
99.9 KB
Downloads
15
Checksum
e691f02f331393c7b5c3406746caa7ed2636c7af24b1cef422bc957ed16ecd74
Dependencies
None

presentation.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! プレゼンテーション。pptx と ppt を開く
//!
//! ```no_run
//! use folio::presentation::Presentation;
//! use std::path::Path;
//!
//! let mut file = Presentation::open(Path::new("deck.pptx")).unwrap();
//!
//! for index in 0..file.count() {
//!     println!("{}", file.read(index).unwrap().text());
//! }
//! ```

use crate::error::Error;
use crate::package::Package;
use crate::slide::{self, Slide};
use crate::xml::{Event, Reader};
use std::path::Path;


/// どちらの形式を開いたか
enum Origin {
    /// pptx
    Package(Package),
    /// ppt。読み込んだスライドを持つ
    Record(Vec<Slide>),
}


/// 読むために開いたプレゼンテーション
pub struct Presentation {
    source: Origin,
    main: String,
    part: Vec<String>,
}


impl Presentation {
    /// pptx か ppt を開く
    ///
    /// 先頭のバイトを見て、どちらの形式かを決める。読む側は形式を気にしなくてよい。
    pub fn open(path: &Path) -> Result<Presentation, Error> {
        if crate::cfb::is_compound(path) {
            return open_record(path);
        }

        let mut package = Package::open(path)?;
        let main = package.main()?;
        let listed = read_order(&mut package, &main)?;
        let link = package.relationship(&main)?;
        let mut part = Vec::with_capacity(listed.len());

        for id in listed {
            let Some(one) = link.iter().find(|one| one.id == id) else {
                return Err(Error::at(format!("スライド {id} の指し先がありません"), &main));
            };

            part.push(one.target.clone());
        }

        Ok(Presentation {source: Origin::Package(package), main, part})
    }

    /// 何枚あるか
    pub fn count(&self) -> usize {
        match &self.source {
            Origin::Package(_) => self.part.len(),
            Origin::Record(slide) => slide.len(),
        }
    }

    /// 旧形式かどうか
    pub fn is_record(&self) -> bool {
        matches!(self.source, Origin::Record(_))
    }

    /// スライドのパートの名前。並び順
    pub fn slide(&self) -> &[String] {
        &self.part
    }

    /// 本体のパートの名前
    pub fn main(&self) -> &str {
        &self.main
    }

    /// 包みそのもの。folio が読まないパートを自分で取り出すときに使う
    ///
    /// 旧形式には包みがないので None を返す。
    pub fn package(&mut self) -> Option<&mut Package> {
        match &mut self.source {
            Origin::Package(one) => Some(one),
            Origin::Record(_) => None,
        }
    }

    /// 1 枚読む。番号は 0 から
    pub fn read(&mut self, index: usize) -> Result<Slide, Error> {
        if let Origin::Record(all) = &self.source {
            let Some(one) = all.get(index) else {
                return Err(Error::new(format!("{index} 枚目はありません")));
            };

            return Ok(one.clone());
        }

        let Some(part) = self.part.get(index) else {
            return Err(Error::new(format!("{index} 枚目はありません")));
        };

        let part = part.clone();

        let Origin::Package(package) = &mut self.source else {
            return Err(Error::new("包みがありません"));
        };

        let mut reader = package.reader(&part)?;
        slide::read(&mut reader).map_err(|error| error.within(&part))
    }

    /// 全部の文字を、スライドごとに改行でつないで返す
    pub fn text(&mut self) -> Result<String, Error> {
        let mut result = String::new();

        for index in 0..self.count() {
            if index > 0 {
                result.push('\n');
            }

            result.push_str(&self.read(index)?.text());
            result.push('\n');
        }

        Ok(result)
    }
}


/// ppt を開く
fn open_record(path: &Path) -> Result<Presentation, Error> {
    let mut file = crate::cfb::Compound::open(path)?;

    if !file.has("PowerPoint Document") {
        return Err(Error::new("ppt の PowerPoint Document ストリームがありません"));
    }

    let main = file.read("PowerPoint Document")?;

    let user = if file.has("Current User") {
        file.read("Current User")?
    } else {
        Vec::new()
    };

    let slide = crate::ppt::read(&main, &user)?;

    Ok(Presentation {
        source: Origin::Record(slide),
        main: "PowerPoint Document".to_string(),
        part: Vec::new(),
    })
}


/// presentation.xml から、スライドの並びを読む
fn read_order(package: &mut Package, main: &str) -> Result<Vec<String>, Error> {
    let data = package.read(main)?;
    let mut reader = Reader::over(data);
    let mut result = Vec::new();
    let mut inside = false;

    while let Some(event) = reader.next().map_err(|error| error.within(main))? {
        if event == Event::Close {
            if reader.name() == "sldIdLst" {
                inside = false;
            }

            continue;
        }

        if event == Event::Text {
            continue;
        }

        match reader.name() {
            "sldIdLst" => inside = event == Event::Open,
            "sldId" if inside => result.push(reader.attribute("r:id").unwrap_or("").to_string()),
            _ => {}
        }
    }

    Ok(result)
}