Class: CooCoo::Drawing::CairoCanvas
- Inherits:
-
Canvas
show all
- Defined in:
- lib/coo-coo/drawing/cairo_canvas.rb
Instance Attribute Summary collapse
Attributes inherited from Canvas
#fill_color, #stroke_color
Instance Method Summary
collapse
-
#blit(img, x, y, w, h) ⇒ Object
-
#circle(x, y, r) ⇒ Object
-
#flush ⇒ Object
-
#initialize(surface_or_width, height = nil) ⇒ CairoCanvas
constructor
A new instance of CairoCanvas.
-
#line(x1, y1, x2, y2) ⇒ Object
-
#rect(x, y, w, h) ⇒ Object
-
#stroke(points) ⇒ Object
-
#text(txt, x, y, font, font_size, style = Cairo::FONT_SLANT_NORMAL) ⇒ Object
-
#to_blob ⇒ Object
-
#to_vector(grayscale = false) ⇒ Object
Constructor Details
#initialize(surface_or_width, height = nil) ⇒ CairoCanvas
Returns a new instance of CairoCanvas.
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/coo-coo/drawing/cairo_canvas.rb', line 9
def initialize(surface_or_width, height = nil)
if height
surface = Cairo::ImageSurface.new(surface_or_width, height)
else
surface = surface_or_width
end
@surface = surface
@context = Cairo::Context.new(@surface)
end
|
Instance Attribute Details
Returns the value of attribute context.
7
8
9
|
# File 'lib/coo-coo/drawing/cairo_canvas.rb', line 7
def context
@context
end
|
Returns the value of attribute surface.
7
8
9
|
# File 'lib/coo-coo/drawing/cairo_canvas.rb', line 7
def surface
@surface
end
|
Instance Method Details
#blit(img, x, y, w, h) ⇒ Object
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/coo-coo/drawing/cairo_canvas.rb', line 67
def blit(img, x, y, w, h)
surface = Cairo::ImageSurface.from_png(StringIO.new(img))
zx = w / surface.width.to_f
zy = h / surface.height.to_f
@context.set_source(surface, x / zx, y / zy)
@context.scale(zx, zy)
@context.paint
self
end
|
#circle(x, y, r) ⇒ Object
60
61
62
63
64
65
|
# File 'lib/coo-coo/drawing/cairo_canvas.rb', line 60
def circle(x, y, r)
@context.circle(x, y, r)
@context.set_source_rgba(*ChunkyPNG::Color.to_truecolor_alpha_bytes(fill_color))
@context.fill
self
end
|
20
21
22
23
|
# File 'lib/coo-coo/drawing/cairo_canvas.rb', line 20
def flush
@surface.flush
self
end
|
#line(x1, y1, x2, y2) ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/coo-coo/drawing/cairo_canvas.rb', line 25
def line(x1, y1, x2, y2)
@context.set_source_rgba(ChunkyPNG::Color.to_truecolor_alpha_bytes(stroke_color))
@context.move_to(x1, y1)
@context.line_to(x2, y2)
@context.stroke
self
end
|
#rect(x, y, w, h) ⇒ Object
53
54
55
56
57
58
|
# File 'lib/coo-coo/drawing/cairo_canvas.rb', line 53
def rect(x, y, w, h)
@context.rectangle(x, y, w, h)
@context.set_source_rgba(*ChunkyPNG::Color.to_truecolor_alpha_bytes(fill_color))
@context.fill
self
end
|
#stroke(points) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/coo-coo/drawing/cairo_canvas.rb', line 33
def stroke(points)
@context.set_source_rgba(*ChunkyPNG::Color.to_truecolor_alpha_bytes(stroke_color))
@context.set_line_width(points[0][2])
@context.move_to(points[0][0], points[0][1])
@context.line_cap = Cairo::LINE_CAP_ROUND
@context.line_join = Cairo::LINE_JOIN_ROUND
points.each.drop(1).each do |(x, y, w, color)|
@context.set_line_width(w)
if color
@context.set_source_rgba(*ChunkyPNG::Color.to_truecolor_alpha_bytes(color))
end
@context.line_to(x, y)
end
@context.stroke
self
end
|
#text(txt, x, y, font, font_size, style = Cairo::FONT_SLANT_NORMAL) ⇒ Object
78
79
80
81
82
83
84
85
|
# File 'lib/coo-coo/drawing/cairo_canvas.rb', line 78
def text(txt, x, y, font, font_size, style = Cairo::FONT_SLANT_NORMAL)
@context.move_to(x, y + font_size)
@context.set_source_rgba(*ChunkyPNG::Color.to_truecolor_alpha_bytes(fill_color))
@context.select_font_face(font, style)
@context.font_size = font_size
@context.show_text(txt)
self
end
|
87
88
89
90
91
92
|
# File 'lib/coo-coo/drawing/cairo_canvas.rb', line 87
def to_blob
data = StringIO.new
@surface.write_to_png(data)
data.rewind
data.read
end
|
#to_vector(grayscale = false) ⇒ Object
94
95
96
97
|
# File 'lib/coo-coo/drawing/cairo_canvas.rb', line 94
def to_vector(grayscale = false)
@surface.flush
chunky_to_vector(ChunkyPNG::Image.from_blob(to_blob), grayscale)
end
|