Skip to content

Commit 463a51b

Browse files
committed
chore: pdf refactoring
1 parent 589523d commit 463a51b

File tree

6 files changed

+119
-57
lines changed

6 files changed

+119
-57
lines changed

crates/sdo-pdf/src/font.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -320,28 +320,27 @@ impl Fonts {
320320

321321
pub const FONTS: [&str; 8] = ["C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7"];
322322

323-
pub fn font_dict<'a>(
324-
font_info: &'a Fonts,
325-
print: &DocumentFontCacheInfo,
326-
) -> (DictResource<Font<'static>>, [Option<&'a FontInfo>; 8]) {
327-
let mut infos = [None; 8];
328-
let mut dict = DictResource::new();
329-
for (cset, info) in print
330-
.chsets
331-
.iter()
332-
.map(FontCacheInfo::index)
333-
.enumerate()
334-
.filter_map(|(cset, fc_index)| {
335-
fc_index
336-
.and_then(|fc_index| font_info.get(fc_index))
337-
.map(|info| (cset, info))
338-
})
339-
{
340-
dict.insert(
341-
FONTS[cset].to_owned(),
342-
Resource::global(font_info.index(info)),
343-
);
344-
infos[cset] = Some(info);
323+
impl Fonts {
324+
pub fn font_dict<'a>(
325+
&'a self,
326+
print: &DocumentFontCacheInfo,
327+
) -> (DictResource<Font<'static>>, [Option<&'a FontInfo>; 8]) {
328+
let mut infos = [None; 8];
329+
let mut dict = DictResource::new();
330+
for (cset, info) in print
331+
.chsets
332+
.iter()
333+
.map(FontCacheInfo::index)
334+
.enumerate()
335+
.filter_map(|(cset, fc_index)| {
336+
fc_index
337+
.and_then(|fc_index| self.get(fc_index))
338+
.map(|info| (cset, info))
339+
})
340+
{
341+
dict.insert(FONTS[cset].to_owned(), Resource::global(self.index(info)));
342+
infos[cset] = Some(info);
343+
}
344+
(dict, infos)
345345
}
346-
(dict, infos)
347346
}

crates/sdo-pdf/src/sdoc/mod.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use pdf_create::{
22
common::{MediaBox, ProcSet, Rectangle},
3-
high::{DictResource, Font, GlobalResource, Page, Res, Resource, Resources, XObject},
3+
high::{DictResource, Font, GlobalResource, Handle, Page, Res, Resource, Resources, XObject},
44
};
55
use signum::{
66
chsets::cache::DocumentFontCacheInfo,
@@ -16,7 +16,10 @@ mod text;
1616
use contents::Contents;
1717
use text::TextContents;
1818

19-
use crate::{font::FontInfo, image::image_for_site};
19+
use crate::{
20+
font::{FontInfo, Fonts},
21+
image::image_for_site,
22+
};
2023

2124
/// Write the text for a PDF page
2225
fn write_pdf_page_text(
@@ -141,3 +144,31 @@ pub fn generate_pdf_page<GC: GenerationContext>(
141144
contents,
142145
})
143146
}
147+
148+
pub fn generate_pdf_pages<GC: GenerationContext>(
149+
gc: &GC,
150+
hnd: &mut Handle,
151+
overrides: &Overrides,
152+
font_info: &Fonts,
153+
) -> Result<(), crate::Error> {
154+
let res = &mut hnd.res;
155+
let pages = &mut hnd.pages;
156+
157+
let (fonts, infos) = font_info.font_dict(gc.fonts());
158+
let font_dict = res.push_font_dict(fonts);
159+
for page in gc.text_pages() {
160+
let page_info = gc.page_at(page.index as usize).unwrap();
161+
162+
let page = generate_pdf_page(
163+
gc,
164+
overrides,
165+
&infos,
166+
font_dict.clone(),
167+
page,
168+
page_info,
169+
res,
170+
)?;
171+
pages.push(page);
172+
}
173+
Ok(())
174+
}

