Skip to content

Commit

Permalink
Replace font checking method to use canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
lqez committed Nov 29, 2018
1 parent 6f7593b commit 5d4770b
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/js/base/core/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ const isSupportAmd = typeof define === 'function' && define.amd; // eslint-disab
*/
function isFontInstalled(fontName) {
const testFontName = fontName === 'Comic Sans MS' ? 'Courier New' : 'Comic Sans MS';
const $tester = $('<div>').css({
position: 'absolute',
left: '-9999px',
top: '-9999px',
fontSize: '200px',
}).text('mmmmmmmmmwwwwwww').appendTo(document.body);
const testText = 'mmmmmmmmmwwwwwww';
const testSize = '200px';

const originalWidth = $tester.css('fontFamily', testFontName).width();
const width = $tester.css('fontFamily', fontName + ',' + testFontName).width();
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');

$tester.remove();
context.font = testSize + " '" + testFontName + "'";
const originalWidth = context.measureText(testText).width;

context.font = testSize + " '" + fontName + "', '" + testFontName + "'";
const width = context.measureText(testText).width;

return originalWidth !== width;
}
Expand Down

0 comments on commit 5d4770b

Please sign in to comment.