1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | 1 5 7 1 128 7 14 128 1 128 128 270 5 128 1 375 761 24 737 2 735 2 733 371 371 4 1 4 4 4 4 4 4 4 1 2 1 2 1 1 8 8 8 8 8 1 5 1 1 1 1 1 1 72 72 72 72 72 72 72 72 72 72 72 72 36 36 36 36 36 118 118 118 118 118 118 1 118 347 1 118 112 113 116 1 6 1 118 347 1 118 375 375 32 343 116 116 116 115 115 115 115 115 116 113 116 113 116 1 1 1 227 1 226 4 222 2 2 220 2 2 218 5 213 8 205 107 118 375 118 118 118 36 31 106 31 94 31 96 2 31 31 111 111 17 111 10 10 4 111 121 111 11 11 11 115 115 115 115 11 111 111 111 112 113 31 31 31 31 1 | /* * svgicons2svgfont * https://github.com/nfroidure/svgicons2svgfont * * Copyright (c) 2013-2015 Nicolas Froidure and contributors * Licensed under the MIT license. */ "use strict"; // Transform helpers (will move elsewhere later) function parseTransforms(value) { return value.match( /(rotate|translate|scale|skewX|skewY|matrix)\s*\(([^\)]*)\)\s*/g ).map(function(transform) { return transform.match(/[\w\.\-]+/g); }); } function transformPath(path, transforms) { transforms.forEach(function(transform) { path[transform[0]].apply(path, transform.slice(1).map(function(n) { return parseFloat(n, 10); })); }); return path; } function applyTransforms(d, parents) { var transforms = []; parents.forEach(function(parent) { if('undefined' !== typeof parent.attributes.transform) { transforms = transforms.concat(parseTransforms(parent.attributes.transform)); } }); return transformPath(new SVGPathData(d), transforms).encode(); } // Rendering function tagShouldRender(tag, parents) { return !parents.some(function(tag) { if('undefined' !== typeof tag.attributes.display && 'none' == tag.attributes.display.toLowerCase()) { return true; } if('undefined' !== typeof tag.attributes.width && 0 === parseFloat(tag.attributes.width, 0)) { return true; } if('undefined' !== typeof tag.attributes.height && 0 === parseFloat(tag.attributes.height, 0)) { return true; } if('undefined' !== typeof tag.attributes.viewBox) { var values = tag.attributes.viewBox.split(/\s*,*\s|\s,*\s*|,/); if(0 === parseFloat(values[2]) || 0 === parseFloat(values[3])) { return true; } } }); } // Shapes helpers (should also move elsewhere) function rectToPath(attributes) { var x = 'undefined' !== typeof attributes.x ? parseFloat(attributes.x, 10) : 0; var y = 'undefined' !== typeof attributes.y ? parseFloat(attributes.y, 10) : 0; var width = 'undefined' !== typeof attributes.width ? parseFloat(attributes.width, 10) : 0; var height = 'undefined' !== typeof attributes.height ? parseFloat(attributes.height, 10) : 0; var rx = 'undefined' !== typeof attributes.rx ? parseFloat(attributes.rx, 10) : 0; var ry = 'undefined' !== typeof attributes.ry ? parseFloat(attributes.ry, 10) : 0; return '' + // start at the left corner 'M' + (x + rx) + ' ' + y + // top line 'h' + (width - (rx * 2)) + // upper right corner ( rx || ry ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx + ' ' + ry : '' ) + // Draw right side 'v' + (height - (ry * 2)) + // Draw bottom right corner ( rx || ry ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + (rx * -1) + ' ' + ry : '' ) + // Down the down side 'h' + ((width - (rx * 2)) * -1) + // Draw bottom right corner ( rx || ry ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + (rx * -1) + ' ' + (ry * -1) : '' ) + // Down the left side 'v' + ((height - (ry * 2)) * -1) + // Draw bottom right corner ( rx || ry ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx + ' ' + (ry * -1) : '' ) + // Close path 'z'; } function polylineToPath(attributes) { return 'M' + attributes.points; } function lineToPath(attributes) { // Move to the line start return '' + 'M' + (parseFloat(attributes.x1,10)||0).toString(10) + ' ' + (parseFloat(attributes.y1,10)||0).toString(10) + ' ' + ((parseFloat(attributes.x1,10)||0)+1).toString(10) + ' ' + ((parseFloat(attributes.y1,10)||0)+1).toString(10) + ' ' + ((parseFloat(attributes.x2,10)||0)+1).toString(10) + ' ' + ((parseFloat(attributes.y2,10)||0)+1).toString(10) + ' ' + (parseFloat(attributes.x2,10)||0).toString(10) + ' ' + (parseFloat(attributes.y2,10)||0).toString(10) + 'Z'; } // http://www.whizkidtech.redprince.net/bezier/circle/ var KAPPA = ((Math.sqrt(2)-1)/3)*4; function circleToPath(attributes) { var cx = parseFloat(attributes.cx, 10); var cy = parseFloat(attributes.cy, 10); var rx = 'undefined' !== typeof attributes.rx ? parseFloat(attributes.rx, 10) : parseFloat(attributes.r, 10); var ry = 'undefined' !== typeof attributes.ry ? parseFloat(attributes.ry, 10) : parseFloat(attributes.r, 10); return '' + 'M' + (cx - rx) + ',' + cy + 'C' + (cx - rx) + ',' + (cy + ry*KAPPA) + ' ' + (cx - rx*KAPPA) + ',' + (cy + ry) + ' ' + cx + ',' + (cy + ry) + 'C' + (cx + rx*KAPPA) + ',' + (cy+ry) + ' ' + (cx + rx) + ',' + (cy + ry*KAPPA) + ' ' + (cx + rx) + ',' + cy + 'C' + (cx + rx) + ',' + (cy - ry*KAPPA) + ' ' + (cx + rx*KAPPA) + ',' + (cy - ry) + ' ' + cx + ',' + (cy - ry) + 'C' + (cx - rx*KAPPA) + ',' + (cy - ry) + ' ' + (cx - rx) + ',' + (cy - ry*KAPPA) + ' ' + (cx - rx) + ',' + cy + 'Z'; } function polygonToPath(attributes) { return 'M' + attributes.points + 'Z'; } // Required modules var util = require("util"); var Stream = require("readable-stream"); var Sax = require("sax"); var SVGPathData = require("svg-pathdata"); // Inherit of duplex stream util.inherits(SVGIcons2SVGFontStream, Stream.Transform); // Constructor function SVGIcons2SVGFontStream(options) { var _this = this; var glyphs = []; var log; var error; options = options || {}; options.fontName = options.fontName || 'iconfont'; options.fixedWidth = options.fixedWidth || false; options.descent = options.descent || 0; options.round = options.round || 10e12; log = (options.log || console.log.bind(console)); error = options.error || console.error.bind(console); // Ensure new were used if(!(this instanceof SVGIcons2SVGFontStream)) { return new SVGIcons2SVGFontStream(options); } // Parent constructor Stream.Transform.call(this, { objectMode: true }); // Setting objectMode separately this._writableState.objectMode = true; this._readableState.objectMode = false; // Parse input this._transform = function _svgIcons2SVGFontStreamTransform( svgIconStream, unused, svgIconStreamCallback ) { // Parsing each icons asynchronously var saxStream = Sax.createStream(true); var parents = []; var glyph = svgIconStream.metadata || {}; glyph.d = []; glyphs.push(glyph); if('string' !== typeof glyph.name) { _this.emit('error', new Error('Please provide a name for the glyph at' + ' index ' + (glyphs.length - 1))); } if(glyphs.some(function(anotherGlyph) { return (anotherGlyph !== glyph && anotherGlyph.name === glyph.name); })) { _this.emit('error', new Error('The glyph name "' + glyph.name + '" must be unique.')); } if(glyph.unicode && glyph.unicode instanceof Array && glyph.unicode.length) { if(glyph.unicode.some(function(unicodeA, i) { return glyph.unicode.some(function(unicodeB, j) { return i !== j && unicodeA === unicodeB; }); })) { _this.emit('error', new Error('Given codepoints for the glyph "' + glyph.name + '" contain duplicates.')); } } else if('string' !== typeof glyph.unicode) { _this.emit('error', new Error('Please provide a codepoint for the glyph "' + glyph.name + '"')); } if(glyphs.some(function(anotherGlyph) { return (anotherGlyph !== glyph && anotherGlyph.unicode === glyph.unicode); })) { _this.emit('error', new Error('The glyph "' + glyph.name + '" codepoint seems to be used already elsewhere.')); } saxStream.on('opentag', function(tag) { parents.push(tag); // Checking if any parent rendering is disabled and exit if so if(!tagShouldRender(tag, parents)) { return; } // Save the view size if('svg' === tag.name) { glyph.dX = 0; glyph.dY = 0; if('viewBox' in tag.attributes) { var values = tag.attributes.viewBox.split(/\s*,*\s|\s,*\s*|,/); glyph.dX = parseFloat(values[0], 10); glyph.dY = parseFloat(values[1], 10); glyph.width = parseFloat(values[2], 10); glyph.height = parseFloat(values[3], 10); } if('width' in tag.attributes) { glyph.width = parseFloat(tag.attributes.width, 10); } if('height' in tag.attributes) { glyph.height = parseFloat(tag.attributes.height, 10); } if(!glyph.width || !glyph.height) { log('Glyph "' + glyph.name + '" has no size attribute on which to' + ' get the gylph dimensions (heigh and width or viewBox' + ' attributes)'); glyph.width = 150; glyph.height = 150; } // Clipping path unsupported } else if('clipPath' === tag.name) { log('Found a clipPath element in the icon "' + glyph.name + '" the' + 'result may be different than expected.'); // Change rect elements to the corresponding path } else if('rect' === tag.name && 'none' !== tag.attributes.fill) { glyph.d.push(applyTransforms(rectToPath(tag.attributes), parents)); } else if('line' === tag.name && 'none' !== tag.attributes.fill) { log('Found a line element in the icon "' + glyph.name + '" the result' + ' could be different than expected.'); glyph.d.push(applyTransforms(lineToPath(tag.attributes), parents)); } else if('polyline' === tag.name && 'none' !== tag.attributes.fill) { log('Found a polyline element in the icon "' + glyph.name + '" the' + ' result could be different than expected.'); glyph.d.push(applyTransforms(polylineToPath(tag.attributes), parents)); } else if('polygon' === tag.name && 'none' !== tag.attributes.fill) { glyph.d.push(applyTransforms(polygonToPath(tag.attributes), parents)); } else if('circle' === tag.name || 'ellipse' === tag.name && 'none' !== tag.attributes.fill) { glyph.d.push(applyTransforms(circleToPath(tag.attributes), parents)); } else if('path' === tag.name && tag.attributes.d && 'none' !== tag.attributes.fill) { glyph.d.push(applyTransforms(tag.attributes.d, parents)); } }); saxStream.on('closetag', function(tag) { parents.pop(); }); saxStream.on('end', function() { svgIconStreamCallback(); }); svgIconStream.pipe(saxStream); }; // Output data this._flush = function _svgIcons2SVGFontStreamFlush(svgFontFlushCallback) { var fontWidth = ( glyphs.length > 1 ? glyphs.reduce(function (curMax, glyph) { return Math.max(curMax, glyph.width); }, 0) : glyphs[0].width); var fontHeight = options.fontHeight || ( 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 (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' + ' to unexpected results. Using the normalize option could' + ' solve the problem.'); } // Output the SVG file // (find a SAX parser that allows modifying SVG on the fly) _this.push('\ <?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\ <svg xmlns="http://www.w3.org/2000/svg">\n\ <defs>\n\ <font id="' + options.fontName + '" horiz-adv-x="' + fontWidth + '">\n\ <font-face font-family="' + options.fontName + '"\n\ units-per-em="' + fontHeight + '" ascent="' + (fontHeight - options.descent) + '"\n\ descent="' + options.descent + '" />\n\ <missing-glyph horiz-adv-x="0" />\n'); glyphs.forEach(function(glyph) { var ratio = fontHeight / glyph.height , d = ''; if(options.fixedWidth) { glyph.width = fontWidth; } if(options.normalize) { glyph.height = fontHeight; if(!options.fixedWidth) { glyph.width *= ratio; } } glyph.d.forEach(function(cD) { d+=' '+new SVGPathData(cD) .toAbs() .translate(-glyph.dX, -glyph.dY) .scale( options.normalize ? ratio : 1, options.normalize ? ratio : 1) .ySymetry(glyph.height - options.descent) .round(options.round) .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) .round(options.round) .encode(); } delete glyph.d; delete glyph.running; glyph.unicode.forEach(function(unicode, i){ _this.push('\ <glyph glyph-name="' + glyph.name + (i == 0 ? '' : '-' + i) + '"\n\ unicode="' + unicode.split('').map(function(char) { return '&#x' + char.charCodeAt(0).toString(16).toUpperCase() + ';'; }).join('') + '"\n\ horiz-adv-x="' + glyph.width + '" d="' + d +'" />\n'); }); }); _this.push('\ </font>\n\ </defs>\n\ </svg>\n'); log("Font created"); 'function' === (typeof options.callback) && (options.callback)(glyphs); svgFontFlushCallback(); }; } module.exports = SVGIcons2SVGFontStream; |