Improved Kanji support - Now converts from Unicode to Shift JIS

This commit is contained in:
hooper114 2009-02-22 14:13:32 +00:00
parent 2262c365c4
commit de4f39782f
7 changed files with 6977 additions and 5 deletions

View File

@ -23,6 +23,7 @@
#include <string.h>
#include "common.h"
#include "gs1.h"
#include "sjis.h"
#define HIBCSET "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"
@ -269,6 +270,81 @@ int eci_process(struct zint_symbol *symbol, unsigned char source[], unsigned cha
return 0;
}
int unicode2shift_jis(struct zint_symbol *symbol, unsigned char source[], unsigned char preprocessed[])
{ /* QR Code supports compression of Shift-JIS data using "Kanji" mode - this function
attempts to convert Unicode characters to Shift-JIS to allow this */
int bpos, jpos, len, error_number, i;
int next;
unsigned long int uval, jval;
len = ustrlen(source);
bpos = 0;
jpos = 0;
error_number = 0;
next = 0;
do {
uval = 0;
jval = 0;
if(source[bpos] <= 0x7f) {
/* 1 byte mode */
uval = source[bpos];
next = bpos + 1;
}
if((source[bpos] >= 0x80) && (source[bpos] <= 0xbf)) {
strcpy(symbol->errtxt, "Corrupt Unicode data");
return ERROR_INVALID_DATA;
}
if((source[bpos] >= 0xc0) && (source[bpos] <= 0xc1)) {
strcpy(symbol->errtxt, "Overlong encoding not supported");
return ERROR_INVALID_DATA;
}
if((source[bpos] >= 0xc2) && (source[bpos] <= 0xdf)) {
/* 2 byte mode */
uval = ((source[bpos] & 0x1f) << 6) + (source[bpos + 1] & 0x3f);
next = bpos + 2;
}
if((source[bpos] >= 0xe0) && (source[bpos] <= 0xef)) {
/* 3 byte mode */
uval = ((source[bpos] & 0x0f) << 12) + ((source[bpos + 1] & 0x3f) << 6) + (source[bpos + 2] & 0x3f);
next = bpos + 3;
}
if(source[bpos] >= 0xf0) {
strcpy(symbol->errtxt, "Unicode sequences of more than 3 bytes not supported");
return ERROR_INVALID_DATA;
}
for(i = 0; i < 6843; i++) {
if(sjis_lookup[i * 2] == uval) {
jval = sjis_lookup[(i * 2) + 1];
}
}
if(jval == 0) {
strcpy(symbol->errtxt, "Invalid Shift JIS character");
return ERROR_INVALID_DATA;
}
preprocessed[jpos] = (jval & 0xff00) >> 8;
preprocessed[jpos + 1] = (jval & 0xff);
/* printf("Unicode value U+%04X = Shift JIS value 0x%04X\n", uval, jval); */
jpos += 2;
bpos = next;
} while(bpos < len);
return error_number;
}
int gs1_compliant(int symbology)
{
/* Returns 1 if symbology supports GS1 data */
@ -380,11 +456,20 @@ int ZBarcode_Encode(struct zint_symbol *symbol, unsigned char *source)
}
break;
case KANJI_MODE:
if(symbol->symbology != BARCODE_QRCODE) {
if((symbol->symbology != BARCODE_QRCODE) && (symbol->symbology != BARCODE_MICROQR)) {
strcpy(symbol->errtxt, "Selected symbology does not support Kanji mode");
return ERROR_INVALID_OPTION;
}
ustrcpy(preprocessed, source); break;
error_number = unicode2shift_jis(symbol, source, preprocessed);
if(error_number != 0) { return error_number; }
break;
case SJIS_MODE:
if((symbol->symbology != BARCODE_QRCODE) && (symbol->symbology != BARCODE_MICROQR)) {
strcpy(symbol->errtxt, "Selected symbology does not support Kanji mode");
return ERROR_INVALID_OPTION;
}
ustrcpy(preprocessed, source);
break;
}
if(symbol->symbology == BARCODE_CODE16K) {

View File

@ -663,6 +663,7 @@ int microqr(struct zint_symbol *symbol, unsigned char source[])
if(is_sane(QRSET, source) == 0) { char_system = ALPHANUM; }
if(is_sane(NESET, source) == 0) { char_system = NUMERIC; }
if(symbol->input_mode == KANJI_MODE) { char_system = KANJI; }
if(symbol->input_mode == SJIS_MODE) { char_system = KANJI; }
width = 0;
format = 0;

View File

@ -67,7 +67,7 @@ int qr_code(struct zint_symbol *symbol, unsigned char source[])
int i, j;
int kanji;
if(symbol->input_mode == KANJI_MODE) { kanji = 1; } else { kanji = 0; }
if((symbol->input_mode == KANJI_MODE) || (symbol->input_mode == SJIS_MODE)) { kanji = 1; } else { kanji = 0; }
code = encode(symbol->option_1, symbol->option_2, source, kanji);
if(code == NULL) {

6875
backend/sjis.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -146,6 +146,7 @@ struct zint_symbol {
#define UNICODE_MODE 1
#define GS1_MODE 2
#define KANJI_MODE 3
#define SJIS_MODE 4
#define WARN_INVALID_OPTION 2
#define ERROR_TOO_LONG 5

View File

@ -86,7 +86,8 @@ void usage(void)
" --mode=NUMBER (Maxicode and Composite) Set encoding mode.\n"
" --null=NUMBER Character to represent NULL.\n"
" --gs1 Treat input as GS1 data\n"
" --kanji Treat input as Kanji characters\n"
" --kanji Treat input as Kanji characters in Unicode\n"
" --sjis Treat input as Shift-JIS\n"
, ZINT_VERSION);
}
@ -162,6 +163,7 @@ int main(int argc, char **argv)
{"null=", 1, 0, 0},
{"gs1", 0, 0, 0},
{"kanji", 0, 0, 0},
{"sjis", 0, 0, 0},
{0, 0, 0, 0}
};
c = getopt_long(argc, argv, "htb:w:d:o:i:rcmp", long_options, &option_index);
@ -189,6 +191,9 @@ int main(int argc, char **argv)
if(!strcmp(long_options[option_index].name, "kanji")) {
my_symbol->input_mode = KANJI_MODE;
}
if(!strcmp(long_options[option_index].name, "sjis")) {
my_symbol->input_mode = SJIS_MODE;
}
if(!strcmp(long_options[option_index].name, "fg=")) {
strncpy(my_symbol->fgcolour, optarg, 7);
}

