mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-09 04:22:20 +00:00
Merge #6348: fix(render): a ToUnicode CMap reads its mappings past a token it can't parse (hayro-cmap patch 3)
# Conflicts: # crates/render/src/raster/tests.rs # vendor/README.md
This commit is contained in:
commit
d5ea69db50
5 files changed
+206
-6
No files matched your search
@@ -10229,3 +10229,40 @@ fn a_content_stream_without_endstream_is_drawn_to_its_length() {
|
||||
assert_eq!(centre(end), [255, 0, 0, 255], "{end:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A ToUnicode CMap with a token the scanner can't read before its mappings (a stray `>` after
|
||||
/// a CMap name that holds one, as in pdf.js `issue11651.pdf`; a CMap name with spaces in it, as
|
||||
/// in `issue7492.pdf`) still maps its codes (vendored hayro-cmap patch 3, hayro-postscript
|
||||
/// patch 2): the scan stopped at that token, so the page's text could not be copied or found.
|
||||
#[test]
|
||||
fn a_to_unicode_cmap_reads_its_mappings_past_a_token_it_cannot_parse() {
|
||||
use hayro::hayro_interpret::hayro_cmap::{BfString, CMap};
|
||||
let body = "1 begincodespacerange\n<0000> <FFFF>\nendcodespacerange\n2 beginbfchar\n<002B> <0048>\n<0048> <0065>\nendbfchar\n1 beginbfrange\n<0050> <0052> <0061>\nendbfrange\nendcmap\n";
|
||||
let stray = format!(
|
||||
"/CIDInit /ProcSet findresource begin 12 dict begin begincmap\n/CIDSystemInfo <</Registry (Adobe) /Ordering (UC>) /Supplement 0>> def\n/CMapName /Adobe-Identity-UC> def\n/CMapType 2 def\n{body}"
|
||||
);
|
||||
let spaced = format!(
|
||||
"/CIDInit /ProcSet findresource begin 12 dict begin begincmap\n/CMapName /-usr-share-fonts-Panton Family-Fontfabric - Panton.otf,000-UTF16 def\n/CMapType 2 def\n{body}"
|
||||
);
|
||||
for (what, data) in [("a stray >", stray), ("a name with spaces", spaced)] {
|
||||
let cmap = CMap::parse(data.as_bytes(), |_| None).unwrap_or_else(|| panic!("{what}: parsed"));
|
||||
assert_eq!(cmap.lookup_bf_string(0x2B), Some(BfString::Char('H')), "{what}");
|
||||
assert_eq!(cmap.lookup_bf_string(0x48), Some(BfString::Char('e')), "{what}");
|
||||
assert_eq!(cmap.lookup_bf_string(0x51), Some(BfString::Char('b')), "{what}: bfrange");
|
||||
}
|
||||
// Unreadable tokens are stepped over a bounded number of times (1,024 per CMap): a long run
|
||||
// of them ends the scan, and the mappings read before it are kept.
|
||||
let flood = format!("{body}{}", "-".repeat(1 << 16));
|
||||
assert!(CMap::parse(flood.as_bytes(), |_| None).is_some_and(|c| c.lookup_bf_string(0x2B).is_some()));
|
||||
// A run past the limit before the mappings ends the scan before them, and a CMap that read
|
||||
// no mappings, like one of unreadable tokens only, reports failure. (A run of `-` with
|
||||
// nothing between them is one token, as in PostScript, so these are spaced.)
|
||||
let flood_first = format!("{}\n{body}", "- ".repeat(1025));
|
||||
assert!(CMap::parse(flood_first.as_bytes(), |_| None).is_none());
|
||||
assert!(CMap::parse("-".repeat(1 << 16).as_bytes(), |_| None).is_none());
|
||||
// Unclosed strings after the mappings: the scan ends and keeps them. (That the first one,
|
||||
// which reads to the end of the data, is not read again for each `(` inside it is
|
||||
// hayro-postscript's `parse_object_or_skip_does_not_read_a_failed_read_again`.)
|
||||
let unclosed = format!("{body}{}{}", "(".repeat(4096), "x".repeat(1 << 16));
|
||||
assert!(CMap::parse(unclosed.as_bytes(), |_| None).is_some_and(|c| c.lookup_bf_string(0x48).is_some()));
|
||||
}
|
||||
Vendored
+4
-4
@@ -47,13 +47,13 @@ Other resource categories retain their existing parent-chain lookup.
|
||||
| winit | 0.30.13 | Apache-2.0 | (1) on Windows 11 (build 22000 and later), `WM_DPICHANGED` applies the rect Windows suggests. It used to nudge the window onto the monitor `MonitorFromWindow` names, which during a drag is still the monitor being left; the nudge sent another `WM_DPICHANGED`, so a window dragged to a monitor with another scale factor grew and stayed on the wrong monitor at the wrong scale (#324). Backport of winit master 488c036a ([winit#4341](https://github.com/rust-windowing/winit/pull/4341)), which no 0.30 release has; Windows 10 keeps the old adjustment, as on winit master. (2) on Wayland, a seat's text input is destroyed when the seat goes away, not whenever it loses a capability: losing any capability (a touch screen or tablet unplugged, a remote-desktop session ending) destroyed it, and only an added capability recreated it, so IME stopped working until restart. Port of [winit#4747](https://github.com/rust-windowing/winit/pull/4747); (3) on Windows, an IME composition reports its cursor from `GCS_CURSORPOS` only when `WM_IME_COMPOSITION` says it is set, and at the end of the composition otherwise: the Microsoft Korean IME never sets it, so the cursor read as 0 (egui then drew no caret while composing; it now draws it after the syllable). Port of [winit#4746](https://github.com/rust-windowing/winit/pull/4746); (4) on Windows, a hidden window is not maximized until it is shown, and a hidden window shown while maximized gets one `ShowWindow(SW_MAXIMIZE)`: `SW_MAXIMIZE` shows a hidden window, so PdfCraft, which eframe keeps hidden until its first frame is drawn, appeared unpainted (white) when it restored a maximized window, and could lose its focus. Port of [winit#4587](https://github.com/rust-windowing/winit/pull/4587) (the later, focus-safe form of [winit#4575](https://github.com/rust-windowing/winit/pull/4575)); (5) on X11, when allowing or disallowing IME replaces the focused window's input context (egui does it each time a text field gains or loses focus), the new context is focused: no focus event follows, so it stayed unfocused until the window lost and regained focus. Port of [winit#4727](https://github.com/rust-windowing/winit/pull/4727); (6) on macOS, `insertText` commits through IME when a composition was started (`pending_commit`, set by `setMarkedText`) rather than when marked text is left: input methods that end a composition with `unmarkText` before `insertText` lost the text, and Apple Korean dropped the key typed after a syllable (`한` then `5` or `;`); other `insertText` calls stay `KeyboardInput`; a commit outside a key press (a candidate clicked with the mouse, dictation) clears the marked text and leaves no key to swallow, as in [winit#4748](https://github.com/rust-windowing/winit/pull/4748) (without it the next key, such as Backspace, was lost). `validAttributesForMarkedText` lists the underline and marked-clause attributes, without which some input methods don't compose. Port of [winit#4650](https://github.com/rust-windowing/winit/pull/4650), the 0.30 backport of [winit#4651](https://github.com/rust-windowing/winit/pull/4651) (merged on winit master), with the underline attribute spelled as AppKit's `NSUnderlineStyleAttributeName` (`NSUnderline`); (7) on macOS, disallowing IME with a composition in progress (focus leaving a text field) tells the input method to discard its marked text (`discardMarkedText`): it kept composing it once IME was allowed again, so the abandoned syllable came back (`하`, then `가` typed in the next field, became `학`, then `하` + `가`; Japanese `か` then `さ` became `傘`). Port of [winit#4745](https://github.com/rust-windowing/winit/pull/4745); (8) on macOS, text inserted outside a key press without a composition (the emoji picker) is reported as `Ime::Commit`; it was dropped. Port of the emoji-picker part of [winit#4749](https://github.com/rust-windowing/winit/pull/4749) (0.30 backport of [winit#4748](https://github.com/rust-windowing/winit/pull/4748)); its `selectedRange` change for dictation is left out because it switches on press-and-hold for every held key (an accent menu instead of key repeat, and the chosen accent added after the repeated letters); (9) on macOS, a key whose text was committed through IME isn't also sent as `KeyboardInput` unless AppKit forwards it as a command (Enter): Apple Korean answers Space with no syllable in progress with `setMarkedText(" ")`, `insertText(" ")`, `setMarkedText("")`, and the last call reset winit's state, so the space arrived twice. Same goal as [winit#4478](https://github.com/rust-windowing/winit/pull/4478), whose changes don't reach this call order (macOS 14); to be proposed upstream there; (10) on macOS, when the first key in a fresh app reaches Apple Korean's `insertText` as a raw compatibility jamo instead of starting a composition, that key is interpreted once more, synchronously, before it is sent as `KeyboardInput` (g k s typed ㅎㅏㄴ instead of 한); only an unmodified, Line truncated
|
||||
| egui-wgpu | 0.36.2 | MIT OR Apache-2.0 | (1) `winit::surface_fit`: a window's surface is configured within the device's `max_texture_dimension_2d`, both sides shrunk by one factor that egui then draws at (`pixels_per_point × render_scale`), so the whole window is drawn into the surface (on DX12 it is stretched over the window and lines up with the pointer; wgpu's GL backend copies it unscaled into a corner); the longer side becomes exactly the limit; the depth and MSAA textures follow, and a recreated surface starts again from the window's own size. egui-wgpu configured the window's size as is, and `Surface::configure` panics beyond the limit: a window restored at another monitor's scale (opening an Outlook attachment on a mixed-DPI laptop asked an 8192-pixel device for 8600 × 3423, #577) or stretched across monitors closed PdfCraft, before its window appeared when it happened at start-up ([emilk/egui#8361](https://github.com/emilk/egui/issues/8361)). The published crate leaves out egui's licence files; `LICENSE-APACHE` and `LICENSE-MIT` are from the 0.36.2 tag; (2) `winit::catch_errors`: a new window's surface is configured inside wgpu error scopes (validation, out of memory, internal), and a failure is returned as `WgpuError::ConfigureSurface` (for the root window at start-up eframe returns it as `Error::Wgpu`, which PdfCraft retries with OpenGL); nothing is kept for the window. wgpu's default handler panicked instead, and eframe configures the root surface before the app is created, so a driver that can't configure one closed PdfCraft at launch (a 2015 Intel GPU through wgpu's GL backend timed out waiting for the GPU to go idle, `GpuWaitTimeout`, #519). Unchanged, so still a panic: an error in a later reconfigure (resize, a lost surface), a device lost during the first configure (wgpu reports it only to a callback), and a window shown at zero size (first configured on resize) | (1) `pdfcraft` `tests::a_window_larger_than_the_gpu_limit_gets_a_surface_within_it`, `tests::surface_fit_never_empties_or_overflows_a_surface`, and `tests::vendor_provenance_egui_wgpu_surface_fit_identity`, which keeps the patch in the build and in the paths that size a surface; (2) `pdfcraft` `tests::a_gpu_error_while_a_window_is_set_up_is_returned_not_a_panic` (a real device: an error inside `catch_errors` is returned, not a panic) and `tests::vendor_provenance_egui_wgpu_catch_errors_identity`, which keeps it around `add_surface`'s configure |
|
||||
| vello_common | 0.0.8 | Apache-2.0 OR MIT | (1) `clip::intersect` (the clip step every clipped path passes through) starts its row walk at the first row both paths have strips in, not the earlier of the two first rows. The walk visited every row from the top of the clip for each path (on the A1 sample's full-page canvas, about half its strip rows per stroke), and rows where one path has no strips produce no output, so the strips are unchanged (vello_common 0.1 starts at the later row too). Its cost grew with the page, and a full-page render of the A1 sample was about 30% faster; the output is byte-identical; (4) `flatten::stroke`: when kurbo 0.13.1's stroker returns an outline containing NaN, which `fill` would drop whole, the path is stroked again from a copy (`robust_for_stroking`, one element out per element in) in which each curve whose control points lie within the stroke tolerance of its chord is a line (the #4907 fix, a nearly straight cubic given a NaN join, kurbo issues #617 and #519; its row here was lost in a merge and is restored with this entry) and each cubic whose second control point is its end point, as `y` builds (ISO 32000-2 §8.5.2.1 Table 58), has that control point moved the tolerance towards its first (`open_zero_end_handle`; the curve moves by at most 4/9 of the tolerance): kurbo's cusp search on such a curve's offset can stop on the end point, where the derivative is zero, and normalising it gives NaN, so at 16, 32 and 63 pt a two-curve `y` path drew nothing and a file in the PDFBox tracker lost its 32 pt "XX" outline. Only an exact zero triggers it (none of 192,000 random strokes with a handle 1e-9 long, nor of 256,000 `v` strokes, came back with NaN; 1,073 of 192,000 random `y`/`v` strokes at 1–64 pt did before and none after). A finite outline takes the old path unchanged | (1) `clip::tests::intersect_walks_only_rows_both_paths_have_strips` counts the rows the walk visits (`cargo test --manifest-path vendor/vello_common/Cargo.toml`) and fails without the patch; the render crate's pixel tests run through the patched crate; (4) `flatten::tests::a_tiny_degenerate_cubic_does_not_drop_the_stroke` and the render crate's `stroke_degenerate_cubic` fail without the near-line replacement, `flatten::tests::a_wide_stroke_of_a_curve_with_a_zero_end_tangent_is_drawn` (widths 1–64, every join and cap, `y` and the mirrored `v`) and the render crate's `stroke_zero_end_tangent` fail without the moved control point; `a_finite_outline_is_stroked_as_before` and `a_path_with_nan_is_still_dropped` pass either way |
|
||||
| hayro-cmap | 0.1.0 | Apache-2.0 OR MIT | (1) malformed parts of a CMap (bad bfchar/bfrange entries, unresolvable `usecmap`, unparseable tail) are skipped instead of discarding the whole map, so search and redaction find text the edit layer can read (#3177); (2) a binary CMap's `usecmap` counts against the 16-level nesting limit as a text one does, where it passed its own depth and a self-referencing binary CMap recursed until the stack overflowed (from storytold/craft-libs#254) | (1) `hayro-cmap::tests::bad_bfchar_entry_is_skipped`, `unresolvable_usecmap_is_skipped`; (2) `tests::self_referencing_binary_usecmap_stops_at_the_nesting_limit` |
|
||||
| hayro-postscript | 0.1.0 | Apache-2.0 OR MIT | (1) the token scanner's `object::read` skips `<<` and `>>` in a loop; it called itself once per delimiter, so a CMap stream holding a long run of `<` or `>` overflowed the stack and aborted the process (from storytold/craft-libs#263) | (1) `hayro-postscript::object::tests::long_open_dict_run_does_not_overflow`, `long_close_dict_run_does_not_overflow`, `dict_delimiters_are_skipped`, `nested_dict_delimiters_are_skipped` |
|
||||
| hayro-cmap | 0.1.0 | Apache-2.0 OR MIT | (1) malformed parts of a CMap (bad bfchar/bfrange entries, unresolvable `usecmap`, unparseable tail) are skipped instead of discarding the whole map, so search and redaction find text the edit layer can read (#3177); (2) a binary CMap's `usecmap` counts against the 16-level nesting limit as a text one does, where it passed its own depth and a self-referencing binary CMap recursed until the stack overflowed (from storytold/craft-libs#254); (3) a token the scanner can't read (a stray `>` after a CMap name, a CMap name with spaces in it) is stepped over with `parse_object_or_skip`, at most 1,024 times per CMap, instead of ending the scan before the mappings that follow it (pdf.js `issue11651.pdf`, `issue7492.pdf`: no text could be copied) | (1) `hayro-cmap::tests::bad_bfchar_entry_is_skipped`, `unresolvable_usecmap_is_skipped`; (2) `tests::self_referencing_binary_usecmap_stops_at_the_nesting_limit`; (3) `hayro-cmap::tests::{mappings_after_an_unreadable_token_are_read, a_number_run_into_an_operator_does_not_start_its_mappings, the_scan_steps_over_at_most_max_skipped_unreadable_tokens, a_cmap_of_unreadable_tokens_only_reports_failure}`, `raster::tests::a_to_unicode_cmap_reads_its_mappings_past_a_token_it_cannot_parse` (pdfcraft-render) |
|
||||
| hayro-postscript | 0.1.0 | Apache-2.0 OR MIT | (1) the token scanner's `object::read` skips `<<` and `>>` in a loop; it called itself once per delimiter, so a CMap stream holding a long run of `<` or `>` overflowed the stack and aborted the process (from storytold/craft-libs#263); (2) `Scanner::parse_object_or_skip`: after an object that can't be read, the scanner reads on from where the failed read stopped, or from one byte past where its token starts (after whitespace and comments) when it read nothing, so a caller can read on, one failure per unreadable token, and the data is read about once; a number that can't be read goes to the end of its token, so `1beginbfchar` is not read as `beginbfchar` (hayro-cmap patch 3) | (1) `hayro-postscript::object::tests::long_open_dict_run_does_not_overflow`, `long_close_dict_run_does_not_overflow`, `dict_delimiters_are_skipped`, `nested_dict_delimiters_are_skipped`; (2) `hayro-postscript::tests::{parse_object_or_skip_steps_over_an_unreadable_token, parse_object_or_skip_skips_the_rest_of_an_unreadable_number, parse_object_or_skip_does_not_read_a_failed_read_again, parse_object_or_skip_stays_at_the_end}`, `raster::tests::a_to_unicode_cmap_reads_its_mappings_past_a_token_it_cannot_parse` (pdfcraft-render) |
|
||||
|
||||
`hayro-postscript` is the crates.io 0.1.0 package plus patch (1). The local Cargo
|
||||
`hayro-postscript` is the crates.io 0.1.0 package plus patches (1) and (2). The local Cargo
|
||||
registry cache metadata for that package (`.cargo_vcs_info.json`) records upstream
|
||||
VCS commit `a2354aebaf86f5ab13e5fd6b6b0521306488bf9d`. The registry copy and this vendored tree's
|
||||
source files differ only in `src/object.rs` (the patch above); the vendored tree
|
||||
source files differ only in `src/object.rs` (patch 1) and `src/lib.rs` (patch 2); the vendored tree
|
||||
omits the registry dotfiles `.cargo_vcs_info.json` and `.cargo-ok`, so the upstream
|
||||
commit is recorded here.
|
||||
|
||||
|
||||
Vendored
+54
@@ -1176,6 +1176,60 @@ endbfchar
|
||||
assert!(cmap.lookup_bf_string(0x41).is_some());
|
||||
}
|
||||
|
||||
// PdfCraft patch (3): a token the scanner can't read (a stray `>` after a CMap name, a name
|
||||
// with spaces in it) is stepped over, so the mappings after it are still read; at most
|
||||
// `MAX_SKIPPED` of them per CMap.
|
||||
|
||||
#[test]
|
||||
fn mappings_after_an_unreadable_token_are_read() {
|
||||
for header in [
|
||||
&b"/CMapName /Adobe-Identity-UC> def"[..],
|
||||
b"/CMapName /-usr-share-fonts-Panton Family - Panton.otf,000-UTF16 def",
|
||||
] {
|
||||
let mut data = header.to_vec();
|
||||
data.extend_from_slice(
|
||||
b"\n1 beginbfchar <41> <0058> endbfchar\n1 beginbfrange <50> <52> <0061> endbfrange",
|
||||
);
|
||||
let cmap = parse_with_preamble(&data);
|
||||
assert_eq!(cmap.lookup_bf_string(0x41), Some(BfString::Char('X')));
|
||||
assert_eq!(cmap.lookup_bf_string(0x51), Some(BfString::Char('b')));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_number_run_into_an_operator_does_not_start_its_mappings() {
|
||||
// `1beginbfchar` is one token, not `1` and `beginbfchar`: the pairs after it are not
|
||||
// mappings, and the well-formed block after them still reads.
|
||||
let cmap = parse_with_preamble(
|
||||
b"1beginbfchar <41> <0058> endbfchar\n1 beginbfchar <42> <0059> endbfchar",
|
||||
);
|
||||
assert_eq!(cmap.lookup_bf_string(0x41), None);
|
||||
assert_eq!(cmap.lookup_bf_string(0x42), Some(BfString::Char('Y')));
|
||||
}
|
||||
|
||||
/// A CMap with `count` unreadable tokens, each after a space, before one `bfchar` mapping.
|
||||
fn mapping_after_unreadable_tokens(count: usize) -> Option<CMap> {
|
||||
let mut data = PREAMBLE.to_vec();
|
||||
data.extend_from_slice(&b" >".repeat(count));
|
||||
data.extend_from_slice(b"\n1 beginbfchar <41> <0058> endbfchar");
|
||||
CMap::parse(&data, |_| None)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_scan_steps_over_at_most_max_skipped_unreadable_tokens() {
|
||||
let limit = parse::MAX_SKIPPED;
|
||||
let cmap = mapping_after_unreadable_tokens(limit).expect("read up to the limit");
|
||||
assert_eq!(cmap.lookup_bf_string(0x41), Some(BfString::Char('X')));
|
||||
// One more ends the scan before the mapping, and a CMap with no mappings fails.
|
||||
assert!(mapping_after_unreadable_tokens(limit + 1).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_cmap_of_unreadable_tokens_only_reports_failure() {
|
||||
assert!(CMap::parse(b"> ) >", |_| None).is_none());
|
||||
assert!(CMap::parse(&b" >".repeat(4 * parse::MAX_SKIPPED), |_| None).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_bfchar_code_keeps_later_entries_aligned() {
|
||||
// The malformed 5-byte code still has a destination object; both
|
||||
|
||||
Vendored
+15
-2
@@ -15,6 +15,9 @@ struct Context<F> {
|
||||
get_cmap: F,
|
||||
}
|
||||
|
||||
/// PdfCraft patch (3): most unreadable tokens one CMap may step over before its scan stops.
|
||||
pub(crate) const MAX_SKIPPED: usize = 1024;
|
||||
|
||||
pub(crate) fn parse_inner<'a>(
|
||||
data: &[u8],
|
||||
get_cmap: impl Fn(CMapName<'_>) -> Option<&'a [u8]> + Clone + 'a,
|
||||
@@ -61,10 +64,20 @@ pub(crate) fn parse_inner<'a>(
|
||||
// must see failure rather than an empty map, so its encoding fallback
|
||||
// paths run exactly as they did before the tolerant-usecmap change.
|
||||
let mut usecmap_failed = false;
|
||||
let mut skipped = 0usize;
|
||||
while !scanner.at_end() {
|
||||
let Ok(obj) = scanner.parse_object() else {
|
||||
// PdfCraft patch (3): a token that can't be read (a stray `>` after a CMap name, a
|
||||
// name with spaces in it, both written into ToUnicode CMaps) is stepped over, so the
|
||||
// mappings after it are still read; it used to end the scan before any of them.
|
||||
// At most `MAX_SKIPPED` such tokens: a failed read may scan to the end of the data (an
|
||||
// unclosed string), so a stream of them would cost its length squared.
|
||||
let Ok(obj) = scanner.parse_object_or_skip() else {
|
||||
truncated = true;
|
||||
break;
|
||||
skipped += 1;
|
||||
if skipped > MAX_SKIPPED {
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
};
|
||||
|
||||
let Object::Name(name) = &obj else { continue };
|
||||
|
||||
Vendored
+96
@@ -60,6 +60,37 @@ impl<'a> Scanner<'a> {
|
||||
object::read(&mut self.reader)
|
||||
}
|
||||
|
||||
/// PdfCraft patch (2): parse the next object; when it can't be read, read on from where
|
||||
/// the failed read stopped, or, when it read nothing, from one byte past where its token
|
||||
/// starts, so a caller can read on past a token it can't parse (a stray `>` in a name, a
|
||||
/// CMap name with spaces in it) instead of stopping there. Every failure moves forward, one
|
||||
/// failure per unreadable token, and what a failed read went through (an unclosed string,
|
||||
/// to the end of the data) is not read again, so a loop over this ends and reads the data
|
||||
/// about once. A number that can't be read is skipped to the end of its token: its tail
|
||||
/// (`beginbfchar` in `1beginbfchar`) is not read as a token of its own.
|
||||
pub fn parse_object_or_skip(&mut self) -> Result<Object<'a>> {
|
||||
// The whitespace and comments before the token are not the token: skipped first, they
|
||||
// don't count as progress, so a token after a space fails once, not twice.
|
||||
object::skip_whitespace_and_comments(&mut self.reader);
|
||||
let start = self.reader.offset();
|
||||
// `object::read` reads a token starting with one of these bytes as a number.
|
||||
let number = matches!(
|
||||
self.reader.peek_byte(),
|
||||
Some(b'.' | b'+' | b'-' | b'0'..=b'9')
|
||||
);
|
||||
let read = object::read(&mut self.reader);
|
||||
// A number read stops at the first byte that can't be part of a number, which may be
|
||||
// inside the token; the rest of the token goes with it.
|
||||
if read.is_err() && number {
|
||||
self.reader.forward_while(reader::is_regular);
|
||||
}
|
||||
if read.is_err() && self.reader.offset() <= start {
|
||||
self.reader
|
||||
.jump(start.saturating_add(1).min(self.reader.data.len()));
|
||||
}
|
||||
read
|
||||
}
|
||||
|
||||
/// Parse the next object as a [`Number`].
|
||||
pub fn parse_number(&mut self) -> Result<Number> {
|
||||
match self.parse_object()? {
|
||||
@@ -176,4 +207,69 @@ endcmap"#;
|
||||
let mut scanner = Scanner::new(b"42 ");
|
||||
assert!(scanner.parse_name().is_err());
|
||||
}
|
||||
|
||||
/// PdfCraft patch (2) test: a token that can't be read fails once and is stepped over,
|
||||
/// whether or not whitespace or a comment comes before it, and the objects after it read.
|
||||
#[test]
|
||||
fn parse_object_or_skip_steps_over_an_unreadable_token() {
|
||||
let mut s = Scanner::new(b"/A> /B % note\n > /C");
|
||||
assert_eq!(
|
||||
s.parse_object_or_skip(),
|
||||
Ok(Object::Name(Name::new(b"A", true)))
|
||||
);
|
||||
assert!(s.parse_object_or_skip().is_err());
|
||||
assert_eq!(
|
||||
s.parse_object_or_skip(),
|
||||
Ok(Object::Name(Name::new(b"B", true)))
|
||||
);
|
||||
assert!(s.parse_object_or_skip().is_err());
|
||||
assert_eq!(
|
||||
s.parse_object_or_skip(),
|
||||
Ok(Object::Name(Name::new(b"C", true)))
|
||||
);
|
||||
assert!(s.at_end());
|
||||
}
|
||||
|
||||
/// PdfCraft patch (2) test: a number that can't be read fails once with the rest of its
|
||||
/// token, which is not read as a name of its own; the tokens after it still read.
|
||||
#[test]
|
||||
fn parse_object_or_skip_skips_the_rest_of_an_unreadable_number() {
|
||||
let mut s = Scanner::new(b"1beginbfchar 2.5.3x 16#zz! -x /C");
|
||||
for _ in 0..4 {
|
||||
assert!(s.parse_object_or_skip().is_err());
|
||||
}
|
||||
assert_eq!(
|
||||
s.parse_object_or_skip(),
|
||||
Ok(Object::Name(Name::new(b"C", true)))
|
||||
);
|
||||
assert!(s.at_end());
|
||||
}
|
||||
|
||||
/// PdfCraft patch (2) test: what a failed read went through is not read again. An unclosed
|
||||
/// string reads to the end of the data, so one failure leaves nothing to read; the nested
|
||||
/// `(` inside it are not each read to the end once more.
|
||||
#[test]
|
||||
fn parse_object_or_skip_does_not_read_a_failed_read_again() {
|
||||
let mut s = Scanner::new(b"/A (((( x");
|
||||
assert_eq!(
|
||||
s.parse_object_or_skip(),
|
||||
Ok(Object::Name(Name::new(b"A", true)))
|
||||
);
|
||||
assert!(s.parse_object_or_skip().is_err());
|
||||
assert!(s.at_end());
|
||||
}
|
||||
|
||||
/// PdfCraft patch (2) test: at the end of the data a read fails and the scanner stays at
|
||||
/// the end; it never steps past the data.
|
||||
#[test]
|
||||
fn parse_object_or_skip_stays_at_the_end() {
|
||||
for input in [&b""[..], b" ", b">"] {
|
||||
let mut s = Scanner::new(input);
|
||||
for _ in 0..3 {
|
||||
assert!(s.parse_object_or_skip().is_err());
|
||||
assert!(s.reader.offset() <= input.len());
|
||||
}
|
||||
assert!(s.at_end());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user