Add constants for PDF 1.7 spec, table 3.27.

BUG=pdfium:1049

Change-Id: Ie8bdb893d2af8d63420027a7ef95baf58cd97aa6
Reviewed-on: https://pdfium-review.googlesource.com/34691
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 5df974a..c6d7a05 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -302,6 +302,7 @@
 
 source_set("constants") {
   sources = [
+    "constants/page_object.h",
     "constants/stream_dict_common.h",
     "constants/transparency.h",
   ]
diff --git a/constants/page_object.h b/constants/page_object.h
new file mode 100644
index 0000000..b7f927b
--- /dev/null
+++ b/constants/page_object.h
@@ -0,0 +1,26 @@
+// Copyright 2018 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.
+
+#ifndef CONSTANTS_PAGE_OBJECT_H_
+#define CONSTANTS_PAGE_OBJECT_H_
+
+namespace pdfium {
+namespace page_object {
+
+// PDF 1.7 spec, table 3.27.
+// Entries in a page object.
+
+constexpr char kType[] = "Type";
+constexpr char kParent[] = "Parent";
+constexpr char kResources[] = "Resources";
+constexpr char kMediaBox[] = "MediaBox";
+constexpr char kCropBox[] = "CropBox";
+constexpr char kArtBox[] = "ArtBox";
+constexpr char kContents[] = "Contents";
+constexpr char kRotate[] = "Rotate";
+
+}  // namespace page_object
+}  // namespace pdfium
+
+#endif  // CONSTANTS_PAGE_OBJECT_H_
diff --git a/core/fpdfapi/page/cpdf_page.cpp b/core/fpdfapi/page/cpdf_page.cpp
index 1046351..e730aee 100644
--- a/core/fpdfapi/page/cpdf_page.cpp
+++ b/core/fpdfapi/page/cpdf_page.cpp
@@ -9,6 +9,7 @@
 #include <set>
 #include <utility>
 
+#include "constants/page_object.h"
 #include "core/fpdfapi/cpdf_pagerendercontext.h"
 #include "core/fpdfapi/page/cpdf_contentparser.h"
 #include "core/fpdfapi/page/cpdf_pageobject.h"
@@ -30,15 +31,15 @@
   if (!pPageDict)
     return;
 
-  CPDF_Object* pPageAttr = GetPageAttr("Resources");
+  CPDF_Object* pPageAttr = GetPageAttr(pdfium::page_object::kResources);
   m_pResources = pPageAttr ? pPageAttr->GetDict() : nullptr;
   m_pPageResources = m_pResources;
 
-  CFX_FloatRect mediabox = GetBox("MediaBox");
+  CFX_FloatRect mediabox = GetBox(pdfium::page_object::kMediaBox);
   if (mediabox.IsEmpty())
     mediabox = CFX_FloatRect(0, 0, 612, 792);
 
-  m_BBox = GetBox("CropBox");
+  m_BBox = GetBox(pdfium::page_object::kCropBox);
   if (m_BBox.IsEmpty())
     m_BBox = mediabox;
   else
@@ -102,7 +103,7 @@
     if (CPDF_Object* pObj = pPageDict->GetDirectObjectFor(name))
       return pObj;
 
-    pPageDict = pPageDict->GetDictFor("Parent");
+    pPageDict = pPageDict->GetDictFor(pdfium::page_object::kParent);
     if (!pPageDict || pdfium::ContainsKey(visited, pPageDict))
       break;
   }
@@ -194,7 +195,7 @@
 }
 
 int CPDF_Page::GetPageRotation() const {
-  CPDF_Object* pRotate = GetPageAttr("Rotate");
+  CPDF_Object* pRotate = GetPageAttr(pdfium::page_object::kRotate);
   int rotate = pRotate ? (pRotate->GetInteger() / 90) % 4 : 0;
   return (rotate < 0) ? (rotate + 4) : rotate;
 }
