pdf-core 0.2.0 → 0.2.1

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
  SHA1:
3
- metadata.gz: c82f9b6d4628406f50c582c41acd34a87877665a
4
- data.tar.gz: dfc8e85e060c5c4fbfca3f08172237fc76fe6fab
3
+ metadata.gz: 214a429acbb26d39087be7eaf5df268a5f5b072d
4
+ data.tar.gz: dcc45ccaf9206b5d2e85922ac9019eb3a23f3bde
5
5
  SHA512:
6
- metadata.gz: be3d2a5a9f2ae4e13ab6968ebd077bca6c3cac533d6b16572df103e3977d9340197f7c6ad274e3b95c8653d72e6b68f343d56395568b9bb2b66a90e768a8e072
7
- data.tar.gz: 829e5ce883da56c582dcdf185388087d98247d13b05952258577dbd472cc25e9cf1b2f0502f70a73e2628d282cecafdd4bde1004674fd24804b759532cddf4c0
6
+ metadata.gz: ec4be03057a7332b0e92b74b18826140afa8ab5faaafd94d52c009aecab3cda187a59f9d70fc0a36c2f95de071c95f1c49f958ec7bf9c93d779b3f313eca3e61
7
+ data.tar.gz: a548742fc3a1b06836540855cc832278e10a7dec1bc6ebe56af827894babd59cfe1a2dd36ad6c0c355aa57d60355d0ae6dc0a3c2dd34aae41343532df0b0944e
data/lib/pdf/core.rb CHANGED
@@ -13,7 +13,6 @@ require_relative "core/document_state"
13
13
  require_relative "core/name_tree"
14
14
  require_relative "core/graphics_state"
15
15
  require_relative "core/page_geometry"
16
- require_relative "core/outline"
17
16
 
18
17
  module PDF
19
18
  module Core
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdf-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregory Brown
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2014-02-20 00:00:00.000000000 Z
15
+ date: 2014-03-04 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: pdf-reader
@@ -106,7 +106,6 @@ files:
106
106
  - lib/pdf/core/literal_string.rb
107
107
  - lib/pdf/core/name_tree.rb
108
108
  - lib/pdf/core/object_store.rb
109
- - lib/pdf/core/outline.rb
110
109
  - lib/pdf/core/page.rb
111
110
  - lib/pdf/core/page_geometry.rb
112
111
  - lib/pdf/core/pdf_object.rb
@@ -133,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
132
  version: 1.3.6
134
133
  requirements: []
135
134
  rubyforge_project: prawn
136
- rubygems_version: 2.2.0
135
+ rubygems_version: 2.2.2
137
136
  signing_key:
138
137
  specification_version: 4
139
138
  summary: PDF::Core is used by Prawn to render PDF documents
