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.



211
212
213
214
215
216
217
# 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

Instance Attribute Details

#anti_aliasingObject Also known as: anti_aliasing?

Returns the value of attribute anti_aliasing.



199
200
201
# File 'lib/gd2/canvas.rb', line 199

def anti_aliasing
  @anti_aliasing
end

#brushObject

Returns the value of attribute brush.



197
198
199
# File 'lib/gd2/canvas.rb', line 197

def brush
  @brush
end

#colorObject

Returns the value of attribute color.



197
198
199
# File 'lib/gd2/canvas.rb', line 197

def color
  @color
end

#dont_blendObject

Returns the value of attribute dont_blend.



197
198
199
# File 'lib/gd2/canvas.rb', line 197

def dont_blend
  @dont_blend
end

#fontObject

Returns the value of attribute font.



199
200
201
# File 'lib/gd2/canvas.rb', line 199

def font
  @font
end

#styleObject

Returns the value of attribute style.



197
198
199
# File 'lib/gd2/canvas.rb', line 197

def style
  @style
end

#thicknessObject

Returns the value of attribute thickness.



197
198
199
# File 'lib/gd2/canvas.rb', line 197

def thickness
  @thickness
end

#tileObject

Returns the value of attribute tile.



197
198
199
# File 'lib/gd2/canvas.rb', line 197

def tile
  @tile
end

#transformation_matrixObject (readonly)

Returns the value of attribute transformation_matrix.



197
198
199
# File 'lib/gd2/canvas.rb', line 197

def transformation_matrix
  @transformation_matrix
end

Instance Method Details

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



255
256
257
258
259
260
261
262
263
264
# 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) ⇒ Object



349
350
351
# 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

#cartesianObject



286
287
288
289
290
# 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) ⇒ Object



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

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

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



358
359
360
361
# 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

#fillObject



323
324
325
326
# 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) ⇒ Object



328
329
330
331
332
333
# 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) ⇒ Object



312
313
314
# 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) ⇒ Object



316
317
318
319
320
321
# 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

#locationObject



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

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

#move(x, y) ⇒ Object



301
302
303
304
305
306
# 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) ⇒ Object



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

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

#point(x, y) ⇒ Object



292
293
294
# 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) ⇒ Object



340
341
342
343
344
345
346
347
# 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) ⇒ Object



335
336
337
338
# 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) ⇒ Object



278
279
280
281
282
283
284
# 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) ⇒ Object



272
273
274
275
276
# 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

#text(string, angle = 0.0) ⇒ Object



367
368
369
# 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) ⇒ Object



371
372
373
374
# 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

#translate(tx, ty) ⇒ Object



266
267
268
269
270
# 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) ⇒ Object



353
354
355
356
# 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