user1618465
user1618465

Reputation: 1941

Get the printed length of a word by text width

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 )

Upvotes: 2

Views: 1742

Answers (1)

user1618465
user1618465

Reputation: 1941

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))

Upvotes: 5

Related Questions