From aaed698534284ca45a986ae7344246cffbc6faa9 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Thu, 22 Mar 2018 18:39:05 +0000 Subject: Consolidate QuadPoints reading code in fpdfsdk. Also fix nits in QuadPoints code in cpdf_annot.cpp. Change-Id: I7852b673d3dca906e6d250cb3cfa305f8ea7e742 Reviewed-on: https://pdfium-review.googlesource.com/28893 Reviewed-by: Henrique Nakashima Commit-Queue: Lei Zhang --- core/fpdfdoc/cpdf_annot.cpp | 19 +++++++------- fpdfsdk/fpdfannot.cpp | 21 +++------------- fpdfsdk/fpdfdoc.cpp | 60 ++++++++++++++++++++++++--------------------- fpdfsdk/fsdk_define.h | 6 +++++ 4 files changed, 51 insertions(+), 55 deletions(-) diff --git a/core/fpdfdoc/cpdf_annot.cpp b/core/fpdfdoc/cpdf_annot.cpp index 91cdb12d9f..42bda6e319 100644 --- a/core/fpdfdoc/cpdf_annot.cpp +++ b/core/fpdfdoc/cpdf_annot.cpp @@ -205,9 +205,12 @@ CPDF_Form* CPDF_Annot::GetAPForm(const CPDF_Page* pPage, AppearanceMode mode) { return pResult; } -// Static. +// static CFX_FloatRect CPDF_Annot::RectFromQuadPointsArray(const CPDF_Array* pArray, size_t nIndex) { + ASSERT(pArray); + ASSERT(nIndex < pArray->GetCount() / 8); + // QuadPoints are defined with 4 pairs of numbers // ([ pair0, pair1, pair2, pair3 ]), where // pair0 = top_left @@ -224,7 +227,7 @@ CFX_FloatRect CPDF_Annot::RectFromQuadPointsArray(const CPDF_Array* pArray, pArray->GetNumberAt(2 + nIndex * 8), pArray->GetNumberAt(3 + nIndex * 8)); } -// Static. +// static CFX_FloatRect CPDF_Annot::BoundingRectFromQuadPoints( CPDF_Dictionary* pAnnotDict) { CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints"); @@ -240,23 +243,21 @@ CFX_FloatRect CPDF_Annot::BoundingRectFromQuadPoints( return ret; } -// Static. +// static CFX_FloatRect CPDF_Annot::RectFromQuadPoints(CPDF_Dictionary* pAnnotDict, size_t nIndex) { CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints"); - if (!pArray) return CFX_FloatRect(); - return RectFromQuadPointsArray(pArray, nIndex); } -// Static. +// static bool CPDF_Annot::IsAnnotationHidden(CPDF_Dictionary* pAnnotDict) { return !!(pAnnotDict->GetIntegerFor("F") & ANNOTFLAG_HIDDEN); } -// Static. +// static CPDF_Annot::Subtype CPDF_Annot::StringToAnnotSubtype( const ByteString& sSubtype) { if (sSubtype == "Text") @@ -316,7 +317,7 @@ CPDF_Annot::Subtype CPDF_Annot::StringToAnnotSubtype( return CPDF_Annot::Subtype::UNKNOWN; } -// Static. +// static ByteString CPDF_Annot::AnnotSubtypeToString(CPDF_Annot::Subtype nSubtype) { if (nSubtype == CPDF_Annot::Subtype::TEXT) return "Text"; @@ -375,7 +376,7 @@ ByteString CPDF_Annot::AnnotSubtypeToString(CPDF_Annot::Subtype nSubtype) { return ""; } -// Static. +// static size_t CPDF_Annot::QuadPointCount(const CPDF_Array* pArray) { return pArray->GetCount() / 8; } diff --git a/fpdfsdk/fpdfannot.cpp b/fpdfsdk/fpdfannot.cpp index d2a15ce8c0..b5dccaade1 100644 --- a/fpdfsdk/fpdfannot.cpp +++ b/fpdfsdk/fpdfannot.cpp @@ -627,24 +627,9 @@ FPDFAnnot_GetAttachmentPoints(FPDF_ANNOTATION annot, if (!annot || !quad_points || !FPDFAnnot_HasAttachmentPoints(annot)) return false; - CPDF_Dictionary* pAnnotDict = - CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict(); - if (!pAnnotDict) - return false; - - CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints"); - if (!pArray) - return false; - - quad_points->x1 = pArray->GetNumberAt(0); - quad_points->y1 = pArray->GetNumberAt(1); - quad_points->x2 = pArray->GetNumberAt(2); - quad_points->y2 = pArray->GetNumberAt(3); - quad_points->x3 = pArray->GetNumberAt(4); - quad_points->y3 = pArray->GetNumberAt(5); - quad_points->x4 = pArray->GetNumberAt(6); - quad_points->y4 = pArray->GetNumberAt(7); - return true; + return GetQuadPointsFromDictionary( + CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict(), 0, + quad_points); } FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_SetRect(FPDF_ANNOTATION annot, diff --git a/fpdfsdk/fpdfdoc.cpp b/fpdfsdk/fpdfdoc.cpp index befdc4d7a3..774dc28e8c 100644 --- a/fpdfsdk/fpdfdoc.cpp +++ b/fpdfsdk/fpdfdoc.cpp @@ -78,6 +78,31 @@ CPDF_Dictionary* CPDFDictionaryFromFPDFLink(FPDF_LINK link) { return ToDictionary(static_cast(link)); } +const CPDF_Array* GetQuadPointsArrayFromDictionary(CPDF_Dictionary* dict) { + return dict ? dict->GetArrayFor("QuadPoints") : nullptr; +} + +bool GetQuadPointsFromDictionary(CPDF_Dictionary* dict, + size_t quad_index, + FS_QUADPOINTSF* quad_points) { + ASSERT(quad_points); + + const CPDF_Array* pArray = GetQuadPointsArrayFromDictionary(dict); + if (!pArray || quad_index >= pArray->GetCount() / 8) + return false; + + quad_index *= 8; + quad_points->x1 = pArray->GetNumberAt(quad_index); + quad_points->y1 = pArray->GetNumberAt(quad_index + 1); + quad_points->x2 = pArray->GetNumberAt(quad_index + 2); + quad_points->y2 = pArray->GetNumberAt(quad_index + 3); + quad_points->x3 = pArray->GetNumberAt(quad_index + 4); + quad_points->y3 = pArray->GetNumberAt(quad_index + 5); + quad_points->x4 = pArray->GetNumberAt(quad_index + 6); + quad_points->y4 = pArray->GetNumberAt(quad_index + 7); + return true; +} + FPDF_EXPORT FPDF_BOOKMARK FPDF_CALLCONV FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) { CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); @@ -379,41 +404,20 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFLink_GetAnnotRect(FPDF_LINK link_annot, } FPDF_EXPORT int FPDF_CALLCONV FPDFLink_CountQuadPoints(FPDF_LINK link_annot) { - if (!link_annot) - return 0; - CPDF_Dictionary* pAnnotDict = CPDFDictionaryFromFPDFLink(link_annot); - CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints"); - if (!pArray) - return 0; - return static_cast(pArray->GetCount() / 8); + const CPDF_Array* pArray = + GetQuadPointsArrayFromDictionary(CPDFDictionaryFromFPDFLink(link_annot)); + return pArray ? static_cast(pArray->GetCount() / 8) : 0; } FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFLink_GetQuadPoints(FPDF_LINK link_annot, int quad_index, FS_QUADPOINTSF* quad_points) { - if (!link_annot || !quad_points) + if (!quad_points || quad_index < 0) return false; - CPDF_Dictionary* pAnnotDict = CPDFDictionaryFromFPDFLink(link_annot); - CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints"); - if (!pArray) - return false; - - if (quad_index < 0 || - static_cast(quad_index) >= pArray->GetCount() / 8 || - (static_cast(quad_index * 8 + 7) >= pArray->GetCount())) { - return false; - } - - quad_points->x1 = pArray->GetNumberAt(quad_index * 8); - quad_points->y1 = pArray->GetNumberAt(quad_index * 8 + 1); - quad_points->x2 = pArray->GetNumberAt(quad_index * 8 + 2); - quad_points->y2 = pArray->GetNumberAt(quad_index * 8 + 3); - quad_points->x3 = pArray->GetNumberAt(quad_index * 8 + 4); - quad_points->y3 = pArray->GetNumberAt(quad_index * 8 + 5); - quad_points->x4 = pArray->GetNumberAt(quad_index * 8 + 6); - quad_points->y4 = pArray->GetNumberAt(quad_index * 8 + 7); - return true; + return GetQuadPointsFromDictionary(CPDFDictionaryFromFPDFLink(link_annot), + static_cast(quad_index), + quad_points); } FPDF_EXPORT unsigned long FPDF_CALLCONV FPDF_GetMetaText(FPDF_DOCUMENT document, diff --git a/fpdfsdk/fsdk_define.h b/fpdfsdk/fsdk_define.h index d503ba5e1d..acd8437169 100644 --- a/fpdfsdk/fsdk_define.h +++ b/fpdfsdk/fsdk_define.h @@ -10,6 +10,7 @@ #include "core/fpdfapi/parser/cpdf_parser.h" #include "core/fxge/dib/cfx_dibitmap.h" #include "core/fxge/fx_dib.h" +#include "public/fpdf_doc.h" #include "public/fpdfview.h" #ifdef PDF_ENABLE_XFA @@ -83,6 +84,11 @@ CPDF_Dictionary* CPDFDictionaryFromFPDFBookmark(FPDF_BOOKMARK bookmark); CPDF_Dictionary* CPDFDictionaryFromFPDFLink(FPDF_LINK link); +const CPDF_Array* GetQuadPointsArrayFromDictionary(CPDF_Dictionary* dict); +bool GetQuadPointsFromDictionary(CPDF_Dictionary* dict, + size_t quad_index, + FS_QUADPOINTSF* quad_points); + CFX_FloatRect CFXFloatRectFromFSRECTF(const FS_RECTF& rect); void FSRECTFFromCFXFloatRect(const CFX_FloatRect& rect, FS_RECTF* out_rect); -- cgit v1.2.3