2

Is there any way to get the largest word in a list but not by the number of characters in each word, but by its width once printed?

For example the width of the character I is less than the length of m.

Examples:

mwmwmwmwmw (len=10)

iiiiiiiiiiiiiiiiiiii (len=20)

As we can see in this example, the len of the second string is higher than the first one, but in terms of width, it is shorter.

I know it is a weird question but interesting seeing the different approaches.

Of course this depend on the font used at the printing time :)

NOTE: I know it depends on the font but even in that case that should more or less happen for every font (what I mean is that the m will be larger tan the i )

user1618465
  • 1,813
  • 2
  • 32
  • 58
  • May I know the motivation of doing this? – Peaceful May 05 '16 at 03:01
  • 1
    You're right that this will depend on the font. What graphics library are you using? – ChrisGPT was on strike May 05 '16 at 03:02
  • 3
    You said it: it depends on how the characters are rendered - which font, kerning, spacing, and a myriad of other factors is being used. What information about the rendering do you have? – mhawke May 05 '16 at 03:02
  • 1
    Have you checked the solution via tkinter? http://stackoverflow.com/questions/2922295/calculating-the-pixel-size-of-a-string-with-python – Adib May 05 '16 at 03:05
  • Another possible method: http://stackoverflow.com/questions/17856242/convert-string-to-image-in-python – pyInTheSky May 05 '16 at 03:56
  • Have you considered printing 20 copies of each character on a separate line, firing up your text editor, and sorting them visually? Once you have that list, you can write the function directly. – aghast May 05 '16 at 17:23

1 Answers1

5

I finally used this other question link to get create a function that does what I want:

def get_printed_size(text, myfont):
    root = tk.Tk() 
    font = tkFont.Font(family=myfont[0], size=myfont[1])
    (w,h) = (font.measure(text),font.metrics("linespace"))
    print "%s %s: (%s,%s)" % (myfont[0],myfont[1],w,h)
    return w

that can be called:

get_printed_size("some text", ("Helvetica",10))
Community
  • 1
  • 1
user1618465
  • 1,813
  • 2
  • 32
  • 58