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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c9ccd2f148c786f735078767b672eacf4e8861100894f68b248905074c73e7eb
4
- data.tar.gz: eaceeb41569d8fab78bcd46891608f6760d4c2a3d47ef45da896c500e32dd736
3
+ metadata.gz: ff9335841bf5176c9489855fe26937637022fb139e2b212e6f5b4aba351aca21
4
+ data.tar.gz: cbeaed5651257801ed201b45af40b510a28cdf65934b7d18b3347c7372ec5cfa
5
5
  SHA512:
6
- metadata.gz: 909be54b72b05e78ec5bda296a335cfb2b1e78cf2ade15695d45b59e29bb9c73ef95cbfa5f1c4bb4c61a8a191055aaff2dfb3e36e0efb4970b7c0bf71d19cee8
7
- data.tar.gz: 609afef48ccdeed968a0f1791dd1b7f5e295ce1169e6ab4c4ace378fdd6bd6f5eab116b67c15b4feb40eab0f590a6a4dc29cb0e30910c393624dd8a61f72d112
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. Builds the /Outlines tree on the
6
- # Catalog so PDF viewers show a navigation panel.
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; self[:Title]; end
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pdfrb
4
- VERSION = "0.8.10"
4
+ VERSION = "0.8.11"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdfrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.10
4
+ version: 0.8.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.