Differences between revisions 8 and 9
Revision 8 as of 2008-07-17 01:26:50
Size: 1815
Comment:
Revision 9 as of 2008-07-17 01:31:17
Size: 1841
Comment:
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
self.font_renderer.set_font_mode(FTGLFontMode.TEXTURE) # or EXTRUDE, or PIMAP self.font_renderer.set_font_mode(FTGLFontMode.TEXTURE) # or EXTRUDE, PIXMAP, BITMAP, POLYGON or OUTLINE

Using FTGL in python in EMAN2

Programmers have to create an instance of an EMFTGL object, for instance as a member variable:

self.font_renderer = EMFTGL()
self.font_renderer.set_face_size(16)
self.font_renderer.set_using_display_lists(True)
self.font_renderer.set_depth(32) # only applicable if font mode is EXTRUDE
self.font_renderer.set_font_mode(FTGLFontMode.TEXTURE) # or EXTRUDE, PIXMAP, BITMAP, POLYGON or OUTLINE
#self.font_renderer.set_font_file_name("/usr/share/fonts/dejavu/DejaVuSerif-Bold.ttf")

Note that the EMFTGL has default values for all of its parameters and that in general you want to limit the number of times you call the setter functions, because they cause FTGL to regenerate a FTFont class and this is not a trivial expense.

Then to render text use a command similar to the following in your paint function:

glPushMatrix() # FTGL does transformations - must push
self.font_renderer.render_string("Important Information")
glPopMatrix()

To get the bounding box of the string use:

bbox = self.font_renderer.bounding_box("Important Information")

If you want to rotate the font as though it is centered on the origin you might use

message = "Important Information"
rotation = 45
rot_axis = [1,0,0]
bbox = self.font_renderer.bounding_box(message)

glPushMatrix()
glTranslate(-(bbox[0]-bbox[3])/2,-(bbox[1]-bbox[4])/2,-(bbox[2]-bbox[5])/2)
glRotate(rotation,*rot_axis)
glTranslate((bbox[0]-bbox[3])/2,(bbox[1]-bbox[4])/2,(bbox[2]-bbox[5])/2)
self.font_renderer.render_string(message)
glPopMatrix()

Recommendations

TEXTURE fonts are probably the most versatile types. They are fast, antialiased, and can be transformed just like any OpenGL primitive. See http://ftgl.sourceforge.net/docs/html/ftgl-tutorial.html

Eman2UsingFTGL (last edited 2022-02-18 00:29:09 by TunayDurmaz)