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.2.1 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() {
"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.
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 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');
// 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);
}
}
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 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++)
traverseSheet(sheet);
head.removeChild(canvas);
}
catch (e) {
// NOTE Ignore any errors, which might occur.
// throw e;
}
}, false);
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);
@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