Skip to content

Commit

Permalink
Separate findObjectAt out of findObject to allow searching by position
Browse files Browse the repository at this point in the history
  • Loading branch information
vbuch committed Nov 29, 2023
1 parent 661e8ff commit 1c9e4c3
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions packages/placeholder-plain/src/findObject.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
import getIndexFromRef from './getIndexFromRef';

/**
* @typedef {object} FindObjectAtReturnType
* @property {Buffer} dictionary
* @property {Buffer} stream
*/

/**
* @param {Buffer} pdf
* @param {number} position
* @returns {FindObjectAtReturnType}
*/
export const findObjectAt = (pdf, position) => {
let slice = pdf.subarray(position);
slice = slice.subarray(0, slice.indexOf('endobj', 'utf8') - 1);
// ^ Buffer from the start position until the first endobj (included).

const dictionary = slice.subarray(
slice.indexOf('<<', 'utf8') + 2,
slice.indexOf('>>', 'utf8') - 1,
);
const stream = slice.subarray(
slice.indexOf('stream', 'utf8') + 6,
slice.indexOf('endstream', 'utf8') - 1,
);

return {
dictionary,
stream,
};
};

/**
* @param {Buffer} pdf
* @param {Map} refTable
Expand All @@ -9,13 +40,7 @@ const findObject = (pdf, refTable, ref) => {
const index = getIndexFromRef(refTable, ref);

const offset = refTable.offsets.get(index);
let slice = pdf.slice(offset);
slice = slice.slice(0, slice.indexOf('endobj', 'utf8'));

// FIXME: What if it is a stream?
slice = slice.slice(slice.indexOf('<<', 'utf8') + 2);
slice = slice.slice(0, slice.lastIndexOf('>>', 'utf8'));
return slice;
return findObjectAt(pdf, offset).stream;
};

export default findObject;

0 comments on commit 1c9e4c3

Please sign in to comment.