]> git.mjollnir.org Git - moodle.git/commitdiff
Bug #5937 - Fix nonascii chars in graphs:
authorskodak <skodak>
Thu, 29 Jun 2006 21:22:16 +0000 (21:22 +0000)
committerskodak <skodak>
Thu, 29 Jun 2006 21:22:16 +0000 (21:22 +0000)
 * 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

lang/en_utf8/fonts/README.txt
lang/en_utf8/fonts/default.ttf [deleted file]
lib/graphlib.php
lib/textlib.class.php

index c71ce0467c60ed67dc4c2fe4e8bfbf904519895c..8af4e331d48ab003174367836ccf61fd8b01d549 100644 (file)
@@ -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 (executable)
index 58cd6b5..0000000
Binary files a/lang/en_utf8/fonts/default.ttf and /dev/null differ
index 712cc4c6caa1a3dc0fb3afbb80156385457f67ac..4feb2ce73ba07cd43e98e135aa75b41948d092d7 100644 (file)
@@ -1,4 +1,4 @@
-<?php
+<?php  // $Id$
 
 /*
 Graph Class. PHP Class to draw line, point, bar, and area graphs, including numeric x-axis and double y-axis.
@@ -154,35 +154,27 @@ function init() {
   global $CFG;
 
   /// A default.ttf is searched for in this order:
+  ///      dataroot/lang/xx_local/fonts
   ///      dataroot/lang/xx/fonts
   ///      dirroot/lang/xx/fonts
+  ///      dataroot/lang
   ///      lib/
 
   $currlang = current_language();
-  if (file_exists("$CFG->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) {
index 2c2012558b86ec952e05d294fd279b2d612bd557..add5e0649a918cd186aa37777d5b39066ea32670 100644 (file)
@@ -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;
+    }
+
 }
 ?>