Skip to content

Commit

Permalink
chore: split out pdf image conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiphoseer committed Jan 7, 2025
1 parent 0c86fd5 commit e611867
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 54 deletions.
4 changes: 2 additions & 2 deletions crates/pdf/src/high.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,10 @@ pub struct Res<'a> {

impl Res<'_> {
/// Push an XObject, returning the index it was pushed at
pub fn push_xobject<T: Into<XObject>>(&mut self, value: T) -> usize {
pub fn push_xobject<T: Into<XObject>>(&mut self, value: T) -> Resource<XObject> {
let index = self.x_objects.len();
self.x_objects.push(value.into());
index
Resource::Global { index }
}
}

Expand Down
20 changes: 20 additions & 0 deletions crates/sdo-pdf/src/image.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use pdf_create::{
common::{ColorIs, ColorSpace, ImageMetadata},
high::Image,
};
use signum::docs::{hcim::ImageSite, DocumentInfo};

/// Return a PDF Image for a site
pub fn image_for_site(di: &DocumentInfo, site: &ImageSite) -> Image {
Image {
meta: ImageMetadata {
width: site.sel.w as usize,
height: site.sel.h as usize,
color_space: ColorSpace::DeviceGray,
bits_per_component: 1,
image_mask: true,
decode: ColorIs::One,
},
data: di.image_at(site.img).select(site.sel),
}
}
2 changes: 2 additions & 0 deletions crates/sdo-pdf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub mod cmap;
pub mod font;
mod image;
mod info;
pub mod sdoc;

pub use image::image_for_site;
pub use info::{prepare_info, MetaInfo};
8 changes: 2 additions & 6 deletions crates/sdo-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,9 @@ impl Handle {
page_text,
pbuf_entry,
sdoc.image_sites(),
&di.images,
di,
*pd,
&self.fc,
&di.fonts,
);

let blob = self.page_as_blob(&page)?;
Expand Down Expand Up @@ -515,10 +514,7 @@ impl Handle {

self.active = Some(ActiveDocument {
sdoc: sdoc.into_owned(),
di: DocumentInfo {
fonts: dfci,
images,
},
di: DocumentInfo::new(dfci, images),
pd,
});
}
Expand Down
15 changes: 14 additions & 1 deletion crates/signum/src/docs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,20 @@ pub struct DocumentInfo {
/// Information on how to locate the font information in a [crate::chsets::cache::ChsetCache]
pub fonts: DocumentFontCacheInfo,
/// Decoded images embedded in the document
pub images: Vec<(String, Page)>,
images: Vec<(String, Page)>,
}

impl DocumentInfo {
/// Create a new instance
pub fn new(fonts: DocumentFontCacheInfo, images: Vec<(String, Page)>) -> Self {
Self { fonts, images }
}

/// Get the image associated with the given index
pub fn image_at(&self, img: u16) -> &Page {
let (_, page) = &self.images[img as usize];
page
}
}

/// Common adjustments to the graphics state for rendering
Expand Down
11 changes: 5 additions & 6 deletions crates/signum/src/raster/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
printer::PrinterKind,
FontKind,
},
docs::{hcim, pbuf, tebu},
docs::{hcim, pbuf, tebu, DocumentInfo},
util::Pos,
};

