encodeCharset method
Encode the string starting at position position starting with the character set charset
Implementation
int encodeCharset(String contents, Charset charset, int position) {
assert(position < contents.length);
final mCost = memoizedCost![charset.index][position];
if (mCost > 0) {
return mCost;
}
int minCost = MathUtils.maxValue;
Latch minLatch = Latch.none;
final atEnd = position + 1 >= contents.length;
final sets = [Charset.A, Charset.B];
for (int i = 0; i <= 1; i++) {
if (canEncode(contents, sets[i], position)) {
int cost = 1;
Latch latch = Latch.none;
if (charset != sets[i]) {
cost++;
latch = Latch.values[sets[i].index];
}
if (!atEnd) {
cost += encodeCharset(contents, sets[i], position + 1);
}
if (cost < minCost) {
minCost = cost;
minLatch = latch;
}
cost = 1;
if (charset == sets[(i + 1) % 2]) {
cost++;
latch = Latch.shift;
if (!atEnd) {
cost += encodeCharset(contents, charset, position + 1);
}
if (cost < minCost) {
minCost = cost;
minLatch = latch;
}
}
}
}
if (canEncode(contents, Charset.C, position)) {
int cost = 1;
Latch latch = Latch.none;
if (charset != Charset.C) {
cost++;
latch = Latch.C;
}
final advance =
contents.codeUnitAt(position) == Code128Writer._escapeFnc1 ? 1 : 2;
if (position + advance < contents.length) {
cost += encodeCharset(contents, Charset.C, position + advance);
}
if (cost < minCost) {
minCost = cost;
minLatch = latch;
}
}
if (minCost == MathUtils.maxValue) {
throw ArgumentError('Bad character in input: '
'ASCII value=${contents.codeUnitAt(position)}');
}
memoizedCost![charset.index][position] = minCost;
minPath![charset.index][position] = minLatch;
return minCost;
}