How to get width of text, including metrics?



  • Hi, I need to get width of text, including its metrics. When I use this underneath method, it gets width of text, but without metrics. Please, how can I get the full width, please? I need it to animate the text below to move exactly one width of letter.

    font_name = 'monaco'
    string = 'HHHH'
    font_size = 300
    fontSize(font_size)
    font(font_name)
    
    path = BezierPath()
    path.text(string, (0, 0), fontSize=font_size, font=font_name)
    left, bottom, right, top = path.bounds()
    
    stroke(0)
    strokeWidth(0.1)
    for i in range(100):
        for j in range(100):
            line((i * 10, 0), (i * 10, 700))
            
    stroke(1, 0, 0)
    strokeWidth(2)
    line((left, 0), (left, 700))
    line((right, 0), (right, 700))
    stroke(None)
    
    text_width = right - left
    text_height = top - bottom
    
    text(string, (text_width/4,0))
    text(string, (0,300))
    
    saveImage('bug.png')
    

    bug.png



  • hello @jansindl3r,

    have a look at textWidth(txt), it returns the width & height of a text box.

    good luck!



  • Hi @gferreira thanks a lot! Perfect..
    Jan



  • @gferreira I also believe that you meant
    textSize('string')


Log in to reply