class PNG::Font

Implements a simple bitmap font by extracting letters from a PNG.

Constants

LETTERS

Attributes

canvas[R]
height[R]
width[R]

Public Class Methods

default() click to toggle source
# File lib/png/font.rb, line 15
def self.default
  @@default ||= new(File.join(File.dirname(__FILE__), "default_font.png"))
end
new(png_file) click to toggle source
# File lib/png/font.rb, line 19
def initialize png_file
  @canvas = PNG.load_file png_file
  @height, @width = canvas.height / 4, canvas.width / 26
  @cache = {}
end

Public Instance Methods

[](c) click to toggle source
# File lib/png/font.rb, line 36
def [] c
  c = c.chr unless String === c
  x0, y0, x1, y1 = coordinates c

  @cache[c] ||= @canvas.extract(x0, y0, x1, y1)
end
coordinates(c) click to toggle source
# File lib/png/font.rb, line 25
def coordinates c
  i = LETTERS.index c

  raise ArgumentError, "Can't find #{c.inspect}" unless i

  x = (i % 26) * width
  y = (3 - (i / 26)) * height # start from the top (3rd row)

  return x, y, x+width-1, y+height-1
end