Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F13305
testDetectChineseTofu.html
Public
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Authored By
•
bzimport
Nov 22 2014, 3:04 AM
2014-11-22 03:04:43 (UTC+0)
Size
1 KB
Referenced Files
None
Subscribers
None
testDetectChineseTofu.html
View Options
<!DOCTYPE html>
<
html
>
<
head
>
<
meta
charset
=
"utf-8"
/>
<
script
type
=
"text/javascript"
>
/**
* Convert a Chinese Hanzi character to image data
*
* Draw the character on canvas, and return the bitmap in format: rgba rgba ...
*
* @param {string} character
* @return {array}
*/
function
convertHanziToImageData
(
character
)
{
var
canv
=
document
.
createElement
(
"canvas"
);
canv
.
width
=
80
;
// 4+72+4, put the 72px char at center, and leave 4px as padding
canv
.
height
=
80
;
var
ctx
=
canv
.
getContext
(
"2d"
);
ctx
.
font
=
"72px 宋体"
;
ctx
.
fillText
(
character
,
4
,
68
);
var
imgData
=
ctx
.
getImageData
(
0
,
0
,
canv
.
width
,
canv
.
height
).
data
;
return
imgData
;
}
/**
* Detect Chinese tofu
*
* Create tofu's and the character's images, compare them to see if the
* character is rendered as a tofu
*
* @param {string} text
* @return {boolean}
*/
function
detectChineseTofu
(
character
)
{
// TODO: TOFU_IMAGE_DATA can be created only once
var
TOFU_IMAGE_DATA
=
convertHanziToImageData
(
"鿿"
);
// a known tofu: 鿿
var
characterImageData
=
convertHanziToImageData
(
character
);
for
(
var
i
=
0
;
i
<
TOFU_IMAGE_DATA
.
length
;
i
++
)
if
(
characterImageData
[
i
]
!=
TOFU_IMAGE_DATA
[
i
]
)
return
false
;
return
true
;
}
</
script
>
</
head
>
<
body
>
<
img
id
=
"canvasImg"
src
=
""
/>
<
script
>
function
testDetectChineseTofu
(
character
)
{
if
(
detectChineseTofu
(
character
)
)
alert
(
character
+
" is tofu"
);
else
alert
(
character
+
" is not tofu"
);
}
testDetectChineseTofu
(
"我"
);
testDetectChineseTofu
(
"鿾"
);
// another known tofu: 鿾
</
script
>
</
body
>
</
html
>
File Metadata
Details
Attached
Mime Type
text/html
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
12744
Default Alt Text
testDetectChineseTofu.html (1 KB)
Attached To
Mode
T65122: improve detectTofu algorithm so it can detect replacement characters in fixed-width glyphs
Attached
Detach File
Event Timeline
Log In to Comment