diff --git a/fpdfsdk/fpdf_editpage.cpp b/fpdfsdk/fpdf_editpage.cpp
index 0a8a745..d8b39b4 100644
--- a/fpdfsdk/fpdf_editpage.cpp
+++ b/fpdfsdk/fpdf_editpage.cpp
@@ -11,6 +11,7 @@
 #include <utility>
 #include <vector>
 
+#include "constants/page_object.h"
 #include "core/fpdfapi/edit/cpdf_pagecontentgenerator.h"
 #include "core/fpdfapi/page/cpdf_form.h"
 #include "core/fpdfapi/page/cpdf_formobject.h"
@@ -186,9 +187,10 @@
   if (!pPageDict)
     return nullptr;
 
-  pPageDict->SetRectFor("MediaBox", CFX_FloatRect(0, 0, width, height));
-  pPageDict->SetNewFor<CPDF_Number>("Rotate", 0);
-  pPageDict->SetNewFor<CPDF_Dictionary>("Resources");
+  pPageDict->SetRectFor(pdfium::page_object::kMediaBox,
+                        CFX_FloatRect(0, 0, width, height));
+  pPageDict->SetNewFor<CPDF_Number>(pdfium::page_object::kRotate, 0);
+  pPageDict->SetNewFor<CPDF_Dictionary>(pdfium::page_object::kResources);
 
 #ifdef PDF_ENABLE_XFA
   auto* pContext = static_cast<CPDFXFA_Context*>(pDoc->GetExtension());
@@ -492,7 +494,8 @@
     return;
 
   rotate %= 4;
-  pPage->GetDict()->SetNewFor<CPDF_Number>("Rotate", rotate * 90);
+  pPage->GetDict()->SetNewFor<CPDF_Number>(pdfium::page_object::kRotate,
+                                           rotate * 90);
 }
 
 FPDF_BOOL FPDFPageObj_SetFillColor(FPDF_PAGEOBJECT page_object,
diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp
index d32c1f1..d35cfe3 100644
--- a/fpdfsdk/fpdf_flatten.cpp
+++ b/fpdfsdk/fpdf_flatten.cpp
@@ -11,6 +11,7 @@
 #include <utility>
 #include <vector>
 
+#include "constants/page_object.h"
 #include "core/fpdfapi/page/cpdf_page.h"
 #include "core/fpdfapi/page/cpdf_pageobject.h"
 #include "core/fpdfapi/parser/cpdf_array.h"
@@ -56,7 +57,7 @@
     rc.right = pPageObject->m_Right;
     rc.bottom = pPageObject->m_Bottom;
     rc.top = pPageObject->m_Top;
-    if (IsValidRect(rc, pDict->GetRectFor("MediaBox")))
+    if (IsValidRect(rc, pDict->GetRectFor(pdfium::page_object::kMediaBox)))
       pRectArray->push_back(rc);
   }
 }
@@ -73,7 +74,7 @@
   else if (pStream->KeyExist("BBox"))
     rect = pStream->GetRectFor("BBox");
 
-  if (IsValidRect(rect, pPageDic->GetRectFor("MediaBox")))
+  if (IsValidRect(rect, pPageDic->GetRectFor(pdfium::page_object::kMediaBox)))
     pRectArray->push_back(rect);
 
   pObjectArray->push_back(pStream);
@@ -183,18 +184,20 @@
                      CPDF_Dictionary* pPage,
                      CPDF_Document* pDocument) {
   CPDF_Array* pContentsArray = nullptr;
-  CPDF_Stream* pContentsStream = pPage->GetStreamFor("Contents");
+  CPDF_Stream* pContentsStream =
+      pPage->GetStreamFor(pdfium::page_object::kContents);
   if (!pContentsStream) {
-    pContentsArray = pPage->GetArrayFor("Contents");
+    pContentsArray = pPage->GetArrayFor(pdfium::page_object::kContents);
     if (!pContentsArray) {
       if (!key.IsEmpty()) {
         pPage->SetNewFor<CPDF_Reference>(
-            "Contents", pDocument, NewIndirectContentsStream(key, pDocument));
+            pdfium::page_object::kContents, pDocument,
+            NewIndirectContentsStream(key, pDocument));
       }
       return;
     }
   }