Expand Down Expand Up @@ -54,10 +54,9 @@ pub fn render_doc_page(
page_text: &tebu::PageText,
pbuf_entry: &pbuf::Page,
image_sites: &[hcim::ImageSite],
images: &[(String, Page)],
di: &DocumentInfo,
pd: FontKind,
fc: &ChsetCache,
print: &DocumentFontCacheInfo,
) -> Page {
let fmt = &pbuf_entry.format;
let width = pd.scale_x(fmt.left + fmt.right + 20);
Expand All @@ -68,9 +67,9 @@ pub fn render_doc_page(

#[allow(clippy::type_complexity)]
let print_char: Box<dyn Fn(&tebu::Char, &mut u16, u16, &mut Page)> = match pd {
FontKind::Editor => Box::new(move |te, x, y, p| print_echar(print, fc, te, x, y, p)),
FontKind::Editor => Box::new(move |te, x, y, p| print_echar(&di.fonts, fc, te, x, y, p)),
FontKind::Printer(pk) => {
Box::new(move |te, x, y, p| print_pchar(print, fc, te, pk, x, y, p))
Box::new(move |te, x, y, p| print_pchar(&di.fonts, fc, te, pk, x, y, p))
}
};
for (skip, line) in &page_text.content {
Expand All @@ -97,7 +96,7 @@ pub fn render_doc_page(
let w = pd.scale_x(site.site.w);
let py = pd.scale_y(10 + site.site.y - site._5 / 2);
let h = pd.scale_y(site.site.h / 2);
let (_, image) = &images[site.img as usize];
let image = di.image_at(site.img);
page.draw_image(px, py, w, h, image, site.sel);
}
page
Expand Down
10 changes: 1 addition & 9 deletions src/cli/sdoc/imgseq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,7 @@ pub fn output_print(
continue;
}
}
let page = render_doc_page(
page_text,
pbuf_entry,
&doc.sites,
&info.images,
pd,
fc,
&info.fonts,
);
let page = render_doc_page(page_text, pbuf_entry, &doc.sites, info, pd, fc);
let image = page.to_image();
let file_name = format!("page-{}.png", pbuf_entry.log_pnr);
println!("Saving {}", file_name);
Expand Down
2 changes: 1 addition & 1 deletion src/cli/sdoc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl<'a> Document<'a> {
}

let fonts = dfci.ok_or_else(|| eyre!("Document has no CSET chunk"))?;
Ok(DocumentInfo { fonts, images })
Ok(DocumentInfo::new(fonts, images))
}
}

Expand Down
39 changes: 10 additions & 29 deletions src/cli/sdoc/pdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ use std::{collections::BTreeMap, fs::File, io::BufWriter, path::Path};
use color_eyre::eyre::{self, eyre, OptionExt};
use log::{debug, info};
use pdf_create::{
common::{
ColorIs, ColorSpace, ImageMetadata, OutputIntent, OutputIntentSubtype, PdfString, ProcSet,
Rectangle,
},
high::{DictResource, Handle, Image, Page, Resource, Resources, XObject},
common::{OutputIntent, OutputIntentSubtype, PdfString, ProcSet, Rectangle},
high::{DictResource, Handle, Page, Resource, Resources, XObject},
};
use sdo_pdf::{font::Fonts, prepare_info, sdoc::Contents, MetaInfo};
use sdo_pdf::{font::Fonts, image_for_site, prepare_info, sdoc::Contents, MetaInfo};
use signum::{
chsets::{
cache::{ChsetCache, FontCacheInfo},
Expand Down Expand Up @@ -79,37 +76,21 @@ pub fn prepare_document(

let mut x_objects: DictResource<XObject> = BTreeMap::new();
let mut img = vec![];
for (index, site) in doc
.sites
let image_sites = &doc.sites[..];
for (index, site) in image_sites
.iter()
.enumerate()
.filter(|(_, site)| site.page == page_info.phys_pnr)
{
let key = format!("I{}", index);
let width = site.sel.w as usize;
let height = site.sel.h as usize;
//let area = width * height;

let img_num = site.img as usize;
let (_, im) = &di.images[img_num];
let data = im.select(site.sel);

let img_index = hnd.res.push_xobject(Image {
meta: ImageMetadata {
width,
height,
color_space: ColorSpace::DeviceGray,
bits_per_component: 1,
image_mask: true,
decode: ColorIs::One,
},
data,
});
debug!(
"Adding image from #{} on page {} as /{}",
img_num, page_info.log_pnr, &key
site.img, page_info.log_pnr, &key
);
x_objects.insert(key.clone(), Resource::Global { index: img_index });

let image = image_for_site(di, site);

x_objects.insert(key.clone(), hnd.res.push_xobject(image));
img.push((site, key));
}

Expand Down

0 comments on commit e611867

Please sign in to comment.