pdfrb 0.8.10 → 0.8.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/pdfrb/document/outline.rb +55 -2
- data/lib/pdfrb/model/type/outline_item.rb +4 -1
- data/lib/pdfrb/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ff9335841bf5176c9489855fe26937637022fb139e2b212e6f5b4aba351aca21
|
|
4
|
+
data.tar.gz: cbeaed5651257801ed201b45af40b510a28cdf65934b7d18b3347c7372ec5cfa
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5bd0ad5e3e50563ce7aeb42990fcae8e830fefd8cb10cc5270d6f281b570a620ab5bfa5e8df8c08aaa6fc8252b0e5e395a447e050a853fade8d75dd1fa6f99b1
|
|
7
|
+
data.tar.gz: c3e35e0a4d2b44e723f171aafcc3a807f399e5b00041e52fb423658654849064047190f35e1d84abba9259f37f4cbfe48b69ab2eff56dfedf577c0f600c977e2
|
|
@@ -2,8 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
module Pdfrb
|
|
4
4
|
class Document
|
|
5
|
-
# Bookmark/outline facade.
|
|
6
|
-
#
|
|
5
|
+
# Bookmark/outline facade. Two directions:
|
|
6
|
+
# * build — add/build! construct the /Outlines tree on the
|
|
7
|
+
# Catalog so PDF viewers show a navigation panel;
|
|
8
|
+
# * read — each/to_a walk an existing outline depth-first
|
|
9
|
+
# (parents before children), yielding typed
|
|
10
|
+
# Model::Type::OutlineItem objects.
|
|
7
11
|
class Outline
|
|
8
12
|
attr_reader :document, :entries
|
|
9
13
|
|
|
@@ -53,6 +57,55 @@ module Pdfrb
|
|
|
53
57
|
@document.catalog.value[:Outlines] = root_ref
|
|
54
58
|
root
|
|
55
59
|
end
|
|
60
|
+
|
|
61
|
+
# Enumerate bookmark items depth-first (preorder: a parent
|
|
62
|
+
# yields before its children). Returns an Enumerator when
|
|
63
|
+
# blockless. Malformed chains (loops, dangling refs) are
|
|
64
|
+
# tolerated: each item is visited once.
|
|
65
|
+
def each(&)
|
|
66
|
+
return enum_for(:each) unless block_given?
|
|
67
|
+
|
|
68
|
+
root = outline_root
|
|
69
|
+
walk_outline_chain(root&.value&.[](:First), {}, &)
|
|
70
|
+
self
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def to_a
|
|
74
|
+
each.to_a
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def empty?
|
|
78
|
+
root = outline_root
|
|
79
|
+
root.nil? || root.value[:First].nil?
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
private
|
|
83
|
+
|
|
84
|
+
def outline_root
|
|
85
|
+
ref = document.catalog.value[:Outlines]
|
|
86
|
+
return nil if ref.nil?
|
|
87
|
+
|
|
88
|
+
obj = document.resolve(ref)
|
|
89
|
+
obj.is_a?(Pdfrb::Model::Object) ? obj : nil
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Follow a sibling chain starting at +ref+, recursing into
|
|
93
|
+
# each item's children (/First) before the next sibling.
|
|
94
|
+
def walk_outline_chain(ref, seen, &)
|
|
95
|
+
return if ref.nil?
|
|
96
|
+
|
|
97
|
+
item = document.object(ref)
|
|
98
|
+
return unless item.is_a?(Pdfrb::Model::Object)
|
|
99
|
+
return if seen.key?(item.oid)
|
|
100
|
+
|
|
101
|
+
seen[item.oid] = true
|
|
102
|
+
typed = document.wrap(item.value,
|
|
103
|
+
type: Pdfrb::Model::Type::OutlineItem,
|
|
104
|
+
oid: item.oid, gen: item.gen)
|
|
105
|
+
yield typed
|
|
106
|
+
walk_outline_chain(typed.value[:First], seen, &)
|
|
107
|
+
walk_outline_chain(typed.value[:Next], seen, &)
|
|
108
|
+
end
|
|
56
109
|
end
|
|
57
110
|
|
|
58
111
|
class OutlineEntry
|
|
@@ -8,7 +8,10 @@ module Pdfrb
|
|
|
8
8
|
class OutlineItem < Pdfrb::Model::Cos::Dictionary
|
|
9
9
|
arlington_object "OutlineItem"
|
|
10
10
|
|
|
11
|
-
def title
|
|
11
|
+
def title
|
|
12
|
+
Pdfrb::Model::Cos::TextString.decode(self[:Title])
|
|
13
|
+
end
|
|
14
|
+
|
|
12
15
|
def parent; self[:Parent]; end
|
|
13
16
|
def prev; self[:Prev]; end
|
|
14
17
|
def next; self[:Next]; end
|
data/lib/pdfrb/version.rb
CHANGED