In Files

Parent

Class Index [+]

Quicksearch

PNG::Font

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

Constants

LETTERS

Attributes

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

Public Class Methods

default() click to toggle source
    # File lib/png/font.rb, line 16
16:   def self.default
17:     @@default ||= new(File.join(File.dirname(__FILE__), "default_font.png"))
18:   end
new(png_file) click to toggle source
    # File lib/png/font.rb, line 20
20:   def initialize(png_file)
21:     @canvas = PNG.load_file png_file
22:     @height, @width = canvas.height / 4, canvas.width / 26
23:     @cache = {}
24:   end

Public Instance Methods

[](c) click to toggle source
    # File lib/png/font.rb, line 37
37:   def [] c
38:     c = c.chr unless String === c
39:     x0, y0, x1, y1 = coordinates c
40: 
41:     @cache[c] ||= @canvas.extract(x0, y0, x1, y1)
42:   end
coordinates(c) click to toggle source
    # File lib/png/font.rb, line 26
26:   def coordinates c
27:     i = LETTERS.index c
28: 
29:     raise ArgumentError, "Can't find #{c.inspect}" unless i
30: 
31:     x = (i % 26) * width
32:     y = (3 - (i / 26)) * height # start from the top (3rd row)
33: 
34:     return x, y, x+width-1, y+height-1
35:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.