Class: Chem::GDWriter

Inherits:
Object
  • Object
show all
Includes:
Writer
Defined in:
lib/chem/db/gd.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Writer

#draw_body, #fbox, #to_256

Constructor Details

#initialize(mol, params) ⇒ GDWriter

Constructor for GD Adaptor See chem/db/vector.rb for detail parameters



12
13
14
15
16
17
18
19
# File 'lib/chem/db/gd.rb', line 12

def initialize mol, params
  params[:size]        ||= [350, 350]
  params[:orig_point]  ||= [10, 10]
  params[:margin]      ||= [10, 10]
  @default_pointsize = (params[:pointsize] ? params[:pointsize] : 14)
  params[:upside_down] = params[:upside_down] ? false : true
  super
end

Class Method Details

.save(mol, filename, params) ⇒ Object



28
29
30
31
# File 'lib/chem/db/gd.rb', line 28

def self.save(mol, filename, params)
  writer = self.new(mol, params)
  writer.draw(filename, params)
end

Instance Method Details

#draw(filename, params) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chem/db/gd.rb', line 48

def draw filename, params

  x, y = params[:size]
  x += params[:margin][0] * 2
  y += params[:margin][0] * 2
  
  @img = GD::Image.new(x, y)
  background_color = @img.colorAllocate(255, 255, 255)

  # Vector#draw_body
  draw_body
  
  @img.png(open(filename, "w"))
end

#fill(nodes, color, params = {}) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/chem/db/gd.rb', line 33

def fill(nodes, color, params = {})
  poly = GD::Polygon.new
  nodes.each do |node|
    poly.addPt(node[0], node[1])
  end
  @img.filledPolygon(poly, @img.colorAllocate(*to_256(color)))
end

#line(from, to, color) ⇒ Object

Draws line This method may be invoked from chem/db/vector.rb



23
24
25
26
# File 'lib/chem/db/gd.rb', line 23

def line(from, to, color)
  a_color = @img.colorAllocate(*to_256(color))
  @img.line(from[0], from[1], to[0], to[1], a_color)
end

#text(str, x, y, params = {}) ⇒ Object



41
42
43
44
45
46
# File 'lib/chem/db/gd.rb', line 41

def text(str, x, y, params = {})
  font = GD::Font::LargeFont
  a_color = nil
  a_color = params[:color] ? @img.colorAllocate(*to_256(params[:color])) : @img.colorAllocate(0, 0, 0)
  @img.string(font, x - font.width / 2.0, y - font.height / 2.0, str, a_color)
end