Chromium Code Reviews| 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 arabic[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; | |
|
Lei Zhang
2016/07/28 21:50:03
kFoo
dsinclair
2016/08/02 17:55:47
Done.
| |
| 18 const CFX_WideString roman[] = {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 nMaxNum = 1000000; | |
| 22 num %= nMaxNum; | |
| 23 int i = 0; | |
| 24 CFX_WideString wsRomanNumber; | |
| 25 while (num > 0) { | |
| 26 while (num >= arabic[i]) { | |
| 27 num = num - arabic[i]; | |
| 28 wsRomanNumber += roman[i]; | |
| 29 } | |
| 30 i = i + 1; | |
| 31 } | |
| 32 return wsRomanNumber; | |
| 33 } | |
| 34 | |
| 35 CFX_WideString MakeLetters(int num) { | |
| 36 if (num == 0) | |
| 37 return CFX_WideString(); | |
| 38 | |
| 39 CFX_WideString wsLetters; | |
| 40 const int nMaxCount = 1000; | |
| 41 const int nLetterCount = 26; | |
| 42 num -= 1; | |
|
Lei Zhang
2016/07/28 21:50:03
--num;
dsinclair
2016/08/02 17:55:47
Done.
| |
| 43 | |
| 44 int count = num / nLetterCount + 1; | |
| 45 count %= nMaxCount; | |
| 46 FX_WCHAR ch = L'a' + num % nLetterCount; | |
| 47 for (int i = 0; i < count; i++) | |
| 48 wsLetters += ch; | |
| 49 return wsLetters; | |
| 50 } | |
| 51 | |
| 52 CFX_WideString GetLabelNumPortion(int num, const CFX_ByteString& bsStyle) { | |
| 53 CFX_WideString wsNumPortion; | |
| 54 if (bsStyle.IsEmpty()) | |
| 55 return wsNumPortion; | |
| 56 if (bsStyle == "D") { | |
| 57 wsNumPortion.Format(L"%d", num); | |
| 58 } else if (bsStyle == "R") { | |
| 59 wsNumPortion = MakeRoman(num); | |
| 60 wsNumPortion.MakeUpper(); | |
| 61 } else if (bsStyle == "r") { | |
| 62 wsNumPortion = MakeRoman(num); | |
| 63 } else if (bsStyle == "A") { | |
| 64 wsNumPortion = MakeLetters(num); | |
| 65 wsNumPortion.MakeUpper(); | |
| 66 } else if (bsStyle == "a") { | |
| 67 wsNumPortion = MakeLetters(num); | |
| 68 } | |
| 69 return wsNumPortion; | |
| 70 } | |
| 71 | |
| 72 } // namespace | |
| 73 | |
| 74 CPDF_PageLabel::CPDF_PageLabel(CPDF_Document* pDocument) | |
| 75 : m_pDocument(pDocument) {} | |
| 76 | |
| 77 CFX_WideString CPDF_PageLabel::GetLabel(int nPage) const { | |
| 78 CFX_WideString wsLabel; | |
| 79 if (!m_pDocument) | |
| 80 return wsLabel; | |
| 81 | |
| 82 CPDF_Dictionary* pPDFRoot = m_pDocument->GetRoot(); | |
| 83 if (!pPDFRoot) | |
| 84 return wsLabel; | |
| 85 | |
| 86 CPDF_Dictionary* pLabels = pPDFRoot->GetDictBy("PageLabels"); | |
| 87 CPDF_NumberTree numberTree(pLabels); | |
| 88 CPDF_Object* pValue = nullptr; | |
| 89 int n = nPage; | |
| 90 while (n >= 0) { | |
| 91 pValue = numberTree.LookupValue(n); | |
| 92 if (pValue) | |
| 93 break; | |
| 94 n--; | |
| 95 } | |
| 96 | |
| 97 if (pValue) { | |
| 98 pValue = pValue->GetDirect(); | |
| 99 if (CPDF_Dictionary* pLabel = pValue->AsDictionary()) { | |
| 100 if (pLabel->KeyExist("P")) | |
| 101 wsLabel += pLabel->GetUnicodeTextBy("P"); | |
| 102 | |
| 103 CFX_ByteString bsNumberingStyle = pLabel->GetStringBy("S", ""); | |
| 104 int nLabelNum = nPage - n + pLabel->GetIntegerBy("St", 1); | |
| 105 CFX_WideString wsNumPortion = | |
| 106 GetLabelNumPortion(nLabelNum, bsNumberingStyle); | |
| 107 wsLabel += wsNumPortion; | |
| 108 return wsLabel; | |
| 109 } | |
| 110 } | |
| 111 wsLabel.Format(L"%d", nPage + 1); | |
| 112 return wsLabel; | |
| 113 } | |
| 114 | |
| 115 int32_t CPDF_PageLabel::GetPageByLabel(const CFX_ByteStringC& bsLabel) const { | |
| 116 if (!m_pDocument) | |
| 117 return -1; | |
| 118 | |
| 119 CPDF_Dictionary* pPDFRoot = m_pDocument->GetRoot(); | |
| 120 if (!pPDFRoot) | |
| 121 return -1; | |
| 122 | |
| 123 int nPages = m_pDocument->GetPageCount(); | |
| 124 for (int i = 0; i < nPages; i++) { | |
| 125 if (PDF_EncodeText(GetLabel(i)).Compare(bsLabel)) | |
| 126 return i; | |
| 127 } | |
| 128 | |
| 129 int nPage = FXSYS_atoi(CFX_ByteString(bsLabel).c_str()); // NUL terminate. | |
| 130 return nPage > 0 && nPage <= nPages ? nPage : -1; | |
| 131 } | |
| 132 | |
| 133 int32_t CPDF_PageLabel::GetPageByLabel(const CFX_WideStringC& wsLabel) const { | |
| 134 return GetPageByLabel(PDF_EncodeText(wsLabel.c_str()).AsStringC()); | |
| 135 } | |
| OLD | NEW |