2
pac2.rb - rcairo sample script with #scale and #translate.
4
Original: pac.rb in http://www.artima.com/rubycs/articles/pdf_writer3.html
7
$LOAD_PATH.unshift "../packages/cairo/ext/"
8
$LOAD_PATH.unshift "../packages/cairo/lib/"
12
def pac(surface, width, height)
20
cr = Cairo::Context.new(surface)
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)
25
cr.scale(width, height)
28
cr.set_source_rgb(*black)
29
cr.rectangle(0, 0, 1, 1)
39
wall2_y = wall1_y + wall_space
42
cr.set_source_rgb(*magenta)
43
cr.rounded_rectangle(wall_x, wall1_y, wall_width, wall_height, wall_radius)
45
cr.set_source_rgb(*cyan)
46
cr.rounded_rectangle(wall_x, wall1_y, wall_width, wall_height, wall_radius)
49
cr.set_source_rgb(*magenta)
50
cr.rounded_rectangle(wall_x, wall2_y, wall_width, wall_height, wall_radius)
52
cr.set_source_rgb(*cyan)
53
cr.rounded_rectangle(wall_x, wall2_y, wall_width, wall_height, wall_radius)
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))
75
small_dot_width = 0.01
76
small_dot_height = 0.015
82
cr.set_source_rgb(*yellow)
84
cr.translate(dot_x, dot_y)
85
cr.scale(dot_width, dot_height)
86
cr.circle(0, 0, 1).fill
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
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)
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))
122
cr.set_source_rgb(*blue)
124
cr.set_source_rgb(*cyan)
131
white_eye_width = 0.03
132
white_eye_height = 0.04
133
black_eye_width = 0.01
134
black_eye_height = 0.02
136
cr.set_source_rgb(*white)
137
cr.rectangle(eye_x, eye_y - white_eye_height,
138
white_eye_width, white_eye_height)
140
cr.rectangle(eye_x + eye_space, eye_y - white_eye_height,
141
white_eye_width, white_eye_height)
144
cr.set_source_rgb(*black)
145
cr.rectangle(eye_x, eye_y - black_eye_height,
146
black_eye_width, black_eye_height)
148
cr.rectangle(eye_x + eye_space, eye_y - black_eye_height,
149
black_eye_width, black_eye_height)
155
width = 841.889763779528
156
height = 595.275590551181
158
surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, width, height)
159
cr = pac(surface, width, height)
160
cr.target.write_to_png("pac2.png")
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)
169
puts("#{surface_class_name} isn't supported.")
173
scalable_surface_output.call("PSSurface", "ps")
174
scalable_surface_output.call("PDFSurface", "pdf")
175
scalable_surface_output.call("SVGSurface", "svg")