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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 PDFium Authors. All rights reserved. 1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "core/fpdfdoc/cpdf_pagelabel.h" 7 #include "core/fpdfdoc/cpdf_pagelabel.h"
8 8
9 #include "core/fpdfapi/parser/cpdf_dictionary.h" 9 #include "core/fpdfapi/parser/cpdf_dictionary.h"
10 #include "core/fpdfapi/parser/cpdf_document.h" 10 #include "core/fpdfapi/parser/cpdf_document.h"
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 wsNumPortion = MakeLetters(num); 68 wsNumPortion = MakeLetters(num);
69 } 69 }
70 return wsNumPortion; 70 return wsNumPortion;
71 } 71 }
72 72
73 } // namespace 73 } // namespace
74 74
75 CPDF_PageLabel::CPDF_PageLabel(CPDF_Document* pDocument) 75 CPDF_PageLabel::CPDF_PageLabel(CPDF_Document* pDocument)
76 : m_pDocument(pDocument) {} 76 : m_pDocument(pDocument) {}
77 77
78 CFX_WideString CPDF_PageLabel::GetLabel(int nPage) const { 78 bool CPDF_PageLabel::GetLabel(int nPage, CFX_WideString* wsLabel) const {
79 CFX_WideString wsLabel;
80 if (!m_pDocument) 79 if (!m_pDocument)
81 return wsLabel; 80 return false;
81
82 if (nPage < 0 || nPage >= m_pDocument->GetPageCount())
83 return false;
82 84
83 CPDF_Dictionary* pPDFRoot = m_pDocument->GetRoot(); 85 CPDF_Dictionary* pPDFRoot = m_pDocument->GetRoot();
84 if (!pPDFRoot) 86 if (!pPDFRoot)
85 return wsLabel; 87 return false;
86 88
87 CPDF_Dictionary* pLabels = pPDFRoot->GetDictFor("PageLabels"); 89 CPDF_Dictionary* pLabels = pPDFRoot->GetDictFor("PageLabels");
90 if (!pLabels)
91 return false;
92
88 CPDF_NumberTree numberTree(pLabels); 93 CPDF_NumberTree numberTree(pLabels);
89 CPDF_Object* pValue = nullptr; 94 CPDF_Object* pValue = nullptr;
90 int n = nPage; 95 int n = nPage;
91 while (n >= 0) { 96 while (n >= 0) {
92 pValue = numberTree.LookupValue(n); 97 pValue = numberTree.LookupValue(n);
93 if (pValue) 98 if (pValue)
94 break; 99 break;
95 n--; 100 n--;
96 } 101 }
97 102
98 if (pValue) { 103 if (pValue) {
99 pValue = pValue->GetDirect(); 104 pValue = pValue->GetDirect();
100 if (CPDF_Dictionary* pLabel = pValue->AsDictionary()) { 105 if (CPDF_Dictionary* pLabel = pValue->AsDictionary()) {
101 if (pLabel->KeyExist("P")) 106 if (pLabel->KeyExist("P"))
102 wsLabel += pLabel->GetUnicodeTextFor("P"); 107 *wsLabel += pLabel->GetUnicodeTextFor("P");
103 108
104 CFX_ByteString bsNumberingStyle = pLabel->GetStringFor("S", ""); 109 CFX_ByteString bsNumberingStyle = pLabel->GetStringFor("S", "");
105 int nLabelNum = nPage - n + pLabel->GetIntegerFor("St", 1); 110 int nLabelNum = nPage - n + pLabel->GetIntegerFor("St", 1);
106 CFX_WideString wsNumPortion = 111 CFX_WideString wsNumPortion =
107 GetLabelNumPortion(nLabelNum, bsNumberingStyle); 112 GetLabelNumPortion(nLabelNum, bsNumberingStyle);
108 wsLabel += wsNumPortion; 113 *wsLabel += wsNumPortion;
109 return wsLabel; 114 return true;
110 } 115 }
111 } 116 }
112 wsLabel.Format(L"%d", nPage + 1); 117 wsLabel->Format(L"%d", nPage + 1);
113 return wsLabel; 118 return true;
114 } 119 }
115 120
116 int32_t CPDF_PageLabel::GetPageByLabel(const CFX_ByteStringC& bsLabel) const { 121 int32_t CPDF_PageLabel::GetPageByLabel(const CFX_ByteStringC& bsLabel) const {
117 if (!m_pDocument) 122 if (!m_pDocument)
118 return -1; 123 return -1;
119 124
120 CPDF_Dictionary* pPDFRoot = m_pDocument->GetRoot(); 125 CPDF_Dictionary* pPDFRoot = m_pDocument->GetRoot();
121 if (!pPDFRoot) 126 if (!pPDFRoot)
122 return -1; 127 return -1;
123 128
124 int nPages = m_pDocument->GetPageCount(); 129 int nPages = m_pDocument->GetPageCount();
125 for (int i = 0; i < nPages; i++) { 130 for (int i = 0; i < nPages; i++) {
126 if (PDF_EncodeText(GetLabel(i)).Compare(bsLabel)) 131 CFX_WideString str;
132 if (!GetLabel(i, &str))
133 continue;
134 if (PDF_EncodeText(str).Compare(bsLabel))
127 return i; 135 return i;
128 } 136 }
129 137
130 int nPage = FXSYS_atoi(CFX_ByteString(bsLabel).c_str()); // NUL terminate. 138 int nPage = FXSYS_atoi(CFX_ByteString(bsLabel).c_str()); // NUL terminate.
131 return nPage > 0 && nPage <= nPages ? nPage : -1; 139 return nPage > 0 && nPage <= nPages ? nPage : -1;
132 } 140 }
133 141
134 int32_t CPDF_PageLabel::GetPageByLabel(const CFX_WideStringC& wsLabel) const { 142 int32_t CPDF_PageLabel::GetPageByLabel(const CFX_WideStringC& wsLabel) const {
135 return GetPageByLabel(PDF_EncodeText(wsLabel.c_str()).AsStringC()); 143 return GetPageByLabel(PDF_EncodeText(wsLabel.c_str()).AsStringC());
136 } 144 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698