crates/sdo-web/src/lib.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ use signum::{
1818
docs::{
1919
container::parse_sdoc0001_container,
2020
four_cc,
21-
hcim::{parse_image, Hcim},
22-
header, DocumentInfo, SDoc,
21+
hcim::{parse_image, Hcim, ImageSite},
22+
header, pbuf,
23+
tebu::PageText,
24+
DocumentInfo, GenerationContext, SDoc,
2325
},
2426
raster::{self, render_doc_page, render_editor_text},
2527
util::FourCC,
@@ -105,6 +107,24 @@ pub struct ActiveDocument {
105107
pd: FontKind,
106108
}
107109

110+
impl GenerationContext for ActiveDocument {
111+
fn image_sites(&self) -> &[ImageSite] {
112+
self.sdoc.image_sites()
113+
}
114+
115+
fn document_info(&self) -> &DocumentInfo {
116+
&self.di
117+
}
118+
119+
fn text_pages(&self) -> &[PageText] {
120+
&self.sdoc.tebu.pages
121+
}
122+
123+
fn page_at(&self, index: usize) -> Option<&pbuf::Page> {
124+
self.sdoc.pbuf.page_at(index)
125+
}
126+
}
127+
108128
#[wasm_bindgen]
109129
pub struct Handle {
110130
document: Document,

crates/signum/src/docs/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use nom::{
1010
number::complete::{be_u16, be_u32, le_u32},
1111
Finish, IResult,
1212
};
13+
use tebu::PageText;
1314

1415
use crate::{
1516
chsets::cache::DocumentFontCacheInfo,
@@ -241,4 +242,15 @@ pub trait GenerationContext {
241242
fn image_sites(&self) -> &[ImageSite];
242243
/// Get the document information
243244
fn document_info(&self) -> &DocumentInfo;
245+
246+
/// Get the text pages
247+
fn text_pages(&self) -> &[PageText];
248+
249+
/// Get a specific page buffer page
250+
fn page_at(&self, index: usize) -> Option<&pbuf::Page>;
251+
252+
/// Get the document font-cache info
253+
fn fonts(&self) -> &DocumentFontCacheInfo {
254+
&self.document_info().fonts
255+
}
244256
}

crates/signum/src/docs/pbuf.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ impl PBuf<'_> {
4747
pages,
4848
}
4949
}
50+
51+
/// Get a specific page
52+
pub fn page_at(&self, index: usize) -> Option<&Page> {
53+
self.pages[index].as_ref().map(|(p, _)| p)
54+
}
5055
}
5156

5257
#[derive(Debug, Serialize)]

src/cli/sdoc/pdf.rs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@ use pdf_create::{
66
common::{OutputIntent, OutputIntentSubtype, PdfString},
77
high::Handle,
88
};
9-
use sdo_pdf::{
10-
font::{font_dict, Fonts},
11-
prepare_info,
12-
sdoc::generate_pdf_page,
13-
MetaInfo,
14-
};
9+
use sdo_pdf::{font::Fonts, prepare_info, sdoc::generate_pdf_pages, MetaInfo};
1510
use signum::{
1611
chsets::{cache::ChsetCache, FontKind, UseTableVec},
17-
docs::{hcim::ImageSite, GenerationContext, Overrides},
12+
docs::{hcim::ImageSite, pbuf, tebu::PageText, GenerationContext, Overrides},
1813
};
1914

2015
use super::{Document, DocumentInfo};
@@ -37,6 +32,19 @@ pub fn prepare_meta(hnd: &mut Handle, meta: &MetaInfo) -> eyre::Result<()> {
3732
struct GenCtx<'a> {
3833
di: &'a DocumentInfo,
3934
image_sites: &'a [ImageSite],
35+
text_pages: &'a [PageText],
36+
pages: &'a [Option<pbuf::Page>],
37+
}
38+
39+
impl<'a> GenCtx<'a> {
40+
fn new(doc: &'a Document<'_>, di: &'a DocumentInfo) -> Self {
41+
Self {
42+
di,
43+
image_sites: &doc.sites[..],
44+
text_pages: &doc.tebu[..],
45+
pages: &doc.pages[..],
46+
}
47+
}
4048
}
4149

4250
impl GenerationContext for GenCtx<'_> {
@@ -47,6 +55,14 @@ impl GenerationContext for GenCtx<'_> {
4755
fn document_info(&self) -> &DocumentInfo {
4856
self.di
4957
}
58+
59+
fn text_pages(&self) -> &[PageText] {
60+
self.text_pages
61+
}
62+
63+
fn page_at(&self, index: usize) -> Option<&pbuf::Page> {
64+
self.pages[index].as_ref()
65+
}
5066
}
5167

5268
pub fn prepare_document(
@@ -56,30 +72,9 @@ pub fn prepare_document(
5672
overrides: &Overrides,
5773
font_info: &Fonts,
5874
) -> eyre::Result<()> {
59-
let gc = GenCtx {
60-
di,
61-
image_sites: &doc.sites[..],
62-
};
63-
64-
let (fonts, infos) = font_dict(font_info, &di.fonts);
75+
let gc = GenCtx::new(doc, di);
6576

66-
let font_dict = hnd.res.push_font_dict(fonts);
67-
68-
for page in &doc.tebu {
69-
let page_info = doc.pages[page.index as usize].as_ref().unwrap();
70-
let res = &mut hnd.res;
71-
72-
let page = generate_pdf_page(
73-
&gc,
74-
overrides,
75-
&infos,
76-
font_dict.clone(),
77-
page,
78-
page_info,
79-
res,
80-
)?;
81-
hnd.pages.push(page);
82-
}
77+
generate_pdf_pages(&gc, hnd, overrides, font_info)?;
8378

8479
Ok(())
8580
}

0 commit comments

Comments
 (0)