The new NodeBox forum is live! Everything from support.nodebox.net moved here: sign in with the same email to pick up your old posts. Something not working? Write to forum-support@nodebox.net.

NodeBox 1ArchiveFrom the old forum

calculate textsize for fixed width

6 replies Last reply

  1. Where it is quite esay to do in nodebox 3 I have difficulties making the same behavior work in Nodebox 1:
    I want text blocks to have different font sizes, so that they align vertically. See the attached photo. My current code does it quite good bot not perfect, it depends on the text input. Does anyone know a better attempt to do this?

    txt="Hello Width!"
    desired_width=300
    fontsize(100)
    w_draft=textwidth(txt)
    fz=100*desired_width/w_draft
    fontsize(fz)
    print textwidth(txt)
    text(txt, 100, 100)
    
    Copy link
  2. There is no specific command for that, as I want to scale the text to full width, not make a textarea

    Copy link
  3. Thanks for your help, or let's say clues (^^^)
    I was able to do it with BezierPath, file size is the only downside :)

    ´´´
    width=500
    last_y=0
    for txt in ['Hi', 'Hello', 'Width!']:
    path = textpath(txt,0,0)
    path.fit(0, last_y, width)
    last_y = last_y + path.bounds.size.height
    drawpath(path)
    ´´´

    Copy link
  4. width=500
    last_y=0
    for txt in ['Hi', 'Hello', 'Width!']:
      path = textpath(txt,0,0)
      path.fit(0, last_y, width)
      last_y = last_y + path.bounds.size.height
      drawpath(path)
    
    Copy link
  5. NodeBox team

    Indeed, The "fit" approach is also how NodeBox 3 does it.

    I've also tried your approach, and it seems to work fine, only the commas and question marks at the end throw things off. Here's my version and the result:

    lines = """Lorem ipsum
    dolor sit amet
    consectetur adipiscing elit
    Donec velit arcu
    cursus eu vestibulum id
    finibus nec mauris"""
    
    y = 100
    line_height = 0.95
    desired_width=300
    for line in lines.split('\n'):
        fontsize(100)
        w_draft = textwidth(line)
        scaled_font_size = 100 * desired_width / w_draft
        fontsize(scaled_font_size)
        text(line, 100, y + scaled_font_size * line_height)    
        y += scaled_font_size * line_height
    
    Copy link

Know something that helps?

Sign in with your email address to reply. No password needed.

Sign in to reply