Expose stylesheet and highBitdepth SVG input params

This commit is contained in:
Lovell Fuller
2025-06-16 11:11:02 +01:00
parent f92540f134
commit c4b1d80c35
9 changed files with 102 additions and 3 deletions

View File

@@ -730,6 +730,9 @@ sharp({ openSlide: { level: 0 } });
sharp({ level: 0 }); // Deprecated
sharp({ jp2: { oneshot: true } });
sharp({ jp2: { oneshot: false } });
sharp({ svg: { stylesheet: 'test' }});
sharp({ svg: { highBitdepth: true }});
sharp({ svg: { highBitdepth: false }});
sharp({ autoOrient: true });
sharp({ autoOrient: false });

View File

@@ -139,6 +139,41 @@ describe('SVG input', function () {
assert.strictEqual(info.channels, 4);
});
it('Can apply custom CSS', async () => {
const svg = `<?xml version="1.0" encoding="UTF-8"?>
<svg width="10" height="10" xmlns="http://www.w3.org/2000/svg">
<circle cx="5" cy="5" r="4" fill="green" />
</svg>`;
const stylesheet = 'circle { fill: red }';
const [r, g, b, a] = await sharp(Buffer.from(svg), { svg: { stylesheet } })
.extract({ left: 5, top: 5, width: 1, height: 1 })
.raw()
.toBuffer();
assert.deepEqual([r, g, b, a], [255, 0, 0, 255]);
});
it('Invalid stylesheet input option throws', () =>
assert.throws(
() => sharp({ svg: { stylesheet: 123 } }),
/Expected string for svg\.stylesheet but received 123 of type number/
)
);
it('Valid highBitdepth input option does not throw', () =>
assert.doesNotThrow(
() => sharp({ svg: { highBitdepth: true } })
)
);
it('Invalid highBitdepth input option throws', () =>
assert.throws(
() => sharp({ svg: { highBitdepth: 123 } }),
/Expected boolean for svg\.highBitdepth but received 123 of type number/
)
);
it('Fails to render SVG larger than 32767x32767', () =>
assert.rejects(
() => sharp(Buffer.from('<svg xmlns="http://www.w3.org/2000/svg" width="32768" height="1" />')).toBuffer(),