Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1532)

Unified Diff: core/fpdfdoc/cpdf_pagelabel.cpp

Issue 2521843003: Add API for getting page labels. (Closed)
Patch Set: Add tests, distinguish between failure and empty label Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: core/fpdfdoc/cpdf_pagelabel.cpp
diff --git a/core/fpdfdoc/cpdf_pagelabel.cpp b/core/fpdfdoc/cpdf_pagelabel.cpp
index 64075ef55a181ae5b9136f8c392f0ea6cee50225..2a79d77ca7e38091b766e2f7122c44db526da16f 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;
}

Powered by Google App Engine
This is Rietveld 408576698