Make FPDFPage_SetRotation() take effect immediately.

CPDF_Page holds the page dimensions, but only calculates it once in its
constructor. Refactor that code into CPDF_Page::UpdateDimensions() and
call it from FPDFPage_SetRotation().

Update the test for FPDFPage_SetRotation() to expect this desired
behavior.

BUG=pdfium:1209

Change-Id: I8f1d15593b1a839be023233cc56e239a3db74e65
Reviewed-on: https://pdfium-review.googlesource.com/c/47635
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_page.cpp b/core/fpdfapi/page/cpdf_page.cpp
index 30e23eb..56874fb 100644
--- a/core/fpdfapi/page/cpdf_page.cpp
+++ b/core/fpdfapi/page/cpdf_page.cpp
@@ -37,39 +37,7 @@
   m_pResources = pPageAttr ? pPageAttr->GetDict() : nullptr;
   m_pPageResources = m_pResources;
 
-  CFX_FloatRect mediabox = GetBox(pdfium::page_object::kMediaBox);
-  if (mediabox.IsEmpty())
-    mediabox = CFX_FloatRect(0, 0, 612, 792);
-
-  m_BBox = GetBox(pdfium::page_object::kCropBox);
-  if (m_BBox.IsEmpty())
-    m_BBox = mediabox;
-  else
-    m_BBox.Intersect(mediabox);
-
-  m_PageSize.width = m_BBox.Width();
-  m_PageSize.height = m_BBox.Height();
-
-  int rotate = GetPageRotation();
-  if (rotate % 2)
-    std::swap(m_PageSize.width, m_PageSize.height);
-
-  switch (rotate) {
-    case 0:
-      m_PageMatrix = CFX_Matrix(1.0f, 0, 0, 1.0f, -m_BBox.left, -m_BBox.bottom);
-      break;
-    case 1:
-      m_PageMatrix =
-          CFX_Matrix(0, -1.0f, 1.0f, 0, -m_BBox.bottom, m_BBox.right);
-      break;
-    case 2:
-      m_PageMatrix = CFX_Matrix(-1.0f, 0, 0, -1.0f, m_BBox.right, m_BBox.top);
-      break;
-    case 3:
-      m_PageMatrix = CFX_Matrix(0, 1.0f, -1.0f, 0, m_BBox.top, -m_BBox.left);
-      break;
-  }
-
+  UpdateDimensions();
   m_Transparency.SetIsolated();
   LoadTransInfo();
 }
@@ -219,3 +187,36 @@
   int rotate = pRotate ? (pRotate->GetInteger() / 90) % 4 : 0;
   return (rotate < 0) ? (rotate + 4) : rotate;
 }
+
+void CPDF_Page::UpdateDimensions() {
+  CFX_FloatRect mediabox = GetBox(pdfium::page_object::kMediaBox);
+  if (mediabox.IsEmpty())
+    mediabox = CFX_FloatRect(0, 0, 612, 792);
+
+  m_BBox = GetBox(pdfium::page_object::kCropBox);
+  if (m_BBox.IsEmpty())
+    m_BBox = mediabox;
+  else
+    m_BBox.Intersect(mediabox);
+
+  m_PageSize.width = m_BBox.Width();
+  m_PageSize.height = m_BBox.Height();
+
+  switch (GetPageRotation()) {
+    case 0:
+      m_PageMatrix = CFX_Matrix(1.0f, 0, 0, 1.0f, -m_BBox.left, -m_BBox.bottom);
+      break;
+    case 1:
+      std::swap(m_PageSize.width, m_PageSize.height);
+      m_PageMatrix =
+          CFX_Matrix(0, -1.0f, 1.0f, 0, -m_BBox.bottom, m_BBox.right);
+      break;
+    case 2:
+      m_PageMatrix = CFX_Matrix(-1.0f, 0, 0, -1.0f, m_BBox.right, m_BBox.top);
+      break;
+    case 3:
+      std::swap(m_PageSize.width, m_PageSize.height);
+      m_PageMatrix = CFX_Matrix(0, 1.0f, -1.0f, 0, m_BBox.top, -m_BBox.left);
+      break;
+  }
+}
diff --git a/core/fpdfapi/page/cpdf_page.h b/core/fpdfapi/page/cpdf_page.h
index b641253..1253227 100644
--- a/core/fpdfapi/page/cpdf_page.h
+++ b/core/fpdfapi/page/cpdf_page.h
@@ -60,6 +60,7 @@
   CPDF_Document* GetPDFDocument() const { return m_pPDFDocument.Get(); }
   View* GetView() const { return m_pView.Get(); }
   void SetView(View* pView) { m_pView = pView; }
+  void UpdateDimensions();
 
  private:
   CPDF_Page(CPDF_Document* pDocument,
diff --git a/fpdfsdk/fpdf_editpage.cpp b/fpdfsdk/fpdf_editpage.cpp
index 0f4c02e..09b287d 100644
--- a/fpdfsdk/fpdf_editpage.cpp
+++ b/fpdfsdk/fpdf_editpage.cpp
@@ -677,6 +677,7 @@
   rotate %= 4;
   pPage->GetDict()->SetNewFor<CPDF_Number>(pdfium::page_object::kRotate,
                                            rotate * 90);
+  pPage->UpdateDimensions();
 }
 
 FPDF_BOOL FPDFPageObj_SetFillColor(FPDF_PAGEOBJECT page_object,
diff --git a/fpdfsdk/fpdf_editpage_embeddertest.cpp b/fpdfsdk/fpdf_editpage_embeddertest.cpp
index 6be553a..40359c4 100644
--- a/fpdfsdk/fpdf_editpage_embeddertest.cpp
+++ b/fpdfsdk/fpdf_editpage_embeddertest.cpp
@@ -32,15 +32,15 @@
 
     {
       // Render the page after rotation.
-      // Note that even though the rotation has changed, nothing else has.
-      // TODO(https://crbug.com/pdfium/1209): Fix tne rendering.
+      // Note that the change affects the rendering, as expected.
+      // It behaves just like the case below, rather than the case above.
       EXPECT_EQ(1, FPDFPage_GetRotation(page));
       const int page_width = static_cast<int>(FPDF_GetPageWidth(page));
       const int page_height = static_cast<int>(FPDF_GetPageHeight(page));
-      EXPECT_EQ(200, page_width);
-      EXPECT_EQ(300, page_height);
+      EXPECT_EQ(300, page_width);
+      EXPECT_EQ(200, page_height);
       ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
-      CompareBitmap(bitmap.get(), page_width, page_height, kOriginalMD5);
+      CompareBitmap(bitmap.get(), page_width, page_height, kRotatedMD5);
     }
 
     UnloadPage(page);