-  pPage->ConvertToIndirectObjectFor("Contents", pDocument);
+  pPage->ConvertToIndirectObjectFor(pdfium::page_object::kContents, pDocument);
   if (!pContentsArray) {
     pContentsArray = pDocument->NewIndirect<CPDF_Array>();
     auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pContentsStream);
@@ -206,7 +209,7 @@
                                             sStream.GetLength());
     pContentsArray->AddNew<CPDF_Reference>(pDocument,
                                            pContentsStream->GetObjNum());
-    pPage->SetNewFor<CPDF_Reference>("Contents", pDocument,
+    pPage->SetNewFor<CPDF_Reference>(pdfium::page_object::kContents, pDocument,
                                      pContentsArray->GetObjNum());
   }
   if (!key.IsEmpty()) {
@@ -253,9 +256,10 @@
 
   CFX_FloatRect rcOriginalCB;
   CFX_FloatRect rcMerger = CalculateRect(&RectArray);
-  CFX_FloatRect rcOriginalMB = pPageDict->GetRectFor("MediaBox");
-  if (pPageDict->KeyExist("CropBox"))
-    rcOriginalMB = pPageDict->GetRectFor("CropBox");
+  CFX_FloatRect rcOriginalMB =
+      pPageDict->GetRectFor(pdfium::page_object::kMediaBox);
+  if (pPageDict->KeyExist(pdfium::page_object::kCropBox))
+    rcOriginalMB = pPageDict->GetRectFor(pdfium::page_object::kCropBox);
 
   if (rcOriginalMB.IsEmpty())
     rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f);
@@ -270,14 +274,17 @@
     rcOriginalCB = rcOriginalMB;
 
   if (!rcOriginalMB.IsEmpty())
-    pPageDict->SetRectFor("MediaBox", rcOriginalMB);
+    pPageDict->SetRectFor(pdfium::page_object::kMediaBox, rcOriginalMB);
 
   if (!rcOriginalCB.IsEmpty())
     pPageDict->SetRectFor("ArtBox", rcOriginalCB);
 
-  CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources");
-  if (!pRes)
-    pRes = pPageDict->SetNewFor<CPDF_Dictionary>("Resources");
+  CPDF_Dictionary* pRes =
+      pPageDict->GetDictFor(pdfium::page_object::kResources);
+  if (!pRes) {
+    pRes =
+        pPageDict->SetNewFor<CPDF_Dictionary>(pdfium::page_object::kResources);
+  }
 
   CPDF_Stream* pNewXObject = pDocument->NewIndirect<CPDF_Stream>(
       nullptr, 0,
@@ -311,7 +318,7 @@
     pNewOXbjectDic->SetNewFor<CPDF_Name>("Type", "XObject");
     pNewOXbjectDic->SetNewFor<CPDF_Name>("Subtype", "Form");
     pNewOXbjectDic->SetNewFor<CPDF_Number>("FormType", 1);
-    CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox");
+    CFX_FloatRect rcBBox = pPageDict->GetRectFor(pdfium::page_object::kArtBox);
     pNewOXbjectDic->SetRectFor("BBox", rcBBox);
   }
 
diff --git a/fpdfsdk/fpdf_ppo.cpp b/fpdfsdk/fpdf_ppo.cpp
index b730424..c4bd51e 100644
--- a/fpdfsdk/fpdf_ppo.cpp
+++ b/fpdfsdk/fpdf_ppo.cpp
@@ -12,6 +12,7 @@
 #include <utility>
 #include <vector>
 
+#include "constants/page_object.h"
 #include "core/fpdfapi/page/cpdf_page.h"
 #include "core/fpdfapi/page/cpdf_pageobject.h"
 #include "core/fpdfapi/parser/cpdf_array.h"
