Tidy up shading patterns

R=thestig@chromium.org

Review URL: https://codereview.chromium.org/1570873005 .
diff --git a/core/include/fpdfapi/fpdf_resource.h b/core/include/fpdfapi/fpdf_resource.h
index d6d43ee..b10a01c 100644
--- a/core/include/fpdfapi/fpdf_resource.h
+++ b/core/include/fpdfapi/fpdf_resource.h
@@ -636,24 +636,27 @@
   void ReleaseColorSpace();
   FX_FLOAT* m_pBuffer;
 };
-#define PATTERN_TILING 1
-#define PATTERN_SHADING 2
+
 class CPDF_Pattern {
  public:
+  enum PatternType { TILING = 1, SHADING };
+
   virtual ~CPDF_Pattern();
+
   void SetForceClear(FX_BOOL bForceClear) { m_bForceClear = bForceClear; }
 
-  CPDF_Object* m_pPatternObj;
-
-  int m_PatternType;
-
+  const PatternType m_PatternType;
+  CPDF_Document* const m_pDocument;
+  CPDF_Object* const m_pPatternObj;
   CFX_Matrix m_Pattern2Form;
   CFX_Matrix m_ParentMatrix;
 
-  CPDF_Document* m_pDocument;
-
  protected:
-  CPDF_Pattern(const CFX_Matrix* pParentMatrix);
+  CPDF_Pattern(PatternType type,
+               CPDF_Document* pDoc,
+               CPDF_Object* pObj,
+               const CFX_Matrix* pParentMatrix);
+
   FX_BOOL m_bForceClear;
 };
 
@@ -705,30 +708,21 @@
            m_ShadingType == kCoonsPatchMeshShading ||
            m_ShadingType == kTensorProductPatchMeshShading;
   }
-
-  CPDF_Object* m_pShadingObj;
-
-  FX_BOOL m_bShadingObj;
-
   FX_BOOL Load();
 
-  FX_BOOL Reload();
-
   ShadingType m_ShadingType;
+  FX_BOOL m_bShadingObj;
+  CPDF_Object* m_pShadingObj;
 
-  CPDF_ColorSpace* m_pCS;  // Still keep m_pCS as some CPDF_ColorSpace (name
-                           // object) are not managed as counted objects. Refer
-                           // to CPDF_DocPageData::GetColorSpace.
+  // Still keep |m_pCS| as some CPDF_ColorSpace (name object) are not managed
+  // as counted objects. Refer to CPDF_DocPageData::GetColorSpace.
+  CPDF_ColorSpace* m_pCS;
 
   CPDF_CountedColorSpace* m_pCountedCS;
-
   CPDF_Function* m_pFunctions[4];
-
   int m_nFuncs;
-
- protected:
-  void Clear();
 };
