-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathdictionary.rb
More file actions
341 lines (307 loc) · 12.9 KB
/
Copy pathdictionary.rb
File metadata and controls
341 lines (307 loc) · 12.9 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# -*- encoding: utf-8; frozen_string_literal: true -*-
#
#--
# This file is part of HexaPDF.
#
# HexaPDF - A Versatile PDF Creation and Manipulation Library For Ruby
# Copyright (C) 2014-2026 Thomas Leitner
#
# HexaPDF is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License version 3 as
# published by the Free Software Foundation with the addition of the
# following permission added to Section 15 as permitted in Section 7(a):
# FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
# THOMAS LEITNER, THOMAS LEITNER DISCLAIMS THE WARRANTY OF NON
# INFRINGEMENT OF THIRD PARTY RIGHTS.
#
# HexaPDF is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
# License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with HexaPDF. If not, see <http://www.gnu.org/licenses/>.
#
# The interactive user interfaces in modified source and object code
# versions of HexaPDF must display Appropriate Legal Notices, as required
# under Section 5 of the GNU Affero General Public License version 3.
#
# In accordance with Section 7(b) of the GNU Affero General Public
# License, a covered work must retain the producer line in every PDF that
# is created or manipulated using HexaPDF.
#
# If the GNU Affero General Public License doesn't fit your need,
# commercial licenses are available at <https://gettalong.at/hexapdf/>.
#++
require 'hexapdf/error'
require 'hexapdf/object'
require 'hexapdf/dictionary_fields'
require 'hexapdf/reference'
module HexaPDF
# Implementation of the PDF dictionary type.
#
# Subclasses should use the available class method ::define_field to create fields according to
# the PDF specification. This allows, among other things, automatic type checking and
# basic validation.
#
# Fields defined in superclasses are inherited by their subclasses. This avoids duplicating basic
# field information. If fields differ from their superclass definition, they can be defined again
# in the subclass.
#
# See: PDF2.0 s7.3.7
class Dictionary < HexaPDF::Object
include DictionaryFields
# Defines an entry for the field +name+ and returns the initalized
# HexaPDF::DictionaryFields::Field object. A suitable converter module (see
# HexaPDF::DictionaryFields::Field#converter) is selected based on the type argument.
#
# Options:
#
# type:: The class (or an array of classes) that a value of this field must have. Here is a
# mapping from PDF object types to classes:
#
# Boolean:: \[TrueClass, FalseClass] (or use the Boolean constant)
# Integer:: Integer
# Real:: Float
# String:: String (for text strings), PDFByteString (for binary strings)
# Date:: PDFDate
# Name:: Symbol
# Array:: PDFArray or Array
# Dictionary:: Dictionary (or any subclass) or Hash
# Stream:: Stream (or any subclass)
# Null:: NilClass
#
# If an array of classes is provided, the value can be an instance of any of these
# classes.
#
# If a Symbol object instead of a class is provided, the class is looked up using the
# 'object.type_map' global configuration option when necessary to support lazy loading.
#
# Note that if multiple types are allowed and one of the allowed types is Dictionary (or
# a Symbol), it has to be the first in the list. Otherwise automatic type conversion
# functions won't work correctly.
#
# required:: Specifies whether this field is required, either +true+ or +false+.
#
# default:: Specifies the default value for the field, if any.
#
# indirect:: Specifies whether the value (or the values in the array value) of this field has
# to be an indirect object (+true+), a direct object (+false+) or if it doesn't
# matter (unspecified or +nil+).
#
# allowed_values:: An array of allowed values for this field.
#
# version:: Specifies the minimum version of the PDF specification needed for this value.
def self.define_field(name, type:, required: false, default: nil, indirect: nil,
allowed_values: nil, version: '1.0')
@fields ||= {}
@fields[name] = Field.new(type, required: required, default: default, indirect: indirect,
allowed_values: allowed_values, version: version)
end
# Returns the field entry for the given field name.
#
# The ancestor classes are also searched for such a field entry if none is found for the
# current class.
def self.field(name)
@fields&.[](name) || superclass.field(name)
end
# :call-seq:
# class.each_field {|name, data| block } -> class
# class.each_field -> Enumerator
#
# Calls the block once for each field defined either in this class or in one of the ancestor
# classes.
def self.each_field(&block) # :yields: name, data
return to_enum(__method__) unless block_given?
superclass.each_field(&block) if self != Dictionary && superclass != Dictionary
@fields.each(&block) if defined?(@fields)
end
# Defines the static PDF type of the class in cases where this is possible, i.e. when the class
# implements one specific PDF type (e.g. the HexaPDF::Type::Catalog class).
def self.define_type(type)
@type = type
end
# Returns the statically defined PDF type of the class.
#
# See ::define_type
def self.type
defined?(@type) && @type
end
# Returns the value for the given dictionary entry.
#
# This method should be used instead of direct access to the value because it provides
# numerous advantages:
#
# * References are automatically resolved.
#
# * Returns the native Ruby object for values with class HexaPDF::Object. However, all
# subclasses of HexaPDF::Object are returned as is (it makes no sense, for example, to return
# the hash that describes the Catalog instead of the Catalog object).
#
# * Automatically wraps hash values in specific subclasses of this class if field information is
# available (see ::define_field).
#
# * Returns the default value if one is specified and no value is available.
#
# Note: If field information is available for the entry, a Hash or Array value will always be
# wrapped by Dictionary or PDFArray. Otherwise, the value will be returned as-is.
#
# Note: This method may throw a "can't add a new key into hash during iteration" error in
# certain cases because it potentially modifies the underlying hash!
def [](name)
field = self.class.field(name)
data = if key?(name)
value[name]
elsif field&.default?
value[name] = field.default
end
value[name] = data = document.deref(data) if data.kind_of?(HexaPDF::Reference)
if data.instance_of?(HexaPDF::Object) || (data.kind_of?(HexaPDF::Object) && data.value.nil?)
data = data.value
end
if (result = field&.convert(data, document))
self[name] = data = result
end
data
end
# Stores the data under name in the dictionary. Name has to be a Symbol.
#
# If the current value for this name has the class HexaPDF::Object (and only this, no
# subclasses) and the given value has not (including subclasses), the value is stored inside the
# HexaPDF::Object.
def []=(name, data)
unless name.kind_of?(Symbol)
raise ArgumentError, "Only Symbol (Name) keys are allowed to be used in PDF dictionaries"
end
if value[name].instance_of?(HexaPDF::Object) && !data.kind_of?(HexaPDF::Object) &&
!data.kind_of?(HexaPDF::Reference)
value[name].value = data
else
value[name] = data
end
end
# Returns +true+ if the given key is present in the dictionary and not +nil+.
def key?(key)
!value[key].nil?
end
# Deletes the name-value pair from the dictionary and returns the value. If such a pair does
# not exist, +nil+ is returned.
def delete(name)
value.delete(name) { nil }
end
# :call-seq:
# dict.each {|name, value| block} -> dict
# dict.each -> Enumerator
#
# Calls the given block once for every name-value entry that is stored in the dictionary.
#
# Note that the yielded value is already preprocessed like in #[].
def each
return to_enum(__method__) unless block_given?
value.each_key {|name| yield(name, self[name]) }
self
end
# Returns, in order or availability, the value of ::type, the /Type field or the result of
# Object#type.
def type
self.class.type || self[:Type] || super
end
# Returns +true+ if the dictionary contains no entries.
def empty?
value.empty?
end
# Returns a hash containing the preprocessed values (like in #[]).
def to_hash
value.each_with_object({}) {|(k, _), h| h[k] = self[k] }
end
private
# Ensures that the value is useful for a Dictionary and updates the object's value with
# information from the dictionary's field.
def after_data_change # :nodoc:
super
data.value ||= {}
unless value.kind_of?(Hash)
raise ArgumentError, "A PDF dictionary object needs a hash value, not a #{value.class}"
end
set_required_fields_with_defaults
end
# Sets all required fields that have no current value but a default value to their respective
# default value.
def set_required_fields_with_defaults
return if (type = value[:Type]) && self.class.type != type
self.class.each_field do |name, field|
if !key?(name) && field.required? && field.default?
value[name] = field.default
end
end
end
# Iterates over all currently set entries and all fields that are required.
def each_set_key_or_required_field #:yields: name, field
value.keys.each {|name| yield(name, self.class.field(name)) }
self.class.each_field do |name, field|
yield(name, field) if field.required? && !value.key?(name)
end
end
# Performs validation tasks based on the currently set keys and defined fields.
def perform_validation(&block)
super
each_set_key_or_required_field do |name, field|
obj = key?(name) ? self[name] : nil
validate_nested(obj, &block)
# The checks below need associated field information
next unless field
# Check that required fields are set
if field.required? && obj.nil?
yield("Required field #{name} is not set", field.default?)
self[name] = obj = field.default if field.default?
end
# Check if the document version is set high enough
if field.version > document.instance_variable_get(:@version)
yield("Field #{name} requires document version to be #{field.version}", true)
document.version = field.version
end
# The checks below assume that the field has a value
next if obj.nil?
# Check the type of the field
unless field.valid_object?(obj)
msg = "Type of field #{name} is invalid: #{obj.class}"
if field.type.include?(String) && obj.kind_of?(Symbol)
yield(msg, true)
self[name] = obj.to_s
elsif field.type.include?(Symbol) && obj.kind_of?(String)
yield(msg, true)
self[name] = obj.intern
else
yield(msg, !field.required? || field.default?)
if field.required? && field.default?
self[name] = obj = field.default
else
delete(name)
next
end
end
end
# Check the value of the field against the allowed values.
if field.allowed_values && !field.allowed_values.include?(obj)
yield("Field #{name} does not contain an allowed value: #{obj.inspect}")
end
# Check if field value needs to be (in)direct
unless field.indirect.nil?
obj = value[name] # we need the unwrapped object!
if field.indirect && (!obj.kind_of?(HexaPDF::Object) || !obj.indirect?)
yield("Field #{name} needs to be an indirect object", true)
value[name] = document.add(obj)
elsif !field.indirect && obj.kind_of?(HexaPDF::Object) && obj.indirect?
yield("Field #{name} needs to be a direct object", true)
value[name] = obj.value
document.delete(obj)
end
end
end
end
end
# :nodoc:
# Forward declaration of Stream to circumvent circular require problem
class Stream < Dictionary
end
end