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 "fxbarcode/oned/BC_OneDimWriter.h"
24 
25 #include <algorithm>
26 #include <memory>
27 #include <vector>
28 
29 #include "build/build_config.h"
30 #include "core/fxge/cfx_defaultrenderdevice.h"
31 #include "core/fxge/cfx_font.h"
32 #include "core/fxge/cfx_graphstatedata.h"
33 #include "core/fxge/cfx_pathdata.h"
34 #include "core/fxge/cfx_renderdevice.h"
35 #include "core/fxge/cfx_unicodeencodingex.h"
36 #include "core/fxge/text_char_pos.h"
37 #include "fxbarcode/BC_Writer.h"
38 
39 CBC_OneDimWriter::CBC_OneDimWriter() = default;
40 
41 CBC_OneDimWriter::~CBC_OneDimWriter() = default;
42 
SetPrintChecksum(bool checksum)43 void CBC_OneDimWriter::SetPrintChecksum(bool checksum) {
44   m_bPrintChecksum = checksum;
45 }
46 
SetDataLength(int32_t length)47 void CBC_OneDimWriter::SetDataLength(int32_t length) {
48   m_iDataLenth = length;
49 }
50 
SetCalcChecksum(bool state)51 void CBC_OneDimWriter::SetCalcChecksum(bool state) {
52   m_bCalcChecksum = state;
53 }
54 
SetFont(CFX_Font * cFont)55 bool CBC_OneDimWriter::SetFont(CFX_Font* cFont) {
56   if (!cFont)
57     return false;
58 
59   m_pFont = cFont;
60   return true;
61 }
62 
SetFontSize(float size)63 void CBC_OneDimWriter::SetFontSize(float size) {
64   m_fFontSize = size;
65 }
66 
SetFontStyle(int32_t style)67 void CBC_OneDimWriter::SetFontStyle(int32_t style) {
68   m_iFontStyle = style;
69 }
70 
SetFontColor(FX_ARGB color)71 void CBC_OneDimWriter::SetFontColor(FX_ARGB color) {
72   m_fontColor = color;
73 }
74 
EncodeWithHint(const ByteString & contents,BCFORMAT format,int32_t & outWidth,int32_t & outHeight,int32_t hints)75 uint8_t* CBC_OneDimWriter::EncodeWithHint(const ByteString& contents,
76                                           BCFORMAT format,
77                                           int32_t& outWidth,
78                                           int32_t& outHeight,
79                                           int32_t hints) {
80   outHeight = 1;
81   return EncodeImpl(contents, outWidth);
82 }
83 
Encode(const ByteString & contents,BCFORMAT format,int32_t & outWidth,int32_t & outHeight)84 uint8_t* CBC_OneDimWriter::Encode(const ByteString& contents,
85                                   BCFORMAT format,
86                                   int32_t& outWidth,
87                                   int32_t& outHeight) {
88   return EncodeWithHint(contents, format, outWidth, outHeight, 0);
89 }
90 
AppendPattern(uint8_t * target,int32_t pos,const int8_t * pattern,int32_t patternLength,bool startColor)91 int32_t CBC_OneDimWriter::AppendPattern(uint8_t* target,
92                                         int32_t pos,
93                                         const int8_t* pattern,
94                                         int32_t patternLength,
95                                         bool startColor) {
96   bool color = startColor;
97   int32_t numAdded = 0;
98   for (int32_t i = 0; i < patternLength; i++) {
99     for (int32_t j = 0; j < pattern[i]; j++)
100       target[pos++] = color ? 1 : 0;
101     numAdded += pattern[i];
102     color = !color;
103   }
104   return numAdded;
105 }
106 
CalcTextInfo(const ByteString & text,TextCharPos * charPos,CFX_Font * cFont,float geWidth,int32_t fontSize,float & charsLen)107 void CBC_OneDimWriter::CalcTextInfo(const ByteString& text,
108                                     TextCharPos* charPos,
109                                     CFX_Font* cFont,
110                                     float geWidth,
111                                     int32_t fontSize,
112                                     float& charsLen) {
113   std::unique_ptr<CFX_UnicodeEncodingEx> encoding =
114       FX_CreateFontEncodingEx(cFont);
115 
116   const size_t length = text.GetLength();
117   std::vector<uint32_t> charcodes(length);
118   float charWidth = 0;
119   for (size_t i = 0; i < length; ++i) {
120     charcodes[i] = encoding->CharCodeFromUnicode(text[i]);
121     int32_t glyph_code = encoding->GlyphFromCharCode(charcodes[i]);
122     uint32_t glyph_value = cFont->GetGlyphWidth(glyph_code);
123     float temp = glyph_value * fontSize / 1000.0;
124     charWidth += temp;
125   }
126   charsLen = charWidth;
127   float leftPositon = (float)(geWidth - charsLen) / 2.0f;
128   if (leftPositon < 0 && geWidth == 0) {
129     leftPositon = 0;
130   }
131   float penX = 0.0;
132   float penY = (float)abs(cFont->GetDescent()) * (float)fontSize / 1000.0f;
133   float left = leftPositon;
134   float top = 0.0;
135   charPos[0].m_Origin = CFX_PointF(penX + left, penY + top);
136   charPos[0].m_GlyphIndex = encoding->GlyphFromCharCode(charcodes[0]);
137   charPos[0].m_FontCharWidth = cFont->GetGlyphWidth(charPos[0].m_GlyphIndex);
138 #if defined(OS_MACOSX)
139   charPos[0].m_ExtGID = charPos[0].m_GlyphIndex;
140 #endif
141   penX += (float)(charPos[0].m_FontCharWidth) * (float)fontSize / 1000.0f;
142   for (size_t i = 1; i < length; i++) {
143     charPos[i].m_Origin = CFX_PointF(penX + left, penY + top);
144     charPos[i].m_GlyphIndex = encoding->GlyphFromCharCode(charcodes[i]);
145     charPos[i].m_FontCharWidth = cFont->GetGlyphWidth(charPos[i].m_GlyphIndex);
146 #if defined(OS_MACOSX)
147     charPos[i].m_ExtGID = charPos[i].m_GlyphIndex;
148 #endif
149     penX += (float)(charPos[i].m_FontCharWidth) * (float)fontSize / 1000.0f;
150   }
151 }
152 
ShowDeviceChars(CFX_RenderDevice * device,const CFX_Matrix * matrix,const ByteString str,float geWidth,TextCharPos * pCharPos,float locX,float locY,int32_t barWidth)153 void CBC_OneDimWriter::ShowDeviceChars(CFX_RenderDevice* device,
154                                        const CFX_Matrix* matrix,
155                                        const ByteString str,
156                                        float geWidth,
157                                        TextCharPos* pCharPos,
158                                        float locX,
159                                        float locY,
160                                        int32_t barWidth) {
161   int32_t iFontSize = (int32_t)fabs(m_fFontSize);
162   int32_t iTextHeight = iFontSize + 1;
163   CFX_FloatRect rect((float)locX, (float)locY, (float)(locX + geWidth),
164                      (float)(locY + iTextHeight));
165   if (geWidth != m_Width) {
166     rect.right -= 1;
167   }
168   FX_RECT re = matrix->TransformRect(rect).GetOuterRect();
169   device->FillRect(re, kBackgroundColor);
170   CFX_Matrix affine_matrix(1.0, 0.0, 0.0, -1.0, (float)locX,
171                            (float)(locY + iFontSize));
172   if (matrix) {
173     affine_matrix.Concat(*matrix);
174   }
175   device->DrawNormalText(str.GetLength(), pCharPos, m_pFont.Get(),
176                          static_cast<float>(iFontSize), affine_matrix,
177                          m_fontColor, FXTEXT_CLEARTYPE);
178 }
179 
ShowChars(WideStringView contents,CFX_RenderDevice * device,const CFX_Matrix * matrix,int32_t barWidth,int32_t multiple)180 bool CBC_OneDimWriter::ShowChars(WideStringView contents,
181                                  CFX_RenderDevice* device,
182                                  const CFX_Matrix* matrix,
183                                  int32_t barWidth,
184                                  int32_t multiple) {
185   if (!device || !m_pFont)
186     return false;
187 
188   ByteString str = FX_UTF8Encode(contents);
189   std::vector<TextCharPos> charpos(str.GetLength());
190   float charsLen = 0;
191   float geWidth = 0;
192   if (m_locTextLoc == BC_TEXT_LOC_ABOVEEMBED ||
193       m_locTextLoc == BC_TEXT_LOC_BELOWEMBED) {
194     geWidth = 0;
195   } else if (m_locTextLoc == BC_TEXT_LOC_ABOVE ||
196              m_locTextLoc == BC_TEXT_LOC_BELOW) {
197     geWidth = (float)barWidth;
198   }
199   int32_t iFontSize = (int32_t)fabs(m_fFontSize);
200   int32_t iTextHeight = iFontSize + 1;
201   CalcTextInfo(str, charpos.data(), m_pFont.Get(), geWidth, iFontSize,
202                charsLen);
203   if (charsLen < 1)
204     return true;
205 
206   int32_t locX = 0;
207   int32_t locY = 0;
208   switch (m_locTextLoc) {
209     case BC_TEXT_LOC_ABOVEEMBED:
210       locX = (int32_t)(barWidth - charsLen) / 2;
211       locY = 0;
212       geWidth = charsLen;
213       break;
214     case BC_TEXT_LOC_ABOVE:
215       locX = 0;
216       locY = 0;
217       geWidth = (float)barWidth;
218       break;
219     case BC_TEXT_LOC_BELOWEMBED:
220       locX = (int32_t)(barWidth - charsLen) / 2;
221       locY = m_Height - iTextHeight;
222       geWidth = charsLen;
223       break;
224     case BC_TEXT_LOC_BELOW:
225     default:
226       locX = 0;
227       locY = m_Height - iTextHeight;
228       geWidth = (float)barWidth;
229       break;
230   }
231   ShowDeviceChars(device, matrix, str, geWidth, charpos.data(), (float)locX,
232                   (float)locY, barWidth);
233   return true;
234 }
235 
RenderDeviceResult(CFX_RenderDevice * device,const CFX_Matrix * matrix,WideStringView contents)236 bool CBC_OneDimWriter::RenderDeviceResult(CFX_RenderDevice* device,
237                                           const CFX_Matrix* matrix,
238                                           WideStringView contents) {
239   if (m_output.empty())
240     return false;
241 
242   CFX_GraphStateData stateData;
243   CFX_PathData path;
244   path.AppendRect(0, 0, static_cast<float>(m_Width),
245                   static_cast<float>(m_Height));
246   device->DrawPath(&path, matrix, &stateData, kBackgroundColor,
247                    kBackgroundColor, FXFILL_ALTERNATE);
248   CFX_Matrix scaledMatrix(m_outputHScale, 0.0, 0.0,
249                           static_cast<float>(m_Height), 0.0, 0.0);
250   scaledMatrix.Concat(*matrix);
251   for (const auto& rect : m_output) {
252     CFX_GraphStateData data;
253     device->DrawPath(&rect, &scaledMatrix, &data, kBarColor, 0, FXFILL_WINDING);
254   }
255 
256   return m_locTextLoc == BC_TEXT_LOC_NONE || !contents.Contains(' ') ||
257          ShowChars(contents, device, matrix, m_barWidth, m_multiple);
258 }
259 
RenderResult(WideStringView contents,uint8_t * code,int32_t codeLength)260 bool CBC_OneDimWriter::RenderResult(WideStringView contents,
261                                     uint8_t* code,
262                                     int32_t codeLength) {
263   if (codeLength < 1)
264     return false;
265 
266   m_ModuleHeight = std::max(m_ModuleHeight, 20);
267   const int32_t codeOldLength = codeLength;
268   const int32_t leftPadding = m_bLeftPadding ? 7 : 0;
269   const int32_t rightPadding = m_bRightPadding ? 7 : 0;
270   codeLength += leftPadding;
271   codeLength += rightPadding;
272   m_outputHScale =
273       m_Width > 0 ? static_cast<float>(m_Width) / static_cast<float>(codeLength)
274                   : 1.0;
275   m_multiple = 1;
276   const int32_t outputWidth = codeLength;
277   m_barWidth = m_Width;
278 
279   m_output.clear();
280   m_output.reserve(codeOldLength * m_multiple);
281   for (int32_t inputX = 0, outputX = leftPadding * m_multiple;
282        inputX < codeOldLength; ++inputX, outputX += m_multiple) {
283     if (code[inputX] != 1)
284       continue;
285 
286     if (outputX >= outputWidth)
287       return true;
288 
289     if (outputX + m_multiple > outputWidth && outputWidth - outputX > 0) {
290       RenderVerticalBars(outputX, outputWidth - outputX);
291       return true;
292     }
293 
294     RenderVerticalBars(outputX, m_multiple);
295   }
296   return true;
297 }
298 
RenderVerticalBars(int32_t outputX,int32_t width)299 void CBC_OneDimWriter::RenderVerticalBars(int32_t outputX, int32_t width) {
300   for (int i = 0; i < width; ++i) {
301     float x = outputX + i;
302     m_output.emplace_back();
303     m_output.back().AppendRect(x, 0.0f, x + 1, 1.0f);
304   }
305 }
306