tag:support.nodebox.net,2012-11-01:/discussions/nodebox-1/43-calculate-textsize-for-fixed-widthNodeBox: Discussion 2014-08-26T08:43:22Ztag:support.nodebox.net,2012-11-01:Comment/342677792014-08-25T17:30:30Z2014-08-25T17:30:30Zcalculate textsize for fixed width<div><p>see: <a href=
"http://nodebox.net/code/index.php/Reference">http://nodebox.net/code/index.php/Reference</a></p></div>ugajintag:support.nodebox.net,2012-11-01:Comment/342677792014-08-25T19:38:36Z2014-08-25T19:38:36Zcalculate textsize for fixed width<div><p>There is no specific command for that, as I want to scale the
text to full width, not make a textarea</p></div>dreerrtag:support.nodebox.net,2012-11-01:Comment/342677792014-08-25T20:28:28Z2014-08-25T20:28:41Zcalculate textsize for fixed width<div><p>Take another look.</p></div>ugajintag:support.nodebox.net,2012-11-01:Comment/342677792014-08-25T21:49:10Z2014-08-25T21:49:18Zcalculate textsize for fixed width<div><p>Thanks for your help, or let's say clues (^^^)<br>
I was able to do it with BezierPath, file size is the only downside
:)</p>
<p>´´´<br>
width=500<br>
last_y=0<br>
for txt in ['Hi', 'Hello', 'Width!']:<br>
path = textpath(txt,0,0) path.fit(0, last_y, width) last_y = last_y
+ path.bounds.size.height drawpath(path) ´´´</p></div>dreerrtag:support.nodebox.net,2012-11-01:Comment/342677792014-08-25T21:52:15Z2014-08-25T21:52:17Zcalculate textsize for fixed width<div><pre>
<code>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)</code>
</pre></div>dreerrtag:support.nodebox.net,2012-11-01:Comment/342677792014-08-26T08:43:21Z2014-08-26T08:43:21Zcalculate textsize for fixed width<div><p>Indeed, The "fit" approach is also how NodeBox 3 does it.</p>
<p>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:</p>
<pre>
<code>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</code>
</pre></div>Frederik De Bleser