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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
defmodule TTF do
use Bitwise
defstruct bounding_box: nil, cmap: [], cmap_subtable_count: nil, data: nil, glyph_count: nil, glyph_locations: nil, header: nil, loca_offset_format: nil, magic: nil, path: nil, table_count: nil, units_per_em: nil
alias TTF.Glyph
@cmap_platform_id_unicode 0
@cmap_unicode_2_bmp_only 3
@cmap_platform_id_apple 1
@cmap_platform_id_microsoft 3
@cmap_microsoft_unicode_bmp 1
@valid_head_magics [0x5F0F3CF5]
@valid_head_versions [0x00010000]
@valid_magics [0x00010000]
@valid_maxp_versions [0x00010000]
def load(path) do
IO.inspect([TTF, :load, path])
File.read!(path)
|> parse(path)
end
def parse(data, path \\ nil) do
%__MODULE__{data: data, path: path}
|> parse_header()
|> parse_maxp()
|> parse_cmap()
|> parse_head()
|> parse_loca()
|> parse_glyphs()
|> IO.inspect()
end
def parse_header(ttf = %__MODULE__{data: <<magic::unsigned-big-32, rest::binary>>, magic: nil}) do
if ! Enum.find(@valid_magics, & &1 == magic) do
raise ArgumentError, "invalid magic #{magic}"
end
%__MODULE__{ttf | magic: magic}
|> parse_header(rest)
end
def parse_header(ttf = %__MODULE__{header: nil}, <<table_count::unsigned-big-16, _::size(16), _::size(16), _::size(16), rest::binary>>) do
%__MODULE__{ttf | header: %{}, table_count: table_count}
|> parse_header_table(rest, 0)
end
def parse_header(ttf, <<_::binary>>) do
ttf
end
def parse_header_table(ttf = %{table_count: table_count}, <<rest::binary>>, i) when i >= table_count do
ttf
|> parse_header(rest)
end
def parse_header_table(ttf, <<tag0, tag1, tag2, tag3, checksum::unsigned-big-32, offset::unsigned-big-32, size::unsigned-big-32, rest::binary>>, i) do
tag = to_string([tag0, tag1, tag2, tag3])
header = ttf.header
|> Map.put(tag, %{tag: tag, checksum: checksum, offset: offset, size: size})
%__MODULE__{ttf | header: header}
|> parse_header_table(rest, i + 1)
end
def table(ttf, name) do
offset = ttf.header[name].offset
size = ttf.header[name].size
<<_::binary-size(offset), table_data::binary-size(size), _::binary>> = ttf.data
table_data
end
def parse_maxp(ttf) do
maxp = table(ttf, "maxp")
<<version::unsigned-big-32, glyph_count::unsigned-big-16, _::binary>> = maxp
if ! Enum.find(@valid_maxp_versions, & &1 == version) do
raise ArgumentError, "invalid maxp version #{version}"
end
%__MODULE__{ttf | glyph_count: glyph_count}
end
def parse_cmap(ttf) do
cmap = table(ttf, "cmap")
<<_version::unsigned-big-16, subtable_count::unsigned-big-16, rest::binary>> = cmap
%__MODULE__{ttf | cmap_subtable_count: subtable_count, cmap: []}
|> parse_cmap_subtable(cmap, rest, 0)
end
def parse_cmap_subtable(ttf = %__MODULE__{cmap_subtable_count: count}, _, _, i) when i >= count do
ttf
end
def parse_cmap_subtable(ttf, cmap, <<platform_id::unsigned-big-16, platform_specific_id::unsigned-big-16, offset::unsigned-big-32, rest::binary>>, i) do
ttf
|> parse_cmap_subtable(cmap, rest, i, platform_id, platform_specific_id, offset)
end
def parse_cmap_subtable(ttf, cmap, binary, i, @cmap_platform_id_unicode, @cmap_unicode_2_bmp_only, _offset) do
ttf
|> parse_cmap_subtable(cmap, binary, i + 1)
end
def parse_cmap_subtable(ttf, cmap, binary, i, @cmap_platform_id_apple, _, _offset) do
ttf
|> parse_cmap_subtable(cmap, binary, i + 1)
end
def parse_cmap_subtable(ttf, cmap, _binary, i, @cmap_platform_id_microsoft, @cmap_microsoft_unicode_bmp, offset) do
<<_::binary-size(offset), format::unsigned-big-16, rest::binary>> = cmap
if format != 4 do
raise ArgumentError, "invalid cmap subtable format #{format}"
end
table_start_rest_byte_size = byte_size(rest) + 2
<<subtable_length::unsigned-big-16, _language_code::unsigned-big-16, segment_count::unsigned-big-16, _search_range::unsigned-big-16, _entry_selector::unsigned-big-16, _range_shift::unsigned-big-16, rest::binary>> = rest
segment_count = trunc(segment_count / 2)
{end_codes, rest} = parse_ub16_array(rest, segment_count)
<<_::unsigned-16, rest::binary>> = rest
{start_codes, rest} = parse_ub16_array(rest, segment_count)
{id_deltas, rest} = parse_ub16_array(rest, segment_count)
id_deltas = id_deltas
|> Tuple.to_list()
|> Enum.map(&ub16_to_signed/1)
|> List.to_tuple()
{id_range_offsets, rest} = parse_ub16_array(rest, segment_count)
glyph_index_array_size = trunc((subtable_length - (table_start_rest_byte_size - byte_size(rest))) / 2)
glyph_indexes = parse_ub16_array(rest, glyph_index_array_size)
subtable = %{platform_id: @cmap_platform_id_microsoft, platform_specific_id: @cmap_microsoft_unicode_bmp, segment_count: segment_count, end_codes: end_codes, start_codes: start_codes, id_deltas: id_deltas, id_range_offsets: id_range_offsets, glyph_indexes: glyph_indexes}
%__MODULE__{ttf | cmap: [subtable | ttf.cmap]}
|> parse_cmap_subtable(cmap, rest, i + 1)
end
def parse_ub16_array(binary, size), do: parse_ub16_array(binary, size, 0, [])
def parse_ub16_array(rest, size, i, acc) when i >= size do
{acc |> Enum.reverse() |> List.to_tuple(), rest}
end
def parse_ub16_array(<<first::unsigned-big-16, rest::binary>>, size, i, acc) do
parse_ub16_array(rest, size, i + 1, [first | acc])
end
def ub16_to_signed(ub16) do
if (band(bsl(1, 15), ub16) != 0) do
- band(0xFFFF, bnot(ub16)) - 1
else
ub16
end
end
def parse_head(ttf) do
head = table(ttf, "head")
<<version::unsigned-big-32, _::binary-size(8), magic::unsigned-big-32, rest::binary>> = head
if ! Enum.find(@valid_head_versions, & &1 == version) do
raise ArgumentError, "invalid head version #{version}"
end
if ! Enum.find(@valid_head_magics, & &1 == magic) do
raise ArgumentError, "invalid head magic #{magic}"
end
<<_::binary-size(2), units_per_em::unsigned-big-16, _::binary-size(16), bb1::unsigned-big-16, bb2::unsigned-big-16, bb3::unsigned-big-16, bb4::unsigned-big-16, _::binary-size(6), loca_offset_format::unsigned-big-16, _::binary>> = rest
loca_offset_format = if loca_offset_format == 0, do: :short, else: :long
%__MODULE__{ttf | units_per_em: units_per_em, bounding_box: {bb1, bb2, bb3, bb4}, loca_offset_format: loca_offset_format}
end
def parse_loca(ttf) do
loca = table(ttf, "loca")
ttf
|> parse_loca(loca, 0, ttf.glyph_count + 1, [])
end
def parse_loca(ttf, _, i, max, acc) when (i >= max) do
glyph_locations = acc |> Enum.reverse() |> List.to_tuple()
%__MODULE__{ttf | glyph_locations: glyph_locations}
end
def parse_loca(ttf, loca, i, max, acc) do
if ttf.loca_offset_format == :short do
<<first::unsigned-big-16, rest::binary>> = loca
parse_loca(ttf, rest, i + 1, max, [first * 2 | acc])
else
<<first::unsigned-big-32, rest::binary>> = loca
parse_loca(ttf, rest, i + 1, max, [first | acc])
end
end
def parse_glyphs(ttf) do
ttf
end
def codepoint_to_font_index(font, codepoint) do
codepoint_to_font_index(font, codepoint, 0, hd(font.cmap).segment_count)
end
def codepoint_to_font_index(_font, _codepoint, i, segment_count) when i >= segment_count do
nil
end
def codepoint_to_font_index(font, codepoint, i, _segment_count) do
start_code = elem(hd(font.cmap).start_codes, i)
end_code = elem(hd(font.cmap).end_codes, i)
if start_code <= codepoint && codepoint <= end_code do
id_range_offset = elem(hd(font.cmap).id_range_offsets, i)
id_delta = elem(hd(font.cmap).id_deltas, i)
if (id_range_offset == 0) do
band(0xFFFF, (codepoint + id_delta))
else
glyph_index_offset = i + bsr(id_range_offset, 1) + codepoint - start_code - hd(font.cmap).segment_count
glyph_index = elem(hd(font.cmap).glyph_indexes, glyph_index_offset)
band(0xFFFF, (glyph_index + id_delta))
end
else
nil
end
end
def glyph_location(ttf, index) do
elem(ttf.glyph_locations, index)
end
def has_glyph?(ttf, codepoint) do
if codepoint_to_font_index(ttf, codepoint) do
true
else
false
end
end
def glyph(ttf, codepoint) 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
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}
if (contour_count == -1) do
glyph |> Glyph.CompoundContours.parse(rest)
else
glyph |> Glyph.SimpleContours.parse(rest)
end
end
end