OptionalContent.h 4.04 KB
Newer Older
1 2 3 4 5
//========================================================================
//
// OptionalContent.h
//
// Copyright 2007 Brad Hards <bradh@kde.org>
6
// Copyright 2008 Carlos Garcia Campos <carlosgc@gnome.org>
7
// Copyright 2013, 2018, 2019 Albert Astals Cid <aacid@kde.org>
Albert Astals Cid's avatar
Albert Astals Cid committed
8
// Copyright 2018 Adam Reichold <adam.reichold@t-online.de>
9
// Copyright 2019 Oliver Sander <oliver.sander@tu-dresden.de>
10 11 12 13 14 15 16 17 18 19
//
// Released under the GPL (version 2, or later, at your option)
//
//========================================================================

#ifndef OPTIONALCONTENT_H
#define OPTIONALCONTENT_H

#include "Object.h"
#include "CharTypes.h"
20 21
#include <unordered_map>
#include <memory>
22 23 24 25

class GooString;
class XRef;

26 27
class OptionalContentGroup;
class OCDisplayNode;
28 29 30 31 32 33 34 35

//------------------------------------------------------------------------

class OCGs {
public:

  OCGs(Object *ocgObject, XRef *xref);

36 37 38
  OCGs(const OCGs &) = delete;
  OCGs& operator=(const OCGs &) = delete;

39
  // Is OCGS valid?
40
  bool isOk() const { return ok; }
41
  
Albert Astals Cid's avatar
Albert Astals Cid committed
42
  bool hasOCGs() const;
43
  const std::unordered_map< Ref, std::unique_ptr< OptionalContentGroup > > &getOCGs() const { return optionalContentGroups; }
44

45
  OptionalContentGroup* findOcgByRef( const Ref ref);
46

47 48 49 50
  // Get the root node of the optional content group display tree
  // (which does not necessarily include all of the OCGs).
  OCDisplayNode *getDisplayRoot();

51
  Array* getOrderArray() 
52
    { return (order.isArray() && order.arrayGetLength() > 0) ? order.getArray() : nullptr; }
53
  Array* getRBGroupsArray() 
54
    { return (rbgroups.isArray() && rbgroups.arrayGetLength()) ? rbgroups.getArray() : nullptr; }
55

56
  bool optContentIsVisible( const Object *dictRef );
57 58

private:
59
  bool ok;
60

61
  bool evalOCVisibilityExpr(const Object *expr, int recursion);
62 63 64 65 66
  bool allOn( Array *ocgArray );
  bool allOff( Array *ocgArray );
  bool anyOn( Array *ocgArray );
  bool anyOff( Array *ocgArray );

67
  std::unordered_map< Ref, std::unique_ptr< OptionalContentGroup > > optionalContentGroups;
68

69 70
  Object order;
  Object rbgroups;
71
  XRef *m_xref;
72
  std::unique_ptr< OCDisplayNode > display; // root node of display tree
73 74 75 76 77 78 79 80
};

//------------------------------------------------------------------------

class OptionalContentGroup {
public:
  enum State { On, Off };

81 82 83 84 85 86 87
  // Values from the optional content usage dictionary.
  enum UsageState {
    ocUsageOn,
    ocUsageOff,
    ocUsageUnset
  };

88
  OptionalContentGroup(Dict *dict);
89 90 91 92 93

  OptionalContentGroup(GooString *label);

  ~OptionalContentGroup();

94 95 96
  OptionalContentGroup(const OptionalContentGroup &) = delete;
  OptionalContentGroup& operator=(const OptionalContentGroup &) = delete;

Albert Astals Cid's avatar
Albert Astals Cid committed
97
  const GooString* getName() const;
98

99
  Ref getRef() const;
100 101
  void setRef(const Ref ref);

102
  State getState() const { return m_state; };
103 104
  void setState(State state) { m_state = state; };

105 106
  UsageState getViewState() const { return viewState; }
  UsageState getPrintState() const { return printState; }
107

108 109 110
private:
  GooString *m_name;
  Ref m_ref;
111 112 113
  State m_state;
  UsageState viewState;	 // suggested state when viewing
  UsageState printState; // suggested state when printing
114 115
};

116 117 118 119 120
//------------------------------------------------------------------------

class OCDisplayNode {
public:

121
  static OCDisplayNode *parse(const Object *obj, OCGs *oc, XRef *xref, int recursion = 0);
122 123 124
  OCDisplayNode();
  ~OCDisplayNode();

125 126 127
  OCDisplayNode(const OCDisplayNode &) = delete;
  OCDisplayNode& operator=(const OCDisplayNode &) = delete;

Albert Astals Cid's avatar
Albert Astals Cid committed
128 129 130 131
  const GooString *getName() const { return name; }
  const OptionalContentGroup *getOCG() const { return ocg; }
  int getNumChildren() const;
  OCDisplayNode *getChild(int idx) const;
132 133 134

private:

135
  OCDisplayNode(const GooString *nameA);
136 137
  OCDisplayNode(OptionalContentGroup *ocgA);
  void addChild(OCDisplayNode *child);
138 139
  void addChildren(std::vector<OCDisplayNode*> *childrenA);
  std::vector<OCDisplayNode*> *takeChildren();
140

141 142
  GooString *name;		// display name (may be nullptr)
  OptionalContentGroup *ocg;	// nullptr for display labels
143
  std::vector<OCDisplayNode*> *children;		// nullptr if there are no children
144 145 146
				//   [OCDisplayNode]
};

147
#endif