views:

188

answers:

3

I have a Python script which needs to calculate the exact size of arbitrary strings displayed in arbitrary fonts in order to generate simple diagrams. I can easily do it with Tkinter.

import Tkinter as tk
import tkFont
root = tk.Tk()
canvas = tk.Canvas(root, width=300, height=200)
canvas.pack()
(x,y) = (5,5)
text = "yellow world"
fonts = []
for (family,size) in [("times",12),("times",24)]:
    font = tkFont.Font(family=family, size=size)
    (w,h) = (font.measure(text),font.metrics("linespace"))
    print "%s %s: (%s,%s)" % (family,size,w,h)
    canvas.create_rectangle(x,y,x+w,y+h)
    canvas.create_text(x,y,text=text,font=font,anchor=tk.NW)
    fonts.append(font) # save object from garbage collecting
    y += h+5
tk.mainloop()

The results seem to depend on the version of Python and/or the system:

Python 2.5 Mac 0S X, times 12: (63,12), times 24: (128,24). Python 2.6 Mac OS X, times 12: (64,14), times 24: (127,27). Python 2.6 Windows XP, times 12: (78,19), times 24: (169,36)

After Ned Batchelder mentioned it, I discovered that the size of fonts differs from platform to platform. It may not be a deal breaker as long as you stick with Tkinter, which remains consistent with itself. But my complete program does not use Tkinter to perform the actual drawing: it just relies on its font size calculations to generate an output (in SVG or as a Python script to be sent to Nodebox). And it's there that things go really wrong:

Output of mocodo

(Please look at the image in real size. Note that the main font used for these outputs is not Times, but Trebuchet MS.)

I now suspect that such discrepancies can't be avoided with Tkinter. Which other cross-platform solution would you recommend?

+2  A: 

You haven't done anything wrong: the size of fonts does differ from platform to platform.

I'm not sure why the Python version matters, but the differences are only one pixel, so it could be different rounding or different rendering of the same font.

Ned Batchelder
@Ned: the python version matters because the tk version matters -- font measurements changed between tk 8.4.x and 8.5.x (the versions used by python 2.5 and 2.6, respectively).
Bryan Oakley
A: 

Theres a couple unknowns here that maybe you could clear up (I couldn't find good documentation on the tkFont class):

  • Are you sure the units are consistent across platforms? There are different coordinate systems that can return things close but not actually the pixel dimensions (in Windows things are usually measured using device-independent coordinates for example).

  • Does Tk take care of kerning/inter-glyph spacing as well? This is typically the cause of small pixel errors when measuring fonts. If it doesn't you may need to drop down to a system level API to do accurate font measurements.

Ron Warholic
+1  A: 

You have two problems. Let's tackle them one at a time

1: the difference between python 2.5 and 2.6 on the same platform with the same font

These two versions of python use different versions of tk. On my mac box, 2.5 uses tk version 8.4.19 and 2.6 uses 8.5.7. In version 8.5.2 of tk were some changes to the font measurement features of tk. Assuming that the changes were improvements, I think it's safe to assume that the numbers you get from python 2.6 are more accurate than the ones from 2.5.

2: the difference between python 2.6 on the mac and 2.6 on the PC.

Obviously, from the screenshots you include, the PC is using a larger font and thus you get larger numbers for the measurement. The question is, why? You are specifying the font size in points (1/72 of an inch). In order for Tk (or any rendering system) to render the font, it needs to know how many pixels are in an inch. This will vary on different systems, and Tk isn't always given an accurate number by the underlying OS in order to do its calculations.

Historically, Apple and Microsoft have standardized on 72ppi and 96ppi regardless of the actual display, so the numbers are always going to be different. For more information about the differences in how the mac and windows calculate pixel density see the Dots Per Inch article on wikipedia.

You might try solving this by specifying a font in pixels rather than in points. You can do this by using negative numbers for the font size.

Finally, one thing you might add to your little example code is to print out the result of the command "font.actual()" command -- you might see something different between your windows and mac boxes, which would explain the differences there. This tells you exactly which font is being used by Tk.

Bryan Oakley
Thank you so much for your answer! You made me understand and fix the two problems. The first one came from my version of Python 2.6: namely, 2.6.1, which actually suffers of a regression bug concerning the font metrics: updating to Python 2.6.5 was the easy solution. The second problem disappeared too when I used negative numbers to specify the size of the fonts. This behavior was actually explained in the Tk documentation at http://www.tcl.tk/man/tcl8.4/TkCmd/tk.htm, which I didn't know.All rocks now and I'm very grateful!
Aristide
@Aristide: If my answer or any other answer helps you, please consider marking the answer as 'accepted' -- it's a part of your duty as a question-asker.
Bryan Oakley
@Bryan: your answer did help me. I've already marked it as useful, but for the rest of my duty, the system keeps saying: "You may accept this answer and award your bounty in 10 hours". I suppose there's a minimum delay of 24h. Please be assured I will gladly retry tomorrow!
Aristide