Class: Prune::Shapes::Line

Inherits:
Base
  • Object
show all
Defined in:
lib/prune/shapes/line.rb

Constant Summary collapse

STYLES =
{
  :solid => "[] 0 d",
  :dashed => "[5 5] 0 d"
}

Instance Attribute Summary

Attributes inherited from Base

#height, #id, #width, #x, #y

Instance Method Summary collapse

Methods included from Functions

#mm_to_pt, #pt_to_mm

Constructor Details

#initialize(page, from_x, from_y, to_x, to_y, options = {}) ⇒ Line

Draw line.

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/prune/shapes/line.rb', line 12

def initialize(page, from_x, from_y, to_x, to_y, options = {})
  super(page)
  # Check arguments.
  raise LineArgumentError unless from_x.is_a?(Numeric)
  raise LineArgumentError unless from_y.is_a?(Numeric)
  raise LineArgumentError unless to_x.is_a?(Numeric)
  raise LineArgumentError unless to_y.is_a?(Numeric)
  # Set instance variables.
  @id = options[:id] if options.key?(:id)
  @x = from_x
  @y = from_y
  @to_x = to_x
  @to_y = to_y
  @width = to_x - from_x
  @height = -(to_y - from_y)
  @options = options
  # Parse options.
  @style = options[:style] || :none
  @width = options[:width] || 1.0
  @color = options[:color] || "#000000"
end

Instance Method Details

#renderObject

Render shape.

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/prune/shapes/line.rb', line 35

def render
  return [] if @style == :none
  return [] if @width <= 0.0
  raise LineStyleError unless STYLES.include?(@style)
  stream = []
  stream << "q"
  stream << "2 J"
  stream << "0 j"
  stream << STYLES[@style]
  stream << "%s RG" % convert_color_str(@color)
  stream << "%.2f w" % width
  stream << "%.2f %.2f m" % [@x, @y]
  stream << "%.2f %.2f l" % [@to_x, @to_y]
  stream << "s"
  stream << "Q"
  super(stream)
end