module AsciiPaint

An instance of class AsciiPaint::Config is used to set global configuration options for ASCII paint. Pass a block to {AsciiPaint.config} to access this object.

Constants

VERSION

Public Class Methods

block_paint(block_string, out_filename, conf = {}, &block) click to toggle source

TODO: support filename arguments TODO: write tests TODO: write docs

# File lib/ascii_paint/block_string.rb, line 36
def self.block_paint(block_string, out_filename, conf = {}, &block)
  block_string = block_string.join("\n") if block_string.respond_to? :join
  ascii_art = BlockString.new(block_string).to_a
  paint(ascii_art, out_filename, conf, &block)
end
config() { |configuration| ... } click to toggle source

Passes an instance of {AsciiPaint::Config} to the given block. Used to set global configuration.

@return [Config] the global configuration

# File lib/ascii_paint/config.rb, line 157
def self.config
  @configuration ||= Config.default
  yield(@configuration) if block_given?
  @configuration
end
paint(ascii_art, out_filename, configuration = {}) { |out_filename| ... } click to toggle source

Paints a PNG based on the given ASCII art.

Example:

text = "
!!!!! @@@@@ $$$$$ %%%%% ^^^^^       @@@@@ $$$$$ %%%%% ^   ^ !!!!! 
!   ! @     $       %     ^         @   @ $   $   %   ^^  ^   !   
!!!!! @@@@@ $       %     ^         @@@@@ $$$$$   %   ^ ^ ^   !   
!   !     @ $       %     ^         @     $   $   %   ^  ^^   !   
!   ! @@@@@ $$$$$ %%%%% ^^^^^ !!!!! @     $   $ %%%%% ^   ^   !   
"

AsciiPaint.paint(text, 'out.png')

@param ascii_art [#to_s, Array<String>]

multiline string, string array or filename with the ASCII art to paint

@param out_filename [#to_s]

the name of the painted PNG file

@param conf [Hash<Symbol, value>]

configuration settings. Keys should be the names of attributes of
{AsciiPaint::Config}, such as +:character_height+.

@return [String]

the name of the painted PNG file
# File lib/ascii_paint.rb, line 32
def self.paint(ascii_art, out_filename, configuration = {})
  # TODO: full path return value
  global_configuration = self.config
  local_configuration = global_configuration.dup.set_options(configuration)

  ascii_array = ascii_art_to_array(ascii_art)
  image = ascii_to_image(ascii_array, local_configuration)
  save_image(image, out_filename, local_configuration)

  if block_given?
    begin
      yield out_filename
    ensure
      FileUtils.rm(out_filename)
    end

    true
  else
    out_filename
  end
end

Private Class Methods

ascii_art_to_array(ascii_art) click to toggle source
# File lib/ascii_paint.rb, line 56
def self.ascii_art_to_array(ascii_art)
  return ascii_art if ascii_art.is_a? Array

  ascii_art = ascii_art.to_s.split("\n")
  if ascii_art.size == 1
    ascii_art = File.open(ascii_art[0], 'r').to_a.map(&:chomp)
  end
  rectangularize(ascii_art)
end
ascii_to_colors(strings, configuration) click to toggle source
# File lib/ascii_paint.rb, line 80
def self.ascii_to_colors(strings, configuration)
  strings.map do |line|
    line.chars.map do |char|
      color = configuration.color_map[char]
      raise AsciiPaint::CharacterNotFound.new "Couldn't find a color mapping for character: #{char}" unless color
      color
    end
  end
end
ascii_to_image(ascii_array, configuration) click to toggle source
# File lib/ascii_paint.rb, line 66
def self.ascii_to_image(ascii_array, configuration)
  colors_grid = ascii_to_colors(ascii_array, configuration)
  image = blank_image(ascii_array, configuration)
  painted_image(image, colors_grid, configuration)
end
blank_image(lines, configuration) click to toggle source
# File lib/ascii_paint.rb, line 72
def self.blank_image(lines, configuration)
  height = lines.size
  width = lines.map(&:size).max

  width_pixels, height_pixels = configuration.characters_to_pixels(width, height)
  png = ChunkyPNG::Image.new(width_pixels, height_pixels, ChunkyPNG::Color::TRANSPARENT)
end
painted_image(image, color_grid, configuration) click to toggle source
# File lib/ascii_paint.rb, line 90
def self.painted_image(image, color_grid, configuration)
  color_grid.each_with_index do |row, y|
    row.each_with_index do |color, x|
      x_pixels, y_pixels = configuration.characters_to_pixels(x, y)
      width_pixels, height_pixels = configuration.characters_to_pixels(1, 1)
      image.rect(x_pixels, y_pixels, x_pixels + width_pixels, y_pixels + height_pixels, configuration.border_color, color)
    end
  end

  image
end
rectangularize(lines) click to toggle source
# File lib/ascii_paint.rb, line 102
def self.rectangularize(lines)
  width = lines.map(&:size).max

  lines.map do |line|
    line.ljust(width, ' ')
  end
end
root() click to toggle source
# File lib/ascii_paint.rb, line 114
def self.root
  Pathname.new(__FILE__).parent.parent
end
save_image(image, filename, _) click to toggle source
# File lib/ascii_paint.rb, line 110
def self.save_image(image, filename, _)
  image.save(filename, :interlace => true)
end