OLD | NEW |
| (Empty) |
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 "barcode.h" | |
24 #include "include/BC_Encoder.h" | |
25 #include "include/BC_CommonBitMatrix.h" | |
26 #include "include/BC_Dimension.h" | |
27 #include "include/BC_SymbolShapeHint.h" | |
28 #include "include/BC_SymbolInfo.h" | |
29 #include "include/BC_EncoderContext.h" | |
30 #include "include/BC_HighLevelEncoder.h" | |
31 #include "include/BC_EdifactEncoder.h" | |
32 CBC_EdifactEncoder::CBC_EdifactEncoder() | |
33 { | |
34 } | |
35 CBC_EdifactEncoder::~CBC_EdifactEncoder() | |
36 { | |
37 } | |
38 FX_INT32 CBC_EdifactEncoder::getEncodingMode() | |
39 { | |
40 return EDIFACT_ENCODATION; | |
41 } | |
42 void CBC_EdifactEncoder::Encode(CBC_EncoderContext &context, FX_INT32 &e) | |
43 { | |
44 CFX_WideString buffer; | |
45 while (context.hasMoreCharacters()) { | |
46 FX_WCHAR c = context.getCurrentChar(); | |
47 encodeChar(c, buffer, e); | |
48 if (e != BCExceptionNO) { | |
49 return; | |
50 } | |
51 context.m_pos++; | |
52 FX_INT32 count = buffer.GetLength(); | |
53 if (count >= 4) { | |
54 context.writeCodewords(encodeToCodewords(buffer, 0, e)); | |
55 if (e != BCExceptionNO) { | |
56 return; | |
57 } | |
58 buffer.Delete(0, 4); | |
59 FX_INT32 newMode = CBC_HighLevelEncoder::lookAheadTest(context.m_msg
, context.m_pos, getEncodingMode()); | |
60 if (newMode != getEncodingMode()) { | |
61 context.signalEncoderChange(ASCII_ENCODATION); | |
62 break; | |
63 } | |
64 } | |
65 } | |
66 buffer += (FX_WCHAR)31; | |
67 handleEOD(context, buffer, e); | |
68 } | |
69 void CBC_EdifactEncoder::handleEOD(CBC_EncoderContext &context, CFX_WideString b
uffer, FX_INT32 &e) | |
70 { | |
71 FX_INT32 count = buffer.GetLength(); | |
72 if (count == 0) { | |
73 return; | |
74 } | |
75 if (count == 1) { | |
76 context.updateSymbolInfo(e); | |
77 if (e != BCExceptionNO) { | |
78 return; | |
79 } | |
80 FX_INT32 available = context.m_symbolInfo->m_dataCapacity - context.getC
odewordCount(); | |
81 FX_INT32 remaining = context.getRemainingCharacters(); | |
82 if (remaining == 0 && available <= 2) { | |
83 return; | |
84 } | |
85 } | |
86 if (count > 4) { | |
87 e = BCExceptionIllegalStateCountMustNotExceed4; | |
88 return; | |
89 } | |
90 FX_INT32 restChars = count - 1; | |
91 CFX_WideString encoded = encodeToCodewords(buffer, 0, e); | |
92 if (e != BCExceptionNO) { | |
93 return; | |
94 } | |
95 FX_BOOL endOfSymbolReached = !context.hasMoreCharacters(); | |
96 FX_BOOL restInAscii = endOfSymbolReached && restChars <= 2; | |
97 if (restChars <= 2) { | |
98 context.updateSymbolInfo(context.getCodewordCount() + restChars, e); | |
99 if (e != BCExceptionNO) { | |
100 return; | |
101 } | |
102 FX_INT32 available = context.m_symbolInfo->m_dataCapacity - context.getC
odewordCount(); | |
103 if (available >= 3) { | |
104 restInAscii = FALSE; | |
105 context.updateSymbolInfo(context.getCodewordCount() + encoded.GetLen
gth(), e); | |
106 if (e != BCExceptionNO) { | |
107 return; | |
108 } | |
109 } | |
110 } | |
111 if (restInAscii) { | |
112 context.resetSymbolInfo(); | |
113 context.m_pos -= restChars; | |
114 } else { | |
115 context.writeCodewords(encoded); | |
116 } | |
117 context.signalEncoderChange(ASCII_ENCODATION); | |
118 } | |
119 void CBC_EdifactEncoder::encodeChar(FX_WCHAR c, CFX_WideString &sb, FX_INT32 &e) | |
120 { | |
121 if (c >= ' ' && c <= '?') { | |
122 sb += c; | |
123 } else if (c >= '@' && c <= '^') { | |
124 sb += (FX_WCHAR)(c - 64); | |
125 } else { | |
126 CBC_HighLevelEncoder::illegalCharacter(c, e); | |
127 } | |
128 } | |
129 CFX_WideString CBC_EdifactEncoder::encodeToCodewords(CFX_WideString sb, FX_INT32
startPos, FX_INT32 &e) | |
130 { | |
131 FX_INT32 len = sb.GetLength() - startPos; | |
132 if (len == 0) { | |
133 e = BCExceptionNoContents; | |
134 return (FX_LPWSTR)""; | |
135 } | |
136 FX_WCHAR c1 = sb.GetAt(startPos); | |
137 FX_WCHAR c2 = len >= 2 ? sb.GetAt(startPos + 1) : 0; | |
138 FX_WCHAR c3 = len >= 3 ? sb.GetAt(startPos + 2) : 0; | |
139 FX_WCHAR c4 = len >= 4 ? sb.GetAt(startPos + 3) : 0; | |
140 FX_INT32 v = (c1 << 18) + (c2 << 12) + (c3 << 6) + c4; | |
141 FX_WCHAR cw1 = (FX_WCHAR) ((v >> 16) & 255); | |
142 FX_WCHAR cw2 = (FX_WCHAR) ((v >> 8) & 255); | |
143 FX_WCHAR cw3 = (FX_WCHAR) (v & 255); | |
144 CFX_WideString res; | |
145 res += cw1; | |
146 if (len >= 2) { | |
147 res += cw2; | |
148 } | |
149 if (len >= 3) { | |
150 res += cw3; | |
151 } | |
152 return res; | |
153 } | |
OLD | NEW |