Skip to content

Instantly share code, notes, and snippets.

@vieron
Last active January 12, 2016 15:57
Show Gist options
  • Save vieron/6842774 to your computer and use it in GitHub Desktop.
Save vieron/6842774 to your computer and use it in GitHub Desktop.
Javascript Font Loader for Fonts.com that allows multiple weights-per-family
/*
* fonts.com implement one different font-family name per weight or style of
* the same font. This means you need to explicitly declare in your elements
* that use a font-weight different to `normal` the new font-family
* corresponding to that weight.
*
* See http://stackoverflow.com/questions/5824925/is-there-any-way-to-fix-fonts-com-font-face-declarations
*/
var FontsComLoader = (function() {
var _get = function(url, callback) {
var xdomain = 'XDomainRequest' in window && window.XDomainRequest !== null;
var xhr = xdomain ? new XDomainRequest() : new XMLHttpRequest();
if (xdomain) {
xhr.onload = function(data) {
callback.call(xhr, xhr.responseText);
};
}
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
callback.call(xhr, xhr.responseText);
}
};
xhr.open("GET", url, true);
xhr.send();
};
var methods = {
init: function(name, url, variants) {
this.name = name;
this.url = url;
this.variants = variants;
this.protocol = window.location.protocol + '//';
this.fontscomURL = this.protocol + 'fast.fonts.net';
},
load: function(name, url, variants) {
if (!variants || !name || !url) return;
var ins = new methods.init(name, url, variants);
_get(ins.url, function(stylesheet) {
ins.addStyleTag();
ins.CSSString = stylesheet;
ins.appendCSS(ins.parseCSS());
});
return ins;
},
parseCSS: function(css) {
css = css || this.CSSString;
var weight, style, regx;
css = css.replace(/url\("\//g, 'url("' + this.fontscomURL +'/');
css = css.replace(/url\('\//g, 'url(\'' + this.fontscomURL + '/');
css = css.replace(/url\(\//g, 'url(' + this.fontscomURL + '/');
for (var matchExp in this.variants) {
regx = new RegExp('(font-family:(.*)' + matchExp + '(.*)";)', 'g');
weight = typeof this.variants[matchExp] === 'string' ? this.variants[matchExp] :
this.variants[matchExp].weight;
style = this.variants[matchExp].style || 'normal';
css = css.replace(regx, '$1 font-weight: ' + weight + ';' + 'font-style:' + style + ';');
}
css = css.replace(/font-family:(.*)";/g, 'font-family: "' + this.name + '";');
return css;
},
addStyleTag: function() {
this.scriptTag = document.createElement('style');
document.head.appendChild(this.scriptTag);
// this.disable();
return this;
},
appendCSS: function(css) {
css = css || this.CSSString;
this.scriptTag.innerHTML = css;
// this.enable();
return this;
},
disable: function() {
this.scriptTag.disabled = true;
return this;
},
enable: function() {
this.scriptTag.disabled = false;
return this;
}
};
methods.init.prototype = methods;
return methods;
})();
.helvetica-regular,
.helvetica-bold {
font-family: 'HelveticaNeueFontsCom', 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
.helvetica-regular {
font-weight: 400;
}
.helvetica-bold {
font-weight: 700;
}
/* Keys in object should match font-family names provided by fonts.com,
* they will be replaced by 'HelveticaNeueFontsCom' in this case. */
FontsComLoader.load('HelveticaNeueFontsCom', 'https://fast.fonts.net/cssapi/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.css', {
'W02-55Roma': '400',
'W02-75Bold': '700'
});
/*
// It's also posible to specify font-style properties.
FontsComLoader.load('HelveticaNeueFontsCom', 'https://fast.fonts.net/cssapi/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.css', {
'W02-55Roma': {
weight: '400',
style: 'normal'
},
'W02-75Bold': {
weight: '700',
style: 'normal'
}
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment