Skip to content

Instantly share code, notes, and snippets.

@antimatter15
Created September 12, 2010 17:08
Show Gist options
  • Save antimatter15/576244 to your computer and use it in GitHub Desktop.
Save antimatter15/576244 to your computer and use it in GitHub Desktop.
//A somewhat conservative test for supporting characters.
//Actually, it's a test to see if the glyphs of two characters
//are identical. This tends to work, however, some implementations
//may instead replace the text with a box with the hex code
//and this wouldn't work in those cases. One way might be to detect
//the presence of a rectangular box, but that may cause issues with
//glyphs that are rectangluar boxes (eg. Mail symbol).
//Basically, it compares pixels of the character at num and num+1
//if the glyphs are identical, it assumes its non-supported.
function supportsCharacter(num){
function text(chrnum){
//convert a character number into a ImageData.data array
var x = document.createElement('canvas').getContext('2d');
x.fillText(String.fromCharCode(chrnum),2,8);
return x.getImageData(0, 0, 11, 12).data;
}
var x = text(num), y = text(num+1);
for(var i = x.length; x[i] == y[i] && i--;); //loop while pixels are equal or reached end.
return i != -1 //if it reached the end, then the characters are the same
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment