pdf-skryba 0.9.1 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1401,6 +1401,58 @@ var PdfObjectsRefusal = class extends ReaderError {
1401
1401
  objectNumber;
1402
1402
  offset;
1403
1403
  };
1404
+ var REFUSAL_IS_BOUND = {
1405
+ syntax: false,
1406
+ truncated: false,
1407
+ "nesting-too-deep": true,
1408
+ "token-too-long": true,
1409
+ "string-too-long": true,
1410
+ "stream-length": false,
1411
+ "stream-end": false,
1412
+ "stream-too-large": true,
1413
+ "length-unresolved": false,
1414
+ "startxref-missing": false,
1415
+ "xref-offset": false,
1416
+ "xref-syntax": false,
1417
+ "xref-chain": false,
1418
+ "xref-too-large": true,
1419
+ "filter-unsupported": false,
1420
+ predictor: false,
1421
+ "stream-decode": false,
1422
+ "object-stream": false,
1423
+ "xref-stream": false,
1424
+ "xref-stale": false,
1425
+ "ref-hops": true,
1426
+ "page-tree": false,
1427
+ "page-box": false,
1428
+ "content-syntax": false,
1429
+ "content-operands": false,
1430
+ "content-too-long": true,
1431
+ "inline-image": false,
1432
+ encrypted: false,
1433
+ "to-unicode": false,
1434
+ "value-too-large": true,
1435
+ "document-too-large": true,
1436
+ "glyphs-too-many": true,
1437
+ "forms-too-many": true,
1438
+ "font-tables-too-many": true,
1439
+ "page-missing": false
1440
+ };
1441
+ function isSecurityRefusal(error) {
1442
+ if (error instanceof PdfObjectsRefusal) return REFUSAL_IS_BOUND[error.reason];
1443
+ return error instanceof ReaderError && error.code === "FILE_UNSAFE";
1444
+ }
1445
+ function rethrowSecurityRefusal(error) {
1446
+ if (isSecurityRefusal(error)) throw error;
1447
+ }
1448
+ function partOrAbsent(read) {
1449
+ try {
1450
+ return read();
1451
+ } catch (error) {
1452
+ if (error instanceof PdfObjectsRefusal && !isSecurityRefusal(error)) return void 0;
1453
+ throw error;
1454
+ }
1455
+ }
1404
1456
 
1405
1457
  // src/extractor/pdf-objects/parse.ts
1406
1458
  var MAX_OBJECT_NUMBER = 8388607;
@@ -1795,8 +1847,9 @@ import { inflateSync } from "node:zlib";
1795
1847
  import { unfilterPngRows } from "doc-ir/internal";
1796
1848
  var FLATE = "FlateDecode";
1797
1849
  var LZW = "LZWDecode";