+
 struct CPDF_MeshVertex {
   FX_FLOAT x, y;
   FX_FLOAT r, g, b;
diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp
index 3fbd3e4..d3e3807 100644
--- a/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp
+++ b/core/src/fpdfapi/fpdf_page/fpdf_page_parser.cpp
@@ -1100,10 +1100,10 @@
   if (!pPattern) {
     return;
   }
-  if (pPattern->m_PatternType != PATTERN_SHADING) {
+  if (pPattern->m_PatternType != CPDF_Pattern::SHADING) {
     return;
   }
-  CPDF_ShadingPattern* pShading = (CPDF_ShadingPattern*)pPattern;
+  CPDF_ShadingPattern* pShading = static_cast<CPDF_ShadingPattern*>(pPattern);
   if (!pShading->m_bShadingObj) {
     return;
   }
diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_pattern.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_pattern.cpp
index fc050b9..1eae578 100644
--- a/core/src/fpdfapi/fpdf_page/fpdf_page_pattern.cpp
+++ b/core/src/fpdfapi/fpdf_page/fpdf_page_pattern.cpp
@@ -26,25 +26,23 @@
 
 }  // namespace
 
-CPDF_Pattern::CPDF_Pattern(const CFX_Matrix* pParentMatrix)
-    : m_pPatternObj(NULL),
-      m_PatternType(PATTERN_TILING),
-      m_pDocument(NULL),
+CPDF_Pattern::CPDF_Pattern(PatternType type,
+                           CPDF_Document* pDoc,
+                           CPDF_Object* pObj,
+                           const CFX_Matrix* pParentMatrix)
+    : m_PatternType(type),
+      m_pDocument(pDoc),
+      m_pPatternObj(pObj),
       m_bForceClear(FALSE) {
-  if (pParentMatrix) {
+  if (pParentMatrix)
     m_ParentMatrix = *pParentMatrix;
-  }
 }
 CPDF_Pattern::~CPDF_Pattern() {}
 CPDF_TilingPattern::CPDF_TilingPattern(CPDF_Document* pDoc,
                                        CPDF_Object* pPatternObj,
                                        const CFX_Matrix* parentMatrix)
-    : CPDF_Pattern(parentMatrix) {
-  m_PatternType = PATTERN_TILING;
-  m_pPatternObj = pPatternObj;
-  m_pDocument = pDoc;
+    : CPDF_Pattern(TILING, pDoc, pPatternObj, parentMatrix) {
   CPDF_Dictionary* pDict = m_pPatternObj->GetDict();
-  ASSERT(pDict);
   m_Pattern2Form = pDict->GetMatrix("Matrix");
   m_bColored = pDict->GetInteger("PaintType") == 1;
   if (parentMatrix) {
@@ -81,46 +79,34 @@
                                          CPDF_Object* pPatternObj,
                                          FX_BOOL bShading,
                                          const CFX_Matrix* parentMatrix)
-    : CPDF_Pattern(parentMatrix) {
-  m_PatternType = PATTERN_SHADING;
-  m_pPatternObj = bShading ? NULL : pPatternObj;
-  m_pDocument = pDoc;
-  m_bShadingObj = bShading;
+    : CPDF_Pattern(SHADING,
+                   pDoc,
+                   bShading ? nullptr : pPatternObj,
+                   parentMatrix),
+      m_ShadingType(kInvalidShading),
+      m_bShadingObj(bShading),
+      m_pShadingObj(pPatternObj),
+      m_pCS(nullptr),
+      m_pCountedCS(nullptr),
+      m_nFuncs(0) {
   if (!bShading) {
     CPDF_Dictionary* pDict = m_pPatternObj->GetDict();
-    ASSERT(pDict);
     m_Pattern2Form = pDict->GetMatrix("Matrix");
     m_pShadingObj = pDict->GetElementValue("Shading");
-    if (parentMatrix) {
+    if (parentMatrix)
       m_Pattern2Form.Concat(*parentMatrix);
-    }
-  } else {
-    m_pShadingObj = pPatternObj;
   }
-  m_ShadingType = kInvalidShading;
-  m_pCS = NULL;
-  m_nFuncs = 0;
-  for (int i = 0; i < 4; i++) {
-    m_pFunctions[i] = NULL;
-  }
-  m_pCountedCS = NULL;
+  for (int i = 0; i < FX_ArraySize(m_pFunctions); ++i)
+    m_pFunctions[i] = nullptr;
 }
+
 CPDF_ShadingPattern::~CPDF_ShadingPattern() {
-  Clear();
-}
-void CPDF_ShadingPattern::Clear() {
-  for (int i = 0; i < m_nFuncs; i++) {
+  for (int i = 0; i < m_nFuncs; ++i)
     delete m_pFunctions[i];
-    m_pFunctions[i] = NULL;
-  }
+
   CPDF_ColorSpace* pCS = m_pCountedCS ? m_pCountedCS->get() : NULL;
-  if (pCS && m_pDocument) {
+  if (pCS && m_pDocument)
     m_pDocument->GetPageData()->ReleaseColorSpace(pCS->GetArray());
-  }
-  m_ShadingType = kInvalidShading;
-  m_pCS = NULL;
-  m_pCountedCS = NULL;
-  m_nFuncs = 0;
 }
 
 FX_BOOL CPDF_ShadingPattern::Load() {
@@ -168,10 +154,6 @@
 
   return TRUE;
 }
-FX_BOOL CPDF_ShadingPattern::Reload() {
-  Clear();
-  return Load();
-}
 FX_BOOL CPDF_MeshStream::Load(CPDF_Stream* pShadingStream,
                               CPDF_Function** pFuncs,
                               int nFuncs,
diff --git a/core/src/fpdfapi/fpdf_render/fpdf_render_image.cpp b/core/src/fpdfapi/fpdf_render/fpdf_render_image.cpp
index 640adb1..3f7347b 100644
--- a/core/src/fpdfapi/fpdf_render/fpdf_render_image.cpp
+++ b/core/src/fpdfapi/fpdf_render/fpdf_render_image.cpp
@@ -522,12 +522,14 @@
                              m_pRenderStatus->m_bDropObjects, NULL, TRUE);
     CFX_Matrix patternDevice = *pObj2Device;
     patternDevice.Translate((FX_FLOAT)-rect.left, (FX_FLOAT)-rect.top);
-    if (m_pPattern->m_PatternType == PATTERN_TILING) {
-      bitmap_render.DrawTilingPattern((CPDF_TilingPattern*)m_pPattern,
-                                      m_pImageObject, &patternDevice, FALSE);
+    if (m_pPattern->m_PatternType == CPDF_Pattern::TILING) {
+      bitmap_render.DrawTilingPattern(
+          static_cast<CPDF_TilingPattern*>(m_pPattern), m_pImageObject,
+          &patternDevice, FALSE);
     } else {
-      bitmap_render.DrawShadingPattern((CPDF_ShadingPattern*)m_pPattern,
-                                       m_pImageObject, &patternDevice, FALSE);
+      bitmap_render.DrawShadingPattern(
+          static_cast<CPDF_ShadingPattern*>(m_pPattern), m_pImageObject,
+          &patternDevice, FALSE);
     }
   }
   {
diff --git a/core/src/fpdfapi/fpdf_render/fpdf_render_pattern.cpp b/core/src/fpdfapi/fpdf_render/fpdf_render_pattern.cpp
index 718fa73..8cee178 100644
--- a/core/src/fpdfapi/fpdf_render/fpdf_render_pattern.cpp
+++ b/core/src/fpdfapi/fpdf_render/fpdf_render_pattern.cpp
@@ -1187,12 +1187,12 @@
   if (!pattern) {
     return;
   }
-  if (pattern->m_PatternType == PATTERN_TILING) {
-    DrawTilingPattern((CPDF_TilingPattern*)pattern, pPathObj, pObj2Device,
-                      bStroke);
+  if (pattern->m_PatternType == CPDF_Pattern::TILING) {
+    DrawTilingPattern(static_cast<CPDF_TilingPattern*>(pattern), pPathObj,
+                      pObj2Device, bStroke);
   } else {
-    DrawShadingPattern((CPDF_ShadingPattern*)pattern, pPathObj, pObj2Device,
-                       bStroke);
+    DrawShadingPattern(static_cast<CPDF_ShadingPattern*>(pattern), pPathObj,
+                       pObj2Device, bStroke);
   }
 }
 void CPDF_RenderStatus::ProcessPathPattern(CPDF_PathObject* pPathObj,