Commits

Barry Schwartz committed 2f0fad2

Code to write a glyph-update module for fontforge.

  • Participants
  • Parent commits ccef366

Comments (0)

Files changed (4)

 pure_DATA = fontdesign.pure
 
 fontdesigndir = $(puredir)/fontdesign
-fontdesign_DATA = $(addprefix fontdesign/, bezier.pure hobby.pure	\
+fontdesign_DATA = $(addprefix fontdesign/, bezier.pure	\
+	fontforge_python.pure glyph.pure hobby.pure			\
 	line_intersection.pure piecewise.pure)

File fontdesign.pure

 using fontdesign::bezier;
+using fontdesign::fontforge_python;
+using fontdesign::glyph;
 using fontdesign::hobby;
 using fontdesign::line_intersection;
 using fontdesign::piecewise;

File fontdesign/fontforge_python.pure

+using bezier;
+using glyph;
+using piecewise;
+using system;
+
+namespace fontdesign;
+
+cubic_piecewise_to_points pwise = points + last_point
+ when
+   points = foldl (\ pts c -> pts + curve_points c) [] q;
+ end
+ with
+   curve_points c
+   = [Oncurve ((ctrlpoints c)!0),
+      Offcurve ((ctrlpoints c)!1),
+      Offcurve ((ctrlpoints c)!2)] if order c == 3;
+   = [Oncurve ((ctrlpoints c)!0)] if order c == 1;
+   = throw "bad input to cubic_piecewise_to_points" otherwise;
+   last_point
+   = [] if (is_cyclic pwise) || q == [];
+   = [Oncurve (last (ctrlpoints (last q)))] otherwise;
+ end
+ when
+   q = pieces pwise;
+ end;
+
+//-------------------------------------------------------------------------
+
+private new_contour_python;
+new_contour_python contour_var_name =
+  contour_var_name + " = fontforge.contour()\n";
+
+private point_to_python;
+point_to_python contour_var_name (Oncurve p) =
+  contour_var_name + " += fontforge.point("
+  + str (re p) + "," + str (im p) + ")\n";
+point_to_python contour_var_name (Offcurve p) =
+  contour_var_name + " += fontforge.point("
+  + str (re p) + "," + str (im p) + ", False)\n";
+
+private cyclicness_to_python;
+cyclicness_to_python contour_var_name cyclic
+= contour_var_name + ".closed = True\n" if cyclic;
+= contour_var_name + ".closed = False\n" if ~cyclic;
+
+print_python_contour_code contour_var_name output pwise::piecewise =
+  fputs (new_contour_python contour_var_name) output $$
+  foldl (\ _ pt -> fputs (point_to_python contour_var_name pt) output) () points $$
+  fputs (cyclicness_to_python contour_var_name (is_cyclic pwise)) output
+ when
+   points = cubic_piecewise_to_points pwise;
+ end;
+
+/*
+print_python_contour_code contour_var_name output cs::curveset =
+  foldl (\ _ elem -> print elem) () (elements cs)
+ with
+   print pwise::piecewise = print_python_contour_code contour_var_name output pwise;
+   print _ = ();
+ end;
+*/
+
+//-------------------------------------------------------------------------
+
+private new_layer_python;
+new_layer_python layer_var_name =
+  layer_var_name + " = fontforge.layer()\n";
+
+print_python_layer_code layer_var_name contour_var_name output layer::layer =
+  fputs (new_layer_python layer_var_name) output $$
+  foldl (\ _ elem -> print elem) () (elements layer)
+ with
+   print cs::curveset =
+     foldl (\ _ elem -> print elem) () (elements cs);
+   print pwise::piecewise =
+     print_python_contour_code contour_var_name output pwise $$
+     fputs (layer_var_name + " += " + contour_var_name + "\n") output;
+   print _ = ();
+ end;
+
+//-------------------------------------------------------------------------
+
+print_python_glyph_code glyph_var_name layer_var_name contour_var_name output glyph::glyph =
+  foldl (\ _ elem -> print elem) () (filter (typep ('layer)) (elements glyph)) $$
+  fprintf output "%s.canonicalContours()\n" glyph_var_name $$
+  fprintf output "%s.canonicalStart()\n" glyph_var_name
+ with
+   print layer::layer =
+     print_python_layer_code layer_var_name contour_var_name output layer $$
+     fputs (glyph_var_name + ".layers['" + name layer + "'] = " + layer_var_name + "\n") output;
+   print _ = ();
+ end;
+     
+print_python_glyph_update_module output glyph =
+  fputs "import fontforge\n" output $$
+  fputs "import psMat\n" output $$
+  fputs "my_glyph = fontforge.activeGlyph()\n" output $$
+  print_python_glyph_code "my_glyph" "my_layer" "my_contour" output glyph;
+
+//-------------------------------------------------------------------------

File fontdesign/glyph.pure

+namespace fontdesign;
+
+type curveset (Curveset name::string elements::list);
+type layer (Layer name::string elements::list);
+type glyph (Glyph name::string elements::list);
+
+name (Curveset n::string _) = n;
+name (Layer n::string _) = n;
+name (Glyph n::string _) = n;
+
+elements (Curveset _ elem::list) = elem;
+elements (Layer _ elem::list) = elem;
+elements (Glyph _ elem::list) = elem;