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:

{
  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 which may be an explicit destination array or a dictionary with a /D entry (as allowed for a named destination).



134
135
136
# File 'lib/hexapdf/document/destinations.rb', line 134

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

Class Method Details

.valid?(destination) ⇒ Boolean

Returns true if the destination is valid.

Returns:

  • (Boolean)


126
127
128
129
130
# File 'lib/hexapdf/document/destinations.rb', line 126

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.



191
192
193
194
195
196
197
198
# File 'lib/hexapdf/document/destinations.rb', line 191

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.



157
158
159
160
161
162
163
164
# File 'lib/hexapdf/document/destinations.rb', line 157

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.



147
148
149
# File 'lib/hexapdf/document/destinations.rb', line 147

def page
  @destination[0]
end

#remote?Boolean

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

Returns:

  • (Boolean)


139
140
141
# File 'lib/hexapdf/document/destinations.rb', line 139

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

#rightObject

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



181
182
183
184
185
186
187
188
# File 'lib/hexapdf/document/destinations.rb', line 181

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.



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/hexapdf/document/destinations.rb', line 167

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.



152
153
154
# File 'lib/hexapdf/document/destinations.rb', line 152

def type
  TYPE_MAPPING[@destination[1]]
end

#valid?Boolean

Returns true if the destination is valid.

Returns:

  • (Boolean)


211
212
213
# File 'lib/hexapdf/document/destinations.rb', line 211

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

#zoomObject

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



201
202
203
204
205
206
207
208
# File 'lib/hexapdf/document/destinations.rb', line 201

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