@@ -138,17 +139,20 @@
                                              const ByteString& bsSrcTag) {
   if (!pDict || bsSrcTag.IsEmpty())
     return nullptr;
-  if (!pDict->KeyExist("Parent") || !pDict->KeyExist("Type"))
+  if (!pDict->KeyExist(pdfium::page_object::kParent) ||
+      !pDict->KeyExist(pdfium::page_object::kType)) {
     return nullptr;
+  }
 
-  const CPDF_Object* pType = pDict->GetObjectFor("Type")->GetDirect();
+  const CPDF_Object* pType =
+      pDict->GetObjectFor(pdfium::page_object::kType)->GetDirect();
   if (!ToName(pType))
     return nullptr;
   if (pType->GetString().Compare("Page"))
     return nullptr;
 
-  const CPDF_Dictionary* pp =
-      ToDictionary(pDict->GetObjectFor("Parent")->GetDirect());
+  const CPDF_Dictionary* pp = ToDictionary(
+      pDict->GetObjectFor(pdfium::page_object::kParent)->GetDirect());
   if (!pp)
     return nullptr;
 
@@ -158,16 +162,17 @@
   while (pp) {
     if (pp->KeyExist(bsSrcTag))
       return pp->GetObjectFor(bsSrcTag);
-    if (!pp->KeyExist("Parent"))
+    if (!pp->KeyExist(pdfium::page_object::kParent))
       break;
-    pp = ToDictionary(pp->GetObjectFor("Parent")->GetDirect());
+    pp = ToDictionary(
+        pp->GetObjectFor(pdfium::page_object::kParent)->GetDirect());
   }
   return nullptr;
 }
 
 CFX_FloatRect GetMediaBox(const CPDF_Dictionary* pPageDict) {
   const CPDF_Object* pMediaBox =
-      PageDictGetInheritableTag(pPageDict, "MediaBox");
+      PageDictGetInheritableTag(pPageDict, pdfium::page_object::kMediaBox);
   const CPDF_Array* pArray = ToArray(pMediaBox->GetDirect());
   if (!pArray)
     return CFX_FloatRect();
@@ -175,8 +180,8 @@
 }
 
 CFX_FloatRect GetCropBox(const CPDF_Dictionary* pPageDict) {
-  if (pPageDict->KeyExist("CropBox"))
-    return pPageDict->GetRectFor("CropBox");
+  if (pPageDict->KeyExist(pdfium::page_object::kCropBox))
+    return pPageDict->GetRectFor(pdfium::page_object::kCropBox);
   return GetMediaBox(pPageDict);
 }
 
@@ -188,7 +193,9 @@
 
 const CPDF_Object* GetPageOrganizerPageContent(
     const CPDF_Dictionary* pPageDict) {
-  return pPageDict ? pPageDict->GetDirectObjectFor("Contents") : nullptr;
+  return pPageDict
+             ? pPageDict->GetDirectObjectFor(pdfium::page_object::kContents)
+             : nullptr;
 }
 
 bool CopyInheritable(CPDF_Dictionary* pDestPageDict,
@@ -473,8 +480,10 @@
     // Clone the page dictionary
     for (const auto& it : *pSrcPageDict) {
       const ByteString& cbSrcKeyStr = it.first;
-      if (cbSrcKeyStr == "Type" || cbSrcKeyStr == "Parent")
+      if (cbSrcKeyStr == pdfium::page_object::kType ||
+          cbSrcKeyStr == pdfium::page_object::kParent) {
         continue;
+      }
 
       CPDF_Object* pObj = it.second.get();
       pDestPageDict->SetFor(cbSrcKeyStr, pObj->Clone());
@@ -484,30 +493,35 @@
     // Even though some entries are required by the PDF spec, there exist
     // PDFs that omit them. Set some defaults in this case.
     // 1 MediaBox - required
-    if (!CopyInheritable(pDestPageDict, pSrcPageDict, "MediaBox")) {
+    if (!CopyInheritable(pDestPageDict, pSrcPageDict,
+                         pdfium::page_object::kMediaBox)) {
       // Search for "CropBox" in the source page dictionary.
       // If it does not exist, use the default letter size.
-      const CPDF_Object* pInheritable =
-          PageDictGetInheritableTag(pSrcPageDict, "CropBox");
+      const CPDF_Object* pInheritable = PageDictGetInheritableTag(
+          pSrcPageDict, pdfium::page_object::kCropBox);
       if (pInheritable) {
-        pDestPageDict->SetFor("MediaBox", pInheritable->Clone());
+        pDestPageDict->SetFor(pdfium::page_object::kMediaBox,
+                              pInheritable->Clone());
       } else {
         // Make the default size letter size (8.5"x11")
         static const CFX_FloatRect kDefaultLetterRect(0, 0, 612, 792);
-        pDestPageDict->SetRectFor("MediaBox", kDefaultLetterRect);
+        pDestPageDict->SetRectFor(pdfium::page_object::kMediaBox,
+                                  kDefaultLetterRect);
       }
     }
 
     // 2 Resources - required
-    if (!CopyInheritable(pDestPageDict, pSrcPageDict, "Resources")) {
+    if (!CopyInheritable(pDestPageDict, pSrcPageDict,
+                         pdfium::page_object::kResources)) {
       // Use a default empty resources if it does not exist.
-      pDestPageDict->SetNewFor<CPDF_Dictionary>("Resources");
+      pDestPageDict->SetNewFor<CPDF_Dictionary>(
+          pdfium::page_object::kResources);
     }
 
     // 3 CropBox - optional
-    CopyInheritable(pDestPageDict, pSrcPageDict, "CropBox");
+    CopyInheritable(pDestPageDict, pSrcPageDict, pdfium::page_object::kCropBox);
     // 4 Rotate - optional
-    CopyInheritable(pDestPageDict, pSrcPageDict, "Rotate");
+    CopyInheritable(pDestPageDict, pSrcPageDict, pdfium::page_object::kRotate);
 
     // Update the reference
     uint32_t dwOldPageObj = pSrcPageDict->GetObjNum();
@@ -607,7 +621,7 @@
     if (!pDestPageDict)
       return false;
 
-    pDestPageDict->SetRectFor("MediaBox", destPageRect);
+    pDestPageDict->SetRectFor(pdfium::page_object::kMediaBox, destPageRect);
     ByteString bsContent;
     size_t innerPageMax =
         std::min(outerPage + numPagesPerSheet, pageNums.size());
@@ -722,9 +736,12 @@
     const XObjectNameNumberMap& xObjNameNumberMap) {
   ASSERT(pDestPageDict);
 
-  CPDF_Dictionary* pRes = pDestPageDict->GetDictFor("Resources");
-  if (!pRes)
-    pRes = pDestPageDict->SetNewFor<CPDF_Dictionary>("Resources");
+  CPDF_Dictionary* pRes =
+      pDestPageDict->GetDictFor(pdfium::page_object::kResources);
+  if (!pRes) {
+    pRes = pDestPageDict->SetNewFor<CPDF_Dictionary>(
+        pdfium::page_object::kResources);
+  }
 
   CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject");
   if (!pPageXObject)
@@ -737,8 +754,8 @@
   CPDF_Stream* pStream =
       dest()->NewIndirect<CPDF_Stream>(nullptr, 0, std::move(pDict));
   pStream->SetData(bsContent.raw_str(), bsContent.GetLength());
-  pDestPageDict->SetNewFor<CPDF_Reference>("Contents", dest(),
-                                           pStream->GetObjNum());
+  pDestPageDict->SetNewFor<CPDF_Reference>(pdfium::page_object::kContents,
+                                           dest(), pStream->GetObjNum());
 }
 
 }  // namespace
diff --git a/fpdfsdk/fpdf_transformpage.cpp b/fpdfsdk/fpdf_transformpage.cpp
index 210c6b6..af6ac79 100644
--- a/fpdfsdk/fpdf_transformpage.cpp
+++ b/fpdfsdk/fpdf_transformpage.cpp
@@ -10,6 +10,7 @@
 #include <sstream>
 #include <vector>
 
+#include "constants/page_object.h"
 #include "core/fpdfapi/page/cpdf_clippath.h"
 #include "core/fpdfapi/page/cpdf_page.h"
 #include "core/fpdfapi/page/cpdf_pageobject.h"
@@ -51,7 +52,9 @@
 }
 
 CPDF_Object* GetPageContent(CPDF_Dictionary* pPageDict) {
-  return pPageDict ? pPageDict->GetDirectObjectFor("Contents") : nullptr;
+  return pPageDict
+             ? pPageDict->GetDirectObjectFor(pdfium::page_object::kContents)
+             : nullptr;
 }
 
 }  // namespace
