Commit d95802fda353e8731d59c34ea0772b09cfd54f3a

Thomas de Grivel 2022-02-12T18:01:48

load glyphs WIP

diff --git a/lib/exterm/font.ex b/lib/exterm/font.ex
index 5ac2e4e..8d9420b 100644
--- a/lib/exterm/font.ex
+++ b/lib/exterm/font.ex
@@ -54,15 +54,21 @@ defmodule Exterm.Font do
   def render_glyph(font, codepoint) do
     ttf = ttf(font.family)
     if glyph = TTF.glyph(ttf, codepoint) do
+      :gl.pushMatrix()
+      :gl.translatef(0.0, 200.0, 0.0)
+      :gl.scalef(0.1, -0.1, 0.1)
       Enum.each(glyph.contours, fn contour ->
         :gl.begin(:gl_const.gl_line_loop)
         :gl.color3f(0.0, 0.0, 0.0)
         Enum.each(TTF.Glyph.contour_segments(contour), fn control_point ->
           IO.inspect(control_point)
-          :gl.vertex2f(control_point.a.x * 0.01, control_point.a.y * 0.01)
+          :gl.vertex2f(control_point.a.x * 1.0, control_point.a.y * 1.0)
+          :gl.vertex2f(control_point.c.x * 1.0, control_point.c.y * 1.0)
         end)
         :gl.end()
       end)
+      :gl.popMatrix()
+      :gl.translatef(100.0, 0.0, 0.0)
     end
   end
 
diff --git a/lib/ttf.ex b/lib/ttf.ex
index bd43db5..3ce2185 100644
--- a/lib/ttf.ex
+++ b/lib/ttf.ex
@@ -216,9 +216,10 @@ defmodule TTF do
     id = codepoint_to_font_index(ttf, codepoint) || 0
     glyf = table(ttf, "glyf")
     location = glyph_location(ttf, id)
-    <<_::binary-size(location), contour_count::signed-big-16, xmin::unsigned-big-16, ymin::unsigned-big-16, xmax::unsigned-big-16, ymax::unsigned-big-16, rest::binary>> = glyf
+    <<_::binary-size(location), contour_count::signed-big-16, xmin::signed-big-16, ymin::signed-big-16, xmax::signed-big-16, ymax::signed-big-16, rest::binary>> = glyf
     IO.inspect([contour_count: contour_count, id: id, location: location])
     glyph = %Glyph{bounding_box: {xmin, ymin, xmax, ymax}, codepoint: codepoint, contour_count: contour_count, ttf: ttf}
+    IO.inspect(glyph)
     if (contour_count == -1) do
       glyph |> Glyph.CompoundContours.parse(rest)
     else
diff --git a/lib/ttf/glyph/simple_contours.ex b/lib/ttf/glyph/simple_contours.ex
index 36c8101..6d828ff 100644
--- a/lib/ttf/glyph/simple_contours.ex
+++ b/lib/ttf/glyph/simple_contours.ex
@@ -13,6 +13,7 @@ defmodule TTF.Glyph.SimpleContours do
 
   def parse(glyph, rest) do
     {contour_endpoint_indexes, rest} = TTF.parse_ub16_array(rest, glyph.contour_count)
+    check_order!(contour_endpoint_indexes, glyph.contour_count)
     n_points = 1 + elem(contour_endpoint_indexes, glyph.contour_count - 1)
     <<instruction_length::unsigned-big-16, _::binary-size(instruction_length), rest::binary>> = rest
     glyph = %Glyph{glyph | contour_endpoint_indexes: contour_endpoint_indexes, n_points: n_points}
@@ -22,6 +23,20 @@ defmodule TTF.Glyph.SimpleContours do
     |> contours()
   end
 
+  defp check_order!(_, size) when size < 2, do: :ok
+  defp check_order!(tuple, size) do
+    ordered? = 0..(size - 2)
+    |> Enum.all?(fn i ->
+      a = elem(tuple, i)
+      b = elem(tuple, i + 1)
+      a < b
+    end)
+    if ! ordered? do
+      raise ArgumentError, "tuple is not ordered: #{tuple}"
+    end
+    :ok
+  end
+
   def flags(glyph, rest) do
     flags(glyph, rest, 0, [])
   end
@@ -30,9 +45,9 @@ defmodule TTF.Glyph.SimpleContours do
     {%Glyph{glyph | flags: flags}, rest}
   end
   def flags(glyph, <<byte, rest::binary>>, contour, acc) do
-    if band(@repeat_flag, byte) do
+    if band(@repeat_flag, byte) != 0 do
       <<repeat_n::unsigned-8, rest::binary>> = rest
-      repeat = 0..(repeat_n - 1)
+      repeat = 1..repeat_n
       |> Enum.map(fn _ -> byte end)
       flags(glyph, rest, contour + 1, repeat ++ acc)
     else