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 
11 #include "core/fpdfapi/parser/cpdf_array.h"
12 #include "core/fpdfapi/parser/cpdf_string.h"
13 #include "core/fpdfdoc/cpdf_nametree.h"
14 #include "core/fxge/fx_dib.h"
15 
GetColorRef() const16 uint32_t CPDF_Bookmark::GetColorRef() const {
17   if (!m_pDict)
18     return FXSYS_RGB(0, 0, 0);
19 
20   CPDF_Array* pColor = m_pDict->GetArrayFor("C");
21   if (!pColor)
22     return FXSYS_RGB(0, 0, 0);
23 
24   int r = FXSYS_round(pColor->GetNumberAt(0) * 255);
25   int g = FXSYS_round(pColor->GetNumberAt(1) * 255);
26   int b = FXSYS_round(pColor->GetNumberAt(2) * 255);
27   return FXSYS_RGB(r, g, b);
28 }
29 
GetFontStyle() const30 uint32_t CPDF_Bookmark::GetFontStyle() const {
31   return m_pDict ? m_pDict->GetIntegerFor("F") : 0;
32 }
33 
GetTitle() const34 CFX_WideString CPDF_Bookmark::GetTitle() const {
35   if (!m_pDict)
36     return CFX_WideString();
37 
38   CPDF_String* pString = ToString(m_pDict->GetDirectObjectFor("Title"));
39   if (!pString)
40     return CFX_WideString();
41 
42   CFX_WideString title = pString->GetUnicodeText();
43   int len = title.GetLength();
44   if (!len)
45     return CFX_WideString();
46 
47   std::unique_ptr<FX_WCHAR[]> buf(new FX_WCHAR[len]);
48   for (int i = 0; i < len; i++) {
49     FX_WCHAR w = title[i];
50     buf[i] = w > 0x20 ? w : 0x20;
51   }
52   return CFX_WideString(buf.get(), len);
53 }
54 
GetDest(CPDF_Document * pDocument) const55 CPDF_Dest CPDF_Bookmark::GetDest(CPDF_Document* pDocument) const {
56   if (!m_pDict)
57     return CPDF_Dest();
58 
59   CPDF_Object* pDest = m_pDict->GetDirectObjectFor("Dest");
60   if (!pDest)
61     return CPDF_Dest();
62   if (pDest->IsString() || pDest->IsName()) {
63     CPDF_NameTree name_tree(pDocument, "Dests");
64     return CPDF_Dest(name_tree.LookupNamedDest(pDocument, pDest->GetString()));
65   }
66   if (CPDF_Array* pArray = pDest->AsArray())
67     return CPDF_Dest(pArray);
68   return CPDF_Dest();
69 }
70 
GetAction() const71 CPDF_Action CPDF_Bookmark::GetAction() const {
72   return m_pDict ? CPDF_Action(m_pDict->GetDictFor("A")) : CPDF_Action();
73 }
74