Skip to content

Commit 97a41c4

Browse files
committed
Fix bugs, improve CLI
1 parent d1c10fb commit 97a41c4

File tree

9 files changed

+152
-114
lines changed

9 files changed

+152
-114
lines changed

crates/pdf/src/high.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ impl<'a> Handle<'a> {
382382
pages.kids.push(page_ref);
383383
}
384384

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

395-
for (font_ref, font) in &lowering.fonts.store {
395+
for (font_ref, font) in lowering.fonts.store.values() {
396396
let font_low = font.lower(&mut lowering.font_ctx, &mut lowering.id_gen);
397397
fmt.obj(*font_ref, &font_low)?;
398398
}
399399

400-
for (x_ref, x) in &lowering.x_objects.store {
400+
for (x_ref, x) in lowering.x_objects.store.values() {
401401
let x_low = x.lower(&mut (), &mut lowering.id_gen);
402402
fmt.obj(*x_ref, &x_low)?;
403403
}
404404

405405
// FIXME: this only works AFTER all fonts are lowered
406-
for (cproc_ref, char_proc) in &lowering.font_ctx.text_streams.store {
406+
for (cproc_ref, char_proc) in lowering.font_ctx.text_streams.store.values() {
407407
let cp = char_proc.lower(&mut (), &mut lowering.id_gen);
408408
fmt.obj(*cproc_ref, &cp)?;
409409
}

crates/pdf/src/lowering.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Helpers to turn *high* types into *low* types
22
3-
use std::borrow::Cow;
3+
use std::{borrow::Cow, collections::HashMap};
44

55
use crate::{
66
common::Encoding,
@@ -204,13 +204,18 @@ impl<'a> Lowerable<'a> for Encoding<'a> {
204204
}
205205

206206
pub(crate) struct LowerBox<'a, T> {
207-
pub store: Vec<(ObjRef, &'a T)>,
207+
pub store: HashMap<usize, (ObjRef, &'a T)>,
208208
res: &'a [T],
209+
next: usize,
209210
}
210211

211212
impl<'a, T> LowerBox<'a, T> {
212213
fn new(res: &'a [T]) -> Self {
213-
LowerBox { store: vec![], res }
214+
LowerBox {
215+
store: HashMap::new(),
216+
res,
217+
next: res.len(),
218+
}
214219
}
215220
}
216221

@@ -235,12 +240,12 @@ impl<'a, T: Lowerable<'a>> LowerBox<'a, DictResource<T>> {
235240
) -> low::ResDictRes<T::Lower> {
236241
match res {
237242
Resource::Global { index } => {
238-
if let Some((r, _)) = self.store.get(*index) {
243+
if let Some((r, _)) = self.store.get(index) {
239244
low::Resource::Ref(*r)
240245
} else if let Some(font_dict) = self.res.get(*index) {
241246
let id = id_gen.next();
242247
let r = make_ref(id);
243-
self.store.push((r, font_dict));
248+
self.store.insert(*index, (r, font_dict));
244249
low::Resource::Ref(r)
245250
} else {
246251
panic!("Couldn't find {} Dict #{}", T::name(), index);
@@ -258,7 +263,9 @@ impl<'a, T: Lowerable<'a>> LowerBox<'a, T> {
258263
fn put(&mut self, val: &'a T, id_gen: &mut NextID) -> ObjRef {
259264
let id = id_gen.next();
260265
let r = make_ref(id);
261-
self.store.push((r, val));
266+
let index = self.next;
267+
self.next += 1;
268+
self.store.insert(index, (r, val));
262269
r
263270
}
264271

@@ -270,12 +277,12 @@ impl<'a, T: Lowerable<'a>> LowerBox<'a, T> {
270277
) -> low::Resource<T::Lower> {
271278
match res {
272279
Resource::Global { index } => {
273-
if let Some((r, _)) = self.store.get(*index) {
280+
if let Some((r, _)) = self.store.get(index) {
274281
low::Resource::Ref(*r)
275282
} else if let Some(val) = self.res.get(*index) {
276283
let id = id_gen.next();
277284
let r = make_ref(id);
278-
self.store.push((r, val));
285+
self.store.insert(*index, (r, val));
279286
low::Resource::Ref(r)
280287
} else {
281288
panic!("Couldn't find {} #{}", T::name(), index);

crates/signum/src/docs/pbuf.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct PBuf<'a> {
1717
/// The total number of pages
1818
pub page_count: u32,
1919
/// The length of the entry for each page
20-
pub kl: u32,
20+
pub elem_len: u32,
2121
/// The logical number for the first page
2222
pub first_page_nr: u32,
2323
/// A sparse map of pages, ordered by their index
@@ -149,7 +149,7 @@ pub fn parse_pbuf<'a, E: ParseError<&'a [u8]>>(input: &'a [u8]) -> IResult<&'a [
149149
rest,
150150
PBuf {
151151
page_count,
152-
kl,
152+
elem_len: kl,
153153
first_page_nr,
154154
pages,
155155
},

crates/signum/src/raster/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,9 @@ impl Page {
296296
for &val in &line[apos..bpos] {
297297
bw.write_bits(val as usize, 8);
298298
}
299-
bw.write_bits(line[bpos] as usize, rmod);
299+
if rmod > 0 {
300+
bw.write_bits(line[bpos] as usize, rmod);
301+
}
300302
bw.flush();
301303
}
302304
out = bw.done();

docs/changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ The name in brackets indicates the relevant crate.
88
### 26.02.2021
99

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

1214
### 23.02.2021
1315

src/bin/sdo-batch.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{
55
};
66

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

50-
println!("script: {:#?}", script);
51-
println!("opt: {:?}", opt);
51+
//println!("script: {:#?}", script);
52+
//println!("opt: {:?}", opt);
5253

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

9798
let mut documents = Vec::with_capacity(capacity);
98-
for (_doc_file, input) in &doc_files {
99+
for (doc_file, input) in &doc_files {
99100
let mut document = Document::new(&doc_opt);
100101

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

148150
fn main() -> eyre::Result<()> {
149151
color_eyre::install()?;
152+
pretty_env_logger::formatted_builder()
153+
.filter_level(LevelFilter::Info)
154+
.init();
150155
let opt: RunOpts = RunOpts::from_args();
151156

152157
let file_res = File::open(&opt.file);

src/cli/sdoc/console.rs

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
use color_eyre::eyre;
2+
use prettytable::{cell, format, row, Cell, Row, Table};
23
use signum::{
34
chsets::{cache::ChsetCache, encoding::antikro},
4-
docs::tebu::{Char, Flags, Line, Style},
5+
docs::{
6+
hcim::ImageSite,
7+
pbuf::Page,
8+
tebu::{Char, Flags, Line, Style},
9+
},
510
};
611

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

143+
pub fn print_pages(pages: &[Option<Page>]) {
144+
// Create the table
145+
let mut page_table = Table::new();
146+
page_table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
147+
148+
// Add a row per time
149+
page_table.set_titles(row![
150+
"idx", "#phys", "#log", "len", "left", "right", "head", "foot", "numbpos", "kapitel",
151+
"???", "#vi" //, "rest",
152+
]);
153+
154+
for (index, pbuf_entry) in pages.iter().enumerate() {
155+
if let Some(page) = pbuf_entry {
156+
page_table.add_row(row![
157+
index,
158+
page.phys_pnr,
159+
page.log_pnr,
160+
page.format.length,
161+
page.format.left,
162+
page.format.right,
163+
page.format.header,
164+
page.format.footer,
165+
page.numbpos,
166+
page.kapitel,
167+
page.intern,
168+
page.vis_pnr,
169+
//buf,
170+
]);
171+
} else {
172+
page_table.add_row(row![
173+
index, "---", "---", "---", "---", "---", "---", "---", "---", "---", "---",
174+
"---" //, "---"
175+
]);
176+
}
177+
}
178+
179+
// Print the table to stdout
180+
page_table.printstd();
181+
}
182+
183+
fn print_img_sites(sites: &[ImageSite]) {
184+
let mut image_table = Table::new();
185+
image_table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
186+
187+
// Add a row per time
188+
image_table.set_titles(row![
189+
"page", "pos_x", "pos_y", "site_w", "site_h", "[5]", "sel_x", "sel_y", "sel_w", "sel_h",
190+
"[A]", "[B]", "[C]", "img", "[E]", "[F]",
191+
]);
192+
193+
for isite in sites {
194+
image_table.add_row(Row::new(vec![
195+
Cell::new(&format!("{}", isite.page)),
196+
Cell::new(&format!("{}", isite.site.x)),
197+
Cell::new(&format!("{}", isite.site.y)),
198+
Cell::new(&format!("{}", isite.site.w)),
199+
Cell::new(&format!("{}", isite.site.h)),
200+
Cell::new(&format!("{}", isite._5)),
201+
Cell::new(&format!("{}", isite.sel.x)),
202+
Cell::new(&format!("{}", isite.sel.y)),
203+
Cell::new(&format!("{}", isite.sel.w)),
204+
Cell::new(&format!("{}", isite.sel.h)),
205+
Cell::new(&format!("{}", isite._A)),
206+
Cell::new(&format!("{}", isite._B)),
207+
Cell::new(&format!("{}", isite._C)),
208+
Cell::new(&format!("{}", isite.img)),
209+
Cell::new(&format!("{}", isite._E)),
210+
Cell::new(&format!("{:?}", isite._F)),
211+
]));
212+
}
213+
214+
image_table.printstd();
215+
}
216+
138217
pub fn output_console(doc: &Document, fc: &ChsetCache) -> eyre::Result<()> {
218+
print_pages(&doc.pages[..]);
219+
139220
for page_text in &doc.tebu {
140221
let index = page_text.index as usize;
141222
let pbuf_entry = doc.pages[index].as_ref().unwrap();
@@ -151,5 +232,10 @@ pub fn output_console(doc: &Document, fc: &ChsetCache) -> eyre::Result<()> {
151232
page_text.skip, pbuf_entry.log_pnr, pbuf_entry.phys_pnr
152233
);
153234
}
235+
236+
if !doc.sites.is_empty() {
237+
print_img_sites(&doc.sites[..]);
238+
}
239+
154240
Ok(())
155241
}

0 commit comments

Comments
 (0)