finding Text Length in pixels
#1
Hii

I'm pretty new to python and i'm having trouble finding the length of a text string in pixels. Now i know i can get the length in characters by using the len() function but because the text is proportional i cant just multiply this by a fixed number to get how many pixels wide the whole string will take up.

I'm writing a script to emulate (to a degree) a menu system like windows within XBMC.
I need this so i can position the menu headings across the top of the screen without gaps between headings and to position the menu items directly below each heading.
The menu headings will be different lengths and to set the width of each heading control i need to know exactly how wide the text will be.

I will eventually upload this script as i think it'll be useful for other script writers. It will be rather generic and very simply to implement any sort of menu thats required.

Hope someone can help with this little problem


Dire.
Reply
#2
I think there is likley to be a simple way of doing this but here's one idea:

You could try to use the get.pixel function on a small blank image (say white square) containing only 1 character of the character set (in black). you could locate the position of the first and last vertical black pixel if you parse through the image line by line. This would give a reasonable value as to the width of the character in pixels.

You could run this routine up front and loop through your character set to build an indexed list of characters & widths that you could use later in your app to calculate the length of the sentence whenever you want to display text.

Only thing is the spacing between characters which may vary I suppose.

I had the same issue developing an app for a Nokia 6630 in python - in the end i measured each character width on screen in pixels for the whole alphanumeric set using a magnifying lens !!! I built an indexed list from those measurements and used it in a function to wrap sentences to the display with great effect.

cheers
Reply
#3
A simpler solution that follows the same idea is to simply create a list of character width/height via manual tedium, and loop through your string char by char adding length as you go.. If you know the font that will be usedit makes things simple.. If that can change without any way for you to programmatically check then you will need to go with the previous solution.. Another thing to consider is videomode/region, however I'm positive these can be detected through code.. (The actual font, I'm not sure if python can read that directly or not, but it is stored on the disk so it can obviously be read if you put your mind to it..
On the otherhand, if this is a gui app then you'd probably be better following the idea posted in the other forum.. Using xbmcgui.control attributes is definately the way to go if at all possible..
Reply
#4
Check the MyTV script, from memory it has a function to check the length of a string so that it does not force the text to scroll on EPG buttons.
Reply

Logout Mark Read Team Forum Stats Members Help
finding Text Length in pixels0