Cleanup GetZeroAreaPath method

This CL simplifies some of the logic in GetZeroAreaPath. It also removes the
side effect of resetting the object device matrix.

Change-Id: Id1e355bde811341c5ceab0331fbe64b1aed895d5
Reviewed-on: https://pdfium-review.googlesource.com/2712
Reviewed-by: Nicolás Peña <npm@chromium.org>
Commit-Queue: dsinclair <dsinclair@chromium.org>
diff --git a/core/fxge/cfx_pathdata.h b/core/fxge/cfx_pathdata.h
index 7d5e6e2..14a8d99 100644
--- a/core/fxge/cfx_pathdata.h
+++ b/core/fxge/cfx_pathdata.h
@@ -47,10 +47,11 @@
 
   void Transform(const CFX_Matrix* pMatrix);
   bool IsRect() const;
-  bool GetZeroAreaPath(CFX_PathData* NewPath,
-                       CFX_Matrix* pMatrix,
-                       bool& bThin,
-                       bool bAdjust) const;
+  bool GetZeroAreaPath(const CFX_Matrix* pMatrix,
+                       bool bAdjust,
+                       CFX_PathData* NewPath,
+                       bool* bThin,
+                       bool* setIdentity) const;
   bool IsRect(const CFX_Matrix* pMatrix, CFX_FloatRect* rect) const;
 
   void Append(const CFX_PathData& data);
diff --git a/core/fxge/ge/cfx_pathdata.cpp b/core/fxge/ge/cfx_pathdata.cpp
index 5ad3db4..11c421e 100644
--- a/core/fxge/ge/cfx_pathdata.cpp
+++ b/core/fxge/ge/cfx_pathdata.cpp
@@ -263,10 +263,12 @@
     pMatrix->TransformPoint(point.m_PointX, point.m_PointY);
 }
 
-bool CFX_PathData::GetZeroAreaPath(CFX_PathData* NewPath,
-                                   CFX_Matrix* pMatrix,
-                                   bool& bThin,
-                                   bool bAdjust) const {
+bool CFX_PathData::GetZeroAreaPath(const CFX_Matrix* pMatrix,
+                                   bool bAdjust,
+                                   CFX_PathData* NewPath,
+                                   bool* bThin,
+                                   bool* setIdentity) const {
+  *setIdentity = false;
   if (m_Points.size() < 3)
     return false;
 
@@ -275,45 +277,29 @@
       m_Points[2].m_Type == FXPT_TYPE::LineTo &&
       m_Points[0].m_PointX == m_Points[2].m_PointX &&
       m_Points[0].m_PointY == m_Points[2].m_PointY) {
-    if (bAdjust) {
-      if (pMatrix) {
-        FX_FLOAT x = m_Points[0].m_PointX;
-        FX_FLOAT y = m_Points[0].m_PointY;
-        pMatrix->TransformPoint(x, y);
+    for (size_t i = 0; i < 2; i++) {
+      FX_FLOAT x = m_Points[i].m_PointX;
+      FX_FLOAT y = m_Points[i].m_PointY;
+      if (bAdjust) {
+        if (pMatrix)
+          pMatrix->TransformPoint(x, y);
 
-        x = (int)x + 0.5f;
-        y = (int)y + 0.5f;
-        NewPath->AppendPoint(x, y, FXPT_TYPE::MoveTo, false);
-
-        x = m_Points[1].m_PointX;
-        y = m_Points[1].m_PointY;
-        pMatrix->TransformPoint(x, y);
-        x = (int)x + 0.5f;
-        y = (int)y + 0.5f;
-        NewPath->AppendPoint(x, y, FXPT_TYPE::LineTo, false);
-
-        pMatrix->SetIdentity();
-      } else {
-        FX_FLOAT x = (int)m_Points[0].m_PointX + 0.5f;
-        FX_FLOAT y = (int)m_Points[0].m_PointY + 0.5f;
-        NewPath->AppendPoint(x, y, FXPT_TYPE::MoveTo, false);
-
-        x = (int)m_Points[1].m_PointX + 0.5f,
-        y = (int)m_Points[1].m_PointY + 0.5f;
-        NewPath->AppendPoint(x, y, FXPT_TYPE::LineTo, false);
+        x = static_cast<int>(x) + 0.5f;
+        y = static_cast<int>(y) + 0.5f;
       }
-    } else {
-      NewPath->AppendPoint(m_Points[0].m_PointX, m_Points[0].m_PointY,
-                           FXPT_TYPE::MoveTo, false);
-      NewPath->AppendPoint(m_Points[1].m_PointX, m_Points[1].m_PointY,
-                           FXPT_TYPE::LineTo, false);
+      NewPath->AppendPoint(x, y, i == 0 ? FXPT_TYPE::MoveTo : FXPT_TYPE::LineTo,
+                           false);
     }
+    if (bAdjust && pMatrix)
+      *setIdentity = true;
+
     if (m_Points[0].m_PointX != m_Points[1].m_PointX &&
         m_Points[0].m_PointY != m_Points[1].m_PointY) {
-      bThin = true;
+      *bThin = true;
     }
     return true;
   }
+
   if (((m_Points.size() > 3) && (m_Points.size() % 2))) {
     int mid = m_Points.size() / 2;
     bool bZeroArea = false;
@@ -335,10 +321,11 @@
     }
     if (!bZeroArea) {
       NewPath->Append(&t_path, nullptr);
-      bThin = true;
+      *bThin = true;
       return true;
     }
   }
+
   int stratPoint = 0;
   int next = 0;
   for (size_t i = 0; i < m_Points.size(); i++) {
@@ -391,7 +378,7 @@
                                false);
           NewPath->AppendPoint(m_Points[i].m_PointX, m_Points[i].m_PointY,
                                FXPT_TYPE::LineTo, false);
-          bThin = true;
+          *bThin = true;
         }
       }
     } else if (point_type == FXPT_TYPE::BezierTo) {
@@ -399,9 +386,10 @@
       continue;
     }
   }
