use docling_core::{DoclingDocument, Node, Script};
use crate::backend::markdown::escape_text;
use crate::backend::styled::{Para, Style};
use crate::backend::DeclarativeBackend;
use crate::error::ConversionError;
use crate::source::SourceDocument;
pub struct MacWpBackend;
fn u16_at(d: &[u8], o: usize) -> Option<u16> {
Some(u16::from_be_bytes(d.get(o..o + 2)?.try_into().ok()?))
}
fn u32_at(d: &[u8], o: usize) -> Option<u32> {
Some(u32::from_be_bytes(d.get(o..o + 4)?.try_into().ok()?))
}
fn mac_roman(bytes: &[u8]) -> String {
encoding_rs::MACINTOSH
.decode_without_bom_handling(bytes)
.0
.into_owned()
}
pub(crate) fn looks_like_mac_word(bytes: &[u8]) -> bool {
bytes.len() >= 0x80
&& matches!(
(u16_at(bytes, 0), u16_at(bytes, 2)),
(Some(0xfe32 | 0xfe34), Some(0)) | (Some(0xfe37), Some(0x1c | 0x23))
)
}
pub(crate) fn looks_like_clarisworks(bytes: &[u8]) -> bool {
bytes.get(4..8) == Some(b"BOBO") && (1..=6).contains(&bytes[0])
}
impl DeclarativeBackend for MacWpBackend {
fn convert(&self, source: &SourceDocument) -> Result<DoclingDocument, ConversionError> {
let bytes = &source.bytes[..];
let text = if looks_like_mac_word(bytes) {
word(bytes)
} else if looks_like_clarisworks(bytes) {
clarisworks(bytes)?
} else if matches!(u16_at(bytes, 0), Some(3 | 6)) {
macwrite(bytes)?
} else {
return Err(ConversionError::Parse(
"mac_wp: neither a MacWrite 1.0–5.0, a Word for Macintosh 1–5 nor a ClarisWorks 1–6 \
document (MacWrite Pro and MacWrite II are another format)"
.into(),
));
};
let mut doc = DoclingDocument::new(&source.name);
text.emit(&mut doc);
Ok(doc)
}
}
#[derive(Default)]
struct Text {
body: Vec<Block>,
furniture: Vec<(&'static str, String)>,
}
enum Block {
Para(Para),
PageBreak,
}
impl Text {
fn plain(&mut self, text: &str) {
for line in text.split('\r') {
let mut parts = line.split('\x0c').peekable();
while let Some(part) = parts.next() {
let mut para = Para::new();
para.push(&part.replace(['\t', '\x0b'], " "), Style::default());
self.body.push(Block::Para(para));
if parts.peek().is_some() {
self.body.push(Block::PageBreak);
}
}
}
}
fn furniture(&mut self, label: &'static str, text: &str) {
for line in text.split(['\r', '\x0c']) {
let line: String = line
.chars()
.map(|c| if c.is_control() { ' ' } else { c })
.collect();
let line = line.split_whitespace().collect::<Vec<_>>().join(" ");
if !line.is_empty() {
self.furniture.push((label, line));
}
}
}
fn emit(self, doc: &mut DoclingDocument) {
for block in self.body {
match block {
Block::Para(p) => {
if let Some(node) = p.into_node() {
doc.push(node);
}
}
Block::PageBreak => doc.push(Node::PageBreak),
}
}
for (label, text) in self.furniture {
doc.push(Node::FurnitureText {
label: label.into(),
text: escape_text(&text),
});
}
}
}
const NIBBLES: &[u8; 15] = b" etnroaisdlhcfp";
struct Info {
text: bool,
begin: usize,
len: usize,
compressed: bool,
style: Style,
}
fn style_bits(bold: bool, italic: bool, underline: bool, sup: bool, sub: bool) -> Style {
Style {
bold,
italic,
underline,
strike: false,
script: if sup {
Script::Super
} else if sub {
Script::Sub
} else {
Script::Baseline
},
}
}
fn macwrite(d: &[u8]) -> Result<Text, ConversionError> {
let version = u16_at(d, 0).unwrap_or(0);
let bad = || ConversionError::Parse("mac_wp: a truncated MacWrite document".into());
let mut zones: [Vec<Info>; 3] = Default::default();
if version == 6 {
for (record, zone) in [2usize, 1, 0].into_iter().enumerate() {
let w = 40 + 120 + record * 46;
let (Some(at), Some(len)) = (u32_at(d, w + 12), u16_at(d, w + 16)) else {
return Err(bad());
};
let (at, len) = (at as usize, len as usize);
let Some(entries) = d.get(at..at + len) else {
continue;
};
for e in entries.chunks_exact(16) {
let height = i16::from_be_bytes([e[0], e[1]]);
let status = e[8];
let format = u16::from_be_bytes([e[14], e[15]]);
zones[zone].push(Info {
text: height > 0,
begin: (e[9] as usize) << 16 | u16::from_be_bytes([e[10], e[11]]) as usize,
len: u16::from_be_bytes([e[12], e[13]]) as usize,
compressed: status & 8 != 0,
style: style_bits(
format & 2 != 0,
format & 4 != 0,
format & 8 != 0,
format & 0x40 != 0,
format & 0x80 != 0,
),
});
}
}
} else {
let data = u16_at(d, 2).ok_or_else(bad)? as usize;
let counts: Vec<usize> = (0..3)
.map(|i| u16_at(d, 4 + 2 * i).unwrap_or(0) as usize)
.collect();
let mut o = data;
for (zone, &count) in counts.iter().enumerate() {
for _ in 0..count {
let (Some(kind), Some(size)) = (u16_at(d, o), u16_at(d, o + 2)) else {
break;
};
let size = size as usize;
if o + 4 + size > d.len() {
break;
}
zones[zone].push(Info {
text: kind & 7 == 1,
begin: o + 4,
len: size,
compressed: false,
style: Style::default(),
});
o += 4 + size;
}
}
}
let mut text = Text::default();
for (zone, infos) in zones.iter().enumerate() {
for info in infos.iter().filter(|i| i.text) {
let Some(block) = d.get(info.begin..info.begin + info.len) else {
continue;
};
let paras = macwrite_paragraph(block, info);
match zone {
0 => text.body.extend(paras.into_iter().map(Block::Para)),
_ => {
let label = if zone == 1 {
"page_header"
} else {
"page_footer"
};
for p in paras {
text.furniture(label, &p.text());
}
}
}
}
}
Ok(text)
}
fn macwrite_paragraph(block: &[u8], info: &Info) -> Vec<Para> {
let n = u16_at(block, 0).unwrap_or(0) as usize;
let mut chars = Vec::with_capacity(n);
let mut o = 2;
if info.compressed {
let packed = block.get(2..).unwrap_or_default();
let mut nibbles = packed.iter().flat_map(|b| [b >> 4, b & 0xf]);
let mut used = 0usize;
while chars.len() < n {
let Some(x) = nibbles.next() else { break };
used += 1;
if x != 0xf {
chars.push(NIBBLES[x as usize]);
continue;
}
let (Some(hi), Some(lo)) = (nibbles.next(), nibbles.next()) else {
break;
};
used += 2;
chars.push(hi << 4 | lo);
}
o += used.div_ceil(2);
} else {
chars.extend_from_slice(block.get(2..2 + n).unwrap_or(&block[2.min(block.len())..]));
o += chars.len();
}
o += o % 2;
let mut runs: Vec<(usize, Style)> = Vec::new();
if let Some(len) = u16_at(block, o) {
if let Some(fmt) = block.get(o + 2..o + 2 + len as usize) {
for f in fmt.chunks_exact(6) {
let flags = f[3];
runs.push((
u16::from_be_bytes([f[0], f[1]]) as usize,
style_bits(
flags & 1 != 0,
flags & 2 != 0,
flags & 4 != 0,
flags & 0x20 != 0,
flags & 0x40 != 0,
),
));
}
}
}
let mut out = Vec::new();
let mut para = Para::new();
let mut style = info.style;
let mut run = runs.iter().peekable();
let decoded = mac_roman(&chars);
for (i, c) in decoded.chars().enumerate() {
while let Some(&&(pos, s)) = run.peek() {
if pos > i {
break;
}
style = s;
run.next();
}
match c {
'\r' => out.push(para.take()),
'\t' => para.space(),
_ => para.push_char(c, style),
}
}
out.push(para);
out
}
fn word(d: &[u8]) -> Text {
let magic = u16_at(d, 0).unwrap_or(0);
let mut text = Text::default();
let (bot, eot) = match magic {
0xfe32 => (0x80, u32_at(d, 14).unwrap_or(0) as usize),
0xfe34 => (0x100, u32_at(d, 14).unwrap_or(0) as usize),
_ => (
u32_at(d, 20).unwrap_or(0x100) as usize,
u32_at(d, 24).unwrap_or(0) as usize,
),
};
let eot = eot.min(d.len());
let bot = bot.min(eot);
let clx = match magic {
0xfe32 => None,
0xfe34 => Some(30 + 14 * 6),
_ => Some(64 + 18 * 6),
};
let raw = clx
.and_then(|e| pieces(d, e))
.unwrap_or_else(|| d[bot..eot].to_vec());
let all = mac_roman(&raw);
let lengths: Vec<usize> = match magic {
0xfe37 => (0..3)
.map(|i| u32_at(d, 0x24 + 4 * i).unwrap_or(0) as usize)
.collect(),
_ => Vec::new(),
};
let chars: Vec<char> = all.chars().collect();
match lengths.as_slice() {
[main, notes, headers] if *main > 0 && *main <= chars.len() => {
let main_end = *main;
let notes_end = (main_end + notes).min(chars.len());
let headers_end = (notes_end + headers).min(chars.len());
text.plain(&chars[..main_end].iter().collect::<String>());
text.furniture(
"footnote",
&chars[main_end..notes_end].iter().collect::<String>(),
);
text.furniture(
"page_header",
&chars[notes_end..headers_end].iter().collect::<String>(),
);
}
_ => text.plain(&all),
}
text
}
fn pieces(d: &[u8], entry: usize) -> Option<Vec<u8>> {
let at = u32_at(d, entry)? as usize;
let size = u16_at(d, entry + 4)? as usize;
let zone = d.get(at..at + size)?;
if size < 19 {
return None;
}
let mut o = 0;
while *zone.get(o)? == 1 {
o += 3 + u16_at(zone, o + 1)? as usize;
}
if zone[o] != 2 {
return None;
}
let sz = u16_at(zone, o + 1)? as usize;
if sz % 12 != 4 {
return None;
}
let n = sz / 12;
let positions: Vec<usize> = (0..=n)
.map(|i| u32_at(zone, o + 3 + 4 * i).map(|p| p as usize))
.collect::<Option<_>>()?;
let descriptors = o + 3 + 4 * (n + 1);
let mut out = Vec::new();
for i in 0..n {
let len = positions[i + 1].checked_sub(positions[i])?;
let ptr = u32_at(zone, descriptors + 8 * i + 2)? as usize;
out.extend_from_slice(d.get(ptr..ptr + len)?);
}
Some(out)
}
fn clarisworks(d: &[u8]) -> Result<Text, ConversionError> {
let mut main: Option<String> = None;
let mut others: Vec<(u8, String)> = Vec::new();
let mut o = 0;
while let Some(p) = d[o..].windows(4).position(|w| w == b"DSET").map(|p| p + o) {
o = p + 4;
let (Some(size), Some(&file_type), Some(id)) =
(u32_at(d, p + 4), d.get(p + 20), u16_at(d, p + 22))
else {
continue;
};
let end = p.saturating_add(8).saturating_add(size as usize);
if file_type != 1 || end > d.len() || size < 30 {
continue;
}
let kind = d[p + 18];
let (Some(chars), Some(chunks)) = (u32_at(d, p + 28), u16_at(d, p + 32)) else {
continue;
};
let mut b = end;
let mut bytes = Vec::new();
let mut complete = true;
for z in 0..4 + chunks as usize {
let Some(len) = u32_at(d, b) else {
complete = false;
break;
};
let Some(data) = d.get(b + 4..b + 4 + len as usize) else {
complete = false;
break;
};
if z >= 4 {
bytes.extend_from_slice(data);
}
b += 4 + len as usize;
}
if !complete {
continue;
}
bytes.truncate(chars as usize);
let text = mac_roman(&bytes);
if id == 1 && kind >> 4 == 0 && main.is_none() {
main = Some(text);
} else {
others.push((kind >> 4, text));
}
o = b.max(o);
}
if main.is_none() && others.is_empty() {
let kind = match d.get(match d[0] {
1 => 242,
2 | 3 => 248,
4 => 256,
5 => 268,
_ => 278,
}) {
Some(2) => "a spreadsheet",
Some(3) => "a database",
Some(4) => "a painting",
_ => "a document",
};
return Err(ConversionError::Parse(format!(
"mac_wp: {kind} in ClarisWorks with no text zone — only its text is read"
)));
}
let mut text = Text::default();
if let Some(main) = main {
text.plain(&main);
}
for (kind, t) in others {
match kind {
2 => text.furniture("page_header", &t),
4 => text.furniture("page_footer", &t),
6 => text.furniture("footnote", &t),
_ => text.plain(&t),
}
}
Ok(text)
}
#[cfg(test)]
mod tests {
use super::*;
fn paragraphs(doc: &DoclingDocument) -> Vec<String> {
doc.nodes
.iter()
.filter_map(|n| match n {
Node::Paragraph { text } => Some(text.clone()),
Node::InlineGroup { md_text, .. } => Some(md_text.clone()),
Node::FurnitureText { label, text } => Some(format!("[{label}] {text}")),
_ => None,
})
.collect()
}
fn convert(bytes: Vec<u8>) -> DoclingDocument {
MacWpBackend
.convert(&SourceDocument::from_bytes(
"t.mw",
crate::InputFormat::MacWp,
bytes,
))
.unwrap()
}
#[test]
fn macwrite_nibble_compression_decodes() {
let info = Info {
text: true,
begin: 0,
len: 0,
compressed: true,
style: Style::default(),
};
let block = [0, 5, 0xf5, 0x24, 0x58, 0x10, 0, 0];
let paras = macwrite_paragraph(&block, &info);
assert_eq!(paras[0].text(), "Rrose");
}
#[test]
fn macwrite_v3_paragraph_blocks() {
let mut d = vec![0u8; 0x100];
d[1] = 3;
d[2..4].copy_from_slice(&0x100u16.to_be_bytes());
d[4..6].copy_from_slice(&1u16.to_be_bytes());
let mut block = vec![0, 3, b'H', b'i', b'\r', 0];
block.extend([0, 6, 0, 0, 12, 1, 0, 0]);
d.extend((1u16).to_be_bytes());
d.extend((block.len() as u16).to_be_bytes());
d.extend(block);
assert_eq!(paragraphs(&convert(d)), ["**Hi**"]);
}
#[test]
fn word_for_mac_3_text_run() {
let mut d = vec![0u8; 0x100];
d[0..2].copy_from_slice(&0xfe34u16.to_be_bytes());
let body = b"first\rsecond \x8et\x8e\x0cnext\r";
d[14..18].copy_from_slice(&((0x100 + body.len()) as u32).to_be_bytes());
d.extend_from_slice(body);
assert!(looks_like_mac_word(&d));
let doc = convert(d);
assert_eq!(paragraphs(&doc), ["first", "second été", "next"]);
assert!(doc.nodes.iter().any(|n| matches!(n, Node::PageBreak)));
}
#[test]
fn clarisworks_text_zones() {
let mut d = vec![1, 0, 0, 0];
d.extend(b"BOBO");
d.resize(300, 0);
let mut dset = b"DSET".to_vec();
let mut header = vec![0u8; 30];
header[12] = 1; header[15] = 1; header[20..24].copy_from_slice(&7u32.to_be_bytes()); header[24..26].copy_from_slice(&1u16.to_be_bytes()); dset.extend((header.len() as u32).to_be_bytes());
dset.extend(header);
for _ in 0..4 {
dset.extend(0u32.to_be_bytes());
}
dset.extend(8u32.to_be_bytes());
dset.extend(b"one\rtwo\0");
d.extend(dset);
assert!(looks_like_clarisworks(&d));
assert_eq!(paragraphs(&convert(d)), ["one", "two"]);
}
#[test]
fn truncated_files_do_not_panic() {
let dir =
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data/mac_wp/sources");
for entry in std::fs::read_dir(dir).unwrap() {
let bytes = std::fs::read(entry.unwrap().path()).unwrap();
for end in (0..bytes.len()).step_by(13) {
let _ = MacWpBackend.convert(&SourceDocument::from_bytes(
"t.mw",
crate::InputFormat::MacWp,
bytes[..end].to_vec(),
));
}
}
}
#[test]
fn unknown_bytes_are_refused() {
let err = MacWpBackend
.convert(&SourceDocument::from_bytes(
"t.mw",
crate::InputFormat::MacWp,
b"hello".to_vec(),
))
.unwrap_err();
assert!(err.to_string().contains("MacWrite"), "{err}");
}
}