@@ -1,317 +0,0 @@
1
- require "forwardable"
2
-
3
- module PDF
4
- module Core
5
- # The Outline class organizes the outline tree items for the document.
6
- # Note that the prev and parent instance variables are adjusted while navigating
7
- # through the nested blocks. These variables along with the presence or absense
8
- # of blocks are the primary means by which the relations for the various
9
- # OutlineItems and the OutlineRoot are set. Unfortunately, the best way to
10
- # understand how this works is to follow the method calls through a real example.
11
- #
12
- # Some ideas for the organization of this class were gleaned from name_tree. In
13
- # particular the way in which the OutlineItems are finally rendered into document
14
- # objects in PdfObject through a hash.
15
- #
16
- class Outline
17
-
18
- extend Forwardable
19
- def_delegator :@document, :page_number
20
-
21
- attr_accessor :parent
22
- attr_accessor :prev
23
- attr_accessor :document
24
- attr_accessor :items
25
-
26
- def initialize(document)
27
- @document = document
28
- @parent = root
29
- @prev = nil
30
- @items = {}
31
- end
32
-
33
- # Defines/Updates an outline for the document.
34
- # The outline is an optional nested index that appears on the side of a PDF
35
- # document usually with direct links to pages. The outline DSL is defined by nested
36
- # blocks involving two methods: section and page; see the documentation on those methods
37
- # for their arguments and options. Note that one can also use outline#update
38
- # to add more sections to the end of the outline tree using the same syntax and scope.
39
- #
40
- # The syntax is best illustrated with an example:
41
- #
42
- # Prawn::Document.generate(outlined_document.pdf) do
43
- # text "Page 1. This is the first Chapter. "
44
- # start_new_page
45
- # text "Page 2. More in the first Chapter. "
46
- # start_new_page
47
- # outline.define do
48
- # section 'Chapter 1', :destination => 1, :closed => true do
49
- # page :destination => 1, :title => 'Page 1'
50
- # page :destination => 2, :title => 'Page 2'
51
- # end
52
- # end
53
- # start_new_page do
54
- # outline.update do
55
- # section 'Chapter 2', :destination => 2, do
56
- # page :destination => 3, :title => 'Page 3'
57
- # end
58
- # end
59
- # end
60
- #
61
- def define(&block)
62
- instance_eval(&block) if block
63
- end
64
-
65
- alias :update :define
66
-
67
- # Inserts an outline section to the outline tree (see outline#define).
68
- # Although you will probably choose to exclusively use outline#define so
69
- # that your outline tree is contained and easy to manage, this method
70
- # gives you the option to insert sections to the outline tree at any point
71
- # during document generation. This method allows you to add a child subsection
72
- # to any other item at any level in the outline tree.
73
- # Currently the only way to locate the place of entry is with the title for the
74
- # item. If your title names are not unique consider using define_outline.
75
- # The method takes the following arguments:
76
- # title: a string that must match an outline title to add the subsection to
77
- # position: either :first or :last(the default) where the subsection will be placed relative
78
- # to other child elements. If you need to position your subsection in between
79
- # other elements then consider using #insert_section_after
80
- # block: uses the same DSL syntax as outline#define, for example:
81
- #
82
- # Consider using this method inside of outline.update if you want to have the outline object
83
- # to be scoped as self (see #insert_section_after example).
84
- #
85
- # go_to_page 2
86
- # start_new_page
87
- # text "Inserted Page"
88
- # outline.add_subsection_to :title => 'Page 2', :first do
89
- # outline.page :destination => page_number, :title => "Inserted Page"
90
- # end
91
- #
92
- def add_subsection_to(title, position = :last, &block)
93
- @parent = items[title]
94
- raise Prawn::Errors::UnknownOutlineTitle,
95
- "\n No outline item with title: '#{title}' exists in the outline tree" unless @parent
96
- @prev = position == :first ? nil : @parent.data.last
97
- nxt = position == :first ? @parent.data.first : nil
98
- insert_section(nxt, &block)
99
- end
100
-
101
- # Inserts an outline section to the outline tree (see outline#define).
102
- # Although you will probably choose to exclusively use outline#define so
103
- # that your outline tree is contained and easy to manage, this method
104
- # gives you the option to insert sections to the outline tree at any point
105
- # during document generation. Unlike outline.add_section, this method allows
106
- # you to enter a section after any other item at any level in the outline tree.
107
- # Currently the only way to locate the place of entry is with the title for the
108
- # item. If your title names are not unique consider using define_outline.
109
- # The method takes the following arguments:
110
- # title: the title of other section or page to insert new section after
111
- # block: uses the same DSL syntax as outline#define, for example:
112
- #
113
- # go_to_page 2
114
- # start_new_page
115
- # text "Inserted Page"
116
- # update_outline do
117
- # insert_section_after :title => 'Page 2' do
118
- # page :destination => page_number, :title => "Inserted Page"
119
- # end
120
- # end
121
- #
122
- def insert_section_after(title, &block)
123
- @prev = items[title]
124
- raise Prawn::Errors::UnknownOutlineTitle,
125
- "\n No outline item with title: '#{title}' exists in the outline tree" unless @prev
126
- @parent = @prev.data.parent
127
- nxt = @prev.data.next
128
- insert_section(nxt, &block)
129
- end
130
-
131
- # See outline#define above for documentation on how this is used in that context
132
- #
133
- # Adds an outine section to the outline tree.
134
- # Although you will probably choose to exclusively use outline#define so
135
- # that your outline tree is contained and easy to manage, this method
136
- # gives you the option to add sections to the outline tree at any point
137
- # during document generation. When not being called from within another #section block
138
- # the section will be added at the top level after the other root elements of the outline.
139
- # For more flexible placement try using outline#insert_section_after and/or
140
- # outline#add_subsection_to
141
- # Takes the following arguments:
142
- # title: the outline text that appears for the section.
143
- # options: destination - optional integer defining the page number for a destination link
144
- # to the top of the page (using a :FIT destination).
145
- # - or an array with a custom destination (see the #dest_* methods of the
146
- # PDF::Destination module)
147
- # closed - whether the section should show its nested outline elements.
148
- # - defaults to false.
149
- # block: more nested subsections and/or page blocks
150
- #
151
- # example usage:
152
- #
153
- # outline.section 'Added Section', :destination => 3 do
154
- # outline.page :destionation => 3, :title => 'Page 3'
155
- # end
156
- def section(title, options = {}, &block)
157
- add_outline_item(title, options, &block)
158
- end
159
-
160
- # See Outline#define above for more documentation on how it is used in that context
161
- #
162
- # Adds a page to the outline.
163
- # Although you will probably choose to exclusively use outline#define so
164
- # that your outline tree is contained and easy to manage, this method also
165
- # gives you the option to add pages to the root of outline tree at any point
166
- # during document generation. Note that the page will be added at the
167
- # top level after the other root outline elements. For more flexible placement try
168
- # using outline#insert_section_after and/or outline#add_subsection_to.
169
- #
170
- # Takes the following arguments:
171
- # options:
172
- # title - REQUIRED. The outline text that appears for the page.
173
- # destination - optional integer defining the page number for a destination link
174
- # to the top of the page (using a :FIT destination).
175
- # - or an array with a custom destination (see the #dest_* methods of the
176
- # PDF::Destination module)
177
- # closed - whether the section should show its nested outline elements.
178
- # - defaults to false.
179
- # example usage:
180
- #
181
- # outline.page :title => "Very Last Page"
182
- # Note: this method is almost identical to section except that it does not accept a block
183
- # thereby defining the outline item as a leaf on the outline tree structure.
184
- def page(options = {})
185
- if options[:title]
186
- title = options[:title]
187
- else
188
- raise Prawn::Errors::RequiredOption,
189
- "\nTitle is a required option for page"
190
- end
191
- add_outline_item(title, options)
192
- end
193
-
194
- private
195
-
196
- # The Outline dictionary (12.3.3) for this document. It is
197
- # lazily initialized, so that documents that do not have an outline
198
- # do not incur the additional overhead.
199
- def root
200
- document.state.store.root.data[:Outlines] ||= document.ref!(OutlineRoot.new)
201
- end
202
-
203
- def add_outline_item(title, options, &block)
204
- outline_item = create_outline_item(title, options)
205
- set_relations(outline_item)
206
- increase_count
207
- set_variables_for_block(outline_item, block)
208
- block.call if block
209
- reset_parent(outline_item)
210
- end
211
-
212
- def create_outline_item(title, options)
213
- outline_item = OutlineItem.new(title, parent, options)
214
-
215
- case options[:destination]
216
- when Integer
217
- page_index = options[:destination] - 1
218
- outline_item.dest = [document.state.pages[page_index].dictionary, :Fit]
219
- when Array
220
- outline_item.dest = options[:destination]
221
- end
222
-
223
- outline_item.prev = prev if @prev
224
- items[title] = document.ref!(outline_item)
225
- end
226
-
227
- def set_relations(outline_item)
228
- prev.data.next = outline_item if prev
229
- parent.data.first = outline_item unless prev
230
- parent.data.last = outline_item
231
- end
232
-
233
- def increase_count
234
- counting_parent = parent
235
- while counting_parent
236
- counting_parent.data.count += 1
237
- if counting_parent == root
238
- counting_parent = nil
239
- else
240
- counting_parent = counting_parent.data.parent
241
- end
242
- end
243
- end
244
-
245
- def set_variables_for_block(outline_item, block)
246
- self.prev = block ? nil : outline_item
247
- self.parent = outline_item if block
248
- end
249
-
250
- def reset_parent(outline_item)
251
- if parent == outline_item
252
- self.prev = outline_item
253
- self.parent = outline_item.data.parent
254
- end
255
- end
256
-
257
- def insert_section(nxt, &block)
258
- last = @parent.data.last
259
- if block
260
- block.call
261
- end
262
- adjust_relations(nxt, last)
263
- reset_root_positioning
264
- end
265
-
266
- def adjust_relations(nxt, last)
267
- if nxt
268
- nxt.data.prev = @prev
269
- @prev.data.next = nxt
270
- @parent.data.last = last
271
- end
272
- end
273
-
274
- def reset_root_positioning
275
- @parent = root
276
- @prev = root.data.last
277
- end
278
-
279
- end
280
-
281
- class OutlineRoot #:nodoc:
282
- attr_accessor :count, :first, :last
283
-
284
- def initialize
285
- @count = 0
286
- end
287
-
288
- def to_hash
289
- {:Type => :Outlines, :Count => count, :First => first, :Last => last}
290
- end
291
- end
292
-
293
- class OutlineItem #:nodoc:
294
- attr_accessor :count, :first, :last, :next, :prev, :parent, :title, :dest, :closed
295
-
296
- def initialize(title, parent, options)
297
- @closed = options[:closed]
298
- @title = title
299
- @parent = parent
300
- @count = 0
301
- end
302
-
303
- def to_hash
304
- hash = { :Title => title,
305
- :Parent => parent,
306
- :Count => closed ? -count : count }
307
- [{:First => first}, {:Last => last}, {:Next => defined?(@next) && @next},
308
- {:Prev => prev}, {:Dest => dest}].each do |h|
309
- unless h.values.first.nil?
310
- hash.merge!(h)
311
- end
312
- end
313
- hash
314
- end
315
- end
316
- end
317
- end