Class: Prune::Shapes::Rectangle

Inherits:
Base
  • Object
show all
Defined in:
lib/prune/shapes/rectangle.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, x, y, width, height, options = {}) ⇒ Rectangle

Draw rectangle.



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

def initialize(page, x, y, width, height, options = {})
  super(page)
  # Check arguments.
  raise RectangleArgumentError unless x.is_a?(Numeric)
  raise RectangleArgumentError unless y.is_a?(Numeric)
  raise RectangleArgumentError unless width.is_a?(Numeric)
  raise RectangleArgumentError unless height.is_a?(Numeric)

  # Set instance variables.
  @id = options[:id] if options.key?(:id)
  @x = x
  @y = y
  @width = width
  @height = height
  @options = options

  # Parse options.
  @style = options[:style] || :none
  @border_width = options[:width] || 1.0
  @border_color = options[:color] || "#000000"
  @fill = options[:fill] || false
  @fill_color = options[:fill_color] || "#000000"
end

Instance Method Details

#renderObject

Render shape.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/prune/shapes/rectangle.rb', line 37

def render
  stream = []
  boundary = [@x, @y, @width, -@height]
  stream << "q"
  # Set fill color.
  if @fill
    stream << "%s rg" % convert_color_str(@fill_color)
    stream << "%.2f %.2f %.2f %.2f re" % boundary
    stream << "f"
  end
  # Draw boundary.
  unless @style == :none
    raise RectangleStyleError unless STYLES.include?(@style)
    stream << "2 J"
    stream << "0 j"
    stream << STYLES[@style]
    stream << "%s RG" % convert_color_str(@border_color)
    stream << "%.2f w" % @border_width
    stream << "%.2f %.2f %.2f %.2f re" % boundary
    stream << "s"
  end
  stream << "Q"
  super(stream)
end