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/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
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 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 = std::make_unique<CBC_CommonBitMatrix>(m_inputWidth, m_inputHeight);
75 for (int32_t y = 0; y < m_inputHeight; ++y) {
76 for (int32_t x = 0; x < m_inputWidth; ++x) {
77 if (code[x + y * m_inputWidth] == 1)
78 m_output->Set(x, y);
79 }
80 }
81 return true;
82}
83
85 const CFX_Matrix& matrix) {
86 DCHECK(m_output);
87
88 CFX_GraphStateData stateData;
89 CFX_Path path;
91 device->DrawPath(path, &matrix, &stateData, kBackgroundColor,
93 int32_t leftPos = m_leftPadding;
94 int32_t topPos = m_topPadding;
95
96 CFX_Matrix matri = matrix;
97 if (m_Width < m_outputWidth && m_Height < m_outputHeight) {
98 CFX_Matrix matriScale(static_cast<float>(m_Width) / m_outputWidth, 0.0, 0.0,
99 static_cast<float>(m_Height) / m_outputHeight, 0.0,
100 0.0);
101 matriScale.Concat(matrix);
102 matri = matriScale;
103 }
104
106 for (int32_t x = 0; x < m_inputWidth; x++) {
107 for (int32_t y = 0; y < m_inputHeight; y++) {
108 if (m_output->Get(x, y)) {
109 // In the output, each module is shifted by 1 due to the one module
110 // padding added to create quiet areas.
111 int start_x_output = x + 1;
112 int end_x_output = x + 2;
113 int start_y_output = y + 1;
114 int end_y_output = y + 2;
115
116 CFX_Path rect;
117 rect.AppendRect(leftPos + start_x_output * m_multiX,
118 topPos + start_y_output * m_multiY,
119 leftPos + end_x_output * m_multiX,
120 topPos + end_y_output * m_multiY);
121 device->DrawPath(rect, &matri, &data, kBarColor, 0,
123 }
124 }
125 }
126}
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:37
int32_t m_Height
Definition BC_Writer.h:36
int32_t m_ModuleHeight
Definition BC_Writer.h:34
static const FX_ARGB kBackgroundColor
Definition BC_Writer.h:32
int32_t m_ModuleWidth
Definition BC_Writer.h:35
static const FX_ARGB kBarColor
Definition BC_Writer.h:31
CFX_Matrix & operator=(const CFX_Matrix &other)=default
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:309
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)
static constexpr CFX_FillRenderOptions EvenOddOptions()
static constexpr CFX_FillRenderOptions WindingOptions()