Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(793)

Side by Side Diff: xfa/src/fxbarcode/src/BC_OnedEAN13Writer.cpp

Issue 842043002: Organize barcode codes into modules. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@xfa
Patch Set: rebase Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 2009 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_Reader.h"
26 #include "include/BC_OneDReader.h"
27 #include "include/BC_OneDimReader.h"
28 #include "include/BC_OneDimWriter.h"
29 #include "include/BC_OnedEAN13Reader.h"
30 #include "include/BC_OnedEAN13Writer.h"
31 CBC_OnedEAN13Writer::CBC_OnedEAN13Writer()
32 {
33 m_bLeftPadding = TRUE;
34 m_codeWidth = 3 +
35 (7 * 6) +
36 5 +
37 (7 * 6) +
38 3;
39 }
40 CBC_OnedEAN13Writer::~CBC_OnedEAN13Writer()
41 {
42 }
43 FX_BOOL CBC_OnedEAN13Writer::CheckContentValidity(FX_WSTR contents)
44 {
45 for (FX_INT32 i = 0; i < contents.GetLength(); i++) {
46 if (contents.GetAt(i) >= '0' && contents.GetAt(i) <= '9') {
47 continue;
48 } else {
49 return FALSE;
50 }
51 }
52 return TRUE;
53 }
54 CFX_WideString CBC_OnedEAN13Writer::FilterContents(FX_WSTR contents)
55 {
56 CFX_WideString filtercontents;
57 FX_WCHAR ch;
58 for (FX_INT32 i = 0; i < contents.GetLength(); i++) {
59 ch = contents.GetAt(i);
60 if(ch > 175) {
61 i++;
62 continue;
63 }
64 if (ch >= '0' && ch <= '9') {
65 filtercontents += ch;
66 }
67 }
68 return filtercontents;
69 }
70 FX_INT32 CBC_OnedEAN13Writer::CalcChecksum(const CFX_ByteString &contents)
71 {
72 FX_INT32 odd = 0;
73 FX_INT32 even = 0;
74 FX_INT32 j = 1;
75 for(FX_INT32 i = contents.GetLength() - 1; i >= 0; i--) {
76 if(j % 2) {
77 odd += FXSYS_atoi(contents.Mid(i, 1));
78 } else {
79 even += FXSYS_atoi(contents.Mid(i, 1));
80 }
81 j++;
82 }
83 FX_INT32 checksum = (odd * 3 + even) % 10;
84 checksum = (10 - checksum) % 10;
85 return (checksum);
86 }
87 FX_BYTE *CBC_OnedEAN13Writer::Encode(const CFX_ByteString &contents, BCFORMAT fo rmat, FX_INT32 &outWidth, FX_INT32 &outHeight, FX_INT32 &e)
88 {
89 FX_BYTE *ret = Encode(contents, format, outWidth, outHeight, 0, e);
90 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
91 return ret;
92 }
93 FX_BYTE *CBC_OnedEAN13Writer::Encode(const CFX_ByteString &contents, BCFORMAT fo rmat, FX_INT32 &outWidth, FX_INT32 &outHeight, FX_INT32 hints, FX_INT32 &e)
94 {
95 if(format != BCFORMAT_EAN_13) {
96 e = BCExceptionOnlyEncodeEAN_13;
97 }
98 FX_BYTE *ret = CBC_OneDimWriter::Encode(contents, format, outWidth, outHeigh t, hints, e);
99 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
100 return ret;
101 }
102 FX_BYTE *CBC_OnedEAN13Writer::Encode(const CFX_ByteString &contents, FX_INT32 &o utLength, FX_INT32 &e)
103 {
104 if (contents.GetLength() != 13) {
105 e = BCExceptionDigitLengthShould13;
106 return NULL;
107 }
108 m_iDataLenth = 13;
109 FX_INT32 firstDigit = FXSYS_atoi(contents.Mid(0, 1));
110 FX_INT32 parities = CBC_OnedEAN13Reader::FIRST_DIGIT_ENCODINGS[firstDigit];
111 outLength = m_codeWidth;
112 FX_BYTE *result = FX_Alloc(FX_BYTE, m_codeWidth);
113 FX_INT32 pos = 0;
114 pos += AppendPattern(result, pos, CBC_OneDimReader::START_END_PATTERN, 3, 1, e);
115 if (e != BCExceptionNO) {
116 FX_Free (result);
117 return NULL;
118 }
119 FX_INT32 i = 0;
120 for ( i = 1; i <= 6; i++) {
121 FX_INT32 digit = FXSYS_atoi(contents.Mid(i, 1));
122 if ((parities >> (6 - i) & 1) == 1) {
123 digit += 10;
124 }
125 pos += AppendPattern(result, pos, CBC_OneDimReader::L_AND_G_PATTERNS[dig it], 4, 0, e);
126 if (e != BCExceptionNO) {
127 FX_Free (result);
128 return NULL;
129 }
130 }
131 pos += AppendPattern(result, pos, CBC_OneDimReader::MIDDLE_PATTERN, 5, 0, e) ;
132 if (e != BCExceptionNO) {
133 FX_Free (result);
134 return NULL;
135 }
136 for (i = 7; i <= 12; i++) {
137 FX_INT32 digit = FXSYS_atoi(contents.Mid(i, 1));
138 pos += AppendPattern(result, pos, CBC_OneDimReader::L_PATTERNS[digit], 4 , 1, e);
139 if (e != BCExceptionNO) {
140 FX_Free (result);
141 return NULL;
142 }
143 }
144 pos += AppendPattern(result, pos, CBC_OneDimReader::START_END_PATTERN, 3, 1, e);
145 if (e != BCExceptionNO) {
146 FX_Free (result);
147 return NULL;
148 }
149 return result;
150 }
151 void CBC_OnedEAN13Writer::ShowChars(FX_WSTR contents, CFX_DIBitmap *pOutBitmap, CFX_RenderDevice* device, const CFX_Matrix* matrix, FX_INT32 barWidth, FX_INT32 multiple, FX_INT32 &e)
152 {
153 if (device == NULL && pOutBitmap == NULL) {
154 e = BCExceptionIllegalArgument;
155 return;
156 }
157 FX_INT32 leftPadding = 7 * multiple;
158 FX_INT32 leftPosition = 3 * multiple + leftPadding;
159 CFX_ByteString str = FX_UTF8Encode(contents);
160 FX_INT32 iLen = str.GetLength();
161 FXTEXT_CHARPOS* pCharPos = FX_Alloc(FXTEXT_CHARPOS, iLen);
162 if (!pCharPos) {
163 return;
164 }
165 FXSYS_memset32(pCharPos, 0, sizeof(FXTEXT_CHARPOS) * iLen);
166 CFX_FxgeDevice geBitmap;
167 if (pOutBitmap != NULL) {
168 geBitmap.Attach(pOutBitmap);
169 }
170 FX_INT32 iFontSize = (FX_INT32)fabs(m_fFontSize);
171 FX_INT32 iTextHeight = iFontSize + 1;
172 CFX_ByteString tempStr = str.Mid(1, 6);
173 FX_INT32 strWidth = multiple * 42;
174 if (pOutBitmap == NULL) {
175 CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
176 CFX_FloatRect rect((FX_FLOAT)leftPosition, (FX_FLOAT)(m_Height - iTextHe ight), (FX_FLOAT)(leftPosition + strWidth - 0.5), (FX_FLOAT)m_Height);
177 matr.Concat(*matrix);
178 matr.TransformRect(rect);
179 FX_RECT re = rect.GetOutterRect();
180 device->FillRect(&re, m_backgroundColor);
181 CFX_FloatRect rect1((FX_FLOAT)(leftPosition + 47 * multiple), (FX_FLOAT) (m_Height - iTextHeight), (FX_FLOAT)(leftPosition + 47 * multiple + strWidth - 0 .5), (FX_FLOAT)m_Height);
182 CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
183 matr1.Concat(*matrix);
184 matr1.TransformRect(rect1);
185 re = rect1.GetOutterRect();
186 device->FillRect(&re, m_backgroundColor);
187 FX_INT32 strWidth1 = multiple * 7;
188 CFX_Matrix matr2(m_outputHScale, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
189 CFX_FloatRect rect2(0.0f, (FX_FLOAT)(m_Height - iTextHeight), (FX_FLOAT) strWidth1 - 0.5f, (FX_FLOAT)m_Height);
190 matr2.Concat(*matrix);
191 matr2.TransformRect(rect2);
192 re = rect2.GetOutterRect();
193 device->FillRect(&re, m_backgroundColor);
194 }
195 FX_FLOAT blank = 0.0;
196 FX_FLOAT charsWidth = 0;
197 iLen = tempStr.GetLength();
198 if (pOutBitmap == NULL) {
199 strWidth = (FX_INT32)(strWidth * m_outputHScale);
200 }
201 CalcTextInfo(tempStr, pCharPos + 1, m_pFont, (FX_FLOAT)strWidth, iFontSize, blank);
202 CFX_AffineMatrix affine_matrix(1.0, 0.0, 0.0, -1.0, 0.0, (FX_FLOAT)iFontSize );
203 CFX_FxgeDevice ge;
204 if (pOutBitmap != NULL) {
205 ge.Create(strWidth, iTextHeight, FXDIB_Argb);
206 FX_RECT rect(0, 0, strWidth, iTextHeight);
207 ge.FillRect(&rect, m_backgroundColor);
208 ge.DrawNormalText(iLen,
209 pCharPos + 1,
210 m_pFont,
211 CFX_GEModule::Get()->GetFontCache(),
212 (FX_FLOAT)iFontSize ,
213 (CFX_AffineMatrix *) &affine_matrix,
214 m_fontColor, FXTEXT_CLEARTYPE);
215 geBitmap.SetDIBits(ge.GetBitmap(), leftPosition, m_Height - iTextHeight) ;
216 } else {
217 CFX_AffineMatrix affine_matrix1(1.0, 0.0, 0.0, -1.0, (FX_FLOAT)leftPosit ion * m_outputHScale, (FX_FLOAT)(m_Height - iTextHeight) + iFontSize);
218 if (matrix != NULL) {
219 affine_matrix1.Concat(*matrix);
220 }
221 device->DrawNormalText(iLen,
222 pCharPos + 1,
223 m_pFont,
224 CFX_GEModule::Get()->GetFontCache(),
225 (FX_FLOAT)iFontSize ,
226 (CFX_AffineMatrix *) &affine_matrix1,
227 m_fontColor, FXTEXT_CLEARTYPE);
228 }
229 tempStr = str.Mid(7, 6);
230 iLen = tempStr.GetLength();
231 charsWidth = 0.0f;
232 CalcTextInfo(tempStr, pCharPos + 7, m_pFont, (FX_FLOAT)strWidth, iFontSize, blank);
233 if(pOutBitmap != NULL) {
234 FX_RECT rect1(0, 0, strWidth, iTextHeight);
235 ge.FillRect(&rect1, m_backgroundColor);
236 ge.DrawNormalText(iLen,
237 pCharPos + 7,
238 m_pFont,
239 CFX_GEModule::Get()->GetFontCache(),
240 (FX_FLOAT)iFontSize ,
241 (CFX_AffineMatrix *) &affine_matrix,
242 m_fontColor, FXTEXT_CLEARTYPE);
243 geBitmap.SetDIBits(ge.GetBitmap(), leftPosition + 47 * multiple, m_Heigh t - iTextHeight);
244 } else {
245 CFX_AffineMatrix affine_matrix1(1.0, 0.0, 0.0, -1.0, (FX_FLOAT)(leftPosi tion + 47 * multiple) * m_outputHScale, (FX_FLOAT)(m_Height - iTextHeight + iFon tSize));
246 if (matrix != NULL) {
247 affine_matrix1.Concat(*matrix);
248 }
249 device->DrawNormalText(iLen,
250 pCharPos + 7,
251 m_pFont,
252 CFX_GEModule::Get()->GetFontCache(),
253 (FX_FLOAT)iFontSize ,
254 (CFX_AffineMatrix *) &affine_matrix1,
255 m_fontColor, FXTEXT_CLEARTYPE);
256 }
257 tempStr = str.Mid(0, 1);
258 iLen = tempStr.GetLength();
259 strWidth = multiple * 7;
260 if (pOutBitmap == NULL) {
261 strWidth = (FX_INT32)(strWidth * m_outputHScale);
262 }
263 CalcTextInfo(tempStr, pCharPos, m_pFont, (FX_FLOAT)strWidth, iFontSize, blan k);
264 if(pOutBitmap != NULL) {
265 delete ge.GetBitmap();
266 ge.Create(strWidth, iTextHeight, FXDIB_Argb);
267 ge.GetBitmap()->Clear(m_backgroundColor);
268 ge.DrawNormalText(iLen,
269 pCharPos,
270 m_pFont,
271 CFX_GEModule::Get()->GetFontCache(),
272 (FX_FLOAT)iFontSize ,
273 (CFX_AffineMatrix *) &affine_matrix,
274 m_fontColor, FXTEXT_CLEARTYPE);
275 geBitmap.SetDIBits(ge.GetBitmap(), 0, m_Height - iTextHeight);
276 } else {
277 CFX_AffineMatrix affine_matrix1(1.0, 0.0, 0.0, -1.0, 0.0, (FX_FLOAT)(m_H eight - iTextHeight + iFontSize));
278 if (matrix != NULL) {
279 affine_matrix1.Concat(*matrix);
280 }
281 device->DrawNormalText(iLen,
282 pCharPos,
283 m_pFont,
284 CFX_GEModule::Get()->GetFontCache(),
285 (FX_FLOAT)iFontSize ,
286 (CFX_AffineMatrix *) &affine_matrix1,
287 m_fontColor, FXTEXT_CLEARTYPE);
288 }
289 FX_Free(pCharPos);
290 }
291 void CBC_OnedEAN13Writer::RenderResult(FX_WSTR contents, FX_BYTE* code, FX_INT32 codeLength, FX_BOOL isDevice, FX_INT32 &e)
292 {
293 CBC_OneDimWriter::RenderResult(contents, code, codeLength, isDevice, e);
294 }
OLDNEW
« no previous file with comments | « xfa/src/fxbarcode/src/BC_OnedEAN13Reader.cpp ('k') | xfa/src/fxbarcode/src/BC_OnedEAN8Reader.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698