Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
BC_TwoDimWriter.cpp
Go to the documentation of this file.
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
21 : m_bFixedSize(bFixedSize) {}
22
24
25bool 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
84 const CFX_Matrix& matrix) {
85 DCHECK(m_output);
86
87 CFX_GraphStateData stateData;
88 CFX_Path path;
90 device->DrawPath(path, &matrix, &stateData, kBackgroundColor,
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
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,
122 }
123 }
124 }
125}
#define DCHECK
Definition check.h:33
bool RenderResult(pdfium::span< const uint8_t > code, int32_t codeWidth, int32_t codeHeight)
void RenderDeviceResult(CFX_RenderDevice *device, const CFX_Matrix &matrix)
~CBC_TwoDimWriter() override
CBC_TwoDimWriter(bool bFixedSize)
int32_t m_Width
Definition BC_Writer.h:36
int32_t m_Height
Definition BC_Writer.h:35
int32_t m_ModuleHeight
Definition BC_Writer.h:33
static const FX_ARGB kBackgroundColor
Definition BC_Writer.h:31
int32_t m_ModuleWidth
Definition BC_Writer.h:34
static const FX_ARGB kBarColor
Definition BC_Writer.h:30
CFX_Matrix & operator=(const CFX_Matrix &other)=default
constexpr CFX_Matrix(float a1, float b1, float c1, float d1, float e1, float f1)
void Concat(const CFX_Matrix &right)
void AppendRect(float left, float bottom, float right, float top)
Definition cfx_path.cpp:310
bool DrawPath(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_GraphStateData *pGraphState, uint32_t fill_color, uint32_t stroke_color, const CFX_FillRenderOptions &fill_options)
pdfium::CheckedNumeric< int32_t > FX_SAFE_INT32
static constexpr CFX_FillRenderOptions EvenOddOptions()
static constexpr CFX_FillRenderOptions WindingOptions()