Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/zint/backend/telepen.c @ 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 /* telepen.c - Handles Telepen and Telepen numeric */ | |
| 2 /* | |
| 3 libzint - the open source barcode library | |
| 4 Copyright (C) 2008-2024 Robin Stuart <rstuart114@gmail.com> | |
| 5 | |
| 6 Redistribution and use in source and binary forms, with or without | |
| 7 modification, are permitted provided that the following conditions | |
| 8 are met: | |
| 9 | |
| 10 1. Redistributions of source code must retain the above copyright | |
| 11 notice, this list of conditions and the following disclaimer. | |
| 12 2. Redistributions in binary form must reproduce the above copyright | |
| 13 notice, this list of conditions and the following disclaimer in the | |
| 14 documentation and/or other materials provided with the distribution. | |
| 15 3. Neither the name of the project nor the names of its contributors | |
| 16 may be used to endorse or promote products derived from this software | |
| 17 without specific prior written permission. | |
| 18 | |
| 19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
| 20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE | |
| 23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 25 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 26 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 27 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 28 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 29 SUCH DAMAGE. | |
| 30 */ | |
| 31 /* SPDX-License-Identifier: BSD-3-Clause */ | |
| 32 | |
| 33 /* Telepen Barcode Symbology information and History (BSiH) | |
| 34 https://telepen.co.uk/wp-content/uploads/2018/10/Barcode-Symbology-information-and-History.pdf */ | |
| 35 | |
| 36 #define SODIUM_X_F (IS_NUM_F | IS_UX__F | IS_LX__F) /* SODIUM "0123456789Xx" */ | |
| 37 | |
| 38 #include <stdio.h> | |
| 39 #include "common.h" | |
| 40 | |
| 41 static const char TeleTable[128][16] = { | |
| 42 { "31313131" }, { "1131313111" }, { "33313111" }, { "1111313131" }, | |
| 43 { "3111313111" }, { "11333131" }, { "13133131" }, { "111111313111" }, | |
| 44 { "31333111" }, { "1131113131" }, { "33113131" }, { "1111333111" }, | |
| 45 { "3111113131" }, { "1113133111" }, { "1311133111" }, { "111111113131" }, | |
| 46 { "3131113111" }, { "11313331" }, { "333331" }, { "111131113111" }, | |
| 47 { "31113331" }, { "1133113111" }, { "1313113111" }, { "1111113331" }, | |
| 48 { "31131331" }, { "113111113111" }, { "3311113111" }, { "1111131331" }, | |
| 49 { "311111113111" }, { "1113111331" }, { "1311111331" }, { "11111111113111" }, | |
| 50 { "31313311" }, { "1131311131" }, { "33311131" }, { "1111313311" }, | |
| 51 { "3111311131" }, { "11333311" }, { "13133311" }, { "111111311131" }, | |
| 52 { "31331131" }, { "1131113311" }, { "33113311" }, { "1111331131" }, | |
| 53 { "3111113311" }, { "1113131131" }, { "1311131131" }, { "111111113311" }, | |
| 54 { "3131111131" }, { "1131131311" }, { "33131311" }, { "111131111131" }, | |
| 55 { "3111131311" }, { "1133111131" }, { "1313111131" }, { "111111131311" }, | |
| 56 { "3113111311" }, { "113111111131" }, { "3311111131" }, { "111113111311" }, | |
| 57 { "311111111131" }, { "111311111311" }, { "131111111311" }, { "11111111111131" }, | |
| 58 { "3131311111" }, { "11313133" }, { "333133" }, { "111131311111" }, | |
| 59 { "31113133" }, { "1133311111" }, { "1313311111" }, { "1111113133" }, | |
| 60 { "313333" }, { "113111311111" }, { "3311311111" }, { "11113333" }, | |
| 61 { "311111311111" }, { "11131333" }, { "13111333" }, { "11111111311111" }, | |
| 62 { "31311133" }, { "1131331111" }, { "33331111" }, { "1111311133" }, | |
| 63 { "3111331111" }, { "11331133" }, { "13131133" }, { "111111331111" }, | |
| 64 { "3113131111" }, { "1131111133" }, { "33111133" }, { "111113131111" }, | |
| 65 { "3111111133" }, { "111311131111" }, { "131111131111" }, { "111111111133" }, | |
| 66 { "31311313" }, { "113131111111" }, { "3331111111" }, { "1111311313" }, | |
| 67 { "311131111111" }, { "11331313" }, { "13131313" }, { "11111131111111" }, | |
| 68 { "3133111111" }, { "1131111313" }, { "33111313" }, { "111133111111" }, | |
| 69 { "3111111313" }, { "111313111111" }, { "131113111111" }, { "111111111313" }, | |
| 70 { "313111111111" }, { "1131131113" }, { "33131113" }, { "11113111111111" }, | |
| 71 { "3111131113" }, { "113311111111" }, { "131311111111" }, { "111111131113" }, | |
| 72 { "3113111113" }, { "11311111111111" }, { "331111111111" }, { "111113111113" }, | |
| 73 { "31111111111111" }, { "111311111113" }, { "131111111113" }, | |
| 74 {'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'}, | |
| 75 }; | |
| 76 | |
| 77 /* Generated by "backend/tests/test_telepen -f generate_lens -g" */ | |
| 78 static const char TeleLens[128] = { | |
| 79 8, 10, 8, 10, 10, 8, 8, 12, 8, 10, 8, 10, 10, 10, 10, 12, | |
| 80 10, 8, 6, 12, 8, 10, 10, 10, 8, 12, 10, 10, 12, 10, 10, 14, | |
| 81 8, 10, 8, 10, 10, 8, 8, 12, 8, 10, 8, 10, 10, 10, 10, 12, | |
| 82 10, 10, 8, 12, 10, 10, 10, 12, 10, 12, 10, 12, 12, 12, 12, 14, | |
| 83 10, 8, 6, 12, 8, 10, 10, 10, 6, 12, 10, 8, 12, 8, 8, 14, | |
| 84 8, 10, 8, 10, 10, 8, 8, 12, 10, 10, 8, 12, 10, 12, 12, 12, | |
| 85 8, 12, 10, 10, 12, 8, 8, 14, 10, 10, 8, 12, 10, 12, 12, 12, | |
| 86 12, 10, 8, 14, 10, 12, 12, 12, 10, 14, 12, 12, 14, 12, 12, 16 | |
| 87 }; | |
| 88 | |
| 89 INTERNAL int telepen(struct zint_symbol *symbol, unsigned char source[], int length) { | |
| 90 int i, count, check_digit; | |
| 91 int error_number; | |
| 92 char dest[1145]; /* 12 (Start) + 69 * 16 (max for DELs) + 16 (Check) + 12 (stop) + 1 = 1145 */ | |
| 93 char *d = dest; | |
| 94 | |
| 95 error_number = 0; | |
| 96 | |
| 97 count = 0; | |
| 98 | |
| 99 if (length > 69) { /* 16 (Start) + 69 * 16 + 16 (Check) + 16 (Stop) = 1152 */ | |
| 100 return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 390, "Input length %d too long (maximum 69)", length); | |
| 101 } | |
| 102 /* Start character */ | |
| 103 memcpy(d, TeleTable['_'], 12); | |
| 104 d += 12; | |
| 105 | |
| 106 for (i = 0; i < length; i++) { | |
| 107 if (source[i] > 127) { | |
| 108 /* Cannot encode extended ASCII */ | |
| 109 return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 391, | |
| 110 "Invalid character at position %d in input, extended ASCII not allowed", i + 1); | |
| 111 } | |
| 112 memcpy(d, TeleTable[source[i]], TeleLens[source[i]]); | |
| 113 d += TeleLens[source[i]]; | |
| 114 count += source[i]; | |
| 115 } | |
| 116 | |
| 117 check_digit = 127 - (count % 127); | |
| 118 if (check_digit == 127) { | |
| 119 check_digit = 0; | |
| 120 } | |
| 121 memcpy(d, TeleTable[check_digit], TeleLens[check_digit]); | |
| 122 d += TeleLens[check_digit]; | |
| 123 | |
| 124 if (symbol->debug & ZINT_DEBUG_PRINT) printf("Check digit: %d\n", check_digit); | |
| 125 | |
| 126 /* Stop character */ | |
| 127 memcpy(d, TeleTable['z'], 12); | |
| 128 d += 12; | |
| 129 | |
| 130 expand(symbol, dest, d - dest); | |
| 131 | |
| 132 if (symbol->output_options & COMPLIANT_HEIGHT) { | |
| 133 /* Default height from various Telepen docs is based on default 26pt at X 0.01125" | |
| 134 (average of 0.01" - 0.0125") = (26 / 72) / 0.01125 ~ 32; no min height specified */ | |
| 135 (void) set_height(symbol, 0.0f, 32.0f, 0, 1 /*no_errtxt*/); | |
| 136 } else { | |
| 137 (void) set_height(symbol, 0.0f, 50.0f, 0, 1 /*no_errtxt*/); | |
| 138 } | |
| 139 | |
| 140 for (i = 0; i < length; i++) { | |
| 141 if (source[i] == '\0') { | |
| 142 symbol->text[i] = ' '; | |
| 143 } else { | |
| 144 symbol->text[i] = source[i]; | |
| 145 } | |
| 146 } | |
| 147 symbol->text[length] = '\0'; | |
| 148 return error_number; | |
| 149 } | |
| 150 | |
| 151 INTERNAL int telepen_num(struct zint_symbol *symbol, unsigned char source[], int length) { | |
| 152 int count, check_digit, glyph; | |
| 153 int error_number = 0; | |
| 154 int i; | |
| 155 char dest[1129]; /* 12 (Start) + 68 * 16 (max for DELs) + 16 (Check) + 12 (Stop) + 1 = 1129 */ | |
| 156 char *d = dest; | |
| 157 unsigned char temp[137]; | |
| 158 | |
| 159 count = 0; | |
| 160 | |
| 161 if (length > 136) { /* 68*2 */ | |
| 162 return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 392, "Input length %d too long (maximum 136)", length); | |
| 163 } | |
| 164 if ((i = not_sane(SODIUM_X_F, source, length))) { | |
| 165 return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 393, | |
| 166 "Invalid character at position %d in input (digits and \"X\" only)", i); | |
| 167 } | |
| 168 | |
| 169 /* Add a leading zero if required */ | |
| 170 if (length & 1) { | |
| 171 memcpy(temp + 1, source, length++); | |
| 172 temp[0] = '0'; | |
| 173 } else { | |
| 174 memcpy(temp, source, length); | |
| 175 } | |
| 176 temp[length] = '\0'; | |
| 177 to_upper(temp, length); | |
| 178 | |
| 179 /* Start character */ | |
| 180 memcpy(d, TeleTable['_'], 12); | |
| 181 d += 12; | |
| 182 | |
| 183 for (i = 0; i < length; i += 2) { | |
| 184 if (temp[i] == 'X') { | |
| 185 return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 394, "Invalid odd position %d of \"X\" in Telepen data", | |
| 186 i + 1); | |
| 187 } | |
| 188 | |
| 189 if (temp[i + 1] == 'X') { | |
| 190 glyph = ctoi(temp[i]) + 17; | |
| 191 count += glyph; | |
| 192 } else { | |
| 193 glyph = (10 * ctoi(temp[i])) + ctoi(temp[i + 1]); | |
| 194 glyph += 27; | |
| 195 count += glyph; | |
| 196 } | |
| 197 memcpy(d, TeleTable[glyph], TeleLens[glyph]); | |
| 198 d += TeleLens[glyph]; | |
| 199 } | |
| 200 | |
| 201 check_digit = 127 - (count % 127); | |
| 202 if (check_digit == 127) { | |
| 203 check_digit = 0; | |
| 204 } | |
| 205 memcpy(d, TeleTable[check_digit], TeleLens[check_digit]); | |
| 206 d += TeleLens[check_digit]; | |
| 207 | |
| 208 if (symbol->debug & ZINT_DEBUG_PRINT) printf("Check digit: %d\n", check_digit); | |
| 209 | |
| 210 /* Stop character */ | |
| 211 memcpy(d, TeleTable['z'], 12); | |
| 212 d += 12; | |
| 213 | |
| 214 expand(symbol, dest, d - dest); | |
| 215 | |
| 216 if (symbol->output_options & COMPLIANT_HEIGHT) { | |
| 217 (void) set_height(symbol, 0.0f, 32.0f, 0, 1 /*no_errtxt*/); /* Same as alphanumeric Telepen */ | |
| 218 } else { | |
| 219 (void) set_height(symbol, 0.0f, 50.0f, 0, 1 /*no_errtxt*/); | |
| 220 } | |
| 221 | |
| 222 ustrcpy(symbol->text, temp); | |
| 223 return error_number; | |
| 224 } | |
| 225 | |
| 226 /* vim: set ts=4 sw=4 et : */ |
