0.1.2 • Published 3 years ago

resvg-node v0.1.2

Weekly downloads
21
License
MPL-2.0
Repository
github
Last release
3 years ago

resvg for Node.js

resvg is a small, fast, and portable SVG rendering library written in Rust.

Installation

$ npm install resvg-node

Basic Usage

The library currently exports a single function render() which will render a SVG string and return a Node.js Buffer containing the image data.

const { render } = require('resvg-node');

const pngData = render('<svg height="100" width="100" xmlns="http://www.w3.org/2000/svg"><circle cx="50" cy="50" r="40" fill="red" /></svg>');
// pngData: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 ...>

You can also pass a second parameter with extra rendering and font options.

const { render } = require('resvg-node');

const options = {
    background: "#ff00ff",
    fit_to: {
        mode: "width",
        value: 1000
    }
};

const pngData = render('<svg height="100" width="100" xmlns="http://www.w3.org/2000/svg"><circle cx="50" cy="50" r="40" fill="red" /></svg>', options);
// pngData: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 ...>

SvgOptions

OptionTypeDefaultDescription
pathstringThe SVG image path. Used to resolve relative image paths.
fontFontOptionsFont related options.
dpinumber96.0The target DPI. Affects unit conversions.
languagesstring[]"en"A list of languages, for resolving systemLanguage conditional attributes.
shape_renderingShapeRenderingGeometricPrecisionThe default shape rendering method.
text_renderingTextRenderingOptimizeLegibilityThe default text rendering method.
image_renderingImageRenderingOptimizeQualityThe default image rendering method.
fit_toFitToOriginalThe size to render the SVG.
backgroundstringThe background color of the SVG.

FontOptions

OptionTypeDefaultDescription
load_system_fontsbooleantrueIf system fonts should be loaded.
font_filesstring[][]A list of font files to load.
font_dirsstring[][]A list of font directories to load.
default_font_familystringTimes New RomanThe default font family.
default_font_sizenumber12The default font size.
serif_familystringTimes New RomanThe 'serif' font family.
sans_serif_familystringArialThe 'sans-serif' font family.
cursive_familystringComic Sans MSThe 'cursive' font family.
fantasy_familystringImpactThe 'fantasy' font family.
monospace_familystringCourier NewThe 'monospace' font family.

ShapeRendering

NameValue
OptimizeSpeed0
CrispEdges1
GeometricPrecision2

TextRendering

NameValue
OptimizeSpeed0
OptimizeLegibility1
GeometricPrecision2

ImageRendering

NameValue
OptimizeQuality0
OptimizeSpeed1

FitTo

FitTo is a bit special, it specifies how the svg should be scaled and has 4 different modes:

Original

Renders the SVG at it's original size.

{ mode: "original" }

Width

Renders the SVG using the specified width.

{ mode: "width", value: 1080 }

Height

Renders the SVG using the specified width.

{ mode: "height", value: 1080 }

Zoom

Renders the SVG using the specified scale factor.

{ mode: "zoom", value: 1.5 }

Building

Since resvg is a Rust library. you will need to install the Rust toolchain to build the module. Pre-built binaries are also available for Windows and Linux and will be automatically downloaded when the module is installed.

To download a pre-built release:

$ npm run download

To build the module yourself:

$ npm run build:rs

To build an optimized release:

$ npm run build:release:rs

License

resvg and resvg-node are licensed under the MPLv2.0.