mirror of
https://github.com/storytold/designcraft.git
synced 2026-11-16 07:12:43 +00:00
Merge pull request #2291 from aiongg/integration-designcraft-20261104-1
Integration batch integration-designcraft-20261104-1
This commit is contained in:
11 files changed
+258
-17
No files matched your search
@@ -395,7 +395,14 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S
|
||||
for i in (0..n).rev() {
|
||||
hangs[i] = glyphs[i].is_space() && glyphs.get(i + 1).is_none_or(|nx| hangs[i + 1] || is_forced(nx.ch));
|
||||
}
|
||||
// The last glyph that isn't a soft hyphen, kept as the loop goes (a run of soft hyphens
|
||||
// searched back over all of them for every one).
|
||||
let mut last_plain: Option<&Glyph> = None;
|
||||
for (i, g) in glyphs.iter().enumerate() {
|
||||
// Set before the soft-hyphen case below reads it; a soft hyphen leaves it as it was.
|
||||
if g.ch != SOFT_HYPHEN {
|
||||
last_plain = Some(g);
|
||||
}
|
||||
let prev_hang = if i > 0 { right_hang(&glyphs[i - 1], sp) } else { 0.0 };
|
||||
if hangs[i] {
|
||||
continue;
|
||||
@@ -444,7 +451,7 @@ fn items(glyphs: &[Glyph], hyph_after: &[bool], emergency_after: &[bool], sp: &S
|
||||
continue;
|
||||
}
|
||||
if g.ch == SOFT_HYPHEN {
|
||||
let hw = glyphs[..i].iter().rev().find(|g| g.ch != SOFT_HYPHEN).map_or(g.size * 0.33, |p| hyphen_width(p, &mut hy_cache));
|
||||
let hw = last_plain.map_or(g.size * 0.33, |p| hyphen_width(p, &mut hy_cache));
|
||||
let mut p = Item::penalty(hw, sp.hyphen_penalty, true);
|
||||
p.auto = true;
|
||||
p.zone_from = last_space;
|
||||
|
||||
@@ -15,6 +15,9 @@ pub struct Dictionary {
|
||||
|
||||
const MAGIC: &[u8] = b"DCHYPH1\n";
|
||||
|
||||
/// Most bytes a serialized dictionary may inflate to (the bundled English one is far smaller).
|
||||
pub const MAX_INFLATED_BYTES: usize = 64 << 20;
|
||||
|
||||
impl Dictionary {
|
||||
/// Build from `(lowercase word, break mask)` pairs (sorted and de-duplicated here; the first
|
||||
/// entry of a duplicate wins).
|
||||
@@ -76,9 +79,11 @@ impl Dictionary {
|
||||
miniz_oxide::deflate::compress_to_vec(&raw, 10)
|
||||
}
|
||||
|
||||
/// Deserialize data written by [`Dictionary::to_bytes`].
|
||||
/// Deserialize data written by [`Dictionary::to_bytes`]. At most [`MAX_INFLATED_BYTES`] are
|
||||
/// inflated: a few compressed bytes can stand for gigabytes.
|
||||
pub fn from_bytes(data: &[u8]) -> Result<Dictionary, String> {
|
||||
let raw = miniz_oxide::inflate::decompress_to_vec(data).map_err(|e| format!("hyphenation dictionary: inflate failed: {e:?}"))?;
|
||||
let raw = miniz_oxide::inflate::decompress_to_vec_with_limit(data, MAX_INFLATED_BYTES)
|
||||
.map_err(|e| format!("hyphenation dictionary: inflate failed (or over {} MB): {e:?}", MAX_INFLATED_BYTES >> 20))?;
|
||||
let body = raw.strip_prefix(MAGIC).ok_or("hyphenation dictionary: bad header")?;
|
||||
let mut d = Dictionary { words: String::with_capacity(body.len()), offs: vec![0], masks: Vec::new() };
|
||||
let mut cur: Vec<u8> = Vec::new();
|
||||
@@ -147,4 +152,15 @@ mod tests {
|
||||
assert_eq!(back.get("naïveté").map(|m| hyphenated("naïveté", m)), Some("naïve-té".to_string()));
|
||||
assert!(back.get("dog").is_none());
|
||||
}
|
||||
|
||||
/// Data that inflates past the limit is refused instead of being inflated to the end.
|
||||
#[test]
|
||||
fn an_inflation_bomb_is_refused() {
|
||||
let mut raw = MAGIC.to_vec();
|
||||
raw.resize(MAX_INFLATED_BYTES + 1, b'a');
|
||||
let bomb = miniz_oxide::deflate::compress_to_vec(&raw, 10);
|
||||
assert!(bomb.len() < 1 << 20, "{}", bomb.len());
|
||||
let e = Dictionary::from_bytes(&bomb).err().unwrap_or_default();
|
||||
assert!(e.contains("inflate failed"), "{e}");
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,18 @@ fn all_lines(cs: &ComposedStory) -> Vec<&Line> {
|
||||
cs.frames.iter().flat_map(|f| f.lines.iter()).collect()
|
||||
}
|
||||
|
||||
/// A long run of soft hyphens composes: each one's hyphen width comes from the last other glyph,
|
||||
/// kept as the breaker goes (it searched back over the whole run for every one).
|
||||
#[test]
|
||||
fn a_run_of_soft_hyphens_composes() {
|
||||
let text = format!("Word{}end", "\u{AD}".repeat(3_900));
|
||||
let (d, sid, _) = doc_with(&text, Rect::new(36.0, 36.0, 300.0, 700.0), ParaAttrs::default());
|
||||
let t = std::time::Instant::now();
|
||||
let cs = compose_story(&d, sid, &ComposeOptions::default());
|
||||
assert!(t.elapsed() < std::time::Duration::from_secs(20), "{:?}", t.elapsed());
|
||||
assert!(!all_lines(&cs).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn composes_simple_paragraph() {
|
||||
let (d, sid, _) = doc_with(LOREM, Rect::new(36.0, 36.0, 300.0, 700.0), ParaAttrs::default());
|
||||
|
||||
@@ -14,6 +14,10 @@ pub struct RecentPreview {
|
||||
pub rgba: Vec<u8>,
|
||||
}
|
||||
|
||||
/// The most pixels a recent file's preview may have to be shown (its PNG is bounded by
|
||||
/// `designcraft_format::MAX_PREVIEW_BYTES`, but a small PNG can claim a huge picture).
|
||||
const MAX_PREVIEW_PIXELS: u64 = 1 << 24;
|
||||
|
||||
/// The picture a `.designcraft` file saved of its first page; None when the file is gone, isn't
|
||||
/// ours, or was saved before files had one (it gets one when saved again).
|
||||
pub fn recent_preview(path: &str) -> Option<RecentPreview> {
|
||||
@@ -21,6 +25,12 @@ pub fn recent_preview(path: &str) -> Option<RecentPreview> {
|
||||
{
|
||||
// Only the zip directory and the preview entry are read, not the whole document.
|
||||
let png = designcraft_format::read_preview(std::io::BufReader::new(crate::io::open(path).ok()?))?;
|
||||
// A preview is a small picture: one whose header claims more pixels than that isn't
|
||||
// decoded (a decode is bounded, but not at thumbnail size).
|
||||
let (w, h) = designcraft_images::pixel_size(&png)?;
|
||||
if u64::from(w).saturating_mul(u64::from(h)) > MAX_PREVIEW_PIXELS {
|
||||
return None;
|
||||
}
|
||||
let img = designcraft_images::decode_rgba(&png)?;
|
||||
let (width, height) = img.dimensions();
|
||||
Some(RecentPreview { width, height, rgba: img.into_raw() })
|
||||
|
||||
@@ -2859,7 +2859,12 @@ fn clipping_path(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let Some(px) = designcraft_render::decode_pixmap_page(&asset.data, asset.page) else { continue };
|
||||
// A placed PDF cropped to a box: trace that box, which graphic space spans.
|
||||
let px = match designcraft_render::shown_box(asset) {
|
||||
Some(frac) => designcraft_render::crop_pixmap(&px, frac).unwrap_or(px),
|
||||
// A crop too big to trace is skipped, as a graphic that fails to decode is: tracing
|
||||
// the whole page instead would trace something else than the box.
|
||||
Some(frac) => match designcraft_render::crop_pixmap(&px, frac) {
|
||||
Some(cropped) => cropped,
|
||||
None => continue,
|
||||
},
|
||||
None => px,
|
||||
};
|
||||
let (pw, ph) = (px.width() as usize, px.height() as usize);
|
||||
@@ -4660,6 +4665,40 @@ mod clipping_tests {
|
||||
assert!(s.execute("object.clippingPath", &json!({"ids": [f], "threshold": 255})).is_err());
|
||||
}
|
||||
|
||||
/// A placed PDF whose crop box is too large to trace is skipped, not traced from the whole page.
|
||||
#[test]
|
||||
fn clipping_path_skips_a_pdf_whose_crop_is_too_large_to_trace() {
|
||||
let content = "0 0 0 rg 10 10 50 50 re f";
|
||||
let objs = [
|
||||
"<</Type/Catalog/Pages 2 0 R>>".to_string(),
|
||||
"<</Type/Pages/Kids[3 0 R]/Count 1>>".into(),
|
||||
"<</Type/Page/Parent 2 0 R/MediaBox[-30000 -30000 30000 30000]/CropBox[0 0 100 100]/Contents 4 0 R>>".into(),
|
||||
format!("<</Length {}>>\nstream\n{content}\nendstream", content.len()),
|
||||
];
|
||||
let mut bytes = b"%PDF-1.7\n".to_vec();
|
||||
let mut offsets = Vec::new();
|
||||
for (i, body) in objs.iter().enumerate() {
|
||||
offsets.push(bytes.len());
|
||||
bytes.extend_from_slice(format!("{} 0 obj\n{body}\nendobj\n", i + 1).as_bytes());
|
||||
}
|
||||
let xref = bytes.len();
|
||||
bytes.extend_from_slice(b"xref\n0 5\n0000000000 65535 f\r\n");
|
||||
for at in offsets {
|
||||
bytes.extend_from_slice(format!("{at:010} 00000 n\r\n").as_bytes());
|
||||
}
|
||||
bytes.extend_from_slice(format!("trailer\n<</Size 5/Root 1 0 R>>\nstartxref\n{xref}\n%%EOF\n").as_bytes());
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let f = s.execute("frame.create", &json!({"rect": [100, 100, 200, 200]})).unwrap()["id"].as_u64().unwrap();
|
||||
let placed = s
|
||||
.execute("file.place", &json!({"base64": super::super::file::base64_encode(&bytes), "name": "wide.pdf", "frame": f, "pdfCrop": "media"}));
|
||||
assert!(placed.is_ok(), "{placed:?}");
|
||||
let before = s.doc().unwrap().doc.item(ItemId(f)).unwrap().shape;
|
||||
let e = s.execute("object.clippingPath", &json!({"ids": [f], "type": "alpha"}));
|
||||
assert!(e.is_err(), "the graphic is skipped, so there is nothing to trace: {e:?}");
|
||||
assert_eq!(s.doc().unwrap().doc.item(ItemId(f)).unwrap().shape, before, "the frame keeps its shape");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clipping_path_options_report_invert_and_remove_the_clip() {
|
||||
let mut s = Session::new();
|
||||
|
||||
+26
-2
@@ -129,6 +129,17 @@ pub(crate) fn body_html<'a>(doc: &'a Document, epub: bool, split: Option<&EpubSp
|
||||
}
|
||||
let mut body = String::new();
|
||||
let mut toc = toc_entries(doc, &order);
|
||||
// `Document::item` searches every spread: one pre-order index for all the placed graphics.
|
||||
let mut items: std::collections::HashMap<ItemId, &Item> = std::collections::HashMap::new();
|
||||
if order.iter().any(|b| matches!(b, Block::Image(_))) {
|
||||
for sp in doc.spreads.iter().map(|s| &**s).chain(doc.parents.iter().map(|s| &**s)) {
|
||||
for it in &sp.items {
|
||||
it.walk(&mut |i| {
|
||||
items.entry(i.id).or_insert(i);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
for (k, b) in order.iter().enumerate() {
|
||||
match b {
|
||||
// The endnote frame's text is generated from the endnotes, which end the body as a
|
||||
@@ -139,7 +150,7 @@ pub(crate) fn body_html<'a>(doc: &'a Document, epub: bool, split: Option<&EpubSp
|
||||
let _ = write!(body, "<section id=\"{anchor}\">\n{}</section>\n", writer.story(*sid, epub.then_some(anchor.as_str())));
|
||||
}
|
||||
Block::Image(iid) => {
|
||||
let Some(it) = doc.item(*iid) else { continue };
|
||||
let Some(&it) = items.get(iid) else { continue };
|
||||
let Some((markup, media)) = writer.graphic(it) else { continue };
|
||||
if media {
|
||||
let _ = writeln!(body, "<figure>{markup}</figure>");
|
||||
@@ -185,13 +196,26 @@ pub struct HtmlOptions {
|
||||
pub generic_families: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
/// Bytes of data URIs one HTML export may write (a document can place one large asset many times).
|
||||
const MAX_DATA_URI_BYTES: usize = 512 * 1024 * 1024;
|
||||
|
||||
/// File › Export › HTML: one self-contained page (styles inline, images as data URIs) with the
|
||||
/// document's stories and graphics in reading order.
|
||||
pub fn export_html(doc: &Document, opts: &HtmlOptions) -> String {
|
||||
let title = opts.title.clone().unwrap_or_else(|| doc.metadata_title().to_string());
|
||||
let language = opts.language.as_deref().unwrap_or("en");
|
||||
let lang = language_tag(language);
|
||||
let body = body_html(doc, false, None, &|_, mime, data| format!("data:{mime};base64,{}", base64(data))).html;
|
||||
// Each placement embeds its own copy of the asset; past the budget a placement gets no source.
|
||||
let spent = std::cell::Cell::new(0usize);
|
||||
let body = body_html(doc, false, None, &|_, mime, data| {
|
||||
let n = data.len().div_ceil(3).saturating_mul(4).saturating_add(mime.len() + 13);
|
||||
if n > MAX_DATA_URI_BYTES - spent.get() {
|
||||
return String::new();
|
||||
}
|
||||
spent.set(spent.get() + n);
|
||||
format!("data:{mime};base64,{}", base64(data))
|
||||
})
|
||||
.html;
|
||||
format!(
|
||||
"<!DOCTYPE html>\n<html lang=\"{lang}\">\n<head>\n<meta charset=\"utf-8\">\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n<meta name=\"generator\" content=\"DesignCraft\">\n<title>{}</title>\n<style>\nbody {{ max-width: 40em; margin: 2em auto; padding: 0 1em; }}\nfigure {{ margin: 1em 0; }} figure img {{ max-width: 100%; height: auto; }}\n{}</style>\n</head>\n<body>\n{body}</body>\n</html>\n",
|
||||
xml_text(&title),
|
||||
|
||||
@@ -21,6 +21,17 @@ fn modified(doc: &Document, opts: &EpubOptions) -> String {
|
||||
t.map_or_else(|| "2026-01-01T00:00:00Z".to_string(), iso8601)
|
||||
}
|
||||
|
||||
/// Characters of the title the package keeps: it is copied into every content file, so a huge one
|
||||
/// would multiply by the number of chapters or pages.
|
||||
const MAX_TITLE_CHARS: usize = 1024;
|
||||
|
||||
fn capped_title(title: String) -> String {
|
||||
match title.char_indices().nth(MAX_TITLE_CHARS) {
|
||||
Some((at, _)) => title[..at].to_string(),
|
||||
None => title,
|
||||
}
|
||||
}
|
||||
|
||||
/// The book's identifier: the given one, else a `urn:uuid` derived from the title and the
|
||||
/// document's creation time, so a book keeps its identifier from export to export and two books
|
||||
/// with the same title don't share one.
|
||||
@@ -70,7 +81,7 @@ pub struct FixedPage {
|
||||
|
||||
/// A pre-paginated (fixed-layout) EPUB: one XHTML page per document page.
|
||||
pub fn export_fixed_epub(doc: &Document, pages: &[FixedPage], opts: &EpubOptions) -> Result<Vec<u8>, EpubError> {
|
||||
let title = opts.title.clone().unwrap_or_else(|| doc.metadata_title().to_string());
|
||||
let title = capped_title(opts.title.clone().unwrap_or_else(|| doc.metadata_title().to_string()));
|
||||
let lang = language_tag(&opts.language);
|
||||
let id = identifier(doc, opts, &title);
|
||||
let modified = modified(doc, opts);
|
||||
@@ -161,7 +172,7 @@ fn write_package<'a>(opf: &str, nav: &str, entries: impl IntoIterator<Item = (St
|
||||
|
||||
/// Export the document as a reflowable EPUB 3.
|
||||
pub fn export_epub(doc: &Document, opts: &EpubOptions) -> Result<Vec<u8>, EpubError> {
|
||||
let title = opts.title.clone().unwrap_or_else(|| doc.metadata_title().to_string());
|
||||
let title = capped_title(opts.title.clone().unwrap_or_else(|| doc.metadata_title().to_string()));
|
||||
let id = identifier(doc, opts, &title);
|
||||
let modified = modified(doc, opts);
|
||||
let Body { html: body, images, toc, .. } = body_html(doc, true, opts.split.as_ref(), &|name, _, _| name.to_string());
|
||||
|
||||
@@ -50,8 +50,17 @@ pub(crate) struct StoryWriter<'a, 'f> {
|
||||
heading_ids: Option<String>,
|
||||
/// Where EPUB content files start (see [`starts_chapter`]).
|
||||
pub(crate) split: EpubSplit,
|
||||
/// Per story (by address): the byte offsets of its footnote and endnote references, so a note's
|
||||
/// index is a search, not a count over the text before it.
|
||||
note_refs: std::collections::HashMap<usize, (Vec<usize>, Vec<usize>)>,
|
||||
/// Bytes of variable values still allowed into the output (see [`MAX_VARIABLE_BYTES`]).
|
||||
variable_bytes: usize,
|
||||
}
|
||||
|
||||
/// Bytes of variable values one export may write: one stored value referenced many times would
|
||||
/// otherwise multiply into the output.
|
||||
const MAX_VARIABLE_BYTES: usize = 64 * 1024 * 1024;
|
||||
|
||||
impl<'a, 'f> StoryWriter<'a, 'f> {
|
||||
pub(crate) fn new(doc: &'a Document, epub: bool, img_src: Option<&'f dyn Fn(&str, &str, &[u8]) -> String>) -> Self {
|
||||
StoryWriter {
|
||||
@@ -69,9 +78,22 @@ impl<'a, 'f> StoryWriter<'a, 'f> {
|
||||
no_alt: Vec::new(),
|
||||
heading_ids: None,
|
||||
split: doc.settings.epub_split.clone(),
|
||||
note_refs: std::collections::HashMap::new(),
|
||||
variable_bytes: MAX_VARIABLE_BYTES,
|
||||
}
|
||||
}
|
||||
|
||||
/// Index of the footnote (or endnote) whose reference is at byte `pos` of `st`
|
||||
/// (`Story::notes_before` / `endnotes_before`, from offsets found once per story).
|
||||
fn note_index(&mut self, st: &Story, pos: usize, endnote: bool) -> usize {
|
||||
let (foot, end) = self.note_refs.entry(std::ptr::from_ref(st) as usize).or_insert_with(|| {
|
||||
let at = |c: char| st.text.match_indices(c).map(|(i, _)| i).collect::<Vec<_>>();
|
||||
(at(FOOTNOTE_REF), at(ENDNOTE_REF))
|
||||
});
|
||||
let list = if endnote { end } else { foot };
|
||||
list.partition_point(|&p| p < pos)
|
||||
}
|
||||
|
||||
fn epub_type(&self, t: &str) -> String {
|
||||
if self.epub { format!(" epub:type=\"{t}\"") } else { String::new() }
|
||||
}
|
||||
@@ -182,7 +204,7 @@ impl<'a, 'f> StoryWriter<'a, 'f> {
|
||||
if c != FOOTNOTE_REF && c != ENDNOTE_REF && c != OBJECT_MARK {
|
||||
continue;
|
||||
}
|
||||
t.push_str(&run_text(doc, raw.get(seg_start..i).unwrap_or_default()));
|
||||
t.push_str(&run_text(doc, raw.get(seg_start..i).unwrap_or_default(), &mut self.variable_bytes));
|
||||
seg_start = i + c.len_utf8();
|
||||
if c == OBJECT_MARK {
|
||||
let k = mark.unwrap_or_else(|| st.text.get(..a).map_or(0, |t| t.matches(OBJECT_MARK).count()));
|
||||
@@ -198,7 +220,7 @@ impl<'a, 'f> StoryWriter<'a, 'f> {
|
||||
}
|
||||
}
|
||||
}
|
||||
t.push_str(&run_text(doc, raw.get(seg_start..).unwrap_or_default()));
|
||||
t.push_str(&run_text(doc, raw.get(seg_start..).unwrap_or_default(), &mut self.variable_bytes));
|
||||
t
|
||||
}
|
||||
|
||||
@@ -303,7 +325,8 @@ impl<'a, 'f> StoryWriter<'a, 'f> {
|
||||
/// The footnote whose reference is at byte `pos` of `st`: a note reference in `out`, the note
|
||||
/// itself in `after`.
|
||||
fn footnote(&mut self, st: &Story, pos: usize, depth: usize, out: &mut String, after: &mut String) {
|
||||
let Some(note) = st.notes.get(st.notes_before(pos)) else { return };
|
||||
let k = self.note_index(st, pos, false);
|
||||
let Some(note) = st.notes.get(k) else { return };
|
||||
let doc = self.doc;
|
||||
let o = &doc.footnote_options;
|
||||
let n = self.next_footnote;
|
||||
@@ -325,7 +348,8 @@ impl<'a, 'f> StoryWriter<'a, 'f> {
|
||||
/// The endnote whose reference is at byte `pos` of `st`: a note reference in `out`; the note
|
||||
/// joins the endnotes section.
|
||||
fn endnote(&mut self, st: &Story, pos: usize, depth: usize, out: &mut String) {
|
||||
let Some(note) = st.endnotes.get(st.endnotes_before(pos)) else { return };
|
||||
let k = self.note_index(st, pos, true);
|
||||
let Some(note) = st.endnotes.get(k) else { return };
|
||||
let n = self.next_endnote;
|
||||
self.next_endnote = n.saturating_add(1);
|
||||
self.endnote_ids = self.endnote_ids.saturating_add(1);
|
||||
@@ -416,13 +440,21 @@ impl<'a, 'f> StoryWriter<'a, 'f> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Run text with variables replaced by their values.
|
||||
fn run_text(doc: &Document, raw: &str) -> String {
|
||||
/// Run text with variables replaced by their values; `left` is what the export may still write
|
||||
/// of them (a value that doesn't fit is left out).
|
||||
fn run_text(doc: &Document, raw: &str, left: &mut usize) -> String {
|
||||
if raw.chars().any(|c| designcraft_doc::vars::var_index(c).is_some()) {
|
||||
let sub: String = raw
|
||||
.chars()
|
||||
.map(|c| match designcraft_doc::vars::var_index(c) {
|
||||
Some(i) => doc.variable_value(i, None).unwrap_or_default(),
|
||||
Some(i) => {
|
||||
let v = doc.variable_value(i, None).unwrap_or_default();
|
||||
if v.len() > *left {
|
||||
return String::new();
|
||||
}
|
||||
*left -= v.len();
|
||||
v
|
||||
}
|
||||
None => c.to_string(),
|
||||
})
|
||||
.collect();
|
||||
|
||||
@@ -26,6 +26,36 @@ fn exports_valid_package_in_reading_order() {
|
||||
assert!(z.by_name("OEBPS/nav.xhtml").is_ok());
|
||||
}
|
||||
|
||||
/// Many footnotes and endnotes each export with their own text, in reference order.
|
||||
#[test]
|
||||
fn many_notes_keep_their_own_text() {
|
||||
let mut d = Document::new(&NewDocument::default());
|
||||
let lid = d.default_layer();
|
||||
let (_, sid) = d.add_text_frame(SpreadRef::Doc(0), Rect::new(36.0, 36.0, 300.0, 400.0), lid, "x", ParaFormat::default()).unwrap();
|
||||
let st = d.story_mut(sid).unwrap();
|
||||
for i in 0..300 {
|
||||
let end = st.len();
|
||||
st.insert_note(end, &format!("foot{i}z"), ParaFormat::default());
|
||||
let end = st.len();
|
||||
st.insert_endnote(end, &format!("end{i}z"), ParaFormat::default());
|
||||
}
|
||||
let html = export_html(&d, &HtmlOptions::default());
|
||||
let at = |needle: &str| html.find(needle).unwrap_or_else(|| panic!("{needle}"));
|
||||
assert!(at("foot0z") < at("foot1z") && at("foot298z") < at("foot299z"));
|
||||
assert!(at("end0z") < at("end1z") && at("end298z") < at("end299z"));
|
||||
}
|
||||
|
||||
/// A huge title is cut before it is copied into every content file.
|
||||
#[test]
|
||||
fn a_huge_title_is_capped() {
|
||||
let d = tables_and_notes();
|
||||
let opts = EpubOptions { title: Some("T".repeat(5000)), ..EpubOptions::default() };
|
||||
let mut z = zip::ZipArchive::new(std::io::Cursor::new(export_epub(&d, &opts).unwrap())).unwrap();
|
||||
let mut s = String::new();
|
||||
z.by_name("OEBPS/content.xhtml").unwrap().read_to_string(&mut s).unwrap();
|
||||
assert!(s.contains(&"T".repeat(1024)) && !s.contains(&"T".repeat(1025)), "{}", s.len());
|
||||
}
|
||||
|
||||
/// #766: anchored graphics are exported where they are anchored (inline in the text, an
|
||||
/// above-line object as a block of its own), and their images go into the package.
|
||||
#[test]
|
||||
|
||||
@@ -265,6 +265,11 @@ pub fn crop_pixmap(pm: &Pixmap, frac: [f64; 4]) -> Option<Pixmap> {
|
||||
return None;
|
||||
}
|
||||
let (w, h) = (w as u16, h as u16);
|
||||
// A box reaching far outside the page would mostly be transparent padding: it gets no more
|
||||
// pixels than a page raster may have.
|
||||
if u64::from(w) * u64::from(h) > crate::MAX_PAGE_PIXELS {
|
||||
return None;
|
||||
}
|
||||
let mut out = Pixmap::new(w, h);
|
||||
let src = pm.data();
|
||||
let dst = out.data_mut();
|
||||
@@ -286,3 +291,17 @@ pub fn crop_pixmap(pm: &Pixmap, frac: [f64; 4]) -> Option<Pixmap> {
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod crop_tests {
|
||||
use super::*;
|
||||
|
||||
/// A crop box reaching far outside the page gets no bigger a pixmap than a page may have.
|
||||
#[test]
|
||||
fn crop_outside_the_page_is_bounded() {
|
||||
let pm = Pixmap::new(10, 10);
|
||||
assert!(crop_pixmap(&pm, [-0.5, -0.5, 1.5, 1.5]).is_some_and(|c| (c.width(), c.height()) == (20, 20)));
|
||||
// 60,000 × 60,000 pixels: within u16 each way, far past the page raster budget.
|
||||
assert!(crop_pixmap(&pm, [-3000.0, -3000.0, 3000.0, 3000.0]).is_none());
|
||||
}
|
||||
}
|
||||
+43
-2
@@ -249,6 +249,29 @@ fn cmd_ci() -> Result<(), String> {
|
||||
}
|
||||
|
||||
const PNGSUITE_URL: &str = "http://www.schaik.com/pngsuite/PngSuite-2017jul19.tgz";
|
||||
/// The real archive is 68 KB, 178 entries and 256 KB expanded; the site has no HTTPS, so what
|
||||
/// arrives is bounded instead of trusted: compressed size, entries, and expanded bytes.
|
||||
const PNGSUITE_MAX_ARCHIVE: u64 = 1 << 20;
|
||||
const PNGSUITE_MAX_ENTRIES: usize = 1000;
|
||||
const PNGSUITE_MAX_EXTRACTED: u64 = 16 << 20;
|
||||
|
||||
/// Bytes of the files under `dir`; `u64::MAX` when any part of it cannot be read, so the caller refuses it.
|
||||
fn dir_bytes(dir: &std::path::Path) -> u64 {
|
||||
let mut total = 0u64;
|
||||
let mut stack = vec![dir.to_path_buf()];
|
||||
while let Some(d) = stack.pop() {
|
||||
let Ok(rd) = std::fs::read_dir(&d) else { return u64::MAX };
|
||||
for e in rd {
|
||||
let Ok(e) = e else { return u64::MAX };
|
||||
match e.metadata() {
|
||||
Ok(m) if m.is_dir() => stack.push(e.path()),
|
||||
Ok(m) => total = total.saturating_add(m.len()),
|
||||
Err(_) => return u64::MAX,
|
||||
}
|
||||
}
|
||||
}
|
||||
total
|
||||
}
|
||||
|
||||
fn cmd_corpus(download: bool) -> Result<(), String> {
|
||||
let corpus = root().join("corpus");
|
||||
@@ -273,12 +296,30 @@ Tests that use a corpus skip cleanly when it is absent.
|
||||
std::fs::create_dir_all(&dest).map_err(|e| format!("create {}: {e}", dest.display()))?;
|
||||
let tgz = corpus.join("PngSuite-2017jul19.tgz");
|
||||
let mut curl = Command::new("curl");
|
||||
curl.args(["-fsSL", "-o"]).arg(&tgz).arg(PNGSUITE_URL);
|
||||
curl.args(["-fsSL", "--max-filesize"]).arg(PNGSUITE_MAX_ARCHIVE.to_string()).arg("-o").arg(&tgz).arg(PNGSUITE_URL);
|
||||
run(curl, &format!("curl {PNGSUITE_URL}"))?;
|
||||
let refuse = |why: String| {
|
||||
let _ = std::fs::remove_file(&tgz);
|
||||
Err(why)
|
||||
};
|
||||
if std::fs::metadata(&tgz).map_or(true, |m| m.len() > PNGSUITE_MAX_ARCHIVE) {
|
||||
return refuse(format!("the PngSuite download is larger than {PNGSUITE_MAX_ARCHIVE} bytes"));
|
||||
}
|
||||
let listing = Command::new("tar").arg("-tzf").arg(&tgz).output().map_err(|e| format!("tar -tzf: failed to spawn: {e}"))?;
|
||||
if !listing.status.success() || listing.stdout.iter().filter(|b| **b == b'\n').count() > PNGSUITE_MAX_ENTRIES {
|
||||
return refuse(format!("the PngSuite download isn't a tar.gz of at most {PNGSUITE_MAX_ENTRIES} entries"));
|
||||
}
|
||||
let mut tar = Command::new("tar");
|
||||
tar.arg("-xzf").arg(&tgz).arg("-C").arg(&dest);
|
||||
run(tar, "tar -xzf PngSuite-2017jul19.tgz")?;
|
||||
if let Err(e) = run(tar, "tar -xzf PngSuite-2017jul19.tgz") {
|
||||
let _ = std::fs::remove_dir_all(&dest);
|
||||
return refuse(e);
|
||||
}
|
||||
let _ = std::fs::remove_file(&tgz);
|
||||
if dir_bytes(&dest) > PNGSUITE_MAX_EXTRACTED {
|
||||
let _ = std::fs::remove_dir_all(&dest);
|
||||
return Err(format!("the PngSuite archive expanded to more than {PNGSUITE_MAX_EXTRACTED} bytes; removed {}", dest.display()));
|
||||
}
|
||||
let n = std::fs::read_dir(&dest).map(|d| d.flatten().filter(|e| e.path().extension().is_some_and(|x| x == "png")).count()).unwrap_or(0);
|
||||
println!("PngSuite: {n} PNG files in {}", dest.display());
|
||||
if n == 0 { Err("no PNG files extracted".into()) } else { Ok(()) }
|
||||
|
||||
Reference in new issue
Block a user