Skip to content

Instantly share code, notes, and snippets.

@propella
Created November 10, 2011 19:49
Show Gist options
  • Save propella/1355960 to your computer and use it in GitHub Desktop.
Save propella/1355960 to your computer and use it in GitHub Desktop.
env := GZEnvironment empty.
env source:
'
Glyph <: (w:Real, s:Real)
Word <: (w:Real, s:Real, n:Real)
Point <: (x:Real, y:Real)
MakeWords : Glyph >> Word
W = (0, 0, 0):Word
∀ G
if G.s ≠ W.s
W'' = (G.w, G.s, 1)
>> W
else
W'' = (W.w + G.w, W.s, W.n + 1)
>> W
PlaceWords (o:Point, w:Real, h:Real) : Word >> (Point, Real)
x = o.x
y = o.y
∀ W
if (x + W.w < o.x + w) ∨ (W.s = 1)
x'' = x + W.w
>> ((x, y), W.n)
else
x'' = o.x + W.w
y'' = y + h
>> ((o.x, y''), W.n)
DuplicatePlacement : (Point, Real) >> Point
∀ (P, n)
if n > 0
>> P
<< (P, n - 1)
PlaceGlyphs : (Point, Glyph) >> Point
x = 0
y = 0
o = 0
∀ (P, (w, _))
if P.x = x ∧ P.y = y
o'' = o + w
>> (x + o, y)
else
x'' = P.x
y'' = P.y
o'' = w
>> (x'', y'')
LayoutText (o:Point, w:Real, h:Real) : Glyph >> Point
⇒ DupZip (MakeWords → PlaceWords (o, w, h) → DuplicatePlacement, (→)) → PlaceGlyphs
'.
env set: 'w' to: 100.
env set: 'h' to: 10.
env set: 'ox' to: 0.
env set: 'oy' to: 0.
env feed: #(10 0 10 0 10 0 5 1 10 0 10 0 5 1 10 0 10 0 10 0 10 0 5 1 10 0 10 0 10 0) to: 'LayoutText'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment