2
$\begingroup$

Further my previuos question, i'd like to know how i cant draw multi line with an array fonction on the Y axis always there concervant the different colors.

for exemple, i'd like draw:

"Ngons: 5"

"Tris: 60"

Ngons and Tris are the label and have to be in red

5 and 60 are the value and have to be in blue.

How can i do that please ?

$\endgroup$

1 Answer 1

4
$\begingroup$

Building on the previous answer, now you have to think about line-height. A reasonable line-height for typesetting is the height of capital M x 1.45. But the coefficient can range between 1.2 and 1.45 (See the wikipedia article on Leading. These values are suggestions, and for HUD text or UI information use the value that seems most pleasing and doesn't cause the text to be too close or too separated vertically.

Take for instance the default line-height of Blender fonts.

enter image description here

1 / 0.682 = 1.46    # it's only a suggestion anyway.

A multi-line version of the script from the previous question, might look something like this:

def draw_string(x, y, packed_strings):
    font_id = 0
    blf.size(font_id, 18, 72) 
    x_offset = 0
    y_offset = 0
    line_height = (blf.dimensions(font_id, "M")[1] * 1.45)
    for command in packed_strings:
        if len(command) == 2:
            pstr, pcol = command
            #bgl.glColor4f(*pcol) -> Blender 2.7x
            blf.color(font_id, pcol[0], pcol[1], pcol[2], pcol[3]) # Blender 2.8x
            text_width, text_height = blf.dimensions(font_id, pstr)
            blf.position(font_id, (x + x_offset), (y + y_offset), 0)
            blf.draw(font_id, pstr)
            x_offset += text_width
        else:
            x_offset = 0
            y_offset -= line_height
            

x = 60
y = 100
RED = (1, 0, 0, 1)
GREEN = (0, 1, 0, 1)
BLUE = (0, 0, 1, 1)
CR = "Carriage Return"
ps = [("Blue ", RED),("Yellow ", BLUE), CR, ("White ", GREEN)]
draw_string(x, y, ps)

enter image description here

You aren't limited to single words either:

ps = [("Blue Steel", RED),("Yellow Monk Fruit", BLUE), CR, ("White as an otter", GREEN)]
draw_string(x, y, ps)

produces this. (and note I forgot on purpose to add a space after "Fruit" to show how words are separated).

enter image description here

caveat: Don't change the "Carriage Return" string to something with 2 characters, it's a place-holder to keep the code simple and its length must be anything other than 2.

$\endgroup$
1
  • $\begingroup$ Thank you very much once again, it is just that I needed $\endgroup$
    – pistiwique
    Commented Jun 1, 2015 at 10:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .