@sutton-signwriting/font-db
Version:
a javascript package for node that generates SVG and PNG images for individual symbols and complete signs
36 lines (33 loc) • 820 B
JavaScript
const { Resvg } = require('@resvg/resvg-js');
/**
* Convert SVG to PNG
* @param {string} svg SVG string
* @param {{width?: number, height?: number}} scale Options for scaling
* @returns {Promise<Buffer>} PNG buffer
*/
svg2png = function(svg, scale = {}) {
const options = {};
if (scale.width) {
options.fitTo = {
mode: 'width',
value: scale.width
};
}
if (scale.height) {
options.fitTo = {
mode: 'height',
value: scale.height
};
}
return new Promise(function (resolve, reject) {
try {
const resvg = new Resvg(svg, options);
const pngData = resvg.render();
const pngBuffer = pngData.asPng();
resolve(pngBuffer);
} catch (error) {
reject(error);
}
});
}
module.exports = { svg2png };