Module: CairoTools

Includes:
Color
Included in:
ImageGenerator, Shape
Defined in:
lib/cairo_tools.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Color

#hsl, #rgb

Instance Attribute Details

#bottom_marginObject (readonly)

Returns the value of attribute bottom_margin.



12
13
14
# File 'lib/cairo_tools.rb', line 12

def bottom_margin
  @bottom_margin
end

#canvas_heightObject (readonly)

Returns the value of attribute canvas_height.



12
13
14
# File 'lib/cairo_tools.rb', line 12

def canvas_height
  @canvas_height
end

#canvas_widthObject (readonly)

Returns the value of attribute canvas_width.



12
13
14
# File 'lib/cairo_tools.rb', line 12

def canvas_width
  @canvas_width
end

#crObject (readonly)

Returns the value of attribute cr.



12
13
14
# File 'lib/cairo_tools.rb', line 12

def cr
  @cr
end

#left_marginObject (readonly)

Returns the value of attribute left_margin.



12
13
14
# File 'lib/cairo_tools.rb', line 12

def left_margin
  @left_margin
end

#previewObject

Returns the value of attribute preview.



13
14
15
# File 'lib/cairo_tools.rb', line 13

def preview
  @preview
end

#right_marginObject (readonly)

Returns the value of attribute right_margin.



12
13
14
# File 'lib/cairo_tools.rb', line 12

def right_margin
  @right_margin
end

#surfaceObject (readonly)

Returns the value of attribute surface.



12
13
14
# File 'lib/cairo_tools.rb', line 12

def surface
  @surface
end

#top_marginObject (readonly)

Returns the value of attribute top_margin.



12
13
14
# File 'lib/cairo_tools.rb', line 12

def top_margin
  @top_margin
end

Instance Method Details

#circular_text(x, y, radius, font_size, text) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cairo_tools.rb', line 72

