Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / xfa / src / fxbarcode / src / BC_QRCoderMode.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_QRCoderVersion.h"\r
9 #include "include/BC_QRCoderMode.h"\r
10 CBC_QRCoderMode* CBC_QRCoderMode::sBYTE = NULL;\r
11 CBC_QRCoderMode* CBC_QRCoderMode::sNUMERIC = NULL;\r
12 CBC_QRCoderMode* CBC_QRCoderMode::sALPHANUMERIC = NULL;\r
13 CBC_QRCoderMode* CBC_QRCoderMode::sKANJI = NULL;\r
14 CBC_QRCoderMode* CBC_QRCoderMode::sECI = NULL;\r
15 CBC_QRCoderMode* CBC_QRCoderMode::sGBK = NULL;\r
16 CBC_QRCoderMode* CBC_QRCoderMode::sTERMINATOR = NULL;\r
17 CBC_QRCoderMode* CBC_QRCoderMode::sFNC1_FIRST_POSITION = NULL;\r
18 CBC_QRCoderMode* CBC_QRCoderMode::sFNC1_SECOND_POSITION = NULL;\r
19 CBC_QRCoderMode* CBC_QRCoderMode::sSTRUCTURED_APPEND = NULL;\r
20 CBC_QRCoderMode::CBC_QRCoderMode(FX_INT32 *characterCountBitsForVersions,\r
21                                  FX_INT32 x1, FX_INT32 x2, FX_INT32 x3,\r
22                                  FX_INT32 bits, CFX_ByteString name)\r
23 {\r
24     m_characterCountBitsForVersions = characterCountBitsForVersions;\r
25     if (m_characterCountBitsForVersions != NULL) {\r
26         m_characterCountBitsForVersions[0] = x1;\r
27         m_characterCountBitsForVersions[1] = x2;\r
28         m_characterCountBitsForVersions[2] = x3;\r
29     }\r
30     m_name += name;\r
31     m_bits = bits;\r
32 }\r
33 CBC_QRCoderMode::~CBC_QRCoderMode()\r
34 {\r
35     if(m_characterCountBitsForVersions != NULL) {\r
36         FX_Free(m_characterCountBitsForVersions);\r
37     }\r
38 }\r
39 void CBC_QRCoderMode::Initialize()\r
40 {\r
41     sBYTE = FX_NEW CBC_QRCoderMode(FX_Alloc(FX_INT32, 3), 8, 16, 16, 0x4, "BYTE");\r
42     sALPHANUMERIC = FX_NEW CBC_QRCoderMode(FX_Alloc(FX_INT32, 3), 9, 11, 13, 0x2, "ALPHANUMERIC");\r
43     sECI = FX_NEW CBC_QRCoderMode(NULL, 0, 0, 0, 0x7, "ECI");\r
44     sKANJI = FX_NEW CBC_QRCoderMode(FX_Alloc(FX_INT32, 3), 8, 10, 12, 0x8, "KANJI");\r
45     sNUMERIC = FX_NEW CBC_QRCoderMode(FX_Alloc(FX_INT32, 3), 10, 12, 14, 0x1, "NUMERIC");\r
46     sGBK = FX_NEW CBC_QRCoderMode(FX_Alloc(FX_INT32, 3), 8, 10, 12, 0x0D, "GBK");\r
47     sTERMINATOR = FX_NEW CBC_QRCoderMode(FX_Alloc(FX_INT32, 3), 0, 0, 0, 0x00, "TERMINATOR");\r
48     sFNC1_FIRST_POSITION = FX_NEW CBC_QRCoderMode(NULL, 0, 0, 0, 0x05, "FNC1_FIRST_POSITION");\r
49     sFNC1_SECOND_POSITION = FX_NEW CBC_QRCoderMode(NULL, 0, 0, 0, 0x09, "FNC1_SECOND_POSITION");\r
50     sSTRUCTURED_APPEND = FX_NEW CBC_QRCoderMode(FX_Alloc(FX_INT32, 3), 0, 0, 0, 0x03, "STRUCTURED_APPEND");\r
51 }\r
52 void CBC_QRCoderMode::Finalize()\r
53 {\r
54     delete sBYTE;\r
55     delete sALPHANUMERIC;\r
56     delete sECI;\r
57     delete sKANJI;\r
58     delete sNUMERIC;\r
59     delete sGBK;\r
60     delete sTERMINATOR;\r
61     delete sFNC1_FIRST_POSITION;\r
62     delete sFNC1_SECOND_POSITION;\r
63     delete sSTRUCTURED_APPEND;\r
64 }\r
65 CBC_QRCoderMode* CBC_QRCoderMode::ForBits(FX_INT32 bits, FX_INT32 &e)\r
66 {\r
67     switch (bits) {\r
68         case 0x0:\r
69             return sTERMINATOR;\r
70         case 0x1:\r
71             return sNUMERIC;\r
72         case 0x2:\r
73             return sALPHANUMERIC;\r
74         case 0x3:\r
75             return sSTRUCTURED_APPEND;\r
76         case 0x4:\r
77             return sBYTE;\r
78         case 0x5:\r
79             return sFNC1_FIRST_POSITION;\r
80         case 0x7:\r
81             return sECI;\r
82         case 0x8:\r
83             return sKANJI;\r
84         case 0x9:\r
85             return sFNC1_SECOND_POSITION;\r
86         case 0x0D:\r
87             return sGBK;\r
88         default: {\r
89                 e = BCExceptionUnsupportedMode;\r
90                 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);\r
91             }\r
92     }\r
93     return NULL;\r
94 }\r
95 FX_INT32 CBC_QRCoderMode::GetBits()\r
96 {\r
97     return m_bits;\r
98 }\r
99 CFX_ByteString CBC_QRCoderMode::GetName()\r
100 {\r
101     return m_name;\r
102 }\r
103 FX_INT32 CBC_QRCoderMode::GetCharacterCountBits(CBC_QRCoderVersion* version, FX_INT32 &e)\r
104 {\r
105     if(m_characterCountBitsForVersions == NULL) {\r
106         e = BCExceptionCharacterNotThisMode;\r
107         BC_EXCEPTION_CHECK_ReturnValue(e, 0);\r
108     }\r
109     FX_INT32 number = version->GetVersionNumber();\r
110     FX_INT32 offset;\r
111     if(number <= 9) {\r
112         offset = 0;\r
113     } else if(number <= 26) {\r
114         offset = 1;\r
115     } else {\r
116         offset = 2;\r
117     }\r
118     return m_characterCountBitsForVersions[offset];\r
119 }\r
120 void CBC_QRCoderMode::Destroy()\r
121 {\r
122     if(sBYTE) {\r
123         delete CBC_QRCoderMode::sBYTE;\r
124         sBYTE = NULL;\r
125     }\r
126     if(sNUMERIC) {\r
127         delete CBC_QRCoderMode::sNUMERIC;\r
128         sNUMERIC = NULL;\r
129     }\r
130     if(sALPHANUMERIC) {\r
131         delete CBC_QRCoderMode::sALPHANUMERIC;\r
132         sALPHANUMERIC = NULL;\r
133     }\r
134     if(sKANJI) {\r
135         delete CBC_QRCoderMode::sKANJI;\r
136         sKANJI = NULL;\r
137     }\r
138     if(sECI) {\r
139         delete CBC_QRCoderMode::sECI;\r
140         sECI = NULL;\r
141     }\r
142     if(sGBK) {\r
143         delete CBC_QRCoderMode::sGBK;\r
144         sGBK = NULL;\r
145     }\r
146     if(sTERMINATOR) {\r
147         delete CBC_QRCoderMode::sTERMINATOR;\r
148         sTERMINATOR = NULL;\r
149     }\r
150     if(sFNC1_FIRST_POSITION) {\r
151         delete CBC_QRCoderMode::sFNC1_FIRST_POSITION;\r
152         sFNC1_FIRST_POSITION = NULL;\r
153     }\r
154     if(sFNC1_SECOND_POSITION) {\r
155         delete CBC_QRCoderMode::sFNC1_SECOND_POSITION;\r
156         sFNC1_SECOND_POSITION = NULL;\r
157     }\r
158     if(sSTRUCTURED_APPEND) {\r
159         delete CBC_QRCoderMode::sSTRUCTURED_APPEND;\r
160         sSTRUCTURED_APPEND = NULL;\r
161     }\r
162 }\r