From 733e068e077bb59597180bc9b8ff934dd125ffdc Mon Sep 17 00:00:00 2001 From: thestig Date: Wed, 23 Nov 2016 05:52:39 -0800 Subject: Add API for getting page labels. BUG=pdfium:479 Review-Url: https://codereview.chromium.org/2521843003 --- core/fpdfdoc/cpdf_pagelabel.cpp | 28 ++++++++++++++++++---------- core/fpdfdoc/cpdf_pagelabel.h | 2 +- 2 files changed, 19 insertions(+), 11 deletions(-) (limited to 'core/fpdfdoc') diff --git a/core/fpdfdoc/cpdf_pagelabel.cpp b/core/fpdfdoc/cpdf_pagelabel.cpp index 64075ef55a..2a79d77ca7 100644 --- a/core/fpdfdoc/cpdf_pagelabel.cpp +++ b/core/fpdfdoc/cpdf_pagelabel.cpp @@ -75,16 +75,21 @@ CFX_WideString GetLabelNumPortion(int num, const CFX_ByteString& bsStyle) { CPDF_PageLabel::CPDF_PageLabel(CPDF_Document* pDocument) : m_pDocument(pDocument) {} -CFX_WideString CPDF_PageLabel::GetLabel(int nPage) const { - CFX_WideString wsLabel; +bool CPDF_PageLabel::GetLabel(int nPage, CFX_WideString* wsLabel) const { if (!m_pDocument) - return wsLabel; + return false; + + if (nPage < 0 || nPage >= m_pDocument->GetPageCount()) + return false; CPDF_Dictionary* pPDFRoot = m_pDocument->GetRoot(); if (!pPDFRoot) - return wsLabel; + return false; CPDF_Dictionary* pLabels = pPDFRoot->GetDictFor("PageLabels"); + if (!pLabels) + return false; + CPDF_NumberTree numberTree(pLabels); CPDF_Object* pValue = nullptr; int n = nPage; @@ -99,18 +104,18 @@ CFX_WideString CPDF_PageLabel::GetLabel(int nPage) const { pValue = pValue->GetDirect(); if (CPDF_Dictionary* pLabel = pValue->AsDictionary()) { if (pLabel->KeyExist("P")) - wsLabel += pLabel->GetUnicodeTextFor("P"); + *wsLabel += pLabel->GetUnicodeTextFor("P"); CFX_ByteString bsNumberingStyle = pLabel->GetStringFor("S", ""); int nLabelNum = nPage - n + pLabel->GetIntegerFor("St", 1); CFX_WideString wsNumPortion = GetLabelNumPortion(nLabelNum, bsNumberingStyle); - wsLabel += wsNumPortion; - return wsLabel; + *wsLabel += wsNumPortion; + return true; } } - wsLabel.Format(L"%d", nPage + 1); - return wsLabel; + wsLabel->Format(L"%d", nPage + 1); + return true; } int32_t CPDF_PageLabel::GetPageByLabel(const CFX_ByteStringC& bsLabel) const { @@ -123,7 +128,10 @@ int32_t CPDF_PageLabel::GetPageByLabel(const CFX_ByteStringC& bsLabel) const { int nPages = m_pDocument->GetPageCount(); for (int i = 0; i < nPages; i++) { - if (PDF_EncodeText(GetLabel(i)).Compare(bsLabel)) + CFX_WideString str; + if (!GetLabel(i, &str)) + continue; + if (PDF_EncodeText(str).Compare(bsLabel)) return i; } diff --git a/core/fpdfdoc/cpdf_pagelabel.h b/core/fpdfdoc/cpdf_pagelabel.h index 6a0664bfca..0f91f614d9 100644 --- a/core/fpdfdoc/cpdf_pagelabel.h +++ b/core/fpdfdoc/cpdf_pagelabel.h @@ -15,7 +15,7 @@ class CPDF_PageLabel { public: explicit CPDF_PageLabel(CPDF_Document* pDocument); - CFX_WideString GetLabel(int nPage) const; + bool GetLabel(int nPage, CFX_WideString* wsLabel) const; int32_t GetPageByLabel(const CFX_ByteStringC& bsLabel) const; int32_t GetPageByLabel(const CFX_WideStringC& wsLabel) const; -- cgit v1.2.3