gen_eci_sb_h.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /* Generate ECI single-byte tables & routines from unicode.org mapping files */
  3. /*
  4. libzint - the open source barcode library
  5. Copyright (C) 2022-2023 Robin Stuart <rstuart114@gmail.com>
  6. */
  7. /* SPDX-License-Identifier: BSD-3-Clause */
  8. /*
  9. * To create "backend/eci_sb.h" (from project root directory):
  10. *
  11. * php backend/tools/gen_eci_sb_h.php
  12. */
  13. $basename = basename(__FILE__);
  14. $dirname = dirname(__FILE__);
  15. $opts = getopt('d:o:');
  16. $out_dirname = isset($opts['o']) ? $opts['o'] : ($dirname . '/..'); // Where to put output.
  17. $out = array();
  18. $head = <<<'EOD'
  19. /* eci_sb.h - Extended Channel Interpretations single-byte, generated by "backend/tools/gen_eci_sb_h.php"
  20. from "https://unicode.org/Public/MAPPINGS/ISO8859/8859-*.TXT"
  21. and "https://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP125*.TXT" */
  22. /*
  23. libzint - the open source barcode library
  24. Copyright (C) 2021-2022 Robin Stuart <rstuart114@gmail.com>
  25. Redistribution and use in source and binary forms, with or without
  26. modification, are permitted provided that the following conditions
  27. are met:
  28. 1. Redistributions of source code must retain the above copyright
  29. notice, this list of conditions and the following disclaimer.
  30. 2. Redistributions in binary form must reproduce the above copyright
  31. notice, this list of conditions and the following disclaimer in the
  32. documentation and/or other materials provided with the distribution.
  33. 3. Neither the name of the project nor the names of its contributors
  34. may be used to endorse or promote products derived from this software
  35. without specific prior written permission.
  36. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  37. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  39. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  40. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  41. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  42. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  44. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  45. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. SUCH DAMAGE.
  47. */
  48. /* SPDX-License-Identifier: BSD-3-Clause */
  49. #ifndef Z_ECI_SB_H
  50. #define Z_ECI_SB_H
  51. EOD;
  52. $out = explode("\n", $head);
  53. $u_iso8859 = <<<'EOD'
  54. /* Forward reference to base ISO/IEC 8859 routine - see "eci.c" */
  55. static int u_iso8859(const unsigned int u, const unsigned short *tab_s, const unsigned short *tab_u,
  56. const unsigned char *tab_sb, int e, unsigned char *dest);
  57. EOD;
  58. $out = array_merge($out, explode("\n", $u_iso8859));
  59. $iso8859_comments = array(
  60. array(), array(), // 0-1
  61. // ECI Description
  62. array( '4', 'Latin alphabet No. 2 (Latin-2)' ),
  63. array( '5', 'Latin alphabet No. 3 (Latin-3) (South European)' ),
  64. array( '6', 'Latin alphabet No. 4 (Latin-4) (North European)' ),
  65. array( '7', 'Latin/Cyrillic' ),
  66. array( '8', 'Latin/Arabic' ),
  67. array( '9', 'Latin/Greek' ),
  68. array( '10', 'Latin/Hebrew' ),
  69. array( '11', 'Latin alphabet No. 5 (Latin-5) (Latin/Turkish)' ),
  70. array( '12', 'Latin alphabet No. 6 (Latin-6) (Nordic)' ),
  71. array( '13', 'Latin/Thai' ),
  72. array(),
  73. array( '15', 'Latin alphabet No. 7 (Latin-7) (Baltic Rim)' ),
  74. array( '16', 'Latin alphabet No. 8 (Latin-8) (Celtic)' ),
  75. array( '17', 'Latin alphabet No. 9 (Latin-9)' ),
  76. array( '18', 'Latin alphabet No. 10 (Latin-10) (South-Eastern European)' ),
  77. );
  78. // Read the 8859 files.
  79. $tot_8859 = 0;
  80. for ($k = 2; $k <= 16; $k++) {
  81. if ($k == 12) continue;
  82. $file = 'https://unicode.org/Public/MAPPINGS/ISO8859/' . '8859-' . $k . '.TXT';
  83. if (($get = file_get_contents($file)) === false) {
  84. error_log($error = "$basename: ERROR: Could not read mapping file \"$file\"");
  85. exit($error . PHP_EOL);
  86. }
  87. $lines = explode("\n", $get);
  88. // Parse the file.
  89. $sort = array();
  90. $sb = array();
  91. $same = array();
  92. foreach ($lines as $line) {
  93. $line = trim($line);
  94. if ($line === '' || strncmp($line, '0x', 2) !== 0 || strpos($line, "*** NO MAPPING ***") !== false) {
  95. continue;
  96. }
  97. $matches = array();
  98. if (preg_match('/^0x([0-9A-F]{2})[ \t]+0x([0-9A-F]{4})[ \t].*$/', $line, $matches)) {
  99. $mb = hexdec($matches[1]);
  100. $unicode = hexdec($matches[2]);
  101. if ($unicode >= 0xA0) {
  102. if ($unicode <= 0xFF && $unicode == $mb) {
  103. $same[] = $mb;
  104. } else {
  105. $sort[] = $unicode;
  106. $sb[] = $mb;
  107. }
  108. }
  109. }
  110. }
  111. sort($same);
  112. array_multisort($sort, $sb);
  113. $s = array( 0, 0, 0, 0, 0, 0 );
  114. for ($i = 0, $cnt = count($same); $i < $cnt; $i++) {
  115. $v = $same[$i] - 0xA0;
  116. $j = $v >> 4;
  117. $s[$j] |= 1 << ($v & 0xF);
  118. }
  119. // Output.
  120. $out[] = '';
  121. $out[] = '/* Tables for ISO/IEC 8859-' . $k . ' */';
  122. $out[] = 'static const unsigned short iso8859_' . $k . '_s[6] = { /* Straight-thru bit-flags */';
  123. $line = ' ';
  124. for ($i = 0; $i < 6; $i++) {
  125. $line .= sprintf(" 0x%04X,", $s[$i]);
  126. }
  127. $out[] = $line;
  128. $out[] = '};';
  129. $tot_8859 += 6 * 2;
  130. $cnt = count($sort);
  131. $out[] = 'static const unsigned short iso8859_' . $k . '_u[' . $cnt . '] = { /* Unicode codepoints sorted */';
  132. $line = ' ';
  133. for ($i = 0; $i < $cnt; $i++) {
  134. if ($i && $i % 8 === 0) {
  135. $out[] = $line;
  136. $line = ' ';
  137. }
  138. $line .= sprintf(' 0x%04X,', $sort[$i]);
  139. }
  140. if ($line !== ' ') {
  141. $out[] = $line;
  142. }
  143. $out[] = '};';
  144. $tot_8859 += $cnt * 2;
  145. $cnt = count($sb);
  146. $out[] = 'static const unsigned char iso8859_' . $k . '_sb[' . $cnt . '] = { /* Single-byte in Unicode order */';
  147. $line = ' ';
  148. for ($i = 0; $i < $cnt; $i++) {
  149. if ($i && $i % 8 === 0) {
  150. $out[] = $line;
  151. $line = ' ';
  152. }
  153. $line .= sprintf(' 0x%02X,', $sb[$i]);
  154. }
  155. if ($line !== ' ') {
  156. $out[] = $line;
  157. }
  158. $out[] = '};';
  159. $tot_8859 += $cnt;
  160. $out[] = '';
  161. $out[] = '/* ECI ' . $iso8859_comments[$k][0] . ' ISO/IEC 8859-' . $k . ' ' . $iso8859_comments[$k][1] . ' */';
  162. $out[] = 'static int u_iso8859_' . $k . '(const unsigned int u, unsigned char *dest) {';
  163. $out[] = ' return u_iso8859(u, iso8859_' . $k . '_s, iso8859_' . $k . '_u, iso8859_' . $k . '_sb, ARRAY_SIZE(iso8859_' . $k . '_u) - 1, dest);';
  164. $out[] = '}';
  165. }
  166. if (0) {
  167. $out[] = '';
  168. $out[] = '/* Total ISO/IEC 8859 bytes: ' . $tot_8859 . ' */';
  169. }
  170. $u_cp125x = <<<'EOD'
  171. /* Forward reference to base Windows-125x routine - see "eci.c" */
  172. static int u_cp125x(const unsigned int u, const unsigned short *tab_s, const unsigned short *tab_u,
  173. const unsigned char *tab_sb, int e, unsigned char *dest);
  174. EOD;
  175. $out = array_merge($out, explode("\n", $u_cp125x));
  176. $cp125x_comments = array(
  177. // ECI Description
  178. array( '21', 'Latin 2 (Central Europe)' ),
  179. array( '22', 'Cyrillic' ),
  180. array( '23', 'Latin 1' ),
  181. array(), array(), array(),
  182. array( '24', 'Arabic' ),
  183. );
  184. // Read the Windows 125x files.
  185. $tot_cp125x = 0;
  186. for ($k = 0; $k <= 6; $k++) {
  187. if ($k >= 3 && $k <= 5) continue;
  188. $file = 'https://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/' . 'CP125' . $k . '.TXT';
  189. if (($get = file_get_contents($file)) === false) {
  190. error_log($error = "$basename: ERROR: Could not read mapping file \"$file\"");
  191. exit($error . PHP_EOL);
  192. }
  193. $lines = explode("\n", $get);
  194. // Parse the file.
  195. $sort = array();
  196. $sb = array();
  197. $same = array();
  198. foreach ($lines as $line) {
  199. $line = trim($line);
  200. if ($line === '' || strncmp($line, '0x', 2) !== 0 || strpos($line, "*** NO MAPPING ***") !== false) {
  201. continue;
  202. }
  203. $matches = array();
  204. if (preg_match('/^0x([0-9A-F]{2})[ \t]+0x([0-9A-F]{4})[ \t].*$/', $line, $matches)) {
  205. $mb = hexdec($matches[1]);
  206. $unicode = hexdec($matches[2]);
  207. if ($unicode >= 0x80) {
  208. if ($unicode <= 0xFF && $unicode == $mb) {
  209. $same[] = $mb;
  210. } else {
  211. $sort[] = $unicode;
  212. $sb[] = $mb;
  213. }
  214. }
  215. }
  216. }
  217. sort($same);
  218. array_multisort($sort, $sb);
  219. $s = array( 0, 0, 0, 0, 0, 0 );
  220. for ($i = 0, $cnt = count($same); $i < $cnt; $i++) {
  221. $v = $same[$i] - 0xA0;
  222. $j = $v >> 4;
  223. $s[$j] |= 1 << ($v & 0xF);
  224. }
  225. // Output.
  226. $out[] = '';
  227. $out[] = '/* Tables for Windows 125' . $k . ' */';
  228. $out[] = 'static const unsigned short cp125' . $k . '_s[6] = { /* Straight-thru bit-flags */';
  229. $line = ' ';
  230. for ($i = 0; $i < 6; $i++) {
  231. $line .= sprintf(" 0x%04X,", $s[$i]);
  232. }
  233. $out[] = $line;
  234. $out[] = '};';
  235. $tot_cp125x += 6 * 2;
  236. $cnt = count($sort);
  237. $out[] = 'static const unsigned short cp125' . $k . '_u[' . $cnt . '] = { /* Unicode codepoints sorted */';
  238. $line = ' ';
  239. for ($i = 0; $i < $cnt; $i++) {
  240. if ($i && $i % 8 === 0) {
  241. $out[] = $line;
  242. $line = ' ';
  243. }
  244. $line .= sprintf(' 0x%04X,', $sort[$i]);
  245. }
  246. if ($line !== ' ') {
  247. $out[] = $line;
  248. }
  249. $out[] = '};';
  250. $tot_cp125x += $cnt * 2;
  251. $cnt = count($sb);
  252. $out[] = 'static const unsigned char cp125' . $k . '_sb[' . $cnt . '] = { /* Single-byte in Unicode order */';
  253. $line = ' ';
  254. for ($i = 0; $i < $cnt; $i++) {
  255. if ($i && $i % 8 === 0) {
  256. $out[] = $line;
  257. $line = ' ';
  258. }
  259. $line .= sprintf(' 0x%02X,', $sb[$i]);
  260. }
  261. if ($line !== ' ') {
  262. $out[] = $line;
  263. }
  264. $out[] = '};';
  265. $tot_cp125x += $cnt;
  266. $out[] = '';
  267. $out[] = '/* ECI ' . $cp125x_comments[$k][0] . ' Windows-125' . $k . ' ' . $cp125x_comments[$k][1] . ' */';
  268. $out[] = 'static int u_cp125' . $k . '(const unsigned int u, unsigned char *dest) {';
  269. $out[] = ' return u_cp125x(u, cp125' . $k . '_s, cp125' . $k . '_u, cp125' . $k . '_sb, ARRAY_SIZE(cp125' . $k . '_u) - 1, dest);';
  270. $out[] = '}';
  271. }
  272. if (0) {
  273. $out[] = '';
  274. $out[] = '/* Total Windows 125x bytes: ' . $tot_cp125x . ' */';
  275. $out[] = '';
  276. $out[] = '/* Total bytes: ' . ($tot_8859 + $tot_cp125x) . ' */';
  277. }
  278. $out[] = '';
  279. $out[] = '#endif /* Z_ECI_SB_H */';
  280. file_put_contents($out_dirname . '/eci_sb.h', implode("\n", $out) . "\n");
  281. /* vim: set ts=4 sw=4 et : */