• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_viewerpreferences.h"
8 
9 #include "core/fpdfapi/parser/cpdf_array.h"
10 #include "core/fpdfapi/parser/cpdf_dictionary.h"
11 #include "core/fpdfapi/parser/cpdf_document.h"
12 #include "core/fpdfapi/parser/cpdf_name.h"
13 
CPDF_ViewerPreferences(const CPDF_Document * pDoc)14 CPDF_ViewerPreferences::CPDF_ViewerPreferences(const CPDF_Document* pDoc)
15     : m_pDoc(pDoc) {}
16 
17 CPDF_ViewerPreferences::~CPDF_ViewerPreferences() = default;
18 
IsDirectionR2L() const19 bool CPDF_ViewerPreferences::IsDirectionR2L() const {
20   RetainPtr<const CPDF_Dictionary> pDict = GetViewerPreferences();
21   return pDict && pDict->GetByteStringFor("Direction") == "R2L";
22 }
23 
PrintScaling() const24 bool CPDF_ViewerPreferences::PrintScaling() const {
25   RetainPtr<const CPDF_Dictionary> pDict = GetViewerPreferences();
26   return !pDict || pDict->GetByteStringFor("PrintScaling") != "None";
27 }
28 
NumCopies() const29 int32_t CPDF_ViewerPreferences::NumCopies() const {
30   RetainPtr<const CPDF_Dictionary> pDict = GetViewerPreferences();
31   return pDict ? pDict->GetIntegerFor("NumCopies") : 1;
32 }
33 
PrintPageRange() const34 RetainPtr<const CPDF_Array> CPDF_ViewerPreferences::PrintPageRange() const {
35   RetainPtr<const CPDF_Dictionary> pDict = GetViewerPreferences();
36   return pDict ? pDict->GetArrayFor("PrintPageRange") : nullptr;
37 }
38 
Duplex() const39 ByteString CPDF_ViewerPreferences::Duplex() const {
40   RetainPtr<const CPDF_Dictionary> pDict = GetViewerPreferences();
41   return pDict ? pDict->GetByteStringFor("Duplex") : ByteString("None");
42 }
43 
GenericName(const ByteString & bsKey) const44 absl::optional<ByteString> CPDF_ViewerPreferences::GenericName(
45     const ByteString& bsKey) const {
46   RetainPtr<const CPDF_Dictionary> pDict = GetViewerPreferences();
47   if (!pDict)
48     return absl::nullopt;
49 
50   RetainPtr<const CPDF_Name> pName = ToName(pDict->GetObjectFor(bsKey));
51   if (!pName)
52     return absl::nullopt;
53 
54   return pName->GetString();
55 }
56 
GetViewerPreferences() const57 RetainPtr<const CPDF_Dictionary> CPDF_ViewerPreferences::GetViewerPreferences()
58     const {
59   const CPDF_Dictionary* pDict = m_pDoc->GetRoot();
60   return pDict ? pDict->GetDictFor("ViewerPreferences") : nullptr;
61 }
62