• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_bookmark.h"
8 
9 #include <memory>
10 #include <vector>
11 
12 #include "core/fpdfapi/parser/cpdf_array.h"
13 #include "core/fpdfapi/parser/cpdf_dictionary.h"
14 #include "core/fpdfapi/parser/cpdf_string.h"
15 #include "core/fpdfdoc/cpdf_nametree.h"
16 #include "core/fxcrt/fx_memory_wrappers.h"
17 #include "core/fxge/fx_dib.h"
18 
19 CPDF_Bookmark::CPDF_Bookmark() = default;
20 
21 CPDF_Bookmark::CPDF_Bookmark(const CPDF_Bookmark& that) = default;
22 
CPDF_Bookmark(const CPDF_Dictionary * pDict)23 CPDF_Bookmark::CPDF_Bookmark(const CPDF_Dictionary* pDict) : m_pDict(pDict) {}
24 
25 CPDF_Bookmark::~CPDF_Bookmark() = default;
26 
GetTitle() const27 WideString CPDF_Bookmark::GetTitle() const {
28   if (!m_pDict)
29     return WideString();
30 
31   const CPDF_String* pString = ToString(m_pDict->GetDirectObjectFor("Title"));
32   if (!pString)
33     return WideString();
34 
35   WideString title = pString->GetUnicodeText();
36   int len = title.GetLength();
37   if (!len)
38     return WideString();
39 
40   std::vector<wchar_t, FxAllocAllocator<wchar_t>> buf(len);
41   for (int i = 0; i < len; i++) {
42     wchar_t w = title[i];
43     buf[i] = w > 0x20 ? w : 0x20;
44   }
45   return WideString(buf.data(), len);
46 }
47 
GetDest(CPDF_Document * pDocument) const48 CPDF_Dest CPDF_Bookmark::GetDest(CPDF_Document* pDocument) const {
49   if (!m_pDict)
50     return CPDF_Dest();
51 
52   const CPDF_Object* pDest = m_pDict->GetDirectObjectFor("Dest");
53   if (!pDest)
54     return CPDF_Dest();
55   if (pDest->IsString() || pDest->IsName()) {
56     CPDF_NameTree name_tree(pDocument, "Dests");
57     return CPDF_Dest(
58         name_tree.LookupNamedDest(pDocument, pDest->GetUnicodeText()));
59   }
60   if (const CPDF_Array* pArray = pDest->AsArray())
61     return CPDF_Dest(pArray);
62   return CPDF_Dest();
63 }
64 
GetAction() const65 CPDF_Action CPDF_Bookmark::GetAction() const {
66   return CPDF_Action(m_pDict ? m_pDict->GetDictFor("A") : nullptr);
67 }
68