MCPcopy
hub / github.com/zxing-js/library / Base256Encoder

Class Base256Encoder

src/core/datamatrix/encoder/Base256Encoder.ts:9–72  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7import { BASE256_ENCODATION, ASCII_ENCODATION } from './constants';
8
9export class Base256Encoder implements Encoder {
10 public getEncodingMode() {
11 return BASE256_ENCODATION;
12 }
13
14 public encode(context: EncoderContext) {
15 const buffer = new StringBuilder();
16 buffer.append(0o0); // Initialize length field
17 while (context.hasMoreCharacters()) {
18 const c = context.getCurrentChar();
19 buffer.append(c);
20
21 context.pos++;
22
23 const newMode = HighLevelEncoder.lookAheadTest(
24 context.getMessage(),
25 context.pos,
26 this.getEncodingMode()
27 );
28 if (newMode !== this.getEncodingMode()) {
29 // Return to ASCII encodation, which will actually handle latch to new mode
30 context.signalEncoderChange(ASCII_ENCODATION);
31 break;
32 }
33 }
34 const dataCount = buffer.length() - 1;
35 const lengthFieldSize = 1;
36 const currentSize =
37 context.getCodewordCount() + dataCount + lengthFieldSize;
38 context.updateSymbolInfo(currentSize);
39 const mustPad = context.getSymbolInfo().getDataCapacity() - currentSize > 0;
40 if (context.hasMoreCharacters() || mustPad) {
41 if (dataCount <= 249) {
42 buffer.setCharAt(0, StringUtils.getCharAt(dataCount));
43 } else if (dataCount <= 1555) {
44 buffer.setCharAt(
45 0,
46 StringUtils.getCharAt(Math.floor(dataCount / 250) + 249)
47 );
48 buffer.insert(1, StringUtils.getCharAt(dataCount % 250));
49 } else {
50 throw new Error('Message length not in valid ranges: ' + dataCount);
51 }
52 }
53 for (let i = 0, c = buffer.length(); i < c; i++) {
54 context.writeCodeword(
55 this.randomize255State(
56 buffer.charAt(i).charCodeAt(0),
57 context.getCodewordCount() + 1
58 )
59 );
60 }
61 }
62
63 private randomize255State(ch: char, codewordPosition: number): number {
64 const pseudoRandom = ((149 * codewordPosition) % 255) + 1;
65 const tempVariable = ch + pseudoRandom;
66 if (tempVariable <= 255) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected