OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 |
| 7 #include "core/fpdfdoc/cpdf_pagelabel.h" |
| 8 |
| 9 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" |
| 10 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" |
| 11 #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h" |
| 12 #include "core/fpdfdoc/doc_utils.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 CFX_WideString MakeRoman(int num) { |
| 17 const int kArabic[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; |
| 18 const CFX_WideString kRoman[] = {L"m", L"cm", L"d", L"cd", L"c", |
| 19 L"xc", L"l", L"xl", L"x", L"ix", |
| 20 L"v", L"iv", L"i"}; |
| 21 const int kMaxNum = 1000000; |
| 22 |
| 23 num %= kMaxNum; |
| 24 int i = 0; |
| 25 CFX_WideString wsRomanNumber; |
| 26 while (num > 0) { |
| 27 while (num >= kArabic[i]) { |
| 28 num = num - kArabic[i]; |
| 29 wsRomanNumber += kRoman[i]; |
| 30 } |
| 31 i = i + 1; |
| 32 } |
| 33 return wsRomanNumber; |
| 34 } |
| 35 |
| 36 CFX_WideString MakeLetters(int num) { |
| 37 if (num == 0) |
| 38 return CFX_WideString(); |
| 39 |
| 40 CFX_WideString wsLetters; |
| 41 const int nMaxCount = 1000; |
| 42 const int nLetterCount = 26; |
| 43 --num; |
| 44 |
| 45 int count = num / nLetterCount + 1; |
| 46 count %= nMaxCount; |
| 47 FX_WCHAR ch = L'a' + num % nLetterCount; |
| 48 for (int i = 0; i < count; i++) |
| 49 wsLetters += ch; |
| 50 return wsLetters; |
| 51 } |
| 52 |
| 53 CFX_WideString GetLabelNumPortion(int num, const CFX_ByteString& bsStyle) { |
| 54 CFX_WideString wsNumPortion; |
| 55 if (bsStyle.IsEmpty()) |
| 56 return wsNumPortion; |
| 57 if (bsStyle == "D") { |
| 58 wsNumPortion.Format(L"%d", num); |
| 59 } else if (bsStyle == "R") { |
| 60 wsNumPortion = MakeRoman(num); |
| 61 wsNumPortion.MakeUpper(); |
| 62 } else if (bsStyle == "r") { |
| 63 wsNumPortion = MakeRoman(num); |
| 64 } else if (bsStyle == "A") { |
| 65 wsNumPortion = MakeLetters(num); |
| 66 wsNumPortion.MakeUpper(); |
| 67 } else if (bsStyle == "a") { |
| 68 wsNumPortion = MakeLetters(num); |
| 69 } |
| 70 return wsNumPortion; |
| 71 } |
| 72 |
| 73 } // namespace |
| 74 |
| 75 CPDF_PageLabel::CPDF_PageLabel(CPDF_Document* pDocument) |
| 76 : m_pDocument(pDocument) {} |
| 77 |
| 78 CFX_WideString CPDF_PageLabel::GetLabel(int nPage) const { |
| 79 CFX_WideString wsLabel; |
| 80 if (!m_pDocument) |
| 81 return wsLabel; |
| 82 |
| 83 CPDF_Dictionary* pPDFRoot = m_pDocument->GetRoot(); |
| 84 if (!pPDFRoot) |
| 85 return wsLabel; |
| 86 |
| 87 CPDF_Dictionary* pLabels = pPDFRoot->GetDictBy("PageLabels"); |
| 88 CPDF_NumberTree numberTree(pLabels); |
| 89 CPDF_Object* pValue = nullptr; |
| 90 int n = nPage; |
| 91 while (n >= 0) { |
| 92 pValue = numberTree.LookupValue(n); |
| 93 if (pValue) |
| 94 break; |
| 95 n--; |
| 96 } |
| 97 |
| 98 if (pValue) { |
| 99 pValue = pValue->GetDirect(); |
| 100 if (CPDF_Dictionary* pLabel = pValue->AsDictionary()) { |
| 101 if (pLabel->KeyExist("P")) |
| 102 wsLabel += pLabel->GetUnicodeTextBy("P"); |
| 103 |
| 104 CFX_ByteString bsNumberingStyle = pLabel->GetStringBy("S", ""); |
| 105 int nLabelNum = nPage - n + pLabel->GetIntegerBy("St", 1); |
| 106 CFX_WideString wsNumPortion = |
| 107 GetLabelNumPortion(nLabelNum, bsNumberingStyle); |
| 108 wsLabel += wsNumPortion; |
| 109 return wsLabel; |
| 110 } |
| 111 } |
| 112 wsLabel.Format(L"%d", nPage + 1); |
| 113 return wsLabel; |
| 114 } |
| 115 |
| 116 int32_t CPDF_PageLabel::GetPageByLabel(const CFX_ByteStringC& bsLabel) const { |
| 117 if (!m_pDocument) |
| 118 return -1; |
| 119 |
| 120 CPDF_Dictionary* pPDFRoot = m_pDocument->GetRoot(); |
| 121 if (!pPDFRoot) |
| 122 return -1; |
| 123 |
| 124 int nPages = m_pDocument->GetPageCount(); |
| 125 for (int i = 0; i < nPages; i++) { |
| 126 if (PDF_EncodeText(GetLabel(i)).Compare(bsLabel)) |
| 127 return i; |
| 128 } |
| 129 |
| 130 int nPage = FXSYS_atoi(CFX_ByteString(bsLabel).c_str()); // NUL terminate. |
| 131 return nPage > 0 && nPage <= nPages ? nPage : -1; |
| 132 } |
| 133 |
| 134 int32_t CPDF_PageLabel::GetPageByLabel(const CFX_WideStringC& wsLabel) const { |
| 135 return GetPageByLabel(PDF_EncodeText(wsLabel.c_str()).AsStringC()); |
| 136 } |
OLD | NEW |