HighLevelEncoder.java (zxing-zxing-3.4.1) | : | HighLevelEncoder.java (zxing-zxing-3.5.0) | ||
---|---|---|---|---|
skipping to change at line 20 | skipping to change at line 20 | |||
* Unless required by applicable law or agreed to in writing, software | * Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | * distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | * See the License for the specific language governing permissions and | |||
* limitations under the License. | * limitations under the License. | |||
*/ | */ | |||
package com.google.zxing.aztec.encoder; | package com.google.zxing.aztec.encoder; | |||
import com.google.zxing.common.BitArray; | import com.google.zxing.common.BitArray; | |||
import com.google.zxing.common.CharacterSetECI; | ||||
import java.nio.charset.Charset; | ||||
import java.util.Arrays; | import java.util.Arrays; | |||
import java.util.Collection; | import java.util.Collection; | |||
import java.util.Collections; | import java.util.Collections; | |||
import java.util.Comparator; | import java.util.Comparator; | |||
import java.util.Deque; | ||||
import java.util.Iterator; | import java.util.Iterator; | |||
import java.util.LinkedList; | import java.util.LinkedList; | |||
/** | /** | |||
* This produces nearly optimal encodings of text into the first-level of | * This produces nearly optimal encodings of text into the first-level of | |||
* encoding used by Aztec code. | * encoding used by Aztec code. | |||
* | * | |||
* It uses a dynamic algorithm. For each prefix of the string, it determines | * It uses a dynamic algorithm. For each prefix of the string, it determines | |||
* a set of encodings that could lead to this prefix. We repeatedly add a | * a set of encodings that could lead to this prefix. We repeatedly add a | |||
* character and generate a new set of optimal encodings until we have read | * character and generate a new set of optimal encodings until we have read | |||
skipping to change at line 151 | skipping to change at line 155 | |||
SHIFT_TABLE[MODE_LOWER][MODE_PUNCT] = 0; | SHIFT_TABLE[MODE_LOWER][MODE_PUNCT] = 0; | |||
SHIFT_TABLE[MODE_LOWER][MODE_UPPER] = 28; | SHIFT_TABLE[MODE_LOWER][MODE_UPPER] = 28; | |||
SHIFT_TABLE[MODE_MIXED][MODE_PUNCT] = 0; | SHIFT_TABLE[MODE_MIXED][MODE_PUNCT] = 0; | |||
SHIFT_TABLE[MODE_DIGIT][MODE_PUNCT] = 0; | SHIFT_TABLE[MODE_DIGIT][MODE_PUNCT] = 0; | |||
SHIFT_TABLE[MODE_DIGIT][MODE_UPPER] = 15; | SHIFT_TABLE[MODE_DIGIT][MODE_UPPER] = 15; | |||
} | } | |||
private final byte[] text; | private final byte[] text; | |||
private final Charset charset; | ||||
public HighLevelEncoder(byte[] text) { | public HighLevelEncoder(byte[] text) { | |||
this.text = text; | this.text = text; | |||
this.charset = null; | ||||
} | ||||
public HighLevelEncoder(byte[] text, Charset charset) { | ||||
this.text = text; | ||||
this.charset = charset; | ||||
} | } | |||
/** | /** | |||
* @return text represented by this encoder encoded as a {@link BitArray} | * @return text represented by this encoder encoded as a {@link BitArray} | |||
*/ | */ | |||
public BitArray encode() { | public BitArray encode() { | |||
Collection<State> states = Collections.singletonList(State.INITIAL_STATE); | State initialState = State.INITIAL_STATE; | |||
if (charset != null) { | ||||
CharacterSetECI eci = CharacterSetECI.getCharacterSetECI(charset); | ||||
if (null == eci) { | ||||
throw new IllegalArgumentException("No ECI code for character set " + ch | ||||
arset); | ||||
} | ||||
initialState = initialState.appendFLGn(eci.getValue()); | ||||
} | ||||
Collection<State> states = Collections.singletonList(initialState); | ||||
for (int index = 0; index < text.length; index++) { | for (int index = 0; index < text.length; index++) { | |||
int pairCode; | int pairCode; | |||
int nextChar = index + 1 < text.length ? text[index + 1] : 0; | int nextChar = index + 1 < text.length ? text[index + 1] : 0; | |||
switch (text[index]) { | switch (text[index]) { | |||
case '\r': | case '\r': | |||
pairCode = nextChar == '\n' ? 2 : 0; | pairCode = nextChar == '\n' ? 2 : 0; | |||
break; | break; | |||
case '.' : | case '.' : | |||
pairCode = nextChar == ' ' ? 3 : 0; | pairCode = nextChar == ' ' ? 3 : 0; | |||
break; | break; | |||
skipping to change at line 286 | skipping to change at line 305 | |||
} | } | |||
if (state.getBinaryShiftByteCount() > 0) { | if (state.getBinaryShiftByteCount() > 0) { | |||
// It only makes sense to do the characters as binary if we're already | // It only makes sense to do the characters as binary if we're already | |||
// in binary mode. | // in binary mode. | |||
State binaryState = state.addBinaryShiftChar(index).addBinaryShiftChar(ind ex + 1); | State binaryState = state.addBinaryShiftChar(index).addBinaryShiftChar(ind ex + 1); | |||
result.add(binaryState); | result.add(binaryState); | |||
} | } | |||
} | } | |||
private static Collection<State> simplifyStates(Iterable<State> states) { | private static Collection<State> simplifyStates(Iterable<State> states) { | |||
Collection<State> result = new LinkedList<>(); | Deque<State> result = new LinkedList<>(); | |||
for (State newState : states) { | for (State newState : states) { | |||
boolean add = true; | boolean add = true; | |||
for (Iterator<State> iterator = result.iterator(); iterator.hasNext();) { | for (Iterator<State> iterator = result.iterator(); iterator.hasNext();) { | |||
State oldState = iterator.next(); | State oldState = iterator.next(); | |||
if (oldState.isBetterThanOrEqualTo(newState)) { | if (oldState.isBetterThanOrEqualTo(newState)) { | |||
add = false; | add = false; | |||
break; | break; | |||
} | } | |||
if (newState.isBetterThanOrEqualTo(oldState)) { | if (newState.isBetterThanOrEqualTo(oldState)) { | |||
iterator.remove(); | iterator.remove(); | |||
} | } | |||
} | } | |||
if (add) { | if (add) { | |||
result.add(newState); | result.addFirst(newState); | |||
} | } | |||
} | } | |||
return result; | return result; | |||
} | } | |||
} | } | |||
End of changes. 7 change blocks. | ||||
3 lines changed or deleted | 23 lines changed or added |