diff options
Diffstat (limited to 'includes/functions/functions_utf-8.php')
| -rw-r--r-- | includes/functions/functions_utf-8.php | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/includes/functions/functions_utf-8.php b/includes/functions/functions_utf-8.php index 460c881fc3..0417b42fb9 100644 --- a/includes/functions/functions_utf-8.php +++ b/includes/functions/functions_utf-8.php @@ -23,6 +23,10 @@ if (!defined('WT_WEBTREES')) { exit; } +// This is a list of parentheses, which need special RTL logic. +define('WT_UTF8_PARENTHESES1', ')(][}{><»«﴾﴿‹›“”‘’'); +define('WT_UTF8_PARENTHESES2', '()[]{}<>«»﴿﴾›‹”“’‘'); + function utf8_strtoupper($string) { global $ALPHABET_lower, $ALPHABET_upper; // Language-specific conversions, e.g. Turkish dotless i @@ -195,6 +199,51 @@ function utf8_strcasecmp($string1, $string2) { return ($strlen1-$strpos1)-($strlen2-$strpos2); } +/* + * Function to reverse RTL text for proper appearance on charts. + * + * GoogleChart and the GD library don't handle RTL text properly. They assume that all text is LTR. + * This function reverses the input text so that it will appear properly when rendered by GoogleChart + * and by the GD library (the Circle Diagram). + * + * Note 1: Numbers must always be rendered LTR, even when the rest of the text is RTL. + * Note 2: The visual direction of paired characters such as parentheses, brackets, directional + * quotation marks, etc. must be reversed so that the appearance of the RTL text is preserved. + */ +function reverseText($text) { + $text = strip_tags(html_entity_decode($text,ENT_COMPAT,'UTF-8')); + $text = str_replace(array('‎', '‏', WT_UTF8_LRM, WT_UTF8_RLM), '', $text); + $textLanguage = WT_I18N::textScript($text); + if ($textLanguage!='Hebr' && $textLanguage!='Arab') return $text; + + $reversedText = ''; + $numbers = ''; + while ($text!='') { + $charLen = 1; + $letter = substr($text, 0, 1); + if ((ord($letter) & 0xE0) == 0xC0) $charLen = 2; // 2-byte sequence + if ((ord($letter) & 0xF0) == 0xE0) $charLen = 3; // 3-byte sequence + if ((ord($letter) & 0xF8) == 0xF0) $charLen = 4; // 4-byte sequence + + $letter = substr($text, 0, $charLen); + $text = substr($text, $charLen); + if (strpos(WT_UTF8_DIGITS, $letter)!==false) { + $numbers .= $letter; // accumulate numbers in LTR mode + } else { + $reversedText = $numbers.$reversedText; // emit any waiting LTR numbers now + $numbers = ''; + if (strpos(WT_UTF8_PARENTHESES1, $letter)!==false) { + $reversedText = substr(WT_UTF8_PARENTHESES2, strpos(WT_UTF8_PARENTHESES1, $letter), strlen($letter)).$reversedText; + } else { + $reversedText = $letter.$reversedText; + } + } + } + + $reversedText = $numbers.$reversedText; // emit any waiting LTR numbers now + return $reversedText; +} + // This is a list of all reversable character conversions from the UNICODE 5.1 database. // It excludes ambiguous (dotless i) and mixed-case (Dz) characters. // The characters should be arranged in default unicode-collation order. |
