use super::{Object, Stream};
use crate::Result;
pub use crate::parser::ContentOperations;
use crate::writer::Writer;
use std::io::Write;
#[derive(Debug, Clone)]
pub struct Operation {
pub operator: String,
pub operands: Vec<Object>,
}
impl Operation {
pub fn new(operator: &str, operands: Vec<Object>) -> Operation {
Operation {
operator: operator.to_string(),
operands,
}
}
}
#[derive(Debug, Clone)]
pub struct Content<Operations: AsRef<[Operation]> = Vec<Operation>> {
pub operations: Operations,
}
impl<Operations: AsRef<[Operation]>> Content<Operations> {
pub fn encode(&self) -> Result<Vec<u8>> {
let mut buffer = Vec::new();
let mut first_operation = true;
for operation in self.operations.as_ref() {
if first_operation {
first_operation = false;
} else {
buffer.write_all(b"\n")?;
}
if let ("BI", [Object::Stream(image)]) = (operation.operator.as_str(), operation.operands.as_slice()) {
encode_inline_image(&mut buffer, image)?;
continue;
}
for operand in &operation.operands {
Writer::write_object(&mut buffer, operand)?;
buffer.write_all(b" ")?;
}
buffer.write_all(operation.operator.as_bytes())?;
}
Ok(buffer)
}
}
fn encode_inline_image(buffer: &mut Vec<u8>, image: &Stream) -> Result<()> {
buffer.write_all(b"BI")?;
for (key, value) in &image.dict {
if key == b"Length" {
continue;
}
buffer.write_all(b" ")?;
Writer::write_name(buffer, key)?;
buffer.write_all(b" ")?;
Writer::write_object(buffer, value)?;
}
buffer.write_all(b" ID ")?;
buffer.write_all(&image.content)?;
buffer.write_all(b"\nEI")?;
Ok(())
}