Skip to content

Commit

Permalink
Fix bugs, improve CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiphoseer committed Feb 27, 2021
1 parent d1c10fb commit 97a41c4
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 114 deletions.
8 changes: 4 additions & 4 deletions crates/pdf/src/high.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ impl<'a> Handle<'a> {
pages.kids.push(page_ref);
}

for (font_dict_ref, font_dict) in &lowering.font_dicts.store {
for (font_dict_ref, font_dict) in lowering.font_dicts.store.values() {
let dict = lower_dict(
font_dict,
&mut lowering.fonts,
Expand All @@ -392,18 +392,18 @@ impl<'a> Handle<'a> {
fmt.obj(*font_dict_ref, &dict)?;
}

for (font_ref, font) in &lowering.fonts.store {
for (font_ref, font) in lowering.fonts.store.values() {
let font_low = font.lower(&mut lowering.font_ctx, &mut lowering.id_gen);
fmt.obj(*font_ref, &font_low)?;
}

for (x_ref, x) in &lowering.x_objects.store {
for (x_ref, x) in lowering.x_objects.store.values() {
let x_low = x.lower(&mut (), &mut lowering.id_gen);
fmt.obj(*x_ref, &x_low)?;
}

// FIXME: this only works AFTER all fonts are lowered
for (cproc_ref, char_proc) in &lowering.font_ctx.text_streams.store {
for (cproc_ref, char_proc) in lowering.font_ctx.text_streams.store.values() {
let cp = char_proc.lower(&mut (), &mut lowering.id_gen);
fmt.obj(*cproc_ref, &cp)?;
}
Expand Down
23 changes: 15 additions & 8 deletions crates/pdf/src/lowering.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Helpers to turn *high* types into *low* types

use std::borrow::Cow;
use std::{borrow::Cow, collections::HashMap};

use crate::{
common::Encoding,
Expand Down Expand Up @@ -204,13 +204,18 @@ impl<'a> Lowerable<'a> for Encoding<'a> {
}

pub(crate) struct LowerBox<'a, T> {
pub store: Vec<(ObjRef, &'a T)>,
pub store: HashMap<usize, (ObjRef, &'a T)>,
res: &'a [T],
next: usize,
}

impl<'a, T> LowerBox<'a, T> {
fn new(res: &'a [T]) -> Self {
LowerBox { store: vec![], res }
LowerBox {
store: HashMap::new(),
res,
next: res.len(),
}
}
}

Expand All @@ -235,12 +240,12 @@ impl<'a, T: Lowerable<'a>> LowerBox<'a, DictResource<T>> {
) -> low::ResDictRes<T::Lower> {
match res {
Resource::Global { index } => {
if let Some((r, _)) = self.store.get(*index) {
if let Some((r, _)) = self.store.get(index) {
low::Resource::Ref(*r)
} else if let Some(font_dict) = self.res.get(*index) {
let id = id_gen.next();
let r = make_ref(id);
self.store.push((r, font_dict));
self.store.insert(*index, (r, font_dict));
low::Resource::Ref(r)
} else {
panic!("Couldn't find {} Dict #{}", T::name(), index);
Expand All @@ -258,7 +263,9 @@ impl<'a, T: Lowerable<'a>> LowerBox<'a, T> {
fn put(&mut self, val: &'a T, id_gen: &mut NextID) -> ObjRef {
let id = id_gen.next();
let r = make_ref(id);
self.store.push((r, val));
let index = self.next;
self.next += 1;
self.store.insert(index, (r, val));
r
}

Expand All @@ -270,12 +277,12 @@ impl<'a, T: Lowerable<'a>> LowerBox<'a, T> {
) -> low::Resource<T::Lower> {
match res {
Resource::Global { index } => {
if let Some((r, _)) = self.store.get(*index) {
if let Some((r, _)) = self.store.get(index) {
low::Resource::Ref(*r)
} else if let Some(val) = self.res.get(*index) {
let id = id_gen.next();
let r = make_ref(id);
self.store.push((r, val));
self.store.insert(*index, (r, val));
low::Resource::Ref(r)
} else {
panic!("Couldn't find {} #{}", T::name(), index);
Expand Down
4 changes: 2 additions & 2 deletions crates/signum/src/docs/pbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct PBuf<'a> {
/// The total number of pages
pub page_count: u32,
/// The length of the entry for each page
pub kl: u32,
pub elem_len: u32,
/// The logical number for the first page
pub first_page_nr: u32,
/// A sparse map of pages, ordered by their index
Expand Down Expand Up @@ -149,7 +149,7 @@ pub fn parse_pbuf<'a, E: ParseError<&'a [u8]>>(input: &'a [u8]) -> IResult<&'a [
rest,
PBuf {
page_count,
kl,
elem_len: kl,
first_page_nr,
pages,
},
Expand Down
4 changes: 3 additions & 1 deletion crates/signum/src/raster/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,9 @@ impl Page {
for &val in &line[apos..bpos] {
bw.write_bits(val as usize, 8);
}
bw.write_bits(line[bpos] as usize, rmod);
if rmod > 0 {
bw.write_bits(line[bpos] as usize, rmod);
}
bw.flush();
}
out = bw.done();
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ The name in brackets indicates the relevant crate.
### 26.02.2021

- Initial support for images in PDFs
- Introduce `log` crate for output to the console
- Move chunk tables to delayed console output, requires `--format plain` now

### 23.02.2021

Expand Down
11 changes: 8 additions & 3 deletions src/bin/sdo-batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
};

use color_eyre::eyre::{self, WrapErr};
use log::{info, LevelFilter};
use pdf_create::{
common::{PageLabel, PdfString},
encoding::pdf_doc_encode,
Expand Down Expand Up @@ -47,8 +48,8 @@ pub fn run(buffer: &[u8], opt: RunOpts) -> eyre::Result<()> {
let script_res = ron::from_str(script_str);
let script: DocScript = WrapErr::wrap_err(script_res, "Failed to parse DocScript")?;

println!("script: {:#?}", script);
println!("opt: {:?}", opt);
//println!("script: {:#?}", script);
//println!("opt: {:?}", opt);

let doc_opt = Options {
file: PathBuf::from("SDO-TOOL-BUG"),
Expand Down Expand Up @@ -95,9 +96,10 @@ pub fn run(buffer: &[u8], opt: RunOpts) -> eyre::Result<()> {
}

let mut documents = Vec::with_capacity(capacity);
for (_doc_file, input) in &doc_files {
for (doc_file, input) in &doc_files {
let mut document = Document::new(&doc_opt);

info!("Loading document file '{}'", doc_file.display());
document.process_sdoc(&input, &mut fc)?;
documents.push(document);
}
Expand Down Expand Up @@ -147,6 +149,9 @@ pub fn run(buffer: &[u8], opt: RunOpts) -> eyre::Result<()> {

fn main() -> eyre::Result<()> {
color_eyre::install()?;
pretty_env_logger::formatted_builder()
.filter_level(LevelFilter::Info)
.init();
let opt: RunOpts = RunOpts::from_args();

let file_res = File::open(&opt.file);
Expand Down
88 changes: 87 additions & 1 deletion src/cli/sdoc/console.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use color_eyre::eyre;
use prettytable::{cell, format, row, Cell, Row, Table};
use signum::{
chsets::{cache::ChsetCache, encoding::antikro},
docs::tebu::{Char, Flags, Line, Style},
docs::{
hcim::ImageSite,
pbuf::Page,
tebu::{Char, Flags, Line, Style},
},
};

use crate::cli::opt::Format;
Expand Down Expand Up @@ -135,7 +140,83 @@ pub fn print_line(doc: &Document, fc: &ChsetCache, line: &Line, skip: u16) {
}
}

pub fn print_pages(pages: &[Option<Page>]) {
// Create the table
let mut page_table = Table::new();
page_table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);

// Add a row per time
page_table.set_titles(row![
"idx", "#phys", "#log", "len", "left", "right", "head", "foot", "numbpos", "kapitel",
"???", "#vi" //, "rest",
]);

for (index, pbuf_entry) in pages.iter().enumerate() {
if let Some(page) = pbuf_entry {
page_table.add_row(row![
index,
page.phys_pnr,
page.log_pnr,
page.format.length,
page.format.left,
page.format.right,
page.format.header,
page.format.footer,
page.numbpos,
page.kapitel,
page.intern,
page.vis_pnr,
//buf,
]);
} else {
page_table.add_row(row![
index, "---", "---", "---", "---", "---", "---", "---", "---", "---", "---",
"---" //, "---"
]);
}
}

// Print the table to stdout
page_table.printstd();
}

fn print_img_sites(sites: &[ImageSite]) {
let mut image_table = Table::new();
image_table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);

// Add a row per time
image_table.set_titles(row![
"page", "pos_x", "pos_y", "site_w", "site_h", "[5]", "sel_x", "sel_y", "sel_w", "sel_h",
"[A]", "[B]", "[C]", "img", "[E]", "[F]",
]);

for isite in sites {
image_table.add_row(Row::new(vec![
Cell::new(&format!("{}", isite.page)),
Cell::new(&format!("{}", isite.site.x)),
Cell::new(&format!("{}", isite.site.y)),
Cell::new(&format!("{}", isite.site.w)),
Cell::new(&format!("{}", isite.site.h)),
Cell::new(&format!("{}", isite._5)),
Cell::new(&format!("{}", isite.sel.x)),
Cell::new(&format!("{}", isite.sel.y)),
Cell::new(&format!("{}", isite.sel.w)),
Cell::new(&format!("{}", isite.sel.h)),
Cell::new(&format!("{}", isite._A)),
Cell::new(&format!("{}", isite._B)),
Cell::new(&format!("{}", isite._C)),
Cell::new(&format!("{}", isite.img)),
Cell::new(&format!("{}", isite._E)),
Cell::new(&format!("{:?}", isite._F)),
]));
}

image_table.printstd();
}

pub fn output_console(doc: &Document, fc: &ChsetCache) -> eyre::Result<()> {
print_pages(&doc.pages[..]);

for page_text in &doc.tebu {
let index = page_text.index as usize;
let pbuf_entry = doc.pages[index].as_ref().unwrap();
Expand All @@ -151,5 +232,10 @@ pub fn output_console(doc: &Document, fc: &ChsetCache) -> eyre::Result<()> {
page_text.skip, pbuf_entry.log_pnr, pbuf_entry.phys_pnr
);
}

if !doc.sites.is_empty() {
print_img_sites(&doc.sites[..]);
}

Ok(())
}
Loading

0 comments on commit 97a41c4

Please sign in to comment.