Class: HexaPDF::Dictionary
- Includes:
- DictionaryFields
- Defined in:
- lib/hexapdf/dictionary.rb
Overview
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.
See: PDF1.7 s7.3.7
Direct Known Subclasses
Encryption::EncryptionDictionary, NameTreeNode, NumberTreeNode, Stream, Type::AcroForm::Field, Type::AcroForm::Form, Type::Action, Type::Actions::Launch::WinParameters, Type::Annotation, Type::Annotation::AppearanceDictionary, Type::Annotation::Border, Type::Annotations::Widget::AppearanceCharacteristics, Type::Catalog, Type::EmbeddedFile::MacInfo, Type::EmbeddedFile::Parameters, Type::FileSpecification, Type::FileSpecification::EFDictionary, Type::Font, Type::FontDescriptor, Type::GraphicsStateParameter, Type::Info, Type::Names, Type::Page, Type::PageTreeNode, Type::Resources, Type::Trailer, Type::ViewerPreferences
Constant Summary
Constants included from DictionaryFields
HexaPDF::DictionaryFields::Boolean, HexaPDF::DictionaryFields::PDFByteString, HexaPDF::DictionaryFields::PDFDate
Instance Attribute Summary
Attributes inherited from Object
#data, #document, #must_be_indirect
Class Method Summary collapse
-
.define_field(name, type:, required: false, default: nil, indirect: nil, allowed_values: nil, version: '1.0') ⇒ Object
Defines an entry for the field
nameand returns the initalized HexaPDF::DictionaryFields::Field object. -
.define_type(type) ⇒ Object
Defines the static PDF type of the class in cases where this is possible, i.e.
-
.each_field(&block) ⇒ Object
:call-seq: class.each_field {|name, data| block } -> class class.each_field -> Enumerator.
-
.field(name) ⇒ Object
Returns the field entry for the given field name.
-
.type ⇒ Object
Returns the statically defined PDF type of the class.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Returns the value for the given dictionary entry.
-
#[]=(name, data) ⇒ Object
Stores the data under name in the dictionary.
-
#delete(name) ⇒ Object
Deletes the name-value pair from the dictionary and returns the value.
-
#each ⇒ Object
:call-seq: dict.each {|name, value| block} -> dict dict.each -> Enumerator.
-
#empty? ⇒ Boolean
Returns
trueif the dictionary contains no entries. -
#key?(key) ⇒ Boolean
Returns
trueif the given key is present in the dictionary and notnil. -
#to_h ⇒ Object
Returns a dup of the underlying hash.
-
#type ⇒ Object
Returns, in order or availability, the value of ::type, the /Type field or the result of Object#type.
Methods inherited from Object
#<=>, #==, #cache, #cached?, #clear_cache, deep_copy, #deep_copy, #document?, #eql?, #gen, #gen=, #hash, #indirect?, #initialize, #inspect, #must_be_indirect?, #null?, #oid, #oid=, #validate, #value, #value=
Constructor Details
This class inherits a constructor from HexaPDF::Object
Class Method Details
.define_field(name, type:, required: false, default: nil, indirect: nil, allowed_values: nil, version: '1.0') ⇒ Object
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.
- 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 ornil). - allowed_values
-
An array of allowed values for this field.
- version
-
Specifies the minimum version of the PDF specification needed for this value.
99 100 101 102 103 104 |
# File 'lib/hexapdf/dictionary.rb', line 99 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 |
.define_type(type) ⇒ Object
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).
132 133 134 |
# File 'lib/hexapdf/dictionary.rb', line 132 def self.define_type(type) @type = type end |
.each_field(&block) ⇒ Object
: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.
124 125 126 127 128 |
# File 'lib/hexapdf/dictionary.rb', line 124 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 |
.field(name) ⇒ Object
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.
110 111 112 113 114 115 116 |
# File 'lib/hexapdf/dictionary.rb', line 110 def self.field(name) if defined?(@fields) && @fields.key?(name) @fields[name] elsif superclass.respond_to?(:field) superclass.field(name) end end |
.type ⇒ Object
Returns the statically defined PDF type of the class.
See ::define_type
139 140 141 |
# File 'lib/hexapdf/dictionary.rb', line 139 def self.type defined?(@type) && @type end |
Instance Method Details
#[](name) ⇒ Object
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!
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/hexapdf/dictionary.rb', line 164 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 |
#[]=(name, data) ⇒ Object
Stores the data under name in the dictionary. Name has to be a Symbol object.
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.
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/hexapdf/dictionary.rb', line 186 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 |
#delete(name) ⇒ Object
Deletes the name-value pair from the dictionary and returns the value. If such a pair does not exist, nil is returned.
206 207 208 |
# File 'lib/hexapdf/dictionary.rb', line 206 def delete(name) value.delete(name) { nil } end |
#each ⇒ Object
: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 #[].
217 218 219 220 221 |
# File 'lib/hexapdf/dictionary.rb', line 217 def each return to_enum(__method__) unless block_given? value.each_key {|name| yield(name, self[name]) } self end |
#empty? ⇒ Boolean
Returns true if the dictionary contains no entries.
230 231 232 |
# File 'lib/hexapdf/dictionary.rb', line 230 def empty? value.empty? end |
#key?(key) ⇒ Boolean
Returns true if the given key is present in the dictionary and not nil.
200 201 202 |
# File 'lib/hexapdf/dictionary.rb', line 200 def key?(key) !value[key].nil? end |
#to_h ⇒ Object
Returns a dup of the underlying hash.
235 236 237 |
# File 'lib/hexapdf/dictionary.rb', line 235 def to_h value.dup end |
#type ⇒ Object
Returns, in order or availability, the value of ::type, the /Type field or the result of Object#type.
225 226 227 |
# File 'lib/hexapdf/dictionary.rb', line 225 def type self.class.type || self[:Type] || super end |