-
-
Notifications
You must be signed in to change notification settings - Fork 452
/
context.go
135 lines (101 loc) · 3.39 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package types
import (
"fmt"
"log"
"os"
"sort"
"strings"
)
// PDFContext represents the context for processing PDF files.
type PDFContext struct {
*Configuration
*XRefTable
Read *ReadContext
Optimize *OptimizationContext
Write *WriteContext
}
// NewPDFContext initializes a new PDFContext.
func NewPDFContext(fileName string, file *os.File, config *Configuration) (*PDFContext, error) {
if config == nil {
config = NewDefaultConfiguration()
}
fileInfo, err := file.Stat()
if err != nil {
return nil, err
}
ctx := &PDFContext{
config,
newXRefTable(config.ValidationMode),
newReadContext(fileName, file, fileInfo.Size()),
newOptimizationContext(),
NewWriteContext(config.Eol),
}
return ctx, nil
}
// ResetWriteContext prepares an existing WriteContext for a new file to be written.
func (ctx *PDFContext) ResetWriteContext() {
ctx.Write = NewWriteContext(ctx.Write.Eol)
}
func (ctx *PDFContext) String() string {
var logStr []string
logStr = append(logStr, "*************************************************************************************************\n")
logStr = append(logStr, fmt.Sprintf("HeaderVersion: %s\n", VersionString(*ctx.HeaderVersion)))
if ctx.RootVersion != nil {
logStr = append(logStr, fmt.Sprintf("RootVersion: %s\n", VersionString(*ctx.RootVersion)))
}
logStr = append(logStr, fmt.Sprintf("has %d pages\n", ctx.PageCount))
if ctx.Read.UsingObjectStreams {
logStr = append(logStr, "using object streams\n")
}
if ctx.Read.UsingXRefStreams {
logStr = append(logStr, "using xref streams\n")
}
if ctx.Read.Linearized {
logStr = append(logStr, "is linearized file\n")
}
if ctx.Read.Hybrid {
logStr = append(logStr, "is hybrid reference file\n")
}
if ctx.Tagged {
logStr = append(logStr, "is tagged file\n")
}
logStr = append(logStr, "XRefTable:\n")
logStr = append(logStr, fmt.Sprintf(" Size: %d\n", *ctx.XRefTable.Size))
logStr = append(logStr, fmt.Sprintf(" Root object: %s\n", *ctx.Root))
if ctx.Info != nil {
logStr = append(logStr, fmt.Sprintf(" Info object: %s\n", *ctx.Info))
}
if ctx.ID != nil {
logStr = append(logStr, fmt.Sprintf(" ID object: %s\n", *ctx.ID))
}
if ctx.Encrypt != nil {
logStr = append(logStr, fmt.Sprintf(" Encrypt object: %s\n", *ctx.Encrypt))
}
if ctx.AdditionalStreams != nil && len(*ctx.AdditionalStreams) > 0 {
var objectNumbers []string
for _, k := range *ctx.AdditionalStreams {
indRef, _ := k.(PDFIndirectRef)
objectNumbers = append(objectNumbers, fmt.Sprintf("%d", int(indRef.ObjectNumber)))
}
sort.Strings(objectNumbers)
logStr = append(logStr, fmt.Sprintf(" AdditionalStreams: %s\n\n", strings.Join(objectNumbers, ",")))
}
logStr = append(logStr, fmt.Sprintf("XRefTable with %d entres:\n", len(ctx.Table)))
// Print sorted object list.
logStr = ctx.list(logStr)
// Print free list.
logStr, err := ctx.freeList(logStr)
if err != nil {
log.Fatal(err)
}
// Print list of any missing objects.
if len(ctx.XRefTable.Table) != *ctx.XRefTable.Size {
missing, s := ctx.MissingObjects()
logStr = append(logStr, fmt.Sprintf("%d missing objects: %s\n", missing, *s))
}
logStr = append(logStr, fmt.Sprintf("\nTotal pages: %d\n", ctx.PageCount))
logStr = ctx.Optimize.collectFontInfo(logStr)
logStr = ctx.Optimize.collectImageInfo(logStr)
logStr = append(logStr, "\n")
return strings.Join(logStr, "")
}