class GD2::Canvas
Constants
- ANTI_ALIASED
- BRUSHED
- STYLED
Special colors
- STYLED_BRUSHED
- TILED
- TRANSPARENT
Attributes
anti_aliasing[RW]
anti_aliasing?[RW]
brush[R]
color[R]
dont_blend[R]
font[RW]
style[R]
thickness[R]
tile[R]
transformation_matrix[R]
Public Class Methods
new(image)
click to toggle source
# File lib/gd2/canvas.rb, line 211 def initialize(image) @image = image self.thickness = 1 self.anti_aliasing = false @transformation_matrix = Matrix.identity(3) move_to(0, 0) end
Public Instance Methods
affine_transform(a, b, c, d, tx, ty) { || ... }
click to toggle source
# File lib/gd2/canvas.rb, line 255 def affine_transform(a, b, c, d, tx, ty) old_matrix = @transformation_matrix begin @transformation_matrix = Matrix[[a, b, 0], [c, d, 0], [tx, ty, 1]] * @transformation_matrix yield ensure @transformation_matrix = old_matrix end end
arc(cx, cy, width, height, range)
click to toggle source
# File lib/gd2/canvas.rb, line 349 def arc(cx, cy, width, height, range) Arc.new(point(cx, cy), width, height, range).draw(@image, line_pixel) end
brush=(image)
click to toggle source
# File lib/gd2/canvas.rb, line 237 def brush=(image) if @brush = image ::GD2::GD2FFI.send(:gdImageSetBrush, @image.image_ptr, image.image_ptr) end end
cartesian() { |*block_args| ... }
click to toggle source
# File lib/gd2/canvas.rb, line 286 def cartesian affine_transform(1, 0, 0, -1, 0, @image.height - 1) { |*block_args| yield(*block_args) } end
circle(cx, cy, diameter, filled = false)
click to toggle source
# File lib/gd2/canvas.rb, line 363 def circle(cx, cy, diameter, filled = false) ellipse(cx, cy, diameter, diameter, filled) end
color=(color)
click to toggle source
# File lib/gd2/canvas.rb, line 219 def color=(color) @pixel = @image.color2pixel(@color = color) @brush = @style = nil end
dont_blend=(color)
click to toggle source
# File lib/gd2/canvas.rb, line 251 def dont_blend=(color) @dont_blend = color ? @image.color2pixel(color) : nil end
ellipse(cx, cy, width, height, filled = false)
click to toggle source
# File lib/gd2/canvas.rb, line 358 def ellipse(cx, cy, width, height, filled = false) (filled ? FilledEllipse : Ellipse).new(point(cx, cy), width, height). draw(@image, filled ? fill_pixel : line_pixel) end
fill()
click to toggle source
# File lib/gd2/canvas.rb, line 323 def fill ::GD2::GD2FFI.send(:gdImageFill, @image.image_ptr, @point.x.to_i, @point.y.to_i, fill_pixel.to_i) self end
fill_to(border)
click to toggle source
# File lib/gd2/canvas.rb, line 328 def fill_to(border) # An apparent bug in gd prevents us from using fill_pixel ::GD2::GD2FFI.send(:gdImageFillToBorder, @image.image_ptr, @point.x.to_i, @point.y.to_i, @image.color2pixel(border), pixel.to_i) self end
line(x1, y1, x2, y2)
click to toggle source
# File lib/gd2/canvas.rb, line 312 def line(x1, y1, x2, y2) Line.new(point(x1, y1), point(x2, y2)).draw(@image, line_pixel) end
line_to(x, y)
click to toggle source
# File lib/gd2/canvas.rb, line 316 def line_to(x, y) point2 = point(x, y) Line.new(@point, point2).draw(@image, line_pixel) @point = point2 self end
location()
click to toggle source
# File lib/gd2/canvas.rb, line 308 def location @point.transform(transformation_matrix.inverse).coordinates end
move(x, y)
click to toggle source
# File lib/gd2/canvas.rb, line 301 def move(x, y) @point.transform!(Matrix[[1, 0, 0], [0, 1, 0], [x, y, 1]] * @transformation_matrix) # @point = point(@point.x + x, @point.y + y) self end
move_to(x, y)
click to toggle source
# File lib/gd2/canvas.rb, line 296 def move_to(x, y) @point = point(x, y) self end
point(x, y)
click to toggle source
# File lib/gd2/canvas.rb, line 292 def point(x, y) Point.new(x, y).transform!(transformation_matrix) end
polygon(points, filled = false, open = false)
click to toggle source
# File lib/gd2/canvas.rb, line 340 def polygon(points, filled = false, open = false) points = points.map { |(x, y)| point(x, y) } if filled FilledPolygon.new(points).draw(@image, fill_pixel) else (open ? OpenPolygon : Polygon).new(points).draw(@image, line_pixel) end end
rectangle(x1, y1, x2, y2, filled = false)
click to toggle source
# File lib/gd2/canvas.rb, line 335 def rectangle(x1, y1, x2, y2, filled = false) (filled ? FilledRectangle : Rectangle).new(point(x1, y1), point(x2, y2)). draw(@image, filled ? fill_pixel : line_pixel) end
rotate(angle) { |*block_args| ... }
click to toggle source
# File lib/gd2/canvas.rb, line 278 def rotate(angle) cos = Math.cos(angle) sin = Math.sin(angle) affine_transform(cos, sin, -sin, cos, 0, 0) { |*block_args| yield(*block_args) } end
scale(sx, sy = sx) { |*block_args| ... }
click to toggle source
# File lib/gd2/canvas.rb, line 272 def scale(sx, sy = sx) affine_transform(sx, 0, 0, sy, 0, 0) { |*block_args| yield(*block_args) } end
style=(ary)
click to toggle source
# File lib/gd2/canvas.rb, line 228 def style=(ary) if @style = ary ::GD2::GD2FFI.send(:gdImageSetStyle, @image.image_ptr, ary.map { |c| !c ? TRANSPARENT : true == c ? -1 : @image.color2pixel(c) }, ary.length) end end
text(string, angle = 0.0)
click to toggle source
# File lib/gd2/canvas.rb, line 367 def text(string, angle = 0.0) Text.new(get_font, @point, angle, string).draw(@image, pixel) end
text_circle(top, bottom, radius, text_radius, fill_portion)
click to toggle source
# File lib/gd2/canvas.rb, line 371 def text_circle(top, bottom, radius, text_radius, fill_portion) TextCircle.new(get_font, @point, radius, text_radius, fill_portion, top, bottom).draw(@image, pixel) end
thickness=(thickness)
click to toggle source
# File lib/gd2/canvas.rb, line 224 def thickness=(thickness) ::GD2::GD2FFI.send(:gdImageSetThickness, @image.image_ptr, @thickness = thickness.to_i) end
tile=(image)
click to toggle source
# File lib/gd2/canvas.rb, line 243 def tile=(image) if @tile = image ::GD2::GD2FFI.send(:gdImageSetTile, @image.image_ptr, image.image_ptr) end end
translate(tx, ty) { |*block_args| ... }
click to toggle source
# File lib/gd2/canvas.rb, line 266 def translate(tx, ty) affine_transform(1, 0, 0, 1, tx, ty) { |*block_args| yield(*block_args) } end
wedge(cx, cy, width, height, range, filled = false, chord = false)
click to toggle source
# File lib/gd2/canvas.rb, line 353 def wedge(cx, cy, width, height, range, filled = false, chord = false) (filled ? FilledWedge : Wedge).new(point(cx, cy), width, height, range, chord).draw(@image, filled ? fill_pixel : line_pixel) end
Private Instance Methods
fill_pixel()
click to toggle source
# File lib/gd2/canvas.rb, line 408 def fill_pixel defined?(@tile) ? TILED : pixel end
get_font()
click to toggle source
# File lib/gd2/canvas.rb, line 378 def get_font raise NoFontSelectedError, 'No font selected' unless @font @font end
line_pixel()
click to toggle source
# File lib/gd2/canvas.rb, line 388 def line_pixel if @style && @brush STYLED_BRUSHED elsif @style STYLED elsif @brush BRUSHED elsif anti_aliasing? if @dont_blend ::GD2::GD2FFI.send(:gdImageSetAntiAliasedDontBlend, @image.image_ptr, pixel.to_i, @dont_blend.to_i) else ::GD2::GD2FFI.send(:gdImageSetAntiAliased, @image.image_ptr, pixel.to_i) end ANTI_ALIASED else pixel end end
pixel()
click to toggle source
# File lib/gd2/canvas.rb, line 383 def pixel raise NoColorSelectedError, 'No drawing color selected' unless @pixel @pixel end