package com.google.zxing.datamatrix.encoder; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes3.dex */ public class C40Encoder implements Encoder { @Override // com.google.zxing.datamatrix.encoder.Encoder public int getEncodingMode() { return 1; } @Override // com.google.zxing.datamatrix.encoder.Encoder public void encode(EncoderContext encoderContext) { StringBuilder sb = new StringBuilder(); while (true) { if (!encoderContext.hasMoreCharacters()) { break; } char currentChar = encoderContext.getCurrentChar(); encoderContext.pos++; int encodeChar = encodeChar(currentChar, sb); int codewordCount = encoderContext.getCodewordCount() + ((sb.length() / 3) << 1); encoderContext.updateSymbolInfo(codewordCount); int dataCapacity = encoderContext.getSymbolInfo().getDataCapacity() - codewordCount; if (!encoderContext.hasMoreCharacters()) { StringBuilder sb2 = new StringBuilder(); if (sb.length() % 3 == 2 && dataCapacity != 2) { encodeChar = backtrackOneCharacter(encoderContext, sb, sb2, encodeChar); } while (sb.length() % 3 == 1 && (encodeChar > 3 || dataCapacity != 1)) { encodeChar = backtrackOneCharacter(encoderContext, sb, sb2, encodeChar); } } else if (sb.length() % 3 == 0 && HighLevelEncoder.lookAheadTest(encoderContext.getMessage(), encoderContext.pos, getEncodingMode()) != getEncodingMode()) { encoderContext.signalEncoderChange(0); break; } } handleEOD(encoderContext, sb); } private int backtrackOneCharacter(EncoderContext encoderContext, StringBuilder sb, StringBuilder sb2, int i) { int length = sb.length(); sb.delete(length - i, length); encoderContext.pos--; int encodeChar = encodeChar(encoderContext.getCurrentChar(), sb2); encoderContext.resetSymbolInfo(); return encodeChar; } /* JADX INFO: Access modifiers changed from: package-private */ public static void writeNextTriplet(EncoderContext encoderContext, StringBuilder sb) { encoderContext.writeCodewords(encodeToCodewords(sb)); sb.delete(0, 3); } void handleEOD(EncoderContext encoderContext, StringBuilder sb) { int length = (sb.length() / 3) << 1; int length2 = sb.length() % 3; int codewordCount = encoderContext.getCodewordCount() + length; encoderContext.updateSymbolInfo(codewordCount); int dataCapacity = encoderContext.getSymbolInfo().getDataCapacity() - codewordCount; if (length2 == 2) { sb.append((char) 0); while (sb.length() >= 3) { writeNextTriplet(encoderContext, sb); } if (encoderContext.hasMoreCharacters()) { encoderContext.writeCodeword((char) 254); } } else if (dataCapacity == 1 && length2 == 1) { while (sb.length() >= 3) { writeNextTriplet(encoderContext, sb); } if (encoderContext.hasMoreCharacters()) { encoderContext.writeCodeword((char) 254); } encoderContext.pos--; } else if (length2 == 0) { while (sb.length() >= 3) { writeNextTriplet(encoderContext, sb); } if (dataCapacity > 0 || encoderContext.hasMoreCharacters()) { encoderContext.writeCodeword((char) 254); } } else { throw new IllegalStateException("Unexpected case. Please report!"); } encoderContext.signalEncoderChange(0); } int encodeChar(char c, StringBuilder sb) { if (c == ' ') { sb.append((char) 3); return 1; } if (c >= '0' && c <= '9') { sb.append((char) (c - ',')); return 1; } if (c >= 'A' && c <= 'Z') { sb.append((char) (c - '3')); return 1; } if (c < ' ') { sb.append((char) 0); sb.append(c); return 2; } if (c <= '/') { sb.append((char) 1); sb.append((char) (c - '!')); return 2; } if (c <= '@') { sb.append((char) 1); sb.append((char) (c - '+')); return 2; } if (c <= '_') { sb.append((char) 1); sb.append((char) (c - 'E')); return 2; } if (c <= 127) { sb.append((char) 2); sb.append((char) (c - '`')); return 2; } sb.append("\u0001\u001e"); return encodeChar((char) (c - 128), sb) + 2; } private static String encodeToCodewords(CharSequence charSequence) { int charAt = (charSequence.charAt(0) * 1600) + (charSequence.charAt(1) * '(') + charSequence.charAt(2) + 1; return new String(new char[]{(char) (charAt / 256), (char) (charAt % 256)}); } }