From fc615c63d34037896963f636e4b5d9e05efd41de Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Fri, 8 Jun 2018 20:40:15 +0000 Subject: 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 Commit-Queue: Lei Zhang --- BUILD.gn | 1 + constants/page_object.h | 26 ++++++++++++++++ core/fpdfapi/page/cpdf_page.cpp | 11 ++++--- fpdfsdk/fpdf_editpage.cpp | 11 ++++--- fpdfsdk/fpdf_flatten.cpp | 37 +++++++++++++--------- fpdfsdk/fpdf_ppo.cpp | 69 +++++++++++++++++++++++++---------------- fpdfsdk/fpdf_transformpage.cpp | 24 +++++++++----- 7 files changed, 121 insertions(+), 58 deletions(-) create mode 100644 constants/page_object.h diff --git a/BUILD.gn b/BUILD.gn index 5df974aff8..c6d7a05a46 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -302,6 +302,7 @@ visibility = [ 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 0000000000..b7f927b56f --- /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 10463510a3..e730aee668 100644 --- a/core/fpdfapi/page/cpdf_page.cpp +++ b/core/fpdfapi/page/cpdf_page.cpp @@ -9,6 +9,7 @@ #include #include +#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 @@ CPDF_Page::CPDF_Page(CPDF_Document* pDocument, 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 @@ CPDF_Object* CPDF_Page::GetPageAttr(const ByteString& name) const { 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 @@ CFX_Matrix CPDF_Page::GetDisplayMatrix(const FX_RECT& rect, int iRotate) const { } 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 0a8a74577b..d8b39b4d08 100644 --- a/fpdfsdk/fpdf_editpage.cpp +++ b/fpdfsdk/fpdf_editpage.cpp @@ -11,6 +11,7 @@ #include #include +#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 @@ FPDF_EXPORT FPDF_PAGE FPDF_CALLCONV FPDFPage_New(FPDF_DOCUMENT document, if (!pPageDict) return nullptr; - pPageDict->SetRectFor("MediaBox", CFX_FloatRect(0, 0, width, height)); - pPageDict->SetNewFor("Rotate", 0); - pPageDict->SetNewFor("Resources"); + pPageDict->SetRectFor(pdfium::page_object::kMediaBox, + CFX_FloatRect(0, 0, width, height)); + pPageDict->SetNewFor(pdfium::page_object::kRotate, 0); + pPageDict->SetNewFor(pdfium::page_object::kResources); #ifdef PDF_ENABLE_XFA auto* pContext = static_cast(pDoc->GetExtension()); @@ -492,7 +494,8 @@ FPDF_EXPORT void FPDF_CALLCONV FPDFPage_SetRotation(FPDF_PAGE page, return; rotate %= 4; - pPage->GetDict()->SetNewFor("Rotate", rotate * 90); + pPage->GetDict()->SetNewFor(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 d32c1f15cf..d35cfe370d 100644 --- a/fpdfsdk/fpdf_flatten.cpp +++ b/fpdfsdk/fpdf_flatten.cpp @@ -11,6 +11,7 @@ #include #include +#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 @@ void GetContentsRect(CPDF_Document* pDoc, 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 @@ void ParserStream(CPDF_Dictionary* pPageDic, 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 @@ void SetPageContents(const ByteString& key, 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( - "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(); auto pAcc = pdfium::MakeRetain(pContentsStream); @@ -206,7 +209,7 @@ void SetPageContents(const ByteString& key, sStream.GetLength()); pContentsArray->AddNew(pDocument, pContentsStream->GetObjNum()); - pPage->SetNewFor("Contents", pDocument, + pPage->SetNewFor(pdfium::page_object::kContents, pDocument, pContentsArray->GetObjNum()); } if (!key.IsEmpty()) { @@ -253,9 +256,10 @@ FPDF_EXPORT int FPDF_CALLCONV FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { 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 @@ FPDF_EXPORT int FPDF_CALLCONV FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { 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("Resources"); + CPDF_Dictionary* pRes = + pPageDict->GetDictFor(pdfium::page_object::kResources); + if (!pRes) { + pRes = + pPageDict->SetNewFor(pdfium::page_object::kResources); + } CPDF_Stream* pNewXObject = pDocument->NewIndirect( nullptr, 0, @@ -311,7 +318,7 @@ FPDF_EXPORT int FPDF_CALLCONV FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { pNewOXbjectDic->SetNewFor("Type", "XObject"); pNewOXbjectDic->SetNewFor("Subtype", "Form"); pNewOXbjectDic->SetNewFor("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 b73042466b..c4bd51e603 100644 --- a/fpdfsdk/fpdf_ppo.cpp +++ b/fpdfsdk/fpdf_ppo.cpp @@ -12,6 +12,7 @@ #include #include +#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 CPDF_Object* PageDictGetInheritableTag(const CPDF_Dictionary* pDict, 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 @@ const CPDF_Object* PageDictGetInheritableTag(const CPDF_Dictionary* pDict, 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 GetMediaBox(const CPDF_Dictionary* pPageDict) { } 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 @@ CFX_FloatRect GetTrimBox(const CPDF_Dictionary* pPageDict) { 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 @@ bool CPDF_PageExporter::ExportPage(const std::vector& pageNums, // 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 @@ bool CPDF_PageExporter::ExportPage(const std::vector& pageNums, // 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("Resources"); + pDestPageDict->SetNewFor( + 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 @@ bool CPDF_NPageToOneExporter::ExportNPagesToOne( 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 @@ void CPDF_NPageToOneExporter::FinishPage( const XObjectNameNumberMap& xObjNameNumberMap) { ASSERT(pDestPageDict); - CPDF_Dictionary* pRes = pDestPageDict->GetDictFor("Resources"); - if (!pRes) - pRes = pDestPageDict->SetNewFor("Resources"); + CPDF_Dictionary* pRes = + pDestPageDict->GetDictFor(pdfium::page_object::kResources); + if (!pRes) { + pRes = pDestPageDict->SetNewFor( + pdfium::page_object::kResources); + } CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject"); if (!pPageXObject) @@ -737,8 +754,8 @@ void CPDF_NPageToOneExporter::FinishPage( CPDF_Stream* pStream = dest()->NewIndirect(nullptr, 0, std::move(pDict)); pStream->SetData(bsContent.raw_str(), bsContent.GetLength()); - pDestPageDict->SetNewFor("Contents", dest(), - pStream->GetObjNum()); + pDestPageDict->SetNewFor(pdfium::page_object::kContents, + dest(), pStream->GetObjNum()); } } // namespace diff --git a/fpdfsdk/fpdf_transformpage.cpp b/fpdfsdk/fpdf_transformpage.cpp index 210c6b6fde..af6ac792fb 100644 --- a/fpdfsdk/fpdf_transformpage.cpp +++ b/fpdfsdk/fpdf_transformpage.cpp @@ -10,6 +10,7 @@ #include #include +#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 @@ bool GetBoundingBox(CPDF_Page* page, } 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 @@ FPDF_EXPORT void FPDF_CALLCONV FPDFPage_SetMediaBox(FPDF_PAGE page, 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 @@ FPDF_EXPORT void FPDF_CALLCONV FPDFPage_SetCropBox(FPDF_PAGE page, 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 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_GetMediaBox(FPDF_PAGE page, 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 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_GetCropBox(FPDF_PAGE page, 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 @@ FPDFPage_TransFormWithClip(FPDF_PAGE page, pContentArray->AddNew(pDoc, pStream->GetObjNum()); pContentArray->AddNew(pDoc, pContentObj->GetObjNum()); pContentArray->AddNew(pDoc, pEndStream->GetObjNum()); - pPageDict->SetNewFor("Contents", pDoc, + pPageDict->SetNewFor(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 @@ FPDF_EXPORT void FPDF_CALLCONV FPDFPage_InsertClipPath(FPDF_PAGE page, CPDF_Array* pContentArray = pDoc->NewIndirect(); pContentArray->AddNew(pDoc, pStream->GetObjNum()); pContentArray->AddNew(pDoc, pContentObj->GetObjNum()); - pPageDict->SetNewFor("Contents", pDoc, + pPageDict->SetNewFor(pdfium::page_object::kContents, pDoc, pContentArray->GetObjNum()); } } -- cgit v1.2.3