Skip to content

Instantly share code, notes, and snippets.

@subzey
Created March 13, 2012 18:24
Show Gist options
  • Save subzey/2030480 to your computer and use it in GitHub Desktop.
Save subzey/2030480 to your computer and use it in GitHub Desktop.
Firefox bug #733698 workaround
(function(){
var FakeTextMetrics,
proto,
fontSetterNative,
measureTextNative,
fillTextNative,
strokeTextNative;
if (
!window.CanvasRenderingContext2D ||
!window.TextMetrics ||
!(proto = window.CanvasRenderingContext2D.prototype) ||
!proto.hasOwnProperty("font") ||
!proto.hasOwnProperty("mozTextStyle") ||
typeof proto.__lookupSetter__ !== "function" ||
!(fontSetterNative = proto.__lookupSetter__("font"))
){
return;
}
proto.__defineSetter__("font", function(value){
try {
return fontSetterNative.call(this, value);
} catch (e){
if (e.name !== 'NS_ERROR_FAILURE'){
throw e;
}
}
});
measureTextNative = proto.measureText;
FakeTextMetrics = function(){
this.width = 0;
this.isFake = true;
this.__proto__ = window.TextMetrics.prototype;
};
proto.measureText = function($0){
try {
return measureTextNative.apply(this, arguments);
} catch (e) {
if (e.name !== 'NS_ERROR_FAILURE'){
throw e;
} else {
return new FakeTextMetrics();
}
}
};
fillTextNative = proto.fillText;
proto.fillText = function($0, $1, $2, $3){
try {
fillTextNative.apply(this, arguments);
} catch (e) {
if (e.name !== 'NS_ERROR_FAILURE'){
throw e;
}
}
};
strokeTextNative = proto.strokeText;
proto.strokeText = function($0, $1, $2, $3){
try {
strokeTextNative.apply(this, arguments);
} catch (e) {
if (e.name !== 'NS_ERROR_FAILURE'){
throw e;
}
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment