Skip to content

Commit

Permalink
Add simple caching of the converted Woff to local storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Manfred committed Feb 9, 2011
1 parent af12e47 commit 18b8001
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 56 deletions.
59 changes: 47 additions & 12 deletions dist/convert.js
Expand Up @@ -2,19 +2,30 @@ var Worf = {
VERSION: "0.1.0",

font_face: function(url, rules) {
Worf.Converter.woffToSfntAsBase64(url, function(base64, format) {
var declaration = Worf.Converter.fontFaceDeclaration(rules, base64, format);
var style = document.createElement('style');
style.innerHTML = declaration;
document.head.appendChild(style);
});
var sfnt = Worf.Caching.get(url);
if (!sfnt) {
Worf.Converter.woffToSfntAsBase64(url, function(base64, flavor) {
Worf.define_font_face(flavor, base64, rules);
Worf.Caching.set(url, flavor, base64);
});
} else {
sfnt.push(rules);
Worf.define_font_face.apply(this, sfnt);
}
},

define_font_face: function(flavor, base64, rules) {
var declaration = Worf.Converter.fontFaceDeclaration(flavor, base64, rules);
var style = document.createElement('style');
style.innerHTML = declaration;
document.head.appendChild(style);
}
}

Worf.Converter = {
fontFaceDeclaration: function(rules, data, format) {
fontFaceDeclaration: function(flavor, data, rules) {
return("@font-face { \
src: url(//:) format('no404'), url(data:font/"+format+";charset=utf-8;base64,"+data+") format("+format+"); \
src: url(//:) format('no404'), url(data:font/"+flavor+";charset=utf-8;base64,"+data+") format("+flavor+"); \
"+rules+" \
}");
},
Expand Down Expand Up @@ -49,10 +60,11 @@ Worf.Converter = {

woffToSfntAsBase64: function(src, callback) {
this.load(src, function(woff) {
callback(
Worf.Util.Base64.encode(Worf.Converter.woffToSfnt(woff)),
Worf.Converter.woffFlavor(woff)
);
var oldFunction = Worf.Util.Base64._utf8_encode;
Worf.Util.Base64._utf8_encode = function(data) { return data; }
var encoded = Worf.Util.Base64.encode(Worf.Converter.woffToSfnt(woff));
Worf.Util.Base64._utf8_encode = oldFunction;
callback(encoded, Worf.Converter.woffFlavor(woff));
});
},

Expand Down Expand Up @@ -155,6 +167,29 @@ Worf.Converter = {
}
}

Worf.Caching = {
baseKey: function(url) {
return('worf.' + url);
},

get: function(url) {
var baseKey = Worf.Caching.baseKey(url);
if (localStorage[baseKey + '.timestamp']) {
return([
localStorage[baseKey + '.flavor'],
localStorage[baseKey + '.base64']
]);
}
},

set: function(url, flavor, base64) {
var baseKey = Worf.Caching.baseKey(url);
localStorage[baseKey + '.timestamp'] = (new Date()).getTime();
localStorage[baseKey + '.flavor'] = flavor;
localStorage[baseKey + '.base64'] = base64;
}
}

Worf.Util = {};
Worf.Util.Unzip = function (barray){
var outputArr = [],
Expand Down

0 comments on commit 18b8001

Please sign in to comment.