use crate::BasicPdfWriter;
use crate::page::Page;
use format_bytes::write_bytes as wb;
pub struct ImageSpec<'a> {
pub data: &'a [u8],
pub width: usize,
pub height: usize,
pub bits_per_component: u8,
pub color_space: &'a [u8],
pub other: &'a [u8],
}
pub struct Image {
pub obj: usize,
pub width: usize,
pub height: usize,
}
impl Image {
pub fn new(s: &ImageSpec, w: &mut BasicPdfWriter) -> Image {
let obj = w.begin();
let _ = wb!(
&mut w.b,
b"<</Type/XObject/Subtype/Image/Width {}/Height {}/ColorSpace{}/BitsPerComponent {}/Length {}{}>>stream\n",
s.width, s.height, s.color_space, s.bits_per_component, s.data.len(), s.other
);
w.b.extend_from_slice(s.data);
w.b.extend_from_slice(b"\nendstream");
w.end();
Image {
obj,
height: s.height,
width: s.width,
}
}
pub fn draw(&self, page: &mut Page, x: f32, y: f32, scale: f32) {
let obj = self.obj;
let w = (self.width as f32) * scale;
let h = (self.height as f32) * scale;
page.xobjs.insert(obj);
let _ = wb!(
&mut page.os,
b"\nq {} 0 0 {} {} {} cm /X{} Do Q",
w,
h,
x,
y,
obj
);
}
}