mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-20 02:58:19 +00:00
* M14: bound large-PDF memory use (#307) Inspect large documents lazily, share parser/resource metadata, bound full-save and raster retention, and cache signature candidate discovery safely. Preserve upstream rendering deduplication, pixel alignment, signature previews, and organizer insertion behavior, with regression coverage for their interaction. Validated with 1,336 workspace tests, 193 parser tests, strict workspace Clippy, 30 WASM crate checks, 1x/2x GUI protocols, and same-PDF workflow comparisons. * cos: drop the writer cache-policy probe and its test-only hooks in production paths * cos: the temporary reader keeps parsed objects until a count or owned-bytes budget Since #552 a stream read from the file is a view into the input, so clearing the whole parse cache after every read saved little memory and cost most of the full-save slowdown (every object parsed twice, indirect /Length objects re-parsed per stream). Clear only past OBJECTS entries or STREAM_BYTES of data of the cache's own (decrypted streams, strings). * cos: the writers refuse a document opened with a stream limit (read-only inspection) * cos: a full save fails rather than writing null when an object reads differently the second time * render: parsing is not timed by the watchdog; timing tests with wide margins A slow first parse (a huge or damaged file) made the whole pool fail every page for its lifetime. Parsing, shared or private, is now its own untimed stage, as on main before the shared parser; rendering is timed from when parsing ends. The init-timeout tests become one deterministic test (a 2 s parse against a 10 ms limit, shared and private). Watchdog tests use limits and injected delays ~10x apart instead of 25-50 ms against real renders. Also: saturating retired-worker count, main's stop-flag test runs on private parsers (the first shared timeout is a private retry), set_queue documents that unlisted results are dropped, and retire() documents the permanent per-worker-parser fallback. * render: page numbers in the lazy page tree use checked conversion * render: the lazy inspector is compared with lopdf on named destinations and object streams too * ui: on-screen pages always render; pages turned back to stay cached; stable thumbnail tags - Visible pages and tiles are always admitted and uploaded (the viewport bounds them); the byte and count limits decide only how much prefetch fits. A HiDPI two-up view or a 5K viewport left pages on 'Rendering...' for good. - Pages no longer demanded stay cached, nearest the current page first, while the 128 MiB allowance and the 24-page count have room: paging back no longer re-renders. - Thumbnail density follows the demand count rounded up to a power of two, so rows scrolling in and out of an organize grid don't change every thumbnail's tag and re-render it. - A suspended (background) tab drains its pool's finished results instead of holding them. - Organize drag-and-drop ends on the primary button only. - Tests: the stale-result test uses valid pixels and checks nothing was uploaded; the organizer wheel test runs 40 steps; regressions for the first three fixes. * vendor: mark the hayro-syntax lock and SegmentList patches and list them as (8) and (9); clippy 1.99 as_chunks in a raster test * render: clippy 1.99 — contains over iter().any() in a raster test --------- Co-authored-by: Brandon Thomas <echelon@gmail.com>
363 lines
8.7 KiB
Rust
363 lines
8.7 KiB
Rust
//! Names.
|
|
|
|
use crate::filter::ascii_hex::decode_hex_digit;
|
|
use crate::object::Object;
|
|
use crate::object::macros::object;
|
|
use crate::reader::Reader;
|
|
use crate::reader::{Readable, ReaderContext, Skippable};
|
|
use crate::trivia::is_regular_character;
|
|
use core::borrow::Borrow;
|
|
use core::fmt::{self, Debug, Formatter};
|
|
use core::hash::{Hash, Hasher};
|
|
use core::ops::Deref;
|
|
use smallvec::SmallVec;
|
|
|
|
#[derive(Clone)]
|
|
enum NameInner<'a> {
|
|
Borrowed(&'a [u8]),
|
|
Owned(SmallVec<[u8; 23]>),
|
|
}
|
|
|
|
/// A PDF name object.
|
|
#[derive(Clone)]
|
|
pub struct Name<'a>(NameInner<'a>);
|
|
|
|
impl<'a> Deref for Name<'a> {
|
|
type Target = [u8];
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
self.as_ref()
|
|
}
|
|
}
|
|
|
|
impl AsRef<[u8]> for Name<'_> {
|
|
fn as_ref(&self) -> &[u8] {
|
|
match &self.0 {
|
|
NameInner::Borrowed(data) => data,
|
|
NameInner::Owned(data) => data,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Borrow<[u8]> for Name<'_> {
|
|
fn borrow(&self) -> &[u8] {
|
|
self.as_ref()
|
|
}
|
|
}
|
|
|
|
impl PartialEq for Name<'_> {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.as_ref() == other.as_ref()
|
|
}
|
|
}
|
|
|
|
impl Eq for Name<'_> {}
|
|
|
|
impl Hash for Name<'_> {
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
self.as_ref().hash(state);
|
|
}
|
|
}
|
|
|
|
impl PartialOrd for Name<'_> {
|
|
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
|
|
Some(self.cmp(other))
|
|
}
|
|
}
|
|
|
|
impl Ord for Name<'_> {
|
|
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
|
|
self.as_ref().cmp(other.as_ref())
|
|
}
|
|
}
|
|
|
|
impl<'a> Name<'a> {
|
|
// PdfCraft patch: (#307) lazy dictionary indexes cannot borrow through a
|
|
// OnceLock without making the enclosing PDF pages invariant in their lifetime.
|
|
// Ordinary PDF keys fit inline, so owning these bytes needs no allocation.
|
|
pub(crate) fn into_owned(self) -> Name<'static> {
|
|
Name(NameInner::Owned(match self.0 {
|
|
NameInner::Borrowed(data) => SmallVec::from_slice(data),
|
|
NameInner::Owned(data) => data,
|
|
}))
|
|
}
|
|
|
|
/// Create a new name from a sequence of bytes.
|
|
#[inline]
|
|
pub fn new(data: &'a [u8]) -> Option<Self> {
|
|
if !data.contains(&b'#') {
|
|
Some(Self::new_unescaped(data))
|
|
} else {
|
|
Self::new_escaped(data)
|
|
}
|
|
}
|
|
|
|
/// Create a new name from an unescaped byte sequence.
|
|
#[inline]
|
|
pub fn new_unescaped(data: &'a [u8]) -> Self {
|
|
Self(NameInner::Borrowed(data))
|
|
}
|
|
|
|
/// Create a new name from bytes that may contain escape sequences.
|
|
#[inline]
|
|
pub fn new_escaped(data: &'a [u8]) -> Option<Self> {
|
|
let mut result = SmallVec::new();
|
|
let mut r = Reader::new(data);
|
|
|
|
while let Some(b) = r.read_byte() {
|
|
if b == b'#' {
|
|
let hex = r.read_bytes(2)?;
|
|
result.push(decode_hex_digit(hex[0])? << 4 | decode_hex_digit(hex[1])?);
|
|
} else {
|
|
result.push(b);
|
|
}
|
|
}
|
|
|
|
Some(Self(NameInner::Owned(result)))
|
|
}
|
|
|
|
/// Return a string representation of the name.
|
|
///
|
|
/// Returns a placeholder in case the name is not UTF-8 encoded.
|
|
pub fn as_str(&self) -> &str {
|
|
core::str::from_utf8(self.as_ref()).unwrap_or("{non-ascii key}")
|
|
}
|
|
}
|
|
|
|
impl Debug for Name<'_> {
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
match core::str::from_utf8(self.as_ref()) {
|
|
Ok(s) => <str as Debug>::fmt(s, f),
|
|
Err(_) => <[u8] as Debug>::fmt(self.as_ref(), f),
|
|
}
|
|
}
|
|
}
|
|
|
|
object!(Name<'a>, Name);
|
|
|
|
impl Skippable for Name<'_> {
|
|
fn skip(r: &mut Reader<'_>, _: bool) -> Option<()> {
|
|
skip_name_like(r, true).map(|_| ())
|
|
}
|
|
}
|
|
|
|
impl<'a> Readable<'a> for Name<'a> {
|
|
fn read(r: &mut Reader<'a>, _: &ReaderContext<'a>) -> Option<Self> {
|
|
let start = r.offset();
|
|
skip_name_like(r, true)?;
|
|
let end = r.offset();
|
|
|
|
// Exclude leading solidus.
|
|
let data = r.range(start + 1..end)?;
|
|
Self::new(data)
|
|
}
|
|
}
|
|
|
|
// This method is shared by `Name` and the parser for content stream operators (which behave like
|
|
// names, except that they aren't preceded by a solidus.
|
|
pub(crate) fn skip_name_like(r: &mut Reader<'_>, solidus: bool) -> Option<()> {
|
|
// Note that we are not validating hex escape sequences here
|
|
// (since this method can lie on the hot path), so it's possible
|
|
// this method will yield invalid names. Validation needs to happen during actual
|
|
// actual reading.
|
|
if solidus {
|
|
r.forward_tag(b"/")?;
|
|
r.forward_while(is_regular_character);
|
|
} else {
|
|
r.forward_while_1(is_regular_character)?;
|
|
}
|
|
|
|
Some(())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use crate::object::Name;
|
|
use crate::reader::Reader;
|
|
use crate::reader::ReaderExt;
|
|
use std::ops::Deref;
|
|
|
|
#[test]
|
|
fn name_1() {
|
|
assert_eq!(
|
|
Reader::new("/".as_bytes())
|
|
.read_without_context::<Name<'_>>()
|
|
.unwrap()
|
|
.deref(),
|
|
b""
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn name_2() {
|
|
assert!(
|
|
Reader::new("dfg".as_bytes())
|
|
.read_without_context::<Name<'_>>()
|
|
.is_none()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn name_3() {
|
|
assert!(
|
|
Reader::new("/AB#FG".as_bytes())
|
|
.read_without_context::<Name<'_>>()
|
|
.is_none()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn name_4() {
|
|
assert_eq!(
|
|
Reader::new("/Name1".as_bytes())
|
|
.read_without_context::<Name<'_>>()
|
|
.unwrap()
|
|
.deref(),
|
|
b"Name1"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn name_5() {
|
|
assert_eq!(
|
|
Reader::new("/ASomewhatLongerName".as_bytes())
|
|
.read_without_context::<Name<'_>>()
|
|
.unwrap()
|
|
.deref(),
|
|
b"ASomewhatLongerName"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn name_6() {
|
|
assert_eq!(
|
|
Reader::new("/A;Name_With-Various***Characters?".as_bytes())
|
|
.read_without_context::<Name<'_>>()
|
|
.unwrap()
|
|
.deref(),
|
|
b"A;Name_With-Various***Characters?"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn name_7() {
|
|
assert_eq!(
|
|
Reader::new("/1.2".as_bytes())
|
|
.read_without_context::<Name<'_>>()
|
|
.unwrap()
|
|
.deref(),
|
|
b"1.2"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn name_8() {
|
|
assert_eq!(
|
|
Reader::new("/$$".as_bytes())
|
|
.read_without_context::<Name<'_>>()
|
|
.unwrap()
|
|
.deref(),
|
|
b"$$"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn name_9() {
|
|
assert_eq!(
|
|
Reader::new("/@pattern".as_bytes())
|
|
.read_without_context::<Name<'_>>()
|
|
.unwrap()
|
|
.deref(),
|
|
b"@pattern"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn name_10() {
|
|
assert_eq!(
|
|
Reader::new("/.notdef".as_bytes())
|
|
.read_without_context::<Name<'_>>()
|
|
.unwrap()
|
|
.deref(),
|
|
b".notdef"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn name_11() {
|
|
assert_eq!(
|
|
Reader::new("/lime#20Green".as_bytes())
|
|
.read_without_context::<Name<'_>>()
|
|
.unwrap()
|
|
.deref(),
|
|
b"lime Green"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn name_12() {
|
|
assert_eq!(
|
|
Reader::new("/paired#28#29parentheses".as_bytes())
|
|
.read_without_context::<Name<'_>>()
|
|
.unwrap()
|
|
.deref(),
|
|
b"paired()parentheses"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn name_13() {
|
|
assert_eq!(
|
|
Reader::new("/The_Key_of_F#23_Minor".as_bytes())
|
|
.read_without_context::<Name<'_>>()
|
|
.unwrap()
|
|
.deref(),
|
|
b"The_Key_of_F#_Minor"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn name_14() {
|
|
assert_eq!(
|
|
Reader::new("/A#42".as_bytes())
|
|
.read_without_context::<Name<'_>>()
|
|
.unwrap()
|
|
.deref(),
|
|
b"AB"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn name_15() {
|
|
assert_eq!(
|
|
Reader::new("/A#3b".as_bytes())
|
|
.read_without_context::<Name<'_>>()
|
|
.unwrap()
|
|
.deref(),
|
|
b"A;"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn name_16() {
|
|
assert_eq!(
|
|
Reader::new("/A#3B".as_bytes())
|
|
.read_without_context::<Name<'_>>()
|
|
.unwrap()
|
|
.deref(),
|
|
b"A;"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn name_17() {
|
|
assert_eq!(
|
|
Reader::new("/k1 ".as_bytes())
|
|
.read_without_context::<Name<'_>>()
|
|
.unwrap()
|
|
.deref(),
|
|
b"k1"
|
|
);
|
|
}
|
|
}
|