@@ -65,7 +68,8 @@
   if (!pPage)
     return;
 
-  SetBoundingBox(pPage, "MediaBox", CFX_FloatRect(left, bottom, right, top));
+  SetBoundingBox(pPage, pdfium::page_object::kMediaBox,
+                 CFX_FloatRect(left, bottom, right, top));
 }
 
 FPDF_EXPORT void FPDF_CALLCONV FPDFPage_SetCropBox(FPDF_PAGE page,
@@ -77,7 +81,8 @@
   if (!pPage)
     return;
 
-  SetBoundingBox(pPage, "CropBox", CFX_FloatRect(left, bottom, right, top));
+  SetBoundingBox(pPage, pdfium::page_object::kCropBox,
+                 CFX_FloatRect(left, bottom, right, top));
 }
 
 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_GetMediaBox(FPDF_PAGE page,
@@ -86,7 +91,8 @@
                                                          float* right,
                                                          float* top) {
   CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
-  return pPage && GetBoundingBox(pPage, "MediaBox", left, bottom, right, top);
+  return pPage && GetBoundingBox(pPage, pdfium::page_object::kMediaBox, left,
+                                 bottom, right, top);
 }
 
 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_GetCropBox(FPDF_PAGE page,
@@ -95,7 +101,8 @@
                                                         float* right,
                                                         float* top) {
   CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
-  return pPage && GetBoundingBox(pPage, "CropBox", left, bottom, right, top);
+  return pPage && GetBoundingBox(pPage, pdfium::page_object::kCropBox, left,
+                                 bottom, right, top);
 }
 
 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
@@ -151,12 +158,13 @@
     pContentArray->AddNew<CPDF_Reference>(pDoc, pStream->GetObjNum());
     pContentArray->AddNew<CPDF_Reference>(pDoc, pContentObj->GetObjNum());
     pContentArray->AddNew<CPDF_Reference>(pDoc, pEndStream->GetObjNum());
-    pPageDict->SetNewFor<CPDF_Reference>("Contents", pDoc,
+    pPageDict->SetNewFor<CPDF_Reference>(pdfium::page_object::kContents, pDoc,
                                          pContentArray->GetObjNum());
   }
 
   // Need to transform the patterns as well.
-  CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources");
+  CPDF_Dictionary* pRes =
+      pPageDict->GetDictFor(pdfium::page_object::kResources);
   if (pRes) {
     CPDF_Dictionary* pPattenDict = pRes->GetDictFor("Pattern");
     if (pPattenDict) {
@@ -302,7 +310,7 @@
     CPDF_Array* pContentArray = pDoc->NewIndirect<CPDF_Array>();
     pContentArray->AddNew<CPDF_Reference>(pDoc, pStream->GetObjNum());
     pContentArray->AddNew<CPDF_Reference>(pDoc, pContentObj->GetObjNum());
-    pPageDict->SetNewFor<CPDF_Reference>("Contents", pDoc,
+    pPageDict->SetNewFor<CPDF_Reference>(pdfium::page_object::kContents, pDoc,
                                          pContentArray->GetObjNum());
   }
 }