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 2011 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_CommonBitMatrix.h" | |
26 #include "include/BC_OneDimWriter.h" | |
27 CBC_OneDimWriter::CBC_OneDimWriter() | |
28 { | |
29 m_locTextLoc = BC_TEXT_LOC_BELOWEMBED; | |
30 m_bPrintChecksum = TRUE; | |
31 m_iDataLenth = 0; | |
32 m_bCalcChecksum = FALSE; | |
33 m_pFont = NULL; | |
34 m_fFontSize = 10;
; | |
35 m_iFontStyle = 0; | |
36 m_fontColor = 0xff000000; | |
37 m_iContentLen = 0; | |
38 m_bLeftPadding = FALSE; | |
39 m_bRightPadding = FALSE; | |
40 m_output = NULL; | |
41 } | |
42 CBC_OneDimWriter::~CBC_OneDimWriter() | |
43 { | |
44 if (m_output != NULL) { | |
45 delete m_output; | |
46 m_output = NULL; | |
47 } | |
48 } | |
49 void CBC_OneDimWriter::SetPrintChecksum(FX_BOOL checksum) | |
50 { | |
51 m_bPrintChecksum = checksum; | |
52 } | |
53 void CBC_OneDimWriter::SetDataLength(FX_INT32 length) | |
54 { | |
55 m_iDataLenth = length; | |
56 } | |
57 void CBC_OneDimWriter::SetCalcChecksum(FX_INT32 state) | |
58 { | |
59 m_bCalcChecksum = state; | |
60 } | |
61 FX_BOOL CBC_OneDimWriter::SetFont(CFX_Font * cFont) | |
62 { | |
63 if (cFont == NULL) { | |
64 return FALSE; | |
65 } | |
66 m_pFont = cFont; | |
67 return TRUE; | |
68 } | |
69 void CBC_OneDimWriter::SetFontSize(FX_FLOAT size) | |
70 { | |
71 m_fFontSize = size; | |
72 } | |
73 void CBC_OneDimWriter::SetFontStyle(FX_INT32 style) | |
74 { | |
75 m_iFontStyle = style; | |
76 } | |
77 void CBC_OneDimWriter::SetFontColor(FX_ARGB color) | |
78 { | |
79 m_fontColor = color; | |
80 } | |
81 FX_WCHAR CBC_OneDimWriter::Upper(FX_WCHAR ch) | |
82 { | |
83 if(ch >= 'a' && ch <= 'z') { | |
84 ch = ch - ('a' - 'A'); | |
85 } | |
86 return ch; | |
87 } | |
88 FX_BYTE* CBC_OneDimWriter::Encode(const CFX_ByteString &contents, BCFORMAT forma
t, | |
89 FX_INT32 &outWidth, FX_INT32 &outHeight, FX_IN
T32 hints, FX_INT32 &e) | |
90 { | |
91 FX_BYTE *ret = NULL; | |
92 outHeight = 1; | |
93 if (m_Width >= 20) { | |
94 ret = Encode(contents, outWidth, e); | |
95 } else { | |
96 ret = Encode(contents, outWidth, e); | |
97 } | |
98 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
99 return ret; | |
100 } | |
101 FX_BYTE *CBC_OneDimWriter::Encode(const CFX_ByteString &contents, BCFORMAT forma
t, | |
102 FX_INT32 &outWidth, FX_INT32 &outHeight, FX_IN
T32 &e) | |
103 { | |
104 FX_BYTE *ret = Encode(contents, format, outWidth, outHeight, 0, e); | |
105 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
106 return ret; | |
107 } | |
108 FX_INT32 CBC_OneDimWriter::AppendPattern(FX_BYTE* target, FX_INT32 pos, const FX
_INT32* pattern , FX_INT32 patternLength, FX_INT32 startColor, FX_INT32 &e) | |
109 { | |
110 if (startColor != 0 && startColor != 1) { | |
111 e = BCExceptionValueMustBeEither0or1; | |
112 return 0; | |
113 } | |
114 FX_BYTE color = (FX_BYTE) startColor; | |
115 FX_INT32 numAdded = 0; | |
116 for (FX_INT32 i = 0; i < patternLength; i++) { | |
117 for (FX_INT32 j = 0; j < pattern[i]; j++) { | |
118 target[pos] = color; | |
119 pos += 1; | |
120 numAdded += 1; | |
121 } | |
122 color ^= 1; | |
123 } | |
124 return numAdded; | |
125 } | |
126 void CBC_OneDimWriter::CalcTextInfo(const CFX_ByteString &text, FXTEXT_CHARPOS *
charPos, CFX_Font *cFont, FX_FLOAT geWidth, FX_INT32 fontSize, FX_FLOAT &charsLe
n) | |
127 { | |
128 #ifdef FXFM_ENCODING_NONE | |
129 IFX_FontEncodingEx* encoding = FX_CreateFontEncodingEx(cFont); | |
130 #else | |
131 IFX_FontEncoding * encoding = FXGE_CreateUnicodeEncoding(cFont); | |
132 #endif | |
133 FX_INT32 length = text.GetLength(); | |
134 FX_DWORD *pCharCode = FX_Alloc(FX_DWORD, text.GetLength()); | |
135 FX_FLOAT charWidth = 0; | |
136 for (FX_INT32 j = 0; j < text.GetLength(); j++) { | |
137 pCharCode[j] = encoding->CharCodeFromUnicode(text[j]); | |
138 FX_INT32 glyp_code = encoding->GlyphFromCharCode(pCharCode[j]); | |
139 FX_INT32 glyp_value = cFont->GetGlyphWidth(glyp_code); | |
140 FX_FLOAT temp = (FX_FLOAT)((glyp_value) * fontSize / 1000.0); | |
141 charWidth += temp; | |
142 } | |
143 charsLen = charWidth; | |
144 FX_FLOAT leftPositon = (FX_FLOAT)(geWidth - charsLen) / 2.0f; | |
145 if (leftPositon < 0 && geWidth == 0) { | |
146 leftPositon = 0; | |
147 } | |
148 FX_FLOAT penX = 0.0; | |
149 FX_FLOAT penY = (FX_FLOAT)FXSYS_abs(cFont->GetDescent()) * (FX_FLOAT)fontSiz
e / 1000.0f; | |
150 FX_FLOAT left = leftPositon; | |
151 FX_FLOAT top = 0.0; | |
152 charPos[0].m_OriginX = penX + left; | |
153 charPos[0].m_OriginY = penY + top; | |
154 charPos[0].m_GlyphIndex = encoding->GlyphFromCharCode(pCharCode[0]); | |
155 charPos[0].m_FontCharWidth = cFont->GetGlyphWidth(charPos[0].m_GlyphIndex); | |
156 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
157 charPos[0].m_ExtGID = charPos[0].m_GlyphIndex; | |
158 #endif | |
159 penX += (FX_FLOAT)(charPos[0].m_FontCharWidth) * (FX_FLOAT)fontSize / 1000.0
f; | |
160 for (FX_INT32 i = 1; i < length; i++) { | |
161 charPos[i].m_OriginX = penX + left; | |
162 charPos[i].m_OriginY = penY + top; | |
163 charPos[i].m_GlyphIndex = encoding->GlyphFromCharCode(pCharCode[i]); | |
164 charPos[i].m_FontCharWidth = cFont->GetGlyphWidth(charPos[i].m_GlyphInde
x); | |
165 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
166 charPos[i].m_ExtGID = charPos[i].m_GlyphIndex; | |
167 #endif | |
168 penX += (FX_FLOAT)(charPos[i].m_FontCharWidth) * (FX_FLOAT)fontSize / 10
00.0f; | |
169 } | |
170 FX_Free (pCharCode); | |
171 delete encoding; | |
172 encoding = NULL; | |
173 } | |
174 void CBC_OneDimWriter::ShowDeviceChars(CFX_RenderDevice *device, const CFX_Matri
x* matrix, const CFX_ByteString str, FX_FLOAT geWidth, FXTEXT_CHARPOS* pCharPos,
FX_FLOAT locX, FX_FLOAT locY, FX_INT32 barWidth) | |
175 { | |
176 FX_INT32 iFontSize = (FX_INT32)fabs(m_fFontSize); | |
177 FX_INT32 iTextHeight = iFontSize + 1; | |
178 CFX_FloatRect rect((FX_FLOAT)locX, (FX_FLOAT)locY, (FX_FLOAT)(locX + geWidth
), (FX_FLOAT)(locY + iTextHeight)); | |
179 if (geWidth != m_Width) { | |
180 rect.right -= 1; | |
181 } | |
182 matrix->TransformRect(rect); | |
183 FX_RECT re = rect.GetOutterRect(); | |
184 device->FillRect(&re, m_backgroundColor); | |
185 CFX_Matrix affine_matrix(1.0, 0.0, 0.0, -1.0, (FX_FLOAT)locX, (FX_FLOAT)(loc
Y + iFontSize)); | |
186 if (matrix != NULL) { | |
187 affine_matrix.Concat(*matrix); | |
188 } | |
189 FX_BOOL ret = device->DrawNormalText(str.GetLength(), | |
190 pCharPos, | |
191 m_pFont, | |
192 CFX_GEModule::Get()->GetFontCache(), | |
193 (FX_FLOAT)iFontSize, | |
194 (CFX_AffineMatrix *) &affine_matrix, | |
195 m_fontColor, FXTEXT_CLEARTYPE); | |
196 } | |
197 void CBC_OneDimWriter::ShowBitmapChars(CFX_DIBitmap *pOutBitmap, const CFX_ByteS
tring str, FX_FLOAT geWidth, FXTEXT_CHARPOS* pCharPos, FX_FLOAT locX, FX_FLOAT l
ocY, FX_INT32 barWidth) | |
198 { | |
199 FX_INT32 iFontSize = (FX_INT32)fabs(m_fFontSize); | |
200 FX_INT32 iTextHeight = iFontSize + 1; | |
201 CFX_FxgeDevice ge; | |
202 ge.Create((int)geWidth, iTextHeight , m_colorSpace); | |
203 FX_RECT geRect(0, 0, (int)geWidth, iTextHeight); | |
204 ge.FillRect(&geRect, m_backgroundColor); | |
205 CFX_AffineMatrix affine_matrix(1.0, 0.0, 0.0, -1.0, 0.0, (FX_FLOAT)iFontSize
); | |
206 FX_BOOL ret = ge.DrawNormalText(str.GetLength(), | |
207 pCharPos, | |
208 m_pFont, | |
209 CFX_GEModule::Get()->GetFontCache(), | |
210 (FX_FLOAT)iFontSize, | |
211 (CFX_AffineMatrix *) &affine_matrix, | |
212 m_fontColor, FXTEXT_CLEARTYPE); | |
213 CFX_FxgeDevice geBitmap; | |
214 geBitmap.Attach(pOutBitmap); | |
215 geBitmap.SetDIBits(ge.GetBitmap(), (int)locX, (int)locY); | |
216 } | |
217 void CBC_OneDimWriter::ShowChars(FX_WSTR contents, CFX_DIBitmap *pOutBitmap, CFX
_RenderDevice *device, const CFX_Matrix* matrix, FX_INT32 barWidth, FX_INT32 mul
tiple, FX_INT32 &e) | |
218 { | |
219 if (device == NULL && pOutBitmap == NULL) { | |
220 e = BCExceptionIllegalArgument; | |
221 return; | |
222 } | |
223 if (m_pFont == NULL) { | |
224 e = BCExceptionNullPointer; | |
225 return; | |
226 } | |
227 CFX_ByteString str = FX_UTF8Encode(contents); | |
228 FX_INT32 iLen = str.GetLength(); | |
229 FXTEXT_CHARPOS* pCharPos = FX_Alloc(FXTEXT_CHARPOS, iLen); | |
230 if (!pCharPos) { | |
231 return; | |
232 } | |
233 FXSYS_memset32(pCharPos, 0, sizeof(FXTEXT_CHARPOS) * iLen); | |
234 FX_FLOAT charsLen = 0; | |
235 FX_FLOAT geWidth = 0; | |
236 if ( m_locTextLoc == BC_TEXT_LOC_ABOVEEMBED || | |
237 m_locTextLoc == BC_TEXT_LOC_BELOWEMBED ) { | |
238 geWidth = 0; | |
239 } else if ( m_locTextLoc == BC_TEXT_LOC_ABOVE || | |
240 m_locTextLoc == BC_TEXT_LOC_BELOW ) { | |
241 geWidth = (FX_FLOAT)barWidth; | |
242 } | |
243 FX_INT32 iFontSize = (FX_INT32)fabs(m_fFontSize); | |
244 FX_INT32 iTextHeight = iFontSize + 1; | |
245 CalcTextInfo(str, pCharPos, m_pFont, geWidth, iFontSize, charsLen); | |
246 if (charsLen < 1) { | |
247 return; | |
248 } | |
249 FX_INT32 locX = 0; | |
250 FX_INT32 locY = 0; | |
251 switch (m_locTextLoc) { | |
252 case BC_TEXT_LOC_ABOVEEMBED: | |
253 locX = (FX_INT32)(barWidth - charsLen) / 2; | |
254 locY = 0; | |
255 geWidth = charsLen; | |
256 break; | |
257 case BC_TEXT_LOC_ABOVE: | |
258 locX = 0; | |
259 locY = 0; | |
260 geWidth = (FX_FLOAT)barWidth; | |
261 break; | |
262 case BC_TEXT_LOC_BELOWEMBED: | |
263 locX = (FX_INT32)(barWidth - charsLen) / 2; | |
264 locY = m_Height - iTextHeight; | |
265 geWidth = charsLen; | |
266 break; | |
267 case BC_TEXT_LOC_BELOW: | |
268 default: | |
269 locX = 0; | |
270 locY = m_Height - iTextHeight; | |
271 geWidth = (FX_FLOAT)barWidth; | |
272 break; | |
273 } | |
274 if (device != NULL) { | |
275 ShowDeviceChars(device, matrix, str, geWidth, pCharPos, (FX_FLOAT)locX,
(FX_FLOAT)locY, barWidth); | |
276 } else { | |
277 ShowBitmapChars(pOutBitmap, str, geWidth, pCharPos, (FX_FLOAT)locX, (FX_
FLOAT)locY, barWidth); | |
278 } | |
279 FX_Free(pCharPos); | |
280 } | |
281 void CBC_OneDimWriter::RenderBitmapResult(CFX_DIBitmap *&pOutBitmap, FX_WSTR con
tents, FX_INT32 &e) | |
282 { | |
283 if (m_output == NULL) { | |
284 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
285 } | |
286 pOutBitmap = CreateDIBitmap(m_output->GetWidth(), m_output->GetHeight()); | |
287 pOutBitmap->Clear(m_backgroundColor); | |
288 if (!pOutBitmap) { | |
289 e = BCExceptionFailToCreateBitmap; | |
290 return; | |
291 } | |
292 for (FX_INT32 x = 0; x < m_output->GetWidth(); x++) { | |
293 for (FX_INT32 y = 0; y < m_output->GetHeight(); y++) { | |
294 if (m_output->Get(x, y)) { | |
295 pOutBitmap->SetPixel(x, y, m_barColor); | |
296 } | |
297 } | |
298 } | |
299 FX_INT32 i = 0; | |
300 for (; i < contents.GetLength(); i++) | |
301 if (contents.GetAt(i) != ' ') { | |
302 break; | |
303 } | |
304 if (m_locTextLoc != BC_TEXT_LOC_NONE && i < contents.GetLength()) { | |
305 ShowChars(contents, pOutBitmap, NULL, NULL, m_barWidth, m_multiple, e); | |
306 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
307 } | |
308 CFX_DIBitmap * pStretchBitmap = pOutBitmap->StretchTo(m_Width, m_Height); | |
309 if (pOutBitmap) { | |
310 delete pOutBitmap; | |
311 } | |
312 pOutBitmap = pStretchBitmap; | |
313 } | |
314 void CBC_OneDimWriter::RenderDeviceResult(CFX_RenderDevice* device, const CFX_Ma
trix* matrix, FX_WSTR contents, FX_INT32 &e) | |
315 { | |
316 if (m_output == NULL) { | |
317 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
318 } | |
319 CFX_GraphStateData stateData; | |
320 CFX_PathData path; | |
321 path.AppendRect(0, 0, (FX_FLOAT)m_Width, (FX_FLOAT)m_Height); | |
322 device->DrawPath(&path, matrix, &stateData, m_backgroundColor, m_backgroundC
olor, FXFILL_ALTERNATE); | |
323 CFX_Matrix matri(m_outputHScale, 0.0, 0.0, (FX_FLOAT)m_Height, 0.0, 0.0); | |
324 matri.Concat(*matrix); | |
325 for (FX_INT32 x = 0; x < m_output->GetWidth(); x++) { | |
326 for (FX_INT32 y = 0; y < m_output->GetHeight(); y++) { | |
327 CFX_PathData rect; | |
328 rect.AppendRect((FX_FLOAT)x, (FX_FLOAT)y, (FX_FLOAT)(x + 1), (FX_FLO
AT)(y + 1)); | |
329 CFX_GraphStateData stateData; | |
330 if (m_output->Get(x, y)) { | |
331 device->DrawPath(&rect, &matri, &stateData, m_barColor, 0, FXFIL
L_WINDING); | |
332 } | |
333 } | |
334 } | |
335 FX_INT32 i = 0; | |
336 for (; i < contents.GetLength(); i++) | |
337 if (contents.GetAt(i) != ' ') { | |
338 break; | |
339 } | |
340 if (m_locTextLoc != BC_TEXT_LOC_NONE && i < contents.GetLength()) { | |
341 ShowChars(contents, NULL, device, matrix, m_barWidth, m_multiple, e); | |
342 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
343 } | |
344 } | |
345 void CBC_OneDimWriter::RenderResult(FX_WSTR contents, FX_BYTE* code, FX_INT32 co
deLength, FX_BOOL isDevice, FX_INT32 &e) | |
346 { | |
347 if (codeLength < 1) { | |
348 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
349 } | |
350 if (m_ModuleHeight < 20.0) { | |
351 m_ModuleHeight = 20; | |
352 } | |
353 FX_INT32 codeOldLength = codeLength; | |
354 FX_INT32 leftPadding = 0; | |
355 FX_INT32 rightPadding = 0; | |
356 if (m_bLeftPadding) { | |
357 leftPadding = 7; | |
358 } | |
359 if (m_bRightPadding) { | |
360 rightPadding = 7; | |
361 } | |
362 codeLength += leftPadding; | |
363 codeLength += rightPadding; | |
364 m_outputHScale = 1.0; | |
365 if (m_Width > 0) { | |
366 m_outputHScale = (FX_FLOAT)m_Width / (FX_FLOAT)codeLength; | |
367 } | |
368 if (!isDevice) { | |
369 m_outputHScale = FX_MAX(m_outputHScale, m_ModuleWidth); | |
370 } | |
371 FX_FLOAT dataLengthScale = 1.0; | |
372 if (m_iDataLenth > 0 && contents.GetLength() != 0) { | |
373 dataLengthScale = FX_FLOAT(contents.GetLength()) / FX_FLOAT(m_iDataLenth
); | |
374 } | |
375 if (m_iDataLenth > 0 && contents.GetLength() == 0) { | |
376 dataLengthScale = FX_FLOAT(1) / FX_FLOAT(m_iDataLenth); | |
377 } | |
378 m_multiple = 1; | |
379 if (!isDevice) { | |
380 m_multiple = (FX_INT32)ceil(m_outputHScale * dataLengthScale); | |
381 } | |
382 FX_INT32 outputHeight = 1; | |
383 if (!isDevice) { | |
384 if (m_Height == 0) { | |
385 outputHeight = FX_MAX(20, m_ModuleHeight); | |
386 } else { | |
387 outputHeight = m_Height; | |
388 } | |
389 } | |
390 FX_INT32 outputWidth = codeLength; | |
391 if (!isDevice) { | |
392 outputWidth = (FX_INT32)(codeLength * m_multiple / dataLengthScale); | |
393 } | |
394 m_barWidth = m_Width; | |
395 if (!isDevice) { | |
396 m_barWidth = codeLength * m_multiple; | |
397 } | |
398 m_output = FX_NEW CBC_CommonBitMatrix; | |
399 m_output->Init(outputWidth, outputHeight); | |
400 FX_INT32 outputX = leftPadding * m_multiple; | |
401 for (FX_INT32 inputX = 0; inputX < codeOldLength; inputX++) { | |
402 if (code[inputX] == 1) { | |
403 if (outputX >= outputWidth ) { | |
404 break; | |
405 } | |
406 if (outputX + m_multiple > outputWidth && outputWidth - outputX > 0)
{ | |
407 m_output->SetRegion(outputX, 0, outputWidth - outputX , outputHe
ight, e); | |
408 break; | |
409 } | |
410 m_output->SetRegion(outputX, 0, m_multiple, outputHeight, e); | |
411 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
412 } | |
413 outputX += m_multiple; | |
414 } | |
415 } | |
OLD | NEW |