From d5d179f4419dfbbbc3598b91071f6ca7cc44357c Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Tue, 18 Dec 2018 21:08:53 -0500 Subject: Add document and object helpers for outlines (bookmarks) --- include/qpdf/QPDFOutlineDocumentHelper.hh | 108 ++++++++++++++++++++++++++ include/qpdf/QPDFOutlineObjectHelper.hh | 122 ++++++++++++++++++++++++++++++ 2 files changed, 230 insertions(+) create mode 100644 include/qpdf/QPDFOutlineDocumentHelper.hh create mode 100644 include/qpdf/QPDFOutlineObjectHelper.hh (limited to 'include') diff --git a/include/qpdf/QPDFOutlineDocumentHelper.hh b/include/qpdf/QPDFOutlineDocumentHelper.hh new file mode 100644 index 00000000..f6fc74a7 --- /dev/null +++ b/include/qpdf/QPDFOutlineDocumentHelper.hh @@ -0,0 +1,108 @@ +// Copyright (c) 2005-2018 Jay Berkenbilt +// +// This file is part of qpdf. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Versions of qpdf prior to version 7 were released under the terms +// of version 2.0 of the Artistic License. At your option, you may +// continue to consider qpdf to be licensed under those terms. Please +// see the manual for additional information. + +#ifndef QPDFOUTLINEDOCUMENTHELPER_HH +#define QPDFOUTLINEDOCUMENTHELPER_HH + +#include +#include +#include + +#include +#include +#include +#include + +#include + +// This is a document helper for outlines, also known as bookmarks. +// Outlines are discussed in section 12.3.3 of the PDF spec +// (ISO-32000). With the help of QPDFOutlineObjectHelper, the outlines +// tree is traversed, and a bidirectional map is made between pages +// and outlines. See also QPDFOutlineObjectHelper. + +class QPDFOutlineDocumentHelper: public QPDFDocumentHelper +{ + public: + QPDF_DLL + QPDFOutlineDocumentHelper(QPDF&); + QPDF_DLL + virtual ~QPDFOutlineDocumentHelper(); + + QPDF_DLL + bool hasOutlines(); + + QPDF_DLL + std::list getTopLevelOutlines(); + + // If the name is a name object, look it up in the /Dests key of + // the document catalog. If the name is a string, look it up in + // the name tree pointed to by the /Dests key of the names + // dictionary. + QPDF_DLL + QPDFObjectHandle + resolveNamedDest(QPDFObjectHandle name); + + // Return a list outlines that are known to target the specified + // page + QPDF_DLL + std::list getOutlinesForPage(QPDFObjGen const&); + + class Accessor + { + friend class QPDFOutlineObjectHelper; + + QPDF_DLL + static bool + checkSeen(QPDFOutlineDocumentHelper& dh, QPDFObjGen const& og) + { + return dh.checkSeen(og); + } + }; + friend class Accessor; + + private: + bool checkSeen(QPDFObjGen const& og); + void initializeByPage(); + + class Members + { + friend class QPDFOutlineDocumentHelper; + + public: + QPDF_DLL + ~Members(); + + private: + Members(); + Members(Members const&); + + std::list outlines; + std::set seen; + QPDFObjectHandle dest_dict; + PointerHolder names_dest; + std::map > by_page; + }; + + PointerHolder m; +}; + +#endif // QPDFOUTLINEDOCUMENTHELPER_HH diff --git a/include/qpdf/QPDFOutlineObjectHelper.hh b/include/qpdf/QPDFOutlineObjectHelper.hh new file mode 100644 index 00000000..f3d608bc --- /dev/null +++ b/include/qpdf/QPDFOutlineObjectHelper.hh @@ -0,0 +1,122 @@ +// Copyright (c) 2005-2018 Jay Berkenbilt +// +// This file is part of qpdf. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Versions of qpdf prior to version 7 were released under the terms +// of version 2.0 of the Artistic License. At your option, you may +// continue to consider qpdf to be licensed under those terms. Please +// see the manual for additional information. + +#ifndef QPDFOUTLINEOBJECTHELPER_HH +#define QPDFOUTLINEOBJECTHELPER_HH + +#include +#include +#include + +class QPDFOutlineDocumentHelper; + +#include + +// This is an object helper for outline items. Outlines, also known as +// bookmarks, are described in section 12.3.3 of the PDF spec +// (ISO-32000). See comments below for details. + +class QPDFOutlineObjectHelper: public QPDFObjectHelper +{ + public: + QPDF_DLL + virtual ~QPDFOutlineObjectHelper() + { + // This must be cleared explicitly to avoid circular references + // that prevent cleanup of pointer holders. + this->m->parent = 0; + } + + // All constructors are private. You can only create one of these + // using QPDFOutlineDocumentHelper. + + // Return parent pointer. Returns a null pointer if this is a + // top-level outline. + QPDF_DLL + PointerHolder getParent(); + + // Return children as a list. + QPDF_DLL + std::list getKids(); + + // Return the destination, regardless of whether it is named or + // explicit and whether it is directly provided or in a GoTo + // action. Returns a null object if the destination can't be + // determined. Named destinations can be resolved using the older + // root /Dest dictionary or the current names tree. + QPDF_DLL + QPDFObjectHandle getDest(); + + // Return the page that the outline points to. Returns a null + // object if the destination page can't be determined. + QPDF_DLL + QPDFObjectHandle getDestPage(); + + // Returns the value of /Count as present in the object, or 0 if + // not present. If count is positive, the outline is open. If + // negative, it is closed. Either way, the absolute value is the + // number descendant items that would be visible if this were + // open. + QPDF_DLL + int getCount(); + + // Returns the title as a UTF-8 string. Returns the empty string + // if there is no title. + QPDF_DLL + std::string getTitle(); + + class Accessor + { + friend class QPDFOutlineDocumentHelper; + + static QPDFOutlineObjectHelper + create(QPDFObjectHandle oh, QPDFOutlineDocumentHelper& dh, int depth) + { + return QPDFOutlineObjectHelper(oh, dh, depth); + } + }; + friend class Accessor; + + private: + QPDF_DLL + QPDFOutlineObjectHelper(QPDFObjectHandle, QPDFOutlineDocumentHelper&, int); + + class Members + { + friend class QPDFOutlineObjectHelper; + + public: + QPDF_DLL + ~Members(); + + private: + Members(QPDFOutlineDocumentHelper& dh); + Members(Members const&); + + QPDFOutlineDocumentHelper& dh; + PointerHolder parent; + std::list kids; + }; + + PointerHolder m; +}; + +#endif // QPDFOUTLINEOBJECTHELPER_HH -- cgit v1.2.3-54-g00ecf