Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / xfa / src / fxbarcode / src / BC_ASCIIEncoder.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.\r
2 // Use of this source code is governed by a BSD-style license that can be\r
3 // found in the LICENSE file.\r
4 \r
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
6 \r
7 #include "barcode.h"\r
8 #include "include/BC_Encoder.h"\r
9 #include "include/BC_Dimension.h"\r
10 #include "include/BC_SymbolShapeHint.h"\r
11 #include "include/BC_SymbolInfo.h"\r
12 #include "include/BC_EncoderContext.h"\r
13 #include "include/BC_HighLevelEncoder.h"\r
14 #include "include/BC_ASCIIEncoder.h"\r
15 CBC_ASCIIEncoder::CBC_ASCIIEncoder()\r
16 {\r
17 }\r
18 CBC_ASCIIEncoder::~CBC_ASCIIEncoder()\r
19 {\r
20 }\r
21 FX_INT32 CBC_ASCIIEncoder::getEncodingMode()\r
22 {\r
23     return ASCII_ENCODATION;\r
24 }\r
25 void CBC_ASCIIEncoder::Encode(CBC_EncoderContext &context, FX_INT32 &e)\r
26 {\r
27     FX_INT32 n = CBC_HighLevelEncoder::determineConsecutiveDigitCount(context.m_msg, context.m_pos);\r
28     if (n >= 2) {\r
29         FX_WCHAR code = encodeASCIIDigits(context.m_msg.GetAt(context.m_pos), context.m_msg.GetAt(context.m_pos + 1), e);\r
30         if (e != BCExceptionNO) {\r
31             return;\r
32         }\r
33         context.writeCodeword(code);\r
34         context.m_pos += 2;\r
35     } else {\r
36         FX_WCHAR c = context.getCurrentChar();\r
37         FX_INT32 newMode = CBC_HighLevelEncoder::lookAheadTest(context.m_msg, context.m_pos, getEncodingMode());\r
38         if (newMode != getEncodingMode()) {\r
39             switch (newMode) {\r
40                 case BASE256_ENCODATION:\r
41                     context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_BASE256);\r
42                     context.signalEncoderChange(BASE256_ENCODATION);\r
43                     return;\r
44                 case C40_ENCODATION:\r
45                     context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_C40);\r
46                     context.signalEncoderChange(C40_ENCODATION);\r
47                     return;\r
48                 case X12_ENCODATION:\r
49                     context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_ANSIX12);\r
50                     context.signalEncoderChange(X12_ENCODATION);\r
51                     break;\r
52                 case TEXT_ENCODATION:\r
53                     context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_TEXT);\r
54                     context.signalEncoderChange(TEXT_ENCODATION);\r
55                     break;\r
56                 case EDIFACT_ENCODATION:\r
57                     context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_EDIFACT);\r
58                     context.signalEncoderChange(EDIFACT_ENCODATION);\r
59                     break;\r
60                 default:\r
61                     e = BCExceptionIllegalStateIllegalMode;\r
62                     return;\r
63             }\r
64         } else if (CBC_HighLevelEncoder::isExtendedASCII(c)) {\r
65             context.writeCodeword(CBC_HighLevelEncoder::UPPER_SHIFT);\r
66             context.writeCodeword((FX_WCHAR) (c - 128 + 1));\r
67             context.m_pos++;\r
68         } else {\r
69             context.writeCodeword((FX_WCHAR) (c + 1));\r
70             context.m_pos++;\r
71         }\r
72     }\r
73 }\r
74 FX_WCHAR CBC_ASCIIEncoder::encodeASCIIDigits(FX_WCHAR digit1, FX_WCHAR digit2, FX_INT32 &e)\r
75 {\r
76     if (CBC_HighLevelEncoder::isDigit(digit1) && CBC_HighLevelEncoder::isDigit(digit2)) {\r
77         FX_INT32 num = (digit1 - 48) * 10 + (digit2 - 48);\r
78         FX_WCHAR a = (FX_WCHAR) (num + 130);\r
79         return (FX_WCHAR) (num + 130);\r
80     }\r
81     e = BCExceptionIllegalArgumentNotGigits;\r
82     return 0;\r
83 }\r