class AsciiPaint::BlockCharacter

Constants

HORIZONTAL_PADDING
UNPADDED_HEIGHT

TODO: make customizable

UNPADDED_WIDTH
VERTICAL_PADDING

Attributes

ascii[R]

Public Class Methods

bottom_padding() click to toggle source
# File lib/ascii_paint/block_character.rb, line 50
def self.bottom_padding
  (VERTICAL_PADDING + 1) / 2
end
height() click to toggle source
# File lib/ascii_paint/block_character.rb, line 30
def self.height
  UNPADDED_HEIGHT + VERTICAL_PADDING
end
left_padding() click to toggle source
# File lib/ascii_paint/block_character.rb, line 38
def self.left_padding
  HORIZONTAL_PADDING / 2
end
new(character) click to toggle source
# File lib/ascii_paint/block_character.rb, line 11
def initialize(character)
  raise "Only single characters please! #{character}" unless character.size == 1

  if character == "\n"
    @newline = true
    return
  end

  load_ascii(character)
end
right_padding() click to toggle source
# File lib/ascii_paint/block_character.rb, line 42
def self.right_padding
  (HORIZONTAL_PADDING + 1) / 2
end
top_padding() click to toggle source
# File lib/ascii_paint/block_character.rb, line 46
def self.top_padding
  VERTICAL_PADDING / 2
end
width() click to toggle source
# File lib/ascii_paint/block_character.rb, line 34
def self.width
  UNPADDED_WIDTH + HORIZONTAL_PADDING
end

Public Instance Methods

newline?() click to toggle source
# File lib/ascii_paint/block_character.rb, line 22
def newline?
  @newline == true
end
to_s() click to toggle source
# File lib/ascii_paint/block_character.rb, line 26
def to_s
  ascii.join("\n")
end

Private Instance Methods

block_characters_dir() click to toggle source
# File lib/ascii_paint/block_character.rb, line 86
def block_characters_dir
  AsciiPaint.root.join('data', 'block_characters')
end
load_ascii(character) click to toggle source

TODO: filename escaping

# File lib/ascii_paint/block_character.rb, line 57
def load_ascii(character)
  path = path_for character
  if File.exists? path
    @ascii = File.open(path, 'r').to_a.map(&:chomp)
    @ascii = pad_ascii(@ascii)
  else
    raise "Character not supported: #{character}"
  end
end
pad_ascii(ascii) click to toggle source
# File lib/ascii_paint/block_character.rb, line 72
def pad_ascii(ascii)
  left = " " * self.class.left_padding
  right = " " * self.class.right_padding
  padded = ascii.map do |row|
    "#{left}#{row}#{right}"
  end

  blank_row = " " * self.class.width
  self.class.top_padding.times { padded.unshift(blank_row) }
  self.class.bottom_padding.times { padded.push(blank_row) }

  padded
end
path_for(character) click to toggle source
# File lib/ascii_paint/block_character.rb, line 67
def path_for(character)
  # Use ASCII codes!
  block_characters_dir.join "#{character.ord}.txt"
end