1850
+ var takesPredictor = (filter) => filter === FLATE || filter === "Fl" || filter === LZW || filter === "LZW";
1798
1851
  function grownTo(buffer, length, needed) {
1799
- if (length + needed > MAX_DECODED_STREAM_BYTES) throw new PdfObjectsRefusal("stream-decode");
1852
+ if (length + needed > MAX_DECODED_STREAM_BYTES) throw new PdfObjectsRefusal("stream-too-large");
1800
1853
  if (length + needed <= buffer.length) return buffer;
1801
1854
  let next = buffer.length;
1802
1855
  while (next < length + needed) next *= 2;
@@ -2011,7 +2064,8 @@ var INFLATE_CHUNK_BYTES = 64 * 1024;
2011
2064
  function inflate(raw) {
2012
2065
  try {
2013
2066
  return inflateSync(raw, { maxOutputLength: MAX_DECODED_STREAM_BYTES, chunkSize: INFLATE_CHUNK_BYTES });
2014
- } catch {
2067
+ } catch (error) {
2068
+ if (error.code === "ERR_BUFFER_TOO_LARGE") throw new PdfObjectsRefusal("stream-too-large");
2015
2069
  throw new PdfObjectsRefusal("stream-decode");
2016
2070
  }
2017
2071
  }
@@ -2037,6 +2091,7 @@ var FILTERS = {
2037
2091
  [FLATE]: (data, parms) => unpredict(inflate(data), parms),
2038
2092
  Fl: (data, parms) => unpredict(inflate(data), parms),
2039
2093
  [LZW]: (data, parms) => unpredict(lzwDecode(data, earlyChangeOf(parms)), parms),
2094
+ LZW: (data, parms) => unpredict(lzwDecode(data, earlyChangeOf(parms)), parms),
2040
2095
  ASCIIHexDecode: (data) => asciiHexDecode(data),
2041
2096
  AHx: (data) => asciiHexDecode(data),
2042
2097
  ASCII85Decode: (data) => ascii85Decode(data),
@@ -2047,7 +2102,7 @@ var FILTERS = {
2047
2102
  var decodeStream = (bytes, stream) => {
2048
2103
  const filters = filtersOf(stream.dict);
2049
2104
  if (filters.length > MAX_FILTER_CHAIN) throw new PdfObjectsRefusal("filter-unsupported");
2050
- const predicted = filters.findIndex((filter) => filter === FLATE || filter === LZW);
2105
+ const predicted = filters.findIndex(takesPredictor);
2051
2106
  let data = bytes.subarray(stream.start, stream.start + stream.length);
2052
2107
  for (const [index, filter] of filters.entries()) {
2053
2108
  const decode = Object.hasOwn(FILTERS, filter) ? FILTERS[filter] : void 0;
@@ -2077,7 +2132,7 @@ var chainRefusal = (dict) => {
2077
2132
  var decodeToImageFilter = (data, dict) => {
2078
2133
  const filters = filtersOf(dict);
2079
2134
  if (filters.length > MAX_FILTER_CHAIN) throw new PdfObjectsRefusal("filter-unsupported");
2080
- const predicted = filters.findIndex((filter) => filter === FLATE || filter === LZW);
2135
+ const predicted = filters.findIndex(takesPredictor);
2081
2136
  let out = data;
2082
2137
  for (const [index, filter] of filters.entries()) {
2083
2138
  if (IMAGE_FILTERS.has(filter)) {
@@ -2095,7 +2150,8 @@ function integerAt(data, from) {
2095
2150
  let parsed2;
2096
2151
  try {
2097
2152
  parsed2 = parseValue(data, from);
2098
- } catch {
2153
+ } catch (error) {
2154
+ if (!(error instanceof PdfObjectsRefusal)) throw error;
2099
2155
  throw new PdfObjectsRefusal("object-stream");
2100
2156
  }
2101
2157
  if (parsed2.value.kind !== "number" || !Number.isInteger(parsed2.value.value) || parsed2.value.value < 0) {
@@ -2145,7 +2201,8 @@ function readObjectStream(bytes, stream, decode) {
2145
2201
  try {
2146
2202
  const parsed2 = parseValue(data, start);
2147
2203
  return { value: parsed2.value, nodes: parsed2.nodes };
2148
- } catch {
2204
+ } catch (error) {
2205
+ if (!(error instanceof PdfObjectsRefusal)) throw error;
2149
2206
  throw new PdfObjectsRefusal("object-stream", num);
2150
2207
  }
2151
2208
  }
@@ -2272,7 +2329,8 @@ function readStreamSection(bytes, offset, spend) {
2272
2329
  let object;
2273
2330
  try {
2274
2331
  object = parseIndirectObject(bytes, offset, () => void 0);
2275
- } catch {
2332
+ } catch (error) {
2333
+ if (!(error instanceof PdfObjectsRefusal)) throw error;
2276
2334
  throw new PdfObjectsRefusal("xref-offset");
2277
2335
  }
2278
2336
  const stream = object.value;
@@ -2843,7 +2901,8 @@ function paletteBytesOf(doc, table) {
2843
2901
  if (table?.kind !== "stream") return void 0;
2844
2902
  try {
2845
2903
  return doc.streamBytes(table);
2846
- } catch {
2904
+ } catch (error) {
2905
+ rethrowSecurityRefusal(error);
2847
2906
  return void 0;
2848
2907
  }
2849
2908
  }
@@ -2951,7 +3010,8 @@ function flatePixels(doc, stream, shape, spent) {
2951
3010
  let contents;
2952
3011
  try {
2953
3012
  contents = doc.rawStreamBytes(stream);
2954
- } catch {
3013
+ } catch (error) {
3014
+ rethrowSecurityRefusal(error);
2955
3015
  return void 0;
2956
3016
  }
2957
3017
  const parms = doc.resolve(stream.dict.get("DecodeParms"));
@@ -3014,7 +3074,8 @@ async function openEmbeddedImages(source) {
3014
3074
  let stream;
3015
3075
  try {
3016
3076
  stream = doc.resolve({ kind: "ref", num: Number(m[1]), gen: m[2] ? Number(m[2]) : 0 });
3017
- } catch {
3077
+ } catch (error) {
3078
+ rethrowSecurityRefusal(error);
3018
3079
  return void 0;
3019
3080
  }
3020
3081
  if (stream?.kind !== "stream") return void 0;
@@ -3034,7 +3095,8 @@ async function openEmbeddedImages(source) {
3034
3095
  let raw;
3035
3096
  try {
3036
3097
  raw = doc.imageStreamOf(stream).data;
3037
- } catch {
3098
+ } catch (error) {
3099
+ rethrowSecurityRefusal(error);
3038
3100
  return void 0;
3039
3101
  }
3040
3102
  if (!(raw[0] === 255 && raw[1] === 216 && raw[2] === 255)) return void 0;
@@ -4949,7 +5011,8 @@ function maskSamplesOf(doc, mask, filter, size) {
4949
5011
  if (filter === "CCITTFaxDecode") return ccittSamplesOf(doc, mask, size.mw, size.mh);
4950
5012
  if (filter === "DCTDecode") return dctSamplesOf(doc, mask, size.mw, size.mh);
4951
5013
  return flateSamplesOf(doc, mask, size.mw, size.mh);
4952
- } catch {
5014
+ } catch (error) {
5015
+ rethrowSecurityRefusal(error);
4953
5016
  return void 0;
4954
5017
  }
4955
5018
  }
@@ -5040,7 +5103,8 @@ function stencilLayoutOf(doc, dict) {
5040
5103
  function samplesOf(doc, stream, width, height) {
5041
5104
  try {
5042
5105
  return unpackSamples(doc.streamBytes(stream), width, height, 1, 1);
5043
- } catch {
5106
+ } catch (error) {
5107
+ rethrowSecurityRefusal(error);
5044
5108
  return void 0;
5045
5109
  }
5046
5110
  }
@@ -5105,7 +5169,6 @@ function ccittStreamOf(doc, declared) {
5105
5169
  if (nameOf2(doc.resolve(dict.get("Subtype"))) !== "Image") return void 0;
5106
5170
  if (boolOf(doc.resolve(dict.get("ImageMask")))) return void 0;
5107
5171
  if (singleFilterOf(doc, dict) !== "CCITTFaxDecode") return void 0;
5108
- if (doc.trailer().has("Encrypt")) return void 0;
5109
5172
  return stream;
5110
5173
  }
5111
5174
  function repack(image, width, height, invert) {
@@ -5120,7 +5183,7 @@ function repack(image, width, height, invert) {
5120
5183
  }
5121
5184
  return data;
5122
5185
  }
5123
- function ccittImageObjectOf(doc, file, scope, name, grey1Kind) {
5186
+ function ccittImageObjectOf(doc, scope, name, grey1Kind) {
5124
5187
  const declared = doc.dictOf(scope.resources?.get("XObject"))?.get(name);
5125
5188
  const stream = ccittStreamOf(doc, declared);
5126
5189
  if (stream === void 0) return void 0;
@@ -5130,7 +5193,8 @@ function ccittImageObjectOf(doc, file, scope, name, grey1Kind) {
5130
5193
  if (width === void 0 || height === void 0 || !pictureSizeOk(width, height)) return void 0;
5131
5194
  const parms = doc.dictOf(doc.resolve(dict.get("DecodeParms")));
5132
5195
  const parm = (key) => doc.resolve(parms?.get(key));
5133
- const raw = file.subarray(stream.start, stream.start + stream.length);
5196
+ const raw = partOrAbsent(() => doc.rawStreamBytes(stream));
5197
+ if (raw === void 0) return void 0;
5134
5198
  const image = decodeCcittFax(raw, {
5135
5199
  K: numberOf2(parm("K")) ?? 0,
5136
5200
  EndOfLine: boolOf(parm("EndOfLine")),
@@ -5217,7 +5281,8 @@ function samplesOf2(doc, stream, layout) {
5217
5281
  let samples;
5218
5282
  try {
5219
5283
  samples = doc.streamBytes(stream);
5220
- } catch {
5284
+ } catch (error) {
5285
+ rethrowSecurityRefusal(error);
5221
5286
  return void 0;
5222
5287
  }
5223
5288
  return unpackSamples(samples, layout.width, layout.height, layout.components, layout.bpc);
@@ -5270,7 +5335,8 @@ function jpegDeclaredOf(doc, dict) {
5270
5335
  function rawBytesOf(doc, stream) {
5271
5336
  try {
5272
5337
  return doc.imageStreamOf(stream).data;
5273
- } catch {
5338
+ } catch (error) {
5339
+ rethrowSecurityRefusal(error);
5274
5340
  return void 0;
5275
5341
  }
5276
5342
  }
@@ -7605,7 +7671,7 @@ function walkForm(walking, operands, scope, depth, state) {
7605
7671
  const { doc } = walking;
7606
7672
  const { resources, xobjects } = scope;
7607
7673
  const name = nameOf4(operands[0]);
7608
- const target = name === void 0 ? void 0 : doc.resolve(xobjects?.get(name));
7674
+ const target = name === void 0 ? void 0 : partOrAbsent(() => doc.resolve(xobjects?.get(name)));
7609
7675
  if (target?.kind !== "stream" || nameOf4(doc.resolve(target.dict.get("Subtype"))) !== "Form") return;
7610
7676
  const own = doc.dictOf(target.dict.get("Resources")) ?? resources;
7611
7677
  chargeFormCall(walking.budget);
@@ -8111,10 +8177,17 @@ function groupOperand(doc, entry) {
8111
8177
  }
8112
8178
  return null;
8113
8179
  }
8180
+ var UNREADABLE_XOBJECT_OP = "unreadableXObject";
8114
8181
  function doXObject(context, frame, name, out) {
8115
8182
  if (name === void 0) return;
8116
8183
  const xobjects = context.doc.dictOf(frame.resources?.get("XObject"));
8117
- const target = context.doc.resolve(xobjects?.get(name));
8184
+ const declared = xobjects?.get(name);
8185
+ const read = partOrAbsent(() => ({ target: context.doc.resolve(declared) }));
8186
+ if (read === void 0) {
8187
+ out.push({ op: UNREADABLE_XOBJECT_OP, args: [name] });
8188
+ return;
8189
+ }
8190
+ const { target } = read;
8118
8191
  if (target?.kind !== "stream") return;
8119
8192
  const subtype = nameOf6(context.doc.resolve(target.dict.get("Subtype")));
8120
8193
  if (subtype === "Image") {
@@ -8604,12 +8677,13 @@ function isSymbolFontOfWalk(walk2, fontName) {
8604
8677
  return answer;
8605
8678
  }
8606
8679
  var glyphUnicode = (unicode, inSymbolFont) => inSymbolFont ? symbolFontText(unicode ?? "") : unicode ?? "";
8607
- var INVENTED_CONTROL = /^[\u0000-\u0008\u000a-\u001f\u007f-\u009f]+$/u;
8608
- var isInventedControl = (text) => INVENTED_CONTROL.test(text);
8680
+ var CONTROL_CHARACTERS = /[\u0000-\u0008\u000a-\u001f\u007f-\u009f]/gu;
8681
+ var withoutControls = (text) => text.replace(CONTROL_CHARACTERS, "");
8609
8682
  function placedTextOf(g, inSymbolFont) {
8610
8683
  const unicode = glyphUnicode(g.unicode, inSymbolFont);
8611
8684
  if (unicode.length === 0) return g.read === "not-ours" ? { text: "", ...UNREAD_FONT_MARK } : void 0;
8612
- return isInventedControl(unicode) ? void 0 : { text: glyphText(unicode) };
8685
+ const kept = withoutControls(unicode);
8686
+ return kept.length === 0 ? void 0 : { text: glyphText(kept) };
8613
8687
  }
8614
8688
  function riseShiftOf(rise, params, textToDevice, origin) {
8615
8689
  if (rise === 0) return NO_RISE;
@@ -9158,7 +9232,7 @@ function fontObjectsOfPage(doc, page) {
9158
9232
  const resolve = (name, scope) => {
9159
9233
  const parent = resourcesOf.get(scope);
9160
9234
  const xobjects = doc.dictOf(parent?.get("XObject"));
9161
- const target = doc.resolve(xobjects?.get(name));
9235
+ const target = partOrAbsent(() => doc.resolve(xobjects?.get(name)));
9162
9236
  if (target?.kind !== "stream") return void 0;
9163
9237
  if (doc.resolve(target.dict.get("Subtype"))?.value !== "Form") return void 0;
9164
9238
  let ops;
@@ -9290,23 +9364,24 @@ function flattenOutline(tree) {
9290
9364
  }
9291
9365
  function outlineTreeOf(doc) {
9292
9366
  const catalog = doc.dictOf(doc.trailer().get("Root"));
9293
- const root = doc.dictOf(catalog?.get("Outlines"));
9367
+ const root = partOrAbsent(() => doc.dictOf(catalog?.get("Outlines")));
9294
9368
  if (root === void 0) return void 0;
9295
9369
  const seen = /* @__PURE__ */ new Set();
9296
9370
  let count2 = 0;
9297
9371
  const readSiblings = (first) => {
9298
9372
  const out = [];
9299
- let node = doc.dictOf(first);
9373
+ let node = partOrAbsent(() => doc.dictOf(first));
9300
9374
  while (node !== void 0 && !seen.has(node)) {
9301
9375
  seen.add(node);
9302
9376
  count2 += 1;
9303
9377
  if (count2 > OUTLINE_READ_MAX_ENTRIES) return void 0;
9304
- const titleValue = doc.resolve(node.get("Title"));
9378
+ const current = node;
9379
+ const titleValue = partOrAbsent(() => doc.resolve(current.get("Title")));
9305
9380
  const title = titleValue?.kind === "string" ? decodePdfTextString(titleValue.bytes) : void 0;
9306
9381
  const children = readSiblings(node.get("First"));
9307
9382
  if (children === void 0) return void 0;
9308
9383
  out.push({ title, items: children });
9309
- node = doc.dictOf(node.get("Next"));
9384
+ node = partOrAbsent(() => doc.dictOf(current.get("Next")));
9310
9385
  }
9311
9386
  return out;
9312
9387
  };
@@ -9483,23 +9558,27 @@ function isVisible(doc, annot) {
9483
9558
  const bits = flags?.kind === "number" ? flags.value : 0;
9484
9559
  return (bits & (FLAG_INVISIBLE | FLAG_HIDDEN | FLAG_NOVIEW)) === 0;
9485
9560
  }
9561
+ function linkOf(doc, item) {
9562
+ const annot = doc.dictOf(item);
9563
+ if (annot === void 0 || nameOf9(doc.resolve(annot.get("Subtype"))) !== "Link") return void 0;
9564
+ if (!isVisible(doc, annot)) return void 0;
9565
+ const action = doc.dictOf(annot.get("A"));
9566
+ if (action === void 0 || nameOf9(doc.resolve(action.get("S"))) !== "URI") return void 0;
9567
+ if (doc.resolve(action.get("NewWindow")) !== void 0) return void 0;
9568
+ const uriValue = doc.resolve(action.get("URI"));
9569
+ if (uriValue?.kind !== "string") return void 0;
9570
+ const url = absoluteTargetOf(Buffer.from(uriValue.bytes).toString("latin1"));
9571
+ const rect = rectOf(doc, annot);
9572
+ if (url === void 0 || rect === void 0) return void 0;
9573
+ return { url, rect };
9574
+ }
9486
9575
  function linkAnnotationsOfPage(doc, page) {
9487
9576
  const links = [];
9488
- const annots = doc.resolve(page.dict.get("Annots"));
9577
+ const annots = partOrAbsent(() => doc.resolve(page.dict.get("Annots")));
9489
9578
  if (annots?.kind !== "array") return links;
9490
9579
  for (const item of annots.items) {
9491
- const annot = doc.dictOf(item);
9492
- if (annot === void 0 || nameOf9(doc.resolve(annot.get("Subtype"))) !== "Link") continue;
9493
- if (!isVisible(doc, annot)) continue;
9494
- const action = doc.dictOf(annot.get("A"));
9495
- if (action === void 0 || nameOf9(doc.resolve(action.get("S"))) !== "URI") continue;
9496
- if (doc.resolve(action.get("NewWindow")) !== void 0) continue;
9497
- const uriValue = doc.resolve(action.get("URI"));
9498
- if (uriValue?.kind !== "string") continue;
9499
- const url = absoluteTargetOf(Buffer.from(uriValue.bytes).toString("latin1"));
9500
- const rect = rectOf(doc, annot);
9501
- if (url === void 0 || rect === void 0) continue;
9502
- links.push({ url, rect });
9580
+ const link = partOrAbsent(() => linkOf(doc, item));
9581
+ if (link !== void 0) links.push(link);
9503
9582
  }
9504
9583
  return links;
9505
9584
  }
@@ -9896,28 +9975,30 @@ function ourImagesOf(ctx, ops) {
9896
9975
  refusals.set(name, "ambiguous");
9897
9976
  continue;
9898
9977
  }
9899
- const scope = { resources: painted.resources };
9900
- const rawName = painted.stencil ? resourceNameOf(name) : name;
9901
- const decode = () => ccittImageObjectOf(ctx.ourDoc, ctx.file, scope, name, IMAGE_KIND.GRAYSCALE_1BPP) ?? jpegImageObjectOf(ctx.ourDoc, scope, name, { rgb: IMAGE_KIND.RGB_24BPP, rgba: IMAGE_KIND.RGBA_32BPP }) ?? flateImageObjectOf(ctx.ourDoc, scope, name, { rgb: IMAGE_KIND.RGB_24BPP, rgba: IMAGE_KIND.RGBA_32BPP });
9902
- const stream = ctx.ourDoc.resolve(ctx.ourDoc.dictOf(painted.resources?.get("XObject"))?.get(rawName));
9903
- if (stream?.kind !== "stream") {
9904
- refusals.set(name, "absent");
9905
- continue;
9906
- }
9907
- const image = painted.stencil ? stencilImageObjectOf(ctx.ourDoc, scope, rawName, { rgba: IMAGE_KIND.RGBA_32BPP }, painted.colour) : documentCacheOf(ctx.ourDoc).getOrCompute(
9908
- PICTURE,
9909
- stream,
9910
- // Never read `.data` here: on a JPEG it is a lazy accessor that decodes on first read, and the cache asks the
9911
- // size on every insertion. The size follows the kind, not an average: four bytes per pixel would charge a
9912
- // 1-bit CCITT page thirty-two times its size, past the cache ceiling, so it would never be kept.
9913
- (decodedImage) => decodedImage === void 0 ? 0 : cachedPictureBytes(decodedImage),
9914
- decode
9915
- );
9916
- if (image === void 0) refusals.set(name, pictureRefusalOf(ctx.ourDoc, stream.dict));
9917
- else decoded.set(name, image);
9978
+ const read = partOrAbsent(() => pictureOf(ctx, name, painted));
9979
+ if (read === void 0) refusals.set(name, "shape");
9980
+ else if ("image" in read) decoded.set(name, read.image);
9981
+ else refusals.set(name, read.refusal);
9918
9982
  }
9919
9983
  return { decoded, refusals };
9920
9984
  }
9985
+ function pictureOf(ctx, name, painted) {
9986
+ const scope = { resources: painted.resources };
9987
+ const rawName = painted.stencil ? resourceNameOf(name) : name;
9988
+ const decode = () => ccittImageObjectOf(ctx.ourDoc, scope, name, IMAGE_KIND.GRAYSCALE_1BPP) ?? jpegImageObjectOf(ctx.ourDoc, scope, name, { rgb: IMAGE_KIND.RGB_24BPP, rgba: IMAGE_KIND.RGBA_32BPP }) ?? flateImageObjectOf(ctx.ourDoc, scope, name, { rgb: IMAGE_KIND.RGB_24BPP, rgba: IMAGE_KIND.RGBA_32BPP });
9989
+ const stream = ctx.ourDoc.resolve(ctx.ourDoc.dictOf(painted.resources?.get("XObject"))?.get(rawName));
9990
+ if (stream?.kind !== "stream") return { refusal: "absent" };
9991
+ const image = painted.stencil ? stencilImageObjectOf(ctx.ourDoc, scope, rawName, { rgba: IMAGE_KIND.RGBA_32BPP }, painted.colour) : documentCacheOf(ctx.ourDoc).getOrCompute(
9992
+ PICTURE,
9993
+ stream,
9994
+ // Never read `.data` here: on a JPEG it is a lazy accessor that decodes on first read, and the cache asks the
9995
+ // size on every insertion. The size follows the kind, not an average: four bytes per pixel would charge a
9996
+ // 1-bit CCITT page thirty-two times its size, past the cache ceiling, so it would never be kept.
9997
+ (decodedImage) => decodedImage === void 0 ? 0 : cachedPictureBytes(decodedImage),
9998
+ decode
9999
+ );
10000
+ return image === void 0 ? { refusal: pictureRefusalOf(ctx.ourDoc, stream.dict) } : { image };
10001
+ }
9921
10002
  function scanHiddenCandidates(ops) {
9922
10003
  let candidate = false;
9923
10004
  let namesLayer = false;
@@ -9964,6 +10045,7 @@ function readPageText(ctx, pageIndex) {
9964
10045
  if (ops.length > PDF_MAX_OPS_PER_PAGE)
9965
10046
  throw new ReaderError4("FILE_UNSAFE", "the PDF has a page with too many drawing operations to be read");
9966
10047
  count(ctx.cost, "operatorsWalked", ops.length);
10048
+ const unreadObjects = ops.filter((op) => op.op === UNREADABLE_XOBJECT_OP).length;
9967
10049
  const fonts = fontObjectsOfPage(ctx.ourDoc, entry);
9968
10050
  const ourFont = (name) => fonts.get(name);
9969
10051
  const symbolVerdict = (name) => {
@@ -10000,46 +10082,11 @@ function readPageText(ctx, pageIndex) {
10000
10082
  glyphs: ctx.keepGlyphs ? glyphs : [],
10001
10083
  ...glyphsUnavailable !== void 0 ? { glyphsUnavailable } : {},
10002
10084
  ...fontUnread ? { fontUnread: true } : {},
10085
+ ...unreadObjects > 0 ? { unreadObjects } : {},
10003
10086
  runs,
10004
10087
  reference
10005
10088
  };
10006
10089
  }
10007
- var REFUSAL_IS_BOUND = {
10008
- syntax: false,
10009
- truncated: false,
10010
- "nesting-too-deep": true,
10011
- "token-too-long": true,
10012
- "string-too-long": true,
10013
- "stream-length": false,
10014
- "stream-end": false,
10015
- "length-unresolved": false,
10016
- "startxref-missing": false,
10017
- "xref-offset": false,
10018
- "xref-syntax": false,
10019
- "xref-chain": false,
10020
- "xref-too-large": true,
10021
- "filter-unsupported": false,
10022
- predictor: false,
10023
- "stream-decode": false,
10024
- "object-stream": false,
10025
- "xref-stream": false,
10026
- "xref-stale": false,
10027
- "ref-hops": true,
10028
- "page-tree": false,
10029
- "page-box": false,
10030
- "content-syntax": false,
10031
- "content-operands": false,
10032
- "content-too-long": true,
10033
- "inline-image": false,
10034
- encrypted: false,
10035
- "to-unicode": false,
10036
- "value-too-large": true,
10037
- "document-too-large": true,
10038
- "glyphs-too-many": true,
10039
- "forms-too-many": true,
10040
- "font-tables-too-many": true,
10041
- "page-missing": false
10042
- };
10043
10090
  function textUnreadReasonOf(error) {
10044
10091
  if (error instanceof GlyphBuilderAbstention) return "unsupported";
10045
10092
  if (error instanceof PdfObjectsRefusal) return REFUSAL_IS_BOUND[error.reason] ? "limit" : "damaged";
@@ -10106,6 +10153,7 @@ function readPageSource(text, pageIndex, wants, ctx) {
10106
10153
  glyphs,
10107
10154
  ...text.glyphsUnavailable !== void 0 ? { glyphsUnavailable: text.glyphsUnavailable } : {},
10108
10155
  ...text.fontUnread === true ? { fontUnread: true } : {},
10156
+ ...text.unreadObjects !== void 0 ? { unreadObjects: text.unreadObjects } : {},
10109
10157
  ...declaredOrder.length > 0 ? { declaredOrder: hoistAgainstTree(declaredOrder, runs) } : {},
10110
10158
  ...noteIds.size > 0 ? { noteIds } : {},
10111
10159
  reference,
@@ -10133,7 +10181,7 @@ async function readPdfSource(bytes, options = {}) {
10133
10181
  const ours = openOrRefuse(bytes);
10134
10182
  const pageCount = ours.document.pages().length;
10135
10183
  onOpen?.(pageCount);
10136
- const info = documentInfoOf(ours.document, bytes);
10184
+ const info = partOrAbsent(() => documentInfoOf(ours.document, bytes));
10137
10185
  const outline = outlineOfDocument(ours.document);
10138
10186
  let layerConfig;
10139
10187
  let layersRead = false;
@@ -10164,7 +10212,12 @@ async function readPdfSource(bytes, options = {}) {
10164
10212
  text.glyphs = [];
10165
10213
  return page;
10166
10214
  });
10167
- return { pages, document: ours.document, info, ...outline !== void 0 ? { outline } : {} };
10215
+ return {
10216
+ pages,
10217
+ document: ours.document,
10218
+ ...info !== void 0 ? { info } : {},
10219
+ ...outline !== void 0 ? { outline } : {}
10220
+ };
10168
10221
  }
10169
10222
 
10170
10223
  // src/extractor/pdf-model.ts
@@ -10737,7 +10790,7 @@ var openPdfSource = async (bytes, cost, pageLimit = NO_CAP, firstPages) => {
10737
10790
  // THE CEILING IS APPLIED BEFORE A SINGLE PAGE IS READ. A
10738
10791
  // refused document therefore pays for no extraction — that is the whole point
10739
10792
  // of a volume guard. The ceiling is the CALLER's (`pageLimit`, `NO_CAP` when it
10740
- // asked for none): see `core/document-caps.ts#DOCUMENT_PAGE_LIMIT` for the
10793
+ // asked for none): see the application's `DOCUMENT_PAGE_LIMIT` for the
10741
10794
  // product's value and for why detection can NOT be done on the file size.
10742
10795
  onOpen: (pageCount) => {
10743
10796
  if (firstPages !== void 0) return;
@@ -14593,6 +14646,15 @@ function pdfTextUnreadNotices(unreadByPage, replaced) {
14593
14646
  }
14594
14647
  return notices.length > 0 ? notices : void 0;
14595
14648
  }
14649
+ function pdfPartUnreadNotices(unreadByPage, replaced) {
14650
+ const kept = [...unreadByPage].filter(([pageIndex, count3]) => count3 > 0 && !replaced.has(pageIndex));
14651
+ if (kept.length === 0) return void 0;
14652
+ const pages = kept.map(([pageIndex]) => pageIndex).sort((left, right) => left - right);
14653
+ const ranges = pageRangesOf(pages).slice(0, MAX_NOTICE_PAGE_RANGES);
14654
+ const shown = ranges.reduce((sum, range) => sum + range.to - range.from + 1, 0);
14655
+ const count2 = kept.reduce((sum, [, objects]) => sum + objects, 0);
14656
+ return [{ kind: "pdf-part-unread", count: count2, pages: ranges, more: pages.length - shown }];
14657
+ }
14596
14658
  var enrichRuns = (source, readItems, glyphsByPage) => {
14597
14659
  const originalOf = /* @__PURE__ */ new Map();
14598
14660
  const items = readItems.map((runs, pageIndex) => {
@@ -14812,6 +14874,10 @@ var assembleResult = (doc, laid, readItems, limits, pageLanguage) => {
14812
14874
  if (reason !== void 0) textUnreadByPage.set(pageIndex, reason);
14813
14875
  });
14814
14876
  const textUnreadNotices = pdfTextUnreadNotices(textUnreadByPage, /* @__PURE__ */ new Set());
14877
+ const unreadObjectsByPage = /* @__PURE__ */ new Map();
14878
+ source.pages.forEach((page, pageIndex) => {
14879
+ if (page.unreadObjects !== void 0) unreadObjectsByPage.set(pageIndex, page.unreadObjects);
14880
+ });
14815
14881
  let tablesWithheld = 0;
14816
14882
  const published = merged2.map((page, pageIndex) => {
14817
14883
  const { blocks, withheld } = withholdOversizedTables(
@@ -14828,6 +14894,7 @@ var assembleResult = (doc, laid, readItems, limits, pageLanguage) => {
14828
14894
  const notices = [
14829
14895
  ...imageNotices ?? [],
14830
14896
  ...textUnreadNotices ?? [],
14897
+ ...pdfPartUnreadNotices(unreadObjectsByPage, /* @__PURE__ */ new Set()) ?? [],
14831
14898
  ...unpublishedPartNotices({ chartsWithheld: 0, imagesMissing: 0, tablesWithheld })
14832
14899
  ];
14833
14900
  return {
@@ -14835,6 +14902,7 @@ var assembleResult = (doc, laid, readItems, limits, pageLanguage) => {
14835
14902
  ...notices.length > 0 ? { notices } : {},
14836
14903
  imageLossByPage,
14837
14904
  textUnreadByPage,
14905
+ unreadObjectsByPage,
14838
14906
  ...publishedImages.length > 0 ? { images: publishedImages, pagesOfImage: images.pagesOfImage } : {},
14839
14907
  scannedPageIndices,
14840
14908
  flattenedGridPages: laid.flattenedGridPages,
@@ -15104,12 +15172,20 @@ var conservationSourceOf = (textSource, toRead) => ({
15104
15172
  // The second reading of the letters control, beside the word verdict (`pipeline/letter-conservation.ts`).
15105
15173
  letters: textSource.reference
15106
15174
  });
15107
- var keptAfterAiReading = (replaced, images, pagesOfImage, imageLossByPage, textUnreadByPage) => {
15108
- const keptLosses = [
15175
+ var LOSS_NOTICES = /* @__PURE__ */ new Set([
15176
+ "pdf-image-unread",
15177
+ "pdf-text-unread",
15178
+ "pdf-part-unread"
15179
+ ]);
15180
+ var keptAfterAiReading = (replaced, extraction) => {
15181
+ const { images, pagesOfImage, imageLossByPage, textUnreadByPage, unreadObjectsByPage, notices } = extraction;
15182
+ const kept = [
15183
+ ...(notices ?? []).filter((notice) => !LOSS_NOTICES.has(notice.kind)),
15109
15184
  ...pdfImageUnreadNotices(imageLossByPage, replaced) ?? [],
15110
- ...pdfTextUnreadNotices(textUnreadByPage, replaced) ?? []
15185
+ ...pdfTextUnreadNotices(textUnreadByPage, replaced) ?? [],
15186
+ ...pdfPartUnreadNotices(unreadObjectsByPage, replaced) ?? []
15111
15187
  ];
15112
- const keptNotices = keptLosses.length > 0 ? keptLosses : void 0;
15188
+ const keptNotices = kept.length > 0 ? kept : void 0;
15113
15189
  const keptImages = images?.filter((image) => {
15114
15190
  const placedOn = pagesOfImage?.get(image.sha256);
15115
15191
  return placedOn === void 0 || [...placedOn].some((pageIndex) => !replaced.has(pageIndex));
@@ -15128,7 +15204,8 @@ var extractPdfWithVision = async (bytes, vision, opts) => {
15128
15204
  docFacts,
15129
15205
  notices,
15130
15206
  imageLossByPage,
15131
- textUnreadByPage
15207
+ textUnreadByPage,
15208
+ unreadObjectsByPage
15132
15209
  } = await extractPdf(bytes, opts?.pageLanguage, opts?.limits === void 0 ? void 0 : { limits: opts.limits });
15133
15210
  const language = opts?.pageLanguage ?? "en";
15134
15211
  const pageCount = pages.length;
@@ -15166,16 +15243,20 @@ var extractPdfWithVision = async (bytes, vision, opts) => {
15166
15243
  throw aiReadingFailure(error, { pageCount, scannedPageCount: scannedPages });
15167
15244
  });
15168
15245
  const merged2 = pages.map((digitalBlocks, pageIndex) => ocrByPage.get(pageIndex) ?? digitalBlocks);
15169
- const { keptNotices, keptImages } = keptAfterAiReading(
15170
- new Set(ocrByPage.keys()),
15246
+ const { keptNotices, keptImages } = keptAfterAiReading(new Set(ocrByPage.keys()), {
15171
15247
  images,
15172
15248
  pagesOfImage,
15173
15249
  imageLossByPage,
15174
- textUnreadByPage
15175
- );
15250
+ textUnreadByPage,
15251
+ unreadObjectsByPage,
15252
+ notices
15253
+ });
15176
15254
  return result(
15177
15255
  pdfPageTabs(merged2, language, opts?.labels),
15178
- { images: keptImages && keptImages.length > 0 ? keptImages : void 0, notices: keptNotices },
15256
+ {
15257
+ images: keptImages && keptImages.length > 0 ? keptImages : void 0,
15258
+ notices: keptNotices
15259
+ },
15179
15260
  scanned
15180
15261
  );
15181
15262
  };