Skip to content
This repository has been archived by the owner on May 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
garyanikin authored May 18, 2017
2 parents d99c305 + 015ad38 commit cc6922e
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 11 deletions.
19 changes: 19 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,25 @@ module.exports = function(grunt) {
dest: 'test/tmp/timestamp_different',
options: {
fontFilename: 'different_2'
}
},
enabled_template_variables: {
src: 'test/src/*.svg',
dest: 'test/tmp/enabled_template_variables',
options: {
relativeFontPath: '../iamrelative',
fontPathVariables: true,
stylesheets: ['css', 'scss', 'less']
}
},
filename_length: {
src: 'test/src_filename_length/*.svg',
dest: 'test/tmp/filename_length',
options: {
autoHint: false,
engine: 'node',
hashes: false,
types: 'woff'
}
},
},
Expand Down
12 changes: 12 additions & 0 deletions Pull_Request_Template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
**PROJECT IS NOT ACTIVELY MAINTAINED**

Which means I will not receive a notification about this pull request.

If you want your pull request to be merged:

1. Explain the use case or bug you’re solving.
2. Add tests.
3. Add docs.
4. Mention sapegin.

If you want to maintain the project, please contact me: http://sapegin.me/
29 changes: 28 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ Type: `string` Default: `null`

Custom font path. Will be used instead of `destCss` *in* CSS file. Useful with CSS preprocessors.

#### fontPathVariables

Type: `boolean` Default: `false`

Create font-path variables for `less`, `scss` and `sass` files. Can be used to override the `relativeFontPath`
in custom preprocessor tasks or configs.

The variable name is a combination of the `font` name appended with `-font-path`.


#### version

Type: `string` Default: `false`
Expand Down Expand Up @@ -614,7 +624,7 @@ And finally, the third, for `icon-web-home.html`, a file that has access to the
<h1><%= homeHeading %></h1>
<p><%= homeMessage %></p>
<ul>
<% for (var i = 0; i < glpyhs.length; i++) { %>
<% for (var i = 0; i < glyphs.length; i++) { %>
<li><a href="#"><%= glyphs[i] %></a></li>
<% } %>
</ul>
Expand All @@ -626,6 +636,23 @@ And finally, the third, for `icon-web-home.html`, a file that has access to the

You can change CSS file syntax using `stylesheet` option (see above). It change file extension (so you can specify any) with some tweaks. Replace all comments with single line comments (which will be removed after compilation).

### Dynamic font-path
You can enable the `fontPathVariables` in combination with `relativeFontPath` to create a overridable font-path.

For example scss:
```scss
$icons-font-path : "/relativeFontPath/" !default;
@font-face {
font-family:"icons";
src:url($icons-font-path + "icons.eot");
src:url($icons-font-path + "icons.eot?#iefix") format("embedded-opentype"),
url($icons-font-path + "icons.woff") format("woff"),
url($icons-font-path + "icons.ttf") format("truetype");
font-weight:normal;
font-style:normal;
}
```

### Sass

If `stylesheet` option is `sass` or `scss`, `_` will prefix the file (so it can be a used as a partial).
Expand Down
6 changes: 3 additions & 3 deletions tasks/bin/eotlitetool.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def make_eot_name_headers(fontdata, nameTableDir):
else:
nameheaders.append(struct.pack('4x')) # len = 0, padding = 0

return b''.join(nameheaders)
return ''.join(nameheaders)

# just return a null-string (len = 0)
def make_root_string():
Expand Down Expand Up @@ -445,11 +445,11 @@ def make_eot_header(fontdata):
*([eotSize, fontDataSize, version, flags] + panose + [charset, italic] +
[weight, fsType, magicNumber] + urange + codepage + [checkSumAdjustment]))

return b''.join((fixed, nameheaders, rootstring))
return ''.join((fixed, nameheaders, rootstring))


def write_eot_font(eot, header, data):
open(eot,'wb').write(b''.join((header, data)))
open(eot,'wb').write(''.join((header, data)))
return

def main():
Expand Down
4 changes: 3 additions & 1 deletion tasks/templates/bem.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* Generated by grunt-webfont */

