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

Annotation of /Speck/PixFont.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (view) (download) (as text)

1 : sheradon 1.1 """A simple but stylish pixel font, for drawing reasonably fast text
2 :     in OpenGL. It's the Silkscreen font by Jason Kottke
3 :     (http://kottke.org/plus/type/silkscreen), transmogrified to this
4 :     by Larry Sheradon (sheradon@mcs.anl.gov)
5 :     """
6 :     from OpenGL.GL import *
7 :     import string
8 :    
9 :     letters = {}
10 :    
11 :     # a letter is tuple of (width, list of rectangle-tuples)
12 :     # rectangle is (left, bottom, width, height) where 0,0 is bottom left
13 :     letters[' '] = [0]
14 :     letters['A'] = [4, (0,0,1,4), (1,4,2,1), (1,2,2,1), (3,0,1,4)]
15 :     letters['B'] = [4, (0,0,1,5), (1,4,2,1), (1,2,2,1), (1,0,2,1), (3,1,1,3)]
16 :     letters['C'] = [4, (0,1,1,3), (1,4,2,1), (1,0,2,1), (3,3,1,1), (3,1,1,1)]
17 :     letters['D'] = [4, (0,0,1,5), (1,4,2,1), (1,0,2,1), (3,1,1,3)]
18 :     letters['E'] = [3, (0,0,1,5), (1,4,2,1), (1,2,2,1), (1,0,2,1)]
19 :     letters['F'] = [3, (0,0,1,5), (1,4,2,1), (1,2,2,1)]
20 :     letters['G'] = [4, (0,1,1,3), (1,4,3,1), (1,0,2,1), (2,2,2,1), (3,1,1,1)]
21 :     letters['H'] = [4, (0,0,1,5), (1,2,2,1), (3,0,1,5)]
22 :     letters['I'] = [1, (0,0,1,5)]
23 :     letters['J'] = [4, (0,1,1,1), (1,0,2,1), (3,1,1,4)]
24 :     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)]
25 :     letters['L'] = [3, (0,0,1,5), (1,0,2,1)]
26 :     letters['M'] = [5, (0,0,1,5), (1,3,1,1), (2,2,1,1), (3,3,1,1), (4,0,1,5)]
27 :     letters['N'] = [5, (0,0,1,5), (1,3,1,1), (2,2,1,1), (3,1,1,1), (4,0,1,5)]
28 :     letters['O'] = [4, (0,1,1,3), (1,4,2,1), (1,0,2,1), (3,1,1,3)]
29 :     letters['P'] = [4, (0,0,1,5), (1,4,2,1), (1,2,2,1), (3,3,1,1)]
30 :     letters['Q'] = [4, (0,1,1,3), (1,4,2,1), (1,0,2,1), (3,1,1,3), (3,-1,1,1)]
31 :     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)]
32 :     letters['S'] = [4, (0,0,3,1), (3,1,1,1), (1,2,2,1), (0,3,1,1), (1,4,3,1)]
33 :     letters['T'] = [3, (0,4,3,1), (1,0,1,4)]
34 :     letters['U'] = [4, (0,1,1,4), (1,0,2,1), (3,1,1,4)]
35 :     letters['V'] = [5, (0,3,1,2), (1,1,1,2), (2,0,1,1), (3,1,1,2), (4,3,1,2)]
36 :     letters['W'] = [5, (0,1,1,4), (1,0,1,1), (2,1,1,3), (3,0,1,1), (4,1,1,4)]
37 :     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)]
38 :     letters['Y'] = [5, (0,4,1,1), (1,3,1,1), (2,0,1,3), (3,3,1,1), (4,4,1,1)]
39 :     letters['Z'] = [3, (0,0,3,1), (0,1,1,1), (1,2,1,1), (2,3,1,1), (0,4,3,1)]
40 :     letters['1'] = [3, (0,0,3,1), (1,1,1,4), (0,4,1,1)]
41 :     letters['2'] = [4, (0,0,4,1), (0,1,1,1), (1,2,2,1), (3,3,1,1), (0,4,3,1)]
42 :     letters['3'] = [4, (0,0,3,1), (3,1,1,1), (1,2,2,1), (3,3,1,1), (0,4,3,1)]
43 :     letters['4'] = [4, (0,2,1,3), (1,2,3,1), (2,0,1,5)]
44 :     letters['5'] = [4, (0,0,3,1), (3,1,1,1), (0,2,3,1), (0,3,1,1), (0,4,4,1)]
45 :     letters['6'] = [4, (0,1,1,3), (1,4,2,1), (1,2,2,1), (1,0,2,1), (3,1,1,1)]
46 :     letters['7'] = [4, (0,4,4,1), (3,3,1,1), (2,2,1,1), (1,0,1,2)]
47 :     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)]
48 :     letters['9'] = [4, (0,3,1,1), (1,0,2,1), (1,2,2,1), (1,4,2,1), (3,1,1,3)]
49 :     letters['0'] = [4, (0,1,1,3), (1,4,2,1), (1,0,2,1), (3,1,1,3)]
50 :     letters['!'] = [1, (0,0,1,1), (0,2,1,3)]
51 :     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)]
52 :     letters['#'] = [5, (0,1,5,1), (0,3,5,1), (1,0,1,5), (3,1,1,5)]
53 :     letters['%'] = [5, (0,3,2,2), (1,0,1,2), (2,2,1,1), (3,3,1,2), (3,0,2,2)]
54 :     letters['^'] = [3, (0,4,1,1), (1,5,1,1), (2,4,1,1)]
55 :     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)]
56 :     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)]
57 :     letters['('] = [2, (0,1,1,3), (1,0,1,1), (1,4,1,1)]
58 :     letters[')'] = [2, (1,1,1,3), (0,0,1,1), (0,4,1,1)]
59 :     letters['_'] = [4, (0,-1,4,1)]
60 :     letters['+'] = [5, (0,2,5,1), (2,0,1,5)]
61 :     letters['-'] = [4, (0,2,4,1)]
62 :     letters['`'] = [2, (0,4,1,1), (1,3,1,1)]
63 :     letters['~'] = [4, (0,3,1,1), (1,4,1,1), (2,3,1,1), (3,4,1,1)]
64 :     letters['['] = [2, (0,0,1,5), (1,4,1,1), (1,0,1,1)]
65 :     letters[']'] = [2, (0,0,1,1), (0,4,1,1), (1,0,1,5)]
66 :     letters['\\']= [3, (0,3,1,2), (1,2,1,1), (2,0,1,2)]
67 :     letters['{'] = [3, (0,2,1,1), (1,0,1,2), (1,3,1,2), (2,0,1,1), (2,4,1,1)]
68 :     letters['}'] = [3, (0,0,1,1), (0,4,1,1), (1,0,1,2), (1,3,1,2), (2,2,1,1)]
69 :     letters['|'] = [1, (0,-1,1,7)]
70 :     letters[';'] = [2, (0,0,1,1), (1,1,1,1), (1,3,1,1)]
71 :     letters[':'] = [1, (0,1,1,1), (0,3,1,1)]
72 :     letters["'"] = [1, (0,3,1,2)]
73 :     letters['"'] = [3, (0,3,1,2), (2,3,1,2)]
74 :     letters[','] = [2, (0,-1,1,1), (1,0,1,1)]
75 :     letters['.'] = [1, (0,0,1,1)]
76 :     letters['/'] = [3, (0,0,1,2), (1,2,1,1), (2,3,1,2)]
77 :     letters['<'] = [3, (2,0,1,1), (1,1,1,1), (0,2,1,1), (1,3,1,1), (2,4,1,1)]
78 :     letters['>'] = [3, (0,0,1,1), (1,1,1,1), (2,2,1,1), (1,3,1,1), (0,4,1,1)]
79 :     letters['?'] = [4, (0,4,3,1), (1,2,2,1), (1,0,1,1), (3,3,1,1)]
80 :    
81 :     # -----------------------------------------------------
82 :     def drawString(rawstr, x, y, height, align='left', valign='bottom'):
83 :     # -----------------------------------------------------
84 :     """Draws the given string in a pixely/block font. It'll be 'height'
85 :     GL world-coordinates tall. Align can be left, center, or right;
86 :     if left, coords are lower-left corner; center they're the center, etc.
87 :     @type rawstr: string
88 :     @type coords: (float, float)
89 :     @type height: float
90 :     @type align: string
91 :     """
92 :     str = string.upper(rawstr)
93 :     glPushMatrix()
94 :     glTranslatef(x, y, 0)
95 :    
96 :     pixSize = float(height) / 5.0
97 :     lines = string.split(string.upper(rawstr), r'\N')
98 :     if valign == 'center': yOffset = 3.5 * len(lines) - 1 - 5
99 :     else: yOffset = (len(lines)-1) * 7
100 :     for line in lines:
101 :     if align == 'left': xOffset = 0
102 :     elif align == 'center': xOffset = -1 * pixLen(line)/2.0
103 :     elif align == 'right': xOffset = -1 * pixLen(line)
104 :     for c in line:
105 :     letter = getLetter(c)
106 :     # draw each rectangle of the letter:
107 :     for rect in letter[1:]:
108 :     botLeft = (pixSize * (xOffset + rect[0]), pixSize * (yOffset + rect[1]))
109 :     glRectf(botLeft[0], botLeft[1], botLeft[0]+pixSize*rect[2], botLeft[1]+pixSize*rect[3])
110 :     # shift over for next letter
111 :     xOffset = xOffset + letter[0] + 2
112 :     yOffset -= 7
113 :    
114 :     glPopMatrix()
115 :    
116 :     # ---------------
117 :     def getLetter(c):
118 :     # ---------------
119 :     """Just return the letter-definition of character c, if it exists.
120 :     If it's not there, return a space.
121 :     @type c: string
122 :     @rtype: list (see 'letters' definition above)
123 :     """
124 :     try: letter = letters[c]
125 :     except KeyError: letter = letters[' ']
126 :     return letter
127 :    
128 :     # --------------
129 :     def pixLen(str):
130 :     # --------------
131 :     """Number of "pixels" (in the font) wide that str is.
132 :     @type str: string
133 :     @rtype: integer
134 :     """
135 :     count = 0
136 :     for c in str:
137 :     count += getLetter(c)[0]
138 :     count += 2 * (len(str) - 1) # spaces between letters
139 :     return count

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3