File: polygon.rb

package info (click to toggle)
ruby-rghost 0.9.9-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,188 kB
  • sloc: ruby: 3,374; makefile: 6; sh: 1
file content (72 lines) | stat: -rw-r--r-- 1,781 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Draw one shape based in relative node points.
#===Options
#
# * <tt>:x and :y</tt> - Initial position.
# * <tt>:content</tt> - Facade to ShapeContent with same parameters.
# * <tt>:border</tt> - Facade to Border with same parameters.
#===Examples
#
# doc.polygon :x => 3.5, :y => 5.5 do
#   node :x => 4,  :y => 0
#   node :x => 0,  :y => -4
#   node :x => -4, :y => 0
#   node :x => 0,  :y => 4
# end
#
# link:images/polygon01.png
#
# doc.polygon :x => 3.5, :y => 4.5 do
#   node :x => 2,  :y => 2
#   node :x => 2,  :y => -2
#   node :x => -2, :y => -2
# end
#
# link:images/polygon02.png
#
# doc.polygon :x => 1, :y => 5, :border => {:width => 2, :linejoin => 1} do
#   node :x => 2,  :y => 2/2
#   node :x => 2*2,:y => -2
#   node :x => -1, :y => -3
#   node :x => 2,  :y => 1
#   node :x => 3,  :y => 2
# end
#
# link:images/polygon03.png
class RGhost::Polygon < RGhost::PsObject
  attr_reader :points

  DEFAULT_OPTIONS = {
    x: :limit_left,
    y: :current_row,
    content: RGhost::ShapeContent::DEFAULT_OPTIONS,
    border: RGhost::Border::DEFAULT_OPTIONS

  }
  def initialize(options = {}, &block)
    super("") {}

    @options = DEFAULT_OPTIONS.dup.merge(options)
    @points = []
    instance_eval(&block) if block
  end

  # Creates new relative point by :x => 2 and :y => 4. Used as instance_eval
  def node(point)
    @points << point
  end

  def ps
    graph = RGhost::Graphic.new
    graph.set RGhost::Cursor.moveto(@options)
    graph.set RGhost::Border.new(@options[:border]) if @options[:border]

    @points.each { |p| graph.set RGhost::Line.rlineto(p) }

    graph.raw :closepath
    # graph.raw :gsave
    graph.set RGhost::ShapeContent.new(@options[:content]) if @options[:content]
    # graph.raw :grestore
    graph.raw :stroke
    graph
  end
end