License Public Domain
Lines 34
Keywords
fit (1) follow (1) path (1) text (3)
Permissions
Owner: tomdesmedt
Viewable by Everyone
Editable by All Siafoo Users
Hide
Easily highlight source code for your blog with our Syntax Highlighter. Join Siafoo Now or Learn More

fit text to path Atom Feed 0

In Brief Draws the given text along the outline of the path.... more
# 's
 1from nodebox.geo import angle
2def fit_text_to_path(txt, path, start=0.01, dy=10, loop=False):
3 """ Draws text along the outline of the given path.
4 txt : a string of text
5 path : a BezierPath object (oval, curve, ...)
6 start : starting position on the path, between 0.0 and 1.0
7 dy : distance from the path
8 loop : flip from the end of the path to the beginning?
9 """
10 W = _ctx.textwidth(txt)
11 dW = W / path.length
12 first = True
13 for ch in txt:
14 w = textwidth(ch)
15 if first:
16 t = start
17 first = False
18 else:
19 t += w / W * dW
20 if loop:
21 t = t % 1.0
22 pt = path.point(t)
23 a = angle(pt.x, pt.y, pt.ctrl2.x, pt.ctrl2.y)
24 transform(CORNER)
25 push()
26 translate(pt.x, pt.y)
27 rotate(-a+0)
28 text(ch, -w, -dy)
29 pop()
30
31speed(20)
32def draw():
33 path = oval(200, 200, 200, 400)
34 txt = "text following a path"
35 fit_text_to_path(txt, path, start=FRAME*0.01, loop=True)

Draws the given text along the outline of the path.

http://www.siafoo.net/image/105