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_viewerpreferences.h"
8 
9 #include "core/fpdfapi/parser/cpdf_dictionary.h"
10 #include "core/fpdfapi/parser/cpdf_document.h"
11 #include "core/fpdfapi/parser/cpdf_name.h"
12 
CPDF_ViewerPreferences(const CPDF_Document * pDoc)13 CPDF_ViewerPreferences::CPDF_ViewerPreferences(const CPDF_Document* pDoc)
14     : m_pDoc(pDoc) {}
15 
16 CPDF_ViewerPreferences::~CPDF_ViewerPreferences() = default;
17 
IsDirectionR2L() const18 bool CPDF_ViewerPreferences::IsDirectionR2L() const {
19   const CPDF_Dictionary* pDict = GetViewerPreferences();
20   return pDict && pDict->GetStringFor("Direction") == "R2L";
21 }
22 
PrintScaling() const23 bool CPDF_ViewerPreferences::PrintScaling() const {
24   const CPDF_Dictionary* pDict = GetViewerPreferences();
25   return !pDict || pDict->GetStringFor("PrintScaling") != "None";
26 }
27 
NumCopies() const28 int32_t CPDF_ViewerPreferences::NumCopies() const {
29   const CPDF_Dictionary* pDict = GetViewerPreferences();
30   return pDict ? pDict->GetIntegerFor("NumCopies") : 1;
31 }
32 
PrintPageRange() const33 CPDF_Array* CPDF_ViewerPreferences::PrintPageRange() const {
34   CPDF_Dictionary* pDict = GetViewerPreferences();
35   return pDict ? pDict->GetArrayFor("PrintPageRange") : nullptr;
36 }
37 
Duplex() const38 ByteString CPDF_ViewerPreferences::Duplex() const {
39   const CPDF_Dictionary* pDict = GetViewerPreferences();
40   return pDict ? pDict->GetStringFor("Duplex") : ByteString("None");
41 }
42 
GenericName(const ByteString & bsKey) const43 Optional<ByteString> CPDF_ViewerPreferences::GenericName(
44     const ByteString& bsKey) const {
45   const CPDF_Dictionary* pDict = GetViewerPreferences();
46   if (!pDict)
47     return {};
48 
49   const CPDF_Name* pName = ToName(pDict->GetObjectFor(bsKey));
50   if (!pName)
51     return {};
52 
53   return pName->GetString();
54 }
55 
GetViewerPreferences() const56 CPDF_Dictionary* CPDF_ViewerPreferences::GetViewerPreferences() const {
57   CPDF_Dictionary* pDict = m_pDoc->GetRoot();
58   return pDict ? pDict->GetDictFor("ViewerPreferences") : nullptr;
59 }
60