forked from santhoshtr/tofudetector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tofudetector.js
57 lines (50 loc) · 1.25 KB
/
tofudetector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Detect tofu
*
* Create a temporary span in the page with fontsize 72px and font-family
* sans-serif for each letter of the text.
* For each of these spans, calculate the width and height. If they are same
* for all spans, we can understand that each of the letter is rendered using
* same glyph - it must be a tofu.
*
* @param {string} text
* @return {boolean}
*/
function detectTofu( text, algorithm, runLength ) {
var index,
$fixture,
width = {},
height = {},
fontFamily,
length,
detected = false;
runLength = runLength? runLength: 4;
length = Math.min( runLength, text.length ),
fontFamily = 'sans-serif';
if (algorithm === 'tofu-font' ) {
fontFamily += ',tofu';
}
$fixture = $( '<span>' )
.css( {
fontSize: '72px',
fontFamily: fontFamily
} )
.appendTo( 'body' );
for ( index = 0; index < length; index++ ) {
$fixture.text( text[index] );
width[index] = $fixture.width() || width[index-1];
height[index] = $fixture.height();
if( index > 0 &&
( width[index] !== width[index - 1] ||
height[index] !== height[index - 1] )
) {
detected = false;
break;
}
}
$fixture.remove();
if ( index === length ) {
detected = true;
}
return detected;
}