laya-coreui-es
Version:
laya核心库、ui库es版本,剔除了媒体、TTF等功能。基于 LayaAir-release_2.12.0 修改
206 lines (205 loc) • 7.44 kB
JavaScript
export class ArabicReshaper {
characterMapContains(c) {
for (var i = 0; i < ArabicReshaper.charsMap.length; ++i) {
if (ArabicReshaper.charsMap[i][0] === c) {
return true;
}
}
return false;
}
getCharRep(c) {
for (var i = 0; i < ArabicReshaper.charsMap.length; ++i) {
if (ArabicReshaper.charsMap[i][0] === c) {
return ArabicReshaper.charsMap[i];
}
}
return false;
}
getCombCharRep(c1, c2) {
for (var i = 0; i < ArabicReshaper.combCharsMap.length; ++i) {
if (ArabicReshaper.combCharsMap[i][0][0] === c1 && ArabicReshaper.combCharsMap[i][0][1] === c2) {
return ArabicReshaper.combCharsMap[i];
}
}
return false;
}
isTransparent(c) {
for (var i = 0; i < ArabicReshaper.transChars.length; ++i) {
if (ArabicReshaper.transChars[i] === c) {
return true;
}
}
return false;
}
getOriginalCharsFromCode(code) {
var j;
for (j = 0; j < ArabicReshaper.charsMap.length; ++j) {
if (ArabicReshaper.charsMap[j].indexOf(code) > -1) {
return String.fromCharCode(ArabicReshaper.charsMap[j][0]);
}
}
for (j = 0; j < ArabicReshaper.combCharsMap.length; ++j) {
if (ArabicReshaper.combCharsMap[j].indexOf(code) > -1) {
return String.fromCharCode(ArabicReshaper.combCharsMap[j][0][0]) +
String.fromCharCode(ArabicReshaper.combCharsMap[j][0][1]);
}
}
return String.fromCharCode(code);
}
convertArabic(normal) {
var crep, combcrep, shaped = '';
for (var i = 0; i < normal.length; ++i) {
var current = normal.charCodeAt(i);
if (this.characterMapContains(current)) {
var prev = null, next = null, prevID = i - 1, nextID = i + 1;
for (; prevID >= 0; --prevID) {
if (!this.isTransparent(normal.charCodeAt(prevID))) {
break;
}
}
prev = (prevID >= 0) ? normal.charCodeAt(prevID) : null;
crep = prev ? this.getCharRep(prev) : false;
if (!crep || crep[2] == null && crep[3] == null) {
prev = null;
}
for (; nextID < normal.length; ++nextID) {
if (!this.isTransparent(normal.charCodeAt(nextID))) {
break;
}
}
next = (nextID < normal.length) ? normal.charCodeAt(nextID) : null;
crep = next ? this.getCharRep(next) : false;
if (!crep || crep[3] == null && crep[4] == null) {
next = null;
}
if (current === 0x0644 && next != null &&
(next === 0x0622 || next === 0x0623 || next === 0x0625 || next === 0x0627)) {
combcrep = this.getCombCharRep(current, next);
if (prev != null) {
shaped += String.fromCharCode(combcrep[4]);
}
else {
shaped += String.fromCharCode(combcrep[1]);
}
++i;
continue;
}
crep = this.getCharRep(current);
if (prev != null && next != null && crep[3] != null) {
shaped += String.fromCharCode(crep[3]);
continue;
}
else if (prev != null && crep[4] != null) {
shaped += String.fromCharCode(crep[4]);
continue;
}
else if (next != null && crep[2] != null) {
shaped += String.fromCharCode(crep[2]);
continue;
}
else {
shaped += String.fromCharCode(crep[1]);
}
}
else {
shaped += String.fromCharCode(current);
}
}
return shaped;
}
convertArabicBack(apfb) {
var toReturn = '', selectedChar;
var i;
for (i = 0; i < apfb.length; ++i) {
selectedChar = apfb.charCodeAt(i);
toReturn += this.getOriginalCharsFromCode(selectedChar);
}
return toReturn;
}
}
ArabicReshaper.charsMap = [[0x0621, 0xFE80, null, null, null],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]];
ArabicReshaper.combCharsMap = [[[0x0644, 0x0622], 0xFEF5, null, null, 0xFEF6],
[[0x0644, 0x0623], 0xFEF7, null, null, 0xFEF8],
[[0x0644, 0x0625], 0xFEF9, null, null, 0xFEFA],
[[0x0644, 0x0627], 0xFEFB, null, null, 0xFEFC]];
ArabicReshaper.transChars = [0x0610,
0x0612,
0x0613,
0x0614,
0x0615,
0x064B,
0x064C,
0x064D,
0x064E,
0x064F,
0x0650,
0x0651,
0x0652,
0x0653,
0x0654,
0x0655,
0x0656,
0x0657,
0x0658,
0x0670,
0x06D6,
0x06D7,
0x06D8,
0x06D9,
0x06DA,
0x06DB,
0x06DC,
0x06DF,
0x06E0,
0x06E1,
0x06E2,
0x06E3,
0x06E4,
0x06E7,
0x06E8,
0x06EA,
0x06EB,
0x06EC,
0x06ED];