svgicons2svgfont
Advanced tools
Comparing version 0.0.2 to 0.0.3
{ | ||
"name": "svgicons2svgfont", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Read a set of SVG icons and ouput a SVG font", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/nfroidure/svgicons2svgfont", |
@@ -10,12 +10,8 @@ /* | ||
// http://en.wikipedia.org/wiki/Private_Use_(Unicode) | ||
var UNICODE_PRIVATE_USE_AREA = { | ||
start: 0xE001, | ||
end: 0xF8FF | ||
} | ||
// http://www.whizkidtech.redprince.net/bezier/circle/ | ||
, KAPPA = ((Math.sqrt(2)-1)/3)*4; | ||
var KAPPA = ((Math.sqrt(2)-1)/3)*4; | ||
// Required modules | ||
var Path = require("path") | ||
, Stream = require("stream").PassThrough | ||
, Fs = require("fs") | ||
@@ -25,34 +21,9 @@ , Sax = require("sax") | ||
function svgicons2svgfont(files, options) { | ||
function svgicons2svgfont(glyphs, options) { | ||
options = options || {}; | ||
options.fontName = options.fontName || 'iconfont'; | ||
var Stream = require("stream").PassThrough | ||
, outputStream = new Stream() | ||
, usedCodePoints = [] | ||
var outputStream = new Stream() | ||
, log = (options.log || console.log.bind(console)) | ||
, error = options.error || console.error.bind(console) | ||
, glyphs = files.map(function(file) { | ||
// Creating an object for each icon | ||
var matches = Path.basename(file).match(/^(?:u([0-9a-f]{4})\-)?(.*).svg$/i); | ||
if(matches&&matches[1]) { | ||
usedCodePoints.push(parseInt(matches[1], 16)); | ||
return { | ||
name: matches[2], | ||
codepoint: matches[1], | ||
character: '&#x' + matches[1].toUpperCase() + ';', | ||
file: file, | ||
d: [], | ||
running: true | ||
}; | ||
} else if(matches) { | ||
return { | ||
name: matches[2], | ||
codepoint: 0, | ||
character: '', | ||
file: file, | ||
d: [], | ||
running: true | ||
}; | ||
} | ||
}).forEach(function (glyph, index, glyphs) { | ||
, error = options.error || console.error.bind(console); | ||
glyphs = glyphs.forEach(function (glyph, index, glyphs) { | ||
// Parsing each icons asynchronously | ||
@@ -164,4 +135,6 @@ var saxStream = Sax.createStream(true); | ||
delete glyph.running; | ||
outputStream.write('\ | ||
<glyph glyph-name="'+glyph.name+'" unicode="'+glyph.character+'" horiz-adv-x="'+glyph.width+'" d="'+d+'" />\n'); | ||
outputStream.write('\ | ||
<glyph glyph-name="' + glyph.name + '"\n\ | ||
unicode="&#x' + (glyph.codepoint.toString(16)).toUpperCase() + ';"\n\ | ||
horiz-adv-x="' + glyph.width + '" d="' + d +'" />\n'); | ||
}); | ||
@@ -179,28 +152,22 @@ outputStream.write('\ | ||
}); | ||
Fs.createReadStream(glyph.file).pipe(saxStream); | ||
// Find a free codepoint and rename the file | ||
if(0 === glyph.codepoint) { | ||
for(var i = UNICODE_PRIVATE_USE_AREA.start, | ||
j=UNICODE_PRIVATE_USE_AREA.end; i<j; i++) { | ||
if(-1 === usedCodePoints.indexOf(i)) { | ||
glyph.codepoint = i.toString(16); | ||
glyph.character = '&#x' + i.toString(16).toUpperCase() + ';'; | ||
usedCodePoints.push(i); | ||
if(options.appendCodepoints) { | ||
Fs.rename(glyph.file, Path.dirname(glyph.file) + '/' | ||
+ 'u' + i.toString(16).toUpperCase() + '-' + glyph.name + '.svg', | ||
function(err) { | ||
if(err) { | ||
error("Could not save codepoint: " + 'u' | ||
+ i.toString(16).toUpperCase() +' for ' + glyph.name + '.svg'); | ||
} else { | ||
log("Saved codepoint: " + 'u' + i.toString(16).toUpperCase() | ||
+' for ' + glyph.name + '.svg'); | ||
} | ||
}); | ||
} | ||
break; | ||
} | ||
} | ||
if('string' !== typeof glyph.name) { | ||
throw Error('Please provide a name for the glyph at index ' + index); | ||
} | ||
if(glyphs.some(function(g) { | ||
return (g !== glyph && g.name === glyph.name); | ||
})) { | ||
throw Error('The glyph name "' + glyph.name + '" must be unique.'); | ||
} | ||
if('number' !== typeof glyph.codepoint) { | ||
throw Error('Please provide a codepoint for the glyph "' + glyph.name + '"'); | ||
} | ||
if(glyphs.some(function(g) { | ||
return (g !== glyph && g.codepoint === glyph.codepoint); | ||
})) { | ||
throw Error('The glyph "' + glyph.name | ||
+ '" codepoint seems to be used already elsewhere.'); | ||
} | ||
glyph.running = true; | ||
glyph.d = []; | ||
glyph.stream.pipe(saxStream); | ||
}); | ||
@@ -207,0 +174,0 @@ return outputStream; |
var assert = require('assert') | ||
, svgicons2svgfont = require(__dirname + '/../src/index.js') | ||
, Fs = require('fs') | ||
, StringDecoder = require('string_decoder').StringDecoder; | ||
, StringDecoder = require('string_decoder').StringDecoder | ||
, Path = require("path"); | ||
// 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); | ||
var codepoint = 0xE001 | ||
, dest = __dirname + '/results/' + options.fontName + '.svg' | ||
, stream = svgicons2svgfont(Fs.readdirSync(__dirname + '/fixtures/' + options.fontName) | ||
.map(function(file) { | ||
var matches = file.match(/^(?:u([0-9a-f]{4})\-)?(.*).svg$/i); | ||
return { | ||
codepoint: (matches[1] ? parseInt(matches[1], 16) : codepoint++), | ||
name: matches[2], | ||
stream: Fs.createReadStream(__dirname + '/fixtures/' + options.fontName + '/' + file) | ||
}; | ||
}), options); | ||
stream.pipe(Fs.createWriteStream(dest)).on('finish', function() { | ||
@@ -27,7 +33,13 @@ assert.equal( | ||
var content = '' | ||
, decoder = new StringDecoder('utf8'); | ||
var stream = svgicons2svgfont(Fs.readdirSync(__dirname + '/fixtures/' + options.fontName) | ||
.map(function(file) { | ||
return __dirname + '/fixtures/' + options.fontName + '/' + file; | ||
}), options); | ||
, decoder = new StringDecoder('utf8') | ||
, codepoint = 0xE001 | ||
, stream = svgicons2svgfont(Fs.readdirSync(__dirname + '/fixtures/' + options.fontName) | ||
.map(function(file) { | ||
var matches = file.match(/^(?:u([0-9a-f]{4})\-)?(.*).svg$/i); | ||
return { | ||
codepoint: (matches[1] ? parseInt(matches[1], 16) : codepoint++), | ||
name: matches[2], | ||
stream: Fs.createReadStream(__dirname + '/fixtures/' + options.fontName + '/' + file) | ||
}; | ||
}), options); | ||
stream.on('data', function(chunk) { | ||
@@ -141,1 +153,35 @@ content += decoder.write(chunk); | ||
}); | ||
describe('Providing bad glyphs', function() { | ||
it("should fail when not providing glyph name", function() { | ||
var hadError = false; | ||
try { | ||
svgicons2svgfont([{ | ||
stream: Fs.createReadStream('/dev/null'), | ||
codepoint: 0xE001 | ||
}]); | ||
} catch(err) { | ||
assert.equal(err instanceof Error, true); | ||
assert.equal(err.message, 'Please provide a name for the glyph at index 0'); | ||
hadError = true; | ||
} | ||
assert.equal(hadError, true); | ||
}); | ||
it("should fail when not providing codepoints", function() { | ||
var hadError = false; | ||
try { | ||
svgicons2svgfont([{ | ||
stream: Fs.createReadStream('/dev/null'), | ||
name: 'test' | ||
}]); | ||
} catch(err) { | ||
assert.equal(err instanceof Error, true); | ||
assert.equal(err.message, 'Please provide a codepoint for the glyph "test"'); | ||
hadError = true; | ||
} | ||
assert.equal(hadError, true); | ||
}); | ||
}); |
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
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
47643
336