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 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_OneDimWriter.h" | |
28 #include "include/BC_OneDimReader.h" | |
29 #include "include/BC_OnedEAN8Writer.h" | |
30 #include "include/BC_CommonBitMatrix.h" | |
31 CBC_OnedEAN8Writer::CBC_OnedEAN8Writer() | |
32 { | |
33 m_iDataLenth = 8; | |
34 m_codeWidth = 3 + | |
35 (7 * 4) + | |
36 5 + | |
37 (7 * 4) + | |
38 3; | |
39 } | |
40 CBC_OnedEAN8Writer::~CBC_OnedEAN8Writer() | |
41 { | |
42 } | |
43 void CBC_OnedEAN8Writer::SetDataLength(FX_INT32 length) | |
44 { | |
45 m_iDataLenth = 8; | |
46 } | |
47 FX_BOOL CBC_OnedEAN8Writer::SetTextLocation(BC_TEXT_LOC location) | |
48 { | |
49 if ( location == BC_TEXT_LOC_BELOWEMBED) { | |
50 m_locTextLoc = location; | |
51 return TRUE; | |
52 } | |
53 return FALSE; | |
54 } | |
55 FX_BOOL CBC_OnedEAN8Writer::CheckContentValidity(FX_WSTR contents) | |
56 { | |
57 for (FX_INT32 i = 0; i < contents.GetLength(); i++) { | |
58 if (contents.GetAt(i) >= '0' && contents.GetAt(i) <= '9') { | |
59 continue; | |
60 } else { | |
61 return FALSE; | |
62 } | |
63 } | |
64 return TRUE; | |
65 } | |
66 CFX_WideString CBC_OnedEAN8Writer::FilterContents(FX_WSTR contents) | |
67 { | |
68 CFX_WideString filtercontents; | |
69 FX_WCHAR ch; | |
70 for (FX_INT32 i = 0; i < contents.GetLength(); i++) { | |
71 ch = contents.GetAt(i); | |
72 if(ch > 175) { | |
73 i++; | |
74 continue; | |
75 } | |
76 if (ch >= '0' && ch <= '9') { | |
77 filtercontents += ch; | |
78 } | |
79 } | |
80 return filtercontents; | |
81 } | |
82 FX_INT32 CBC_OnedEAN8Writer::CalcChecksum(const CFX_ByteString &contents) | |
83 { | |
84 FX_INT32 odd = 0; | |
85 FX_INT32 even = 0; | |
86 FX_INT32 j = 1; | |
87 for(FX_INT32 i = contents.GetLength() - 1; i >= 0; i--) { | |
88 if(j % 2) { | |
89 odd += FXSYS_atoi(contents.Mid(i, 1)); | |
90 } else { | |
91 even += FXSYS_atoi(contents.Mid(i, 1)); | |
92 } | |
93 j++; | |
94 } | |
95 FX_INT32 checksum = (odd * 3 + even) % 10; | |
96 checksum = (10 - checksum) % 10; | |
97 return (checksum); | |
98 } | |
99 FX_BYTE *CBC_OnedEAN8Writer::Encode(const CFX_ByteString &contents, BCFORMAT for
mat, FX_INT32 &outWidth, FX_INT32 &outHeight , FX_INT32 &e) | |
100 { | |
101 FX_BYTE *ret = Encode(contents, format, outWidth, outHeight, 0, e); | |
102 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
103 return ret; | |
104 } | |
105 FX_BYTE *CBC_OnedEAN8Writer::Encode(const CFX_ByteString &contents, BCFORMAT for
mat, | |
106 FX_INT32 &outWidth, FX_INT32 &outHeight, FX_
INT32 hints , FX_INT32 &e) | |
107 { | |
108 if (format != BCFORMAT_EAN_8) { | |
109 e = BCExceptionOnlyEncodeEAN_8; | |
110 return NULL; | |
111 } | |
112 FX_BYTE *ret = CBC_OneDimWriter::Encode(contents, format, outWidth, outHeigh
t, hints, e); | |
113 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
114 return ret; | |
115 } | |
116 FX_BYTE *CBC_OnedEAN8Writer::Encode(const CFX_ByteString &contents, FX_INT32 &ou
tLength , FX_INT32 &e) | |
117 { | |
118 if (contents.GetLength() != 8) { | |
119 e = BCExceptionDigitLengthMustBe8; | |
120 return NULL; | |
121 } | |
122 outLength = m_codeWidth; | |
123 FX_BYTE *result = FX_Alloc(FX_BYTE, m_codeWidth); | |
124 FX_INT32 pos = 0; | |
125 pos += AppendPattern(result, pos, CBC_OneDimReader::START_END_PATTERN, 3, 1,
e); | |
126 if (e != BCExceptionNO) { | |
127 FX_Free (result); | |
128 return NULL; | |
129 } | |
130 FX_INT32 i = 0; | |
131 for (i = 0; i <= 3; i++) { | |
132 FX_INT32 digit = FXSYS_atoi(contents.Mid(i, 1)); | |
133 pos += AppendPattern(result, pos, CBC_OneDimReader::L_PATTERNS[digit], 4
, 0, e); | |
134 if (e != BCExceptionNO) { | |
135 FX_Free (result); | |
136 return NULL; | |
137 } | |
138 } | |
139 pos += AppendPattern(result, pos, CBC_OneDimReader::MIDDLE_PATTERN, 5, 0, e)
; | |
140 if (e != BCExceptionNO) { | |
141 FX_Free (result); | |
142 return NULL; | |
143 } | |
144 for (i = 4; i <= 7; i++) { | |
145 FX_INT32 digit = FXSYS_atoi(contents.Mid(i, 1)); | |
146 pos += AppendPattern(result, pos, CBC_OneDimReader::L_PATTERNS[digit], 4
, 1, e); | |
147 if (e != BCExceptionNO) { | |
148 FX_Free (result); | |
149 return NULL; | |
150 } | |
151 } | |
152 pos += AppendPattern(result, pos, CBC_OneDimReader::START_END_PATTERN, 3, 1,
e); | |
153 if (e != BCExceptionNO) { | |
154 FX_Free (result); | |
155 return NULL; | |
156 } | |
157 return result; | |
158 } | |
159 void CBC_OnedEAN8Writer::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) | |
160 { | |
161 if (device == NULL && pOutBitmap == NULL) { | |
162 e = BCExceptionIllegalArgument; | |
163 return; | |
164 } | |
165 FX_INT32 leftPosition = 3 * multiple; | |
166 CFX_ByteString str = FX_UTF8Encode(contents); | |
167 FX_INT32 iLength = str.GetLength(); | |
168 FXTEXT_CHARPOS* pCharPos = FX_Alloc(FXTEXT_CHARPOS, iLength); | |
169 if (!pCharPos) { | |
170 return; | |
171 } | |
172 FXSYS_memset32(pCharPos, 0, sizeof(FXTEXT_CHARPOS) * iLength); | |
173 CFX_ByteString tempStr = str.Mid(0, 4); | |
174 FX_INT32 iLen = tempStr.GetLength(); | |
175 FX_INT32 strWidth = 7 * multiple * 4; | |
176 FX_FLOAT blank = 0.0; | |
177 CFX_FxgeDevice geBitmap; | |
178 if (pOutBitmap != NULL) { | |
179 geBitmap.Attach(pOutBitmap); | |
180 } | |
181 FX_FLOAT charsWidth = 0; | |
182 FX_INT32 iFontSize = (FX_INT32)fabs(m_fFontSize); | |
183 FX_INT32 iTextHeight = iFontSize + 1; | |
184 if (pOutBitmap == NULL) { | |
185 CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); | |
186 CFX_FloatRect rect((FX_FLOAT)leftPosition, (FX_FLOAT)(m_Height - iTextHe
ight), (FX_FLOAT)(leftPosition + strWidth - 0.5), (FX_FLOAT)m_Height); | |
187 matr.Concat(*matrix); | |
188 matr.TransformRect(rect); | |
189 FX_RECT re = rect.GetOutterRect(); | |
190 device->FillRect(&re, m_backgroundColor); | |
191 CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); | |
192 CFX_FloatRect rect1((FX_FLOAT)(leftPosition + 33 * multiple), (FX_FLOAT)
(m_Height - iTextHeight), (FX_FLOAT)(leftPosition + 33 * multiple + strWidth - 0
.5), (FX_FLOAT)m_Height); | |
193 matr1.Concat(*matrix); | |
194 matr1.TransformRect(rect1); | |
195 re = rect1.GetOutterRect(); | |
196 device->FillRect(&re, m_backgroundColor); | |
197 } | |
198 if (pOutBitmap == NULL) { | |
199 strWidth = (FX_INT32)(strWidth * m_outputHScale); | |
200 } | |
201 CalcTextInfo(tempStr, pCharPos, m_pFont, (FX_FLOAT)strWidth, iFontSize, blan
k); | |
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 delete ge.GetBitmap(); | |
206 ge.Create(strWidth, iTextHeight, FXDIB_Argb); | |
207 ge.GetBitmap()->Clear(m_backgroundColor); | |
208 ge.DrawNormalText(iLen, | |
209 pCharPos, | |
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 affine_matrix1.Concat(*matrix); | |
219 FX_BOOL ret = device->DrawNormalText(iLen, | |
220 pCharPos, | |
221 m_pFont, | |
222 CFX_GEModule::Get()->GetFontCache()
, | |
223 (FX_FLOAT)iFontSize, | |
224 (CFX_AffineMatrix *) &affine_matrix
1, | |
225 m_fontColor, FXTEXT_CLEARTYPE); | |
226 } | |
227 tempStr = str.Mid(4, 4); | |
228 iLen = tempStr.GetLength(); | |
229 charsWidth = 0.0f; | |
230 CalcTextInfo(tempStr, pCharPos + 4, m_pFont, (FX_FLOAT)strWidth, iFontSize,
blank); | |
231 if (pOutBitmap != NULL) { | |
232 delete ge.GetBitmap(); | |
233 ge.Create(strWidth, iTextHeight, FXDIB_Argb); | |
234 ge.GetBitmap()->Clear(m_backgroundColor); | |
235 ge.DrawNormalText(iLen, | |
236 pCharPos + 4, | |
237 m_pFont, | |
238 CFX_GEModule::Get()->GetFontCache(), | |
239 (FX_FLOAT)iFontSize , | |
240 (CFX_AffineMatrix *) &affine_matrix, | |
241 m_fontColor, FXTEXT_CLEARTYPE); | |
242 geBitmap.SetDIBits(ge.GetBitmap(), leftPosition + 33 * multiple, m_Heigh
t - iTextHeight); | |
243 } else { | |
244 CFX_AffineMatrix affine_matrix1(1.0, 0.0, 0.0, -1.0, (FX_FLOAT)(leftPosi
tion + 33 * multiple) * m_outputHScale, (FX_FLOAT)(m_Height - iTextHeight + iFon
tSize)); | |
245 if (matrix != NULL) { | |
246 affine_matrix1.Concat(*matrix); | |
247 } | |
248 FX_BOOL ret = device->DrawNormalText(iLen, | |
249 pCharPos + 4, | |
250 m_pFont, | |
251 CFX_GEModule::Get()->GetFontCache()
, | |
252 (FX_FLOAT)iFontSize , | |
253 (CFX_AffineMatrix *) &affine_matrix
1, | |
254 m_fontColor, FXTEXT_CLEARTYPE); | |
255 } | |
256 FX_Free(pCharPos); | |
257 } | |
258 void CBC_OnedEAN8Writer::RenderResult(FX_WSTR contents, FX_BYTE* code, FX_INT32
codeLength, FX_BOOL isDevice, FX_INT32 &e) | |
259 { | |
260 CBC_OneDimWriter::RenderResult(contents, code, codeLength, isDevice, e); | |
261 } | |
OLD | NEW |