• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <algorithm>
8 
9 #include "core/fxge/cfx_graphstatedata.h"
10 #include "core/fxge/cfx_pathdata.h"
11 #include "core/fxge/cfx_renderdevice.h"
12 #include "fxbarcode/BC_TwoDimWriter.h"
13 #include "fxbarcode/BC_Writer.h"
14 #include "fxbarcode/common/BC_CommonBitMatrix.h"
15 #include "third_party/base/numerics/safe_math.h"
16 #include "third_party/base/ptr_util.h"
17 
CBC_TwoDimWriter()18 CBC_TwoDimWriter::CBC_TwoDimWriter() : m_iCorrectLevel(1), m_bFixedSize(true) {}
19 
~CBC_TwoDimWriter()20 CBC_TwoDimWriter::~CBC_TwoDimWriter() {}
21 
RenderDeviceResult(CFX_RenderDevice * device,const CFX_Matrix * matrix)22 void CBC_TwoDimWriter::RenderDeviceResult(CFX_RenderDevice* device,
23                                           const CFX_Matrix* matrix) {
24   CFX_GraphStateData stateData;
25   CFX_PathData path;
26   path.AppendRect(0, 0, (float)m_Width, (float)m_Height);
27   device->DrawPath(&path, matrix, &stateData, m_backgroundColor,
28                    m_backgroundColor, FXFILL_ALTERNATE);
29   int32_t leftPos = 0;
30   int32_t topPos = 0;
31   if (m_bFixedSize) {
32     leftPos = (m_Width - m_output->GetWidth()) / 2;
33     topPos = (m_Height - m_output->GetHeight()) / 2;
34   }
35   CFX_Matrix matri = *matrix;
36   if (m_Width < m_output->GetWidth() && m_Height < m_output->GetHeight()) {
37     CFX_Matrix matriScale((float)m_Width / (float)m_output->GetWidth(), 0.0,
38                           0.0, (float)m_Height / (float)m_output->GetHeight(),
39                           0.0, 0.0);
40     matriScale.Concat(*matrix);
41     matri = matriScale;
42   }
43   for (int32_t x = 0; x < m_output->GetWidth(); x++) {
44     for (int32_t y = 0; y < m_output->GetHeight(); y++) {
45       CFX_PathData rect;
46       rect.AppendRect((float)leftPos + x, (float)topPos + y,
47                       (float)(leftPos + x + 1), (float)(topPos + y + 1));
48       if (m_output->Get(x, y)) {
49         CFX_GraphStateData data;
50         device->DrawPath(&rect, &matri, &data, m_barColor, 0, FXFILL_WINDING);
51       }
52     }
53   }
54 }
55 
GetErrorCorrectionLevel() const56 int32_t CBC_TwoDimWriter::GetErrorCorrectionLevel() const {
57   return m_iCorrectLevel;
58 }
59 
RenderResult(uint8_t * code,int32_t codeWidth,int32_t codeHeight)60 bool CBC_TwoDimWriter::RenderResult(uint8_t* code,
61                                     int32_t codeWidth,
62                                     int32_t codeHeight) {
63   int32_t inputWidth = codeWidth;
64   int32_t inputHeight = codeHeight;
65   int32_t tempWidth = inputWidth + 2;
66   int32_t tempHeight = inputHeight + 2;
67   float moduleHSize = std::min(m_ModuleWidth, m_ModuleHeight);
68   moduleHSize = std::min(moduleHSize, 8.0f);
69   moduleHSize = std::max(moduleHSize, 1.0f);
70   pdfium::base::CheckedNumeric<int32_t> scaledWidth = tempWidth;
71   pdfium::base::CheckedNumeric<int32_t> scaledHeight = tempHeight;
72   scaledWidth *= moduleHSize;
73   scaledHeight *= moduleHSize;
74 
75   int32_t outputWidth = scaledWidth.ValueOrDie();
76   int32_t outputHeight = scaledHeight.ValueOrDie();
77   if (m_bFixedSize) {
78     if (m_Width < outputWidth || m_Height < outputHeight) {
79       return false;
80     }
81   } else {
82     if (m_Width > outputWidth || m_Height > outputHeight) {
83       outputWidth =
84           (int32_t)(outputWidth * ceil((float)m_Width / (float)outputWidth));
85       outputHeight =
86           (int32_t)(outputHeight * ceil((float)m_Height / (float)outputHeight));
87     }
88   }
89   int32_t multiX = (int32_t)ceil((float)outputWidth / (float)tempWidth);
90   int32_t multiY = (int32_t)ceil((float)outputHeight / (float)tempHeight);
91   if (m_bFixedSize) {
92     multiX = std::min(multiX, multiY);
93     multiY = multiX;
94   }
95   int32_t leftPadding = std::max((outputWidth - (inputWidth * multiX)) / 2, 0);
96   int32_t topPadding = std::max((outputHeight - (inputHeight * multiY)) / 2, 0);
97   m_output = pdfium::MakeUnique<CBC_CommonBitMatrix>();
98   m_output->Init(outputWidth, outputHeight);
99   for (int32_t inputY = 0, outputY = topPadding;
100        (inputY < inputHeight) && (outputY < outputHeight - multiY);
101        inputY++, outputY += multiY) {
102     for (int32_t inputX = 0, outputX = leftPadding;
103          (inputX < inputWidth) && (outputX < outputWidth - multiX);
104          inputX++, outputX += multiX) {
105       if (code[inputX + inputY * inputWidth] == 1 &&
106           !m_output->SetRegion(outputX, outputY, multiX, multiY)) {
107         return false;
108       }
109     }
110   }
111   return true;
112 }
113