From 94e2dc1dafaa05ae0315911e033585f09e41cf01 Mon Sep 17 00:00:00 2001 From: Nadav Kavalerchik Date: Wed, 9 Jul 2014 14:57:31 +0300 Subject: [PATCH] MDL-19270 - Quiz graph - Fix reverse display of Hebrew legend text in RTL mode --- lib/classes/text.php | 12 ++++++++++++ lib/graphlib.php | 18 +++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/classes/text.php b/lib/classes/text.php index 8e730cb6c92..b99b5d6fc6c 100644 --- a/lib/classes/text.php +++ b/lib/classes/text.php @@ -378,6 +378,18 @@ class core_text { } } + /** + * Reverse UTF-8 multibytes character sets (used for RTL languages) + * (We only do this because there is no mb_strrev or iconv_strrev) + * + * @param string $str the multibyte string to reverse + * @return string the reversed multi byte string + */ + public static function strrev($str) { + preg_match_all('/./us', $str, $ar); + return join('', array_reverse($ar[0])); + } + /** * Try to convert upper unicode characters to plain ascii, * the returned string may contain unconverted unicode characters. diff --git a/lib/graphlib.php b/lib/graphlib.php index 0fa5eef9593..5b11a82fa66 100644 --- a/lib/graphlib.php +++ b/lib/graphlib.php @@ -1190,10 +1190,26 @@ class graph { //ImageColorTransparent($this->image, $this->colour['white']); // colour for transparency } + /** + * Prepare label's text for GD output. + * + * @param string $label string to be prepared. + * @return string Reversed input string, if we are in RTL mode and has no numbers. + * Otherwise, returns the string as is. + */ + private function prepare_label_text($label) { + if (right_to_left() and !preg_match('/[0-9]/i', $label)) { + return core_text::strrev($label); + } else { + return $label; + } + } + function print_TTF($message) { $points = $message['points']; $angle = $message['angle']; - $text = $message['text']; + // We have to manually reverse the label, since php GD cannot handle RTL characters properly in UTF8 strings. + $text = $this->prepare_label_text($message['text']); $colour = $this->colour[$message['colour']]; $font = $this->parameter['path_to_fonts'].$message['font'];