diff --git a/src/commands.js b/src/commands.js index cde272a..5e55bd7 100644 --- a/src/commands.js +++ b/src/commands.js @@ -1,29 +1,76 @@ -const which = require('which').sync; -const isLinux = require('./helpers.js').isLinux(); - -const FontFaceException = require('./exception.js'); - -const commands = {}; -const check = [ - 'fontforge', -]; -let missing = []; - -check.forEach(cmd => { - try { - commands[cmd] = which(cmd); - } catch (e) { - missing = [...missing, cmd]; - } -}); - -if (missing.length) { - const installCmd = isLinux ? 'sudo apt-get install' : 'brew install'; - - throw new FontFaceException( - 'We are missing some required font packages.\n' + - 'That can be installed with:\n' + - `${installCmd} ${missing.join(' ')}`); -} - -module.exports = commands; +'use strict'; + +var which = require('which').sync; +var exec = require('child_process').execSync; +var isLinux = require('./helpers.js').isLinux(); +var isWindows = require('./helpers.js').isWindows(); + + +var FontFaceException = require('./exception.js'); + +var commands = {}; +var done = false; + +module.exports = function() { + + if (done) { + return commands; + } + done = true; + + var check = { + fontforge: {}, + ttf2eot: {} + }; + + if (isLinux) { + check.ttf2svg = {}; + } else { + check['batik-ttf2svg'] = {}; + } + + var missing = []; + + for (var cmd in check) { + if (check.hasOwnProperty(cmd)) { + + + try { + if (isWindows) { + commands[cmd] = '"' + which(cmd) + '"'; + } + else { + commands[cmd] = which(cmd); + } + } catch(e) { + missing.push(cmd); + } + } + } + + if (missing.length) { + if (isLinux) { + var errNPM = [], errAPT = []; + missing.forEach(function(cmd){ + if (cmd.indexOf("ttf2") != -1) { + errNPM.push(cmd); + } else{ + errAPT.push(cmd); + } + }); + + throw new FontFaceException( + 'We are missing some required font packages.\n' + + 'That can be installed with:\n' + + (errAPT.length ? 'sudo apt-get install ' + errAPT.join(' ') + '\n' : '') + + (errAPT.length && errNPM.length ? 'and' : '') + + (errNPM.length ? 'sudo npm install -g ' + errNPM.join(' ') : '')); + } else { + throw new FontFaceException( + 'We are missing some required font packages.\n' + + 'That can be installed with:\n' + + 'brew install ' + missing.join(' ')); + } + } + return commands; +}() diff --git a/src/configure.js b/src/configure.js index 24a3bdd..1c78f65 100644 --- a/src/configure.js +++ b/src/configure.js @@ -1,37 +1,48 @@ -const path = require('path'); -const fs = require('fs'); -const merge = require('./helpers.js').merge; -const fontforge = require('./fontforge.js'); +'use strict'; -module.exports = options => { - const _ = { - source: options.source, - dest_dir: options.dest, - collate: options.collate || false, - }; +var path = require('path'); +var fs = require('fs'); +var merge = require('./helpers.js').merge; +var fontforge = require('./fontforge.js'); - _.extension = path.extname(_.source); - _.basename = path.basename(_.source, _.extension); - _.dest_dir = _.collate ? path.join(_.dest_dir, _.basename) : _.dest_dir; - _.target = path.join(_.dest_dir, _.basename); - _.config_file = `${_.source.replace(_.extension, '')}.json`; - _.ttf = [_.target, '.ttf'].join(''); - _.eot = [_.target, '.eot'].join(''); - _.svg = [_.target, '.svg'].join(''); - _.woff = [_.target, '.woff'].join(''); - _.woff2 = [_.target, '.woff2'].join(''); - _.css = [_.target, '.css'].join(''); - _.css_fontpath = ''; - _.name = fontforge.getName(_.source); - _.weight = fontforge.getWeight(_.source); - _.style = fontforge.getStyle(_.source); - _.embed = []; +var resolve = require('path').resolve +var isWindows = require('./helpers.js').isWindows() - if (fs.existsSync(_.config_file)) { - merge(_, JSON.parse(fs.readFileSync(_.config_file))); - } +module.exports = function(options) { + + const _ = { + source: options.source, + dest_dir: options.dest, + collate: options.collate || false, + css_fontpath : options.css_fontpath + }; + + _.extension = path.extname(_.source); + _.basename = path.basename(_.source, _.extension); + _.dest_dir = _.collate ? path.join(_.dest_dir, _.basename) : _.dest_dir; + _.target = path.join(_.dest_dir, _.basename); + if (isWindows) { + _.target = _.target.replace(/\\/g,'/'); + _.target = resolve(_.target); + } + _.config_file = _.source.replace(_.extension, '') + '.json'; + _.ttf = [_.target, '.ttf'].join(''); + _.eot = [_.target, '.eot'].join(''); + _.svg = [_.target, '.svg'].join(''); + _.woff = [_.target, '.woff'].join(''); + _.woff2 = [_.target, '.woff2'].join(''); + _.css = [_.target, '.css'].join(''); + _.css_fontpath = ''; + _.name = fontforge.getName(_.source); + _.weight = fontforge.getWeight(_.source); + _.style = fontforge.getStyle(_.source); + _.embed = []; + + if (fs.existsSync(_.config_file)) { + merge(_, JSON.parse(fs.readFileSync(_.config_file))); + } - merge(_, options); + merge(_, options); - return _; -}; + return _; +} diff --git a/src/encode.js b/src/encode.js index 1ac5b17..aaef0b6 100644 --- a/src/encode.js +++ b/src/encode.js @@ -1,28 +1,32 @@ -const fs = require('fs'); -const path = require('path'); -const { removeNewLines } = require('./helpers'); +'use strict'; + +var fs = require('fs'); +var path = require('path'); +var removeNewLines = require('./helpers').removeNewLines; // Encode font file to data:uri and *remove* source file. -function encode(fontFile) { - const dataUri = fs.readFileSync(fontFile, 'base64'); // Convert to data:uri - const type = path.extname(fontFile).substring(1); - const fontUrl = `'data:application/x-font-${type};charset=utf-8;base64,${dataUri}'`; +module.exports = function (fontFile) { + var dataUri, type, fontUrl; + + // Convert to data:uri + dataUri = fs.readFileSync(fontFile, 'base64'); + type = path.extname(fontFile).substring(1); + fontUrl = '\'data:application/x-font-' + type + ';charset=utf-8;base64,' + dataUri + '\''; - // Remove source file - fs.unlinkSync(fontFile); + // Remove source file + fs.unlinkSync(fontFile); - return fontUrl; + return fontUrl; } -function svg(fontFile) { - const dataUri = removeNewLines(fs.readFileSync(fontFile)); - const fontUrl = `'data:image/svg+xml;charset=utf8,${dataUri}'`; +module.exports.svg = function(fontFile) { + var dataUri, fontUrl; - // Remove source file - fs.unlinkSync(fontFile); + dataUri = removeNewLines(fs.readFileSync(fontFile)); + fontUrl = '\'data:image/svg+xml;charset=utf8,' + dataUri + '\''; - return fontUrl; -} + // Remove source file + fs.unlinkSync(fontFile); -module.exports = encode; -module.exports.svg = svg; + return fontUrl; +} \ No newline at end of file diff --git a/src/exception.js b/src/exception.js index 822910f..bc8a4e3 100644 --- a/src/exception.js +++ b/src/exception.js @@ -1,4 +1,6 @@ -module.exports = function FontFaceException(message) { - this.message = message; - this.name = 'FontFaceException'; +'use strict'; + +module.exports = function(message) { + this.message = message; + this.name = "FontFaceException"; }; diff --git a/src/fontfacegen.js b/src/fontfacegen.js index 265c244..3ed1b63 100644 --- a/src/fontfacegen.js +++ b/src/fontfacegen.js @@ -1,34 +1,39 @@ -const path = require('path'); -const mkdirp = require('mkdirp').sync; -const configure = require('./configure.js'); -const ttf = require('./ttf.js'); -const ttf2woff = require('./ttf2woff.js'); -const ttf2woff2 = require('./ttf2woff2.js'); -const ttf2eot = require('./ttf2eot.js'); -const ttf2svg = require('./ttf2svg.js'); -const stylesheets = require('./stylesheets.js'); +'use strict'; -module.exports = options => { - const config = configure(options); +var - mkdirp(config.dest_dir); +path = require('path'), +mkdirp = require('mkdirp').sync, +configure = require('./configure.js'), +ttf = require('./ttf.js'), +ttf2woff = require('./ttf2woff.js'), +ttf2woff2 = require('./ttf2woff2.js'), +ttf2eot = require('./ttf2eot.js'), +ttf2svg = require('./ttf2svg.js'), +stylesheets = require('./stylesheets.js'); - if (config.css) { - mkdirp(path.dirname(config.css)); - } +module.exports = function(options) { + var config = configure(options); + + mkdirp(config.dest_dir); - if (config.less) { - mkdirp(path.dirname(config.less)); - } + if (config.css) { + mkdirp(path.dirname(config.css)); + } - if (config.scss) { - mkdirp(path.dirname(config.scss)); - } + if (config.less) { + mkdirp(path.dirname(config.less)); + } - ttf(config.source, config.ttf, config.name, config); // TODO: better options handling - ttf2eot(config.ttf, config.eot); - ttf2svg(config.ttf, config.svg); - ttf2woff(config.ttf, config.woff); - ttf2woff2(config.ttf, config.woff2); - stylesheets(config); + if (config.scss) { + config.scss = config.scss + '/' + config.basename + '.scss'; + mkdirp(path.dirname(config.scss)); + } + + ttf(config.source, config.ttf, config.name); + ttf2eot(config.ttf, config.eot); + ttf2svg(config.ttf, config.svg, config.name); + ttf2woff(config.ttf, config.woff); + ttf2woff2(config.ttf, config.woff2); + stylesheets(config); }; diff --git a/src/fontforge.js b/src/fontforge.js index c148c07..75cb159 100644 --- a/src/fontforge.js +++ b/src/fontforge.js @@ -1,77 +1,95 @@ -const execSync = require('child_process').execSync; +'use strict'; -const fontForgeCommand = require('./commands.js').fontforge; +var execSync = require('child_process').execSync; +var fs = require('fs'); +var fontForgeCommand = require('./commands.js').fontforge; +var isWindows = require('./helpers.js').isWindows(); -const weightTable = { - thin: '100', - extralight: '200', - book: '300', - light: '300', - medium: 'normal', - normal: 'normal', - demibold: '600', - semibold: '700', - bold: '700', - extrabold: '800', - black: '900', + +var weightTable = { + thin: '100', + extralight: '200', + book: '300', + light: '300', + medium: 'normal', + normal: 'normal', + demibold: '600', + semibold: '700', + bold: '700', + extrabold: '800', + black: '900' }; function FontForgeException(e, cmd) { - this.message = `FontForge command failed: ${e.toString()}\n` + - `From command: ${cmd}`; - this.name = 'FontForgeException'; + this.message = 'FontForge command failed: ' + e.toString() + '\n' + + 'From command: ' + cmd; + this.name = "FontForgeException"; } function fontforge(source, script, target, name) { - let cmd = `${fontForgeCommand} -lang=ff -c '${script}' '${source}'`; - - if (target !== undefined) { - cmd += ` '${target}'`; - } - - if (name !== undefined) { - cmd += ` '${name}'`; - } - cmd += ' 2> /dev/null'; + //var cmd = fontForgeCommand + ' -lang=ff -c \'' + script + '\' \'' + source + '\''; + var cmd = fontForgeCommand + ' -lang=ff -script build/script.pe ' + '\"' + source + '\"'; - let result; + + if (target !== undefined) { + cmd += ' \"' + target + '\"'; + } - try { - result = execSync(cmd).toString(); - } catch (e) { - throw new FontForgeException(e, cmd); - } + if (name !== undefined) { + cmd += ' \"' + name + '\"'; + } + + if(!isWindows) { + cmd += ' 2> /dev/null' + } + + var dir = "./build"; + var file = dir + "/script.pe"; + + if (!fs.existsSync(dir)){ + fs.mkdirSync(dir); + } + + fs.writeFileSync(file, script); + + var result; + //console.log(cmd); + //process.exit(); + try { + result = execSync(cmd).toString(); + } catch (e) { + throw new FontForgeException(e, cmd) + } - return result; + return result; } function getName(source) { - const result = fontforge(source, 'Open($1);Print($fontname);'); - if (result) { - return result.trim().replace(' ', '_'); - } - return false; + var result = fontforge(source, 'Open($1);Print($fontname);'); + if (result) { + return result.trim().replace(' ', '_'); + } + return false; } function getWeight(source) { - const result = fontforge(source, 'Open($1);Print($weight);'); - if (result) { - const weight = result.trim().replace(' ', '').toLowerCase(); - if (weightTable[weight]) { - return weightTable[weight]; + var result = fontforge(source, 'Open($1);Print($weight);'); + if (result) { + var weight = result.trim().replace(' ', '').toLowerCase(); + if (weightTable[weight]) + return weightTable[weight]; + return weight; } - return weight; - } - return false; + return false; } function getStyle(source) { - const result = fontforge(source, 'Open($1);Print($italicangle);'); - if (result) { - return (parseInt(result.trim(), 10) === 0) ? 'normal' : 'italic'; - } - return false; + var result = fontforge(source, 'Open($1);Print($italicangle);'); + if (result) { + return (parseInt(result.trim()) === 0) ? 'normal' : 'italic'; + } + return false; } module.exports = fontforge; diff --git a/src/helpers.js b/src/helpers.js index 3f51b5d..def3902 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1,20 +1,22 @@ -const os = require('os'); +'use strict'; + +var os = require('os'); function has(haystack, needle) { - return haystack.indexOf(needle) !== -1; + return haystack.indexOf(needle) !== -1; } function quote(str) { - return `"${str}"`; + return '"' + str + '"'; } function merge(destination, source) { - Object.keys(source).forEach(property => { - if (source.hasOwnProperty(property)) { - destination[property] = source[property]; // eslint-disable-line no-param-reassign + for (var property in source) { + if (source.hasOwnProperty(property)) { + destination[property] = source[property]; + } } - }); - return destination; + return destination; } function trim(buffer) { @@ -33,26 +35,22 @@ function removeNewLines(buffer) { return buffer.toString().replace(/\r?\n|\r/g); } -function uniqueChars(subset) { - return (typeof subset === 'string' ? subset.split('') : subset) - .filter((ch, i, chars) => chars.indexOf(ch) === i); -} - -function charToHex(ch) { - return ch.charCodeAt(0).toString(16); -} +var _isLinux = os.type().toLowerCase() == "linux"; +var _isWindows = os.type().toLowerCase() == "windows_nt"; -const _isLinux = os.type().toLowerCase() === 'linux'; function isLinux() { return _isLinux; } +function isWindows() { + return _isWindows; +} + module.exports.has = has; module.exports.quote = quote; module.exports.merge = merge; module.exports.trim = trim; module.exports.removeNewLines = removeNewLines; -module.exports.uniqueChars = uniqueChars; -module.exports.charToHex = charToHex; module.exports.isLinux = isLinux; +module.exports.isWindows = isWindows; \ No newline at end of file diff --git a/src/stylesheets.js b/src/stylesheets.js index 9752e26..f0537d3 100644 --- a/src/stylesheets.js +++ b/src/stylesheets.js @@ -1,136 +1,136 @@ -const fs = require('fs'); -const path = require('path'); -const encodeFont = require('./encode.js'); -const { has } = require('./helpers.js'); +'use strict'; + +var fs = require('fs'); +var path = require('path'); +var encodeFont = require('./encode.js'); +var has = require('./helpers.js').has; function css(stylesheet, name, filename, weight, style, woff2, woff, ttf, svg, embedSvg) { - const resultLines = [ - '@font-face {', - ` font-family: "${name}";`, - ` src: url("${filename}.eot");`, - ` src: url("${filename}.eot?#iefix") format("embedded-opentype"),`, - ` url(${woff2}) format("woff2"),`, - ` url(${woff}) format("woff"),`, - ` url(${ttf}) format("ttf"),`, - ]; - - if (embedSvg) { - resultLines.push(` url(${svg}) format("svg");`); - } else { - resultLines.push(` url("${filename}.svg#${name}") format("svg");`); - } - - resultLines.push( - ` font-style: ${style};`, - ` font-weight: ${weight};`, - '}' - ); - - const result = resultLines.join('\n'); - - fs.writeFileSync(stylesheet, result); - return result; + + var resultLines = [ + '@font-face {', + ' font-family: "' + name + '";', + ' src: url("' + filename + '.eot");', + ' src: url("' + filename + '.eot?#iefix") format("embedded-opentype"),', + ' url(' + woff2 + ') format("woff2"),', + ' url(' + woff + ') format("woff"),', + ' url(' + ttf + ') format("ttf"),' + ]; + + if (embedSvg) { + resultLines.push(' url(' + svg + ') format("svg");'); + } else { + resultLines.push(' url("' + filename + '.svg#' + name + '") format("svg");'); + } + + resultLines.push( + ' font-style: ' + style + ';', + ' font-weight: ' + weight + ';', + '}' + ) + + var result = resultLines.join('\n'); + + fs.writeFileSync(stylesheet, result); + return result; } function less(stylesheet, name, filename, weight, style, woff2, woff, ttf, svg, embedSvg) { - const resultLines = [ - '@font-face {', - ` font-family: "${name}";`, - ` src: url("${filename}.eot");`, - ` src: url("${filename}.eot?#iefix") format("embedded-opentype"),`, - ` url(${woff2}) format("woff2"),`, - ` url(${woff}) format("woff"),`, - ` url(${ttf}) format("ttf"),`, - ]; - - if (embedSvg) { - resultLines.push(` url(${svg}) format("svg");`); - } else { - resultLines.push(` url("${filename}.svg#${name}") format("svg");`); - } - - resultLines.push( - ` font-weight: ${weight};`, - ` font-style: ${style};`, - '}' - ); - - const result = resultLines.join('\n'); - - fs.writeFileSync(stylesheet, result); - return result; + var resultLines = [ + '@font-face {', + ' font-family: "' + name + '";', + ' src: url("' + filename + '.eot");', + ' src: url("' + filename + '.eot?#iefix") format("embedded-opentype"),', + ' url(' + woff2 + ') format("woff2"),', + ' url(' + woff + ') format("woff"),', + ' url(' + ttf + ') format("ttf"),' + ]; + + if (embedSvg) { + resultLines.push(' url(' + svg + ') format("svg");'); + } else { + resultLines.push(' url("' + filename + '.svg#' + name + '") format("svg");'); + } + + resultLines.push( + ' font-weight: ' + weight + ';', + ' font-style: ' + style + ';', + '}' + ); + + var result = resultLines.join('\n'); + + fs.writeFileSync(stylesheet, result); + return result; } function scss(stylesheet, name, filename, weight, style, woff2, woff, ttf, svg, embedSvg) { - const resultLines = [ - '@font-face {', - ` font-family: "${name}";`, - ` src: url("${filename}.eot");`, - ` src: url("${filename}.eot?#iefix") format("embedded-opentype"),`, - ` url(${woff2}) format("woff2"),`, - ` url(${woff}) format("woff"),`, - ` url(${ttf}) format("ttf"),`, - ]; - - if (embedSvg) { - resultLines.push(` url(${svg}) format("svg");`); - } else { - resultLines.push(` url("${filename}.svg#${name}") format("svg");`); - } - - resultLines.push( - ` url("${filename}.svg#${name}") format("svg");`, - ` font-weight: ${weight};`, - ` font-style: ${style};`, - '}' - ); - - const result = resultLines.join('\n'); - - fs.writeFileSync(stylesheet, result); - return result; + var resultLines = [ + '@font-face {', + ' font-family: "' + name + '";', + ' src: url("' + filename + '.eot");', + ' src: url("' + filename + '.eot?#iefix") format("embedded-opentype"),', + ' url(' + woff2 + ') format("woff2"),', + ' url(' + woff + ') format("woff"),', + ' url(' + ttf + ') format("ttf"),' + ]; + + if (embedSvg) { + resultLines.push(' url(' + svg + ') format("svg");'); + } else { + resultLines.push(' url("' + filename + '.svg#' + name + '") format("svg");'); + } + + resultLines.push( + ' url("' + filename + '.svg#' + name + '") format("svg");', + ' font-weight: ' + weight + ';', + ' font-style: ' + style + ';', + '}' + ); + + var result = resultLines.join("\n"); + + fs.writeFileSync(stylesheet, result); + return result; } -module.exports = config => { - let woff; - let woff2; - let ttf; - let svg; - let embedSvg; - - const name = config.name; - const filename = (config.collate) - ? path.join(config.css_fontpath, config.basename, config.basename) - : path.join(config.css_fontpath, config.basename); - const weight = config.weight; - const style = config.style; - - woff2 = `"${filename}.woff2"`; - woff = `"${filename}.woff"`; - ttf = `"${filename}.ttf"`; - - if (has(config.embed, 'woff2')) { - woff2 = encodeFont(config.woff2); - } - if (has(config.embed, 'woff')) { - woff = encodeFont(config.woff); - } - if (has(config.embed, 'ttf')) { - ttf = encodeFont(config.ttf); - } - if (has(config.embed, 'svg')) { - svg = encodeFont.svg(config.svg); - embedSvg = true; - } else { - embedSvg = false; - } - if (config.css) { - css(config.css, name, filename, weight, style, woff2, woff, ttf, svg, embedSvg); - } - if (config.less) { - less(config.less, name, filename, weight, style, woff2, woff, ttf, svg, embedSvg); - } - if (config.scss) { - scss(config.scss, name, filename, weight, style, woff2, woff, ttf, svg, embedSvg); - } +module.exports = function(config) { + var name, filename, weight, style, woff, woff2, ttf, svg, embedSvg; + + name = config.name; + filename = (config.collate) + ? path.join(config.css_fontpath, config.basename, config.basename) + : path.join(config.css_fontpath, config.basename); + + filename = filename.replace(/\//g,'\\'); + weight = config.weight; + style = config.style; + woff2 = '"' + filename + '.woff2"'; + woff = '"' + filename + '.woff"'; + ttf = '"' + filename + '.ttf"'; + + if (has(config.embed, 'woff2')) { + woff2 = encodeFont(config.woff2); + } + if (has(config.embed, 'woff')) { + woff = encodeFont(config.woff); + } + if (has(config.embed, 'ttf')) { + ttf = encodeFont(config.ttf); + } + if (has(config.embed, 'svg')) { + svg = encodeFont.svg(config.svg); + embedSvg = true; + } else { + embedSvg = false; + } + if (config.css) { + css(config.css, name, filename, weight, style, woff2, woff, ttf, svg, embedSvg); + } + if (config.less) { + less(config.less, name, filename, weight, style, woff2, woff, ttf, svg, embedSvg); + } + if (config.scss) { + scss(config.scss, name, filename, weight, style, woff2, woff, ttf, svg, embedSvg); + } }; diff --git a/src/ttf.js b/src/ttf.js index 779371c..9161849 100644 --- a/src/ttf.js +++ b/src/ttf.js @@ -1,21 +1,13 @@ -const fontforge = require('./fontforge.js'); -const { uniqueChars, charToHex } = require('./helpers.js'); +'use strict'; -module.exports = (source, target, name, opts = {}) => { - const subset = opts.subset; - let subsetCmd = ''; +var fontforge = require('./fontforge.js'); - if (subset) { - subsetCmd = uniqueChars(subset) - .map(ch => `SelectFewer(0u${charToHex(ch)});`) - .join(''); - subsetCmd = `SelectWorthOutputting();${subsetCmd}DetachAndRemoveGlyphs();`; - } - - return fontforge( - source, - `Open($1);SetFontNames($3,$3,$3);${subsetCmd}Generate($2, "", 8);`, - target, - name - ); +module.exports = function(source, target, name) { + + return fontforge( + source, + 'Open($1);SetFontNames($3,$3,$3);Generate($2, "", 8);', + target, + name + ); }; diff --git a/src/ttf2eot.js b/src/ttf2eot.js index 4b4cde7..cb9869f 100644 --- a/src/ttf2eot.js +++ b/src/ttf2eot.js @@ -1,17 +1,29 @@ -const ttf2eot = require('ttf2eot'); -const fs = require('fs'); - -const FontFaceException = require('./exception.js'); - -module.exports = (source, dest) => { - try { - const ttf = new Uint8Array(fs.readFileSync(source)); - const eot = new Buffer(ttf2eot(ttf).buffer); - - fs.writeFileSync(dest, eot); - } catch (e) { - throw new FontFaceException( - `eot conversion failed: ${e.toString()}\n` + - 'Your EOT file will probably not be in a working state'); - } +'use strict'; + +var quote = require('./helpers.js').quote; +var trim = require('./helpers.js').trim; +var execSync = require('child_process').execSync; + +var FontFaceException = require('./exception.js'); + +var ttf2eotCommand = require('./commands.js').ttf2eot; + +module.exports = function(source, dest) { + + var command = [ttf2eotCommand, quote(source), '>', quote(dest)].join(' '); + + try { + + var result = execSync(command).toString(); + + } catch (e) { + + throw new FontFaceException( + 'ttf2eot command failed: ' + e.toString() + '\n' + + 'From command: ' + command + '\n' + + trim(result) + '\n' + + 'Your EOT file will probably not be in a working state'); + } + + return result; }; diff --git a/src/ttf2svg.js b/src/ttf2svg.js index 27f0cf0..c4d9f87 100644 --- a/src/ttf2svg.js +++ b/src/ttf2svg.js @@ -1,16 +1,37 @@ -const ttf2svg = require('ttf2svg'); -const fs = require('fs'); +'use strict'; -const FontFaceException = require('./exception.js'); +var execSync = require('child_process').execSync; +var quote = require('./helpers.js').quote; +var trim = require('./helpers.js').trim; +var isLinux = require('./helpers.js').isLinux(); +var isWindows = require('./helpers.js').isWindows(); +var commands = require('./commands.js'); -module.exports = (source, target) => { - try { - const ttf = fs.readFileSync(source); - const svg = ttf2svg(ttf); - fs.writeFileSync(target, svg); - } catch (e) { - throw new FontFaceException( - `svg conversion failed: ${e.toString()}\n` + - 'Your SVG file will probably not be in a working state'); - } +module.exports = function(source, target, name) { + + var command = ''; + + if (isLinux) { + command = [commands.ttf2svg, quote(source), '>', quote(target)].join(' '); + } else if(isWindows){ + command = ['java -jar batik/batik-ttf2svg-1.8.jar', quote(source), '-id', quote(name), '-o', quote(target)].join(' '); + } + else { + command = [commands['batik-ttf2svg'], quote(source), '-id', quote(name), '-o', quote(target)].join(' '); + } + + try { + var result = execSync(command); + + + } catch (e) { + throw new FontFaceException( + 'ttf2svg command failed\n' + + 'From command: ' + command + '\n' + + trim(result) + '\n' + + 'Your SVG file will probably not be in a working state'); + + } + + return result; }; diff --git a/src/ttf2woff.js b/src/ttf2woff.js index 37b7e1c..85541d6 100644 --- a/src/ttf2woff.js +++ b/src/ttf2woff.js @@ -1,9 +1,11 @@ -const fontforge = require('./fontforge.js'); +'use strict'; -module.exports = (source, target) => ( - fontforge( - source, - 'Open($1);Generate($2, "", 8);', - target - ) -); +var fontforge = require('./fontforge.js'); + +module.exports = function(source, target) { + return fontforge( + source, + 'Open($1);Generate($2, "", 8);', + target + ); +}; diff --git a/src/ttf2woff2.js b/src/ttf2woff2.js index 592e801..f02ffaf 100644 --- a/src/ttf2woff2.js +++ b/src/ttf2woff2.js @@ -1,6 +1,8 @@ -const fs = require('fs'); -const ttf2woff2 = require('ttf2woff2'); +'use strict'; -module.exports = (source, target) => { - fs.writeFileSync(target, ttf2woff2(fs.readFileSync(source))); +var fs = require('fs'); +var ttf2woff2 = require('ttf2woff2'); + +module.exports = function(source, target) { + fs.writeFileSync(target, ttf2woff2(fs.readFileSync(source))); };