lenght (in pixel) of a string...

It’s possible to know the pixel lenght of a string ?
I.E.
I want draw at the end of text “foo iiii”.
I use the glRasterPos
to assign the start postion of the text.
Now I want to know the final postion, after the last “i” letter.
How I can do this?

I try with
len(“foo iii”)*LETTER_WIDTH, but the letter
don’t have the same width…I.E. the “i” is smaller pixel
size than “o”.

Ah…another little question: can you tell me the code for a composite event, like SHIFT+AKEY?

I try like this:

if (evt == AKEY and val):
    if (evt == LEFTSHIFTKEY and val):
          print "A"
    else:
          print "a"

but it don’t work

I have the same problem in Blender, i tries to work around it by using the digitalreadout.tga. I used a plane with a fixed size (for example 1x1 units) and moved an empty each time a key was pressed. It more or less works except for the “.” character, etc…

i hope this helps a little and maybe someone else found a more elegant solution…

try using that code


"""
string operation module

For now, the only operation is:

Length(string) which returns the lentgh in pixel of a string

For debugging purpose, it will print out a charater that is NOT
in the lookup table.

"""

LW = {}
LW["a"] = 8
LW["b"] = 7
LW["c"] = 7
LW["d"] = 7
LW["e"] = 7
LW["f"] = 5
LW["g"] = 7
LW["h"] = 7
LW["i"] = 5
LW["j"] = 5
LW["k"] = 6
LW["l"] = 5
LW["m"] = 11
LW["n"] = 7
LW["o"] = 6
LW["p"] = 7
LW["q"] = 7
LW["r"] = 5
LW["s"] = 7
LW["t"] = 5
LW["u"] = 7
LW["v"] = 7
LW["w"] = 9
LW["x"] = 7
LW["y"] = 7
LW["z"] = 7
LW["A"] = 9
LW["B"] = 8
LW["C"] = 8
LW["D"] = 9
LW["E"] = 8
LW["F"] = 8
LW["G"] = 9
LW["H"] = 9
LW["I"] = 6
LW["J"] = 7
LW["K"] = 9
LW["L"] = 8
LW["M"] = 11
LW["N"] = 9
LW["O"] = 9
LW["P"] = 9
LW["Q"] = 9
LW["R"] = 9
LW["S"] = 8
LW["T"] = 7
LW["U"] = 9
LW["V"] = 9
LW["W"] = 11
LW["X"] = 9
LW["Y"] = 9
LW["Z"] = 8
LW["0"] = 7
LW["1"] = 5
LW["2"] = 7
LW["3"] = 7
LW["4"] = 7
LW["5"] = 7
LW["6"] = 7
LW["7"] = 7
LW["8"] = 7
LW["9"] = 7
LW[" "] = 3
LW["!"] = 3
LW["?"] = 6
LW["("] = 4
LW[")"] = 4
LW["\\"] = 5
LW["/"] = 5
LW["\""] = 3
LW["'"] = 3
LW[":"] = 4
LW[";"] = 4
LW["|"] = 3
LW[">"] = 7
LW["<"] = 7
LW["."] = 4
LW[","] = 4
LW["*"] = 8
LW["-"] = 11
LW["+"] = 8
LW["="] = 8
LW["@"] = 13
LW["#"] = 10
LW["$"] = 9
LW["%"] = 12
LW["^"] = 7
LW["&"] = 11
LW["~"] = 8
LW["`"] = 5
LW["_"] = 8
LW["{"] = 5
LW["}"] = 5	
LW[""] = 0
LW["	"] = 16

def Length(string):
	global LW
	len = 0
	for s in string:
		try:
			len += LW[s]
		except:
			print "missing: ", repr(s)
	return len

some length might be off by one or two pixel, but that’s about it.

Martin

Yeah I ended up doin somethin like that. But its VERY tedious, and it only works properly for one particular font because the relative sizes of the characters can be different for different fonts. What bitmap font did you use to work those out?

Keith. 8)

The default font of the GUI module.

Martin

Thx Martin!
Are you writing an utility lib to use string in Blender?

nope, that was done for my GUI class.

Martin

And about the composite event?
I.E. “SHIFT+A”

you mean, a capital A like that “A”?

that’s also include in the dictionnary.

Martin

I don’t know the code to recognize the composite keyboard
event, like “SHIFT A”. I’ve try this:

if (evt == AKEY and val): 
    if (evt == LEFTSHIFTKEY and val): 
          print "A" 
    else: 
          print "a"

But it don’t work… :frowning:

you could try using a global variable to determine the state of the Shift key (pressed or depressed)


ShiftState = 0

def event(evt, val):
        if evt == LEFTSHIFTKEY: 
                ShiftState = val
        if evt == AKEY:
                if ShiftState:
                        print "A"
                else:
                        print "a"

Martin

Mhhh…it don’t work…but take a strange result…
I’ve LEFTSHIFTKEY event when I press the leftmouse button! :o

And however don’t work… :frowning:

I’m writing a command line interface for MakeHuman. When
is decent, I post it and you can see this strange effect…

Keys pressed with the shift key have a different ascii value to keys pressed without the shift key. for example Shift+A produces a capital A which has an ascii value of 65. but just pressing ‘a’ on its own will produce a small case ‘a’ which has a different ascii value (not sure what it is) Most programs test if the ‘a’ has been pressed on its own by determining if the ascii value of the key pressed is equal to the ascii value of little ‘a’. They test if the Shift+A has been pressed by determining if the ascii value of the key pressed is equal to the ascii value of capital ‘A’, 65. Thats the simplest way to do it, but it relies on CAPS LOCK being off.

Keith. 8)

Thanks!
I’ve solved, but now there is a new problem with
SHIFT Q:

https://blenderartists.org/forum/viewtopic.php?t=10046