calculate textsize for fixed width

dreerr's Avatar

dreerr

25 Aug, 2014 02:52 PM

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)
  1. 1 Posted by ugajin on 25 Aug, 2014 05:30 PM

    ugajin's Avatar
  2. 2 Posted by dreerr on 25 Aug, 2014 07:38 PM

    dreerr's Avatar

    There is no specific command for that, as I want to scale the text to full width, not make a textarea

  3. 3 Posted by ugajin on 25 Aug, 2014 08:28 PM

    ugajin's Avatar

    Take another look.

  4. 4 Posted by dreerr on 25 Aug, 2014 09:49 PM

    dreerr's Avatar

    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) ´´´

  5. 5 Posted by dreerr on 25 Aug, 2014 09:52 PM

    dreerr's Avatar
    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)
    
  6. Support Staff 6 Posted by Frederik De Ble... on 26 Aug, 2014 08:43 AM

    Frederik De Bleser's Avatar

    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
    

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Already uploaded files

  • Bildschirmfoto_2014-08-25_um_16.51.19.png 33.1 KB

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

Recent Discussions

19 Jun, 2025 10:26 PM
17 Jun, 2025 06:15 AM
17 Jun, 2025 06:11 AM
11 Jun, 2025 10:57 AM
11 Jun, 2025 09:53 AM

 

10 Jun, 2025 08:03 AM
10 Jun, 2025 01:33 AM
08 Jun, 2025 09:25 AM
08 Jun, 2025 09:19 AM
08 Jun, 2025 09:19 AM
08 Jun, 2025 09:18 AM