From ee78b9f9ea49560dc21d5eb25a704aff8f44152c Mon Sep 17 00:00:00 2001 From: Luiz Filipe Machado Barni Date: Wed, 26 Feb 2020 21:22:23 -0300 Subject: [PATCH] added enable decoding to StringBuilder --- .../pdf417/decoder/DecodedBitStreamParser.ts | 12 ++++++++++-- src/core/util/StringBuilder.ts | 16 +++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/core/pdf417/decoder/DecodedBitStreamParser.ts b/src/core/pdf417/decoder/DecodedBitStreamParser.ts index 84f8a1d3..ecbc5e34 100644 --- a/src/core/pdf417/decoder/DecodedBitStreamParser.ts +++ b/src/core/pdf417/decoder/DecodedBitStreamParser.ts @@ -128,10 +128,18 @@ export default /*final*/ class DecodedBitStreamParser { * @throws FormatException */ static decode(codewords: Int32Array, ecLevel: string): DecoderResult { + // pass encoding to result (will be used for decode symbols in byte mode) + let result: StringBuilder = new StringBuilder(''); // let encoding: Charset = StandardCharsets.ISO_8859_1; let encoding = CharacterSetECI.ISO8859_1; - // pass encoding to result (will be used for decode symbols in byte mode) - let result: StringBuilder = new StringBuilder('', encoding); + /** + * @note the next command is specific from this TypeScript library + * because TS can't properly cast some values to char and + * convert it to string later correctlt due to encoding + * differences from Java version. As reported here: + * https://github.com/zxing-js/library/pull/264/files#r382831593 + */ + result.enableDecoding(encoding); // Get compaction mode let codeIndex: int = 1; let code: int = codewords[codeIndex++]; diff --git a/src/core/util/StringBuilder.ts b/src/core/util/StringBuilder.ts index f7685234..70b4f224 100644 --- a/src/core/util/StringBuilder.ts +++ b/src/core/util/StringBuilder.ts @@ -1,16 +1,26 @@ +import CharacterSetECI from '../common/CharacterSetECI'; import StringEncoding from './StringEncoding'; + export default class StringBuilder { - public constructor(private value: string = '', private format = null) {} + private encoding: CharacterSetECI; + + public constructor(private value: string = '') {} + + public enableDecoding(encoding: CharacterSetECI): StringBuilder { + this.encoding = encoding + return this; + } public append(s: string | number): StringBuilder { if (typeof s === 'string') { this.value += s.toString(); - } else if (this.format) { + } else if (this.encoding) { // use passed format (fromCharCode will return UTF8 encoding) - this.value += StringEncoding.decode(new Uint8Array([s]), this.format); + this.value += StringEncoding.decode(new Uint8Array([s]), this.encoding); } else { + // correctly converts from UTF-8, but not other encodings this.value += String.fromCharCode(s); } return this;