Add Input mode support for Unicode, GS1 and Kanji

This commit is contained in:
hooper114 2009-01-11 09:11:03 +00:00
parent e246efe727
commit bc88024f04
5 changed files with 196 additions and 114 deletions

View File

@ -596,8 +596,11 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[])
symbol->rows += 1; symbol->rows += 1;
} }
error_number = gs1_verify(symbol, source, reduced); if(symbol->input_mode != GS1_MODE) {
if(error_number != 0) { return error_number; } /* GS1 data has not been checked yet */
error_number = gs1_verify(symbol, source, reduced);
if(error_number != 0) { return error_number; }
}
/* Decide on mode using same system as PDF417 and rules of ISO 15417 Annex E */ /* Decide on mode using same system as PDF417 and rules of ISO 15417 Annex E */
indexliste = 0; indexliste = 0;

View File

@ -22,6 +22,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "common.h" #include "common.h"
#include "gs1.h"
struct zint_symbol *ZBarcode_Create() struct zint_symbol *ZBarcode_Create()
{ {
@ -46,6 +47,7 @@ struct zint_symbol *ZBarcode_Create()
symbol->option_1 = -1; symbol->option_1 = -1;
symbol->option_2 = 0; symbol->option_2 = 0;
symbol->option_3 = 928; // PDF_MAX symbol->option_3 = 928; // PDF_MAX
symbol->input_mode = DATA_MODE;
strcpy(symbol->primary, ""); strcpy(symbol->primary, "");
for(i = 0; i < 178; i++) { for(i = 0; i < 178; i++) {
for(j = 0; j < 1000; j++) { for(j = 0; j < 1000; j++) {
@ -138,17 +140,93 @@ void error_tag(char error_string[], int error_number)
} }
} }
int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *unicode) int eci_process(struct zint_symbol *symbol, unsigned char source[], unsigned char preprocessed[])
{
int j, i, next, input_length;
input_length = ustrlen(source);
/* Supports UTF-8 input by converting it to Latin-1 Extended ASCII */
/* Currently only supports Latin-1 characters but expect this to be expanded
to the full ECI spectrum */
j = 0;
i = 0;
do {
next = -1;
if(source[i] < 128) {
preprocessed[j] = source[i];
j++;
next = i + 1;
} else {
if(source[i] == 0xC2) {
preprocessed[j] = source[i + 1];
j++;
next = i + 2;
}
if(source[i] == 0xC3) {
preprocessed[j] = source[i + 1] + 64;
j++;
next = i + 2;
}
}
if(next == -1) {
strcpy(symbol->errtxt, "error: Invalid character in input string (only Latin-1 characters supported)");
return ERROR_INVALID_DATA;
}
i = next;
} while(i < input_length);
preprocessed[j] = '\0';
return 0;
}
int gs1_compliant(int symbology)
{
/* Returns 1 if symbology supports GS1 data */
int result = 0;
switch(symbology) {
case BARCODE_EAN128:
case BARCODE_RSS_EXP:
case BARCODE_RSS_EXPSTACK:
case BARCODE_EANX_CC:
case BARCODE_EAN128_CC:
case BARCODE_RSS14_CC:
case BARCODE_RSS_LTD_CC:
case BARCODE_RSS_EXP_CC:
case BARCODE_UPCA_CC:
case BARCODE_UPCE_CC:
case BARCODE_RSS14STACK_CC:
case BARCODE_RSS14_OMNI_CC:
case BARCODE_RSS_EXPSTACK_CC:
result = 1;
break;
}
return result;
}
void ustrcpy(unsigned char dest[], unsigned char source[]) {
int i;
i = 0;
do {
dest[i] = source[i];
i++;
} while (source[i - 1] != '\0');
}
int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *source)
{ {
int error_number, error_buffer; int error_number, error_buffer;
error_number = 0; error_number = 0;
int input_length; int input_length;
int i, j, next;
input_length = ustrlen(unicode); input_length = ustrlen(source);
unsigned char latin1[input_length]; unsigned char preprocessed[input_length];
if(ustrlen(unicode) == 0) { if(ustrlen(source) == 0) {
strcpy(symbol->errtxt, "No input data [Z00]"); strcpy(symbol->errtxt, "No input data [Z00]");
error_tag(symbol->errtxt, ERROR_INVALID_DATA); error_tag(symbol->errtxt, ERROR_INVALID_DATA);
return ERROR_INVALID_DATA; return ERROR_INVALID_DATA;
@ -194,36 +272,29 @@ int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *unicode)
error_buffer = error_number; error_buffer = error_number;
} }
/* Supports UTF-8 input by converting it to Latin-1 Extended ASCII */ if((symbol->input_mode < 0) || (symbol->input_mode > 3)) { symbol->input_mode = DATA_MODE; }
/* At the moment all symbologies need this but in future versions this will switch(symbol->input_mode) {
be adapted to allow automatic ECI switching for more extended character support */ case DATA_MODE:
j = 0; ustrcpy(preprocessed, source); break;
i = 0; case UNICODE_MODE:
do { error_number = eci_process(symbol, source, preprocessed);
next = -1; if(error_number != 0) { return error_number; }
if(unicode[i] < 128) { break;
latin1[j] = unicode[i]; case GS1_MODE:
j++; if(gs1_compliant(symbol->symbology) == 1) {
next = i + 1; ustrcpy(preprocessed, source);
} else { } else {
if(unicode[i] == 0xC2) { strcpy(symbol->errtxt, "Selected symbology does not support GS1 mode");
latin1[j] = unicode[i + 1]; return ERROR_INVALID_OPTION;
j++;
next = i + 2;
} }
if(unicode[i] == 0xC3) { break;
latin1[j] = unicode[i + 1] + 64; case KANJI_MODE:
j++; if(symbol->symbology != BARCODE_QRCODE) {
next = i + 2; strcpy(symbol->errtxt, "Selected symbology does not support Kanji mode");
return ERROR_INVALID_OPTION;
} }
} ustrcpy(preprocessed, source); break;
if(next == -1) { }
strcpy(symbol->errtxt, "error: Invalid character in input string (only Latin-1 characters supported)");
return ERROR_INVALID_DATA;
}
i = next;
} while(i < input_length);
latin1[j] = '\0';
if(symbol->symbology == BARCODE_CODE16K) { if(symbol->symbology == BARCODE_CODE16K) {
symbol->whitespace_width = 16; symbol->whitespace_width = 16;
@ -238,77 +309,77 @@ int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *unicode)
} }
switch(symbol->symbology) { switch(symbol->symbology) {
case BARCODE_C25MATRIX: error_number = matrix_two_of_five(symbol, latin1); break; case BARCODE_C25MATRIX: error_number = matrix_two_of_five(symbol, preprocessed); break;
case BARCODE_C25IND: error_number = industrial_two_of_five(symbol, latin1); break; case BARCODE_C25IND: error_number = industrial_two_of_five(symbol, preprocessed); break;
case BARCODE_C25INTER: error_number = interleaved_two_of_five(symbol, latin1); break; case BARCODE_C25INTER: error_number = interleaved_two_of_five(symbol, preprocessed); break;
case BARCODE_C25IATA: error_number = iata_two_of_five(symbol, latin1); break; case BARCODE_C25IATA: error_number = iata_two_of_five(symbol, preprocessed); break;
case BARCODE_C25LOGIC: error_number = logic_two_of_five(symbol, latin1); break; case BARCODE_C25LOGIC: error_number = logic_two_of_five(symbol, preprocessed); break;
case BARCODE_DPLEIT: error_number = dpleit(symbol, latin1); break; case BARCODE_DPLEIT: error_number = dpleit(symbol, preprocessed); break;
case BARCODE_DPIDENT: error_number = dpident(symbol, latin1); break; case BARCODE_DPIDENT: error_number = dpident(symbol, preprocessed); break;
case BARCODE_UPCA: error_number = eanx(symbol, latin1); break; case BARCODE_UPCA: error_number = eanx(symbol, preprocessed); break;
case BARCODE_UPCE: error_number = eanx(symbol, latin1); break; case BARCODE_UPCE: error_number = eanx(symbol, preprocessed); break;
case BARCODE_EANX: error_number = eanx(symbol, latin1); break; case BARCODE_EANX: error_number = eanx(symbol, preprocessed); break;
case BARCODE_EAN128: error_number = ean_128(symbol, latin1); break; case BARCODE_EAN128: error_number = ean_128(symbol, preprocessed); break;
case BARCODE_CODE39: error_number = c39(symbol, latin1); break; case BARCODE_CODE39: error_number = c39(symbol, preprocessed); break;
case BARCODE_PZN: error_number = pharmazentral(symbol, latin1); break; case BARCODE_PZN: error_number = pharmazentral(symbol, preprocessed); break;
case BARCODE_EXCODE39: error_number = ec39(symbol, latin1); break; case BARCODE_EXCODE39: error_number = ec39(symbol, preprocessed); break;
case BARCODE_CODABAR: error_number = codabar(symbol, latin1); break; case BARCODE_CODABAR: error_number = codabar(symbol, preprocessed); break;
case BARCODE_CODE93: error_number = c93(symbol, latin1); break; case BARCODE_CODE93: error_number = c93(symbol, preprocessed); break;
case BARCODE_LOGMARS: error_number = c39(symbol, latin1); break; case BARCODE_LOGMARS: error_number = c39(symbol, preprocessed); break;
case BARCODE_CODE128: error_number = code_128(symbol, latin1); break; case BARCODE_CODE128: error_number = code_128(symbol, preprocessed); break;
case BARCODE_CODE128B: error_number = code_128(symbol, latin1); break; case BARCODE_CODE128B: error_number = code_128(symbol, preprocessed); break;
case BARCODE_NVE18: error_number = nve_18(symbol, latin1); break; case BARCODE_NVE18: error_number = nve_18(symbol, preprocessed); break;
case BARCODE_CODE11: error_number = code_11(symbol, latin1); break; case BARCODE_CODE11: error_number = code_11(symbol, preprocessed); break;
case BARCODE_MSI_PLESSEY: error_number = msi_handle(symbol, latin1); break; case BARCODE_MSI_PLESSEY: error_number = msi_handle(symbol, preprocessed); break;
case BARCODE_TELEPEN: error_number = telepen(symbol, latin1); break; case BARCODE_TELEPEN: error_number = telepen(symbol, preprocessed); break;
case BARCODE_TELEPEN_NUM: error_number = telepen_num(symbol, latin1); break; case BARCODE_TELEPEN_NUM: error_number = telepen_num(symbol, preprocessed); break;
case BARCODE_PHARMA: error_number = pharma_one(symbol, latin1); break; case BARCODE_PHARMA: error_number = pharma_one(symbol, preprocessed); break;
case BARCODE_PLESSEY: error_number = plessey(symbol, latin1); break; case BARCODE_PLESSEY: error_number = plessey(symbol, preprocessed); break;
case BARCODE_ITF14: error_number = itf14(symbol, latin1); break; case BARCODE_ITF14: error_number = itf14(symbol, preprocessed); break;
case BARCODE_FLAT: error_number = flattermarken(symbol, latin1); break; case BARCODE_FLAT: error_number = flattermarken(symbol, preprocessed); break;
case BARCODE_FIM: error_number = fim(symbol, latin1); break; case BARCODE_FIM: error_number = fim(symbol, preprocessed); break;
case BARCODE_POSTNET: error_number = post_plot(symbol, latin1); break; case BARCODE_POSTNET: error_number = post_plot(symbol, preprocessed); break;
case BARCODE_PLANET: error_number = planet_plot(symbol, latin1); break; case BARCODE_PLANET: error_number = planet_plot(symbol, preprocessed); break;
case BARCODE_RM4SCC: error_number = royal_plot(symbol, latin1); break; case BARCODE_RM4SCC: error_number = royal_plot(symbol, preprocessed); break;
case BARCODE_AUSPOST: error_number = australia_post(symbol, latin1); break; case BARCODE_AUSPOST: error_number = australia_post(symbol, preprocessed); break;
case BARCODE_AUSREPLY: error_number = australia_post(symbol, latin1); break; case BARCODE_AUSREPLY: error_number = australia_post(symbol, preprocessed); break;
case BARCODE_AUSROUTE: error_number = australia_post(symbol, latin1); break; case BARCODE_AUSROUTE: error_number = australia_post(symbol, preprocessed); break;
case BARCODE_AUSREDIRECT: error_number = australia_post(symbol, latin1); break; case BARCODE_AUSREDIRECT: error_number = australia_post(symbol, preprocessed); break;
case BARCODE_CODE16K: error_number = code16k(symbol, latin1); break; case BARCODE_CODE16K: error_number = code16k(symbol, preprocessed); break;
case BARCODE_PHARMA_TWO: error_number = pharma_two(symbol, latin1); break; case BARCODE_PHARMA_TWO: error_number = pharma_two(symbol, preprocessed); break;
case BARCODE_ONECODE: error_number = imail(symbol, latin1); break; case BARCODE_ONECODE: error_number = imail(symbol, preprocessed); break;
case BARCODE_DATAMATRIX: error_number = dmatrix(symbol, latin1); break; case BARCODE_DATAMATRIX: error_number = dmatrix(symbol, preprocessed); break;
case BARCODE_PDF417: error_number = pdf417enc(symbol, latin1); break; case BARCODE_PDF417: error_number = pdf417enc(symbol, preprocessed); break;
case BARCODE_PDF417TRUNC: error_number = pdf417enc(symbol, latin1); break; case BARCODE_PDF417TRUNC: error_number = pdf417enc(symbol, preprocessed); break;
case BARCODE_QRCODE: error_number = qr_code(symbol, latin1); break; case BARCODE_QRCODE: error_number = qr_code(symbol, preprocessed); break;
case BARCODE_MICROPDF417: error_number = micro_pdf417(symbol, latin1); break; case BARCODE_MICROPDF417: error_number = micro_pdf417(symbol, preprocessed); break;
case BARCODE_ISBNX: error_number = eanx(symbol, latin1); break; case BARCODE_ISBNX: error_number = eanx(symbol, preprocessed); break;
case BARCODE_MAXICODE: error_number = maxicode(symbol, latin1); break; case BARCODE_MAXICODE: error_number = maxicode(symbol, preprocessed); break;
case BARCODE_RSS14: error_number = rss14(symbol, latin1); break; case BARCODE_RSS14: error_number = rss14(symbol, preprocessed); break;
case BARCODE_RSS14STACK: error_number = rss14(symbol, latin1); break; case BARCODE_RSS14STACK: error_number = rss14(symbol, preprocessed); break;
case BARCODE_RSS14STACK_OMNI: error_number = rss14(symbol, latin1); break; case BARCODE_RSS14STACK_OMNI: error_number = rss14(symbol, preprocessed); break;
case BARCODE_RSS_LTD: error_number = rsslimited(symbol, latin1); break; case BARCODE_RSS_LTD: error_number = rsslimited(symbol, preprocessed); break;
case BARCODE_RSS_EXP: error_number = rssexpanded(symbol, latin1); break; case BARCODE_RSS_EXP: error_number = rssexpanded(symbol, preprocessed); break;
case BARCODE_RSS_EXPSTACK: error_number = rssexpanded(symbol, latin1); break; case BARCODE_RSS_EXPSTACK: error_number = rssexpanded(symbol, preprocessed); break;
case BARCODE_EANX_CC: error_number = composite(symbol, latin1); break; case BARCODE_EANX_CC: error_number = composite(symbol, preprocessed); break;
case BARCODE_EAN128_CC: error_number = composite(symbol, latin1); break; case BARCODE_EAN128_CC: error_number = composite(symbol, preprocessed); break;
case BARCODE_RSS14_CC: error_number = composite(symbol, latin1); break; case BARCODE_RSS14_CC: error_number = composite(symbol, preprocessed); break;
case BARCODE_RSS_LTD_CC: error_number = composite(symbol, latin1); break; case BARCODE_RSS_LTD_CC: error_number = composite(symbol, preprocessed); break;
case BARCODE_RSS_EXP_CC: error_number = composite(symbol, latin1); break; case BARCODE_RSS_EXP_CC: error_number = composite(symbol, preprocessed); break;
case BARCODE_UPCA_CC: error_number = composite(symbol, latin1); break; case BARCODE_UPCA_CC: error_number = composite(symbol, preprocessed); break;
case BARCODE_UPCE_CC: error_number = composite(symbol, latin1); break; case BARCODE_UPCE_CC: error_number = composite(symbol, preprocessed); break;
case BARCODE_RSS14STACK_CC: error_number = composite(symbol, latin1); break; case BARCODE_RSS14STACK_CC: error_number = composite(symbol, preprocessed); break;
case BARCODE_RSS14_OMNI_CC: error_number = composite(symbol, latin1); break; case BARCODE_RSS14_OMNI_CC: error_number = composite(symbol, preprocessed); break;
case BARCODE_RSS_EXPSTACK_CC: error_number = composite(symbol, latin1); break; case BARCODE_RSS_EXPSTACK_CC: error_number = composite(symbol, preprocessed); break;
case BARCODE_AZTEC: error_number = aztec(symbol, latin1); break; case BARCODE_AZTEC: error_number = aztec(symbol, preprocessed); break;
case BARCODE_KIX: error_number = kix_code(symbol, latin1); break; case BARCODE_KIX: error_number = kix_code(symbol, preprocessed); break;
case BARCODE_CODE32: error_number = code32(symbol, latin1); break; case BARCODE_CODE32: error_number = code32(symbol, preprocessed); break;
case BARCODE_CODABLOCKF: error_number = codablock(symbol, latin1); break; case BARCODE_CODABLOCKF: error_number = codablock(symbol, preprocessed); break;
case BARCODE_DAFT: error_number = daft_code(symbol, latin1); break; case BARCODE_DAFT: error_number = daft_code(symbol, preprocessed); break;
case BARCODE_EAN14: error_number = ean_14(symbol, latin1); break; case BARCODE_EAN14: error_number = ean_14(symbol, preprocessed); break;
case BARCODE_MICROQR: error_number = microqr(symbol, latin1); break; case BARCODE_MICROQR: error_number = microqr(symbol, preprocessed); break;
case BARCODE_AZRUNE: error_number = aztec_runes(symbol, latin1); break; case BARCODE_AZRUNE: error_number = aztec_runes(symbol, preprocessed); break;
case BARCODE_KOREAPOST: error_number = korea_post(symbol, latin1); break; case BARCODE_KOREAPOST: error_number = korea_post(symbol, preprocessed); break;
} }
if(error_number == 0) { if(error_number == 0) {
error_number = error_buffer; error_number = error_buffer;

View File

@ -26,17 +26,13 @@
#ifndef NO_QR #ifndef NO_QR
#include <qrencode.h> #include <qrencode.h>
static int kanji = 0; QRcode *encode(int security, int size, const unsigned char *intext, int kanji)
QRcode *encode(int security, int size, const unsigned char *intext)
{ {
int version; int version;
QRecLevel level; QRecLevel level;
QRencodeMode hint; QRencodeMode hint;
QRcode *code; QRcode *code;
level=QR_ECLEVEL_L;
if(kanji) { if(kanji) {
hint = QR_MODE_KANJI; hint = QR_MODE_KANJI;
} else { } else {
@ -70,8 +66,11 @@ int qr_code(struct zint_symbol *symbol, unsigned char source[])
QRcode *code; QRcode *code;
int errno = 0; int errno = 0;
int i, j; int i, j;
int kanji;
code = encode(symbol->option_1, symbol->option_2, source); if(symbol->input_mode == KANJI_MODE) { kanji = 1; } else { kanji = 0; }
code = encode(symbol->option_1, symbol->option_2, source, kanji);
if(code == NULL) { if(code == NULL) {
strcpy(symbol->errtxt, "libqrencode failed to encode the input data [581]"); strcpy(symbol->errtxt, "libqrencode failed to encode the input data [581]");
return ERROR_ENCODING_PROBLEM; return ERROR_ENCODING_PROBLEM;

View File

@ -1845,8 +1845,11 @@ int rssexpanded(struct zint_symbol *symbol, unsigned char source[])
separator_row = 0; separator_row = 0;
reader=0; reader=0;
i = gs1_verify(symbol, source, reduced); if(symbol->input_mode != GS1_MODE) {
if(i != 0) { return i; } /* GS1 data has not been verified yet */
i = gs1_verify(symbol, source, reduced);
if(i != 0) { return i; }
}
if((symbol->symbology == BARCODE_RSS_EXP_CC) || (symbol->symbology == BARCODE_RSS_EXPSTACK_CC)) { if((symbol->symbology == BARCODE_RSS_EXP_CC) || (symbol->symbology == BARCODE_RSS_EXPSTACK_CC)) {
/* make space for a composite separator pattern */ /* make space for a composite separator pattern */

View File

@ -39,6 +39,7 @@ struct zint_symbol {
int option_1; int option_1;
int option_2; int option_2;
int option_3; int option_3;
int input_mode;
char text[100]; char text[100];
int rows; int rows;
int width; int width;
@ -132,6 +133,11 @@ struct zint_symbol {
#define BARCODE_BOX 4 #define BARCODE_BOX 4
#define BARCODE_STDOUT 8 #define BARCODE_STDOUT 8
#define DATA_MODE 0
#define UNICODE_MODE 1
#define GS1_MODE 2
#define KANJI_MODE 3
#define WARN_INVALID_OPTION 2 #define WARN_INVALID_OPTION 2
#define ERROR_TOO_LONG 5 #define ERROR_TOO_LONG 5
#define ERROR_INVALID_DATA 6 #define ERROR_INVALID_DATA 6