+
   size_t new_path_size = NewPath->GetPoints().size();
   if (m_Points.size() > 3 && new_path_size > 0)
-    bThin = true;
+    *bThin = true;
   return new_path_size != 0;
 }
 
diff --git a/core/fxge/ge/cfx_renderdevice.cpp b/core/fxge/ge/cfx_renderdevice.cpp
index b0be466..d2c677e 100644
--- a/core/fxge/ge/cfx_renderdevice.cpp
+++ b/core/fxge/ge/cfx_renderdevice.cpp
@@ -535,19 +535,25 @@
       !(fill_mode & FX_FILL_TEXT_MODE)) {
     CFX_PathData newPath;
     bool bThin = false;
-    if (pPathData->GetZeroAreaPath(&newPath, (CFX_Matrix*)pObject2Device, bThin,
-                                   !!m_pDeviceDriver->GetDriverType())) {
+    bool setIdentity = false;
+    if (pPathData->GetZeroAreaPath(pObject2Device,
+                                   !!m_pDeviceDriver->GetDriverType(), &newPath,
+                                   &bThin, &setIdentity)) {
       CFX_GraphStateData graphState;
       graphState.m_LineWidth = 0.0f;
+
       uint32_t strokecolor = fill_color;
       if (bThin)
         strokecolor = (((fill_alpha >> 2) << 24) | (strokecolor & 0x00ffffff));
-      CFX_Matrix* pMatrix = nullptr;
-      if (pObject2Device && !pObject2Device->IsIdentity())
-        pMatrix = (CFX_Matrix*)pObject2Device;
+
+      const CFX_Matrix* pMatrix = nullptr;
+      if (pObject2Device && !pObject2Device->IsIdentity() && !setIdentity)
+        pMatrix = pObject2Device;
+
       int smooth_path = FX_ZEROAREA_FILL;
       if (fill_mode & FXFILL_NOPATHSMOOTH)
         smooth_path |= FXFILL_NOPATHSMOOTH;
+
       m_pDeviceDriver->DrawPath(&newPath, pMatrix, &graphState, 0, strokecolor,
                                 smooth_path, blend_type);
     }