Socket
Socket
Sign inDemoInstall

svgicons2svgfont

Package Overview
Dependencies
2
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.1 to 0.0.2

tests/expected/variableheighticons.svg

3

bin/svgicons2svgfont.js

@@ -5,4 +5,5 @@ #! /usr/bin/env node

, Fs = require('fs');
svgicons2svgfont(Fs.readdirSync(process.argv[2]).map(function(file) {
return process.argv[2] + '/' + file;
}), process.argv[3]);
})).pipe(Fs.createWriteStream(process.argv[3]));
{
"name": "svgicons2svgfont",
"version": "0.0.1",
"version": "0.0.2",
"description": "Read a set of SVG icons and ouput a SVG font",

@@ -23,3 +23,3 @@ "homepage": "https://github.com/nfroidure/svgicons2svgfont",

"sax": "0.5.x",
"svg-pathdata": "0.x.x"
"svg-pathdata": "0.0.2"
},

@@ -26,0 +26,0 @@ "devDependencies": {

@@ -15,9 +15,14 @@ svgicons2svgfont [![Build Status](https://travis-ci.org/nfroidure/svgicons2svgfont.png?branch=master)](https://travis-ci.org/nfroidure/svgicons2svgfont)

```js
var svgicons2svgfont = require('svgicons2svgfont');
svgicons2svgfont([
var svgicons2svgfont = require('svgicons2svgfont')
, fs = require('fs');
, fontStream = svgicons2svgfont([
'icons/directory/icon1.svg',
'icons/directory/icon2.svg'
],
'font/destination/file.svg',
options);
], options);
// Saving in a file
fontStream.pipe(fs.createWriteStream('font/destination/file.svg'))
.on('finish',function() {
console.log('Font written !')
});
```

@@ -24,0 +29,0 @@

@@ -12,5 +12,5 @@ /*

var UNICODE_PRIVATE_USE_AREA = {
start: 0xE001,
end: 0xF8FF
}
start: 0xE001,
end: 0xF8FF
}
// http://www.whizkidtech.redprince.net/bezier/circle/

@@ -23,9 +23,9 @@ , KAPPA = ((Math.sqrt(2)-1)/3)*4;

, Sax = require("sax")
,
SVGPathData = require("svg-pathdata");
, SVGPathData = require("svg-pathdata");
function svgicons2svgfont(files, dest, options) {
function svgicons2svgfont(files, options) {
options = options || {};
options.fontName = options.fontName || 'iconfont';
var outputFont = Fs.createWriteStream(dest)
var Stream = require("stream").PassThrough
, outputStream = new Stream()
, usedCodePoints = []

@@ -65,8 +65,8 @@ , log = (options.log || console.log.bind(console))

// Move to the left corner
'M' + parseInt(tag.attributes.x,10).toString(10)
+ ' ' + parseInt(tag.attributes.y,10).toString(10)
'M' + parseFloat(tag.attributes.x,10).toString(10)
+ ' ' + parseFloat(tag.attributes.y,10).toString(10)
// Draw the rectangle
+ 'h' + parseInt(tag.attributes.width, 10).toString(10)
+ 'v' + (parseInt(tag.attributes.height, 10)*-1).toString(10)
+ 'h' + (parseInt(tag.attributes.width, 10)*-1).toString(10)
+ 'h' + parseFloat(tag.attributes.width, 10).toString(10)
+ 'v' + (parseFloat(tag.attributes.height, 10)).toString(10)
+ 'h' + (parseFloat(tag.attributes.width, 10)*-1).toString(10)
+ 'z'

@@ -79,8 +79,10 @@ );

// Move to the line start
'M' + parseInt(tag.attributes.x1,10).toString(10)
+ ' ' + parseInt(tag.attributes.y1,10).toString(10)
// Draw the line (rect with a weight of 1)
+ 'H' + parseInt(tag.attributes.x2, 10).toString(10)
+ 'v-5'
+ 'H' + parseInt(tag.attributes.x1, 10).toString(10)
'M' + parseFloat(tag.attributes.x1,10).toString(10)
+ ' ' + parseFloat(tag.attributes.y1,10).toString(10)
+ ' ' + (parseFloat(tag.attributes.x1,10)+1).toString(10)
+ ' ' + (parseFloat(tag.attributes.y1,10)+1).toString(10)
+ ' ' + (parseFloat(tag.attributes.x2,10)+1).toString(10)
+ ' ' + (parseFloat(tag.attributes.y2,10)+1).toString(10)
+ ' ' + parseFloat(tag.attributes.x2,10).toString(10)
+ ' ' + parseFloat(tag.attributes.y2,10).toString(10)
+ 'Z'

@@ -99,8 +101,8 @@ );

} else if('circle' === tag.name || 'ellipse' === tag.name) {
var cx = parseInt(tag.attributes.cx,10)
, cy = parseInt(tag.attributes.cy,10)
var cx = parseFloat(tag.attributes.cx,10)
, cy = parseFloat(tag.attributes.cy,10)
, rx = 'undefined' !== typeof tag.attributes.rx ?
parseInt(tag.attributes.rx,10) : parseInt(tag.attributes.r,10)
parseFloat(tag.attributes.rx,10) : parseFloat(tag.attributes.r,10)
, ry = 'undefined' !== typeof tag.attributes.ry ?
parseInt(tag.attributes.ry,10) : parseInt(tag.attributes.r,10);
parseFloat(tag.attributes.ry,10) : parseFloat(tag.attributes.r,10);
glyph.d.push(

@@ -126,5 +128,5 @@ 'M' + (cx - rx) + ',' + cy

if('width' === attr.name && 'svg' === saxStream._parser.tag.name) {
glyph.width = parseInt(attr.value, 10);
glyph.width = parseFloat(attr.value, 10);
} else if('height' === attr.name && 'svg' === saxStream._parser.tag.name) {
glyph.height = parseInt(attr.value, 10);
glyph.height = parseFloat(attr.value, 10);
} else if('d' === attr.name && 'path' === saxStream._parser.tag.name) {

@@ -147,3 +149,3 @@ if(attr.value) {

}) : glyphs[0].height)) {
log('The provided icons does not have the same length it could lead'
log('The provided icons does not have the same height it could lead'
+' to unexpected results.');

@@ -153,3 +155,3 @@ }

// (find a SAX parser that allows modifying SVG on the fly)
outputFont.write('<?xml version="1.0" standalone="no"?> \n\
outputStream.write('<?xml version="1.0" standalone="no"?> \n\
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >\n\

@@ -164,18 +166,18 @@ <svg xmlns="http://www.w3.org/2000/svg">\n\

glyph.d.forEach(function(cD) {
d+=' '+new SVGPathData(cD).ySymetry(fontHeight).encode();
d+=' '+new SVGPathData(cD).ySymetry(glyph.height).encode();
});
delete glyph.d;
delete glyph.running;
outputFont.write('\
outputStream.write('\
<glyph glyph-name="'+glyph.name+'" unicode="'+glyph.character+'" horiz-adv-x="'+glyph.width+'" d="'+d+'" />\n');
});
outputFont.write('\
outputStream.write('\
</font>\n\
</defs>\n\
</svg>\n');
outputFont.on('finish', function() {
log("Font saved to " + dest);
outputStream.on('finish', function() {
log("Font created");
'function' === (typeof options.callback) && (options.callback)(glyphs);
});
outputFont.end();
outputStream.end();
}

@@ -210,2 +212,3 @@ });

});
return outputStream;
}

@@ -212,0 +215,0 @@

var assert = require('assert')
, svgicons2svgfont = require(__dirname + '/../src/index.js')
, Fs = require('fs');
, Fs = require('fs')
, StringDecoder = require('string_decoder').StringDecoder;
// Helper
function generateFont(options, done) {
options.callback = options.callback || function() {
// Helpers
function generateFontToFile(options, done) {
var dest = __dirname + '/results/' + options.fontName + '.svg';
var stream = svgicons2svgfont(Fs.readdirSync(__dirname + '/fixtures/' + options.fontName)
.map(function(file) {
return __dirname + '/fixtures/' + options.fontName + '/' + file;
}), options);
stream.pipe(Fs.createWriteStream(dest)).on('finish', function() {
assert.equal(
Fs.readFileSync(__dirname + '/expected/' + options.fontName + '.svg',
{encoding: 'utf8'}),
Fs.readFileSync(__dirname + '/results/' + options.fontName + '.svg',
Fs.readFileSync(dest,
{encoding: 'utf8'})
);
done();
};
svgicons2svgfont(Fs.readdirSync(__dirname + '/fixtures/' + options.fontName)
});
}
function generateFontToMemory(options, done) {
var content = ''
, decoder = new StringDecoder('utf8');
var stream = svgicons2svgfont(Fs.readdirSync(__dirname + '/fixtures/' + options.fontName)
.map(function(file) {
return __dirname + '/fixtures/' + options.fontName + '/' + file;
}), __dirname + '/results/' + options.fontName + '.svg', options);
}), options);
stream.on('data', function(chunk) {
content += decoder.write(chunk);
});
stream.on('finish', function() {
assert.equal(
Fs.readFileSync(__dirname + '/expected/' + options.fontName + '.svg',
{encoding: 'utf8'}),
content
);
done();
});
}
// Tests
describe('Generating fonts', function() {
describe('Generating fonts to files', function() {
it("should work for simple SVG", function(done) {
generateFont({
generateFontToFile({
fontName: 'originalicons'

@@ -32,3 +55,3 @@ }, done);

it("should work for simple SVG", function(done) {
generateFont({
generateFontToFile({
fontName: 'cleanicons'

@@ -39,3 +62,3 @@ }, done);

it("should work for codepoint mapped SVG icons", function(done) {
generateFont({
generateFontToFile({
fontName: 'prefixedicons'

@@ -46,3 +69,3 @@ }, done);

it("should work with multipath SVG icons", function(done) {
generateFont({
generateFontToFile({
fontName: 'multipathicons'

@@ -53,3 +76,3 @@ }, done);

it("should work with simple shapes SVG icons", function(done) {
generateFont({
generateFontToFile({
fontName: 'shapeicons'

@@ -59,2 +82,63 @@ }, done);

it("should work with variable height icons", function(done) {
generateFontToFile({
fontName: 'variableheighticons'
}, done);
});
});
describe('Generating fonts to memory', function() {
it("should work for simple SVG", function(done) {
generateFontToMemory({
fontName: 'originalicons'
}, done);
});
it("should work for simple SVG", function(done) {
generateFontToMemory({
fontName: 'cleanicons'
}, done);
});
it("should work for codepoint mapped SVG icons", function(done) {
generateFontToMemory({
fontName: 'prefixedicons'
}, done);
});
it("should work with multipath SVG icons", function(done) {
generateFontToMemory({
fontName: 'multipathicons'
}, done);
});
it("should work with simple shapes SVG icons", function(done) {
generateFontToMemory({
fontName: 'shapeicons'
}, done);
});
});
describe('Testing CLI', function() {
it("should work for simple SVG", function(done) {
(require('child_process').exec)(
'node '+__dirname+'../bin/svgicons2svgfont.js '
+ __dirname + '/expected/originalicons.svg '
+ __dirname + '/results/originalicons.svg',
function() {
assert.equal(
Fs.readFileSync(__dirname + '/expected/originalicons.svg',
{encoding: 'utf8'}),
Fs.readFileSync(__dirname + '/results/originalicons.svg',
{encoding: 'utf8'})
);
done();
}
);
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc