Skip to content

Commit

Permalink
Merge pull request #111 from PeculiarVentures/romashine/fix-bugs
Browse files Browse the repository at this point in the history
fix bugs
  • Loading branch information
microshine authored Dec 7, 2023
2 parents 65925d3 + 6c698ee commit 73eb094
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
20 changes: 18 additions & 2 deletions packages/core/src/objects/Stream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ endstream`,
`<</Length 5>>
stream
12345
endstream`,
5,
],
[
`<</Length 10>>
stream
12345
endstream`,
5,
],
[
`<</Length 1>>
stream
12345
endstream`,
5,
],
Expand All @@ -43,7 +57,9 @@ endstream`,
const vector: [Uint8Array, string][] = [
[
Buffer.from("sd"),
`<< /Length 2 >>
`<<
/Length 2
>>
stream
sd
endstream`,
Expand All @@ -68,7 +84,7 @@ endstream`,

// const outStream = new ByteStream();
// stream.toPDF(outStream);

// const parsedStream = new pdf.Stream();
// parsedStream.fromPDF(outStream);
// const decodedStream = await parsedStream.decode();
Expand Down
40 changes: 13 additions & 27 deletions packages/core/src/objects/Stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,36 +166,22 @@ export class PDFStream extends PDFDictionary implements EncryptionObject {
throw new Error("Required filed 'Length' is missing");
}

if (length.isIndirect()) {
if (length.documentUpdate) {
PDFNumeric.assertPositiveInteger(length);
PDFStream.skipEndOfLine(reader);
PDFStream.skipEndOfLine(reader);
const startPosition = reader.position;

this.stream = reader.read(length.value);
} else {
PDFStream.skipEndOfLine(reader);

let endStreamPosition = reader.findIndex("endstream");
if (endStreamPosition === -1) {
throw new ParsingError("Cannot find 'endstream' keyword");
}
if (reader.view[endStreamPosition - 1] === 0x0D && reader.view[endStreamPosition - 2] === 0x0A) {
endStreamPosition -= 2;
} else if (reader.view[endStreamPosition - 1] === 0x0A || reader.view[endStreamPosition - 1] === 0x0D) {
endStreamPosition -= 1;
} else {
throw new Error("Wrong end of line (must be \\r\\n or \\n)");
}
this.stream = reader.view.subarray(reader.position, endStreamPosition);
reader.position = endStreamPosition;
}
let endStreamPosition = reader.findIndex("endstream");
if (endStreamPosition === -1) {
throw new ParsingError("Cannot find 'endstream' keyword");
}
if (reader.view[endStreamPosition - 1] === 0x0D && reader.view[endStreamPosition - 2] === 0x0A) {
endStreamPosition -= 2;
} else if (reader.view[endStreamPosition - 1] === 0x0A || reader.view[endStreamPosition - 1] === 0x0D) {
endStreamPosition -= 1;
} else {
if (length.value) {
PDFStream.skipEndOfLine(reader);

this.stream = reader.read(length.value);
}
throw new Error("Wrong end of line (must be \\r\\n or \\n)");
}
this.stream = reader.view.subarray(startPosition, endStreamPosition);
reader.position = endStreamPosition;

if (this.has("Filter") || !(this.has("Type") && this.get("Type", PDFName).text === "XRef") && this.documentUpdate?.document.encryptHandler) {
this.encrypted = true;
Expand Down
11 changes: 10 additions & 1 deletion packages/core/src/structure/CrossReferenceTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ export class CrossReferenceTable extends TrailerDictionary implements CrossRefer
while (counter--) {
id++;
const entityPosition = reader.position;
const line = Convert.ToUtf8String(reader.read(20));
const line = Convert.ToUtf8String(reader.read(18));

const view = reader.view.subarray(reader.position);
if (view[0] === 0x0D && view[1] === 0x0A) {
reader.read(2);
} else if (view[0] === 0x0A || view[0] === 0x0D) {
reader.read(1);
} else {
throw new Error("Wrong end of line (must be \\r\\n or \\n)");
}

const matches = /([0-9]{10}) ([0-9]{5}) ([fn])/.exec(line);
if (!matches) {
Expand Down
2 changes: 1 addition & 1 deletion packages/doc/src/forms/TextEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export class TextEditor extends FormComponent {

public override paint(): void {
const ap = this.target.AP.get();
if (!ap.has("N")) {
if (!ap.has("N") || ap.N instanceof core.PDFNull) {
// If AP created by get() function it doesn't have a required filed N
ap.N = core.FormDictionary.create(this.target.documentUpdate!);
}
Expand Down

0 comments on commit 73eb094

Please sign in to comment.