[Bio] / Speck / PixFont.py Repository:
ViewVC logotype

View of /Speck/PixFont.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (download) (as text) (annotate) (vendor branch)
Thu Sep 9 19:54:06 2004 UTC (15 years, 2 months ago) by sheradon
Branch: init, MAIN
CVS Tags: v0, HEAD
Changes since 1.1: +0 -0 lines
Initial load to CVS

"""A simple but stylish pixel font, for drawing reasonably fast text
	in OpenGL. It's the Silkscreen font by Jason Kottke
	(http://kottke.org/plus/type/silkscreen), transmogrified to this
	by Larry Sheradon (sheradon)
"""
from OpenGL.GL import *
import string

letters = {}

# a letter is tuple of (width, list of rectangle-tuples)
# rectangle is (left, bottom, width, height) where 0,0 is bottom left
letters[' '] = [0]
letters['A'] = [4, (0,0,1,4), (1,4,2,1), (1,2,2,1), (3,0,1,4)]
letters['B'] = [4, (0,0,1,5), (1,4,2,1), (1,2,2,1), (1,0,2,1), (3,1,1,3)]
letters['C'] = [4, (0,1,1,3), (1,4,2,1), (1,0,2,1), (3,3,1,1), (3,1,1,1)]
letters['D'] = [4, (0,0,1,5), (1,4,2,1), (1,0,2,1), (3,1,1,3)]
letters['E'] = [3, (0,0,1,5), (1,4,2,1), (1,2,2,1), (1,0,2,1)]
letters['F'] = [3, (0,0,1,5), (1,4,2,1), (1,2,2,1)]
letters['G'] = [4, (0,1,1,3), (1,4,3,1), (1,0,2,1), (2,2,2,1), (3,1,1,1)]
letters['H'] = [4, (0,0,1,5), (1,2,2,1), (3,0,1,5)]
letters['I'] = [1, (0,0,1,5)]
letters['J'] = [4, (0,1,1,1), (1,0,2,1), (3,1,1,4)]
letters['K'] = [4, (0,0,1,5), (1,2,1,1), (2,3,1,1), (3,4,1,1), (2,1,1,1), (3,0,1,1)]
letters['L'] = [3, (0,0,1,5), (1,0,2,1)]
letters['M'] = [5, (0,0,1,5), (1,3,1,1), (2,2,1,1), (3,3,1,1), (4,0,1,5)]
letters['N'] = [5, (0,0,1,5), (1,3,1,1), (2,2,1,1), (3,1,1,1), (4,0,1,5)]
letters['O'] = [4, (0,1,1,3), (1,4,2,1), (1,0,2,1), (3,1,1,3)]
letters['P'] = [4, (0,0,1,5), (1,4,2,1), (1,2,2,1), (3,3,1,1)]
letters['Q'] = [4, (0,1,1,3), (1,4,2,1), (1,0,2,1), (3,1,1,3), (3,-1,1,1)]
letters['R'] = [4, (0,0,1,5), (1,4,2,1), (1,2,2,1), (3,3,1,1), (2,1,1,1), (3,0,1,1)]
letters['S'] = [4, (0,0,3,1), (3,1,1,1), (1,2,2,1), (0,3,1,1), (1,4,3,1)]
letters['T'] = [3, (0,4,3,1), (1,0,1,4)]
letters['U'] = [4, (0,1,1,4), (1,0,2,1), (3,1,1,4)]
letters['V'] = [5, (0,3,1,2), (1,1,1,2), (2,0,1,1), (3,1,1,2), (4,3,1,2)]
letters['W'] = [5, (0,1,1,4), (1,0,1,1), (2,1,1,3), (3,0,1,1), (4,1,1,4)]
letters['X'] = [5, (0,0,1,1), (1,1,1,1), (2,2,1,1), (3,3,1,1), (4,4,1,1), (0,4,1,1), (1,3,1,1), (3,1,1,1), (4,0,1,1)]
letters['Y'] = [5, (0,4,1,1), (1,3,1,1), (2,0,1,3), (3,3,1,1), (4,4,1,1)]
letters['Z'] = [3, (0,0,3,1), (0,1,1,1), (1,2,1,1), (2,3,1,1), (0,4,3,1)]
letters['1'] = [3, (0,0,3,1), (1,1,1,4), (0,4,1,1)]
letters['2'] = [4, (0,0,4,1), (0,1,1,1), (1,2,2,1), (3,3,1,1), (0,4,3,1)]
letters['3'] = [4, (0,0,3,1), (3,1,1,1), (1,2,2,1), (3,3,1,1), (0,4,3,1)]
letters['4'] = [4, (0,2,1,3), (1,2,3,1), (2,0,1,5)]
letters['5'] = [4, (0,0,3,1), (3,1,1,1), (0,2,3,1), (0,3,1,1), (0,4,4,1)]
letters['6'] = [4, (0,1,1,3), (1,4,2,1), (1,2,2,1), (1,0,2,1), (3,1,1,1)]
letters['7'] = [4, (0,4,4,1), (3,3,1,1), (2,2,1,1), (1,0,1,2)]
letters['8'] = [4, (1,0,2,1), (0,1,1,1), (3,1,1,1), (1,2,2,1), (0,3,1,1), (3,3,1,1), (1,4,2,1)]
letters['9'] = [4, (0,3,1,1), (1,0,2,1), (1,2,2,1), (1,4,2,1), (3,1,1,3)]
letters['0'] = [4, (0,1,1,3), (1,4,2,1), (1,0,2,1), (3,1,1,3)]
letters['!'] = [1, (0,0,1,1), (0,2,1,3)]
letters['@'] = [5, (0,1,1,3), (1,4,3,1), (2,2,1,2), (3,2,1,1), (1,0,3,1), (4,3,1,1)]
letters['#'] = [5, (0,1,5,1), (0,3,5,1), (1,0,1,5), (3,1,1,5)]
letters['%'] = [5, (0,3,2,2), (1,0,1,2), (2,2,1,1), (3,3,1,2), (3,0,2,2)]
letters['^'] = [3, (0,4,1,1), (1,5,1,1), (2,4,1,1)]
letters['&'] = [4, (0,1,1,1), (0,3,1,1), (1,4,3,1), (1,2,2,1), (1,0,3,1), (2,5,1,1), (2,-1,1,1)]
letters['*'] = [5, (0,1,1,1), (0,3,1,1), (1,2,3,1), (2,0,1,5), (4,1,1,1), (4,3,1,1)]
letters['('] = [2, (0,1,1,3), (1,0,1,1), (1,4,1,1)]
letters[')'] = [2, (1,1,1,3), (0,0,1,1), (0,4,1,1)]
letters['_'] = [4, (0,-1,4,1)]
letters['+'] = [5, (0,2,5,1), (2,0,1,5)]
letters['-'] = [4, (0,2,4,1)]
letters['`'] = [2, (0,4,1,1), (1,3,1,1)]
letters['~'] = [4, (0,3,1,1), (1,4,1,1), (2,3,1,1), (3,4,1,1)]
letters['['] = [2, (0,0,1,5), (1,4,1,1), (1,0,1,1)]
letters[']'] = [2, (0,0,1,1), (0,4,1,1), (1,0,1,5)]
letters['\\']= [3, (0,3,1,2), (1,2,1,1), (2,0,1,2)]
letters['{'] = [3, (0,2,1,1), (1,0,1,2), (1,3,1,2), (2,0,1,1), (2,4,1,1)]
letters['}'] = [3, (0,0,1,1), (0,4,1,1), (1,0,1,2), (1,3,1,2), (2,2,1,1)]
letters['|'] = [1, (0,-1,1,7)]
letters[';'] = [2, (0,0,1,1), (1,1,1,1), (1,3,1,1)]
letters[':'] = [1, (0,1,1,1), (0,3,1,1)]
letters["'"] = [1, (0,3,1,2)]
letters['"'] = [3, (0,3,1,2), (2,3,1,2)]
letters[','] = [2, (0,-1,1,1), (1,0,1,1)]
letters['.'] = [1, (0,0,1,1)]
letters['/'] = [3, (0,0,1,2), (1,2,1,1), (2,3,1,2)]
letters['<'] = [3, (2,0,1,1), (1,1,1,1), (0,2,1,1), (1,3,1,1), (2,4,1,1)]
letters['>'] = [3, (0,0,1,1), (1,1,1,1), (2,2,1,1), (1,3,1,1), (0,4,1,1)]
letters['?'] = [4, (0,4,3,1), (1,2,2,1), (1,0,1,1), (3,3,1,1)]

