From 257900b6dae0fe8d82d48ecc91d92e5dbb3a28bf Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Fri, 9 Feb 2018 22:28:49 +0000 Subject: Use CPDF_Dictionary::SetRectFor() when appropriate. Change-Id: I4ced221fb5691927485deceb7002dac880c2c210 Reviewed-on: https://pdfium-review.googlesource.com/26110 Commit-Queue: Lei Zhang Reviewed-by: Henrique Nakashima --- fpdfsdk/fpdf_flatten.cpp | 18 ++++-------------- fpdfsdk/fpdf_ppo.cpp | 24 +++++------------------- fpdfsdk/fpdf_transformpage.cpp | 15 ++++----------- fpdfsdk/fpdfeditpage.cpp | 6 +----- fpdfsdk/fpdfedittext.cpp | 37 ++++++++++++++++--------------------- 5 files changed, 30 insertions(+), 70 deletions(-) diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp index 4d06693006..299318bd08 100644 --- a/fpdfsdk/fpdf_flatten.cpp +++ b/fpdfsdk/fpdf_flatten.cpp @@ -269,21 +269,11 @@ FPDF_EXPORT int FPDF_CALLCONV FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { else rcOriginalCB = rcOriginalMB; - if (!rcOriginalMB.IsEmpty()) { - CPDF_Array* pMediaBox = pPageDict->SetNewFor("MediaBox"); - pMediaBox->AddNew(rcOriginalMB.left); - pMediaBox->AddNew(rcOriginalMB.bottom); - pMediaBox->AddNew(rcOriginalMB.right); - pMediaBox->AddNew(rcOriginalMB.top); - } + if (!rcOriginalMB.IsEmpty()) + pPageDict->SetRectFor("MediaBox", rcOriginalMB); - if (!rcOriginalCB.IsEmpty()) { - CPDF_Array* pCropBox = pPageDict->SetNewFor("ArtBox"); - pCropBox->AddNew(rcOriginalCB.left); - pCropBox->AddNew(rcOriginalCB.bottom); - pCropBox->AddNew(rcOriginalCB.right); - pCropBox->AddNew(rcOriginalCB.top); - } + if (!rcOriginalCB.IsEmpty()) + pPageDict->SetRectFor("ArtBox", rcOriginalCB); CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources"); if (!pRes) diff --git a/fpdfsdk/fpdf_ppo.cpp b/fpdfsdk/fpdf_ppo.cpp index 5499ecc222..ac60a3af3a 100644 --- a/fpdfsdk/fpdf_ppo.cpp +++ b/fpdfsdk/fpdf_ppo.cpp @@ -490,11 +490,8 @@ bool CPDF_PageExporter::ExportPage(const std::vector& pageNums, pCurPageDict->SetFor("MediaBox", pInheritable->Clone()); } else { // Make the default size letter size (8.5"x11") - CPDF_Array* pArray = pCurPageDict->SetNewFor("MediaBox"); - pArray->AddNew(0); - pArray->AddNew(0); - pArray->AddNew(612); - pArray->AddNew(792); + static const CFX_FloatRect kDefaultLetterRect(0, 0, 612, 792); + pCurPageDict->SetRectFor("MediaBox", kDefaultLetterRect); } } @@ -547,9 +544,6 @@ class CPDF_NPageToOneExporter : public CPDF_PageOrganizer { // Map XObject's object name to it's object number. using XObjectNameNumberMap = std::map; - static void SetMediaBox(CPDF_Dictionary* pDestPageDict, - const CFX_SizeF& pagesize); - // Creates a xobject from the source page dictionary, and appends the // bsContent string with the xobject reference surrounded by the // transformation matrix. @@ -602,6 +596,8 @@ bool CPDF_NPageToOneExporter::ExportNPagesToOne( NupState nupState(destPageSize, numPagesOnXAxis, numPagesOnYAxis); size_t curpage = 0; + const CFX_FloatRect destPageRect(0, 0, destPageSize.width, + destPageSize.height); for (size_t outerPage = 0; outerPage < pageNums.size(); outerPage += numPagesPerSheet) { // Create a new page @@ -609,7 +605,7 @@ bool CPDF_NPageToOneExporter::ExportNPagesToOne( if (!pCurPageDict) return false; - SetMediaBox(pCurPageDict, destPageSize); + pCurPageDict->SetRectFor("MediaBox", destPageRect); ByteString bsContent; size_t innerPageMax = std::min(outerPage + numPagesPerSheet, pageNums.size()); @@ -635,16 +631,6 @@ bool CPDF_NPageToOneExporter::ExportNPagesToOne( return true; } -// static -void CPDF_NPageToOneExporter::SetMediaBox(CPDF_Dictionary* pDestPageDict, - const CFX_SizeF& pagesize) { - CPDF_Array* pArray = pDestPageDict->SetNewFor("MediaBox"); - pArray->AddNew(0); - pArray->AddNew(0); - pArray->AddNew(pagesize.width); - pArray->AddNew(pagesize.height); -} - void CPDF_NPageToOneExporter::AddSubPage( CPDF_Dictionary* pPageDict, const NupPageSettings& settings, diff --git a/fpdfsdk/fpdf_transformpage.cpp b/fpdfsdk/fpdf_transformpage.cpp index 565d90be4c..5a7e094b07 100644 --- a/fpdfsdk/fpdf_transformpage.cpp +++ b/fpdfsdk/fpdf_transformpage.cpp @@ -26,15 +26,8 @@ namespace { void SetBoundingBox(CPDF_Page* page, const ByteString& key, - float left, - float bottom, - float right, - float top) { - CPDF_Array* pBoundingBoxArray = page->m_pFormDict->SetNewFor(key); - pBoundingBoxArray->AddNew(left); - pBoundingBoxArray->AddNew(bottom); - pBoundingBoxArray->AddNew(right); - pBoundingBoxArray->AddNew(top); + const CFX_FloatRect& rect) { + page->m_pFormDict->SetRectFor(key, rect); } bool GetBoundingBox(CPDF_Page* page, @@ -69,7 +62,7 @@ FPDF_EXPORT void FPDF_CALLCONV FPDFPage_SetMediaBox(FPDF_PAGE page, if (!pPage) return; - SetBoundingBox(pPage, "MediaBox", left, bottom, right, top); + SetBoundingBox(pPage, "MediaBox", CFX_FloatRect(left, bottom, right, top)); } FPDF_EXPORT void FPDF_CALLCONV FPDFPage_SetCropBox(FPDF_PAGE page, @@ -81,7 +74,7 @@ FPDF_EXPORT void FPDF_CALLCONV FPDFPage_SetCropBox(FPDF_PAGE page, if (!pPage) return; - SetBoundingBox(pPage, "CropBox", left, bottom, right, top); + SetBoundingBox(pPage, "CropBox", CFX_FloatRect(left, bottom, right, top)); } FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_GetMediaBox(FPDF_PAGE page, diff --git a/fpdfsdk/fpdfeditpage.cpp b/fpdfsdk/fpdfeditpage.cpp index a032bf6607..ca2cf3fb6e 100644 --- a/fpdfsdk/fpdfeditpage.cpp +++ b/fpdfsdk/fpdfeditpage.cpp @@ -142,11 +142,7 @@ FPDF_EXPORT FPDF_PAGE FPDF_CALLCONV FPDFPage_New(FPDF_DOCUMENT document, if (!pPageDict) return nullptr; - CPDF_Array* pMediaBoxArray = pPageDict->SetNewFor("MediaBox"); - pMediaBoxArray->AddNew(0); - pMediaBoxArray->AddNew(0); - pMediaBoxArray->AddNew(static_cast(width)); - pMediaBoxArray->AddNew(static_cast(height)); + pPageDict->SetRectFor("MediaBox", CFX_FloatRect(0, 0, width, height)); pPageDict->SetNewFor("Rotate", 0); pPageDict->SetNewFor("Resources"); diff --git a/fpdfsdk/fpdfedittext.cpp b/fpdfsdk/fpdfedittext.cpp index 7bbb6a857c..460e852eaa 100644 --- a/fpdfsdk/fpdfedittext.cpp +++ b/fpdfsdk/fpdfedittext.cpp @@ -35,9 +35,9 @@ CPDF_Dictionary* LoadFontDesc(CPDF_Document* pDoc, const uint8_t* data, uint32_t size, int font_type) { - CPDF_Dictionary* fontDesc = pDoc->NewIndirect(); - fontDesc->SetNewFor("Type", "FontDescriptor"); - fontDesc->SetNewFor("FontName", font_name); + CPDF_Dictionary* pFontDesc = pDoc->NewIndirect(); + pFontDesc->SetNewFor("Type", "FontDescriptor"); + pFontDesc->SetNewFor("FontName", font_name); int flags = 0; if (FXFT_Is_Face_fixedwidth(pFont->GetFace())) flags |= FXFONT_FIXED_PITCH; @@ -51,25 +51,20 @@ CPDF_Dictionary* LoadFontDesc(CPDF_Document* pDoc, // TODO(npm): How do I know if a font is symbolic, script, allcap, smallcap flags |= FXFONT_NONSYMBOLIC; - fontDesc->SetNewFor("Flags", flags); + pFontDesc->SetNewFor("Flags", flags); FX_RECT bbox; pFont->GetBBox(bbox); - auto pBBox = pdfium::MakeUnique(); - pBBox->AddNew(bbox.left); - pBBox->AddNew(bbox.top); - pBBox->AddNew(bbox.right); - pBBox->AddNew(bbox.bottom); - fontDesc->SetFor("FontBBox", std::move(pBBox)); + pFontDesc->SetRectFor("FontBBox", CFX_FloatRect(bbox)); // TODO(npm): calculate italic angle correctly - fontDesc->SetNewFor("ItalicAngle", pFont->IsItalic() ? -12 : 0); + pFontDesc->SetNewFor("ItalicAngle", pFont->IsItalic() ? -12 : 0); - fontDesc->SetNewFor("Ascent", pFont->GetAscent()); - fontDesc->SetNewFor("Descent", pFont->GetDescent()); + pFontDesc->SetNewFor("Ascent", pFont->GetAscent()); + pFontDesc->SetNewFor("Descent", pFont->GetDescent()); // TODO(npm): calculate the capheight, stemV correctly - fontDesc->SetNewFor("CapHeight", pFont->GetAscent()); - fontDesc->SetNewFor("StemV", pFont->IsBold() ? 120 : 70); + pFontDesc->SetNewFor("CapHeight", pFont->GetAscent()); + pFontDesc->SetNewFor("StemV", pFont->IsBold() ? 120 : 70); CPDF_Stream* pStream = pDoc->NewIndirect(); pStream->SetData(data, size); @@ -79,8 +74,8 @@ CPDF_Dictionary* LoadFontDesc(CPDF_Document* pDoc, static_cast(size)); } ByteString fontFile = font_type == FPDF_FONT_TYPE1 ? "FontFile" : "FontFile2"; - fontDesc->SetNewFor(fontFile, pDoc, pStream->GetObjNum()); - return fontDesc; + pFontDesc->SetNewFor(fontFile, pDoc, pStream->GetObjNum()); + return pFontDesc; } const char ToUnicodeStart[] = @@ -276,11 +271,11 @@ void* LoadSimpleFont(CPDF_Document* pDoc, } fontDict->SetNewFor("LastChar", static_cast(currentChar)); fontDict->SetNewFor("Widths", pDoc, widthsArray->GetObjNum()); - CPDF_Dictionary* fontDesc = + CPDF_Dictionary* pFontDesc = LoadFontDesc(pDoc, name, pFont.get(), data, size, font_type); fontDict->SetNewFor("FontDescriptor", pDoc, - fontDesc->GetObjNum()); + pFontDesc->GetObjNum()); return pDoc->LoadFont(fontDict); } @@ -319,10 +314,10 @@ void* LoadCompositeFont(CPDF_Document* pDoc, pCIDFont->SetNewFor("CIDSystemInfo", pDoc, pCIDSystemInfo->GetObjNum()); - CPDF_Dictionary* fontDesc = + CPDF_Dictionary* pFontDesc = LoadFontDesc(pDoc, name, pFont.get(), data, size, font_type); pCIDFont->SetNewFor("FontDescriptor", pDoc, - fontDesc->GetObjNum()); + pFontDesc->GetObjNum()); uint32_t glyphIndex; uint32_t currentChar = FXFT_Get_First_Char(pFont->GetFace(), &glyphIndex); -- cgit v1.2.3