Class: Txt2img::Txt

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(txt, font = nil, width = 20, pointsize = 30) ⇒ Txt

Returns a new instance of Txt.



9
10
11
12
13
14
# File 'lib/txt2img.rb', line 9

def initialize(txt, font = nil, width = 20, pointsize = 30)
  @txt = txt
  @font ||= File.expand_path("../../fonts/microhei.ttc", __FILE__)
  @width ||= 20
  @pointsize ||= 30
end

Instance Attribute Details

#fontObject

Returns the value of attribute font.



7
8
9
# File 'lib/txt2img.rb', line 7

def font
  @font
end

#pointsizeObject

Returns the value of attribute pointsize.



7
8
9
# File 'lib/txt2img.rb', line 7

def pointsize
  @pointsize
end

#txtObject

Returns the value of attribute txt.



7
8
9
# File 'lib/txt2img.rb', line 7

def txt
  @txt
end

#widthObject

Returns the value of attribute width.



7
8
9
# File 'lib/txt2img.rb', line 7

def width
  @width
end

Instance Method Details

#heightObject



30
31
32
# File 'lib/txt2img.rb', line 30

def height
  @txt.split(/[\r\n]+/).size
end

#wrap!Object

TODO optimize



26
27
28
# File 'lib/txt2img.rb', line 26

def wrap!
  @txt = @txt.split(/[\r\n]+/).map{|x| x.scan(/.{1,#{width}}/).join("\n")}.join("\n")
end

#write(path) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/txt2img.rb', line 16

def write(path)
  wrap!
  image = QuickMagick::Image::solid(width * pointsize + 100, ((height * pointsize) * 1.3).to_i, :white)
  image.font = font
  image.pointsize = 40
  image.draw_text(0, 0, txt, :gravity => "center")
  image.save(path)
end