PageRenderTime 65ms CodeModel.GetById 41ms RepoModel.GetById 1ms app.codeStats 0ms

/pdf/html2ps/manager.encoding.php

https://github.com/williamjmendoza/sigecost
PHP | 297 lines | 201 code | 51 blank | 45 comment | 29 complexity | f6a7de12d7fdbdeb2583eba397c846cd MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  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 = array();
  29. /**
  30. * Number of the current custom encoding vector
  31. */
  32. var $_custom_vector_index = 0;
  33. var $_utf8_mapping;
  34. function ManagerEncoding() {
  35. $this->new_custom_encoding_vector();
  36. }
  37. /**
  38. * Add new custom symbol not present in the existing encoding
  39. * vectors.
  40. *
  41. * Note: encoding vector this character was placed to should be
  42. * extracted via get_current_custom_encoding_name immediately after
  43. * add_custom_char call.
  44. *
  45. * @param char[2] $char UCS-2 character (represented as 2-octet
  46. * string)
  47. *
  48. * @return char index of this character in custom encoding vector
  49. */
  50. function add_custom_char($char) {
  51. // Check if current encoding vector is full; if it is, we should
  52. // add a new one.
  53. if ($this->is_custom_encoding_full()) {
  54. $this->new_custom_encoding_vector();
  55. };
  56. // Get name of the custom encoding where new character should be
  57. // placed
  58. $vector_name = $this->get_current_custom_encoding_name();
  59. // Get (zero-based) index of this character in the encoding vector
  60. $index = count($this->_encodings[$vector_name]);
  61. // Add new character to the custom encoding vector
  62. $this->_encodings[$vector_name][chr($index)] = $char;
  63. // Add new character to the UTF8 mapping table
  64. $this->_utf8_mapping[code_to_utf8($char)][$vector_name] = chr($index);
  65. return chr($index);
  66. }
  67. function generate_mapping($mapping_file) {
  68. global $g_utf8_converters;
  69. $this->_utf8_mapping = array();
  70. foreach (array_keys($g_utf8_converters) as $encoding) {
  71. $flipped = array_flip($g_utf8_converters[$encoding][0]);
  72. foreach ($flipped as $utf => $code) {
  73. $this->_utf8_mapping[code_to_utf8($utf)][$encoding] = $code;
  74. };
  75. };
  76. $file = fopen($mapping_file,'w');
  77. fwrite($file, serialize($this->_utf8_mapping));
  78. fclose($file);
  79. }
  80. function &get() {
  81. global $g_manager_encodings;
  82. return $g_manager_encodings;
  83. }
  84. function get_canonized_encoding_name($encoding) {
  85. global $g_encoding_aliases;
  86. if (isset($g_encoding_aliases[$encoding])) {
  87. return $g_encoding_aliases[$encoding];
  88. };
  89. return $encoding;
  90. }
  91. function get_current_custom_encoding_name() {
  92. return $this->get_custom_encoding_name($this->get_custom_vector_index());
  93. }
  94. function get_custom_encoding_name($index) {
  95. return sprintf('custom%d',
  96. $index);
  97. }
  98. function get_custom_vector_index() {
  99. return $this->_custom_vector_index;
  100. }
  101. function get_encoding_glyphs($encoding) {
  102. $vector = $this->get_encoding_vector($encoding);
  103. if (is_null($vector)) {
  104. error_log(sprintf("Cannot get encoding vector for encoding '%s'", $encoding));
  105. return null;
  106. };
  107. return $this->vector_to_glyphs($vector);
  108. }
  109. /**
  110. * Get an encoding vector (array containing 256 elements; every
  111. * element is an ucs-2 encoded character)
  112. *
  113. * @param $encoding Encoding name
  114. *
  115. * @return Array encoding vector; null if this encoding is not known to the script
  116. */
  117. function get_encoding_vector($encoding) {
  118. $encoding = $this->get_canonized_encoding_name($encoding);
  119. global $g_utf8_converters;
  120. if (isset($g_utf8_converters[$encoding])) {
  121. $vector = $g_utf8_converters[$encoding][0];
  122. } elseif (isset($this->_encodings[$encoding])) {
  123. $vector = $this->_encodings[$encoding];
  124. } else {
  125. return null;
  126. };
  127. for ($i = 0; $i <= 255; $i++) {
  128. if (!isset($vector[chr($i)])) {
  129. $vector[chr($i)] = 0xFFFF;
  130. };
  131. };
  132. return $vector;
  133. }
  134. function get_glyph_to_code_mapping($encoding) {
  135. $vector = $this->get_encoding_vector($encoding);
  136. $result = array();
  137. foreach ($vector as $code => $uccode) {
  138. if (isset($GLOBALS['g_unicode_glyphs'][$uccode])) {
  139. $result[$GLOBALS['g_unicode_glyphs'][$uccode]][] = $code;
  140. };
  141. };
  142. return $result;
  143. }
  144. function get_mapping($char) {
  145. if (!isset($this->_utf8_mapping)) {
  146. $this->load_mapping(CACHE_DIR . 'utf8.mappings.dat');
  147. };
  148. if (!isset($this->_utf8_mapping[$char])) {
  149. return null;
  150. };
  151. return $this->_utf8_mapping[$char];
  152. }
  153. function get_next_utf8_char($raw_content, &$ptr) {
  154. if ((ord($raw_content[$ptr]) & 0xF0) == 0xF0) {
  155. $charlen = 4;
  156. } elseif ((ord($raw_content[$ptr]) & 0xE0) == 0xE0) {
  157. $charlen = 3;
  158. } elseif ((ord($raw_content[$ptr]) & 0xC0) == 0xC0) {
  159. $charlen = 2;
  160. } else {
  161. $charlen = 1;
  162. };
  163. $char = substr($raw_content,$ptr,$charlen);
  164. $ptr += $charlen;
  165. return $char;
  166. }
  167. function get_ps_encoding_vector($encoding) {
  168. $vector = $this->get_encoding_vector($encoding);
  169. $result = "/".$encoding." [ \n";
  170. for ($i=0; $i<256; $i++) {
  171. if ($i % 10 == 0) { $result .= "\n"; };
  172. // ! Note the order of array checking; optimizing interpreters may break this
  173. if (isset($vector[chr($i)]) && isset($GLOBALS['g_unicode_glyphs'][$vector[chr($i)]])) {
  174. $result .= " /".$GLOBALS['g_unicode_glyphs'][$vector[chr($i)]];
  175. } else {
  176. $result .= " /.notdef";
  177. };
  178. };
  179. $result .= " ] readonly def";
  180. return $result;
  181. }
  182. function is_custom_encoding($encoding) {
  183. return preg_match('/^custom\d+$/', $encoding);
  184. }
  185. function is_custom_encoding_full() {
  186. return count($this->_encodings[$this->get_current_custom_encoding_name()]) >= 256;
  187. }
  188. function load_mapping($mapping_file) {
  189. if (!is_readable($mapping_file)) {
  190. $this->generate_mapping($mapping_file);
  191. } else {
  192. $this->_utf8_mapping = unserialize(file_get_contents($mapping_file));
  193. };
  194. }
  195. /**
  196. * Create new custom 256-characters encoding vector. Reserve first
  197. * 32 symbols for system use.
  198. *
  199. * Custom encoding vectors have names 'customX' when X stand for the
  200. * encoding index.
  201. */
  202. function new_custom_encoding_vector() {
  203. $initial_vector = array();
  204. for ($i = 0; $i <= 32; $i++) {
  205. $initial_vector[chr($i)] = chr($i);
  206. };
  207. $this->register_encoding(sprintf('custom%d',
  208. $this->next_custom_vector_index()),
  209. $initial_vector);
  210. }
  211. /**
  212. * Returns index for the next custom encoding
  213. */
  214. function next_custom_vector_index() {
  215. return ++$this->_custom_vector_index;
  216. }
  217. function register_encoding($name, $vector) {
  218. $this->_encodings[$name] = $vector;
  219. }
  220. function to_utf8($word, $encoding) {
  221. $vector = $this->get_encoding_vector($encoding);
  222. $converted = '';
  223. for ($i=0, $size=strlen($word); $i < $size; $i++) {
  224. $converted .= code_to_utf8($vector[$word{$i}]);
  225. };
  226. return $converted;
  227. }
  228. function vector_to_glyphs($vector) {
  229. $result = array();
  230. foreach ($vector as $code => $ucs2) {
  231. if (isset($GLOBALS['g_unicode_glyphs'][$ucs2])) {
  232. $result[$code] = $GLOBALS['g_unicode_glyphs'][$ucs2];
  233. } elseif ($ucs2 == 0xFFFF) {
  234. $result[$code] = ".notdef";
  235. } else {
  236. // Use "Unicode and Glyph Names" mapping from Adobe
  237. // http://partners.adobe.com/public/developer/opentype/index_glyph.html
  238. $result[$code] = sprintf("u%04X", $ucs2);
  239. };
  240. };
  241. return $result;
  242. }
  243. }
  244. global $g_manager_encodings;
  245. $g_manager_encodings = new ManagerEncoding;
  246. ?>