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