PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/pub/System/ToPDFPlugin/html2pdf/manager.encoding.php

https://github.com/foswiki/ToPDFPlugin
PHP | 257 lines | 190 code | 47 blank | 20 comment | 30 complexity | 3e76373d129f546c99ed938db0d41dc6 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. require_once(HTML2PS_DIR.'encoding.inc.php');
  3. require_once(HTML2PS_DIR.'encoding.entities.inc.php');
  4. require_once(HTML2PS_DIR.'encoding.glyphs.inc.php');
  5. require_once(HTML2PS_DIR.'encoding.iso-8859-1.inc.php');
  6. require_once(HTML2PS_DIR.'encoding.iso-8859-2.inc.php');
  7. require_once(HTML2PS_DIR.'encoding.iso-8859-3.inc.php');
  8. require_once(HTML2PS_DIR.'encoding.iso-8859-4.inc.php');
  9. require_once(HTML2PS_DIR.'encoding.iso-8859-5.inc.php');
  10. require_once(HTML2PS_DIR.'encoding.iso-8859-6.inc.php');
  11. require_once(HTML2PS_DIR.'encoding.iso-8859-7.inc.php');
  12. require_once(HTML2PS_DIR.'encoding.iso-8859-8.inc.php');
  13. require_once(HTML2PS_DIR.'encoding.iso-8859-9.inc.php');
  14. require_once(HTML2PS_DIR.'encoding.iso-8859-10.inc.php');
  15. require_once(HTML2PS_DIR.'encoding.iso-8859-11.inc.php');
  16. require_once(HTML2PS_DIR.'encoding.iso-8859-13.inc.php');
  17. require_once(HTML2PS_DIR.'encoding.iso-8859-14.inc.php');
  18. require_once(HTML2PS_DIR.'encoding.iso-8859-15.inc.php');
  19. require_once(HTML2PS_DIR.'encoding.koi8-r.inc.php');
  20. require_once(HTML2PS_DIR.'encoding.cp866.inc.php');
  21. require_once(HTML2PS_DIR.'encoding.windows-1250.inc.php');
  22. require_once(HTML2PS_DIR.'encoding.windows-1251.inc.php');
  23. require_once(HTML2PS_DIR.'encoding.windows-1252.inc.php');
  24. require_once(HTML2PS_DIR.'encoding.dingbats.inc.php');
  25. require_once(HTML2PS_DIR.'encoding.symbol.inc.php');
  26. // TODO: this works for PS encoding names only
  27. class ManagerEncoding {
  28. var $_encodings;
  29. var $_custom_vector_name;
  30. var $_utf8_mapping;
  31. function toUTF8($word, $encoding) {
  32. $vector = $this->getEncodingVector($encoding);
  33. $converted = '';
  34. for ($i=0, $size=strlen($word); $i < $size; $i++) {
  35. $converted .= code_to_utf8($vector[$word{$i}]);
  36. };
  37. return $converted;
  38. }
  39. function getMapping($char) {
  40. if (!isset($this->_utf8_mapping)) {
  41. $this->_loadMapping(CACHE_DIR . 'utf8.mappings.dat');
  42. };
  43. if (!isset($this->_utf8_mapping[$char])) {
  44. return null;
  45. };
  46. return $this->_utf8_mapping[$char];
  47. }
  48. function _loadMapping($mapping_file) {
  49. if (!is_readable($mapping_file)) {
  50. $this->_generateMapping($mapping_file);
  51. } else {
  52. $this->_utf8_mapping = unserialize(file_get_contents($mapping_file));
  53. };
  54. }
  55. function _generateMapping($mapping_file) {
  56. global $g_utf8_converters;
  57. $this->_utf8_mapping = array();
  58. foreach (array_keys($g_utf8_converters) as $encoding) {
  59. $flipped = array_flip($g_utf8_converters[$encoding][0]);
  60. foreach ($flipped as $utf => $code) {
  61. $this->_utf8_mapping[code_to_utf8($utf)][$encoding] = $code;
  62. };
  63. };
  64. $file = fopen($mapping_file,'w');
  65. fwrite($file, serialize($this->_utf8_mapping));
  66. fclose($file);
  67. }
  68. function ManagerEncoding() {
  69. $this->_encodings = array();
  70. $this->registerCustomEncoding("custom", array(0,1,2,3,4,5,6,7,8,9,10,
  71. 11,12,13,14,15,16,17,18,19,20,
  72. 21,22,23,24,25,26,27,28,29,30,
  73. 31,32));
  74. }
  75. function getCanonizedEncodingName($encoding) {
  76. global $g_encoding_aliases;
  77. if (isset($g_encoding_aliases[$encoding])) {
  78. return $g_encoding_aliases[$encoding];
  79. };
  80. return $encoding;
  81. }
  82. function registerCustomEncoding($name, $vector) {
  83. $this->registerEncoding($name, $vector);
  84. $this->_custom_vector_name = $name;
  85. }
  86. function getCustomEncodingName() {
  87. return $this->_custom_vector_name;
  88. }
  89. function getCustomEncodingVector() {
  90. return $this->_encodings[$this->getCustomEncodingName()];
  91. }
  92. function registerEncoding($name, $vector) {
  93. $this->_encodings[$name] = $vector;
  94. }
  95. /**
  96. * @TODO: handle more than 256 custom characters
  97. */
  98. function addCustomChar($char) {
  99. $vector_name = $this->getCustomEncodingName();
  100. $index = count($this->_encodings[$vector_name]);
  101. $this->_encodings[$vector_name][$index] = $char;
  102. $this->_utf8_mapping[code_to_utf8($char)]['custom'] = chr($index);
  103. return chr($index);
  104. }
  105. /**
  106. * Get an encoding vector (array containing 256 elements; every
  107. * element is an ucs-2 encoded character)
  108. *
  109. * @param $encoding Encoding name
  110. *
  111. * @return Array encoding vector; null if this encoding is not known to the script
  112. */
  113. function getEncodingVector($encoding) {
  114. $encoding = $this->getCanonizedEncodingName($encoding);
  115. /**
  116. * @TODO: HACK. Currently custom encoding and "standard" encodings
  117. * are handled separately, so we must explicitly check if current
  118. * encoding is custom
  119. */
  120. if ($encoding == $this->getCustomEncodingName()) {
  121. $vector = array();
  122. $custom_vector = $this->getCustomEncodingVector();
  123. $size = count($custom_vector);
  124. for ($i=0; $i<$size; $i++) {
  125. $vector[chr($i)] = $custom_vector[$i];
  126. };
  127. } else {
  128. global $g_utf8_converters;
  129. if (!isset($g_utf8_converters[$encoding])) {
  130. return null;
  131. };
  132. $vector = $g_utf8_converters[$encoding][0];
  133. };
  134. for ($i=0; $i<=255; $i++) {
  135. if (!isset($vector[chr($i)])) {
  136. $vector[chr($i)] = 0xFFFF;
  137. };
  138. };
  139. return $vector;
  140. }
  141. function &get() {
  142. global $g_manager_encodings;
  143. return $g_manager_encodings;
  144. }
  145. function get_encoding_glyphs($encoding) {
  146. $vector = $this->getEncodingVector($encoding);
  147. if (is_null($vector)) {
  148. error_log(sprintf("Cannot get encoding vector for encoding '%s'", $encoding));
  149. return null;
  150. };
  151. return $this->vector_to_glyphs($vector);
  152. }
  153. function get_glyph_to_code_mapping($encoding) {
  154. $vector = $this->getEncodingVector($encoding);
  155. $result = array();
  156. foreach ($vector as $code => $uccode) {
  157. if (isset($GLOBALS['g_unicode_glyphs'][$uccode])) {
  158. $result[$GLOBALS['g_unicode_glyphs'][$uccode]][] = $code;
  159. };
  160. };
  161. return $result;
  162. }
  163. function vector_to_glyphs($vector) {
  164. $result = array();
  165. foreach ($vector as $code => $ucs2) {
  166. if (isset($GLOBALS['g_unicode_glyphs'][$ucs2])) {
  167. $result[$code] = $GLOBALS['g_unicode_glyphs'][$ucs2];
  168. } elseif ($ucs2 == 0xFFFF) {
  169. $result[$code] = ".notdef";
  170. } else {
  171. // Use "Unicode and Glyph Names" mapping from Adobe
  172. // http://partners.adobe.com/public/developer/opentype/index_glyph.html
  173. $result[$code] = sprintf("u%04X", $ucs2);
  174. };
  175. };
  176. return $result;
  177. }
  178. function get_ps_encoding_vector($encoding) {
  179. $vector = $this->getEncodingVector($encoding);
  180. $result = "/".$encoding." [ \n";
  181. for ($i=0; $i<256; $i++) {
  182. if ($i % 10 == 0) { $result .= "\n"; };
  183. // ! Note the order of array checking; optimizing interpreters may break this
  184. if (isset($vector[chr($i)]) && isset($GLOBALS['g_unicode_glyphs'][$vector[chr($i)]])) {
  185. $result .= " /".$GLOBALS['g_unicode_glyphs'][$vector[chr($i)]];
  186. } else {
  187. $result .= " /.notdef";
  188. };
  189. };
  190. $result .= " ] readonly def";
  191. return $result;
  192. }
  193. function getNextUTF8Char($raw_content, &$ptr) {
  194. if ((ord($raw_content[$ptr]) & 0xF0) == 0xF0) {
  195. $charlen = 4;
  196. } elseif ((ord($raw_content[$ptr]) & 0xE0) == 0xE0) {
  197. $charlen = 3;
  198. } elseif ((ord($raw_content[$ptr]) & 0xC0) == 0xC0) {
  199. $charlen = 2;
  200. } else {
  201. $charlen = 1;
  202. };
  203. $char = substr($raw_content,$ptr,$charlen);
  204. $ptr += $charlen;
  205. return $char;
  206. }
  207. }
  208. global $g_manager_encodings;
  209. $g_manager_encodings = new ManagerEncoding;
  210. ?>