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.
This code enforces font-smothing for web fonts even if it's not enabled in the system settings. More info: http://pixelsvsbytes.com/blog/2013/02/nice-web-fonts-for-every-browser
// 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);
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);
@Toutouwai
Copy link
Copy Markdown

IE8 gives a Javascript error: 'document.head' is null or not an object.

@skill83
Copy link
Copy Markdown

skill83 commented Jan 20, 2015

u can use document.getElementsByTagName('head')[0]

@fab1an
Copy link
Copy Markdown

fab1an commented Nov 20, 2015

Can you upload the script to CDNs like https://cdnjs.com/?

@las3r
Copy link
Copy Markdown

las3r commented Sep 11, 2016

I'd love a bower version for this as well if you'd like to do the honours... ;)

@zietbukuel
Copy link
Copy Markdown

Yes, a bower or npm version would be awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment