aboutsummaryrefslogtreecommitdiffstats
path: root/fxbarcode
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2020-01-07 23:41:49 +0000
committerChromium commit bot <commit-bot@chromium.org>2020-01-07 23:41:49 +0000
commitae5d92af623ac0949f076e6a916dbc1fb0f13e1d (patch)
tree32f7ffe49da5860de6b0db06d66112c50467c233 /fxbarcode
parent5131f71d63052ca851f6a78830d2da564be50f44 (diff)
downloadplatform_external_pdfium-ae5d92af623ac0949f076e6a916dbc1fb0f13e1d.tar.gz
platform_external_pdfium-ae5d92af623ac0949f076e6a916dbc1fb0f13e1d.tar.bz2
platform_external_pdfium-ae5d92af623ac0949f076e6a916dbc1fb0f13e1d.zip
Move CFX_Barcode from fwl/ to fxbarcode/
FWL is a windowing layer, but this code doesn't have much to do with windowing. Instead, it is a top-level entry point for much of the barcode library underneath it. All the other fwl/ files are prefixed with cfwl_, so this was inconsistent. Change-Id: Iafa583bd5d3f10753006232af3d7fcd9ae90a157 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/64652 Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'fxbarcode')
-rw-r--r--fxbarcode/BUILD.gn4
-rw-r--r--fxbarcode/cfx_barcode.cpp232
-rw-r--r--fxbarcode/cfx_barcode.h62
-rw-r--r--fxbarcode/cfx_barcode_unittest.cpp237
4 files changed, 535 insertions, 0 deletions
diff --git a/fxbarcode/BUILD.gn b/fxbarcode/BUILD.gn
index 8c092ea8e..2b10e95d1 100644
--- a/fxbarcode/BUILD.gn
+++ b/fxbarcode/BUILD.gn
@@ -39,6 +39,8 @@ source_set("fxbarcode") {
"cbc_qrcode.h",
"cbc_upca.cpp",
"cbc_upca.h",
+ "cfx_barcode.cpp",
+ "cfx_barcode.h",
"common/BC_CommonBitMatrix.cpp",
"common/BC_CommonBitMatrix.h",
"common/BC_CommonByteMatrix.cpp",
@@ -143,6 +145,7 @@ source_set("fxbarcode") {
pdfium_unittest_source_set("unittests") {
sources = [
"cbc_pdf417i_unittest.cpp",
+ "cfx_barcode_unittest.cpp",
"datamatrix/BC_DataMatrixWriter_unittest.cpp",
"oned/BC_OnedCodaBarWriter_unittest.cpp",
"oned/BC_OnedCode128Writer_unittest.cpp",
@@ -156,6 +159,7 @@ pdfium_unittest_source_set("unittests") {
]
deps = [
":fxbarcode",
+ "../core/fxge",
]
pdfium_root_dir = "../"
}
diff --git a/fxbarcode/cfx_barcode.cpp b/fxbarcode/cfx_barcode.cpp
new file mode 100644
index 000000000..54addad64
--- /dev/null
+++ b/fxbarcode/cfx_barcode.cpp
@@ -0,0 +1,232 @@
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "fxbarcode/cfx_barcode.h"
+
+#include <memory>
+
+#include "fxbarcode/cbc_codabar.h"
+#include "fxbarcode/cbc_code128.h"
+#include "fxbarcode/cbc_code39.h"
+#include "fxbarcode/cbc_codebase.h"
+#include "fxbarcode/cbc_datamatrix.h"
+#include "fxbarcode/cbc_ean13.h"
+#include "fxbarcode/cbc_ean8.h"
+#include "fxbarcode/cbc_pdf417i.h"
+#include "fxbarcode/cbc_qrcode.h"
+#include "fxbarcode/cbc_upca.h"
+#include "fxbarcode/utils.h"
+#include "third_party/base/ptr_util.h"
+
+namespace {
+
+std::unique_ptr<CBC_CodeBase> CreateBarCodeEngineObject(BC_TYPE type) {
+ switch (type) {
+ case BC_CODE39:
+ return pdfium::MakeUnique<CBC_Code39>();
+ case BC_CODABAR:
+ return pdfium::MakeUnique<CBC_Codabar>();
+ case BC_CODE128:
+ return pdfium::MakeUnique<CBC_Code128>(BC_CODE128_B);
+ case BC_CODE128_B:
+ return pdfium::MakeUnique<CBC_Code128>(BC_CODE128_B);
+ case BC_CODE128_C:
+ return pdfium::MakeUnique<CBC_Code128>(BC_CODE128_C);
+ case BC_EAN8:
+ return pdfium::MakeUnique<CBC_EAN8>();
+ case BC_UPCA:
+ return pdfium::MakeUnique<CBC_UPCA>();
+ case BC_EAN13:
+ return pdfium::MakeUnique<CBC_EAN13>();
+ case BC_QR_CODE:
+ return pdfium::MakeUnique<CBC_QRCode>();
+ case BC_PDF417:
+ return pdfium::MakeUnique<CBC_PDF417I>();
+ case BC_DATAMATRIX:
+ return pdfium::MakeUnique<CBC_DataMatrix>();
+ case BC_UNKNOWN:
+ default:
+ return nullptr;
+ }
+}
+
+} // namespace
+
+CFX_Barcode::CFX_Barcode() {}
+
+CFX_Barcode::~CFX_Barcode() {}
+
+std::unique_ptr<CFX_Barcode> CFX_Barcode::Create(BC_TYPE type) {
+ auto barcode = pdfium::WrapUnique(new CFX_Barcode()); // Private ctor.
+ barcode->m_pBCEngine = CreateBarCodeEngineObject(type);
+ return barcode;
+}
+
+BC_TYPE CFX_Barcode::GetType() {
+ return m_pBCEngine ? m_pBCEngine->GetType() : BC_UNKNOWN;
+}
+
+bool CFX_Barcode::SetCharEncoding(BC_CHAR_ENCODING encoding) {
+ return m_pBCEngine && m_pBCEngine->SetCharEncoding(encoding);
+}
+
+bool CFX_Barcode::SetModuleHeight(int32_t moduleHeight) {
+ return m_pBCEngine && m_pBCEngine->SetModuleHeight(moduleHeight);
+}
+
+bool CFX_Barcode::SetModuleWidth(int32_t moduleWidth) {
+ return m_pBCEngine && m_pBCEngine->SetModuleWidth(moduleWidth);
+}
+
+bool CFX_Barcode::SetHeight(int32_t height) {
+ return m_pBCEngine && m_pBCEngine->SetHeight(height);
+}
+
+bool CFX_Barcode::SetWidth(int32_t width) {
+ return m_pBCEngine && m_pBCEngine->SetWidth(width);
+}
+
+bool CFX_Barcode::SetPrintChecksum(bool checksum) {
+ switch (GetType()) {
+ case BC_CODE39:
+ case BC_CODABAR:
+ case BC_CODE128:
+ case BC_CODE128_B:
+ case BC_CODE128_C:
+ case BC_EAN8:
+ case BC_EAN13:
+ case BC_UPCA:
+ return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
+ ->SetPrintChecksum(checksum),
+ true)
+ : false;
+ default:
+ return false;
+ }
+}
+
+bool CFX_Barcode::SetDataLength(int32_t length) {
+ switch (GetType()) {
+ case BC_CODE39:
+ case BC_CODABAR:
+ case BC_CODE128:
+ case BC_CODE128_B:
+ case BC_CODE128_C:
+ case BC_EAN8:
+ case BC_EAN13:
+ case BC_UPCA:
+ return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
+ ->SetDataLength(length),
+ true)
+ : false;
+ default:
+ return false;
+ }
+}
+
+bool CFX_Barcode::SetCalChecksum(bool state) {
+ switch (GetType()) {
+ case BC_CODE39:
+ case BC_CODABAR:
+ case BC_CODE128:
+ case BC_CODE128_B:
+ case BC_CODE128_C:
+ case BC_EAN8:
+ case BC_EAN13:
+ case BC_UPCA:
+ return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
+ ->SetCalChecksum(state),
+ true)
+ : false;
+ default:
+ return false;
+ }
+}
+
+bool CFX_Barcode::SetFont(CFX_Font* pFont) {
+ switch (GetType()) {
+ case BC_CODE39:
+ case BC_CODABAR:
+ case BC_CODE128:
+ case BC_CODE128_B:
+ case BC_CODE128_C:
+ case BC_EAN8:
+ case BC_EAN13:
+ case BC_UPCA:
+ return m_pBCEngine
+ ? static_cast<CBC_OneCode*>(m_pBCEngine.get())->SetFont(pFont)
+ : false;
+ default:
+ return false;
+ }
+}
+
+bool CFX_Barcode::SetFontSize(float size) {
+ switch (GetType()) {
+ case BC_CODE39:
+ case BC_CODABAR:
+ case BC_CODE128:
+ case BC_CODE128_B:
+ case BC_CODE128_C:
+ case BC_EAN8:
+ case BC_EAN13:
+ case BC_UPCA:
+ return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
+ ->SetFontSize(size),
+ true)
+ : false;
+ default:
+ return false;
+ }
+}
+
+bool CFX_Barcode::SetFontColor(FX_ARGB color) {
+ switch (GetType()) {
+ case BC_CODE39:
+ case BC_CODABAR:
+ case BC_CODE128:
+ case BC_CODE128_B:
+ case BC_CODE128_C:
+ case BC_EAN8:
+ case BC_EAN13:
+ case BC_UPCA:
+ return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
+ ->SetFontColor(color),
+ true)
+ : false;
+ default:
+ return false;
+ }
+}
+
+bool CFX_Barcode::SetTextLocation(BC_TEXT_LOC location) {
+ return m_pBCEngine && m_pBCEngine->SetTextLocation(location);
+}
+
+bool CFX_Barcode::SetWideNarrowRatio(int8_t ratio) {
+ return m_pBCEngine && m_pBCEngine->SetWideNarrowRatio(ratio);
+}
+
+bool CFX_Barcode::SetStartChar(char start) {
+ return m_pBCEngine && m_pBCEngine->SetStartChar(start);
+}
+
+bool CFX_Barcode::SetEndChar(char end) {
+ return m_pBCEngine && m_pBCEngine->SetEndChar(end);
+}
+
+bool CFX_Barcode::SetErrorCorrectionLevel(int32_t level) {
+ return m_pBCEngine && m_pBCEngine->SetErrorCorrectionLevel(level);
+}
+
+bool CFX_Barcode::Encode(WideStringView contents) {
+ return m_pBCEngine && m_pBCEngine->Encode(contents);
+}
+
+bool CFX_Barcode::RenderDevice(CFX_RenderDevice* device,
+ const CFX_Matrix* matrix) {
+ return m_pBCEngine && m_pBCEngine->RenderDevice(device, matrix);
+}
diff --git a/fxbarcode/cfx_barcode.h b/fxbarcode/cfx_barcode.h
new file mode 100644
index 000000000..14204ea34
--- /dev/null
+++ b/fxbarcode/cfx_barcode.h
@@ -0,0 +1,62 @@
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef FXBARCODE_CFX_BARCODE_H_
+#define FXBARCODE_CFX_BARCODE_H_
+
+#include <memory>
+
+#include "core/fxcrt/fx_coordinates.h"
+#include "core/fxcrt/fx_string.h"
+#include "core/fxcrt/fx_system.h"
+#include "core/fxge/fx_dib.h"
+#include "fxbarcode/BC_Library.h"
+
+class CBC_CodeBase;
+class CFX_Font;
+class CFX_RenderDevice;
+class CFX_Matrix;
+
+class CFX_Barcode {
+ public:
+ ~CFX_Barcode();
+
+ static std::unique_ptr<CFX_Barcode> Create(BC_TYPE type);
+ BC_TYPE GetType();
+ bool Encode(WideStringView contents);
+
+ bool RenderDevice(CFX_RenderDevice* device, const CFX_Matrix* matrix);
+
+ bool SetCharEncoding(BC_CHAR_ENCODING encoding);
+
+ bool SetModuleHeight(int32_t moduleHeight);
+ bool SetModuleWidth(int32_t moduleWidth);
+
+ bool SetHeight(int32_t height);
+ bool SetWidth(int32_t width);
+
+ bool SetPrintChecksum(bool checksum);
+ bool SetDataLength(int32_t length);
+ bool SetCalChecksum(bool state);
+
+ bool SetFont(CFX_Font* pFont);
+ bool SetFontSize(float size);
+ bool SetFontColor(FX_ARGB color);
+
+ bool SetTextLocation(BC_TEXT_LOC location);
+
+ bool SetWideNarrowRatio(int8_t ratio);
+ bool SetStartChar(char start);
+ bool SetEndChar(char end);
+ bool SetErrorCorrectionLevel(int32_t level);
+
+ private:
+ CFX_Barcode();
+
+ std::unique_ptr<CBC_CodeBase> m_pBCEngine;
+};
+
+#endif // FXBARCODE_CFX_BARCODE_H_
diff --git a/fxbarcode/cfx_barcode_unittest.cpp b/fxbarcode/cfx_barcode_unittest.cpp
new file mode 100644
index 000000000..7569bf286
--- /dev/null
+++ b/fxbarcode/cfx_barcode_unittest.cpp
@@ -0,0 +1,237 @@
+// Copyright 2017 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "fxbarcode/cfx_barcode.h"
+
+#include <memory>
+#include <string>
+#include <utility>
+
+#include "core/fxcrt/fx_coordinates.h"
+#include "core/fxcrt/fx_string.h"
+#include "core/fxge/cfx_defaultrenderdevice.h"
+#include "core/fxge/cfx_renderdevice.h"
+#include "core/fxge/dib/cfx_dibitmap.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/utils/bitmap_saver.h"
+#include "testing/utils/hash.h"
+#include "third_party/base/ptr_util.h"
+
+class BarcodeTest : public testing::Test {
+ public:
+ void SetUp() override {
+ BC_Library_Init();
+
+ auto device = pdfium::MakeUnique<CFX_DefaultRenderDevice>();
+ auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
+ if (bitmap->Create(640, 480, FXDIB_Rgb32))
+ bitmap_ = bitmap;
+ ASSERT_TRUE(bitmap_);
+ ASSERT_TRUE(device->Attach(bitmap_, false, nullptr, false));
+ device_ = std::move(device);
+ }
+
+ void TearDown() override {
+ bitmap_.Reset();
+ device_.reset();
+ barcode_.reset();
+ BC_Library_Destroy();
+ }
+
+ CFX_Barcode* barcode() const { return barcode_.get(); }
+
+ void Create(BC_TYPE type) {
+ barcode_ = CFX_Barcode::Create(type);
+ barcode_->SetHeight(298);
+ barcode_->SetWidth(418);
+ }
+
+ bool RenderDevice() {
+ return barcode_->RenderDevice(device_.get(), &matrix_);
+ }
+
+ std::string BitmapChecksum() {
+ return GenerateMD5Base16(bitmap_->GetBuffer(),
+ bitmap_->GetPitch() * bitmap_->GetHeight());
+ }
+
+ // Manually insert calls to this as needed for debugging.
+ void SaveBitmap(const std::string& filename) {
+ BitmapSaver::WriteBitmapToPng(bitmap_.Get(), filename);
+ }
+
+ protected:
+ CFX_Matrix matrix_;
+ std::unique_ptr<CFX_Barcode> barcode_;
+ std::unique_ptr<CFX_RenderDevice> device_;
+ RetainPtr<CFX_DIBitmap> bitmap_;
+};
+
+// https://crbug.com/pdfium/738
+#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
+#define MAYBE_Code39 DISABLED_Code39
+#else
+#define MAYBE_Code39 Code39
+#endif
+TEST_F(BarcodeTest, MAYBE_Code39) {
+ Create(BC_CODE39);
+ EXPECT_TRUE(barcode()->Encode(L"CLAMS"));
+ RenderDevice();
+ EXPECT_EQ("cd4cd3f36da38ff58d9f621827018903", BitmapChecksum());
+}
+
+// https://crbug.com/pdfium/738
+#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
+#define MAYBE_CodaBar DISABLED_CodaBar
+#else
+#define MAYBE_CodaBar CodaBar
+#endif
+TEST_F(BarcodeTest, MAYBE_CodaBar) {
+ Create(BC_CODABAR);
+ EXPECT_TRUE(barcode()->Encode(L"$123-456"));
+ RenderDevice();
+ EXPECT_EQ("5fad4fc19f099001a0fe83c89430c977", BitmapChecksum());
+}
+
+TEST_F(BarcodeTest, DISABLED_CodaBarLetters) {
+ Create(BC_CODABAR);
+ EXPECT_FALSE(barcode()->Encode(L"clams"));
+}
+
+// https://crbug.com/pdfium/738
+#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
+#define MAYBE_Code128 DISABLED_Code128
+#else
+#define MAYBE_Code128 Code128
+#endif
+TEST_F(BarcodeTest, MAYBE_Code128) {
+ Create(BC_CODE128);
+ EXPECT_TRUE(barcode()->Encode(L"Clams"));
+ RenderDevice();
+ EXPECT_EQ("6351f0f6e997050e4658bbb4777aef74", BitmapChecksum());
+}
+
+// https://crbug.com/pdfium/738
+#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
+#define MAYBE_Code128B DISABLED_Code128B
+#else
+#define MAYBE_Code128B Code128B
+#endif
+TEST_F(BarcodeTest, MAYBE_Code128B) {
+ Create(BC_CODE128_B);
+ EXPECT_TRUE(barcode()->Encode(L"Clams"));
+ RenderDevice();
+ EXPECT_EQ("6351f0f6e997050e4658bbb4777aef74", BitmapChecksum());
+}
+
+// https://crbug.com/pdfium/738
+#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
+#define MAYBE_Code128C DISABLED_Code128C
+#else
+#define MAYBE_Code128C Code128C
+#endif
+TEST_F(BarcodeTest, MAYBE_Code128C) {
+ Create(BC_CODE128_C);
+ EXPECT_TRUE(barcode()->Encode(L"123456"));
+ RenderDevice();
+ EXPECT_EQ("fba730a807ba6363f9bd2bc7f8c56d1f", BitmapChecksum());
+}
+
+TEST_F(BarcodeTest, DISABLED_Code128CLetters) {
+ Create(BC_CODE128_C);
+ EXPECT_FALSE(barcode()->Encode(L"clams"));
+}
+
+// https://crbug.com/pdfium/738
+#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
+#define MAYBE_Ean8 DISABLED_Ean8
+#else
+#define MAYBE_Ean8 Ean8
+#endif
+TEST_F(BarcodeTest, MAYBE_Ean8) {
+ Create(BC_EAN8);
+ EXPECT_TRUE(barcode()->Encode(L"123456"));
+ RenderDevice();
+ EXPECT_EQ("aff88491ac46ca6217d780d185300cde", BitmapChecksum());
+}
+
+TEST_F(BarcodeTest, DISABLED_Ean8Letters) {
+ Create(BC_EAN8);
+ EXPECT_FALSE(barcode()->Encode(L"clams"));
+}
+
+// https://crbug.com/pdfium/738
+#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
+#define MAYBE_UPCA DISABLED_UPCA
+#else
+#define MAYBE_UPCA UPCA
+#endif
+TEST_F(BarcodeTest, MAYBE_UPCA) {
+ Create(BC_UPCA);
+ EXPECT_TRUE(barcode()->Encode(L"123456"));
+ RenderDevice();
+ EXPECT_EQ("fe26a5714cff7ffe3f9b02183efc435b", BitmapChecksum());
+}
+
+TEST_F(BarcodeTest, DISABLED_UPCALetters) {
+ Create(BC_UPCA);
+ EXPECT_FALSE(barcode()->Encode(L"clams"));
+}
+
+// https://crbug.com/pdfium/738
+#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
+#define MAYBE_Ean13 DISABLED_Ean13
+#else
+#define MAYBE_Ean13 Ean13
+#endif
+TEST_F(BarcodeTest, MAYBE_Ean13) {
+ Create(BC_EAN13);
+ EXPECT_TRUE(barcode()->Encode(L"123456"));
+ RenderDevice();
+ EXPECT_EQ("72d2190b98d635c32834bf67552e561e", BitmapChecksum());
+}
+
+TEST_F(BarcodeTest, DISABLED_Ean13Letters) {
+ Create(BC_EAN13);
+ EXPECT_FALSE(barcode()->Encode(L"clams"));
+}
+
+// https://crbug.com/pdfium/738
+#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
+#define MAYBE_Pdf417 DISABLED_Pdf417
+#else
+#define MAYBE_Pdf417 Pdf417
+#endif
+TEST_F(BarcodeTest, MAYBE_Pdf417) {
+ Create(BC_PDF417);
+ EXPECT_TRUE(barcode()->Encode(L"clams"));
+ RenderDevice();
+ EXPECT_EQ("191e35d11613901b7d5d51033689aa89", BitmapChecksum());
+}
+
+// https://crbug.com/pdfium/738
+#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
+#define MAYBE_DataMatrix DISABLED_DataMatrix
+#else
+#define MAYBE_DataMatrix DataMatrix
+#endif
+TEST_F(BarcodeTest, MAYBE_DataMatrix) {
+ Create(BC_DATAMATRIX);
+ EXPECT_TRUE(barcode()->Encode(L"clams"));
+ RenderDevice();
+ EXPECT_EQ("5e5cd9a680b86fcd4ffd53ed36e3c980", BitmapChecksum());
+}
+
+// https://crbug.com/pdfium/738
+#if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
+#define MAYBE_QrCode DISABLED_QrCode
+#else
+#define MAYBE_QrCode QrCode
+#endif
+TEST_F(BarcodeTest, MAYBE_QrCode) {
+ Create(BC_QR_CODE);
+ EXPECT_TRUE(barcode()->Encode(L"clams"));
+ RenderDevice();
+ EXPECT_EQ("4751c6e0f67749fabe24f787128decee", BitmapChecksum());
+}