Class: AnsiArt::PngConverter

Inherits:
Converter show all
Defined in:
lib/ansi_art/png_converter.rb

Instance Method Summary collapse

Methods inherited from Converter

#commit_color, #reset_color, #set_color, #set_color_for, #wait_half_char!

Constructor Details

#initialize(width, options) ⇒ PngConverter

Returns a new instance of PngConverter.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ansi_art/png_converter.rb', line 4

def initialize width, options
  super()

  # GD Image Library
  if RUBY_VERSION > '1.9'
    require 'gd2-ffij'
  else
    require 'gd2'
    $KCODE = 'u'
    require 'jcode'
    String.class_eval do
      def ascii_only?
        !self.mb_char?
      end
    end
  end
  self.class.class_eval('include GD2')
  Image.class_eval('def p *arg; end;') # the gd2 gem puts useless data, mute it

  @options = options
  @fontsize = width.to_i / 40
  height = @fontsize * 23
  @image = Image::TrueColor.new(width,height)
  @canvas = Canvas.new @image
  @canvas.font = Font::TrueType['./uming.ttc', @fontsize,{
    :dpi => 72,
    :charmap => Font::TrueType::CHARMAP_UNICODE,
    :linespacing => 1.0}]
  @canvas.move 0, - (@drift=@canvas.font.bounding_rectangle('')[:upper_left][1])

  @palette = {:normal => [Color[0,0,0],Color[128,0,0],Color[0,128,0],Color[128,128,0],
                          Color[0,0,128],Color[128,0,128],Color[0,128,128],Color[192,192,192]],
              :bright => [Color[128,128,128],Color[255,0,0],Color[0,255,0],Color[255,255,0],
                          Color[0,0,255],Color[255,0,255],Color[0,255,255],Color[255,255,255]]}
end

Instance Method Details

#newLineObject



62
63
64
65
66
67
68
# File 'lib/ansi_art/png_converter.rb', line 62

def newLine
  @canvas.move_to 0, @canvas.location[1] + @fontsize
  # if no enough space for the new line, resize image
  if @canvas.location[1] + @fontsize + @drift > @image.height && !@options[:fixedHeight]
    @image.crop! 0,0,@image.width,@image.height + @fontsize
  end
end

#outputObject



69
70
71
72
73
74
75
76
# File 'lib/ansi_art/png_converter.rb', line 69

def output
  if @options[:path]
    @image.export @options[:path], {:format => 'png',:level => 9};
    return @options[:path] # return a path back
  else
    return @image.png(9)
  end
end

#put(str) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ansi_art/png_converter.rb', line 39

def put str
  return if str.empty?

  # if we got a dual color char, draw it now
  unless @leftColor.nil?
    c = str[/./]
    drawChar c,false

    # overlap left part
    @image.with_clipping  x = @canvas.location[0],
                          y = @canvas.location[1] + @drift,
                          x + @fontsize/2 -1,
                          y + @fontsize - 1 do |image|
      drawChar c, true, @leftColor
    end
    str[/./] = ''
    @leftColor = nil
  end

  str.each_char do |c|
    drawChar c
  end
end