1 // Copyright 2014 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 // Original code is licensed as follows: 7 /* 8 * Copyright 2006-2007 Jeremias Maerki. 9 * 10 * Licensed under the Apache License, Version 2.0 (the "License"); 11 * you may not use this file except in compliance with the License. 12 * You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, software 17 * distributed under the License is distributed on an "AS IS" BASIS, 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 * See the License for the specific language governing permissions and 20 * limitations under the License. 21 */ 22 23 #include "fxbarcode/datamatrix/BC_C40Encoder.h" 24 25 #include "fxbarcode/common/BC_CommonBitMatrix.h" 26 #include "fxbarcode/datamatrix/BC_Encoder.h" 27 #include "fxbarcode/datamatrix/BC_EncoderContext.h" 28 #include "fxbarcode/datamatrix/BC_HighLevelEncoder.h" 29 #include "fxbarcode/datamatrix/BC_SymbolInfo.h" 30 #include "fxbarcode/utils.h" 31 32 namespace { 33 34 WideString EncodeToC40Codewords(const WideString& sb, int32_t startPos) { 35 wchar_t c1 = sb[startPos]; 36 wchar_t c2 = sb[startPos + 1]; 37 wchar_t c3 = sb[startPos + 2]; 38 int32_t v = (1600 * c1) + (40 * c2) + c3 + 1; 39 wchar_t cw[2]; 40 cw[0] = static_cast<wchar_t>(v / 256); 41 cw[1] = static_cast<wchar_t>(v % 256); 42 return WideString(cw, FX_ArraySize(cw)); 43 } 44 45 } // namespace 46 47 CBC_C40Encoder::CBC_C40Encoder() {} 48 CBC_C40Encoder::~CBC_C40Encoder() {} 49 int32_t CBC_C40Encoder::getEncodingMode() { 50 return C40_ENCODATION; 51 } 52 void CBC_C40Encoder::Encode(CBC_EncoderContext& context, int32_t& e) { 53 WideString buffer; 54 while (context.hasMoreCharacters()) { 55 wchar_t c = context.getCurrentChar(); 56 context.m_pos++; 57 int32_t lastCharSize = encodeChar(c, buffer, e); 58 if (e != BCExceptionNO) { 59 return; 60 } 61 int32_t unwritten = (buffer.GetLength() / 3) * 2; 62 int32_t curCodewordCount = context.getCodewordCount() + unwritten; 63 context.updateSymbolInfo(curCodewordCount, e); 64 if (e != BCExceptionNO) { 65 return; 66 } 67 int32_t available = context.m_symbolInfo->dataCapacity() - curCodewordCount; 68 if (!context.hasMoreCharacters()) { 69 if ((buffer.GetLength() % 3) == 2) { 70 if (available < 2 || available > 2) { 71 lastCharSize = BacktrackOneCharacter(&context, &buffer, lastCharSize); 72 if (lastCharSize < 0) { 73 e = BCExceptionGeneric; 74 return; 75 } 76 } 77 } 78 while ((buffer.GetLength() % 3) == 1 && 79 ((lastCharSize <= 3 && available != 1) || lastCharSize > 3)) { 80 lastCharSize = BacktrackOneCharacter(&context, &buffer, lastCharSize); 81 if (lastCharSize < 0) { 82 e = BCExceptionGeneric; 83 return; 84 } 85 } 86 break; 87 } 88 int32_t count = buffer.GetLength(); 89 if ((count % 3) == 0) { 90 int32_t newMode = CBC_HighLevelEncoder::lookAheadTest( 91 context.m_msg, context.m_pos, getEncodingMode()); 92 if (newMode != getEncodingMode()) { 93 context.signalEncoderChange(newMode); 94 break; 95 } 96 } 97 } 98 handleEOD(context, buffer, e); 99 } 100 void CBC_C40Encoder::writeNextTriplet(CBC_EncoderContext& context, 101 WideString& buffer) { 102 context.writeCodewords(EncodeToC40Codewords(buffer, 0)); 103 buffer.Delete(0, 3); 104 } 105 void CBC_C40Encoder::handleEOD(CBC_EncoderContext& context, 106 WideString& buffer, 107 int32_t& e) { 108 int32_t unwritten = (buffer.GetLength() / 3) * 2; 109 int32_t rest = buffer.GetLength() % 3; 110 int32_t curCodewordCount = context.getCodewordCount() + unwritten; 111 context.updateSymbolInfo(curCodewordCount, e); 112 if (e != BCExceptionNO) { 113 return; 114 } 115 int32_t available = context.m_symbolInfo->dataCapacity() - curCodewordCount; 116 if (rest == 2) { 117 buffer += (wchar_t)'\0'; 118 while (buffer.GetLength() >= 3) { 119 writeNextTriplet(context, buffer); 120 } 121 if (context.hasMoreCharacters()) { 122 context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); 123 } 124 } else if (available == 1 && rest == 1) { 125 while (buffer.GetLength() >= 3) { 126 writeNextTriplet(context, buffer); 127 } 128 if (context.hasMoreCharacters()) { 129 context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); 130 } 131 context.m_pos--; 132 } else if (rest == 0) { 133 while (buffer.GetLength() >= 3) { 134 writeNextTriplet(context, buffer); 135 } 136 if (available > 0 || context.hasMoreCharacters()) { 137 context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); 138 } 139 } else { 140 e = BCExceptionIllegalStateUnexpectedCase; 141 return; 142 } 143 context.signalEncoderChange(ASCII_ENCODATION); 144 } 145 int32_t CBC_C40Encoder::encodeChar(wchar_t c, WideString& sb, int32_t& e) { 146 if (c == ' ') { 147 sb += (wchar_t)'\3'; 148 return 1; 149 } else if ((c >= '0') && (c <= '9')) { 150 sb += (wchar_t)(c - 48 + 4); 151 return 1; 152 } else if ((c >= 'A') && (c <= 'Z')) { 153 sb += (wchar_t)(c - 65 + 14); 154 return 1; 155 } else if (c <= 0x1f) { 156 sb += (wchar_t)'\0'; 157 sb += c; 158 return 2; 159 } else if ((c >= '!') && (c <= '/')) { 160 sb += (wchar_t)'\1'; 161 sb += (wchar_t)(c - 33); 162 return 2; 163 } else if ((c >= ':') && (c <= '@')) { 164 sb += (wchar_t)'\1'; 165 sb += (wchar_t)(c - 58 + 15); 166 return 2; 167 } else if ((c >= '[') && (c <= '_')) { 168 sb += (wchar_t)'\1'; 169 sb += (wchar_t)(c - 91 + 22); 170 return 2; 171 } else if ((c >= 60) && (c <= 0x7f)) { 172 sb += (wchar_t)'\2'; 173 sb += (wchar_t)(c - 96); 174 return 2; 175 } else if (c >= 80) { 176 sb += (wchar_t)'\1'; 177 sb += (wchar_t)0x001e; 178 int32_t len = 2; 179 len += encodeChar((c - 128), sb, e); 180 if (e != BCExceptionNO) 181 return 0; 182 return len; 183 } else { 184 e = BCExceptionIllegalArgument; 185 return 0; 186 } 187 } 188 189 int32_t CBC_C40Encoder::BacktrackOneCharacter(CBC_EncoderContext* context, 190 WideString* buffer, 191 int32_t lastCharSize) { 192 if (context->m_pos < 1) 193 return -1; 194 195 int32_t count = buffer->GetLength(); 196 if (count < lastCharSize) 197 return -1; 198 199 buffer->Delete(count - lastCharSize, lastCharSize); 200 context->m_pos--; 201 wchar_t c = context->getCurrentChar(); 202 int32_t e = BCExceptionNO; 203 WideString removed; 204 int32_t len = encodeChar(c, removed, e); 205 if (e != BCExceptionNO) 206 return -1; 207 208 ASSERT(len > 0); 209 context->resetSymbolInfo(); 210 return len; 211 } 212