Method: HexaPDF::Content::Canvas#line_dash_pattern

Defined in:
lib/hexapdf/content/canvas.rb

#line_dash_pattern(value = nil, phase = 0, &block) ⇒ Object Also known as: line_dash_pattern=

:call-seq:

canvas.line_dash_pattern                                  => current_line_dash_pattern
canvas.line_dash_pattern(line_dash_pattern)               => canvas
canvas.line_dash_pattern(length, phase = 0)               => canvas
canvas.line_dash_pattern(array, phase = 0)                => canvas
canvas.line_dash_pattern(value, phase = 0) { block }      => canvas

The line dash pattern defines the appearance of a stroked path (line or curve), ie. if it is solid or if it contains dashes and gaps.

There are multiple ways to set the line dash pattern:

  • By providing a LineDashPattern object

  • By providing a single Integer/Float that is used for both dashes and gaps

  • By providing an array of Integers/Floats that specify the alternating dashes and gaps

The phase (i.e. the distance into the dashes/gaps at which to start) can additionally be set in the last two cases.

A solid line can be achieved by using 0 for the length or by using an empty array.

Returns the current line dash pattern (a LineDashPattern object, see GraphicsState#line_dash_pattern) when no argument is given. Otherwise sets the line dash pattern using the given arguments and returns self. The setter version can also be called in the line_dash_pattern= form (but only without the second argument!).

If arguments and a block are provided, the changed line dash pattern is only active during the block by saving and restoring the graphics state.

Examples:

#>pdf
canvas.line_dash_pattern(10)
canvas.line_dash_pattern                # => LineDashPattern.new([10], 0)
canvas.line_dash_pattern(10, 2)
canvas.line_dash_pattern([5, 3, 1], 2)
canvas.line_dash_pattern = HexaPDF::Content::LineDashPattern.new([5, 3, 1], 1)

canvas.line_dash_pattern(10) do
  canvas.line_dash_pattern              # => LineDashPattern.new([10], 0)
end
canvas.line_dash_pattern                # => LineDashPattern.new([5, 3, 1], 1)

# visual example
[10, [10, 2], [[5, 3, 1], 2]].each_with_index do |pattern, index|
   canvas.line_dash_pattern(*pattern)
   canvas.line_width(10).line(50 + index * 50, 30, 50 + index * 50, 170).
     stroke
end

See: PDF2.0 s8.4.3.5, LineDashPattern



828
829
830
831
# File 'lib/hexapdf/content/canvas.rb', line 828

def line_dash_pattern(value = nil, phase = 0, &block)
  gs_getter_setter(:line_dash_pattern, :d, value && LineDashPattern.normalize(value, phase),
                   &block)
end