View File

@ -182,6 +182,9 @@ echo testing QR Code
./zint -o bar58.png -b 58 --border=10 -d "Demonstration QR Code symbol generated by libzint"
./zint -o bar58.eps -b 58 --border=10 -d "Demonstration QR Code symbol generated by libzint"
./zint -o bar58.svg -b 58 --border=10 -d "Demonstration QR Code symbol generated by libzint"
./zint -o bar58k.png -b 58 --kanji --border=10 -d "登録商標です。"
./zint -o bar58k.eps -b 58 --kanji --border=10 -d "登録商標です。"
./zint -o bar58k.svg -b 58 --kanji --border=10 -d "登録商標です。"
echo testing Code 128 Subset B
./zint -o bar60.png -b 60 --height=50 --border=10 -d 87654321
./zint -o bar60.eps -b 60 --height=50 --border=10 -d 87654321
@ -276,7 +279,6 @@ echo testing Micro PDF417
./zint -o bar84.png -b 84 --border=10 -d "Demonstration MicroPDF417 symbol generated by libzint"
./zint -o bar84.eps -b 84 --border=10 -d "Demonstration MicroPDF417 symbol generated by libzint"
./zint -o bar84.svg -b 84 --border=10 -d "Demonstration MicroPDF417 symbol generated by libzint"
./zint -o bar84k.png -b 84 --border=10 -d "QR コードはデンソーウェーブの登録商標です。"
echo testing USPS OneCode 4-State Customer Barcode
./zint -o bar85.png -b 85 --border=10 -d 01234567094987654321
./zint -o bar85.eps -b 85 --border=10 -d 01234567094987654321
@ -312,6 +314,9 @@ echo testing Micro QR Code
./zint -o bar97.png -b 97 --border=10 -d "MicroQR Code"
./zint -o bar97.eps -b 97 --border=10 -d "MicroQR Code"
./zint -o bar97.svg -b 97 --border=10 -d "MicroQR Code"
./zint -o bar97k.png -b 97 --kanji --border=10 -d "登録商標です。"
./zint -o bar97k.eps -b 97 --kanji --border=10 -d "登録商標です。"
./zint -o bar97k.svg -b 97 --kanji --border=10 -d "登録商標です。"
echo testing HIBC LIC 128
./zint -o bar98.png -b 98 --border=10 -d "A99912345/9901510X3"
./zint -o bar98.eps -b 98 --border=10 -d "A99912345/9901510X3"