Class: HexaPDF::Content::LineDashPattern

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/content/graphics_state.rb

Overview

The line dash pattern defines how a line should be dashed. For use with e.g. Canvas#line_dash_pattern.

A dash pattern consists of two parts: the dash array and the dash phase. The dash array defines the length of alternating dashes and gaps (important: starting with dashes). And the dash phase defines the distance into the dash array at which to start.

It is easier to show. Following are dash arrays and dash phases and how they would be interpreted:

[] 0                      No dash, one solid line
[3] 0                     3 unit dash, 3 unit gap, 3 unit dash, 3 unit gap, ...
[3] 1                     2 unit dash, 3 unit gap, 3 unit dash, 3 unit gap, ...
[2 1] 0                   2 unit dash, 1 unit gap, 2 unit dash, 1 unit gap, ...
[3 5] 6                   2 unit gap, 3 unit dash, 5 unit gap, 3 unit dash, ...
[2 3] 6                   1 unit dash, 3 unit gap, 2 unit dash, 3 unit gap, ...

And visualized it looks like this:

#>pdf-canvas-hide
canvas.line_width(2)
[[[], 0], [[3], 0], [[3], 1], [[2, 1], 0],
 [[3, 5], 6], [[2, 3], 6]].each_with_index do |(arr, phase), index|
  canvas.line_dash_pattern(arr, phase)
  canvas.line(20, 180 - index * 30, 180, 180 - index * 30).stroke
end

See: PDF2.0 s8.4.3.6

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array = [], phase = 0) ⇒ LineDashPattern

Inititalizes the line dash pattern with the given array and phase.

The argument phase must be non-negative and the numbers in the array must be non-negative and must not all be zero.



253
254
255
256
257
258
259
260
# File 'lib/hexapdf/content/graphics_state.rb', line 253

def initialize(array = [], phase = 0)
  if phase < 0 || (!array.empty? &&
    array.inject(0) {|m, n| m < 0 ? m : (n < 0 ? -1 : m + n) } <= 0)
    raise ArgumentError, "Invalid line dash pattern: #{array.inspect} #{phase.inspect}"
  end
  @array = array.freeze
  @phase = phase
end

Instance Attribute Details

#arrayObject (readonly)

The dash array.



244
245
246
# File 'lib/hexapdf/content/graphics_state.rb', line 244

def array
  @array
end

#phaseObject (readonly)

The dash phase.



247
248
249
# File 'lib/hexapdf/content/graphics_state.rb', line 247

def phase
  @phase
end

Class Method Details

.normalize(array, phase = 0) ⇒ Object

:call-seq:

LineDashPattern.normalize(line_dash_pattern)         -> line_dash_pattern
LineDashPattern.normalize(array, phase = 0)          -> LineDashPattern.new(array, phase)
LineDashPattern.normalize(number, phase = 0)         -> LineDashPattern.new([number], phase)
LineDashPattern.normalize(0)                         -> LineDashPattern.new

Returns the arguments normalized to a valid LineDashPattern instance.

If array is 0, the default line dash pattern representing a solid line will be used. If it is a single number, it will be converted into an array holding that number.



232
233
234
235
236
237
238
239
240
241
# File 'lib/hexapdf/content/graphics_state.rb', line 232

def self.normalize(array, phase = 0)
  case array
  when LineDashPattern then array
  when Array then new(array, phase)
  when 0 then new
  when Numeric then new([array], phase)
  else
    raise ArgumentError, "Unknown line dash pattern: #{array} / #{phase}"
  end
end

Instance Method Details

#==(other) ⇒ Object

Returns true if the other line dash pattern is the same as this one.



263
264
265
# File 'lib/hexapdf/content/graphics_state.rb', line 263

def ==(other)
  other.kind_of?(self.class) && other.array == array && other.phase == phase
end

#to_operandsObject

Converts the LineDashPattern object to an array of operands for the associated PDF content operator.



269
270
271
# File 'lib/hexapdf/content/graphics_state.rb', line 269

def to_operands
  [@array, @phase]
end