# -----------------------------------------------------
def drawString(rawstr, x, y, height, align='left', valign='bottom'):
# -----------------------------------------------------
	"""Draws the given string in a pixely/block font. It'll be 'height'
		GL world-coordinates tall. Align can be left, center, or right;
		if left, coords are lower-left corner; center they're the center, etc.
		@type rawstr: string
		@type coords: (float, float)
		@type height: float
		@type align: string
	"""
	str = string.upper(rawstr)
	glPushMatrix()
	glTranslatef(x, y, 0)

	pixSize = float(height) / 5.0
	lines = string.split(string.upper(rawstr), r'\N')
	if valign == 'center': yOffset = 3.5 * len(lines) - 1 - 5 
	else: yOffset = (len(lines)-1) * 7
	for line in lines:
		if align == 'left':		xOffset = 0
		elif align == 'center':	xOffset = -1 * pixLen(line)/2.0
		elif align == 'right':	xOffset = -1 * pixLen(line)
		for c in line:
			letter = getLetter(c)
			# draw each rectangle of the letter:
			for rect in letter[1:]:
				botLeft = (pixSize * (xOffset + rect[0]), pixSize * (yOffset + rect[1]))
				glRectf(botLeft[0], botLeft[1], botLeft[0]+pixSize*rect[2], botLeft[1]+pixSize*rect[3])
			# shift over for next letter
			xOffset = xOffset + letter[0] + 2
		yOffset -= 7 

	glPopMatrix()

# ---------------
def getLetter(c):
# ---------------
	"""Just return the letter-definition of character c, if it exists.
		If it's not there, return a space.
		@type c: string
		@rtype: list (see 'letters' definition above)
	"""
	try:					letter = letters[c]
	except KeyError:	letter = letters[' ']
	return letter

# --------------
def pixLen(str):
# --------------
	"""Number of "pixels" (in the font) wide that str is.
		@type str: string
		@rtype: integer
	"""
	count = 0
	for c in str:
		count += getLetter(c)[0] 
	count += 2 * (len(str) - 1)	# spaces between letters
	return count

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3