2016-08-22 08:46:18 +00:00
|
|
|
/*
|
|
|
|
* This file is subject to the terms and conditions defined in
|
|
|
|
* file 'LICENSE.md', which is part of this source code package.
|
|
|
|
*/
|
|
|
|
|
2016-09-08 17:53:45 +00:00
|
|
|
package model
|
2016-08-15 01:31:35 +00:00
|
|
|
|
|
|
|
import (
|
2016-08-16 09:36:24 +00:00
|
|
|
"fmt"
|
2016-08-18 09:43:44 +00:00
|
|
|
|
|
|
|
"github.com/unidoc/unidoc/common"
|
2016-09-08 17:53:45 +00:00
|
|
|
. "github.com/unidoc/unidoc/pdf/core"
|
2016-08-15 01:31:35 +00:00
|
|
|
)
|
|
|
|
|
2016-08-16 09:36:24 +00:00
|
|
|
type PdfOutlineTreeNode struct {
|
2016-08-17 00:07:56 +00:00
|
|
|
context interface{} // Allow accessing outer structure.
|
|
|
|
First *PdfOutlineTreeNode
|
|
|
|
Last *PdfOutlineTreeNode
|
2016-08-16 09:36:24 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 16:45:38 +00:00
|
|
|
// PDF outline dictionary (Table 152 - p. 376).
|
2016-08-15 01:31:35 +00:00
|
|
|
type PdfOutline struct {
|
2016-08-16 09:36:24 +00:00
|
|
|
PdfOutlineTreeNode
|
2016-12-05 00:46:27 +00:00
|
|
|
Parent *PdfOutlineTreeNode
|
|
|
|
Count *int64
|
2016-09-11 23:17:38 +00:00
|
|
|
|
|
|
|
primitive *PdfIndirectObject
|
2016-08-15 01:31:35 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 16:45:38 +00:00
|
|
|
// Pdf outline item dictionary (Table 153 - pp. 376 - 377).
|
2016-08-15 01:31:35 +00:00
|
|
|
type PdfOutlineItem struct {
|
2016-08-16 09:36:24 +00:00
|
|
|
PdfOutlineTreeNode
|
2016-08-18 14:38:49 +00:00
|
|
|
Title *PdfObjectString
|
|
|
|
Parent *PdfOutlineTreeNode
|
|
|
|
Prev *PdfOutlineTreeNode
|
|
|
|
Next *PdfOutlineTreeNode
|
|
|
|
Count *int64
|
|
|
|
Dest PdfObject
|
|
|
|
A PdfObject
|
|
|
|
SE PdfObject
|
|
|
|
C PdfObject
|
|
|
|
F PdfObject
|
2016-09-11 23:17:38 +00:00
|
|
|
|
|
|
|
primitive *PdfIndirectObject
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPdfOutline() *PdfOutline {
|
|
|
|
outline := &PdfOutline{}
|
|
|
|
|
|
|
|
container := &PdfIndirectObject{}
|
2017-07-08 21:04:13 +00:00
|
|
|
container.PdfObject = MakeDict()
|
2016-09-11 23:17:38 +00:00
|
|
|
|
|
|
|
outline.primitive = container
|
|
|
|
|
|
|
|
return outline
|
2016-08-15 01:31:35 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 09:43:44 +00:00
|
|
|
func NewPdfOutlineTree() *PdfOutline {
|
2016-09-11 23:17:38 +00:00
|
|
|
outlineTree := NewPdfOutline()
|
2016-08-18 09:43:44 +00:00
|
|
|
outlineTree.context = &outlineTree
|
2016-09-11 23:17:38 +00:00
|
|
|
return outlineTree
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPdfOutlineItem() *PdfOutlineItem {
|
|
|
|
outlineItem := &PdfOutlineItem{}
|
|
|
|
|
|
|
|
container := &PdfIndirectObject{}
|
2017-07-08 21:04:13 +00:00
|
|
|
container.PdfObject = MakeDict()
|
2016-09-11 23:17:38 +00:00
|
|
|
|
|
|
|
outlineItem.primitive = container
|
|
|
|
return outlineItem
|
2016-08-18 09:43:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewOutlineBookmark(title string, page *PdfIndirectObject) *PdfOutlineItem {
|
|
|
|
bookmark := PdfOutlineItem{}
|
|
|
|
bookmark.context = &bookmark
|
|
|
|
|
|
|
|
bookmark.Title = MakeString(title)
|
|
|
|
|
|
|
|
destArray := PdfObjectArray{}
|
|
|
|
destArray = append(destArray, page)
|
|
|
|
destArray = append(destArray, MakeName("Fit"))
|
|
|
|
bookmark.Dest = &destArray
|
|
|
|
|
|
|
|
return &bookmark
|
|
|
|
}
|
|
|
|
|
2016-08-16 09:36:24 +00:00
|
|
|
// Does not traverse the tree.
|
2016-12-05 00:46:27 +00:00
|
|
|
func newPdfOutlineFromIndirectObject(container *PdfIndirectObject) (*PdfOutline, error) {
|
|
|
|
dict, isDict := container.PdfObject.(*PdfObjectDictionary)
|
|
|
|
if !isDict {
|
|
|
|
return nil, fmt.Errorf("Outline object not a dictionary")
|
|
|
|
}
|
|
|
|
|
2016-08-16 09:36:24 +00:00
|
|
|
outline := PdfOutline{}
|
2016-12-05 00:46:27 +00:00
|
|
|
outline.primitive = container
|
2016-08-17 00:07:56 +00:00
|
|
|
outline.context = &outline
|
2016-08-16 09:36:24 +00:00
|
|
|
|
2017-07-08 21:04:13 +00:00
|
|
|
if obj := dict.Get("Type"); obj != nil {
|
2016-08-16 09:36:24 +00:00
|
|
|
typeVal, ok := obj.(*PdfObjectName)
|
|
|
|
if ok {
|
|
|
|
if *typeVal != "Outlines" {
|
2016-10-31 21:48:25 +00:00
|
|
|
common.Log.Debug("ERROR Type != Outlines (%s)", *typeVal)
|
2016-08-19 11:34:55 +00:00
|
|
|
// Should be "Outlines" if there, but some files have other types
|
|
|
|
// Log as an error but do not quit.
|
|
|
|
// Might be a good idea to log this kind of deviation from the standard separately.
|
2016-08-16 09:36:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-08 21:04:13 +00:00
|
|
|
if obj := dict.Get("Count"); obj != nil {
|
2016-08-19 11:34:55 +00:00
|
|
|
// This should always be an integer, but in a few cases has been a float.
|
|
|
|
count, err := getNumberAsInt64(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-08-16 09:36:24 +00:00
|
|
|
}
|
|
|
|
outline.Count = &count
|
|
|
|
}
|
|
|
|
|
|
|
|
return &outline, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does not traverse the tree.
|
2016-12-05 00:46:27 +00:00
|
|
|
func (this *PdfReader) newPdfOutlineItemFromIndirectObject(container *PdfIndirectObject) (*PdfOutlineItem, error) {
|
|
|
|
dict, isDict := container.PdfObject.(*PdfObjectDictionary)
|
|
|
|
if !isDict {
|
|
|
|
return nil, fmt.Errorf("Outline object not a dictionary")
|
|
|
|
}
|
|
|
|
|
2016-08-16 09:36:24 +00:00
|
|
|
item := PdfOutlineItem{}
|
2016-12-05 00:46:27 +00:00
|
|
|
item.primitive = container
|
2016-08-17 00:07:56 +00:00
|
|
|
item.context = &item
|
2016-08-16 09:36:24 +00:00
|
|
|
|
|
|
|
// Title (required).
|
2017-07-08 21:04:13 +00:00
|
|
|
obj := dict.Get("Title")
|
|
|
|
if obj == nil {
|
2016-08-16 09:36:24 +00:00
|
|
|
return nil, fmt.Errorf("Missing Title from Outline Item (required)")
|
|
|
|
}
|
2016-08-19 09:13:12 +00:00
|
|
|
obj, err := this.traceToObject(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-16 17:57:23 +00:00
|
|
|
title, ok := TraceToDirectObject(obj).(*PdfObjectString)
|
2016-08-16 09:36:24 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Title not a string (%T)", obj)
|
|
|
|
}
|
|
|
|
item.Title = title
|
|
|
|
|
|
|
|
// Count (optional).
|
2017-07-08 21:04:13 +00:00
|
|
|
if obj := dict.Get("Count"); obj != nil {
|
2016-08-16 09:36:24 +00:00
|
|
|
countVal, ok := obj.(*PdfObjectInteger)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Count not an integer (%T)", obj)
|
|
|
|
}
|
|
|
|
count := int64(*countVal)
|
|
|
|
item.Count = &count
|
|
|
|
}
|
|
|
|
|
2016-08-16 17:57:23 +00:00
|
|
|
// Other keys.
|
2017-07-08 21:04:13 +00:00
|
|
|
if obj := dict.Get("Dest"); obj != nil {
|
2016-08-19 09:13:12 +00:00
|
|
|
item.Dest, err = this.traceToObject(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err := this.traverseObjectData(item.Dest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-16 17:57:23 +00:00
|
|
|
}
|
2017-07-08 21:04:13 +00:00
|
|
|
if obj := dict.Get("A"); obj != nil {
|
2016-08-19 09:13:12 +00:00
|
|
|
item.A, err = this.traceToObject(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err := this.traverseObjectData(item.A)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-16 17:57:23 +00:00
|
|
|
}
|
2017-07-08 21:04:13 +00:00
|
|
|
if obj := dict.Get("SE"); obj != nil {
|
2016-12-05 00:46:27 +00:00
|
|
|
// XXX: To add structure element support.
|
|
|
|
// Currently not supporting structure elements.
|
|
|
|
item.SE = nil
|
|
|
|
/*
|
|
|
|
item.SE, err = this.traceToObject(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
*/
|
2016-08-16 17:57:23 +00:00
|
|
|
}
|
2017-07-08 21:04:13 +00:00
|
|
|
if obj := dict.Get("C"); obj != nil {
|
2016-08-19 09:13:12 +00:00
|
|
|
item.C, err = this.traceToObject(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-16 17:57:23 +00:00
|
|
|
}
|
2017-07-08 21:04:13 +00:00
|
|
|
if obj := dict.Get("F"); obj != nil {
|
2016-08-19 09:13:12 +00:00
|
|
|
item.F, err = this.traceToObject(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-16 17:57:23 +00:00
|
|
|
}
|
2016-08-16 09:36:24 +00:00
|
|
|
|
|
|
|
return &item, nil
|
|
|
|
}
|
2016-08-18 09:43:44 +00:00
|
|
|
|
2016-08-18 14:38:49 +00:00
|
|
|
// Get the outer object of the tree node (Outline or OutlineItem).
|
2016-09-09 15:02:52 +00:00
|
|
|
func (n *PdfOutlineTreeNode) getOuter() PdfModel {
|
2016-08-18 14:38:49 +00:00
|
|
|
if outline, isOutline := n.context.(*PdfOutline); isOutline {
|
|
|
|
return outline
|
2016-08-18 09:43:44 +00:00
|
|
|
}
|
2016-08-18 14:38:49 +00:00
|
|
|
if outlineItem, isOutlineItem := n.context.(*PdfOutlineItem); isOutlineItem {
|
|
|
|
return outlineItem
|
2016-08-18 09:43:44 +00:00
|
|
|
}
|
|
|
|
|
2016-10-31 21:48:25 +00:00
|
|
|
common.Log.Debug("ERROR Invalid outline tree node item") // Should never happen.
|
2016-08-18 09:43:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-11 23:17:38 +00:00
|
|
|
func (this *PdfOutlineTreeNode) GetContainingPdfObject() PdfObject {
|
|
|
|
return this.getOuter().GetContainingPdfObject()
|
2016-08-18 14:38:49 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 23:17:38 +00:00
|
|
|
func (this *PdfOutlineTreeNode) ToPdfObject() PdfObject {
|
|
|
|
return this.getOuter().ToPdfObject()
|
2016-09-08 17:53:45 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 23:17:38 +00:00
|
|
|
func (this *PdfOutline) GetContainingPdfObject() PdfObject {
|
|
|
|
return this.primitive
|
2016-09-08 17:53:45 +00:00
|
|
|
}
|
2016-08-18 09:43:44 +00:00
|
|
|
|
2016-09-08 17:53:45 +00:00
|
|
|
// Recursively build the Outline tree PDF object.
|
2016-09-11 23:17:38 +00:00
|
|
|
func (this *PdfOutline) ToPdfObject() PdfObject {
|
|
|
|
container := this.primitive
|
2016-09-08 17:53:45 +00:00
|
|
|
dict := container.PdfObject.(*PdfObjectDictionary)
|
|
|
|
|
2016-09-11 23:17:38 +00:00
|
|
|
dict.Set("Type", MakeName("Outlines"))
|
2016-08-18 09:43:44 +00:00
|
|
|
|
|
|
|
if this.First != nil {
|
2016-09-11 23:17:38 +00:00
|
|
|
dict.Set("First", this.First.ToPdfObject())
|
2016-08-18 09:43:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if this.Last != nil {
|
2016-09-11 23:17:38 +00:00
|
|
|
dict.Set("Last", this.Last.getOuter().GetContainingPdfObject())
|
2016-09-08 17:53:45 +00:00
|
|
|
//PdfObjectConverterCache[this.Last.getOuter()]
|
2016-08-18 09:43:44 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 00:46:27 +00:00
|
|
|
if this.Parent != nil {
|
|
|
|
dict.Set("Parent", this.Parent.getOuter().GetContainingPdfObject())
|
|
|
|
}
|
|
|
|
|
2016-09-08 17:53:45 +00:00
|
|
|
return container
|
|
|
|
}
|
2016-08-18 09:43:44 +00:00
|
|
|
|
2016-09-11 23:17:38 +00:00
|
|
|
func (this *PdfOutlineItem) GetContainingPdfObject() PdfObject {
|
|
|
|
return this.primitive
|
2016-08-18 09:43:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Outline item.
|
|
|
|
// Recursively build the Outline tree PDF object.
|
2016-09-11 23:17:38 +00:00
|
|
|
func (this *PdfOutlineItem) ToPdfObject() PdfObject {
|
|
|
|
container := this.primitive
|
2016-09-08 17:53:45 +00:00
|
|
|
dict := container.PdfObject.(*PdfObjectDictionary)
|
2016-08-18 09:43:44 +00:00
|
|
|
|
2017-07-08 21:04:13 +00:00
|
|
|
dict.Set("Title", this.Title)
|
2016-08-18 09:43:44 +00:00
|
|
|
if this.A != nil {
|
2017-07-08 21:04:13 +00:00
|
|
|
dict.Set("A", this.A)
|
2016-08-18 09:43:44 +00:00
|
|
|
}
|
2017-07-08 21:04:13 +00:00
|
|
|
if obj := dict.Get("SE"); obj != nil {
|
2016-12-05 00:46:27 +00:00
|
|
|
// XXX: Currently not supporting structure element hierarchy.
|
|
|
|
// Remove it.
|
2017-07-08 21:04:13 +00:00
|
|
|
dict.Remove("SE")
|
|
|
|
// delete(*dict, "SE")
|
2016-12-05 00:46:27 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
if this.SE != nil {
|
|
|
|
(*dict)["SE"] = this.SE
|
|
|
|
}
|
|
|
|
*/
|
2016-08-18 09:43:44 +00:00
|
|
|
if this.C != nil {
|
2017-07-08 21:04:13 +00:00
|
|
|
dict.Set("C", this.C)
|
2016-08-18 09:43:44 +00:00
|
|
|
}
|
|
|
|
if this.Dest != nil {
|
2017-07-08 21:04:13 +00:00
|
|
|
dict.Set("Dest", this.Dest)
|
2016-08-18 09:43:44 +00:00
|
|
|
}
|
|
|
|
if this.F != nil {
|
2017-07-08 21:04:13 +00:00
|
|
|
dict.Set("F", this.F)
|
2016-08-18 09:43:44 +00:00
|
|
|
}
|
|
|
|
if this.Count != nil {
|
2017-07-08 21:04:13 +00:00
|
|
|
dict.Set("Count", MakeInteger(*this.Count))
|
2016-08-18 09:43:44 +00:00
|
|
|
}
|
|
|
|
if this.Next != nil {
|
2017-07-08 21:04:13 +00:00
|
|
|
dict.Set("Next", this.Next.ToPdfObject())
|
2016-08-18 09:43:44 +00:00
|
|
|
}
|
|
|
|
if this.First != nil {
|
2017-07-08 21:04:13 +00:00
|
|
|
dict.Set("First", this.First.ToPdfObject())
|
2016-08-18 09:43:44 +00:00
|
|
|
}
|
|
|
|
if this.Prev != nil {
|
2017-07-08 21:04:13 +00:00
|
|
|
dict.Set("Prev", this.Prev.getOuter().GetContainingPdfObject())
|
2016-09-08 17:53:45 +00:00
|
|
|
//PdfObjectConverterCache[this.Prev.getOuter()]
|
2016-08-18 09:43:44 +00:00
|
|
|
}
|
|
|
|
if this.Last != nil {
|
2017-07-08 21:04:13 +00:00
|
|
|
dict.Set("Last", this.Last.getOuter().GetContainingPdfObject())
|
2016-09-08 17:53:45 +00:00
|
|
|
// PdfObjectConverterCache[this.Last.getOuter()]
|
2016-08-18 14:38:49 +00:00
|
|
|
}
|
|
|
|
if this.Parent != nil {
|
2017-07-08 21:04:13 +00:00
|
|
|
dict.Set("Parent", this.Parent.getOuter().GetContainingPdfObject())
|
2016-09-08 17:53:45 +00:00
|
|
|
//PdfObjectConverterCache[this.Parent.getOuter()]
|
2016-08-18 09:43:44 +00:00
|
|
|
}
|
|
|
|
|
2016-09-08 17:53:45 +00:00
|
|
|
return container
|
2016-08-18 09:43:44 +00:00
|
|
|
}
|