Class: HexaPDF::Document::Destinations::Destination

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/document/destinations.rb

Overview

Wraps an explicit destination array to allow easy access to query its properties.

A *destination array* has the form

[page, type, *arguments]

where page is either a page object or a page number (in case of a destination to a page in a remote PDF document), type is the destination type (see below) and arguments are the required arguments for the specific type of destination.

Destination Types

There are eight different types of destinations, each taking different arguments. The arguments are marked up in the list below and are in the correct order for use in the destination array. The first name in the list is the PDF internal name, the second one the explicit, more descriptive one used by HexaPDF (though the PDF internal name can also be used):

:XYZ, :xyz

Display the page with the given (left, top) coordinate at the upper-left corner of the window and the specified magnification (zoom) factor. A nil value for any argument means not changing it from the current value.

:Fit, :fit_page

Display the page so that it fits horizontally and vertically within the window.

:FitH, :fit_page_horizontal

Display the page so that it fits horizontally within the window, with the given top coordinate being at the top of the window. A nil value for top means not changing it from the current value.

:FitV, :fit_page_vertical

Display the page so that it fits vertically within the window, with the given left coordinate being at the left of the window. A nil value for left means not changing it from the current value.

:FitR, :fit_rectangle

Display the page so that the rectangle specified by (left, bottom)-(right, top) fits horizontally and vertically within the window.

:FitB, :fit_bounding_box

Display the page so that its bounding box fits horizontally and vertically within the window.

:FitBH, :fit_bounding_box_horizontal

Display the page so that its bounding box fits horizontally within the window, with the given top coordinate being at the top of the window. A nil value for top means not changing it from the current value.

:FitBV, :fit_bounding_box_vertical

Display the page so that its bounding box fits vertically within the window, with the given left coordinate being at the left of the window. A nil value for left means not changing it from the current value.

Constant Summary collapse

TYPE_MAPPING =

:nodoc:

{ #:nodoc:
  XYZ: :xyz,
  Fit: :fit_page,
  FitH: :fit_page_horizontal,
  FitV: :fit_page_vertical,
  FitR: :fit_rectangle,
  FitB: :fit_bounding_box,
  FitBH: :fit_bounding_box_horizontal,
  FitBV: :fit_bounding_box_vertical,
}
REVERSE_TYPE_MAPPING =

:nodoc:

Hash[*TYPE_MAPPING.flatten.reverse]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(destination) ⇒ Destination

Creates a new Destination for the given destination specification which may be an explicit destination array or a dictionary with a /D entry (as allowed for a named destination).



133
134
135
136
137
138
139
# File 'lib/hexapdf/document/destinations.rb', line 133

def initialize(destination)
  @destination = if destination.kind_of?(HexaPDF::Dictionary) || destination.kind_of?(Hash)
                   destination[:D]
                 else
                   destination
                 end
end

Class Method Details

.valid?(destination) ⇒ Boolean

Returns true if the destination is valid.

Returns:

  • (Boolean)


124
125
126
127
128
# File 'lib/hexapdf/document/destinations.rb', line 124

def self.valid?(destination)
  TYPE_MAPPING.key?(destination[1]) &&
    (destination[0].kind_of?(Integer) || destination[0]&.type == :Page) &&
    destination[2..-1].all? {|item| item.nil? || item.kind_of?(Numeric) }
end

Instance Method Details

#bottomObject

Returns the argument bottom if used by the destination, raises an error otherwise.



194
195
196
197
198
199
200
201
# File 'lib/hexapdf/document/destinations.rb', line 194

def bottom
  case type
  when :fit_rectangle
    @destination[3]
  else
    raise HexaPDF::Error, "No such argument for destination type #{type}"
  end
end

#leftObject

Returns the argument left if used by the destination, raises an error otherwise.



160
161
162
163
164
165
166
167
# File 'lib/hexapdf/document/destinations.rb', line 160

def left
  case type
  when :xyz, :fit_page_vertical, :fit_rectangle, :fit_bounding_box_vertical
    @destination[2]
  else
    raise HexaPDF::Error, "No such argument for destination type #{type}"
  end
end

#pageObject

Returns the referenced page.

The return value is either a page object or, in case of a destination to a remote document, a page number.



150
151
152
# File 'lib/hexapdf/document/destinations.rb', line 150

def page
  @destination[0]
end

#remote?Boolean

Returns true if the destination references a destination in a remote document.

Returns:

  • (Boolean)


142
143
144
# File 'lib/hexapdf/document/destinations.rb', line 142

def remote?
  @destination[0].kind_of?(Numeric)
end

#rightObject

Returns the argument right if used by the destination, raises an error otherwise.



184
185
186
187
188
189
190
191
# File 'lib/hexapdf/document/destinations.rb', line 184

def right
  case type
  when :fit_rectangle
    @destination[4]
  else
    raise HexaPDF::Error, "No such argument for destination type #{type}"
  end
end

#topObject

Returns the argument top if used by the destination, raises an error otherwise.



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/hexapdf/document/destinations.rb', line 170

def top
  case type
  when :xyz
    @destination[3]
  when :fit_page_horizontal, :fit_bounding_box_horizontal
    @destination[2]
  when :fit_rectangle
    @destination[5]
  else
    raise HexaPDF::Error, "No such argument for destination type #{type}"
  end
end

#typeObject

Returns the type of destination.



155
156
157
# File 'lib/hexapdf/document/destinations.rb', line 155

def type
  TYPE_MAPPING[@destination[1]]
end

#valid?Boolean

Returns true if the destination is valid.

Returns:

  • (Boolean)


214
215
216
# File 'lib/hexapdf/document/destinations.rb', line 214

def valid?
  self.class.valid?(@destination)
end

#valueObject

Returns the wrapped destination array.



219
220
221
# File 'lib/hexapdf/document/destinations.rb', line 219

def value
  @destination
end

#zoomObject

Returns the argument zoom if used by the destination, raises an error otherwise.



204
205
206
207
208
209
210
211
# File 'lib/hexapdf/document/destinations.rb', line 204

def zoom
  case type
  when :xyz
    @destination[4]
  else
    raise HexaPDF::Error, "No such argument for destination type #{type}"
  end
end