~ubuntu-branches/ubuntu/edgy/libcairo-ruby/edgy

« back to all changes in this revision

Viewing changes to samples/pac2.rb

  • Committer: Bazaar Package Importer
  • Author(s): Paul van Tilburg
  • Date: 2006-07-03 12:10:17 UTC
  • mfrom: (1.1.1 upstream) (2.1.1 edgy)
  • Revision ID: james.westby@ubuntu.com-20060703121017-bryi2b921t4x06tk
Tags: 1.2.0-1
* New upstream release.
* Fixed the URL in the description (courtesy of Tomas Pospisek,
  closes: #361857).
* Bumped standards version to 3.7.2; no changes required. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
=begin
 
2
  pac2.rb - rcairo sample script with #scale and #translate.
 
3
 
 
4
  Original: pac.rb in http://www.artima.com/rubycs/articles/pdf_writer3.html
 
5
=end
 
6
 
 
7
$LOAD_PATH.unshift "../packages/cairo/ext/"
 
8
$LOAD_PATH.unshift "../packages/cairo/lib/"
 
9
 
 
10
require "cairo"
 
11
 
 
12
def pac(surface, width, height)
 
13
  white = [1, 1, 1]
 
14
  black = [0, 0, 0]
 
15
  magenta = [1, 0, 1]
 
16
  cyan = [0, 1, 1]
 
17
  yellow = [1, 1, 0]
 
18
  blue = [0, 0, 1]
 
19
  
 
20
  cr = Cairo::Context.new(surface)
 
21
 
 
22
  # NOTE: You may need to set line width when use Cairo::Context#scale
 
23
  cr.set_line_width(cr.line_width / [width, height].max)
 
24
  
 
25
  cr.scale(width, height)
 
26
 
 
27
  cr.save do
 
28
    cr.set_source_rgb(*black)
 
29
    cr.rectangle(0, 0, 1, 1)
 
30
    cr.fill
 
31
  end
 
32
 
 
33
  # Wall
 
34
  wall_width = 0.89
 
35
  wall_height = 0.03
 
36
  wall_space = 0.5
 
37
  wall_x = 0.02
 
38
  wall1_y = 1 - 0.86
 
39
  wall2_y = wall1_y + wall_space
 
40
  wall_radius = 0.01
 
41
  
 
42
  cr.set_source_rgb(*magenta)
 
43
  cr.rounded_rectangle(wall_x, wall1_y, wall_width, wall_height, wall_radius)
 
44
  cr.fill
 
45
  cr.set_source_rgb(*cyan)
 
46
  cr.rounded_rectangle(wall_x, wall1_y, wall_width, wall_height, wall_radius)
 
47
  cr.stroke
 
48
  
 
49
  cr.set_source_rgb(*magenta)
 
50
  cr.rounded_rectangle(wall_x, wall2_y, wall_width, wall_height, wall_radius)
 
51
  cr.fill
 
52
  cr.set_source_rgb(*cyan)
 
53
  cr.rounded_rectangle(wall_x, wall2_y, wall_width, wall_height, wall_radius)
 
54
  cr.stroke
 
55
  
 
56
  # Body
 
57
  body_x = 0.17
 
58
  body_y = 1 - 0.58
 
59
  body_width = 0.23
 
60
  body_height = 0.33
 
61
  
 
62
  cr.save do
 
63
    cr.translate(body_x, body_y)
 
64
    cr.set_source_rgb(*yellow)
 
65
    cr.scale(body_width, body_height)
 
66
    cr.arc(0, 0, 0.5, 30 * (Math::PI / 180), 330 * (Math::PI / 180))
 
67
    cr.line_to(0, 0)
 
68
    cr.close_path
 
69
    cr.fill
 
70
  end
 
71
 
 
72
  # Dot
 
73
  dot_width = 0.02
 
74
  dot_height = 0.03
 
75
  small_dot_width = 0.01
 
76
  small_dot_height = 0.015
 
77
  dot_x = 0.29
 
78
  dot_y = 1 - 0.58
 
79
  dot_step = 0.05
 
80
  
 
81
  cr.save do
 
82
    cr.set_source_rgb(*yellow)
 
83
    cr.save do
 
84
      cr.translate(dot_x, dot_y)
 
85
      cr.scale(dot_width, dot_height)
 
86
      cr.circle(0, 0, 1).fill
 
87
    end
 
88
 
 
89
    4.times do |i|
 
90
      cr.save do
 
91
        cr.translate(dot_x + dot_step * (i + 1), dot_y)
 
92
        cr.scale(small_dot_width, small_dot_height)
 
93
        cr.circle(0, 0, 1).fill
 
94
      end
 
95
    end
 
96
   end
 
97
 
 
98
  # Ghost
 
99
  ghost_x = 0.59
 
100
  ghost_x_step = 0.03
 
101
  ghost_y = 1 - 0.42
 
102
  ghost_y_step = 0.04
 
103
  ghost_width = 0.18
 
104
  ghost_height = 0.29
 
105
  ghost_radius= 0.08
 
106
  cr.move_to(ghost_x, ghost_y)
 
107
  cr.line_to(ghost_x, ghost_y - ghost_height)
 
108
  cr.curve_to(ghost_x + ghost_width / 3.0,
 
109
              ghost_y - ghost_height - ghost_radius,
 
110
              ghost_x + ghost_width * (2.0 / 3.0),
 
111
              ghost_y - ghost_height - ghost_radius,
 
112
              ghost_x + ghost_width,
 
113
              ghost_y - ghost_height)
 
114
  cr.line_to(ghost_x + ghost_width, ghost_y)
 
115
  i = 0
 
116
  (ghost_x + ghost_width).step(ghost_x, -ghost_x_step) do |x|
 
117
    cr.line_to(x, ghost_y + -ghost_y_step * (i % 2))
 
118
    i += 1
 
119
  end
 
120
  cr.close_path
 
121
  
 
122
  cr.set_source_rgb(*blue)
 
123
  cr.fill_preserve
 
124
  cr.set_source_rgb(*cyan)
 
125
  cr.stroke
 
126
 
 
127
  # Ghost Eyes
 
128
  eye_x = 0.62
 
129
  eye_y = 1 - 0.63
 
130
  eye_space = 0.06
 
131
  white_eye_width = 0.03
 
132
  white_eye_height = 0.04
 
133
  black_eye_width = 0.01
 
134
  black_eye_height = 0.02
 
135
 
 
136
  cr.set_source_rgb(*white)
 
137
  cr.rectangle(eye_x, eye_y - white_eye_height,
 
138
               white_eye_width, white_eye_height)
 
139
  cr.fill
 
140
  cr.rectangle(eye_x + eye_space, eye_y - white_eye_height,
 
141
               white_eye_width, white_eye_height)
 
142
  cr.fill
 
143
  
 
144
  cr.set_source_rgb(*black)
 
145
  cr.rectangle(eye_x, eye_y - black_eye_height,
 
146
               black_eye_width, black_eye_height)
 
147
  cr.fill
 
148
  cr.rectangle(eye_x + eye_space, eye_y - black_eye_height,
 
149
               black_eye_width, black_eye_height)
 
150
  cr.fill
 
151
 
 
152
  cr.show_page
 
153
end
 
154
 
 
155
width = 841.889763779528
 
156
height = 595.275590551181
 
157
 
 
158
surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, width, height)
 
159
cr = pac(surface, width, height)
 
160
cr.target.write_to_png("pac2.png")
 
161
 
 
162
scalable_surface_output = Proc.new do |surface_class_name, suffix|
 
163
  if Cairo.const_defined?(surface_class_name)
 
164
    surface_class = Cairo.const_get(surface_class_name)
 
165
    surface = surface_class.new("pac2.#{suffix}", width, height)
 
166
    cr = pac(surface, width, height)
 
167
    cr.target.finish
 
168
  else
 
169
    puts("#{surface_class_name} isn't supported.")
 
170
  end
 
171
end
 
172
 
 
173
scalable_surface_output.call("PSSurface", "ps")
 
174
scalable_surface_output.call("PDFSurface", "pdf")
 
175
scalable_surface_output.call("SVGSurface", "svg")