def circular_text(x, y, radius, font_size, text)
  radians = proc {|text| cr.set_font_size(font_size); cr.text_extents(text).x_advance/radius}
  blank = (2*Math::PI - radians[text])/2
  start = blank + Math::PI/2
  partial = ''
  text.split(//).each do |letter|
    theta = start + radians[partial]
    cr.move_to(x+radius*Math.cos(theta), y+radius*Math.sin(theta))
    cr.set_font_matrix Cairo::Matrix.identity.rotate(theta + Math::PI/2).scale(font_size, font_size)
    cr.show_text letter
    theta += radians[letter]
    partial << letter
  end
end

#clip!Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cairo_tools.rb', line 139

def clip!
  i = self.class.new
  w, h = @canvas_width, @canvas_height
  pattern = Cairo::SurfacePattern.new(@surface)
  clip = cr.copy_path
  i.instance_eval do
    dimensions w, h
    cr.append_path clip
    cr.clip
    cr.set_source(pattern)
    cr.paint
  end
  dimensions w, h
  cr.set_source(Cairo::SurfacePattern.new(i.surface))
  cr.paint
end

#cloudsObject



179
180
181
# File 'lib/cairo_tools.rb', line 179

def clouds
  Cairo::ImageSurface.new(129, 129)
end

#create_text_box(x, y, width = nil, height = nil, valign = :top) ⇒ Object



87
88
89
# File 'lib/cairo_tools.rb', line 87

def create_text_box(x, y, width=nil, height=nil, valign=:top)
  TextBox.new(self, x, y, width, height, valign)
end

#dimensions(width, height) ⇒ Object



23
24
25
26
27
# File 'lib/cairo_tools.rb', line 23

def dimensions(width, height)
  @canvas_width, @canvas_height = width, height
  @surface = Cairo::ImageSurface.new(width, height)
  @cr = Cairo::Context.new(surface)
end

#draw_image(image, x = 0, y = 0, a = 1) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/cairo_tools.rb', line 169

def draw_image(image, x=0, y=0, a=1)
  i = self.class.new
  i.instance_eval do
    draw(image)
  end
  cr.set_source(Cairo::SurfacePattern.new(i.surface))
  cr.source.matrix = Cairo::Matrix.identity.translate(-x, -y)
  cr.paint_with_alpha(a)
end

#draw_text_box(x, y, width = nil, height = nil, valign = :top) {|tb| ... } ⇒ Object

Yields:

  • (tb)


91
92
93
94
95
# File 'lib/cairo_tools.rb', line 91

def draw_text_box(x, y, width=nil, height=nil, valign=:top)
  tb = create_text_box(x, y, width, height, valign)
  yield tb
  tb.draw
end

#generate_image(path, options) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/cairo_tools.rb', line 15

def generate_image(path, options)
  # dummy context is useful sometimes
  @surface = Cairo::ImageSurface.new(1, 1)
  @cr = Cairo::Context.new(surface)
  draw(*options)
  cr.target.write_to_png(path)
end

#get_pixel(x, y) ⇒ Object



129
130
131
# File 'lib/cairo_tools.rb', line 129

def get_pixel(x, y)
  @surface.get_pixel(x, y)
end

#gradient(gradient, *colors) ⇒ Object



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

def gradient(gradient, *colors)
  colors.each_with_index do |color, i|
    array = color.respond_to?(:to_rgb) ? color.to_rgb.to_a : color.to_a
    gradient.add_color_stop(i.to_f/(colors.length - 1), *array)
  end
  cr.set_source(gradient)
end

#heightObject



40
41
42
# File 'lib/cairo_tools.rb', line 40

def height
  canvas_height - top_margin - bottom_margin
end

#linear_gradient(x0, y0, x1, y1, *colors) ⇒ Object



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

def linear_gradient(x0, y0, x1, y1, *colors)
  gradient(Cairo::LinearPattern.new(x0, y0, x1, y1), *colors)
end

#load_image(path, x = 0, y = 0) ⇒ Object



133
134
135
136
137
# File 'lib/cairo_tools.rb', line 133

def load_image(path, x=0, y=0)
  image = Gdk::Pixbuf.new(File.join(File.dirname($0), path))
  cr.set_source_pixbuf(image)
  cr.source.matrix = Cairo::Matrix.identity.translate(x, y)
end

#margin(*rect) ⇒ Object



29
30
31
32
33
34
# File 'lib/cairo_tools.rb', line 29

def margin(*rect)
  rect = rect + rect if rect.length == 1
  rect = rect + rect if rect.length == 2
  @top_margin, @right_margin, @bottom_margin, @left_margin = rect
  cr.matrix = Cairo::Matrix.identity.translate(left_margin, top_margin)
end

#radial_gradient(cx0, cy0, r0, cx1, cy1, r1, *colors) ⇒ Object



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

def radial_gradient(cx0, cy0, r0, cx1, cy1, r1, *colors)
  gradient(Cairo::RadialPattern.new(cx0, cy0, r0, cx1, cy1, r1), *colors)
end

#rounded_rectangle(x, y, w, h, radius_x = 5, radius_y = radius_x) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cairo_tools.rb', line 52

def rounded_rectangle(x, y, w, h, radius_x=5, radius_y=radius_x)
  arc_to_bezier = 0.55228475
  radius_x = w / 2 if radius_x > w - radius_x
  radius_y = h / 2 if radius_y > h - radius_y
  c1 = arc_to_bezier * radius_x
  c2 = arc_to_bezier * radius_y

  cr.new_path
  cr.move_to(x + radius_x, y)
  cr.rel_line_to(w - 2 * radius_x, 0.0)
  cr.rel_curve_to(c1, 0.0, radius_x, c2, radius_x, radius_y)
  cr.rel_line_to(0, h - 2 * radius_y)
  cr.rel_curve_to(0.0, c2, c1 - radius_x, radius_y, -radius_x, radius_y)
  cr.rel_line_to(-w + 2 * radius_x, 0)
  cr.rel_curve_to(-c1, 0, -radius_x, -c2, -radius_x, -radius_y)
  cr.rel_line_to(0, -h + 2 * radius_y)
  cr.rel_curve_to(0.0, -c2, radius_x - c1, -radius_y, radius_x, -radius_y)
  cr.close_path
end

#set_color(color) ⇒ Object



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

def set_color(color)
  cr.set_source_rgba(*color.to_rgb.to_a)
end

#shadow(radius = 3, alpha = 1) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cairo_tools.rb', line 117

def shadow(radius=3, alpha=1)
  shadow_surface = Cairo::ImageSurface.new(@canvas_width, @canvas_height)
  shadow_cr = Cairo::Context.new(shadow_surface)
  shadow_cr.set_source_rgba(0, 0, 0, alpha)
  shadow_cr.mask(Cairo::SurfacePattern.new(surface))
  shadow_surface.blur(radius)
  shadow_cr.set_source(Cairo::SurfacePattern.new(surface))
  shadow_cr.paint
  @surface = shadow_surface
  @cr = shadow_cr
end

#transform(matrix, &block) ⇒ Object



44
45
46
47
48
49
# File 'lib/cairo_tools.rb', line 44

def transform(matrix, &block)
  old_matrix = cr.matrix
  cr.matrix = matrix
  yield
  cr.matrix = old_matrix
end

#transparent!(alpha) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/cairo_tools.rb', line 156

def transparent!(alpha)
  i = self.class.new
  w, h = @canvas_width, @canvas_height
  pattern = Cairo::SurfacePattern.new(@surface)
  i.instance_eval do
    dimensions w, h
    cr.set_source(pattern)
    cr.paint_with_alpha(alpha)
  end
  @surface = i.surface
  @cr = i.cr
end

#widthObject



36
37
38
# File 'lib/cairo_tools.rb', line 36

def width
  canvas_width - right_margin - left_margin
end