Tighten up some non-private fields in DM encoder

git-svn-id: https://zxing.googlecode.com/svn/trunk@2800 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen@gmail.com 2013-05-17 15:50:51 +00:00
parent c608a08978
commit 5214bf1840
11 changed files with 62 additions and 44 deletions

View file

@ -26,14 +26,14 @@ final class ASCIIEncoder implements Encoder {
@Override
public void encode(EncoderContext context) {
//step B
int n = HighLevelEncoder.determineConsecutiveDigitCount(context.msg, context.pos);
int n = HighLevelEncoder.determineConsecutiveDigitCount(context.getMessage(), context.pos);
if (n >= 2) {
context.writeCodeword(encodeASCIIDigits(context.msg.charAt(context.pos),
context.msg.charAt(context.pos + 1)));
context.writeCodeword(encodeASCIIDigits(context.getMessage().charAt(context.pos),
context.getMessage().charAt(context.pos + 1)));
context.pos += 2;
} else {
char c = context.getCurrentChar();
int newMode = HighLevelEncoder.lookAheadTest(context.msg, context.pos, getEncodingMode());
int newMode = HighLevelEncoder.lookAheadTest(context.getMessage(), context.pos, getEncodingMode());
if (newMode != getEncodingMode()) {
switch (newMode) {
case HighLevelEncoder.BASE256_ENCODATION:

View file

@ -33,7 +33,7 @@ final class Base256Encoder implements Encoder {
context.pos++;
int newMode = HighLevelEncoder.lookAheadTest(context.msg, context.pos, getEncodingMode());
int newMode = HighLevelEncoder.lookAheadTest(context.getMessage(), context.pos, getEncodingMode());
if (newMode != getEncodingMode()) {
context.signalEncoderChange(newMode);
break;
@ -43,7 +43,7 @@ final class Base256Encoder implements Encoder {
int lengthFieldSize = 1;
int currentSize = context.getCodewordCount() + dataCount + lengthFieldSize;
context.updateSymbolInfo(currentSize);
boolean mustPad = (context.symbolInfo.dataCapacity - currentSize) > 0;
boolean mustPad = (context.getSymbolInfo().getDataCapacity() - currentSize) > 0;
if (context.hasMoreCharacters() || mustPad) {
if (dataCount <= 249) {
buffer.setCharAt(0, (char) dataCount);

View file

@ -37,7 +37,7 @@ class C40Encoder implements Encoder {
int curCodewordCount = context.getCodewordCount() + unwritten;
context.updateSymbolInfo(curCodewordCount);
int available = context.symbolInfo.dataCapacity - curCodewordCount;
int available = context.getSymbolInfo().getDataCapacity() - curCodewordCount;
if (!context.hasMoreCharacters()) {
//Avoid having a single C40 value in the last triplet
@ -57,7 +57,7 @@ class C40Encoder implements Encoder {
int count = buffer.length();
if ((count % 3) == 0) {
int newMode = HighLevelEncoder.lookAheadTest(context.msg, context.pos, getEncodingMode());
int newMode = HighLevelEncoder.lookAheadTest(context.getMessage(), context.pos, getEncodingMode());
if (newMode != getEncodingMode()) {
context.signalEncoderChange(newMode);
break;
@ -95,7 +95,7 @@ class C40Encoder implements Encoder {
int curCodewordCount = context.getCodewordCount() + unwritten;
context.updateSymbolInfo(curCodewordCount);
int available = context.symbolInfo.dataCapacity - curCodewordCount;
int available = context.getSymbolInfo().getDataCapacity() - curCodewordCount;
if (rest == 2) {
buffer.append('\0'); //Shift 1

View file

@ -19,9 +19,7 @@ package com.google.zxing.datamatrix.encoder;
final class DataMatrixSymbolInfo144 extends SymbolInfo {
DataMatrixSymbolInfo144() {
super(false, 1558, 620, 22, 22, 36);
this.rsBlockData = -1; //special! see below
this.rsBlockError = 62;
super(false, 1558, 620, 22, 22, 36, -1, 62);
}
@Override

View file

@ -37,7 +37,7 @@ final class EdifactEncoder implements Encoder {
context.writeCodewords(encodeToCodewords(buffer, 0));
buffer.delete(0, 4);
int newMode = HighLevelEncoder.lookAheadTest(context.msg, context.pos, getEncodingMode());
int newMode = HighLevelEncoder.lookAheadTest(context.getMessage(), context.pos, getEncodingMode());
if (newMode != getEncodingMode()) {
context.signalEncoderChange(HighLevelEncoder.ASCII_ENCODATION);
break;
@ -63,7 +63,7 @@ final class EdifactEncoder implements Encoder {
if (count == 1) {
//Only an unlatch at the end
context.updateSymbolInfo();
int available = context.symbolInfo.dataCapacity - context.getCodewordCount();
int available = context.getSymbolInfo().getDataCapacity() - context.getCodewordCount();
int remaining = context.getRemainingCharacters();
if (remaining == 0 && available <= 2) {
return; //No unlatch
@ -80,7 +80,7 @@ final class EdifactEncoder implements Encoder {
if (restChars <= 2) {
context.updateSymbolInfo(context.getCodewordCount() + restChars);
int available = context.symbolInfo.dataCapacity - context.getCodewordCount();
int available = context.getSymbolInfo().getDataCapacity() - context.getCodewordCount();
if (available >= 3) {
restInAscii = false;
context.updateSymbolInfo(context.getCodewordCount() + encoded.length());

View file

@ -22,14 +22,14 @@ import java.nio.charset.Charset;
final class EncoderContext {
String msg;
private final String msg;
private SymbolShapeHint shape;
private Dimension minSize;
private Dimension maxSize;
StringBuilder codewords;
private final StringBuilder codewords;
int pos;
int newEncoding;
SymbolInfo symbolInfo;
private int newEncoding;
private SymbolInfo symbolInfo;
private int skipAtEnd;
EncoderContext(String msg) {
@ -73,6 +73,10 @@ final class EncoderContext {
public char getCurrent() {
return msg.charAt(pos);
}
public StringBuilder getCodewords() {
return codewords;
}
public void writeCodewords(String codewords) {
this.codewords.append(codewords);
@ -85,6 +89,10 @@ final class EncoderContext {
public int getCodewordCount() {
return this.codewords.length();
}
public int getNewEncoding() {
return newEncoding;
}
public void signalEncoderChange(int encoding) {
this.newEncoding = encoding;
@ -105,13 +113,17 @@ final class EncoderContext {
public int getRemainingCharacters() {
return getTotalMessageCharCount() - pos;
}
public SymbolInfo getSymbolInfo() {
return symbolInfo;
}
public void updateSymbolInfo() {
updateSymbolInfo(getCodewordCount());
}
public void updateSymbolInfo(int len) {
if (this.symbolInfo == null || len > this.symbolInfo.dataCapacity) {
if (this.symbolInfo == null || len > this.symbolInfo.getDataCapacity()) {
this.symbolInfo = SymbolInfo.lookup(len, shape, minSize, maxSize, true);
}
}

View file

@ -99,15 +99,15 @@ public final class ErrorCorrection {
* @return the codewords with interleaved error correction.
*/
public static String encodeECC200(String codewords, SymbolInfo symbolInfo) {
if (codewords.length() != symbolInfo.dataCapacity) {
if (codewords.length() != symbolInfo.getDataCapacity()) {
throw new IllegalArgumentException(
"The number of codewords does not match the selected symbol");
}
StringBuilder sb = new StringBuilder(symbolInfo.dataCapacity + symbolInfo.errorCodewords);
StringBuilder sb = new StringBuilder(symbolInfo.getDataCapacity() + symbolInfo.getErrorCodewords());
sb.append(codewords);
int blockCount = symbolInfo.getInterleavedBlockCount();
if (blockCount == 1) {
String ecc = createECCBlock(codewords, symbolInfo.errorCodewords);
String ecc = createECCBlock(codewords, symbolInfo.getErrorCodewords());
sb.append(ecc);
} else {
sb.setLength(sb.capacity());
@ -124,13 +124,13 @@ public final class ErrorCorrection {
}
for (int block = 0; block < blockCount; block++) {
StringBuilder temp = new StringBuilder(dataSizes[block]);
for (int d = block; d < symbolInfo.dataCapacity; d += blockCount) {
for (int d = block; d < symbolInfo.getDataCapacity(); d += blockCount) {
temp.append(codewords.charAt(d));
}
String ecc = createECCBlock(temp.toString(), errorSizes[block]);
int pos = 0;
for (int e = block; e < errorSizes[block] * blockCount; e += blockCount) {
sb.setCharAt(symbolInfo.dataCapacity + e, ecc.charAt(pos++));
sb.setCharAt(symbolInfo.getDataCapacity() + e, ecc.charAt(pos++));
}
}
}

View file

@ -178,21 +178,21 @@ public final class HighLevelEncoder {
int encodingMode = ASCII_ENCODATION; //Default mode
while (context.hasMoreCharacters()) {
encoders[encodingMode].encode(context);
if (context.newEncoding >= 0) {
encodingMode = context.newEncoding;
if (context.getNewEncoding() >= 0) {
encodingMode = context.getNewEncoding();
context.resetEncoderSignal();
}
}
int len = context.codewords.length();
int len = context.getCodewordCount();
context.updateSymbolInfo();
int capacity = context.symbolInfo.dataCapacity;
int capacity = context.getSymbolInfo().getDataCapacity();
if (len < capacity) {
if (encodingMode != ASCII_ENCODATION && encodingMode != BASE256_ENCODATION) {
context.writeCodeword('\u00fe'); //Unlatch (254)
}
}
//Padding
StringBuilder codewords = context.codewords;
StringBuilder codewords = context.getCodewords();
if (codewords.length() < capacity) {
codewords.append(PAD);
}
@ -200,7 +200,7 @@ public final class HighLevelEncoder {
codewords.append(randomize253State(PAD, codewords.length() + 1));
}
return context.codewords.toString();
return context.getCodewords().toString();
}
static int lookAheadTest(CharSequence msg, int startpos, int currentMode) {

View file

@ -73,13 +73,13 @@ public class SymbolInfo {
}
private final boolean rectangular;
final int dataCapacity;
final int errorCodewords;
private final int dataCapacity;
private final int errorCodewords;
public final int matrixWidth;
public final int matrixHeight;
private final int dataRegions;
int rsBlockData;
int rsBlockError;
private int rsBlockData;
private int rsBlockError;
public SymbolInfo(boolean rectangular, int dataCapacity, int errorCodewords,
int matrixWidth, int matrixHeight, int dataRegions) {
@ -87,9 +87,9 @@ public class SymbolInfo {
dataCapacity, errorCodewords);
}
private SymbolInfo(boolean rectangular, int dataCapacity, int errorCodewords,
int matrixWidth, int matrixHeight, int dataRegions,
int rsBlockData, int rsBlockError) {
SymbolInfo(boolean rectangular, int dataCapacity, int errorCodewords,
int matrixWidth, int matrixHeight, int dataRegions,
int rsBlockData, int rsBlockError) {
this.rectangular = rectangular;
this.dataCapacity = dataCapacity;
this.errorCodewords = errorCodewords;
@ -209,6 +209,14 @@ public class SymbolInfo {
public int getInterleavedBlockCount() {
return dataCapacity / rsBlockData;
}
public int getDataCapacity() {
return dataCapacity;
}
public int getErrorCodewords() {
return errorCodewords;
}
public int getDataLengthForInterleavedBlock(int index) {
return rsBlockData;

View file

@ -37,7 +37,7 @@ final class X12Encoder extends C40Encoder {
if ((count % 3) == 0) {
writeNextTriplet(context, buffer);
int newMode = HighLevelEncoder.lookAheadTest(context.msg, context.pos, getEncodingMode());
int newMode = HighLevelEncoder.lookAheadTest(context.getMessage(), context.pos, getEncodingMode());
if (newMode != getEncodingMode()) {
context.signalEncoderChange(newMode);
break;
@ -70,7 +70,7 @@ final class X12Encoder extends C40Encoder {
@Override
void handleEOD(EncoderContext context, StringBuilder buffer) {
context.updateSymbolInfo();
int available = context.symbolInfo.dataCapacity - context.getCodewordCount();
int available = context.getSymbolInfo().getDataCapacity() - context.getCodewordCount();
int count = buffer.length();
if (count == 2) {
context.writeCodeword(HighLevelEncoder.X12_UNLATCH);

View file

@ -28,28 +28,28 @@ public final class SymbolInfoTestCase extends Assert {
@Test
public void testSymbolInfo() {
SymbolInfo info = SymbolInfo.lookup(3);
assertEquals(5, info.errorCodewords);
assertEquals(5, info.getErrorCodewords());
assertEquals(8, info.matrixWidth);
assertEquals(8, info.matrixHeight);
assertEquals(10, info.getSymbolWidth());
assertEquals(10, info.getSymbolHeight());
info = SymbolInfo.lookup(3, SymbolShapeHint.FORCE_RECTANGLE);
assertEquals(7, info.errorCodewords);
assertEquals(7, info.getErrorCodewords());
assertEquals(16, info.matrixWidth);
assertEquals(6, info.matrixHeight);
assertEquals(18, info.getSymbolWidth());
assertEquals(8, info.getSymbolHeight());
info = SymbolInfo.lookup(9);
assertEquals(11, info.errorCodewords);
assertEquals(11, info.getErrorCodewords());
assertEquals(14, info.matrixWidth);
assertEquals(6, info.matrixHeight);
assertEquals(32, info.getSymbolWidth());
assertEquals(8, info.getSymbolHeight());
info = SymbolInfo.lookup(9, SymbolShapeHint.FORCE_SQUARE);
assertEquals(12, info.errorCodewords);
assertEquals(12, info.getErrorCodewords());
assertEquals(14, info.matrixWidth);
assertEquals(14, info.matrixHeight);
assertEquals(16, info.getSymbolWidth());