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 2010 ZXing authors | |
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/BC_Writer.h" | |
24 #include "xfa/src/fxbarcode/oned/BC_OneDimWriter.h" | |
25 #include "xfa/src/fxbarcode/oned/BC_OnedEAN13Writer.h" | |
26 #include "xfa/src/fxbarcode/oned/BC_OnedUPCAWriter.h" | |
27 | |
28 CBC_OnedUPCAWriter::CBC_OnedUPCAWriter() { | |
29 m_subWriter = NULL; | |
30 m_bLeftPadding = TRUE; | |
31 m_bRightPadding = TRUE; | |
32 } | |
33 void CBC_OnedUPCAWriter::Init() { | |
34 m_subWriter = new CBC_OnedEAN13Writer; | |
35 } | |
36 CBC_OnedUPCAWriter::~CBC_OnedUPCAWriter() { | |
37 delete m_subWriter; | |
38 } | |
39 FX_BOOL CBC_OnedUPCAWriter::CheckContentValidity( | |
40 const CFX_WideStringC& contents) { | |
41 int32_t i = 0; | |
42 for (i = 0; i < contents.GetLength(); i++) { | |
43 if (contents.GetAt(i) >= '0' && contents.GetAt(i) <= '9') { | |
44 continue; | |
45 } else { | |
46 return FALSE; | |
47 } | |
48 } | |
49 return TRUE; | |
50 } | |
51 CFX_WideString CBC_OnedUPCAWriter::FilterContents( | |
52 const CFX_WideStringC& contents) { | |
53 CFX_WideString filtercontents; | |
54 FX_WCHAR ch; | |
55 for (int32_t i = 0; i < contents.GetLength(); i++) { | |
56 ch = contents.GetAt(i); | |
57 if (ch > 175) { | |
58 i++; | |
59 continue; | |
60 } | |
61 if (ch >= '0' && ch <= '9') { | |
62 filtercontents += ch; | |
63 } | |
64 } | |
65 return filtercontents; | |
66 } | |
67 int32_t CBC_OnedUPCAWriter::CalcChecksum(const CFX_ByteString& contents) { | |
68 int32_t odd = 0; | |
69 int32_t even = 0; | |
70 int32_t j = 1; | |
71 for (int32_t i = contents.GetLength() - 1; i >= 0; i--) { | |
72 if (j % 2) { | |
73 odd += FXSYS_atoi(contents.Mid(i, 1)); | |
74 } else { | |
75 even += FXSYS_atoi(contents.Mid(i, 1)); | |
76 } | |
77 j++; | |
78 } | |
79 int32_t checksum = (odd * 3 + even) % 10; | |
80 checksum = (10 - checksum) % 10; | |
81 return (checksum); | |
82 } | |
83 uint8_t* CBC_OnedUPCAWriter::Encode(const CFX_ByteString& contents, | |
84 BCFORMAT format, | |
85 int32_t& outWidth, | |
86 int32_t& outHeight, | |
87 int32_t& e) { | |
88 uint8_t* ret = Encode(contents, format, outWidth, outHeight, 0, e); | |
89 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
90 return ret; | |
91 } | |
92 uint8_t* CBC_OnedUPCAWriter::Encode(const CFX_ByteString& contents, | |
93 BCFORMAT format, | |
94 int32_t& outWidth, | |
95 int32_t& outHeight, | |
96 int32_t hints, | |
97 int32_t& e) { | |
98 if (format != BCFORMAT_UPC_A) { | |
99 e = BCExceptionOnlyEncodeUPC_A; | |
100 return NULL; | |
101 } | |
102 CFX_ByteString toEAN13String = '0' + contents; | |
103 m_iDataLenth = 13; | |
104 uint8_t* ret = m_subWriter->Encode(toEAN13String, BCFORMAT_EAN_13, outWidth, | |
105 outHeight, hints, e); | |
106 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
107 return ret; | |
108 } | |
109 void CBC_OnedUPCAWriter::ShowChars(const CFX_WideStringC& contents, | |
110 CFX_DIBitmap* pOutBitmap, | |
111 CFX_RenderDevice* device, | |
112 const CFX_Matrix* matrix, | |
113 int32_t barWidth, | |
114 int32_t multiple, | |
115 int32_t& e) { | |
116 if (device == NULL && pOutBitmap == NULL) { | |
117 e = BCExceptionIllegalArgument; | |
118 return; | |
119 } | |
120 int32_t leftPadding = 7 * multiple; | |
121 int32_t leftPosition = 10 * multiple + leftPadding; | |
122 CFX_ByteString str = FX_UTF8Encode(contents); | |
123 int32_t iLen = str.GetLength(); | |
124 FXTEXT_CHARPOS* pCharPos = FX_Alloc(FXTEXT_CHARPOS, iLen); | |
125 FXSYS_memset(pCharPos, 0, sizeof(FXTEXT_CHARPOS) * iLen); | |
126 CFX_ByteString tempStr = str.Mid(1, 5); | |
127 FX_FLOAT strWidth = (FX_FLOAT)35 * multiple; | |
128 FX_FLOAT blank = 0.0; | |
129 CFX_FxgeDevice geBitmap; | |
130 if (pOutBitmap) { | |
131 geBitmap.Attach(pOutBitmap); | |
132 } | |
133 iLen = tempStr.GetLength(); | |
134 int32_t iFontSize = (int32_t)fabs(m_fFontSize); | |
135 int32_t iTextHeight = iFontSize + 1; | |
136 if (pOutBitmap == NULL) { | |
137 CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); | |
138 CFX_FloatRect rect( | |
139 (FX_FLOAT)leftPosition, (FX_FLOAT)(m_Height - iTextHeight), | |
140 (FX_FLOAT)(leftPosition + strWidth - 0.5), (FX_FLOAT)m_Height); | |
141 matr.Concat(*matrix); | |
142 matr.TransformRect(rect); | |
143 FX_RECT re = rect.GetOutterRect(); | |
144 device->FillRect(&re, m_backgroundColor); | |
145 CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); | |
146 CFX_FloatRect rect1( | |
147 (FX_FLOAT)(leftPosition + 40 * multiple), | |
148 (FX_FLOAT)(m_Height - iTextHeight), | |
149 (FX_FLOAT)((leftPosition + 40 * multiple) + strWidth - 0.5), | |
150 (FX_FLOAT)m_Height); | |
151 matr1.Concat(*matrix); | |
152 matr1.TransformRect(rect1); | |
153 re = rect1.GetOutterRect(); | |
154 device->FillRect(&re, m_backgroundColor); | |
155 FX_FLOAT strWidth1 = (FX_FLOAT)multiple * 7; | |
156 CFX_Matrix matr2(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); | |
157 CFX_FloatRect rect2(0.0, (FX_FLOAT)(m_Height - iTextHeight), | |
158 (FX_FLOAT)strWidth1 - 1, (FX_FLOAT)m_Height); | |
159 matr2.Concat(*matrix); | |
160 matr2.TransformRect(rect2); | |
161 re = rect2.GetOutterRect(); | |
162 device->FillRect(&re, m_backgroundColor); | |
163 CFX_Matrix matr3(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); | |
164 CFX_FloatRect rect3( | |
165 (FX_FLOAT)(leftPosition + 85 * multiple), | |
166 (FX_FLOAT)(m_Height - iTextHeight), | |
167 (FX_FLOAT)((leftPosition + 85 * multiple) + strWidth1 - 0.5), | |
168 (FX_FLOAT)m_Height); | |
169 matr3.Concat(*matrix); | |
170 matr3.TransformRect(rect3); | |
171 re = rect3.GetOutterRect(); | |
172 device->FillRect(&re, m_backgroundColor); | |
173 } | |
174 if (pOutBitmap == NULL) { | |
175 strWidth = strWidth * m_outputHScale; | |
176 } | |
177 CalcTextInfo(tempStr, pCharPos + 1, m_pFont, strWidth, iFontSize, blank); | |
178 CFX_Matrix affine_matrix(1.0, 0.0, 0.0, -1.0, 0.0, (FX_FLOAT)iFontSize); | |
179 CFX_FxgeDevice ge; | |
180 if (pOutBitmap) { | |
181 ge.Create((int)strWidth, iTextHeight, FXDIB_Argb); | |
182 ge.GetBitmap()->Clear(m_backgroundColor); | |
183 ge.DrawNormalText(iLen, pCharPos + 1, m_pFont, | |
184 CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize, | |
185 (CFX_Matrix*)&affine_matrix, m_fontColor, | |
186 FXTEXT_CLEARTYPE); | |
187 geBitmap.SetDIBits(ge.GetBitmap(), leftPosition, m_Height - iTextHeight); | |
188 } else { | |
189 CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0, | |
190 (FX_FLOAT)leftPosition * m_outputHScale, | |
191 (FX_FLOAT)(m_Height - iTextHeight + iFontSize)); | |
192 if (matrix) { | |
193 affine_matrix1.Concat(*matrix); | |
194 } | |
195 device->DrawNormalText(iLen, pCharPos + 1, m_pFont, | |
196 CFX_GEModule::Get()->GetFontCache(), | |
197 (FX_FLOAT)iFontSize, (CFX_Matrix*)&affine_matrix1, | |
198 m_fontColor, FXTEXT_CLEARTYPE); | |
199 } | |
200 tempStr = str.Mid(6, 5); | |
201 iLen = tempStr.GetLength(); | |
202 CalcTextInfo(tempStr, pCharPos + 6, m_pFont, strWidth, iFontSize, blank); | |
203 if (pOutBitmap) { | |
204 FX_RECT rect2(0, 0, (int)strWidth, iTextHeight); | |
205 ge.FillRect(&rect2, m_backgroundColor); | |
206 ge.DrawNormalText(iLen, pCharPos + 6, m_pFont, | |
207 CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize, | |
208 (CFX_Matrix*)&affine_matrix, m_fontColor, | |
209 FXTEXT_CLEARTYPE); | |
210 geBitmap.SetDIBits(ge.GetBitmap(), leftPosition + 40 * multiple, | |
211 m_Height - iTextHeight); | |
212 } else { | |
213 CFX_Matrix affine_matrix1( | |
214 1.0, 0.0, 0.0, -1.0, | |
215 (FX_FLOAT)(leftPosition + 40 * multiple) * m_outputHScale, | |
216 (FX_FLOAT)(m_Height - iTextHeight + iFontSize)); | |
217 if (matrix) { | |
218 affine_matrix1.Concat(*matrix); | |
219 } | |
220 device->DrawNormalText(iLen, pCharPos + 6, m_pFont, | |
221 CFX_GEModule::Get()->GetFontCache(), | |
222 (FX_FLOAT)iFontSize, (CFX_Matrix*)&affine_matrix1, | |
223 m_fontColor, FXTEXT_CLEARTYPE); | |
224 } | |
225 tempStr = str.Mid(0, 1); | |
226 iLen = tempStr.GetLength(); | |
227 strWidth = (FX_FLOAT)multiple * 7; | |
228 if (pOutBitmap == NULL) { | |
229 strWidth = strWidth * m_outputHScale; | |
230 } | |
231 CalcTextInfo(tempStr, pCharPos, m_pFont, strWidth, iFontSize, blank); | |
232 if (pOutBitmap) { | |
233 delete ge.GetBitmap(); | |
234 ge.Create((int)strWidth, iTextHeight, FXDIB_Argb); | |
235 ge.GetBitmap()->Clear(m_backgroundColor); | |
236 ge.DrawNormalText(iLen, pCharPos, m_pFont, | |
237 CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize, | |
238 (CFX_Matrix*)&affine_matrix, m_fontColor, | |
239 FXTEXT_CLEARTYPE); | |
240 geBitmap.SetDIBits(ge.GetBitmap(), 0, m_Height - iTextHeight); | |
241 } else { | |
242 CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0, 0, | |
243 (FX_FLOAT)(m_Height - iTextHeight + iFontSize)); | |
244 if (matrix) { | |
245 affine_matrix1.Concat(*matrix); | |
246 } | |
247 device->DrawNormalText(iLen, pCharPos, m_pFont, | |
248 CFX_GEModule::Get()->GetFontCache(), | |
249 (FX_FLOAT)iFontSize, (CFX_Matrix*)&affine_matrix1, | |
250 m_fontColor, FXTEXT_CLEARTYPE); | |
251 } | |
252 tempStr = str.Mid(11, 1); | |
253 iLen = tempStr.GetLength(); | |
254 CalcTextInfo(tempStr, pCharPos + 11, m_pFont, strWidth, iFontSize, blank); | |
255 if (pOutBitmap) { | |
256 delete ge.GetBitmap(); | |
257 ge.Create((int)strWidth, iTextHeight, FXDIB_Argb); | |
258 ge.GetBitmap()->Clear(m_backgroundColor); | |
259 ge.DrawNormalText(iLen, pCharPos + 11, m_pFont, | |
260 CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize, | |
261 (CFX_Matrix*)&affine_matrix, m_fontColor, | |
262 FXTEXT_CLEARTYPE); | |
263 geBitmap.SetDIBits(ge.GetBitmap(), leftPosition + 85 * multiple, | |
264 m_Height - iTextHeight); | |
265 } else { | |
266 CFX_Matrix affine_matrix1( | |
267 1.0, 0.0, 0.0, -1.0, | |
268 (FX_FLOAT)(leftPosition + 85 * multiple) * m_outputHScale, | |
269 (FX_FLOAT)(m_Height - iTextHeight + iFontSize)); | |
270 if (matrix) { | |
271 affine_matrix1.Concat(*matrix); | |
272 } | |
273 device->DrawNormalText(iLen, pCharPos + 11, m_pFont, | |
274 CFX_GEModule::Get()->GetFontCache(), | |
275 (FX_FLOAT)iFontSize, (CFX_Matrix*)&affine_matrix1, | |
276 m_fontColor, FXTEXT_CLEARTYPE); | |
277 } | |
278 FX_Free(pCharPos); | |
279 } | |
280 void CBC_OnedUPCAWriter::RenderResult(const CFX_WideStringC& contents, | |
281 uint8_t* code, | |
282 int32_t codeLength, | |
283 FX_BOOL isDevice, | |
284 int32_t& e) { | |
285 CBC_OneDimWriter::RenderResult(contents, code, codeLength, isDevice, e); | |
286 } | |
OLD | NEW |