Hash :
47faab2d
Author :
Thomas de Grivel
Date :
2022-02-12T10:55:20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
defmodule TTF.Glyph do
defstruct bounding_box: nil, codepoint: nil, contour_count: nil, contour_endpoint_indexes: nil, contours: nil, control_points: nil, flags: nil, n_points: nil, name: nil, ttf: nil
def contour_segments([]) do
[]
end
def contour_segments(contour) do
length = Enum.count(contour)
i = 1
stack = nil
next = nil
start = Enum.at(contour, 0)
mid = nil
end_ = nil
segments = []
contour_segments(contour, length, i, stack, next, start, mid, end_, segments)
end
def contour_segments(contour, length, i, stack, next, start, mid, end_, segments) do
mid = nil
{next, i} = contour_next_point(contour, length, i)
if next == nil do
mid = stack
end_ = Enum.at(contour, 0)
contour_segments_body(contour, length, i, stack, next, start, mid, end_, segments)
else
if next.on_curve? do
end_ = next
mid = stack
stack = nil
contour_segments_body(contour, length, i, stack, next, start, mid, end_, segments)
else
if stack != nil do
mid = stack
end_ = midpoint(stack, next)
stack = next
contour_segments_body(contour, length, i, stack, next, start, mid, end_, segments)
else
stack = next
contour_segments(contour, length, i, stack, next, start, mid, end_, segments)
end
end
end
end
defp contour_segments_body(contour, length, i, stack, next, start, mid, end_, segments) do
segment = %{a: start, b: mid, c: end_}
if next != nil do
start = end_
contour_segments(contour, length, i, stack, next, start, mid, end_, [segment | segments])
else
segments |> Enum.reverse()
end
end
defp contour_next_point(contour, length, i) do
if i < length do
{Enum.at(contour, i), i + 1}
else
{nil, i}
end
end
defp midpoint(p0, p1) do
%TTF.ControlPoint{x: (p0.x + p1.x) / 2.0,
y: (p0.y + p1.y) / 2.0,
on_curve?: true}
end
end