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