@@ -3,20 +3,25 @@ var svgicons2svgfont = require('svgicons2svgfont');
33var svg2ttf = require ( 'svg2ttf' ) ;
44var ttf2woff = require ( 'ttf2woff' ) ;
55var Readable = require ( 'stream' ) . Readable ;
6- var fs = require ( 'fs' ) ;
76
8- module . exports = function createIconFont ( icons , options ) {
7+ /**
8+ * @param {InputFileSystem } fs An input file system
9+ * @param {String[] } icons Array of icon file paths
10+ * @param {Object } options SVG-Font options
11+ * @return {Promise<String> } Base64 encoded font
12+ */
13+ module . exports = function createIconFont ( fs , icons , options ) {
914 assert ( typeof options === 'object' , 'Options are mandatory.' ) ;
15+
1016 return new Promise ( ( resolve , reject ) => {
1117 var fontStream = svgicons2svgfont ( {
1218 name : options . name ,
13- normalize : true ,
14- fontHeight : 512 ,
1519 log : function ( ) { } ,
1620 error : function ( err ) {
1721 reject ( err ) ;
1822 }
1923 } ) ;
24+ var fontIconHeight ;
2025 var svgFont = '' ;
2126 fontStream
2227 . on ( 'finish' , function ( ) {
@@ -36,10 +41,17 @@ module.exports = function createIconFont (icons, options) {
3641 name : 'i' + i
3742 } ;
3843 fontStream . write ( glyph ) ;
39- fs . readFile ( filename , 'utf8' , function ( err , result ) {
44+ fs . readFile ( filename , function ( err , result ) {
45+ result = result . toString ( ) ;
4046 if ( err ) {
4147 return reject ( err ) ;
4248 }
49+ var iconHeight = getSvgHeight ( result , filename ) ;
50+ if ( fontIconHeight && fontIconHeight !== iconHeight ) {
51+ return reject ( `SVG font generation failed as not all icons have the same height. ` +
52+ `Found: "${ fontIconHeight } " and "${ iconHeight } ".` ) ;
53+ }
54+ fontIconHeight = iconHeight ;
4355 // prevent svgs with fill="none" from beeing translated into an empty symbol
4456 result = result . replace ( / \s f i l l \s * = \s * [ " ' ] ? n o n e [ ' " ] ? / ig, '' ) ;
4557 glyph . push ( result ) ;
@@ -52,3 +64,18 @@ module.exports = function createIconFont (icons, options) {
5264 . then ( ( ttfFont ) => ttf2woff ( ttfFont ) . buffer )
5365 . then ( ( woffFont ) => new Buffer ( woffFont ) . toString ( 'base64' ) ) ;
5466} ;
67+
68+ /**
69+ * Reads the height of the svg
70+ *
71+ * @param {String } svg the svg content
72+ * @param {String } filename the file name for error reporting
73+ * @return {Number } height
74+ */
75+ function getSvgHeight ( svg , filename ) {
76+ const parseSvg = / < s v g [ ^ > ] + h e i g h t \s * = \s * [ " ' ] ? ( \d + ) \s * ( p t | ) [ " ' ] ? / i. exec ( svg ) ;
77+ if ( ! parseSvg ) {
78+ throw new Error ( `could not read height for '${ filename } '.` ) ;
79+ }
80+ return parseSvg [ 1 ] ;
81+ }
0 commit comments