Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit f81f363b040219fc40fb8a09acbd5473e72ac0f5 @sapegin committed Nov 30, 2012
@@ -0,0 +1,2 @@
+node_modules
+test/tmp
@@ -0,0 +1,3 @@
+node_modules
+Formula
+test
@@ -0,0 +1,18 @@
+# By mattlenz@github
+
+require 'formula'
+
+class Sfnt2woff < Formula
+ homepage 'http://people.mozilla.com/~jkew/woff/'
+ url 'http://people.mozilla.com/~jkew/woff/woff-code-latest.zip'
+ sha1 '59879f1bdeeafce7fc9d4b51406e80d7a4cd0293'
+
+ def install
+ system 'make all'
+ bin.install 'sfnt2woff'
+ end
+
+ def test
+ system 'sfnt2woff'
+ end
+end
@@ -0,0 +1,23 @@
+The MIT License
+===============
+
+Copyright © 2012 Artem Sapegin, http://sapegin.me
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,87 @@
+# SVG to webfont converter for Grunt
+
+Generate custom icon webfonts from SVG/EPS files via Grunt. Based on [Font Custom](http://endtwist.github.com/fontcustom/).
+
+This task will make all you need to use font-face icon on your website: font in all needed formats, CSS and HTML demo page.
+
+
+## Installation
+
+### OS X
+
+```
+brew install fontforge ttf2eot ttfautohint
+brew install https://raw.github.com/sapegin/grunt-webfont/master/Formula/sfnt2woff.rb
+npm install grunt-webfont
+```
+
+You may need to use `sudo` for `brew`, depending on your setup.
+
+
+## Configuration
+
+Add somewhere in your `grunt.js`:
+
+```javascript
+grunt.loadNpmTasks('grunt-webfont');
+```
+
+Inside your `grunt.js` file add a section named `webfont`. See Parameters section below for details.
+
+
+### Parameters
+
+#### files `string|array`
+
+Glyphs list: SVG or EPS. String or array. Wildcards are supported.
+
+#### destDir `string`
+
+Directory for resulting fonts and CSS files.
+
+#### [font] `string` (default: `'icons'`)
+
+Name of font and base name of font files.
+
+#### [hashes] `boolean` (default: `true`)
+
+Apend font file names with unique string to flush browser cache when you update your icons.
+
+#### [styles] `string|array` (default: `'font,icon'`)
+
+List of style to be added to CSS files: `font` (`font-face` declaration), `icon` (base `.icon` class), `extra` (extra stuff for Bootstrap (only for `stylesheet` = `'bootstrap'`).
+
+#### [types] `string|array` (default: `'woff,ttf,eot,svg'`)
+
+Font files types to generate.
+
+#### [stylesheet] `string` (default: `'bem'`)
+
+Icon classes syntax. `bem` for double class names: `icon icon_awesome` or `bootstrap` for single class names: `icon-awesome`.
+
+#### [skip] `boolean` (default: `false`)
+
+If `true` task will not be ran. In example, you can skip task on Windows (becase of difficult installation):
+
+``` javascript
+skip: require('os').platform() === 'win32'
+```
+
+
+### Config Example
+
+``` javascript
+webfont: {
+ icons: {
+ files: 'icons/*.svg',
+ destDir: 'build/fonts'
+ }
+}
+```
+
+
+---
+
+## License
+
+The MIT License, see the included `License.md` file.
@@ -0,0 +1,56 @@
+/*jshint node:true*/
+module.exports = function(grunt) {
+ 'use strict';
+
+ grunt.initConfig({
+ lint: {
+ files: [
+ 'tasks/webfont.js',
+ 'grunt.js'
+ ]
+ },
+ webfont: {
+ test1: {
+ files: 'test/src/*.svg',
+ destDir: 'test/tmp',
+ hashes: false
+ },
+ test2: {
+ files: 'test/src/*.svg',
+ destDir: 'test/tmp',
+ font: 'myfont',
+ types: 'woff,svg',
+ stylesheet: 'bootstrap'
+ }
+ },
+ test: {
+ tasks: ['test/*_test.js']
+ },
+ jshint: {
+ options: {
+ node: true,
+ white: false,
+ smarttabs: true,
+ eqeqeq: true,
+ immed: true,
+ latedef: false,
+ newcap: true,
+ undef: true
+ }
+ }
+ });
+
+ grunt.loadTasks('tasks');
+
+ var fs = require('fs');
+
+ grunt.registerTask('clean', 'Copy files to test.', function() {
+ grunt.file.expand('test/tmp/**').forEach(function(file) {
+ fs.unlinkSync(file);
+ });
+ fs.rmdirSync('test/tmp');
+ });
+
+ grunt.registerTask('default', 'lint clean webfont test clean');
+
+};
@@ -0,0 +1,45 @@
+{
+ "name": "grunt-webfont",
+ "description": "SVG to webfont converter for Grunt.",
+ "version": "0.0.1",
+ "homepage": "https://github.com/sapegin/grunt-webfont",
+ "author": {
+ "name": "Artem Sapegin",
+ "url": "http://sapegin.me/"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/sapegin/grunt-webfont.git"
+ },
+ "bugs": {
+ "url": "https://github.com/sapegin/grunt-webfont/issues"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://github.com/sapegin/grunt-webfont/blob/master/License.md"
+ }
+ ],
+ "main": "tasks/webfont.js",
+ "engines": {
+ "node": "*"
+ },
+ "dependencies": {
+ "temp": "0.4.x"
+ },
+ "devDependencies": {
+ "grunt": "*"
+ },
+ "keywords": [
+ "gruntplugin",
+ "font",
+ "fontforge",
+ "font-face",
+ "woff",
+ "ttf",
+ "svg",
+ "eot",
+ "truetype",
+ "css"
+ ]
+}
@@ -0,0 +1,88 @@
+# Based on https://github.com/endtwist/fontcustom/blob/master/lib/fontcustom/scripts/generate.py
+
+import fontforge
+import os
+import argparse
+import md5
+import json
+from subprocess import call
+
+
+parser = argparse.ArgumentParser(description='Convert a directory of svg and eps files into a unified font file.')
+parser.add_argument('input_dir', metavar='directory', type=unicode, help='directory of vector files')
+parser.add_argument('output_dir', metavar='directory', type=unicode, help='output directory')
+parser.add_argument('font', metavar='font', type=unicode, help='font name')
+parser.add_argument('types', metavar='types', type=lambda s: s.split(','), help='output types')
+parser.add_argument('--hashes', action='store_true', help='add hashes to file names')
+args = parser.parse_args()
+
+
+f = fontforge.font()
+f.encoding = 'UnicodeFull'
+
+m = md5.new()
+cp = 0xf100
+files = []
+
+KERNING = 15
+
+for dirname, dirnames, filenames in os.walk(args.input_dir):
+ for filename in filenames:
+ name, ext = os.path.splitext(filename)
+ filePath = os.path.join(dirname, filename)
+ size = os.path.getsize(filePath)
+
+ if ext in ['.svg', '.eps']:
+ m.update(filename + str(size) + ';')
+ glyph = f.createChar(cp)
+ glyph.importOutlines(filePath)
+
+ glyph.left_side_bearing = KERNING
+ glyph.right_side_bearing = KERNING
+
+ # possible optimization?
+ # glyph.simplify()
+ # glyph.round()
+
+ files.append(name)
+ cp += 1
+
+fontfile = args.output_dir + '/' + args.font
+if args.hashes:
+ fontfile += '-' + m.hexdigest()
+
+f.fontname = args.font
+f.familyname = args.font
+f.fullname = args.font
+
+# TTF
+f.generate(fontfile + '.ttf')
+
+# SVG
+if 'svg' in args.types:
+ f.generate(fontfile + '.svg')
+
+ # Fix SVG header for webkit (from: https://github.com/fontello/font-builder/blob/master/bin/fontconvert.py)
+ svgfile = open(fontfile + '.svg', 'r+')
+ svgtext = svgfile.read()
+ svgfile.seek(0)
+ svgfile.write(svgtext.replace('<svg>', '<svg xmlns="http://www.w3.org/2000/svg">'))
+ svgfile.close()
+
+scriptPath = os.path.dirname(os.path.realpath(__file__))
+
+# WOFF
+if 'woff' in args.types:
+ call(['sfnt2woff', fontfile + '.ttf'])
+
+# EOT
+if 'eot' in args.types:
+ call('ttf2eot ' + fontfile + '.ttf > ' + fontfile + '.eot', shell=True)
+
+# Hint the TTF file or delete it if not needed
+if 'ttf' in args.types:
+ call('ttfautohint -s -n ' + fontfile + '.ttf ' + fontfile + '-hinted.ttf && mv ' + fontfile + '-hinted.ttf ' + fontfile + '.ttf', shell=True)
+else:
+ os.remove(fontfile + '.ttf')
+
+print json.dumps({'file': fontfile, 'names': files})
@@ -0,0 +1,22 @@
+/* Generated by grunt-webfont */
+
+<% if (fontfaceStyles) { %>@font-face {
+ font-family:"<%= fontBaseName %>";
+ src: url("<%= fontName %>.eot?#iefix") format("embedded-opentype"),
+ url("<%= fontName %>.woff") format("woff"),
+ url("<%= fontName %>.ttf") format("truetype"),
+ url("<%= fontName %>.svg#fontcustom") format("svg");
+ font-weight:normal;
+ font-style:normal;
+ }
+<% } %>
+<% if (baseStyles) { %>.icon {
+ font-family:"<%= fontBaseName %>";
+ display:inline-block;
+ font-style:normal;
+ speak:none;
+ -webkit-font-smoothing:antialiased;
+ }
+<% } %>
+<% if (iconsStyles) { %>/* Icons */<% for (var glyphIdx = 0; glyphIdx < glyphs.length; glyphIdx++) { %>
+.icon_<%= glyphs[glyphIdx] %>:before { content:"\<%= (61696+glyphIdx).toString(16) %>"; }<% } %><% } %>
Oops, something went wrong.

0 comments on commit f81f363

Please sign in to comment.