Class: Chem::RMagickWriter

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Writer

#draw_body, #fbox, #to_256

Constructor Details

#initialize(mol, params) ⇒ RMagickWriter

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



12
13
14
15
16
17
18
19
# File 'lib/chem/db/rmagick.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



51
52
53
54
# File 'lib/chem/db/rmagick.rb', line 51

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

Instance Method Details

#draw(filename, params) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chem/db/rmagick.rb', line 56

def draw filename, params
  @img = Magick::ImageList.new
  x, y = params[:size]
  x += params[:margin][0] * 2
  y += params[:margin][0] * 2
  @img.new_image(x, y)

  @canvas = Magick::Draw.new
  draw_body
  
  @canvas.draw(@img)
  @img.write(filename)
end

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



28
29
30
31
32
33
34
# File 'lib/chem/db/rmagick.rb', line 28

def fill(nodes, color, params = {})
  @canvas.stroke("rgb(%f, %f, %f)" % to_256(color))
  @canvas.fill("rgb(%f, %f, %f)" % to_256(color)) if color
  path = nodes.inject([]){|ret, node| ret << node[0] ; ret << node[1]}
  @canvas.polygon(* path)
  @canvas.fill("black")
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/rmagick.rb', line 23

def line(from, to, color)
  @canvas.stroke("rgb(%f, %f, %f)" % to_256(color))
  @canvas.line(from[0], from[1], to[0], to[1])
end

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chem/db/rmagick.rb', line 36

def text(str, x, y, params = {})
  @canvas.pointsize = @default_pointsize
  metrics = @canvas.get_type_metrics(@img, str)
  @canvas.stroke('transparent')
  @canvas.pointsize(params[:pontsize])             if params[:pointsize]
  @canvas.fill("rgb(%f, %f, %f)" % to_256(params[:color])) if params[:color]

  @canvas.text(x - metrics.width / 2.0,
               y + metrics.height / 4.0,
               str)

  @canvas.pointsize(@default_pointsize)            if params[:pointsize]
  @canvas.fill('black')                            if params[:color]
end