Skip to main content

read_pdf

Function read_pdf 

Source
pub fn read_pdf(bytes: &[u8]) -> Result<Reading, Refusal>
Expand description

Read a PDF’s bytes into a score, or say why it cannot be read. A staff’s bar it cannot read is left as a bar’s rest and listed in Reading::unread, while those are no more than an eighth of the staves’ bars; past that the PDF is refused for the first of them.

let reading = fm_vector::read_pdf(&std::fs::read("score.pdf")?)?;
std::fs::write("score.musicxml", reading.musicxml)?;
Examples found in repository?
examples/survey.rs (line 11)
3fn main() {
4    let list = std::fs::read_to_string(std::env::args().nth(1).expect("a list of PDFs")).unwrap();
5    for path in list.lines().filter(|l| !l.trim().is_empty()) {
6        let Ok(bytes) = std::fs::read(path) else {
7            println!("{path}\tunreadable\t");
8            continue;
9        };
10        let t = std::time::Instant::now();
11        let out = std::panic::catch_unwind(|| fm_vector::read_pdf(&bytes));
12        let ms = t.elapsed().as_millis();
13        match out {
14            Ok(Ok(r)) => println!(
15                "{path}\tread\t{} systems, {} notes, {} syllables, {ms} ms",
16                r.systems, r.notes, r.syllables
17            ),
18            Ok(Err(e)) => println!("{path}\trefused\t{}", e.to_string().replace('\n', " ")),
19            Err(_) => println!("{path}\tpanic\t"),
20        }
21    }
22}