Commits

Barry Schwartz  committed 9894b78

Added namespaces. Some more implementation, including piecewise curves. A "fontdesign" all-inclusive module, as well.

  • Participants
  • Parent commits 98f3a9f

Comments (0)

Files changed (4)

 ACLOCAL_AMFLAGS = -I m4
 
+pure_DATA = fontdesign.pure
+
 fontdesigndir = $(puredir)/fontdesign
-fontdesign_DATA = fontdesign/bezier.pure
+fontdesign_DATA = $(addprefix fontdesign/, bezier.pure	\
+	piecewise.pure)

File fontdesign.pure

+using fontdesign::bezier;
+using fontdesign::piecewise;

File fontdesign/bezier.pure

-// Linear interpolation.
-lerp u v t = (u * (1.0 - t)) + (v * t);
+namespace fontdesign with
 
-// Interpolate once between bezier control points.
-bezstep [p0,p1] t = [lerp p0 p1 t];
-bezstep (p0:p1:tail) t = (lerp p0 p1 t) : (bezstep (p1:tail) t);
+  // Interpolate once between bezier control points.
+  bezstep [p0,p1] t = [p0*(1.0 - t) + p1*t];
+  bezstep (p0:p1:tail) t = (p0*(1.0 - t) + p1*t) : (bezstep (p1:tail) t);
 
-// Return the value of a bezier at time t.
-bezpt [p0] _ = p0;
-bezpt ctrlpoints t = bezpt (bezstep ctrlpoints t) t;
+  // Return the value of a bezier at time t.
+  bezpt [p0] _ = p0;
+  bezpt ctrlpoints t = bezpt (bezstep ctrlpoints t) t;
 
-// Cut a bezier in two.
-bezcut [p0] _ = ([p0],[p0]);
-bezcut ctrlpoints t = ([head ctrlpoints] + subcut!0,
-                       subcut!1 + [last ctrlpoints])
-when
-  subcut = bezcut (bezstep ctrlpoints t) t;
+  // Return the value of the derivative of a bezier at time t.
+  bezderiv ctrlpoints t = n * du
+   when
+     n = #ctrlpoints - 1;
+     du = inner ctrlpoints t;
+   end
+   with
+     inner [u0,u1] _ = u1 - u0;
+     inner points s = inner (bezstep points s) s;
+   end;
+
+  // Cut a bezier in two.
+  bezcut [p0] _ = ([p0],[p0]);
+  bezcut ctrlpoints t = ([head ctrlpoints] + subcut!0,
+                         subcut!1 + [last ctrlpoints])
+   when
+     subcut = bezcut (bezstep ctrlpoints t) t;
+   end;
+
+  // Tagged bezier curves.
+  type bezier (Bezier p::list);
+  ctrlpoints (Bezier p::list) = p;
+  order c::bezier = #(ctrlpoints c) - 1;
+  point c::bezier t = bezpt (ctrlpoints c) t;
+  deriv c::bezier t = bezderiv (ctrlpoints c) t;
+  cut c::bezier t = bezcut (ctrlpoints c) t;
+
 end;
-
-// Return the value of the derivative of a bezier at time t.
-bezderiv ctrlpoints t = n * du
-when
-  n = #ctrlpoints - 1;
-  du = inner ctrlpoints t;
-end
-with
-  inner [u0,u1] _ = u1 - u0;
-  inner points s  = inner (bezstep points s) s;
-end;

File fontdesign/piecewise.pure

+// Piecewise curves.
+
+using math;
+
+namespace fontdesign with
+
+  private floatmod;
+  floatmod a b = (q - floor q)*b when q = (double a)/b end;
+
+  type piecewise (Piecewise parts::list cyclic::int);
+
+  parts (Piecewise p::list _::int) = p;
+  is_cyclic (Piecewise _::list cyclic::int) = cyclic;
+  set_cyclic (Piecewise p::list _::int) cyclic = Piecewise p cyclic;
+
+  length pwise::piecewise = #(parts pwise);
+
+  point pwise@(Piecewise p::list cyclic::int) t
+  = point pwise (floatmod t (#p)) if cyclic && (t < 0 || #p < t);
+  = point pwise 0 if ~cyclic && t < 0;
+  = point pwise (#p) if ~cyclic && #p < t;
+  = point (last p) 1.0 if t == #p;
+  = point (p!(int i)) q
+   when
+     i = floor t;
+     q = t - i;
+   end otherwise;
+
+end;