Class: GD2::Canvas

Inherits:
Object
  • Object
show all
Defined in:
lib/gd2/canvas.rb

Defined Under Namespace

Classes: Arc, Ellipse, FilledEllipse, FilledPolygon, FilledRectangle, FilledWedge, Line, NoColorSelectedError, NoFontSelectedError, OpenPolygon, Point, Polygon, Rectangle, Text, TextCircle, Wedge

Constant Summary collapse

STYLED =

Special colors

-2
BRUSHED =
-3
STYLED_BRUSHED =
-4
TILED =
-5
TRANSPARENT =

Line styles only; not a color index

-6  # Line styles only; not a color index
ANTI_ALIASED =
-7

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image) ⇒ Canvas

Returns a new instance of Canvas.



229
230
231
232
233
234
235
# File 'lib/gd2/canvas.rb', line 229

def initialize(image)
  @image = image
  self.thickness = 1
  self.anti_aliasing = false
  @transformation_matrix = Matrix.identity(3)
  move_to(0, 0)
end

Instance Attribute Details

#anti_aliasingObject Also known as: anti_aliasing?

Returns the value of attribute anti_aliasing.



217
218
219
# File 'lib/gd2/canvas.rb', line 217

def anti_aliasing
  @anti_aliasing
end

#brushObject

Returns the value of attribute brush.



215
216
217
# File 'lib/gd2/canvas.rb', line 215

def brush
  @brush
end

#colorObject

Returns the value of attribute color.



215
216
217
# File 'lib/gd2/canvas.rb', line 215

def color
  @color
end

#dont_blendObject

Returns the value of attribute dont_blend.



215
216
217
# File 'lib/gd2/canvas.rb', line 215

def dont_blend
  @dont_blend
end

#fontObject

Returns the value of attribute font.



217
218
219
# File 'lib/gd2/canvas.rb', line 217

def font
  @font
end

#styleObject

Returns the value of attribute style.



215
216
217
# File 'lib/gd2/canvas.rb', line 215

def style
  @style
end

#thicknessObject

Returns the value of attribute thickness.



215
216
217
# File 'lib/gd2/canvas.rb', line 215

def thickness
  @thickness
end

#tileObject

Returns the value of attribute tile.



215
216
217
# File 'lib/gd2/canvas.rb', line 215

def tile
  @tile
end

#transformation_matrixObject (readonly)

Returns the value of attribute transformation_matrix.



215
216
217
# File 'lib/gd2/canvas.rb', line 215

def transformation_matrix
  @transformation_matrix
end

Instance Method Details

#affine_transform(a, b, c, d, tx, ty) ⇒ Object



273
274
275
276
277
278
279
280
281
282
# File 'lib/gd2/canvas.rb', line 273

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) ⇒ Object



359
360
361
# File 'lib/gd2/canvas.rb', line 359

def arc(cx, cy, width, height, range)
  Arc.new(point(cx, cy), width, height, range).draw(@image, line_pixel)
end

#cartesian(&block) ⇒ Object



298
299
300
# File 'lib/gd2/canvas.rb', line 298

def cartesian(&block)
  affine_transform(1, 0, 0, -1, 0, @image.height - 1, &block)
end

#circle(cx, cy, diameter, filled = false) ⇒ Object



373
374
375
# File 'lib/gd2/canvas.rb', line 373

def circle(cx, cy, diameter, filled = false)
  ellipse(cx, cy, diameter, diameter, filled)
end

#ellipse(cx, cy, width, height, filled = false) ⇒ Object



368
369
370
371
# File 'lib/gd2/canvas.rb', line 368

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

#fillObject



333
334
335
336
# File 'lib/gd2/canvas.rb', line 333

def fill
  GD2FFI.send(:gdImageFill, @image.image_ptr, @point.x.to_i, @point.y.to_i, fill_pixel.to_i)
  self
end

#fill_to(border) ⇒ Object



338
339
340
341
342
343
# File 'lib/gd2/canvas.rb', line 338

def fill_to(border)
  # An apparent bug in gd prevents us from using fill_pixel
  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) ⇒ Object



322
323
324
# File 'lib/gd2/canvas.rb', line 322

def line(x1, y1, x2, y2)
  Line.new(point(x1, y1), point(x2, y2)).draw(@image, line_pixel)
end

#line_to(x, y) ⇒ Object



326
327
328
329
330
331
# File 'lib/gd2/canvas.rb', line 326

def line_to(x, y)
  point2 = point(x, y)
  Line.new(@point, point2).draw(@image, line_pixel)
  @point = point2
  self
end

#locationObject



318
319
320
# File 'lib/gd2/canvas.rb', line 318

def location
  @point.transform(transformation_matrix.inverse).coordinates
end

#move(x, y) ⇒ Object



311
312
313
314
315
316
# File 'lib/gd2/canvas.rb', line 311

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) ⇒ Object



306
307
308
309
# File 'lib/gd2/canvas.rb', line 306

def move_to(x, y)
  @point = point(x, y)
  self
end

#point(x, y) ⇒ Object



302
303
304
# File 'lib/gd2/canvas.rb', line 302

def point(x, y)
  Point.new(x, y).transform!(transformation_matrix)
end

#polygon(points, filled = false, open = false) ⇒ Object



350
351
352
353
354
355
356
357
# File 'lib/gd2/canvas.rb', line 350

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) ⇒ Object



345
346
347
348
# File 'lib/gd2/canvas.rb', line 345

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) ⇒ Object



292
293
294
295
296
# File 'lib/gd2/canvas.rb', line 292

def rotate(angle, &block)
  cos = Math.cos(angle)
  sin = Math.sin(angle)
  affine_transform(cos, sin, -sin, cos, 0, 0, &block)
end

#scale(sx, sy = sx, &block) ⇒ Object



288
289
290
# File 'lib/gd2/canvas.rb', line 288

def scale(sx, sy = sx, &block)
  affine_transform(sx, 0, 0, sy, 0, 0, &block)
end

#text(string, angle = 0.0) ⇒ Object



377
378
379
# File 'lib/gd2/canvas.rb', line 377

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) ⇒ Object



381
382
383
384
# File 'lib/gd2/canvas.rb', line 381

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

#translate(tx, ty, &block) ⇒ Object



284
285
286
# File 'lib/gd2/canvas.rb', line 284

def translate(tx, ty, &block)
  affine_transform(1, 0, 0, 1, tx, ty, &block)
end

#wedge(cx, cy, width, height, range, filled = false, chord = false) ⇒ Object



363
364
365
366
# File 'lib/gd2/canvas.rb', line 363

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