<% if (fontfaceStyles) { %>
<% if (fontPathVariables && stylesheet !== 'css') { %>
<%= fontPathVariable %>
<% } %>
<% if (fontSrc1 && embed.length) { %>
@font-face {
font-family:"<%= fontFamilyName %>";
Expand All @@ -22,7 +25,6 @@
font-family:"<%= fontFamilyName %>";
<% if (stylesheet === 'less') { %>}<% } %>
display:inline-block;
vertical-align:middle;
line-height:1;
font-weight:normal;
font-style:normal;
Expand Down
4 changes: 3 additions & 1 deletion tasks/templates/bootstrap.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
/* Based on https://github.com/endtwist/fontcustom/blob/master/lib/fontcustom/templates/fontcustom.css */

<% if (fontfaceStyles) { %>
<% if (fontPathVariables && stylesheet !== 'css') { %>
<%= fontPathVariable %>
<% } %>
<% if (fontSrc1 && embed.length) { %>
@font-face {
font-family:"<%= fontFamilyName %>";
Expand All @@ -26,7 +29,6 @@
.ligature-icons<% } %> {
font-family:"<%= fontFamilyName %>";
display:inline-block;
vertical-align:middle;
line-height:1;
font-weight:normal;
font-style:normal;
Expand Down
28 changes: 24 additions & 4 deletions tasks/webfont.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ module.exports = function(grunt) {
destStyl: options.destStyl || params.destStyl || params.destCss || params.dest,
dest: options.dest || params.dest,
relativeFontPath: options.relativeFontPath,
fontPathVariables: options.fontPathVariables || false,
addHashes: options.hashes !== false,
addLigatures: options.ligatures === true,
template: options.template,
Expand Down Expand Up @@ -356,7 +357,7 @@ module.exports = function(grunt) {
if (!has(o.types, type)) return;
wf.fontsSrcsMap[type].forEach(function(font, idx) {
if (font) {
fontSrcs[idx].push(generateFontSrc(type, font));
fontSrcs[idx].push(generateFontSrc(type, font, stylesheet));
}
});
});
Expand Down Expand Up @@ -622,7 +623,7 @@ module.exports = function(grunt) {
* @return {Integer}
*/
function getNextCodepoint() {
while (_.includes(o.codepoints, currentCodepoint)) {
while (_.invert(o.codepoints).hasOwnProperty(currentCodepoint)) {
currentCodepoint++;
}
return currentCodepoint;
Expand Down Expand Up @@ -681,17 +682,32 @@ module.exports = function(grunt) {
*
* @param {String} type Type of font
* @param {Object} font URL or Base64 string
* @param {String} stylesheet type: css, scss, ...
* @return {String}
*/
function generateFontSrc(type, font) {
function generateFontSrc(type, font, stylesheet) {
var filename = template(o.fontFilename + font.ext, o);
var fontPathVariableName = o.fontFamilyName + '-font-path';

var url;
if (font.embeddable && has(o.embed, type)) {
url = embedFont(path.join(o.dest, filename));
}
else {
url = o.relativeFontPath + filename;
if (o.fontPathVariables && stylesheet !== 'css') {
if (stylesheet === 'less') {
fontPathVariableName = '@' + fontPathVariableName;
o.fontPathVariable = fontPathVariableName + ' : "' + o.relativeFontPath + '";';
}
else {
fontPathVariableName = '$' + fontPathVariableName;
o.fontPathVariable = fontPathVariableName + ' : "' + o.relativeFontPath + '" !default;';
}
url = filename;
}
else {
url = o.relativeFontPath + filename;
}
if (o.addHashes) {
if (url.indexOf('#iefix') === -1) { // Do not add hashes for OldIE
// Put hash at the end of an URL or before #hash
Expand All @@ -701,6 +717,10 @@ module.exports = function(grunt) {
}

var src = 'url("' + url + '")';
if (o.fontPathVariables && stylesheet !== 'css') {
src = 'url(' + fontPathVariableName + ' + "' + url + '")';
}

if (font.format) src += ' format("' + font.format + '")';

return src;
Expand Down
2 changes: 2 additions & 0 deletions test/src_filename_length/length.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 32 additions & 1 deletion test/webfont_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,26 @@ exports.webfont = {
test.done();
},

enabled_template_variables: function(test) {
var cssFilename = 'test/tmp/enabled_template_variables/_icons.scss';

var css = grunt.file.read(cssFilename);

// There should be a variable declaration for scss preprocessor
test.ok(
find(css, '$icons-font-path : "../iamrelative/" !default;'),
'SCSS enable template variables: variable exists.'
);

// The variable declaration should be used
test.ok(
find(css, 'url($icons-font-path + "'),
'SCSS enable template variables: variable used.'
);

test.done();
},

html_template: function(test) {
var demo = grunt.file.read('test/tmp/html_template/icons.html');

Expand Down Expand Up @@ -899,7 +919,6 @@ exports.webfont = {

test.done();
},

// If font created multiple times with same value as timestamp option
// then md5 of created files must be the same too
timestamp_same: function (test) {
Expand Down Expand Up @@ -945,5 +964,17 @@ exports.webfont = {
test.done();
});
},
filename_length: function(test) {

// File should have been created.
test.ok(fs.existsSync('test/tmp/filename_length/icons.css'));

// File should have been created.
test.ok(fs.existsSync('test/tmp/filename_length/icons.woff'));

// File should have been created.
test.ok(fs.existsSync('test/tmp/filename_length/icons.html'));

test.done();
}
};

0 comments on commit cc6922e

Please sign in to comment.