Add convenience API funcs ZBarcode_UTF8_To_ECI() and

`ZBarcode_Dest_Len_ECI()`, primarily for ZXingC++ but also useful
  in general
This commit is contained in:
gitlost 2025-03-02 20:50:55 +00:00
parent 99f94b1027
commit d0465375bb
12 changed files with 647 additions and 167 deletions

View file

@ -2,7 +2,7 @@
/* Generate ECI single-byte tables & routines from unicode.org mapping files */
/*
libzint - the open source barcode library
Copyright (C) 2022-2023 Robin Stuart <rstuart114@gmail.com>
Copyright (C) 2022-2025 Robin Stuart <rstuart114@gmail.com>
*/
/* SPDX-License-Identifier: BSD-3-Clause */
/*
@ -20,12 +20,13 @@ $out_dirname = isset($opts['o']) ? $opts['o'] : ($dirname . '/..'); // Where to
$out = array();
$head = <<<'EOD'
/* eci_sb.h - Extended Channel Interpretations single-byte, generated by "backend/tools/gen_eci_sb_h.php"
from "https://unicode.org/Public/MAPPINGS/ISO8859/8859-*.TXT"
and "https://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP125*.TXT" */
/* eci_sb.h - Extended Channel Interpretations single-byte, generated by "backend/tools/gen_eci_sb_h.php" from
"https://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP437.TXT" (for libzueci compatibility) and
"https://unicode.org/Public/MAPPINGS/ISO8859/8859-*.TXT" and
"https://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP125*.TXT" */
/*
libzint - the open source barcode library
Copyright (C) 2021-2022 Robin Stuart <rstuart114@gmail.com>
Copyright (C) 2021-2025 Robin Stuart <rstuart114@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
@ -60,6 +61,95 @@ EOD;
$out = explode("\n", $head);
// Read the CP437 file
$tot_cp437 = 0;
//$file = $data_dirname . '/' . 'CP437.TXT';
$file = 'https://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP437.TXT';
if (($get = file_get_contents($file)) === false) {
error_log($error = "$basename: ERROR: Could not read mapping file \"$file\"");
exit($error . PHP_EOL);
}
$lines = explode("\n", $get);
// Parse the file.
$sort = array();
$sb = array();
foreach ($lines as $line) {
$line = trim($line);
if ($line === '' || strncmp($line, '0x', 2) !== 0 || strpos($line, "*** NO MAPPING ***") !== false) {
continue;
}
$matches = array();
if (preg_match('/^0x([0-9a-f]{2})[ \t]+0x([0-9a-f]{4})[ \t].*$/', $line, $matches)) {
$mb = hexdec($matches[1]);
$unicode = hexdec($matches[2]);
if ($unicode >= 0x80) {
$sort[] = $unicode;
$sb[] = $mb;
}
}
}
array_multisort($sort, $sb);
// Output.
$out[] = '';
$out[] = '/* Tables for ECI 2 CP437 (for libzueci compatibility) */';
$cnt = count($sort);
$out[] = 'static const unsigned short cp437_u[' . $cnt . '] = { /* Unicode codepoints sorted */';
$line = ' ';
for ($i = 0; $i < $cnt; $i++) {
if ($i && $i % 8 === 0) {
$out[] = $line;
$line = ' ';
}
$line .= sprintf(' 0x%04X,', $sort[$i]);
}
if ($line !== ' ') {
$out[] = $line;
}
$out[] = '};';
$tot_cp437 += $cnt * 2;
$cnt = count($sb);
$out[] = 'static const unsigned char cp437_sb[' . $cnt . '] = { /* Single-byte in Unicode order */';
$line = ' ';
for ($i = 0; $i < $cnt; $i++) {
if ($i && $i % 8 === 0) {
$out[] = $line;
$line = ' ';
}
$line .= sprintf(' 0x%02X,', $sb[$i]);
}
if ($line !== ' ') {
$out[] = $line;
}
$out[] = '};';
$tot_cp437 += $cnt;
$u_sb = array_flip($sb);
$b = 0x80;
$cnt = 256 - $b;
$max_idx = -1;
for ($i = 0; $i < $cnt; $i++) {
if (isset($u_sb[$i + $b])) {
$max_idx = $i;
}
}
$cnt = $max_idx + 1;
$tot_cp437 += $cnt;
if (0) {
$out[] = '';
$out[] = '/* Total CP437 bytes: ' . $tot_cp437 . ' */';
}
$u_iso8859 = <<<'EOD'
/* Forward reference to base ISO/IEC 8859 routine - see "eci.c" */