telepen.c 9.8 KB

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