Skip to content

Instantly share code, notes, and snippets.

@aalin
Created December 24, 2009 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aalin/263382 to your computer and use it in GitHub Desktop.
Save aalin/263382 to your computer and use it in GitHub Desktop.
class CharacterDrawer
CHARACTERS = {
'A' => [[ [0,0], [2,5], [4,0] ], [ [1,2], [3,2] ]],
'B' => [[ [0,0], [0,5], [2,5], [3,4], [2,3], [3,2], [3,1], [2,0], [0,0] ], [ [0,3], [2,3] ]],
'C' => [[ [4,1], [3,0], [1,0], [0,1], [0,4], [1,5], [3,5], [4,4] ]],
'D' => [[ [0,0], [0,5], [2,5], [3,4], [3,1], [2,0], [0,0] ]],
'E' => [[ [3,0], [0,0], [0,5], [3,5] ], [ [0,3], [2,3] ]],
'F' => [[ [0,0], [0,5], [3,5] ], [ [0,3], [2,3] ]],
'G' => [[ [2,2], [4,2], [4,1], [3,0], [1,0], [0,1], [0,4], [1,5], [3,5], [4,4] ]],
'H' => [[ [0,0], [0,5] ], [ [3,0], [3,5] ], [ [0,3], [3,3] ]],
'I' => [[ [1,0], [1,5] ], [ [0,0], [2,0] ], [ [0,5], [2,5] ]],
'J' => [[ [0,1], [1,0], [2,0], [3,1], [3,5] ]],
'K' => [[ [0,0], [0,5] ], [ [3,5], [0,3], [3,0] ]],
'L' => [[ [0,5], [0,0], [3,0] ]],
'M' => [[ [0,0], [0,5], [2,2], [4,5], [4,0] ]],
'N' => [[ [0,0], [0,5], [3,0], [3,5] ]],
'O' => [[ [0,1], [0,4], [1,5], [3,5], [4,4], [4,1], [3,0], [1,0], [0,1] ]],
'P' => [[ [0,0], [0,5], [2,5], [3,4], [3,3], [2,2], [0,2] ]],
'Q' => [[ [0,1], [0,4], [1,5], [3,5], [4,4], [4,1], [3,0], [1,0], [0,1] ], [ [2,2], [5,-1] ]],
'R' => [[ [0,0], [0,5], [2,5], [3,4], [3,3], [2,2], [0,2] ], [ [2,2], [3,0] ]],
'S' => [[ [0,1], [1,0], [2,0], [3,1], [3,2], [0,3], [0,4], [1,5], [2,5], [3,4] ]],
'T' => [[ [0,5], [4,5] ], [ [2,0], [2,5] ]],
'U' => [[ [0,5], [0,1], [1,0], [3,0], [4,1], [4,5] ]],
'V' => [[ [0,5], [2,0], [4,5] ]],
'W' => [[ [0,5], [1,0], [2,2], [3,0], [4,5] ]],
'X' => [[ [0,0], [4,5] ], [ [0,5], [4,0] ]],
'Y' => [[ [0,5], [2,3], [4,5] ], [ [2,3], [2,0] ]],
'Z' => [[ [0,5], [4,5], [0,0], [4,0] ]],
'a' => [[ [3,3], [3,0] ], [ [3,1], [2,0], [1,0], [0,1], [0,2], [1,3], [2,3], [3,2] ]],
'b' => [[ [0,5], [0,0] ], [ [0,1], [1,0], [2,0], [3,1], [3,2], [2,3], [1,3], [0,2] ]],
'c' => [[ [3,2], [2,3], [1,3], [0,2], [0,1], [1,0], [2,0], [3,1] ]],
'd' => [[ [3,5], [3,0] ], [ [3,1], [2,0], [1,0], [0,1], [0,2], [1,3], [2,3], [3,2] ]],
'e' => [[ [0,1], [3,2], [2,3], [1,3], [0,2], [0,1], [1,0], [2,0], [3,1] ]],
'f' => [[ [1,0], [1,4], [2,5] ], [ [0,3], [2,3] ]],
'g' => [[ [3,3], [3,-1], [2,-2], [1,-2], [0,-1] ], [ [3,1], [2,0], [1,0], [0,1], [0,2], [1,3], [2,3], [3,2] ]],
'h' => [[ [0,5], [0,0] ], [ [0,3], [1,3], [2,2], [2,0] ]],
'i' => [[ [0,0], [0,3] ], [ [0,5] ]],
'j' => [[ [0,-1], [1,-2], [2,-1], [2,3] ], [ [2,5] ]],
'k' => [[ [0,0], [0,5] ], [ [2,3], [0,2], [2,0] ]],
'l' => [[ [0,0], [0,5] ]],
'm' => [[ [0,0], [0,3], [1,3], [2,2], [2,3], [3,3], [4,2], [4,0] ], [ [2,2], [2,0] ]],
'n' => [[ [0,0], [0,3], [1,3], [2,2], [2,0] ]],
'o' => [[ [3,2], [2,3], [1,3], [0,2], [0,1], [1,0], [2,0], [3,1], [3,2] ]],
'p' => [[ [0,-2], [0,3] ], [ [0,2], [1,3], [2,3], [3,2], [3,1], [2,0], [1,0], [0,1] ]],
'q' => [[ [3,-2], [3,3] ], [ [3,1], [2,0], [1,0], [0,1], [0,2], [1,3], [2,3], [3,2] ]],
'r' => [[ [0,0], [0,3] ], [ [0,2], [1,3], [2,2] ]],
's' => [[ [0,1], [0,0], [2,0], [2,1], [0,2], [0,3], [2,3], [2,2] ]],
't' => [[ [1,5], [1,1], [2,0] ], [ [0,3], [2,3] ]],
'u' => [[ [0,3], [0,1], [1,0], [2,1] ], [ [2,3], [2,0] ]],
'v' => [[ [0,3], [1,0], [2,3] ]],
'w' => [[ [0,3], [1,0], [2,3], [3,0], [4,3] ]],
'x' => [[ [0,3], [2,0] ], [ [0,0], [2,3] ]],
'y' => [[ [0,3], [1,0] ], [ [2,3], [1,0], [0,-2] ]],
'z' => [[ [0,3], [2,3], [0,0], [2,0] ]],
'0' => [[ [0,1], [0,4], [1,5], [2,5], [3,4], [3,1], [2,0], [1,0], [0,1] ], [ [2,4], [1,1] ]],
'1' => [[ [0,4], [1,5], [1,0] ], [ [0,0], [2,0] ]],
'2' => [[ [0,4], [1,5], [2,5], [3,4], [0,0], [3,0] ]],
'3' => [[ [0,4], [1,5], [2,5], [3,4], [2,3], [3,2], [3,1], [2,0], [1,0], [0,1] ]],
'4' => [[ [0,5], [0,3], [3,3] ], [ [3,5], [3,0] ]],
'5' => [[ [3,5], [0,5], [0,3], [2,3], [3,2], [3,1], [2,0], [1,0], [0,1] ]],
'6' => [[ [2,5], [0,2], [0,1], [1,0], [2,0], [3,1], [3,2], [2,3], [1,3], [0,2] ]],
'7' => [[ [0,5], [3,5], [1,0] ]],
'8' => [[ [0,4], [1,5], [2,5], [3,4], [3,3], [0,2], [0,1], [1,0], [2,0], [3,1], [3,2], [0,3], [0,4] ]],
'9' => [[ [1,0], [3,3], [3,4], [2,5], [1,5], [0,4], [0,3], [1,2], [2,2], [3,3] ]],
' ' => [],
'.' => [[ [0,0] ]],
'?' => [[ [0,4], [1,5], [2,4], [2,3], [1,2] ], [ [1,0] ]]
}
def initialize(string)
@string = string
end
def draw(x, y)
glPushMatrix
glLoadIdentity
glTranslate(x, y, 0)
glColor(1.0, 1.0, 1.0)
@string.scan(/./).each do |char|
draw_character(char)
glTranslate(character_width(char), 0, 0)
end
glPopMatrix
end
def width
@string.scan(/./).inject(0) do |sum, c|
sum + character_width(c)
end
end
protected
def draw_character(char)
polys = CHARACTERS[char] || CHARACTERS['?']
polys.each do |poly|
glBegin(poly.size == 1 ? GL_POINTS : GL_LINE_STRIP)
poly.each do |x, y|
glVertex(x, y)
end
glEnd
end
end
def character_width(char)
return 4 if char == " "
polys = CHARACTERS[char] || CHARACTERS['?']
xs = polys.map { |poly| poly.map { |x,y| x } }.flatten
xs.max - xs.min + 1
end
end
require 'rubygems'
require 'glapp'
require 'character_drawer'
class FontTest
include GLApp
def update(s)
end
def draw
glMatrixMode(GL_PROJECTION)
glLoadIdentity
glOrtho(0, 200, 0, 200, -100, 100)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity
glEnable(GL_LINE_SMOOTH)
glEnable(GL_POINT_SMOOTH)
glDepthMask(GL_FALSE)
glLineWidth(2.0)
glPointSize(3.0)
CharacterDrawer.new("ABCDEFGHI").draw(10, 70)
CharacterDrawer.new("JKLMNOPQR").draw(10, 60)
CharacterDrawer.new("STUVWXYZ").draw(10, 50)
CharacterDrawer.new("0123456789").draw(10, 40)
CharacterDrawer.new("abcdefghi").draw(10, 30)
CharacterDrawer.new("jklmnopqr").draw(10, 20)
CharacterDrawer.new("stuvwxyz").draw(10, 10)
CharacterDrawer.new("The Quick Brown Fox Jumps Over The Lazy Dog").draw(10, 80)
end
def keyboard_down(key, special)
exit if key == 27
end
end
FontTest.new.show(500, 500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment