svgicons2svgfont
Advanced tools
Comparing version 0.0.12 to 0.1.0
{ | ||
"name": "svgicons2svgfont", | ||
"version": "0.0.12", | ||
"version": "0.1.0", | ||
"description": "Read a set of SVG icons and ouput a SVG font", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/nfroidure/svgicons2svgfont", |
@@ -9,4 +9,11 @@ # svgicons2svgfont | ||
Transform attributes support is currenly experimental, report issues if any. | ||
Transform attributes support is currenly experimental, | ||
[report issues if any](https://github.com/nfroidure/svgicons2svgfont/issues/6). | ||
You can test this library with the | ||
[frontend generator](http://nfroidure.github.io/svgiconfont/). | ||
You may want to convert fonts to icons, if so use | ||
[svgfont2svgicons](https://github.com/nfroidure/svgifont2svgicons). | ||
## Usage | ||
@@ -34,20 +41,37 @@ NodeJS module: | ||
## Options (not plugged to CLI yet) | ||
## API | ||
### fontName | ||
### svgicons2svgfont(options) | ||
#### options.fontName | ||
Type: `String` | ||
Default value: `'iconfont'` | ||
The font family name you want. | ||
### fixedWidth | ||
#### options.fixedWidth | ||
Type: `Boolean` | ||
Default value: `false` | ||
Creates a monospace font of the width of the largest input icon. | ||
### normalize | ||
#### options.centerHorizontally | ||
Type: `Boolean` | ||
Default value: `false` | ||
Calculate the bounds of a glyph and center it horizontally. | ||
**Warning:** The bounds calculation is currently a naive implementation that | ||
may not work for some icons. We need to create a svg-pathdata-draw module on | ||
top of svg-pathdata to get the real bounds of the icon. It's in on the bottom | ||
of my to do, but feel free to work on it. Discuss it in the | ||
[related issue](https://github.com/nfroidure/svgicons2svgfont/issues/18). | ||
#### options.normalize | ||
Type: `Boolean` | ||
Default value: `false` | ||
Normalize icons by scaling them to the height of the highest icon. | ||
### fontHeight | ||
#### options.fontHeight | ||
Type: `Number` | ||
@@ -57,16 +81,27 @@ Default value: `MAX(icons.height)` | ||
### descent | ||
#### options.descent | ||
Type: `Number` | ||
Default value: `0` | ||
The font descent. It is usefull to fix the font baseline yourself. | ||
**Warning:** The descent is a positive value! | ||
The ascent formula is: ascent = fontHeight - descent. | ||
#### options.log | ||
Type: `Function` | ||
Default value: `false` | ||
## Grunt plugins | ||
Allows you to provide your own logging function. Set to `function(){}` to | ||
impeach logging. | ||
## Build systems | ||
### Grunt plugins | ||
[grunt-svgicons2svgfont](https://github.com/nfroidure/grunt-svgicons2svgfont) | ||
and [grunt-webfont](https://github.com/nfroidure/grunt-webfont). | ||
and [grunt-webfont](https://github.com/sapegin/grunt-webfont). | ||
## Gulp plugins | ||
### Gulp plugins | ||
@@ -76,2 +111,6 @@ Try [gulp-iconfont](https://github.com/nfroidure/gulp-iconfont) and | ||
### Mimosa plugin | ||
[https://www.npmjs.org/package/mimosa-svgs-to-iconfonts](mimosa-svgs-to-iconfonts) | ||
## Stats | ||
@@ -83,3 +122,3 @@ | ||
## Contributing | ||
Feel free to pull your code if you agree with publishing under the MIT license. | ||
Feel free to push your code if you agree with publishing under the MIT license. | ||
@@ -184,13 +184,13 @@ /* | ||
})) { | ||
var fontWidth = (glyphs.length > 1 ? glyphs.reduce(function (gA, gB) { | ||
return Math.max(gA.width || gA, gB.width || gB); | ||
}) : glyphs[0].width) | ||
var fontWidth = (glyphs.length > 1 ? glyphs.reduce(function (curMax, glyph) { | ||
return Math.max(curMax, glyph.width); | ||
}, 0) : glyphs[0].width) | ||
, fontHeight = options.fontHeight || | ||
(glyphs.length > 1 ? glyphs.reduce(function (gA, gB) { | ||
return Math.max(gA.height || gA, gB.height || gB); | ||
}) : glyphs[0].height); | ||
(glyphs.length > 1 ? glyphs.reduce(function (curMax, glyph) { | ||
return Math.max(curMax, glyph.height); | ||
}, 0) : glyphs[0].height); | ||
if((!options.normalize) | ||
&& fontHeight>(glyphs.length > 1 ? glyphs.reduce(function (gA, gB) { | ||
return Math.min(gA.height || gA, gB.height || gB); | ||
}) : glyphs[0].height)) { | ||
&& fontHeight>(glyphs.length > 1 ? glyphs.reduce(function (curMin, glyph) { | ||
return Math.min(curMin, glyph.height); | ||
}, Infinity) : glyphs[0].height)) { | ||
log('The provided icons does not have the same height it could lead' | ||
@@ -208,3 +208,3 @@ +' to unexpected results. Using the normalize option could' | ||
<font-face font-family="' + options.fontName + '"\n\ | ||
units-per-em="' + fontHeight + '" ascent="' + (fontHeight + options.descent) + '"\n\ | ||
units-per-em="' + fontHeight + '" ascent="' + (fontHeight - options.descent) + '"\n\ | ||
descent="' + options.descent + '" />\n\ | ||
@@ -215,2 +215,5 @@ <missing-glyph horiz-adv-x="0" />\n'); | ||
, d = ''; | ||
if(options.fixedWidth) { | ||
glyph.width = fontWidth; | ||
} | ||
if(options.normalize) { | ||
@@ -227,5 +230,24 @@ glyph.height = fontHeight; | ||
options.normalize ? ratio : 1) | ||
.ySymetry(glyph.height) | ||
.ySymetry(glyph.height - options.descent) | ||
.encode(); | ||
}); | ||
if(options.centerHorizontally) { | ||
// Naive bounds calculation (should draw, then calculate bounds...) | ||
var pathData = new SVGPathData(d); | ||
var bounds = { | ||
x1:Infinity, | ||
y1:Infinity, | ||
x2:0, | ||
y2:0 | ||
}; | ||
pathData.toAbs().commands.forEach(function(command) { | ||
bounds.x1 = 'undefined' != typeof command.x && command.x < bounds.x1 ? command.x : bounds.x1; | ||
bounds.y1 = 'undefined' != typeof command.y && command.y < bounds.y1 ? command.y : bounds.y1; | ||
bounds.x2 = 'undefined' != typeof command.x && command.x > bounds.x2 ? command.x : bounds.x2; | ||
bounds.y2 = 'undefined' != typeof command.y && command.y > bounds.y2 ? command.y : bounds.y2; | ||
}); | ||
d = pathData | ||
.translate(((glyph.width - (bounds.x2 - bounds.x1)) / 2) - bounds.x1) | ||
.encode(); | ||
} | ||
delete glyph.d; | ||
@@ -236,3 +258,3 @@ delete glyph.running; | ||
unicode="&#x' + (glyph.codepoint.toString(16)).toUpperCase() + ';"\n\ | ||
horiz-adv-x="' + (options.fixedWidth ? fontWidth : glyph.width) + '" d="' + d +'" />\n'); | ||
horiz-adv-x="' + glyph.width + '" d="' + d +'" />\n'); | ||
}); | ||
@@ -244,3 +266,3 @@ outputStream.write('\ | ||
outputStream.on('finish', function() { | ||
log("Font created"); | ||
log("Font created!"); | ||
'function' === (typeof options.callback) && (options.callback)(glyphs); | ||
@@ -247,0 +269,0 @@ }); |
@@ -68,2 +68,10 @@ var assert = require('assert') | ||
it("should work for simple fixedWidth and normalize option", function(done) { | ||
generateFontToFile({ | ||
fontName: 'originalicons', | ||
fixedWidth: true, | ||
normalize: true | ||
}, done, 'n'); | ||
}); | ||
it("should work for simple SVG", function(done) { | ||
@@ -107,2 +115,16 @@ generateFontToFile({ | ||
it("should work with variable width icons", function(done) { | ||
generateFontToFile({ | ||
fontName: 'variablewidthicons' | ||
}, done); | ||
}); | ||
it("should work with centered variable width icons and the fixed width option", function(done) { | ||
generateFontToFile({ | ||
fontName: 'variablewidthicons', | ||
fixedWidth: true, | ||
centerHorizontally: true | ||
}, done, 'n'); | ||
}); | ||
it("should not display hidden pathes", function(done) { | ||
@@ -138,2 +160,9 @@ generateFontToFile({ | ||
it("should work when horizontally centering SVG icons", function(done) { | ||
generateFontToFile({ | ||
fontName: 'tocentericons', | ||
centerHorizontally: true | ||
}, done); | ||
}); | ||
}); | ||
@@ -194,3 +223,3 @@ | ||
fontName: 'originalicons', | ||
descent: -200 | ||
descent: 200 | ||
}, done, '4'); | ||
@@ -197,0 +226,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
127142
82
582
120