Better encapsulate CBC_TwoDimWriter.

Make all CBC_TwoDimWriter members private, and const when possible. Add
appropriate mechanisms to initialize them. Remove shadow member variable
from CBC_DataMatrixWriter sub-class.

Change-Id: Ic5c95ebf869c4a2afb362fe5afd263e56d754790
Reviewed-on: https://pdfium-review.googlesource.com/c/46510
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/fxbarcode/BC_TwoDimWriter.cpp b/fxbarcode/BC_TwoDimWriter.cpp
index bff37df..236ddf3 100644
--- a/fxbarcode/BC_TwoDimWriter.cpp
+++ b/fxbarcode/BC_TwoDimWriter.cpp
@@ -15,13 +15,10 @@
 #include "third_party/base/numerics/safe_math.h"
 #include "third_party/base/ptr_util.h"
 
-CBC_TwoDimWriter::CBC_TwoDimWriter() : m_iCorrectLevel(1), m_bFixedSize(true) {}
+CBC_TwoDimWriter::CBC_TwoDimWriter(bool bFixedSize)
+    : m_bFixedSize(bFixedSize) {}
 
-CBC_TwoDimWriter::~CBC_TwoDimWriter() {}
-
-int32_t CBC_TwoDimWriter::GetErrorCorrectionLevel() const {
-  return m_iCorrectLevel;
-}
+CBC_TwoDimWriter::~CBC_TwoDimWriter() = default;
 
 bool CBC_TwoDimWriter::RenderResult(uint8_t* code,
                                     int32_t codeWidth,
@@ -87,7 +84,7 @@
 
   CFX_GraphStateData stateData;
   CFX_PathData path;
-  path.AppendRect(0, 0, (float)m_Width, (float)m_Height);
+  path.AppendRect(0, 0, m_Width, m_Height);
   device->DrawPath(&path, matrix, &stateData, m_backgroundColor,
                    m_backgroundColor, FXFILL_ALTERNATE);
   int32_t leftPos = m_leftPadding;
diff --git a/fxbarcode/BC_TwoDimWriter.h b/fxbarcode/BC_TwoDimWriter.h
index 22c80ad..a56c9ed 100644
--- a/fxbarcode/BC_TwoDimWriter.h
+++ b/fxbarcode/BC_TwoDimWriter.h
@@ -17,7 +17,7 @@
 
 class CBC_TwoDimWriter : public CBC_Writer {
  public:
-  CBC_TwoDimWriter();
+  explicit CBC_TwoDimWriter(bool bFixedSize);
   ~CBC_TwoDimWriter() override;
 
   virtual bool RenderResult(uint8_t* code,
@@ -26,11 +26,10 @@
   virtual void RenderDeviceResult(CFX_RenderDevice* device,
                                   const CFX_Matrix* matrix);
 
-  int32_t GetErrorCorrectionLevel() const;
+  int32_t error_correction_level() const { return m_iCorrectionLevel; }
 
  protected:
-  int32_t m_iCorrectLevel;
-  bool m_bFixedSize;
+  void set_error_correction_level(int32_t level) { m_iCorrectionLevel = level; }
 
  private:
   std::unique_ptr<CBC_CommonBitMatrix> m_output;
@@ -42,6 +41,8 @@
   int32_t m_inputHeight;
   int32_t m_outputWidth;
   int32_t m_outputHeight;
+  int32_t m_iCorrectionLevel = 1;
+  const bool m_bFixedSize;
 };
 
 #endif  // FXBARCODE_BC_TWODIMWRITER_H_
diff --git a/fxbarcode/cbc_qrcode.cpp b/fxbarcode/cbc_qrcode.cpp
index a5679a0..7c3c48e 100644
--- a/fxbarcode/cbc_qrcode.cpp
+++ b/fxbarcode/cbc_qrcode.cpp
@@ -36,11 +36,9 @@
   int32_t outHeight = 0;
   CBC_QRCodeWriter* pWriter = GetQRCodeWriter();
   std::unique_ptr<uint8_t, FxFreeDeleter> data(
-      pWriter->Encode(WideString(contents), pWriter->GetErrorCorrectionLevel(),
+      pWriter->Encode(WideString(contents), pWriter->error_correction_level(),
                       outWidth, outHeight));
-  if (!data)
-    return false;
-  return pWriter->RenderResult(data.get(), outWidth, outHeight);
+  return data && pWriter->RenderResult(data.get(), outWidth, outHeight);
 }
 
 bool CBC_QRCode::RenderDevice(CFX_RenderDevice* device,
diff --git a/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp b/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp
index 5d60fbf..f08992f 100644
--- a/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp
+++ b/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp
@@ -97,12 +97,12 @@
 
 }  // namespace
 
-CBC_DataMatrixWriter::CBC_DataMatrixWriter() {}
+CBC_DataMatrixWriter::CBC_DataMatrixWriter() : CBC_TwoDimWriter(true) {}
 
-CBC_DataMatrixWriter::~CBC_DataMatrixWriter() {}
+CBC_DataMatrixWriter::~CBC_DataMatrixWriter() = default;
 
 bool CBC_DataMatrixWriter::SetErrorCorrectionLevel(int32_t level) {
-  m_iCorrectLevel = level;
+  set_error_correction_level(level);
   return true;
 }
 
diff --git a/fxbarcode/datamatrix/BC_DataMatrixWriter.h b/fxbarcode/datamatrix/BC_DataMatrixWriter.h
index 6fa41c0..fcda36e 100644
--- a/fxbarcode/datamatrix/BC_DataMatrixWriter.h
+++ b/fxbarcode/datamatrix/BC_DataMatrixWriter.h
@@ -24,9 +24,6 @@
 
   // CBC_TwoDimWriter
   bool SetErrorCorrectionLevel(int32_t level) override;
-
- private:
-  int32_t m_iCorrectLevel;
 };
 
 #endif  // FXBARCODE_DATAMATRIX_BC_DATAMATRIXWRITER_H_
diff --git a/fxbarcode/pdf417/BC_PDF417Writer.cpp b/fxbarcode/pdf417/BC_PDF417Writer.cpp
index aecca68..237fb2d 100644
--- a/fxbarcode/pdf417/BC_PDF417Writer.cpp
+++ b/fxbarcode/pdf417/BC_PDF417Writer.cpp
@@ -30,17 +30,15 @@
 #include "fxbarcode/pdf417/BC_PDF417.h"
 #include "fxbarcode/pdf417/BC_PDF417BarcodeMatrix.h"
 
-CBC_PDF417Writer::CBC_PDF417Writer() {
-  m_bFixedSize = false;
-}
+CBC_PDF417Writer::CBC_PDF417Writer() : CBC_TwoDimWriter(false) {}
 
-CBC_PDF417Writer::~CBC_PDF417Writer() {}
+CBC_PDF417Writer::~CBC_PDF417Writer() = default;
 
 bool CBC_PDF417Writer::SetErrorCorrectionLevel(int32_t level) {
   if (level < 0 || level > 8) {
     return false;
   }
-  m_iCorrectLevel = level;
+  set_error_correction_level(level);
   return true;
 }
 
@@ -56,7 +54,7 @@
     encoder.setDimensions(col, col, 90, 3);
   else if (row >= 3 && row <= 90)
     encoder.setDimensions(30, 1, row, row);
-  if (!encoder.generateBarcodeLogic(contents, m_iCorrectLevel))
+  if (!encoder.generateBarcodeLogic(contents, error_correction_level()))
     return nullptr;
 
   CBC_BarcodeMatrix* barcodeMatrix = encoder.getBarcodeMatrix();
diff --git a/fxbarcode/qrcode/BC_QRCodeWriter.cpp b/fxbarcode/qrcode/BC_QRCodeWriter.cpp
index 8c12d8c..e755cac 100644
--- a/fxbarcode/qrcode/BC_QRCodeWriter.cpp
+++ b/fxbarcode/qrcode/BC_QRCodeWriter.cpp
@@ -30,10 +30,7 @@
 #include "fxbarcode/qrcode/BC_QRCoderMode.h"
 #include "fxbarcode/qrcode/BC_QRCoderVersion.h"
 
-CBC_QRCodeWriter::CBC_QRCodeWriter() {
-  m_bFixedSize = true;
-  m_iCorrectLevel = 1;
-}
+CBC_QRCodeWriter::CBC_QRCodeWriter() : CBC_TwoDimWriter(true) {}
 
 CBC_QRCodeWriter::~CBC_QRCodeWriter() = default;
 
@@ -41,7 +38,7 @@
   if (level < 0 || level > 3) {
     return false;
   }
-  m_iCorrectLevel = level;
+  set_error_correction_level(level);
   return true;
 }