Prevent adding identity transforms to the document (#1493)

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.
This commit is contained in:
Olli Etuaho 2024-01-23 18:36:54 +02:00 committed by GitHub
parent 1f70b450a2
commit a655194de2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 0 deletions

View File

@ -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

View File

@ -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;

View File

@ -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`
]);
});
});
});