-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.py
140 lines (116 loc) · 3.29 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/python
import sys
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import FTGL
fonts = []
width = 600
height = 600
def do_ortho():
w, h = width, height
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
size = max(w, h) / 2.0
aspect = float(w) / float(h)
if w <= h:
aspect = float(h) / float(w)
glOrtho(-size, size, -size*aspect, size*aspect, -100000.0, 100000.0)
else:
glOrtho(-size*aspect, size*aspect, -size, size, -100000.0, 100000.0)
glScaled(aspect, aspect, 1.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def draw_scene():
string = []
chars = []
chars[:] = []
for i in range(1, 32):
chars.append(chr(i))
string.append("".join(chars))
chars[:] = []
for i in range(32, 64):
chars.append(chr(i))
string.append("".join(chars))
chars[:] = []
for i in range(64, 96):
chars.append(chr(i))
string.append("".join(chars))
chars[:] = []
for i in range(96, 128):
chars.append(chr(i))
string.append("".join(chars))
chars[:] = []
for i in range(128, 160):
chars.append(chr(i))
string.append("".join(chars))
chars[:] = []
for i in range(160, 192):
chars.append(chr(i))
string.append("".join(chars))
chars[:] = []
for i in range(192, 224):
chars.append(chr(i))
string.append("".join(chars))
chars[:] = []
for i in range(224, 256):
chars.append(chr(i))
string.append("".join(chars))
glColor3f(1.0, 1.0, 1.0)
for i, font in enumerate(fonts):
x = -250.0
yild = 20.0
for j in range(0, 4):
y = 275.0 - i * 120.0 - j * yild
if i >= 3:
glRasterPos(x, y)
font.Render(string[j])
elif i == 2:
glEnable(GL_TEXTURE_2D)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glPushMatrix()
glTranslatef(x, y, 0.0)
font.Render(string[j])
glPopMatrix()
glDisable(GL_TEXTURE_2D)
glDisable(GL_BLEND)
else:
glPushMatrix()
glTranslatef(x, y, 0.0)
font.Render(string[j])
glPopMatrix()
def on_display():
glClear(GL_COLOR_BUFFER_BIT)
do_ortho()
draw_scene()
glutSwapBuffers()
def on_reshape(w, h):
width, height = w, h
def on_key(key, x, y):
if key == '\x1b':
sys.exit(1)
if __name__ == '__main__':
glutInitWindowSize(width, height)
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE)
glutCreateWindow("PyFTGL Demo")
glClearColor(0.0, 0.0, 0.0, 0.0)
try:
fonts = [
FTGL.OutlineFont(sys.argv[1]),
FTGL.PolygonFont(sys.argv[1]),
FTGL.TextureFont(sys.argv[1]),
FTGL.BitmapFont(sys.argv[1]),
FTGL.PixmapFont(sys.argv[1]),
]
for font in fonts:
font.FaceSize(24, 72)
except:
print "usage:", sys.argv[0], "font_filename.ttf"
sys.exit(0)
glutDisplayFunc(on_display)
glutReshapeFunc(on_reshape)
glutKeyboardUpFunc(on_key)
glutMainLoop()