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 "third_party/base/numerics/safe_math.h" 13 #include "third_party/base/ptr_util.h" 14 #include "xfa/fxbarcode/BC_TwoDimWriter.h" 15 #include "xfa/fxbarcode/BC_Writer.h" 16 #include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" 17 18 CBC_TwoDimWriter::CBC_TwoDimWriter() : m_iCorrectLevel(1), m_bFixedSize(true) {} 19 20 CBC_TwoDimWriter::~CBC_TwoDimWriter() {} 21 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, (FX_FLOAT)m_Width, (FX_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( 38 (FX_FLOAT)m_Width / (FX_FLOAT)m_output->GetWidth(), 0.0, 0.0, 39 (FX_FLOAT)m_Height / (FX_FLOAT)m_output->GetHeight(), 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((FX_FLOAT)leftPos + x, (FX_FLOAT)topPos + y, 47 (FX_FLOAT)(leftPos + x + 1), (FX_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 56 int32_t CBC_TwoDimWriter::GetErrorCorrectionLevel() const { 57 return m_iCorrectLevel; 58 } 59 60 void CBC_TwoDimWriter::RenderBitmapResult(CFX_DIBitmap*& pOutBitmap, 61 int32_t& e) { 62 if (m_bFixedSize) { 63 pOutBitmap = CreateDIBitmap(m_Width, m_Height); 64 } else { 65 pOutBitmap = CreateDIBitmap(m_output->GetWidth(), m_output->GetHeight()); 66 } 67 if (!pOutBitmap) { 68 e = BCExceptionFailToCreateBitmap; 69 return; 70 } 71 pOutBitmap->Clear(m_backgroundColor); 72 int32_t leftPos = 0; 73 int32_t topPos = 0; 74 if (m_bFixedSize) { 75 leftPos = (m_Width - m_output->GetWidth()) / 2; 76 topPos = (m_Height - m_output->GetHeight()) / 2; 77 } 78 for (int32_t x = 0; x < m_output->GetWidth(); x++) { 79 for (int32_t y = 0; y < m_output->GetHeight(); y++) { 80 if (m_output->Get(x, y)) { 81 pOutBitmap->SetPixel(leftPos + x, topPos + y, m_barColor); 82 } 83 } 84 } 85 if (!m_bFixedSize) { 86 std::unique_ptr<CFX_DIBitmap> pStretchBitmap = 87 pOutBitmap->StretchTo(m_Width, m_Height); 88 delete pOutBitmap; 89 pOutBitmap = pStretchBitmap.release(); 90 } 91 } 92 93 void CBC_TwoDimWriter::RenderResult(uint8_t* code, 94 int32_t codeWidth, 95 int32_t codeHeight, 96 int32_t& e) { 97 int32_t inputWidth = codeWidth; 98 int32_t inputHeight = codeHeight; 99 int32_t tempWidth = inputWidth + 2; 100 int32_t tempHeight = inputHeight + 2; 101 FX_FLOAT moduleHSize = std::min(m_ModuleWidth, m_ModuleHeight); 102 moduleHSize = std::min(moduleHSize, 8.0f); 103 moduleHSize = std::max(moduleHSize, 1.0f); 104 pdfium::base::CheckedNumeric<int32_t> scaledWidth = tempWidth; 105 pdfium::base::CheckedNumeric<int32_t> scaledHeight = tempHeight; 106 scaledWidth *= moduleHSize; 107 scaledHeight *= moduleHSize; 108 109 int32_t outputWidth = scaledWidth.ValueOrDie(); 110 int32_t outputHeight = scaledHeight.ValueOrDie(); 111 if (m_bFixedSize) { 112 if (m_Width < outputWidth || m_Height < outputHeight) { 113 e = BCExceptionBitmapSizeError; 114 return; 115 } 116 } else { 117 if (m_Width > outputWidth || m_Height > outputHeight) { 118 outputWidth = (int32_t)(outputWidth * 119 ceil((FX_FLOAT)m_Width / (FX_FLOAT)outputWidth)); 120 outputHeight = (int32_t)( 121 outputHeight * ceil((FX_FLOAT)m_Height / (FX_FLOAT)outputHeight)); 122 } 123 } 124 int32_t multiX = (int32_t)ceil((FX_FLOAT)outputWidth / (FX_FLOAT)tempWidth); 125 int32_t multiY = (int32_t)ceil((FX_FLOAT)outputHeight / (FX_FLOAT)tempHeight); 126 if (m_bFixedSize) { 127 multiX = std::min(multiX, multiY); 128 multiY = multiX; 129 } 130 int32_t leftPadding = (outputWidth - (inputWidth * multiX)) / 2; 131 int32_t topPadding = (outputHeight - (inputHeight * multiY)) / 2; 132 if (leftPadding < 0) { 133 leftPadding = 0; 134 } 135 if (topPadding < 0) { 136 topPadding = 0; 137 } 138 m_output = pdfium::MakeUnique<CBC_CommonBitMatrix>(); 139 m_output->Init(outputWidth, outputHeight); 140 for (int32_t inputY = 0, outputY = topPadding; 141 (inputY < inputHeight) && (outputY < outputHeight - multiY); 142 inputY++, outputY += multiY) { 143 for (int32_t inputX = 0, outputX = leftPadding; 144 (inputX < inputWidth) && (outputX < outputWidth - multiX); 145 inputX++, outputX += multiX) { 146 if (code[inputX + inputY * inputWidth] == 1) { 147 m_output->SetRegion(outputX, outputY, multiX, multiY, e); 148 if (e != BCExceptionNO) 149 return; 150 } 151 } 152 } 153 } 154