From: skodak Date: Thu, 29 Jun 2006 21:22:16 +0000 (+0000) Subject: Bug #5937 - Fix nonascii chars in graphs: X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=9c1cd39de57d1316d52d07a1892c374332165419;p=moodle.git Bug #5937 - Fix nonascii chars in graphs: * text is converted to unicode - no html entities anymore * then all nonascii chars are converted to decimal number entites * default.ttf is searched in dataroot/lang and local language packs too * removed lang_decode() - obsoleded by proper entity converison above * updated documentation in lang/en_utf8/fonts/README.txt merged from MOODLE_16_STABLE --- diff --git a/lang/en_utf8/fonts/README.txt b/lang/en_utf8/fonts/README.txt index c71ce0467c..8af4e331d4 100644 --- a/lang/en_utf8/fonts/README.txt +++ b/lang/en_utf8/fonts/README.txt @@ -1,16 +1,19 @@ Fonts ----- -This directory contains fonts that are used when creating -images with text. +GD font operations need unicode capable font to do the server-side +rendering of text. Default font lib/default.ttf (FreeSans Medium +revision 1.76 from http://savannah.nongnu.org/projects/freefont/) +is suitable for most languages. -The only one used currently is default.ttf +Unsupported languages may add special font +lang/xx_utf8/fonts/default.ttf, it will be used for that language +only. -If a language doesn't have the font here then the one in -/lang/en/fonts/default.ttf is used instead. - -Multibyte strings will need decoding, because the Truetype -routines expect ISO fonts or Unicode strings. If there is a -file here called lang_decode.php, containing a function -called lang_decode(), then it will be used on each string. +If you want to override the default font dirroot/lib/default.ttf, +save another one as dataroot/lang/default.ttf - it will +be used for all site languages without it's font file. +The list of suitable TrueType fonts can be found at: +* http://en.wikipedia.org/wiki/Unicode_fonts +* http://www.alanwood.net/unicode/fonts.html#general diff --git a/lang/en_utf8/fonts/default.ttf b/lang/en_utf8/fonts/default.ttf deleted file mode 100755 index 58cd6b5e61..0000000000 Binary files a/lang/en_utf8/fonts/default.ttf and /dev/null differ diff --git a/lib/graphlib.php b/lib/graphlib.php index 712cc4c6ca..4feb2ce73b 100644 --- a/lib/graphlib.php +++ b/lib/graphlib.php @@ -1,4 +1,4 @@ -dataroot/lang/$currlang/fonts/default.ttf")) { + if (file_exists("$CFG->dataroot/lang/".$currlang."_local/fonts/default.ttf")) { + $fontpath = "$CFG->dataroot/lang/".$currlang."_local/fonts/"; + } else if (file_exists("$CFG->dataroot/lang/$currlang/fonts/default.ttf")) { $fontpath = "$CFG->dataroot/lang/$currlang/fonts/"; } else if (file_exists("$CFG->dirroot/lang/$currlang/fonts/default.ttf")) { $fontpath = "$CFG->dirroot/lang/$currlang/fonts/"; + } else if (file_exists("$CFG->dataroot/lang/default.ttf")) { + $fontpath = "$CFG->dataroot/lang/"; } else { $fontpath = "$CFG->libdir/"; } $this->parameter['path_to_fonts'] = $fontpath; - if (file_exists("$fontpath"."lang_decode.php")) { - $this->parameter['lang_decode'] = "$fontpath"."lang_decode.php"; - } else { - $this->parameter['lang_decode'] = ""; - } - - $this->parameter['lang_transcode'] = ''; /// by default - - $charset = strtolower(current_charset()); - - if ($charset != 'iso-8859-1' and $charset != 'utf-8') { - $this->parameter['lang_transcode'] = $charset; - } - /// End Moodle mods @@ -1245,13 +1237,11 @@ function print_TTF($message) { $x = 0; break; } - if ($this->parameter['lang_decode']) { // Moodle addition - include_once($this->parameter['lang_decode']); - $text = lang_decode($text); - - } else if ($this->parameter['lang_transcode']) { - $text = iconv($this->parameter['lang_transcode'], 'UTF-8', $text); - } + // start of Moodle addition + $textlib = textlib_get_instance(); + $text = $textlib->convert($text, current_charset(), 'UTF-8'); + $text = $textlib->utf8_to_entities($text, true, true); //does not work with hex entities! + // end of Moodle addition ImageTTFText($this->image, $points, $angle, $x, $y, $colour, $font, $text); } @@ -1354,13 +1344,11 @@ function get_boundaryBox($message) { } // get boundary box and offsets for printing at an angle - if ($this->parameter['lang_decode']) { // Moodle addition - include_once($this->parameter['lang_decode']); - $text = lang_decode($text); - - } else if ($this->parameter['lang_transcode']) { - $text = iconv($this->parameter['lang_transcode'], 'UTF-8', $text); - } + // start of Moodle addition + $textlib = textlib_get_instance(); + $text = $textlib->convert($text, current_charset(), 'UTF-8'); + $text = $textlib->utf8_to_entities($text, true, true); //gd does not work with hex entities! + // end of Moodle addition $bounds = ImageTTFBBox($points, $angle, $font, $text); if ($angle < 0) { diff --git a/lib/textlib.class.php b/lib/textlib.class.php index 2c2012558b..add5e0649a 100644 --- a/lib/textlib.class.php +++ b/lib/textlib.class.php @@ -231,5 +231,29 @@ class textlib { return $encoded; } + + /** + * Converts all Unicode chars > 127 to numeric entities &#nnnn; or &#xnnn;. + * + * @param string input string + * @param boolean output decadic only number entities + * @param boolean remove all nonumeric entities + * @return string converted string + */ + function utf8_to_entities($str, $dec=false, $nonnum=false) { + /// Avoid some notices from Typo3 code + $oldlevel = error_reporting(E_PARSE); + if ($nonnum) { + $str = $this->typo3cs->entities_to_utf8($str, true); + } + $result = $this->typo3cs->utf8_to_entities($str); + if ($dec) { + $result = preg_replace('/&#x([0-9a-f]+);/ie', "'&#'.hexdec('$1').';'", $result); + } + /// Restore original debug level + error_reporting($oldlevel); + return $result; + } + } ?>