Python and Pixel perfect placement
I’ve been tinkering around with the basics of a notebook interface for CASes. I’ve been using Tkinter up to now, which is turning out to be the easiest GUI kit I’ve ever messed around with— unfortunately, Tkinter doesn’t seem to support the level of control I need over font information: to manage all the weird convolutions (no pun intended) that mathematical expressions can involve, I’m having to rewrite a text widget from scratch. At least, I haven’t attempted to ‘merely modify’ the text widget, because I think that would be a harder task (and perhaps impossible; certainly more ambitious than I want to be), using any GUI kit. So I’ve been hopping around from Python GUI kit to kit: wxPython, wax, PythonCard, PyGTK, Tkinter, Jython/Swing. The situation was equally hopeless: due to the cross-platform requirements of all of these, the font control is lacking. For instance, there is no reliable way to load a font file with a particular name, as far as I could see; it seems like a lot of the finer levels of font selection can only be managed through the font selection dialogs offered, which is inappropriate for this situation. Also, there didn’t seem to be very good, or reliable ways of gauging the bounding box of a piece of text. These are basically the two features I need most.
I finally remembered today, that the FreeType library does these things and more, and is cross-platform. By using the FreeType library, I could treat individual glyphs of the font as images, and construct all mathematical expressions as images, using logic separate from the main text widget, so I could use rich text widgets, which handle images and text, for input purposes. That would save a lot of trouble— to be honest, given my laziness, would stop the project from halting in its tracks—, because I wouldn’t have to worry about the annoying details of managing line wrapping, justification, etc, or even image placement (assuming the kit used has a rich enough text editor widget). All I have to implement is the logic which makes the images seem responsive as text to user input, handles high-lighting, cursor display, etc. Two problems remain: finding a Python binding for FreeType— it seems there is one, ft2, but I have not tried it out to see if I can work with it— and making a choice of toolkits to use. Tkinter seems like the best choice in terms of standardization, but I’m going to look at wxPython and PyGTK to see if either of those might make my image interaction tasks easier; I’m mostly concerned about cursor positioning and resizing and high-lighting control. Of course, there is also the not insignificant problem of the actual placement of the glyphs; mostly, I’m worried about figuring out how to place the glyphs that form extensible characters, like an integral sign. But I’ll cross that Rubicon once I get to it.
Possibly relevant posts:
- Joys of Python (5/16/2005)
- An Intro to Typography (5/31/2005)
- User directory python package installation (3/21/2007)