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_ASCIIEncoder.h" 24 25#include "fxbarcode/datamatrix/BC_Encoder.h" 26#include "fxbarcode/datamatrix/BC_EncoderContext.h" 27#include "fxbarcode/datamatrix/BC_HighLevelEncoder.h" 28#include "fxbarcode/datamatrix/BC_SymbolInfo.h" 29#include "fxbarcode/utils.h" 30 31CBC_ASCIIEncoder::CBC_ASCIIEncoder() {} 32CBC_ASCIIEncoder::~CBC_ASCIIEncoder() {} 33int32_t CBC_ASCIIEncoder::getEncodingMode() { 34 return ASCII_ENCODATION; 35} 36void CBC_ASCIIEncoder::Encode(CBC_EncoderContext& context, int32_t& e) { 37 int32_t n = CBC_HighLevelEncoder::determineConsecutiveDigitCount( 38 context.m_msg, context.m_pos); 39 if (n >= 2) { 40 wchar_t code = encodeASCIIDigits(context.m_msg[context.m_pos], 41 context.m_msg[context.m_pos + 1], e); 42 if (e != BCExceptionNO) { 43 return; 44 } 45 context.writeCodeword(code); 46 context.m_pos += 2; 47 } else { 48 wchar_t c = context.getCurrentChar(); 49 int32_t newMode = CBC_HighLevelEncoder::lookAheadTest( 50 context.m_msg, context.m_pos, getEncodingMode()); 51 if (newMode != getEncodingMode()) { 52 switch (newMode) { 53 case BASE256_ENCODATION: 54 context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_BASE256); 55 context.signalEncoderChange(BASE256_ENCODATION); 56 return; 57 case C40_ENCODATION: 58 context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_C40); 59 context.signalEncoderChange(C40_ENCODATION); 60 return; 61 case X12_ENCODATION: 62 context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_ANSIX12); 63 context.signalEncoderChange(X12_ENCODATION); 64 break; 65 case TEXT_ENCODATION: 66 context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_TEXT); 67 context.signalEncoderChange(TEXT_ENCODATION); 68 break; 69 case EDIFACT_ENCODATION: 70 context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_EDIFACT); 71 context.signalEncoderChange(EDIFACT_ENCODATION); 72 break; 73 default: 74 e = BCExceptionIllegalStateIllegalMode; 75 return; 76 } 77 } else if (CBC_HighLevelEncoder::isExtendedASCII(c)) { 78 context.writeCodeword(CBC_HighLevelEncoder::UPPER_SHIFT); 79 context.writeCodeword((wchar_t)(c - 128 + 1)); 80 context.m_pos++; 81 } else { 82 context.writeCodeword((wchar_t)(c + 1)); 83 context.m_pos++; 84 } 85 } 86} 87wchar_t CBC_ASCIIEncoder::encodeASCIIDigits(wchar_t digit1, 88 wchar_t digit2, 89 int32_t& e) { 90 if (CBC_HighLevelEncoder::isDigit(digit1) && 91 CBC_HighLevelEncoder::isDigit(digit2)) { 92 int32_t num = (digit1 - 48) * 10 + (digit2 - 48); 93 return (wchar_t)(num + 130); 94 } 95 e = BCExceptionIllegalArgumentNotGigits; 96 return 0; 97} 98