0

I’m writing a cross-platform text parser that, among other things, word wraps text so that another cross-platform program (which I don’t control) can properly render the text in a variable-length font. Word wrapping requires getting the pixel-width of the text, but width varies depending on which OS I run my program on. The following code, for example, says the word "Wings" has length 31 on OS X 10.10, but on Windows XP, it says 30.

from Tkinter import Tk
import tkFont

root = Tk()
font = tkFont.Font(family="Times New Roman", size=-12)
# Note the negative value of size, showing the font size is the same.
print font.measure("Wings”)

This means that text wrapped on one platform won’t be displayed properly when the other program is run on the other platform. If I word wrap on XP and display on OS X, the text that I wrapped assuming it was 30 pixels is now 31 pixels. That one extra pixel could cause an error in how the wrapped text displays on the other program, and that was from just one word. Longer words cause more pixel differences, which makes the word wrapper less accurate.

I’m aware that the Tkinter widths should be different because the different OS's render the font differently. Since the font I’m using in my actual program is wider in Windows than OS X, I’d like to have Tkinter use the Windows measurement, even if I’m on OS X. This seems to be the only way to get a cross-platform font measurement that doesn’t risk word wrapping too late. How can I go about doing this? If I can’t, how else could I get a cross-platform width measurement that's consistent, reasonably accurate, and not too small?

EDIT: The program that reads the output of my program expects a string of text for it to display, and I cannot control how it displays it, or convince it to accept an image. The only thing I can control is that string.

6
  • You're saying you know the fonts are different on different platforms, but you want to calculate the width of the font on windows, even if you're actually running it on the mac? Wouldn't it be easier to simply make your widget big enough on each platform to show the same number of characters? if the string width is 30 on one platform and you want to display 10 of those, make the window 300 pixels. On a platform where the string width is 31, why not just make the window 310 pixels? May 18, 2015 at 22:08
  • If you need bitmaps, use bitmaps. There is a reason people pre-render some text into image formats. If you leave it to the platform font renderer you have basically lost control over the minuscle details. You can always install your own font renderer on both platforms and do all the hard work yourself, you also need to ship all the fonts you need. But if you need that, it might be easier to ship a VM image instead.
    – schlenk
    May 18, 2015 at 22:14
  • Step 1: bundle your font so you know you're using the same one. Step 2: use the same shaping engine on both Operating systems (like harfbuzz or something). Then look at the differences. Until then, "the inputs are different. Obviously the outputs are going to be different." May 18, 2015 at 23:29
  • @Mike The font is the same; the problem is kerning. Windows always has 2 pixels between characters for this font, but OS X can seemingly randomly have 3 or 4 (1.5 or 2 after factoring for retina display). The loss of pixels comes when it goes with 3 instead of 4.
    – Enthalpy
    May 19, 2015 at 1:00
  • @schlenk Post edited to clarify why bitmaps aren’t a viable solution. I may just have to go with the VM image.
    – Enthalpy
    May 19, 2015 at 1:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.