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