Skip to content

Commit

Permalink
Prevent adding identity transforms to the document (#1493)
Browse files Browse the repository at this point in the history
This makes the resulting PDF files smaller. It's cumbersome to filter out all commands that could result in identity transforms in code that's using PDFKit, so it makes sense to have the check in the transform() function.
  • Loading branch information
Oletus committed Jan 23, 2024
1 parent 1f70b45 commit a655194
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Add subset for PDF/UA
- Fix for line breaks in list items (#1486)
- Fix for soft hyphen not being replaced by visible hyphen if necessary (#457)
- Optimize output files by ignoring identity transforms

### [v0.14.0] - 2023-11-09

Expand Down
4 changes: 4 additions & 0 deletions lib/mixins/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ export default {

transform(m11, m12, m21, m22, dx, dy) {
// keep track of the current transformation matrix
if (m11 === 1 && m12 === 0 && m21 === 0 && m22 === 1 && dx === 0 && dy === 0) {
// Ignore identity transforms
return this;
}
const m = this._ctm;
const [m0, m1, m2, m3, m4, m5] = m;
m[0] = m0 * m11 + m2 * m12;
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/vector.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,26 @@ describe('Vector Graphics', () => {
});
});
});

describe('translate', () => {
test('identity transform is ignored', () => {
const docData = logData(document);
const vectorStream = Buffer.from(`1 0 0 -1 0 792 cm\n1 0 0 1 0 0 cm\n`, 'binary');

document
.translate(0, 0);
document.end();

expect(docData).not.toContainChunk([
`5 0 obj`,
`<<
/Length 33
>>`,
`stream`,
vectorStream,
`\nendstream`,
`endobj`
]);
});
});
});

0 comments on commit a655194

Please sign in to comment.