Class: Prawn::Canvas

Inherits:
Object
  • Object
show all
Includes:
Chunkable
Defined in:
lib/prawn/canvas.rb

Constant Summary collapse

KAPPA =
4.0 * ((Math.sqrt(2) - 1.0) / 3.0)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Chunkable

#chunk, #find_chunks, included, #to_pdf

Constructor Details

#initializeCanvas

Returns a new instance of Canvas.



5
6
7
# File 'lib/prawn/canvas.rb', line 5

def initialize
  @chunks = []
end

Instance Attribute Details

#chunksObject

Returns the value of attribute chunks.



9
10
11
# File 'lib/prawn/canvas.rb', line 9

def chunks
  @chunks
end

Instance Method Details

#circle!(params) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/prawn/canvas.rb', line 81

def circle!(params)
  chunk(:circle, params) do
    ellipse!(:point    => params[:point],
             :x_radius => params[:radius],
             :y_radius => params[:radius]) 
                
  end
end

#curve!(params) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/prawn/canvas.rb', line 36

def curve!(params)
  chunk(:curve, params) do |c|
    [ move_to!(:point => params[:point1]),
      curve_to!(:point  => params[:point2],
                :bound1 => params[:bound1],
                :bound2 => params[:bound2]) ]

  end
end

#curve_to!(params) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/prawn/canvas.rb', line 26

def curve_to!(params)
  unless params[:bound1] && params[:bound2]
    raise Prawn::Errors::InvalidGraphicsPath
  end

  chunk(:curve_to, params) do |c|
    "%.3f %.3f %.3f %.3f %.3f %.3f c" % (c[:bound1] + c[:bound2] + c[:point])
  end
end

#ellipse!(params) ⇒ Object



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
73
74
75
76
77
78
79
# File 'lib/prawn/canvas.rb', line 48

def ellipse!(params)
  chunk(:ellipse, params) do |c|
    x, y = c[:point]
    r1   = c[:x_radius]
    r2   = c[:y_radius]

    l1 = r1 * KAPPA
    l2 = r2 * KAPPA

    start          =  move_to!(:point => [x + r1, y])

    to_upper_right = curve_to!(:point  => [x,  y + r2],
                               :bound1 => [x + r1, y + l1], 
                               :bound2 => [x + l2, y + r2])
    to_upper_left = curve_to!(:point => [x - r1, y],
                              :bound1 => [x - l2, y + r2],
                              :bound2 => [x - r1, y + l1])
    
    to_lower_left = curve_to!(:point => [x, y - r2],
                              :bound1 => [x - r1, y - l1], 
                              :bound2 => [x - l2, y - r2])

    to_lower_right = curve_to!(:point  => [x + r1, y],
                               :bound1 => [x + l2, y - r2], 
                               :bound2 => [x + r1, y - l1])

    back_to_start = move_to!(:point => [x,y])

    [start, to_upper_right, to_upper_left,
            to_lower_left,  to_lower_right, back_to_start]
  end
end

#fill!Object



101
102
103
# File 'lib/prawn/canvas.rb', line 101

def fill!
  chunk(:fill) { "F" }
end

#fill_and_stroke!Object



105
106
107
# File 'lib/prawn/canvas.rb', line 105

def fill_and_stroke!
  chunk(:fill_and_stroke) { "b" }
end

#line!(params) ⇒ Object



90
91
92
93
94
95
# File 'lib/prawn/canvas.rb', line 90

def line!(params)
  chunk(:line, params) do |c|
    [ move_to!(:point => c.params[:point1]), 
      line_to!(:point => c.params[:point2]) ]
  end
end

#line_to!(params) ⇒ Object



20
21
22
23
24
# File 'lib/prawn/canvas.rb', line 20

def line_to!(params)
  chunk(:line_to, params) do |c|
    "%.3f %.3f l" % c[:point]
  end
end

#move_to!(params) ⇒ Object



14
15
16
17
18
# File 'lib/prawn/canvas.rb', line 14

def move_to!(params)
  chunk(:move_to, params) do |c|
    "%.3f %.3f m" % c[:point]
  end
end

#rectangle!(params) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/prawn/canvas.rb', line 109

def rectangle!(params)
  chunk(:rectangle, params) do |c|
    x,y = params[:point]
    y  -= params[:height]

    "%.3f %.3f %.3f %.3f re" % [ x, y, params[:width], params[:height] ]
  end
end

#stroke!Object



97
98
99
# File 'lib/prawn/canvas.rb', line 97

def stroke!
  chunk(:stroke) { "S" }
end