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