comparison mupdf-source/thirdparty/zint/backend/tests/tools/gen_test_tab.php @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 <?php
2 /* Generate lookup table from unicode.org mapping file (SHIFTJIS.TXT by default). */
3 /*
4 libzint - the open source barcode library
5 Copyright (C) 2019-2022 Robin Stuart <rstuart114@gmail.com>
6 */
7 /* To create backend/tests/test_sjis_tab.h (from the project root directory):
8 *
9 * php backend/tests/tools/gen_test_tab.php
10 *
11 * To create backend/tests/test_gb2312_tab.h;
12 *
13 * php backend/tests/tools/gen_test_tab.php -f GB2312.TXT -s gb2312_tab
14 *
15 * To create backend/tests/test_gbk.h;
16 *
17 * php backend/tests/tools/gen_test_tab.php -f CP936.TXT -s gbk_tab
18 *
19 * To create backend/tests/test_gb18030_tab.h (note that backend/tests/tools/data/GB18030.TXT
20 * will have to be downloaded first from https://haible.de/bruno/charsets/conversion-tables/GB18030.html
21 * using the version jdk-1.4.2/GB18030.TXT):
22 *
23 * php backend/tests/tools/gen_test_tab.php -f GB18030.TXT -s gb18030_tab
24 *
25 * To create backend/tests/test_big5_tab.h;
26 *
27 * php backend/tests/tools/gen_test_tab.php -f BIG5.TXT -s big5_tab
28 *
29 * To create backend/tests/test_ksx1001_tab.h;
30 *
31 * php backend/tests/tools/gen_test_tab.php -f KSX1001.TXT -s ksx1001_tab
32 *
33 */
34
35 $basename = basename(__FILE__);
36 $dirname = dirname(__FILE__);
37
38 $opts = getopt('d:f:o:s:');
39 $data_dirname = isset($opts['d']) ? $opts['d'] : ($dirname . '/../../tools/data'); // Where to load file from.
40 $file_name = isset($opts['f']) ? $opts['f'] : 'SHIFTJIS.TXT'; // Name of file.
41 $out_dirname = isset($opts['o']) ? $opts['o'] : ($dirname . '/..'); // Where to put output.
42 $suffix_name = isset($opts['s']) ? $opts['s'] : 'sjis_tab'; // Suffix of table and output file.
43
44 $file = $data_dirname . '/' . $file_name;
45
46 // Read the file.
47
48 if (($get = file_get_contents($file)) === false) {
49 error_log($error = "$basename: ERROR: Could not read mapping file \"$file\"");
50 exit($error . PHP_EOL);
51 }
52
53 $lines = explode("\n", $get);
54
55 // Parse the file.
56
57 $tab_lines = array();
58 $sort = array();
59 foreach ($lines as $line) {
60 $line = trim($line);
61 if ($line === '' || strncmp($line, '0x', 2) !== 0 || strpos($line, "*** NO MAPPING ***") !== false) {
62 continue;
63 }
64 if (preg_match('/^0x([0-9A-F]{2,8})[ \t]+0x([0-9A-F]{5})/', $line)) { // Exclude U+10000..10FFFF to save space
65 continue;
66 }
67 $tab_lines[] = preg_replace_callback('/^0x([0-9A-F]{2,8})[ \t]+0x([0-9A-F]{4}).*$/', function ($matches) {
68 global $sort;
69 $mb = hexdec($matches[1]);
70 $unicode = hexdec($matches[2]);
71 $sort[] = $unicode;
72 return sprintf(" 0x%04X, 0x%04X,", $mb, $unicode);
73 }, $line);
74 }
75
76 array_multisort($sort, $tab_lines);
77
78 // Output.
79
80 $out = array();
81 $out[] = '/* Generated by ' . $basename . ' from ' . $file_name . ' */';
82 $out[] = 'static const unsigned int test_' . $suffix_name . '[] = {';
83 $out = array_merge($out, $tab_lines);
84 $out[] = '};';
85
86 $out[] = '';
87 $out[] = 'static const unsigned int test_' . $suffix_name . '_ind[] = {';
88 $first = 0;
89 foreach ($sort as $ind => $unicode) {
90 $div = (int)($unicode / 0x400);
91 while ($div >= $first) {
92 $out[] = ' ' . ($ind * 2) . ',';
93 $first++;
94 }
95 }
96 $out[] = '};';
97
98 file_put_contents($out_dirname . '/test_' . $suffix_name . '.h', implode("\n", $out) . "\n");
99
100 /* vim: set ts=4 sw=4 et : */