svgfont2svgicons
Advanced tools
Comparing version 1.0.0 to 2.0.0
{ | ||
"name": "svgfont2svgicons", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Extract SVG icons from an SVG font", | ||
@@ -24,13 +24,15 @@ "homepage": "https://github.com/nfroidure/svgfont2svgicons", | ||
"dependencies": { | ||
"sax": "0.6.x", | ||
"sax": "1.1.x", | ||
"svg-pathdata": "1.0.0", | ||
"readable-stream": "^1.0.33", | ||
"readable-stream": "^2.0.0", | ||
"plexer": "0.0.3", | ||
"streamqueue": "^0.1.1" | ||
"streamqueue": "^1.1.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~2.0.1", | ||
"mocha-lcov-reporter": "0.0.1", | ||
"coveralls": "~2.11.2", | ||
"istanbul": "~0.3.2" | ||
"istanbul": "~0.3.15", | ||
"mocha": "~2.2.5", | ||
"mocha-lcov-reporter": "0.0.2", | ||
"streamtest": "^1.2.0", | ||
"svgicons2svgfont": "^2.0.2" | ||
}, | ||
@@ -37,0 +39,0 @@ "author": { |
@@ -7,8 +7,9 @@ # svgfont2svgicons | ||
## Usage | ||
NodeJS module: | ||
### In your scripts | ||
```js | ||
var svgfont2svgicons = require('svgfont2svgicons'); | ||
var fs = require('fs'); | ||
var fontStream = fs.createReadStream('myFont.svg'); | ||
var iconProvider = svgfont2svgicons(options); | ||
var fontStream = fs.createReadStream('myFont.svg'); | ||
@@ -24,7 +25,7 @@ // Piping the font | ||
if(icon) { | ||
console.log('New icon:', icon.name, icon.codepoint); | ||
icon.stream.pipe(fs.createWriteStream(icon.name + '.svg')); | ||
console.log('New icon:', icon.metadata.name, icon.metadata.unicode); | ||
icon.pipe(fs.createWriteStream(icon.metadata.name + '.svg')); | ||
} | ||
} while(null !== icon); | ||
}).once('end',function() { | ||
}).once('end', function() { | ||
console.log('No more icons !') | ||
@@ -34,5 +35,5 @@ }); | ||
CLI (install the module globally): | ||
## CLI interface | ||
```sh | ||
svgicons2svgfont font/src/file.svg icons/dest/directory | ||
svgfont2svgicons font/src/file.svg icons/dest/directory | ||
``` | ||
@@ -39,0 +40,0 @@ |
@@ -11,10 +11,9 @@ /* | ||
// Required modules | ||
var Path = require("path") | ||
, util = require("util") | ||
, Stream = require("readable-stream") | ||
, Sax = require("sax") | ||
, SVGPathData = require("svg-pathdata") | ||
, Plexer = require('plexer') | ||
, StreamQueue = require('streamqueue') | ||
; | ||
var Path = require("path"); | ||
var util = require("util"); | ||
var Stream = require("readable-stream"); | ||
var Sax = require("sax"); | ||
var SVGPathData = require("svg-pathdata"); | ||
var Plexer = require('plexer'); | ||
var StreamQueue = require('streamqueue'); | ||
@@ -26,15 +25,13 @@ // Inherit of stream duplexer | ||
function SVGFont2SVGIcons(options) { | ||
var inputStream = null | ||
, outputStream = null | ||
, saxStream = null | ||
, pathParser = null | ||
, startContent = null | ||
, endContent = null | ||
, ascent = 0 | ||
, descent = 0 | ||
, horizontalAdv = 0 | ||
, glyph = null | ||
, glyphCount = 0 | ||
, d = '' | ||
; | ||
var inputStream = null; | ||
var outputStream = null; | ||
var saxStream = null; | ||
var pathParser = null; | ||
var startContent = null; | ||
var endContent = null; | ||
var ascent = 0; | ||
var descent = 0; | ||
var horizontalAdv = 0; | ||
var glyphCount = 0; | ||
var d = ''; | ||
@@ -55,3 +52,3 @@ // Ensure new were used | ||
}, inputStream, outputStream); | ||
// Setting objectMode separately | ||
@@ -63,2 +60,3 @@ this._writableState.objectMode = false; | ||
saxStream.on('opentag', function(tag) { | ||
var stream = null; | ||
// Save the default sizes | ||
@@ -81,19 +79,19 @@ if('font' === tag.name) { | ||
// Fill the glyph object | ||
glyph = { | ||
var stream = new StreamQueue(); | ||
stream.metadata = { | ||
name: '', | ||
codepoint: '', | ||
width: horizontalAdv, | ||
height: Math.abs(descent) + ascent, | ||
stream: new StreamQueue() | ||
height: Math.abs(descent) + ascent | ||
}; | ||
if('glyph-name' in tag.attributes) { | ||
glyph.name = tag.attributes['glyph-name']; | ||
stream.metadata.name = tag.attributes['glyph-name']; | ||
} else { | ||
glyph.name = 'icon' + (++glyphCount); | ||
stream.metadata.name = 'icon' + (++glyphCount); | ||
} | ||
if('horiz-adv-x' in tag.attributes) { | ||
glyph.width = tag.attributes['horiz-adv-x']; | ||
stream.metadata.width = tag.attributes['horiz-adv-x']; | ||
} | ||
if('unicode' in tag.attributes) { | ||
glyph.codepoint = tag.attributes.unicode; | ||
stream.metadata.unicode = [tag.attributes.unicode]; | ||
} | ||
@@ -104,5 +102,5 @@ d = ''; | ||
} | ||
outputStream.write(glyph); | ||
startContent = new Stream.PassThrough() | ||
glyph.stream.queue(startContent); | ||
outputStream.write(stream); | ||
startContent = new Stream.PassThrough(); | ||
stream.queue(startContent); | ||
startContent.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\ | ||
@@ -113,5 +111,5 @@ <svg\ | ||
version="1.1"\ | ||
width="' + glyph.width + '"\ | ||
height="' + glyph.height + '"\ | ||
viewBox="0 0 ' + glyph.width + ' ' + glyph.height + '">\ | ||
width="' + stream.metadata.width + '"\ | ||
height="' + stream.metadata.height + '"\ | ||
viewBox="0 0 ' + stream.metadata.width + ' ' + stream.metadata.height + '">\ | ||
<path\ | ||
@@ -123,5 +121,5 @@ d="'); | ||
pathParser = new SVGPathData.Parser(); | ||
glyph.stream.queue( | ||
stream.queue( | ||
pathParser.pipe(new SVGPathData.Transformer( | ||
SVGPathData.Transformer.Y_AXIS_SIMETRY, glyph.height | ||
SVGPathData.Transformer.Y_AXIS_SIMETRY, stream.metadata.height | ||
)).pipe(new SVGPathData.Transformer( | ||
@@ -135,8 +133,8 @@ SVGPathData.Transformer.TRANSLATE, 0, descent | ||
endContent = new Stream.PassThrough() | ||
glyph.stream.queue(endContent); | ||
stream.queue(endContent); | ||
endContent.write('"\ | ||
id="' + glyph.name + '" />\ | ||
id="' + stream.metadata.name + '" />\ | ||
</svg>'); | ||
endContent.end(); | ||
glyph.stream.done(); | ||
stream.done(); | ||
} | ||
@@ -152,2 +150,1 @@ }); | ||
module.exports = SVGFont2SVGIcons; | ||
@@ -1,7 +0,7 @@ | ||
var assert = require('assert') | ||
, svgfont2svgicons = require(__dirname + '/../src/index.js') | ||
, Fs = require('fs') | ||
, StringDecoder = require('string_decoder').StringDecoder | ||
, Path = require("path") | ||
; | ||
var assert = require('assert'); | ||
var svgfont2svgicons = require(__dirname + '/../src/index.js'); | ||
var Fs = require('fs'); | ||
var StringDecoder = require('string_decoder').StringDecoder; | ||
var Path = require("path"); | ||
var streamtest = require('streamtest'); | ||
@@ -11,48 +11,53 @@ // Tests | ||
it("should work for simple SVG fonts", function(done) { | ||
var fontStream = Fs.createReadStream(__dirname + '/fixtures/cleanicons.svg'); | ||
var iconProvider = svgfont2svgicons(); | ||
var icons = []; | ||
var bufferedIcons = 0; | ||
var ended = false; | ||
streamtest.versions.forEach(function(version) { | ||
fontStream.pipe(iconProvider); | ||
describe('for ' + version + ' streams', function() { | ||
iconProvider.on('readable', function() { | ||
var icon; | ||
do { | ||
icon = iconProvider.read(); | ||
if(icon) { | ||
icon.content = ''; | ||
icons.push(icons); | ||
icon.stream.on('readable', (function(icon) { | ||
return function() { | ||
var chunk; | ||
do { | ||
chunk = icon.stream.read(); | ||
if(chunk) { | ||
icon.content += chunk.toString('utf-8'); | ||
it("should work for simple SVG fonts", function(done) { | ||
var bufferedIcons = 0; | ||
var ended = false; | ||
Fs.createReadStream(__dirname + '/fixtures/cleanicons.svg') | ||
.pipe(svgfont2svgicons()) | ||
.pipe(streamtest[version].toObjects(function(err, icons) { | ||
if(err) { | ||
return done(err); | ||
} | ||
assert.equal(icons.length, 10); | ||
icons.forEach(function(icon, i) { | ||
icon.pipe(streamtest[version].toChunks(function(err, chunks) { | ||
assert.equal( | ||
chunks.reduce(function(content, chunk) { | ||
return content + chunk.toString('utf-8'); | ||
}, ''), | ||
Fs.readFileSync(__dirname + '/expected/cleanicons/' + icon.metadata.name + '.svg') | ||
); | ||
bufferedIcons++; | ||
if(bufferedIcons == icons.length) { | ||
done(); | ||
} | ||
} while(null !== chunk); | ||
}; | ||
})(icon)); | ||
icon.stream.once('end', (function(icon) { | ||
return function() { | ||
assert.equal( | ||
Fs.readFileSync(__dirname + '/expected/cleanicons/' + icon.name + '.svg'), | ||
icon.content | ||
); | ||
bufferedIcons++; | ||
if(ended && icons.length == bufferedIcons) { | ||
done(); | ||
} | ||
}; | ||
})(icon)); | ||
} | ||
} while(null !== icon); | ||
}).once('end',function() { | ||
ended = true; | ||
if(icons.length == bufferedIcons) { | ||
done(); | ||
} | ||
})); | ||
}); | ||
})); | ||
}); | ||
it("should be reentrant with svgicons2svgfont", function(done) { | ||
var svgicons2svgfont = require('svgicons2svgfont'); | ||
Fs.createReadStream(__dirname + '/fixtures/cleanicons2.svg') | ||
.pipe(svgfont2svgicons()) | ||
.pipe(svgicons2svgfont({ | ||
fontName: 'Plop' | ||
})) | ||
.pipe(streamtest[version].toChunks(function(err, chunks) { | ||
assert.equal( | ||
chunks.reduce(function(content, chunk) { | ||
return content + chunk.toString('utf-8'); | ||
}, ''), | ||
Fs.readFileSync(__dirname + '/fixtures/cleanicons2.svg') | ||
); | ||
done(); | ||
})); | ||
}); | ||
}); | ||
@@ -59,0 +64,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25870
20
51
6
206
+ Addedisarray@1.0.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedsax@1.1.6(transitive)
+ Addedstreamqueue@1.1.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
- Removedsax@0.6.1(transitive)
- Removedstreamqueue@0.1.3(transitive)
Updatedreadable-stream@^2.0.0
Updatedsax@1.1.x
Updatedstreamqueue@^1.1.0