Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cpdf_pagelabel.cpp
Go to the documentation of this file.
1// Copyright 2016 The PDFium Authors
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 <utility>
10
11#include "core/fpdfapi/parser/cpdf_dictionary.h"
12#include "core/fpdfapi/parser/cpdf_document.h"
13#include "core/fpdfapi/parser/fpdf_parser_decode.h"
14#include "core/fpdfdoc/cpdf_numbertree.h"
15
16namespace {
17
18WideString MakeRoman(int num) {
19 const int kArabic[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
20 const WideStringView kRoman[] = {L"m", L"cm", L"d", L"cd", L"c",
21 L"xc", L"l", L"xl", L"x", L"ix",
22 L"v", L"iv", L"i"};
23 const int kMaxNum = 1000000;
24
25 num %= kMaxNum;
26 int i = 0;
27 WideString wsRomanNumber;
28 while (num > 0) {
29 while (num >= kArabic[i]) {
30 num = num - kArabic[i];
31 wsRomanNumber += kRoman[i];
32 }
33 i = i + 1;
34 }
35 return wsRomanNumber;
36}
37
38WideString MakeLetters(int num) {
39 if (num == 0)
40 return WideString();
41
42 WideString wsLetters;
43 const int nMaxCount = 1000;
44 const int nLetterCount = 26;
45 --num;
46
47 int count = num / nLetterCount + 1;
48 count %= nMaxCount;
49 wchar_t ch = L'a' + num % nLetterCount;
50 for (int i = 0; i < count; i++)
51 wsLetters += ch;
52 return wsLetters;
53}
54
55WideString GetLabelNumPortion(int num, const ByteString& bsStyle) {
56 if (bsStyle.IsEmpty())
57 return WideString();
58 if (bsStyle == "D")
59 return WideString::FormatInteger(num);
60 if (bsStyle == "R") {
61 WideString wsNumPortion = MakeRoman(num);
62 wsNumPortion.MakeUpper();
63 return wsNumPortion;
64 }
65 if (bsStyle == "r")
66 return MakeRoman(num);
67 if (bsStyle == "A") {
68 WideString wsNumPortion = MakeLetters(num);
69 wsNumPortion.MakeUpper();
70 return wsNumPortion;
71 }
72 if (bsStyle == "a")
73 return MakeLetters(num);
74 return WideString();
75}
76
77} // namespace
78
81
82CPDF_PageLabel::~CPDF_PageLabel() = default;
83
85 if (!m_pDocument)
86 return absl::nullopt;
87
88 if (nPage < 0 || nPage >= m_pDocument->GetPageCount())
89 return absl::nullopt;
90
91 const CPDF_Dictionary* pPDFRoot = m_pDocument->GetRoot();
92 if (!pPDFRoot)
93 return absl::nullopt;
94
95 RetainPtr<const CPDF_Dictionary> pLabels = pPDFRoot->GetDictFor("PageLabels");
96 if (!pLabels)
97 return absl::nullopt;
98
99 CPDF_NumberTree numberTree(std::move(pLabels));
100 RetainPtr<const CPDF_Object> pValue;
101 int n = nPage;
102 while (n >= 0) {
103 pValue = numberTree.LookupValue(n);
104 if (pValue)
105 break;
106 n--;
107 }
108
109 if (pValue) {
110 pValue = pValue->GetDirect();
111 if (const CPDF_Dictionary* pLabel = pValue->AsDictionary()) {
112 WideString label;
113 if (pLabel->KeyExist("P"))
114 label += pLabel->GetUnicodeTextFor("P");
115
116 ByteString bsNumberingStyle = pLabel->GetByteStringFor("S", ByteString());
117 int nLabelNum = nPage - n + pLabel->GetIntegerFor("St", 1);
118 WideString wsNumPortion = GetLabelNumPortion(nLabelNum, bsNumberingStyle);
119 label += wsNumPortion;
120 return label;
121 }
122 }
123 return WideString::FormatInteger(nPage + 1);
124}
bool KeyExist(const ByteString &key) const
ByteString GetByteStringFor(const ByteString &key, const ByteString &default_str) const
WideString GetUnicodeTextFor(const ByteString &key) const
int GetIntegerFor(const ByteString &key, int default_int) const
RetainPtr< const CPDF_Object > LookupValue(int num) const
absl::optional< WideString > GetLabel(int nPage) const
CPDF_PageLabel(CPDF_Document *pDocument)
bool operator==(const char *ptr) const
bool IsEmpty() const
Definition bytestring.h:119
WideString & operator+=(const WideString &str)
WideString & operator+=(wchar_t ch)
static WideString FormatInteger(int i)