Skip to content

Instantly share code, notes, and snippets.

@letorbi
Last active January 15, 2022 17:39
Show Gist options
  • Select an option

  • Save letorbi/5177771 to your computer and use it in GitHub Desktop.

Select an option

Save letorbi/5177771 to your computer and use it in GitHub Desktop.

Revisions

  1. Torben revised this gist Sep 7, 2015. No changes.
  2. Torben revised this gist Sep 7, 2015. No changes.
  3. Torben revised this gist Feb 9, 2015. 1 changed file with 13 additions and 9 deletions.
    22 changes: 13 additions & 9 deletions fontsmoothie.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Font Smoothie copyright 2013,14 Torben Haase <http://pixelsvsbytes.com>
    // Font Smoothie copyright 2013,14,15 Torben Haase <http://pixelsvsbytes.com>
    // Source-URL <https://gist.github.com/letorbi/5177771>
    //
    // Font Smoothie is free software: you can redistribute it and/or modify it under
    @@ -46,7 +46,8 @@ function apply() {
    try {
    // NOTE Safari needs the canvas to be part of the DOM tree to
    // return correct alpha values.
    var canvas = document.head.appendChild(document.createElement('CANVAS'));
    var head = document.getElementsByTagName('HEAD')[0];
    var canvas = head.appendChild(document.createElement('CANVAS'));
    var context = canvas.getContext('2d');
    context.textBaseline = 'top';
    context.font = '32px Arial';
    @@ -56,20 +57,23 @@ function apply() {
    // off we loop through all CSS rules and fix them.
    for (var sheet, i = 0; (context.getImageData(5, 8, 1, 1).data[3] == 255) && (sheet = document.styleSheets[i]); i++)
    sheet && traverseSheet(sheet);
    document.head.removeChild(canvas);
    head.removeChild(canvas);
    }
    catch (e) {
    // NOTE Ignore any errors that might occur.
    throw e; // DEBUG
    }
    }

    if (document.readyState == 'complete') {
    apply();
    }
    else {
    var f = window.onload;
    window.onload = window.onload ? function(evt){f(evt);apply();} : apply;
    // NOTE Run Font Smoothie only if the system is Windows XP
    if (navigator.userAgent.toLowerCase().indexOf('windows nt 5.1') > -1) {
    if (document.readyState == 'complete') {
    apply();
    }
    else {
    var f = window.onload;
    window.onload = window.onload ? function(evt){f(evt);apply();} : apply;
    }
    }

    })();
  4. Torben revised this gist Feb 9, 2015. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions fontsmoothie.min.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    // Font Smoothie copyright 2013,14 Torben Haase <http://pixelsvsbytes.com>
    // Font Smoothie copyright 2013,14,15 Torben Haase <http://pixelsvsbytes.com>
    // Source-URL <https://gist.github.com/letorbi/5177771>
    (function(){function a(){function e(a){if(null===a.cssRules)return console.warn("Fontsmoothie warning: Browser blocks access to CSS rules in "+a.href);for(var b=a.href||location.href,b=RegExp(b.substring(0,b.lastIndexOf("/")).replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&"),"g"),c,f=0;c=a.cssRules[f];f++){if(c.style&&c.style.src&&(c.style.src=c.style.src.replace(b,".").replace(/([^;]*?),\s*(url\(\S*? format\(["']?svg["']?\))([\s,]*[^;]+|)/,"$2, $1$3"),window.opera)){var d=c.cssText;a.deleteRule(f);a.insertRule(d,
    f)}c.styleSheet&&e(c.styleSheet)}}try{var a=document.head.appendChild(document.createElement("CANVAS")),b=a.getContext("2d");b.textBaseline="top";b.font="32px Arial";b.fillText("O",0,0);for(var d,h=0;255==b.getImageData(5,8,1,1).data[3]&&(d=document.styleSheets[h]);h++)d&&e(d);document.head.removeChild(a)}catch(g){throw g;}}if("complete"==document.readyState)a();else{var g=window.onload;window.onload=window.onload?function(e){g(e);a()}:a}})();
    (function(){function a(){function e(a){if(null===a.cssRules)return console.warn("Fontsmoothie warning: Browser blocks access to CSS rules in "+a.href);for(var b=a.href||location.href,b=new RegExp(b.substring(0,b.lastIndexOf("/")).replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&"),"g"),c,f=0;c=a.cssRules[f];f++){if(c.style&&c.style.src&&(c.style.src=c.style.src.replace(b,".").replace(/([^;]*?),\s*(url\(\S*? format\(["']?svg["']?\))([\s,]*[^;]+|)/,"$2, $1$3"),window.opera)){var d=c.cssText;a.deleteRule(f); a.insertRule(d,f)}c.styleSheet&&e(c.styleSheet)}}try{var a=document.getElementsByTagName("HEAD")[0],h=a.appendChild(document.createElement("CANVAS")),b=h.getContext("2d");b.textBaseline="top";b.font="32px Arial";b.fillText("O",0,0);for(var d,k=0;255==b.getImageData(5,8,1,1).data[3]&&(d=document.styleSheets[k]);k++)d&&e(d);a.removeChild(h)}catch(g){throw g;}}if(-1<navigator.userAgent.toLowerCase().indexOf("windows nt 5.1"))if("complete"==document.readyState)a();else{var g=window.onload;window.onload= window.onload?function(e){g(e);a()}:a}})();
  5. Torben revised this gist Jan 27, 2014. 1 changed file with 1 addition and 6 deletions.
    7 changes: 1 addition & 6 deletions fontsmoothie.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,4 @@
    // ==ClosureCompiler==
    // @output_file_name default.js
    // @compilation_level SIMPLE_OPTIMIZATIONS
    // ==/ClosureCompiler==

    // Font Smoothie copyright 2013 Torben Haase <http://pixelsvsbytes.com>
    // Font Smoothie copyright 2013,14 Torben Haase <http://pixelsvsbytes.com>
    // Source-URL <https://gist.github.com/letorbi/5177771>
    //
    // Font Smoothie is free software: you can redistribute it and/or modify it under
  6. Torben revised this gist Jan 27, 2014. 2 changed files with 22 additions and 11 deletions.
    27 changes: 19 additions & 8 deletions fontsmoothie.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    // ==ClosureCompiler==
    // @output_file_name default.js
    // @compilation_level SIMPLE_OPTIMIZATIONS
    // ==/ClosureCompiler==

    // Font Smoothie copyright 2013 Torben Haase <http://pixelsvsbytes.com>
    // Source-URL <https://gist.github.com/letorbi/5177771>
    //
    @@ -13,18 +18,24 @@
    // License along with Font Smoothie. If not, see <http://www.gnu.org/licenses/>.

    (function() { 'use strict';

    function apply() {
    // NOTE Circles through all CSS rules of a sheet and fix them if
    // necassary.
    function traverseSheet(sheet) {
    for (var rule, j = 0; sheet && (rule = sheet.cssRules[j]); j++) {
    if (sheet.cssRules === null)
    return console.warn("Fontsmoothie warning: Browser blocks access to CSS rules in "+sheet.href);
    var href = sheet.href||location.href;
    href = new RegExp(href.substring(0, href.lastIndexOf("/"))
    .replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), "g");
    for (var rule, j = 0; rule = sheet.cssRules[j]; j++) {
    // NOTE Only affect rules that have a src value (AFAIK
    // this is only true for CSSFontFaceRule objects).
    if (rule.style && rule.style.src) {
    // NOTE Create a new rule where SVG is the preferred
    // source format and delete the old rule.
    rule.style.src = rule.style.src.replace(/^([^;]*),([^,]*svg[^,;]*)([^;]*)/g, '$2,$1$3');
    rule.style.src = rule.style.src.replace(href, ".")
    .replace(/([^;]*?),\s*(url\(\S*? format\(["']?svg["']?\))([\s,]*[^;]+|)/, '$2, $1$3');
    // NOTE Opera needs the rule to be deleted and inserted again
    // (in that order) to apply it to the document.
    if (window.opera) {
    @@ -33,10 +44,10 @@ function apply() {
    sheet.insertRule(text, j);
    }
    }
    traverseSheet(rule.styleSheet);
    rule.styleSheet && traverseSheet(rule.styleSheet);
    }
    }
    try {
    // NOTE Safari needs the canvas to be part of the DOM tree to
    // return correct alpha values.
    @@ -49,12 +60,12 @@ function apply() {
    // but only the one at position (5,8). If font-smoothing is
    // off we loop through all CSS rules and fix them.
    for (var sheet, i = 0; (context.getImageData(5, 8, 1, 1).data[3] == 255) && (sheet = document.styleSheets[i]); i++)
    traverseSheet(sheet);
    sheet && traverseSheet(sheet);
    document.head.removeChild(canvas);
    }
    catch (e) {
    // NOTE Ignore any errors that might occur.
    // console.log(e); // DEBUG
    throw e; // DEBUG
    }
    }

    @@ -65,5 +76,5 @@ else {
    var f = window.onload;
    window.onload = window.onload ? function(evt){f(evt);apply();} : apply;
    }

    })();
    6 changes: 3 additions & 3 deletions fontsmoothie.min.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Font Smoothie copyright 2013 Torben Haase <http://pixelsvsbytes.com>
    // Font Smoothie copyright 2013,14 Torben Haase <http://pixelsvsbytes.com>
    // Source-URL <https://gist.github.com/letorbi/5177771>
    (function(){function a(){function d(a){for(var c,b=0;a&&(c=a.cssRules[b]);b++){if(c.style&&c.style.src&&(c.style.src=c.style.src.replace(/^([^;]*),([^,]*svg[^,;]*)([^;]*)/g,"$2,$1$3"),window.opera)){var e=c.cssText;a.deleteRule(b);a.insertRule(e,b)}d(c.styleSheet)}}try{var a=document.head.appendChild(document.createElement("CANVAS")),b=a.getContext("2d");b.textBaseline="top";b.font="32px Arial";b.fillText("O",0,0);for(var e,f=0;255==b.getImageData(5,8,1,1).data[3]&&(e=document.styleSheets[f]);f++)d(e);
    document.head.removeChild(a)}catch(g){}}if("complete"==document.readyState)a();else{var g=window.onload;window.onload=window.onload?function(d){g(d);a()}:a}})();
    (function(){function a(){function e(a){if(null===a.cssRules)return console.warn("Fontsmoothie warning: Browser blocks access to CSS rules in "+a.href);for(var b=a.href||location.href,b=RegExp(b.substring(0,b.lastIndexOf("/")).replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&"),"g"),c,f=0;c=a.cssRules[f];f++){if(c.style&&c.style.src&&(c.style.src=c.style.src.replace(b,".").replace(/([^;]*?),\s*(url\(\S*? format\(["']?svg["']?\))([\s,]*[^;]+|)/,"$2, $1$3"),window.opera)){var d=c.cssText;a.deleteRule(f);a.insertRule(d,
    f)}c.styleSheet&&e(c.styleSheet)}}try{var a=document.head.appendChild(document.createElement("CANVAS")),b=a.getContext("2d");b.textBaseline="top";b.font="32px Arial";b.fillText("O",0,0);for(var d,h=0;255==b.getImageData(5,8,1,1).data[3]&&(d=document.styleSheets[h]);h++)d&&e(d);document.head.removeChild(a)}catch(g){throw g;}}if("complete"==document.readyState)a();else{var g=window.onload;window.onload=window.onload?function(e){g(e);a()}:a}})();
  7. letorbi revised this gist Oct 14, 2013. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion fontsmoothie.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Font Smoothie copyright 2013 Torben Haase <http://pixelsvsbytes.com/>
    // Font Smoothie copyright 2013 Torben Haase <http://pixelsvsbytes.com>
    // Source-URL <https://gist.github.com/letorbi/5177771>
    //
    // Font Smoothie is free software: you can redistribute it and/or modify it under
    2 changes: 1 addition & 1 deletion fontsmoothie.min.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Font Smoothie copyright 2013 Torben Haase <http://pixelsvsbytes.com/>
    // Font Smoothie copyright 2013 Torben Haase <http://pixelsvsbytes.com>
    // Source-URL <https://gist.github.com/letorbi/5177771>
    (function(){function a(){function d(a){for(var c,b=0;a&&(c=a.cssRules[b]);b++){if(c.style&&c.style.src&&(c.style.src=c.style.src.replace(/^([^;]*),([^,]*svg[^,;]*)([^;]*)/g,"$2,$1$3"),window.opera)){var e=c.cssText;a.deleteRule(b);a.insertRule(e,b)}d(c.styleSheet)}}try{var a=document.head.appendChild(document.createElement("CANVAS")),b=a.getContext("2d");b.textBaseline="top";b.font="32px Arial";b.fillText("O",0,0);for(var e,f=0;255==b.getImageData(5,8,1,1).data[3]&&(e=document.styleSheets[f]);f++)d(e);
    document.head.removeChild(a)}catch(g){}}if("complete"==document.readyState)a();else{var g=window.onload;window.onload=window.onload?function(d){g(d);a()}:a}})();
  8. letorbi revised this gist Oct 14, 2013. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions fontsmoothie.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    // Font Smoothie copyright 2013 Torben Haase <http://pixelsvsbytes.com/>
    // Source-URL <https://gist.github.com/letorbi/5177771>
    //
    // Font Smoothie is free software: you can redistribute it and/or modify it under
    // the terms of the GNU Lesser General Public License as published by the Free
    1 change: 1 addition & 0 deletions fontsmoothie.min.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // Font Smoothie copyright 2013 Torben Haase <http://pixelsvsbytes.com/>
    // Source-URL <https://gist.github.com/letorbi/5177771>
    (function(){function a(){function d(a){for(var c,b=0;a&&(c=a.cssRules[b]);b++){if(c.style&&c.style.src&&(c.style.src=c.style.src.replace(/^([^;]*),([^,]*svg[^,;]*)([^;]*)/g,"$2,$1$3"),window.opera)){var e=c.cssText;a.deleteRule(b);a.insertRule(e,b)}d(c.styleSheet)}}try{var a=document.head.appendChild(document.createElement("CANVAS")),b=a.getContext("2d");b.textBaseline="top";b.font="32px Arial";b.fillText("O",0,0);for(var e,f=0;255==b.getImageData(5,8,1,1).data[3]&&(e=document.styleSheets[f]);f++)d(e);
    document.head.removeChild(a)}catch(g){}}if("complete"==document.readyState)a();else{var g=window.onload;window.onload=window.onload?function(d){g(d);a()}:a}})();
  9. letorbi revised this gist Oct 14, 2013. 2 changed files with 24 additions and 16 deletions.
    35 changes: 21 additions & 14 deletions fontsmoothie.js
    Original file line number Diff line number Diff line change
    @@ -11,18 +11,15 @@
    // details. You should have received a copy of the GNU Lesser General Public
    // License along with Font Smoothie. If not, see <http://www.gnu.org/licenses/>.

    window.addEventListener && window.addEventListener('load', function() {
    "use strict";
    // NOTE Using these variables as aliases reduces the code size.
    var doc = document;
    var head = doc.head;
    (function() { 'use strict';

    // NOTE Circles through all CSS rules of a sheet and fix them fn
    function apply() {
    // NOTE Circles through all CSS rules of a sheet and fix them if
    // necassary.
    function traverseSheet(sheet) {
    for (var rule, j = 0; sheet && (rule = sheet.cssRules[j]); j++) {
    // NOTE Only affect rules that have a src value (AFAIK
    // this is only true for CSSFontfaceRule objects).
    // this is only true for CSSFontFaceRule objects).
    if (rule.style && rule.style.src) {
    // NOTE Create a new rule where SVG is the preferred
    // source format and delete the old rule.
    @@ -38,24 +35,34 @@ window.addEventListener && window.addEventListener('load', function() {
    traverseSheet(rule.styleSheet);
    }
    }

    try {
    // NOTE Safari needs the canvas to be part of the DOM tree to
    // return correct alpha values.
    var canvas = head.appendChild(doc.createElement('CANVAS'));
    var canvas = document.head.appendChild(document.createElement('CANVAS'));
    var context = canvas.getContext('2d');
    context.textBaseline = 'top';
    context.font = '32px Arial';
    context.fillText('O', 0, 0);
    // NOTE We won't check the alpha values of all canvas pixels,
    // but only the one at position (5,8). If font-smoothing is
    // off we loop through all CSS rules and fix them.
    for (var sheet, i = 0; (context.getImageData(5, 8, 1, 1).data[3] == 255) && (sheet = doc.styleSheets[i]); i++)
    for (var sheet, i = 0; (context.getImageData(5, 8, 1, 1).data[3] == 255) && (sheet = document.styleSheets[i]); i++)
    traverseSheet(sheet);
    head.removeChild(canvas);
    document.head.removeChild(canvas);
    }
    catch (e) {
    // NOTE Ignore any errors, which might occur.
    // throw e;
    // NOTE Ignore any errors that might occur.
    // console.log(e); // DEBUG
    }
    }, false);
    }

    if (document.readyState == 'complete') {
    apply();
    }
    else {
    var f = window.onload;
    window.onload = window.onload ? function(evt){f(evt);apply();} : apply;
    }

    })();
    5 changes: 3 additions & 2 deletions fontsmoothie.min.js
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,3 @@
    window.addEventListener&&window.addEventListener("load",function(){function e(a){for(var b,d=0;a&&(b=a.cssRules[d]);d++){if(b.style&&b.style.src&&(b.style.src=b.style.src.replace(/^([^;]*),([^,]*svg[^,;]*)([^;]*)/g,"$2,$1$3"),window.opera)){var c=b.cssText;a.deleteRule(d);a.insertRule(c,d)}e(b.styleSheet)}}var c=document,f=c.head;try{var g=f.appendChild(c.createElement("CANVAS")),a=g.getContext("2d");a.textBaseline="top";a.font="32px Arial";a.fillText("O",0,0);for(var h,j=0;255==a.getImageData(5,
    8,1,1).data[3]&&(h=c.styleSheets[j]);j++)e(h);f.removeChild(g)}catch(k){}},!1);
    // Font Smoothie copyright 2013 Torben Haase <http://pixelsvsbytes.com/>
    (function(){function a(){function d(a){for(var c,b=0;a&&(c=a.cssRules[b]);b++){if(c.style&&c.style.src&&(c.style.src=c.style.src.replace(/^([^;]*),([^,]*svg[^,;]*)([^;]*)/g,"$2,$1$3"),window.opera)){var e=c.cssText;a.deleteRule(b);a.insertRule(e,b)}d(c.styleSheet)}}try{var a=document.head.appendChild(document.createElement("CANVAS")),b=a.getContext("2d");b.textBaseline="top";b.font="32px Arial";b.fillText("O",0,0);for(var e,f=0;255==b.getImageData(5,8,1,1).data[3]&&(e=document.styleSheets[f]);f++)d(e);
    document.head.removeChild(a)}catch(g){}}if("complete"==document.readyState)a();else{var g=window.onload;window.onload=window.onload?function(d){g(d);a()}:a}})();
  10. Torben revised this gist Mar 18, 2013. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions fontsmoothie.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,4 @@
    // Font Smoothie 0.2.1 unstable
    //
    // Copyright 2013 Torben Haase <http://pixelsvsbytes.com/>
    // Font Smoothie copyright 2013 Torben Haase <http://pixelsvsbytes.com/>
    //
    // Font Smoothie is free software: you can redistribute it and/or modify it under
    // the terms of the GNU Lesser General Public License as published by the Free
  11. Torben revised this gist Mar 18, 2013. 2 changed files with 17 additions and 17 deletions.
    30 changes: 15 additions & 15 deletions fontsmoothie.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Font Smoothie 0.2.0 unstable
    // Font Smoothie 0.2.1 unstable
    //
    // Copyright 2013 Torben Haase <http://pixelsvsbytes.com/>
    //
    @@ -13,29 +13,29 @@
    // details. You should have received a copy of the GNU Lesser General Public
    // License along with Font Smoothie. If not, see <http://www.gnu.org/licenses/>.

    window.addEventListener && window.addEventListener('load', function(sheet, newSheet) {
    window.addEventListener && window.addEventListener('load', function() {
    "use strict";
    // NOTE Using these variables as aliases reduces the code size.
    var doc = document;
    var head = doc.head;

    // NOTE Circles through all CSS rules of a sheet and fix them fn
    // necassary. The rule parameter is just gevinen to declare a
    // variable of that name (same for sheet and newSheet in the event
    // handler).
    function traverseSheet(sheet, rule) {
    for (var j = 0; sheet && (rule = sheet.cssRules[j]); j++) {
    // necassary.
    function traverseSheet(sheet) {
    for (var rule, j = 0; sheet && (rule = sheet.cssRules[j]); j++) {
    // NOTE Only affect rules that have a src value (AFAIK
    // this is only true for CSSFontfaceRule objects).
    if (rule.style && rule.style.src) {
    // NOTE Opera only applies new stylesheets if they have been
    // added to a style-node created via JavaScript.
    newSheet = newSheet?newSheet:head.appendChild(doc.createElement('STYLE')).sheet;
    // NOTE Create a new rule where SVG is the preferred
    // source format and delete the old rule.
    newSheet.insertRule(rule.cssText.replace(/src:([^;]*),([^,]*svg[^,;]*)([^;]*)/g, 'src:$2,$1$3'), j);
    sheet.deleteRule(j);
    j--;
    rule.style.src = rule.style.src.replace(/^([^;]*),([^,]*svg[^,;]*)([^;]*)/g, '$2,$1$3');
    // NOTE Opera needs the rule to be deleted and inserted again
    // (in that order) to apply it to the document.
    if (window.opera) {
    var text = rule.cssText;
    sheet.deleteRule(j);
    sheet.insertRule(text, j);
    }
    }
    traverseSheet(rule.styleSheet);
    }
    @@ -52,12 +52,12 @@ window.addEventListener && window.addEventListener('load', function(sheet, newSh
    // NOTE We won't check the alpha values of all canvas pixels,
    // but only the one at position (5,8). If font-smoothing is
    // off we loop through all CSS rules and fix them.
    for (var i = 0; (context.getImageData(5, 8, 1, 1).data[3] == 255) && (sheet = doc.styleSheets[i]) && (sheet != newSheet); i++)
    for (var sheet, i = 0; (context.getImageData(5, 8, 1, 1).data[3] == 255) && (sheet = doc.styleSheets[i]); i++)
    traverseSheet(sheet);
    head.removeChild(canvas);
    }
    catch (e) {
    // NOTE Ignore any errors, which might occur.
    throw e;
    // throw e;
    }
    }, false);
    4 changes: 2 additions & 2 deletions fontsmoothie.min.js
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    window.addEventListener&&window.addEventListener("load",function(f,b){function h(a,c){for(var d=0;a&&(c=a.cssRules[d]);d++)c.style&&c.style.src&&(b=b?b:g.appendChild(e.createElement("STYLE")).sheet,b.insertRule(c.cssText.replace(/src:([^;]*),([^,]*svg[^,;]*)([^;]*)/g,"src:$2,$1$3"),d),a.deleteRule(d),d--),h(c.styleSheet)}var e=document,g=e.head;try{var j=g.appendChild(e.createElement("CANVAS")),a=j.getContext("2d");a.textBaseline="top";a.font="32px Arial";a.fillText("O",0,0);for(var k=0;255==a.getImageData(5,
    8,1,1).data[3]&&(f=e.styleSheets[k])&&f!=b;k++)h(f);g.removeChild(j)}catch(l){throw l;}},!1);
    window.addEventListener&&window.addEventListener("load",function(){function e(a){for(var b,d=0;a&&(b=a.cssRules[d]);d++){if(b.style&&b.style.src&&(b.style.src=b.style.src.replace(/^([^;]*),([^,]*svg[^,;]*)([^;]*)/g,"$2,$1$3"),window.opera)){var c=b.cssText;a.deleteRule(d);a.insertRule(c,d)}e(b.styleSheet)}}var c=document,f=c.head;try{var g=f.appendChild(c.createElement("CANVAS")),a=g.getContext("2d");a.textBaseline="top";a.font="32px Arial";a.fillText("O",0,0);for(var h,j=0;255==a.getImageData(5,
    8,1,1).data[3]&&(h=c.styleSheets[j]);j++)e(h);f.removeChild(g)}catch(k){}},!1);
  12. Torben revised this gist Mar 17, 2013. 2 changed files with 34 additions and 24 deletions.
    54 changes: 32 additions & 22 deletions fontsmoothie.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Font Smoothie 0.1.0 unstable
    // Font Smoothie 0.2.0 unstable
    //
    // Copyright 2013 Torben Haase <http://pixelsvsbytes.com/>
    //
    @@ -13,41 +13,51 @@
    // details. You should have received a copy of the GNU Lesser General Public
    // License along with Font Smoothie. If not, see <http://www.gnu.org/licenses/>.

    window.addEventListener && window.addEventListener('load', function() {
    window.addEventListener && window.addEventListener('load', function(sheet, newSheet) {
    "use strict";
    // NOTE Using these variables as aliases reduces the code size.
    var doc = document;
    var head = doc.head;

    // NOTE Circles through all CSS rules of a sheet and fix them fn
    // necassary. The rule parameter is just gevinen to declare a
    // variable of that name (same for sheet and newSheet in the event
    // handler).
    function traverseSheet(sheet, rule) {
    for (var j = 0; sheet && (rule = sheet.cssRules[j]); j++) {
    // NOTE Only affect rules that have a src value (AFAIK
    // this is only true for CSSFontfaceRule objects).
    if (rule.style && rule.style.src) {
    // NOTE Opera only applies new stylesheets if they have been
    // added to a style-node created via JavaScript.
    newSheet = newSheet?newSheet:head.appendChild(doc.createElement('STYLE')).sheet;
    // NOTE Create a new rule where SVG is the preferred
    // source format and delete the old rule.
    newSheet.insertRule(rule.cssText.replace(/src:([^;]*),([^,]*svg[^,;]*)([^;]*)/g, 'src:$2,$1$3'), j);
    sheet.deleteRule(j);
    j--;
    }
    traverseSheet(rule.styleSheet);
    }
    }

    try {
    // NOTE Using these variables as aliases reduces the code size.
    var doc = document;
    var head = doc.head;
    // NOTE Safari needs the canvas to be part of the DOM tree to
    // return correct alpha values.
    var canvas = head.appendChild(doc.createElement('CANVAS'));
    var context = canvas.getContext('2d');
    context.textBaseline = 'top';
    context.font = '32px Arial';
    context.fillText('O', 0, 0);
    var rule, sheet, newSheet;
    // NOTE We won't check the alpha values of all canvas pixels,
    // but only the one at position (5,8). If font-smoothing is
    // off we loop through all CSS rules and fix them.
    for (var i = 0; (context.getImageData(5, 8, 1, 1).data[3] == 255) && (sheet = doc.styleSheets[i]) && (sheet != newSheet); i++) {
    // NOTE Opera only applies new stylesheets if they have been
    // added to a style-node created via JavaScript.
    newSheet = newSheet?newSheet:head.appendChild(doc.createElement('STYLE')).sheet;
    for (var j = 0; sheet && (rule = sheet.cssRules[j]); j++) {
    // NOTE Only affect rules that have a src value (AFAIK
    // this is only true for CSSFontfaceRule objects).
    if (rule.style && rule.style.src) {
    // NOTE Create a new rule where SVG is the preferred
    // source format and delete the old rule.
    newSheet.insertRule(rule.cssText.replace(/src:([^;]*),([^,]*svg[^,;]*)([^;]*)/g, 'src:$2,$1$3'), j);
    sheet.deleteRule(j);
    j--;
    }
    }
    }
    for (var i = 0; (context.getImageData(5, 8, 1, 1).data[3] == 255) && (sheet = doc.styleSheets[i]) && (sheet != newSheet); i++)
    traverseSheet(sheet);
    head.removeChild(canvas);
    }
    catch (e) {
    // NOTE Ignore any errors, which might occur.
    throw e;
    }
    }, false);
    4 changes: 2 additions & 2 deletions fontsmoothie.min.js
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    window.addEventListener&&window.addEventListener("load",function(){try{var d=document,g=d.head,h=g.appendChild(d.createElement("CANVAS")),e=h.getContext("2d");e.textBaseline="top";e.font="32px Arial";e.fillText("O",0,0);for(var f,a,b,j=0;255==e.getImageData(5,8,1,1).data[3]&&(a=d.styleSheets[j])&&a!=b;j++){b=b?b:g.appendChild(d.createElement("STYLE")).sheet;for(var c=0;a&&(f=a.cssRules[c]);c++)f.style&&f.style.src&&(b.insertRule(f.cssText.replace(/src:([^;]*),([^,]*svg[^,;]*)([^;]*)/g,"src:$2,$1$3"),
    c),a.deleteRule(c),c--)}g.removeChild(h)}catch(k){}},!1);
    window.addEventListener&&window.addEventListener("load",function(f,b){function h(a,c){for(var d=0;a&&(c=a.cssRules[d]);d++)c.style&&c.style.src&&(b=b?b:g.appendChild(e.createElement("STYLE")).sheet,b.insertRule(c.cssText.replace(/src:([^;]*),([^,]*svg[^,;]*)([^;]*)/g,"src:$2,$1$3"),d),a.deleteRule(d),d--),h(c.styleSheet)}var e=document,g=e.head;try{var j=g.appendChild(e.createElement("CANVAS")),a=j.getContext("2d");a.textBaseline="top";a.font="32px Arial";a.fillText("O",0,0);for(var k=0;255==a.getImageData(5,
    8,1,1).data[3]&&(f=e.styleSheets[k])&&f!=b;k++)h(f);g.removeChild(j)}catch(l){throw l;}},!1);
  13. Torben created this gist Mar 16, 2013.
    53 changes: 53 additions & 0 deletions fontsmoothie.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    // Font Smoothie 0.1.0 unstable
    //
    // Copyright 2013 Torben Haase <http://pixelsvsbytes.com/>
    //
    // Font Smoothie is free software: you can redistribute it and/or modify it under
    // the terms of the GNU Lesser General Public License as published by the Free
    // Software Foundation, either version 3 of the License, or (at your option) any
    // later version.
    //
    // Font Smoothie is distributed in the hope that it will be useful, but WITHOUT
    // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
    // details. You should have received a copy of the GNU Lesser General Public
    // License along with Font Smoothie. If not, see <http://www.gnu.org/licenses/>.

    window.addEventListener && window.addEventListener('load', function() {
    try {
    // NOTE Using these variables as aliases reduces the code size.
    var doc = document;
    var head = doc.head;
    // NOTE Safari needs the canvas to be part of the DOM tree to
    // return correct alpha values.
    var canvas = head.appendChild(doc.createElement('CANVAS'));
    var context = canvas.getContext('2d');
    context.textBaseline = 'top';
    context.font = '32px Arial';
    context.fillText('O', 0, 0);
    var rule, sheet, newSheet;
    // NOTE We won't check the alpha values of all canvas pixels,
    // but only the one at position (5,8). If font-smoothing is
    // off we loop through all CSS rules and fix them.
    for (var i = 0; (context.getImageData(5, 8, 1, 1).data[3] == 255) && (sheet = doc.styleSheets[i]) && (sheet != newSheet); i++) {
    // NOTE Opera only applies new stylesheets if they have been
    // added to a style-node created via JavaScript.
    newSheet = newSheet?newSheet:head.appendChild(doc.createElement('STYLE')).sheet;
    for (var j = 0; sheet && (rule = sheet.cssRules[j]); j++) {
    // NOTE Only affect rules that have a src value (AFAIK
    // this is only true for CSSFontfaceRule objects).
    if (rule.style && rule.style.src) {
    // NOTE Create a new rule where SVG is the preferred
    // source format and delete the old rule.
    newSheet.insertRule(rule.cssText.replace(/src:([^;]*),([^,]*svg[^,;]*)([^;]*)/g, 'src:$2,$1$3'), j);
    sheet.deleteRule(j);
    j--;
    }
    }
    }
    head.removeChild(canvas);
    }
    catch (e) {
    // NOTE Ignore any errors, which might occur.
    }
    }, false);
    2 changes: 2 additions & 0 deletions fontsmoothie.min.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    window.addEventListener&&window.addEventListener("load",function(){try{var d=document,g=d.head,h=g.appendChild(d.createElement("CANVAS")),e=h.getContext("2d");e.textBaseline="top";e.font="32px Arial";e.fillText("O",0,0);for(var f,a,b,j=0;255==e.getImageData(5,8,1,1).data[3]&&(a=d.styleSheets[j])&&a!=b;j++){b=b?b:g.appendChild(d.createElement("STYLE")).sheet;for(var c=0;a&&(f=a.cssRules[c]);c++)f.style&&f.style.src&&(b.insertRule(f.cssText.replace(/src:([^;]*),([^,]*svg[^,;]*)([^;]*)/g,"src:$2,$1$3"),
    c),a.deleteRule(c),c--)}g.removeChild(h)}catch(k){}},!1);