Class: Txt2img::Txt

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StringUtil

#size, #split_by_width, #wrap_by_width

Constructor Details

#initialize(txt, options = {}) ⇒ Txt

Returns a new instance of Txt.



13
14
15
16
17
18
# File 'lib/txt2img.rb', line 13

def initialize(txt, options = {})
  @txt = txt
  @font = options[:font] || File.expand_path("../../fonts/microhei.ttc", __FILE__)
  @width = options[:width] || 20
  @pointsize = options[:pointsize] || 30
end

Instance Attribute Details

#fontObject

Returns the value of attribute font.



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

def font
  @font
end

#pointsizeObject

Returns the value of attribute pointsize.



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

def pointsize
  @pointsize
end

#txtObject

Returns the value of attribute txt.



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

def txt
  @txt
end

#widthObject

Returns the value of attribute width.



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

def width
  @width
end

Instance Method Details

#heightObject



38
39
40
# File 'lib/txt2img.rb', line 38

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

#wrap!Object

TODO optimize TODO NOT break English word TODO tune the margin and text size



32
33
34
35
36
# File 'lib/txt2img.rb', line 32

def wrap!
  @txt = @txt.split(/[\r\n]+/).map do |line|
    wrap_by_width(line, width)
  end * "\n"
end

#write(path) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/txt2img.rb', line 20

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