• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The PDFium Authors
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 
7 #include "fxbarcode/BC_TwoDimWriter.h"
8 
9 #include <algorithm>
10 
11 #include "core/fxcrt/check.h"
12 #include "core/fxcrt/fx_safe_types.h"
13 #include "core/fxge/cfx_fillrenderoptions.h"
14 #include "core/fxge/cfx_graphstatedata.h"
15 #include "core/fxge/cfx_path.h"
16 #include "core/fxge/cfx_renderdevice.h"
17 #include "fxbarcode/BC_Writer.h"
18 #include "fxbarcode/common/BC_CommonBitMatrix.h"
19 
CBC_TwoDimWriter(bool bFixedSize)20 CBC_TwoDimWriter::CBC_TwoDimWriter(bool bFixedSize)
21     : m_bFixedSize(bFixedSize) {}
22 
23 CBC_TwoDimWriter::~CBC_TwoDimWriter() = default;
24 
RenderResult(pdfium::span<const uint8_t> code,int32_t codeWidth,int32_t codeHeight)25 bool CBC_TwoDimWriter::RenderResult(pdfium::span<const uint8_t> code,
26                                     int32_t codeWidth,
27                                     int32_t codeHeight) {
28   if (code.empty())
29     return false;
30 
31   m_inputWidth = codeWidth;
32   m_inputHeight = codeHeight;
33   int32_t tempWidth = m_inputWidth + 2;
34   int32_t tempHeight = m_inputHeight + 2;
35   const float module_size =
36       std::clamp<float>(std::min(m_ModuleWidth, m_ModuleHeight), 1.0f, 8.0f);
37   FX_SAFE_INT32 scaledWidth = tempWidth;
38   FX_SAFE_INT32 scaledHeight = tempHeight;
39   scaledWidth *= module_size;
40   scaledHeight *= module_size;
41   m_outputWidth = scaledWidth.ValueOrDie();
42   m_outputHeight = scaledHeight.ValueOrDie();
43 
44   if (m_bFixedSize) {
45     if (m_Width < m_outputWidth || m_Height < m_outputHeight) {
46       return false;
47     }
48   } else {
49     if (m_Width > m_outputWidth || m_Height > m_outputHeight) {
50       int32_t width_factor = static_cast<int32_t>(
51           floor(static_cast<float>(m_Width) / m_outputWidth));
52       int32_t height_factor = static_cast<int32_t>(
53           floor(static_cast<float>(m_Height) / m_outputHeight));
54       width_factor = std::max(width_factor, 1);
55       height_factor = std::max(height_factor, 1);
56 
57       m_outputWidth *= width_factor;
58       m_outputHeight *= height_factor;
59     }
60   }
61   m_multiX =
62       static_cast<int32_t>(ceil(static_cast<float>(m_outputWidth) / tempWidth));
63   m_multiY = static_cast<int32_t>(
64       ceil(static_cast<float>(m_outputHeight) / tempHeight));
65   if (m_bFixedSize) {
66     m_multiX = std::min(m_multiX, m_multiY);
67     m_multiY = m_multiX;
68   }
69 
70   m_leftPadding = std::max((m_Width - m_outputWidth) / 2, 0);
71   m_topPadding = std::max((m_Height - m_outputHeight) / 2, 0);
72 
73   m_output = std::make_unique<CBC_CommonBitMatrix>(m_inputWidth, m_inputHeight);
74   for (int32_t y = 0; y < m_inputHeight; ++y) {
75     for (int32_t x = 0; x < m_inputWidth; ++x) {
76       if (code[x + y * m_inputWidth] == 1)
77         m_output->Set(x, y);
78     }
79   }
80   return true;
81 }
82 
RenderDeviceResult(CFX_RenderDevice * device,const CFX_Matrix & matrix)83 void CBC_TwoDimWriter::RenderDeviceResult(CFX_RenderDevice* device,
84                                           const CFX_Matrix& matrix) {
85   DCHECK(m_output);
86 
87   CFX_GraphStateData stateData;
88   CFX_Path path;
89   path.AppendRect(0, 0, m_Width, m_Height);
90   device->DrawPath(path, &matrix, &stateData, kBackgroundColor,
91                    kBackgroundColor, CFX_FillRenderOptions::EvenOddOptions());
92   int32_t leftPos = m_leftPadding;
93   int32_t topPos = m_topPadding;
94 
95   CFX_Matrix matri = matrix;
96   if (m_Width < m_outputWidth && m_Height < m_outputHeight) {
97     CFX_Matrix matriScale(static_cast<float>(m_Width) / m_outputWidth, 0.0, 0.0,
98                           static_cast<float>(m_Height) / m_outputHeight, 0.0,
99                           0.0);
100     matriScale.Concat(matrix);
101     matri = matriScale;
102   }
103 
104   CFX_GraphStateData data;
105   for (int32_t x = 0; x < m_inputWidth; x++) {
106     for (int32_t y = 0; y < m_inputHeight; y++) {
107       if (m_output->Get(x, y)) {
108         // In the output, each module is shifted by 1 due to the one module
109         // padding added to create quiet areas.
110         int start_x_output = x + 1;
111         int end_x_output = x + 2;
112         int start_y_output = y + 1;
113         int end_y_output = y + 2;
114 
115         CFX_Path rect;
116         rect.AppendRect(leftPos + start_x_output * m_multiX,
117                         topPos + start_y_output * m_multiY,
118                         leftPos + end_x_output * m_multiX,
119                         topPos + end_y_output * m_multiY);
120         device->DrawPath(rect, &matri, &data, kBarColor, 0,
121                          CFX_FillRenderOptions::WindingOptions());
122       }
123     }
124   }
125 }
126