Skip to content

Commit

Permalink
Merge pull request #3923 from mapmeld/rtl_fix
Browse files Browse the repository at this point in the history
Support Arabic vowel signs, include more languages in generic RTL fix
  • Loading branch information
bhousel authored Mar 25, 2017
2 parents 31d2733 + fa87f2b commit ec68472
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,61 +62,87 @@ var chars = {
5000: { initial: 'ﻻ', isolated: 'ﻻ', medial: '', final: 'ﻼ' }
};

export var rtlRegex = /[\u0590-\u05FF\u0600-\u06FF\u0780-\u07BF]/;

export function fixArabicScriptTextForSvg(inputText) {
export function fixRTLTextForSvg(inputText) {
var context = true;
var ret = '';
var rtlBuffer = [];
var arabicRegex = /[\u0600-\u06FF]/g;
var arabicTashkil = /[\u0610-\u061A\u064B-\u065F\u0670\u06D6-\u06DC\u06DF-\u06E4\u06E7\u06E8\u06EA-\u06ED]/;
var thaanaVowel = /[\u07A6-\u07B0]/;
var hebrewSign = /[\u0591-\u05bd\u05bf\u05c1-\u05c5\u05c7]/;

for (var i = 0, l = inputText.length; i < l; i++) {
var code = inputText[i].charCodeAt(0);
var nextCode = inputText[i + 1] ? inputText[i + 1].charCodeAt(0) : 0;

if (!chars[code]) {
if (code === 32 && rtlBuffer.length) {
// whitespace
rtlBuffer = [rtlBuffer.reverse().join('') + ' '];
if (!arabicRegex.test(inputText)) {
// Hebrew or Thaana RTL script
for (var n = 0; n < inputText.length; n++) {
var c = inputText[n];
if ((thaanaVowel.test(c) || hebrewSign.test(c)) && rtlBuffer.length) {
rtlBuffer[rtlBuffer.length - 1] += c;
} else if (rtlRegex.test(c)) {
rtlBuffer.push(c);
} else if (c === ' ' && rtlBuffer.length) {
// whitespace within RTL text
rtlBuffer = [rtlBuffer.reverse().join('') + ' '];
} else {
// non-RTL character
ret += rtlBuffer.reverse().join('') + inputText[i];
rtlBuffer = [];
// non-RTL character
ret += rtlBuffer.reverse().join('') + c;
rtlBuffer = [];
}
continue;
}
if (context) {
if (i === l - 1 || nextCode === 32) {
rtlBuffer.push(chars[code].isolated);
} else {
// special case for لا
if (code === 1604 && nextCode === 1575) {
rtlBuffer.push(chars[5000].initial);
i++;
context = true;
continue;
} else {
for (var i = 0, l = inputText.length; i < l; i++) {
var code = inputText[i].charCodeAt(0);
var nextCode = inputText[i + 1] ? inputText[i + 1].charCodeAt(0) : 0;

if (!chars[code]) {
if (code === 32 && rtlBuffer.length) {
// whitespace
rtlBuffer = [rtlBuffer.reverse().join('') + ' '];
} else if (arabicTashkil.test(inputText[i]) && rtlBuffer.length) {
// tashkil mark
rtlBuffer[rtlBuffer.length - 1] += inputText[i];
} else {
// non-RTL character
ret += rtlBuffer.reverse().join('') + inputText[i];
rtlBuffer = [];
}
rtlBuffer.push(chars[code].initial);
continue;
}
} else {
if (i === l - 1 || nextCode === 32){
rtlBuffer.push(chars[code].final);
} else {
// special case for ﻼ
if (code === 1604 && nextCode === 1575){
rtlBuffer.push(chars[5000].final);
i++;
context = true;
continue;
if (context) {
if (i === l - 1 || nextCode === 32) {
rtlBuffer.push(chars[code].isolated);
} else {
// special case for لا
if (code === 1604 && nextCode === 1575) {
rtlBuffer.push(chars[5000].initial);
i++;
context = true;
continue;
}
rtlBuffer.push(chars[code].initial);
}
if (chars[code].medial === ''){
} else {
if (i === l - 1 || nextCode === 32){
rtlBuffer.push(chars[code].final);
} else {
rtlBuffer.push(chars[code].medial);
// special case for ﻼ
if (code === 1604 && nextCode === 1575){
rtlBuffer.push(chars[5000].final);
i++;
context = true;
continue;
}
if (chars[code].medial === ''){
rtlBuffer.push(chars[code].final);
} else {
rtlBuffer.push(chars[code].medial);
}
}
}
context = (chars[code].medial === '') || nextCode === 32;
}
context = (chars[code].medial === '') || nextCode === 32;
}

ret += rtlBuffer.reverse().join('');
return ret;
}
7 changes: 3 additions & 4 deletions modules/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as d3 from 'd3';
import { t, textDirection } from './locale';
import { utilDetect } from './detect';
import { remove as removeDiacritics } from 'diacritics';
import { fixArabicScriptTextForSvg } from './svg_paths_arabic_fix';
import { fixRTLTextForSvg, rtlRegex } from './svg_paths_rtl_fix';


export function utilTagText(entity) {
Expand Down Expand Up @@ -76,10 +76,9 @@ export function utilDisplayName(entity) {
export function utilDisplayNameForPath(entity) {
var name = utilDisplayName(entity);
var isFirefox = utilDetect().browser.toLowerCase().indexOf('firefox') > -1;
var arabicRegex = /[\u0600-\u06FF]/g;

if (!isFirefox && name && arabicRegex.test(name)) {
name = fixArabicScriptTextForSvg(name);
if (!isFirefox && name && rtlRegex.test(name)) {
name = fixRTLTextForSvg(name);
}

return name;
Expand Down

0 comments on commit ec68472

Please sign in to comment.